dymokomi/luce-js
Source of release 0.6.1
0.6.1 / src / engine / arguments_objects.lucb
#==============================================================================================
#
# arguments_objects - The arguments objects: unmapped, and mapped onto the function's variables
#
# DESCRIPTION:
# Port of quickjs.c lines 16131-16266. An arguments object is a fast array: the
# unmapped kind holds the argument values, the mapped (sloppy mode) kind holds a
# variable reference per argument, shared with the function's own variables.
# Redefining an index turns it into a plain array first (the exotic
# define_own_property).
#
#==============================================================================================
# mark: The exotic methods ======================================================================
## The define_own_property of arguments objects: convert to a normal array when redefining
## an existing numeric field, then define it the ordinary way.
func js_arguments_define_own_property(ctx: Context*, this_obj: Value, prop: Atom, val: Value, getter: Value, setter: Value, flags: i32) -> bool!:
let p = this_obj.u.obj
var idx: u32 = 0
# convert to normal array when redefining an existing numeric field
if p.fast_array and js_atom_is_array_index(ctx, &idx, prop) and idx < p.u.array.count:
try convert_fast_array_to_array(ctx, p)
# run the default define own property
return try js_define_property(ctx, this_obj, prop, val, getter, setter,
flags | prop_no_exotic)
## The exotic methods of ClassId.arguments and ClassId.mapped_arguments.
let js_arguments_exotic_methods: ClassExoticMethods = ClassExoticMethods(
define_own_property = js_arguments_define_own_property,
)
# mark: Unmapped arguments ======================================================================
## A new unmapped arguments object holding copies of `argv[0..argc]` (strict mode).
func js_build_arguments(ctx: Context*, argc: i32, argv: Value*) -> Value!:
var props: Property[3] = ---
props[0].u.value = js_new_int32(argc) # length
props[1].u.value = js_dup_value(ctx, ctx.array_proto_values) # Symbol.iterator
props[2].u.getset.getter = js_dup_value(ctx, ctx.throw_type_error).u.obj # callee
props[2].u.getset.setter = js_dup_value(ctx, ctx.throw_type_error).u.obj # callee
let arguments_shape = ctx.arguments_shape else trap("js_build_arguments: no arguments shape")
let val = try js_new_object_from_shape(ctx, js_dup_shape(arguments_shape),
(u32)ClassId.arguments, &props[0])
let p = val.u.obj
# initialize the fast array part
p.u.array.u.ptr = none
if argc > 0:
let mem = js_malloc(ctx, memory.size_of(Value) * (usize)argc) catch failure:
js_free_value(ctx, val)
error(failure.code, failure.message)
let tab = (Value*)mem
for i in 0..<(usize)argc:
tab[i] = js_dup_value(ctx, argv[i])
p.u.array.u.values = tab
p.u.array.count = (u32)argc
return val
## Offsets the parser adds to a variable index to mark a global variable and an argument.
let global_var_offset: i32 = 0x40000000
let argument_var_offset: i32 = 0x20000000
# mark: Mapped arguments ========================================================================
## The finalizer of mapped arguments objects: release the variable references.
func js_mapped_arguments_finalizer(rt: Runtime*, val: Value):
let p = val.u.obj
if let tab = p.u.array.u.ptr:
let var_refs = (VarRef**)tab
for i in 0..<(usize)p.u.array.count:
free_var_ref(rt, var_refs[i])
js_free_rt(rt, tab)
## The GC marking of mapped arguments objects: their variable references.
func js_mapped_arguments_mark(rt: Runtime*, val: Value, mark_func: MarkFunc):
let p = val.u.obj
if let tab = p.u.array.u.ptr:
let var_refs = (VarRef**)tab
for i in 0..<(usize)p.u.array.count:
mark_func(rt, &var_refs[i].header)
## Release the first `count` references of a mapped arguments table and the table (the
## `fail1:` label of js_build_mapped_arguments).
func js_free_var_ref_tab(ctx: Context*, tab: VarRef**, count: i32):
for j in 0..<(usize)count:
free_var_ref(ctx.rt, tab[j])
js_free(ctx, tab)
# legacy arguments object: add references to the function arguments
## A new mapped arguments object: the first `arg_count` arguments are references to the
## frame's argument variables, the others hold copies of `argv`.
func js_build_mapped_arguments(ctx: Context*, argc: i32, argv: Value*, sf: StackFrame*, arg_count: i32) -> Value!:
let cur_sf = ctx.rt.current_stack_frame else trap("js_build_mapped_arguments: no frame")
var props: Property[3] = ---
props[0].u.value = js_new_int32(argc) # length
props[1].u.value = js_dup_value(ctx, ctx.array_proto_values) # Symbol.iterator
props[2].u.value = js_dup_value(ctx, cur_sf.cur_func) # callee
let mapped_arguments_shape = ctx.mapped_arguments_shape else trap("js_build_mapped_arguments: no shape")
let val = try js_new_object_from_shape(ctx, js_dup_shape(mapped_arguments_shape),
(u32)ClassId.mapped_arguments, &props[0])
errdefer js_free_value(ctx, val)
let p = val.u.obj
# initialize the fast array part
p.u.array.u.ptr = none
if argc > 0:
let tab = (VarRef**)(try js_malloc(ctx, memory.size_of(VarRef*) * (usize)argc))
var i: i32 = 0
while i < arg_count:
let var_ref = get_var_ref(ctx, sf, i, true) catch failure:
js_free_var_ref_tab(ctx, tab, i)
error(failure.code, failure.message)
tab[(usize)i] = var_ref
i += 1
while i < argc:
let var_ref = js_create_var_ref(ctx, false) catch failure:
js_free_var_ref_tab(ctx, tab, i)
error(failure.code, failure.message)
var_ref.u.value = js_dup_value(ctx, argv[(usize)i])
tab[(usize)i] = var_ref
i += 1
p.u.array.u.var_refs = tab
p.u.array.count = (u32)argc
return val