Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.1

0.6.1 / src / engine / interp_ops_properties.lucb

#==============================================================================================
#
#   interp_ops_properties - Opcodes: fields, private fields, methods, classes and elements
#
#   DESCRIPTION:
#       Port of the opcode bodies of JS_CallInternal, quickjs.c lines 19163-19695:
#       OP_get_field to OP_copy_data_properties. Each body is a function taking the
#       interpreter state (InterpState, see interpreter_support.lucb); the dispatch loop calls
#       it from its arm. A body that can raise answers false with the exception pending, which
#       the loop treats as C's `goto exception`; one that cannot raise answers nothing.
#
#==============================================================================================

# mark: Fields ==================================================================================

## The body of OP_get_field in JS_CallInternal.
noinline func interp_get_field(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    st.pc += 4
    let val = get_field_inline(st.ctx, st.sf, st.pc, st.sp[-1], atom) catch: return false
    js_free_value(st.ctx, st.sp[-1])
    st.sp[-1] = val
    return true

## The body of OP_get_field2 in JS_CallInternal.
noinline func interp_get_field2(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    st.pc += 4
    let val = get_field_inline(st.ctx, st.sf, st.pc, st.sp[-1], atom) catch: return false
    *st.sp = val
    st.sp += 1
    return true

## The body of OP_get_length in JS_CallInternal.
noinline func interp_get_length(st: InterpState*) -> bool:
    let val = get_field_inline(st.ctx, st.sf, st.pc, st.sp[-1], atom_length) catch: return false
    js_free_value(st.ctx, st.sp[-1])
    st.sp[-1] = val
    return true

## The slow path of GET_FIELD_INLINE for OP_get_field, OP_get_field2 and OP_get_length
## (st.opcode; st.pc is past the atom): JS_GetPropertyInternal from the exotic object
## `exotic` the prototype walk stopped at, or else from the operand. The dispatch loop
## comes here directly when its walk does not find a data property.
noinline func interp_get_field_slow(st: InterpState*, exotic: Object*?, atom: Atom) -> bool:
    var obj = st.sp[-1]
    if let p = exotic:
        obj = js_mkptr(Tag.object, p)
    st.sf.cur_pc = st.pc
    let val = js_get_property_internal(st.ctx, obj, atom, st.sp[-1], false) catch: return false
    if st.opcode == Op.get_field2:
        *st.sp = val
        st.sp += 1
    else:
        js_free_value(st.ctx, st.sp[-1])
        st.sp[-1] = val
    return true

## The body of OP_put_field in JS_CallInternal.
noinline func interp_put_field(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    st.pc += 4

    let obj = st.sp[-2]
    var fast = false
    if obj.tag == Tag.object:
        var pr: Property*? = none
        if let prs = find_own_property(&pr, obj.u.obj, atom):
            if ((i32)prs.flags & (prop_tmask | prop_writable | prop_length)) == prop_writable:
                # fast path
                let found = pr else trap("find_own_property found no property")
                set_value(st.ctx, &found.u.value, st.sp[-1])
                fast = true
    if fast:
        js_free_value(st.ctx, obj)
        st.sp -= 2
        return true
    return interp_put_field_slow(st, atom)

## put_field_slow_path of OP_put_field (st.pc is past the atom): JS_SetPropertyInternal.
## The dispatch loop's put_field comes here directly when its own-property search misses.
noinline func interp_put_field_slow(st: InterpState*, atom: Atom) -> bool:
    let obj = st.sp[-2]
    st.sf.cur_pc = st.pc
    let ret = bool_status(js_set_property_internal(st.ctx, obj, atom, st.sp[-1], obj, prop_throw_strict) catch: recover none)
    js_free_value(st.ctx, obj)
    st.sp -= 2
    if ret < 0:
        return false
    return true

# mark: Private fields, names, methods and classes ==============================================

## The body of OP_private_symbol in JS_CallInternal.
noinline func interp_private_symbol(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    st.pc += 4
    let val = js_new_symbol_from_atom(st.ctx, atom, js_atom_type_private) catch: return false
    *st.sp = val
    st.sp += 1
    return true

## The body of OP_get_private_field in JS_CallInternal.
noinline func interp_get_private_field(st: InterpState*) -> bool:
    let val = js_get_private_field(st.ctx, st.sp[-2], st.sp[-1]) catch: recover js_exception()
    js_free_value(st.ctx, st.sp[-1])
    js_free_value(st.ctx, st.sp[-2])
    st.sp[-2] = val
    st.sp -= 1
    if js_is_exception(val):
        return false
    return true

## The body of OP_put_private_field in JS_CallInternal.
noinline func interp_put_private_field(st: InterpState*) -> bool:
    var ret: i32 = 0
    js_set_private_field(st.ctx, st.sp[-3], st.sp[-1], st.sp[-2]) catch:
        ret = -1
        recover ()
    js_free_value(st.ctx, st.sp[-3])
    js_free_value(st.ctx, st.sp[-1])
    st.sp -= 3
    if ret < 0:
        return false
    return true

## The body of OP_define_private_field in JS_CallInternal.
noinline func interp_define_private_field(st: InterpState*) -> bool:
    var ret: i32 = 0
    js_define_private_field(st.ctx, st.sp[-3], st.sp[-2], st.sp[-1]) catch:
        ret = -1
        recover ()
    js_free_value(st.ctx, st.sp[-2])
    st.sp -= 2
    if ret < 0:
        return false
    return true

## The body of OP_define_field in JS_CallInternal.
noinline func interp_define_field(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    st.pc += 4

    _ = js_define_property_value(st.ctx, st.sp[-2], atom, st.sp[-1], prop_c_w_e | prop_throw) catch:
        st.sp -= 1
        return false
    st.sp -= 1
    return true

## The body of OP_set_name in JS_CallInternal.
noinline func interp_set_name(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    st.pc += 4

    js_define_object_name(st.ctx, st.sp[-1], atom, prop_configurable) catch: return false
    return true

## The body of OP_set_name_computed in JS_CallInternal.
noinline func interp_set_name_computed(st: InterpState*) -> bool:
    js_define_object_name_computed(st.ctx, st.sp[-1], st.sp[-2], prop_configurable) catch: return false
    return true

## The body of OP_set_proto in JS_CallInternal.
noinline func interp_set_proto(st: InterpState*) -> bool:
    st.sf.cur_pc = st.pc
    let proto = st.sp[-1]
    if js_is_object(proto) or js_is_null(proto):
        _ = js_set_prototype_internal(st.ctx, st.sp[-2], proto, true) catch: return false
    js_free_value(st.ctx, proto)
    st.sp -= 1
    return true

## The body of OP_set_home_object in JS_CallInternal.
noinline func interp_set_home_object(st: InterpState*):
    js_method_set_home_object(st.ctx, st.sp[-1], st.sp[-2])

## The body of OP_define_method, OP_define_method_computed in JS_CallInternal.
noinline func interp_define_method(st: InterpState*) -> bool:
    let is_computed = st.opcode == Op.define_method_computed
    var atom: Atom = no_atom
    if is_computed:
        atom = js_value_to_atom(st.ctx, st.sp[-2]) catch: return false
        # opcode += OP_define_method - OP_define_method_computed
        st.opcode = Op.define_method
    else:
        atom = cutils.get_u32(st.pc)
        st.pc += 4
    var op_flags = (i32)*st.pc
    st.pc += 1

    let obj = st.sp[(isize)(-2 - (i32)is_computed)]
    var prop_flags = prop_has_configurable | prop_configurable | prop_has_enumerable | prop_throw
    if (op_flags & op_define_method_enumerable) != 0:
        prop_flags |= prop_enumerable
    op_flags &= 3
    var value = js_undefined()
    var getter = js_undefined()
    var setter = js_undefined()
    if op_flags == op_define_method_method:
        value = st.sp[-1]
        prop_flags |= prop_has_value | prop_has_writable | prop_writable
    elif op_flags == op_define_method_getter:
        getter = st.sp[-1]
        prop_flags |= prop_has_get
    else:
        setter = st.sp[-1]
        prop_flags |= prop_has_set
    var ret: i32 = 0
    js_method_set_properties(st.ctx, st.sp[-1], atom, prop_flags, obj) catch:
        ret = -1
        recover ()
    if ret >= 0:
        ret = bool_status(js_define_property(st.ctx, obj, atom, value, getter, setter, prop_flags) catch: recover none)
    js_free_value(st.ctx, st.sp[-1])
    if is_computed:
        js_free_atom(st.ctx, atom)
        js_free_value(st.ctx, st.sp[-2])
    st.sp -= (usize)(1 + (i32)is_computed)
    if ret < 0:
        return false
    return true

## The body of OP_define_class, OP_define_class_computed in JS_CallInternal.
noinline func interp_define_class(st: InterpState*) -> bool:
    let atom = cutils.get_u32(st.pc)
    let class_flags = (i32)st.pc[4]
    st.pc += 5
    js_op_define_class(st.ctx, st.sp, atom, class_flags, st.var_refs, st.sf, st.opcode == Op.define_class_computed) catch: return false
    return true

# mark: Elements and references =================================================================

## The body of OP_get_array_el in JS_CallInternal.
noinline func interp_get_array_el(st: InterpState*) -> bool:
    # GET_ARRAY_EL_INLINE(get_array_el, 0)
    let val = get_array_el_inline(st.ctx, st.sf, st.pc, st.sp[-2], st.sp[-1]) catch:
        st.sp -= 1
        return false
    js_free_value(st.ctx, st.sp[-2])
    st.sp[-2] = val
    st.sp -= 1
    return true

## The body of OP_get_array_el2 in JS_CallInternal.
noinline func interp_get_array_el2(st: InterpState*) -> bool:
    # GET_ARRAY_EL_INLINE(get_array_el2, 1)
    let val = get_array_el_inline(st.ctx, st.sf, st.pc, st.sp[-2], st.sp[-1]) catch:
        st.sp[-1] = js_undefined()
        return false
    st.sp[-1] = val
    return true

## The body of OP_get_array_el3 in JS_CallInternal.
noinline func interp_get_array_el3(st: InterpState*) -> bool:
    var val = js_undefined()
    var fast = false
    if st.sp[-2].tag == Tag.object and st.sp[-1].tag == Tag.int:
        let arr = st.sp[-2].u.obj
        let idx = (u32)js_value_get_int(st.sp[-1])
        if arr.class_id == ClassId.array and idx < arr.u.array.count:
            val = js_dup_value(st.ctx, arr.u.array.u.values[(usize)idx])
            fast = true
    if not fast:
        # get_array_el3_slow_path
        let key_tag = st.sp[-1].tag
        # for int, string and symbol keys, undefined and null are
        # tested in JS_GetPropertyValue()
        if not (key_tag == Tag.int or key_tag == Tag.string or key_tag == Tag.symbol):
            # must be tested before JS_ToPropertyKey
            if js_is_undefined(st.sp[-2]) or js_is_null(st.sp[-2]):
                js_throw_type_error(st.ctx, "value has no property") catch: return false
            st.sf.cur_pc = st.pc
            st.ret_val = js_to_property_key(st.ctx, st.sp[-1]) catch: return false
            js_free_value(st.ctx, st.sp[-1])
            st.sp[-1] = st.ret_val
        st.sf.cur_pc = st.pc
        val = js_get_property_value(st.ctx, st.sp[-2], js_dup_value(st.ctx, st.sp[-1])) catch: return false
    *st.sp = val
    st.sp += 1
    return true

## The body of OP_get_ref_value in JS_CallInternal.
noinline func interp_get_ref_value(st: InterpState*) -> bool:
    st.sf.cur_pc = st.pc
    let atom = js_value_to_atom(st.ctx, st.sp[-1]) catch: return false
    if js_is_undefined(st.sp[-2]):
        js_throw_reference_error_not_defined(st.ctx, atom) catch:
            js_free_atom(st.ctx, atom)
            return false
    let ret = bool_status(js_has_property(st.ctx, st.sp[-2], atom) catch: recover none)
    var val = js_undefined()
    if ret <= 0:
        if ret < 0:
            js_free_atom(st.ctx, atom)
            return false
        if is_strict_mode(st.ctx):
            js_throw_reference_error_not_defined(st.ctx, atom) catch:
                js_free_atom(st.ctx, atom)
                return false
        val = js_undefined()
    else:
        val = js_get_property(st.ctx, st.sp[-2], atom) catch: recover js_exception()
    js_free_atom(st.ctx, atom)
    if js_is_exception(val):
        return false
    st.sp[0] = val
    st.sp += 1
    return true

## The body of OP_get_super_value in JS_CallInternal.
noinline func interp_get_super_value(st: InterpState*) -> bool:
    st.sf.cur_pc = st.pc
    let atom = js_value_to_atom(st.ctx, st.sp[-1]) catch: return false
    let val = js_get_property_internal(st.ctx, st.sp[-2], atom, st.sp[-3], false) catch: recover js_exception()
    js_free_atom(st.ctx, atom)
    if js_is_exception(val):
        return false
    js_free_value(st.ctx, st.sp[-1])
    js_free_value(st.ctx, st.sp[-2])
    js_free_value(st.ctx, st.sp[-3])
    st.sp[-3] = val
    st.sp -= 2
    return true

## The body of OP_put_array_el in JS_CallInternal.
noinline func interp_put_array_el(st: InterpState*) -> bool:
    var fast = false
    if st.sp[-3].tag == Tag.object and st.sp[-2].tag == Tag.int:
        let arr = st.sp[-3].u.obj
        let idx = (u32)js_value_get_int(st.sp[-2])
        if arr.class_id == ClassId.array:
            if idx >= arr.u.array.count:
                if idx == arr.u.array.count and arr.fast_array and can_extend_fast_array(arr) and arr.prop[0].u.value.tag == Tag.int:
                    # cannot overflow otherwise the length would not be an integer
                    let new_len = idx + 1
                    if new_len <= arr.u.array.u1.size:
                        let array_len = (u32)js_value_get_int(arr.prop[0].u.value)
                        var length_ok = true
                        if new_len > array_len:
                            if ((i32)get_shape_prop(arr.shape).flags & prop_writable) == 0:
                                length_ok = false
                            else:
                                arr.prop[0].u.value = js_new_int32((i32)new_len)
                        if length_ok:
                            arr.u.array.count = new_len
                            arr.u.array.u.values[(usize)idx] = st.sp[-1]
                            fast = true
            else:
                set_value(st.ctx, arr.u.array.u.values + (usize)idx, st.sp[-1])
                fast = true
    if fast:
        js_free_value(st.ctx, st.sp[-3])
        st.sp -= 3
    else:
        # put_array_el_slow_path
        st.sf.cur_pc = st.pc
        let ret = bool_status(js_set_property_value(st.ctx, st.sp[-3], st.sp[-2], st.sp[-1], prop_throw_strict) catch: recover none)
        js_free_value(st.ctx, st.sp[-3])
        st.sp -= 3
        if ret < 0:
            return false
    return true

## The body of OP_put_ref_value in JS_CallInternal.
noinline func interp_put_ref_value(st: InterpState*) -> bool:
    st.sf.cur_pc = st.pc
    let atom = js_value_to_atom(st.ctx, st.sp[-2]) catch: return false
    if js_is_undefined(st.sp[-3]):
        if is_strict_mode(st.ctx):
            js_throw_reference_error_not_defined(st.ctx, atom) catch:
                js_free_atom(st.ctx, atom)
                return false
        else:
            st.sp[-3] = js_dup_value(st.ctx, st.ctx.global_obj)
    var ret = bool_status(js_has_property(st.ctx, st.sp[-3], atom) catch: recover none)
    if ret <= 0:
        if ret < 0:
            js_free_atom(st.ctx, atom)
            return false
        if is_strict_mode(st.ctx):
            js_throw_reference_error_not_defined(st.ctx, atom) catch:
                js_free_atom(st.ctx, atom)
                return false
    ret = bool_status(js_set_property_internal(st.ctx, st.sp[-3], atom, st.sp[-1], st.sp[-3], prop_throw_strict) catch: recover none)
    js_free_atom(st.ctx, atom)
    js_free_value(st.ctx, st.sp[-2])
    js_free_value(st.ctx, st.sp[-3])
    st.sp -= 3
    if ret < 0:
        return false
    return true

## The body of OP_put_super_value in JS_CallInternal.
noinline func interp_put_super_value(st: InterpState*) -> bool:
    st.sf.cur_pc = st.pc
    if st.sp[-3].tag != Tag.object:
        js_throw_type_error_not_an_object(st.ctx) catch: return false
    let atom = js_value_to_atom(st.ctx, st.sp[-2]) catch: return false
    let ret = bool_status(js_set_property_internal(st.ctx, st.sp[-3], atom, st.sp[-1], st.sp[-4], prop_throw_strict) catch: recover none)
    js_free_atom(st.ctx, atom)
    js_free_value(st.ctx, st.sp[-4])
    js_free_value(st.ctx, st.sp[-3])
    js_free_value(st.ctx, st.sp[-2])
    st.sp -= 4
    if ret < 0:
        return false
    return true

## The body of OP_define_array_el in JS_CallInternal.
noinline func interp_define_array_el(st: InterpState*) -> bool:
    let ret = bool_status(js_define_property_value_value(st.ctx, st.sp[-3], js_dup_value(st.ctx, st.sp[-2]), st.sp[-1], prop_c_w_e | prop_throw) catch: recover none)
    st.sp -= 1
    if ret < 0:
        return false
    return true

## The body of OP_append in JS_CallInternal.
# array pos enumobj -- array pos
noinline func interp_append(st: InterpState*) -> bool:
    st.sf.cur_pc = st.pc
    js_append_enumerate(st.ctx, st.sp) catch: return false
    st.sp -= 1
    js_free_value(st.ctx, *st.sp)
    return true

## The body of OP_copy_data_properties in JS_CallInternal.
# target source excludeList
noinline func interp_copy_data_properties(st: InterpState*) -> bool:
    # stack offsets (-1 based):
    # 2 bits for target,
    # 3 bits for source,
    # 2 bits for exclusionList
    let mask = (i32)*st.pc
    st.pc += 1
    st.sf.cur_pc = st.pc
    js_copy_data_properties(st.ctx, st.sp[(isize)(-1 - (mask & 3))], st.sp[(isize)(-1 - ((mask >> 2) & 7))], st.sp[(isize)(-1 - ((mask >> 5) & 7))], false) catch: return false
    return true