Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-js

Source of release 0.6.0

0.6.0 / src / engine / parser_class_fields.lucb

#==============================================================================================
#
#   parser_class_fields - Left-hand side expressions and the class field initializers
#
#   DESCRIPTION:
#       Port of quickjs.c lines 25104-25273: js_parse_left_hand_side_expr, the default
#       constructor, the private class field names, and the field initializer functions
#       (ClassFieldsDef, emit_class_init_start/end, emit_class_field_init) that
#       js_parse_class (parser_class.lucb) fills.
#
#==============================================================================================

# mark: Helpers =================================================================================

func js_parse_left_hand_side_expr(s: ParseState*) -> !:
    try js_parse_postfix_expr(s, pf_postfix_call)

# Compile the implicit constructor of a class that declares none (a derived one calls the
# parent's constructor); `*pfd` is its function.
func js_parse_class_default_ctor(s: ParseState*, has_super: bool, pfd: FunctionDef*?*?) -> !:
    let fd = try js_new_function_def(s.ctx, s.fd(), false, false, s.filename, s.token.ptr,
                                     &s.get_line_col_cache)

    s.cur_func = fd
    fd.has_home_object = true
    fd.super_allowed = true
    fd.has_prototype = false
    fd.has_this_binding = true
    fd.new_target_allowed = true

    # enter body scope (its failure is ignored, as in QuickJS)
    _ = push_scope(s) catch failure: recover -1
    fd.body_scope = fd.scope_level
    var func_type = ParseFunctionKind.class_constructor
    if has_super:
        fd.is_derived_class_constructor = true
        fd.super_call_allowed = true
        fd.arguments_allowed = true
        fd.has_arguments_binding = true
        func_type = ParseFunctionKind.derived_class_constructor
        emit_op(s, (u8)Op.init_ctor)
        # TODO(bnoordhuis) roll into OP_init_ctor
        emit_op(s, (u8)TempOp.scope_put_var_init)
        emit_atom(s, atom_this)
        emit_u16(s, 0)
        emit_class_field_init(s)
    else:
        # error if not invoked as a constructor
        emit_op(s, (u8)Op.check_ctor)
        emit_class_field_init(s)

    fd.func_kind = js_func_normal
    fd.func_type = func_type
    emit_return(s, false)

    s.cur_func = fd.parent
    if let p = pfd:
        *p = fd

    # the real object will be set at the end of the compilation (a failure to add it is
    # ignored, as in QuickJS)
    let idx = cpool_add(s, js_null()) catch failure: recover -1
    fd.parent_cpool_idx = idx

# find field in the current scope
func find_private_class_field(ctx: Context*, fd: FunctionDef*, name: Atom, scope_level: i32) -> i32:
    var idx = fd_scope(fd, scope_level).first
    while idx != -1:
        let vd = fd_var(fd, idx)
        if vd.scope_level != scope_level:
            break
        if vd.var_name == name:
            return idx
        idx = vd.scope_next
    return -1

# initialize the class fields, called by the constructor. Note: super() can be called in
# an arrow function, so <this> and <class_fields_init> can be variable references
func emit_class_field_init(s: ParseState*):
    emit_op(s, (u8)TempOp.scope_get_var)
    emit_atom(s, atom_class_fields_init)
    emit_u16(s, (u16)s.fd().scope_level)

    # no need to call the class field initializer if not defined
    emit_op(s, (u8)Op.dup)
    let label_next = emit_goto(s, (i32)Op.if_false, -1)

    emit_op(s, (u8)TempOp.scope_get_var)
    emit_atom(s, atom_this)
    emit_u16(s, 0)

    emit_op(s, (u8)Op.swap)

    emit_op(s, (u8)Op.call_method)
    emit_u16(s, 0)

    _ = emit_label(s, label_next)
    emit_op(s, (u8)Op.drop)

# build a private setter function name from the private getter name
func get_private_setter_name(ctx: Context*, name: Atom) -> Atom!:
    return try js_atom_concat_str(ctx, name, "<set>")

# mark: Field initializers ======================================================================

## The instance (index 0) or static (index 1) field initializer of a class being parsed
## (ClassFieldsDef).
struct ClassFieldsDef:
    var fields_init_fd: FunctionDef*?
    var computed_fields_count: i32
    var need_brand: bool
    var brand_push_pos: i32
    var is_static: bool

# Create the field initializer function of `cf` and emit its prologue.
func emit_class_init_start(s: ParseState*, cf: ClassFieldsDef*) -> !:
    let init_fd = try js_parse_function_class_fields_init(s)
    cf.fields_init_fd = init_fd

    s.cur_func = init_fd

    if not cf.is_static:
        # add the brand to the newly created instance
        # XXX: would be better to add the code only if needed, maybe in a later pass
        # will be patched later
        emit_op(s, (u8)Op.push_false)
        cf.brand_push_pos = init_fd.last_opcode_pos
        let label_add_brand = emit_goto(s, (i32)Op.if_false, -1)

        emit_op(s, (u8)TempOp.scope_get_var)
        emit_atom(s, atom_this)
        emit_u16(s, 0)

        emit_op(s, (u8)TempOp.scope_get_var)
        emit_atom(s, atom_home_object)
        emit_u16(s, 0)

        emit_op(s, (u8)Op.add_brand)

        _ = emit_label(s, label_add_brand)
    s.cur_func = s.fd().parent

# Finish the field initializer function of `cf` and push its closure.
func emit_class_init_end(s: ParseState*, cf: ClassFieldsDef*):
    let init_fd = cf.fields_init_fd else trap("emit_class_init_end: no initializer")
    s.cur_func = init_fd
    emit_op(s, (u8)Op.return_undef)
    s.cur_func = s.fd().parent

    # a failure to add the constant is ignored, as in QuickJS
    let cpool_idx = cpool_add(s, js_null()) catch failure: recover -1
    init_fd.parent_cpool_idx = cpool_idx
    emit_op(s, (u8)Op.fclosure)
    emit_u32(s, (u32)cpool_idx)
    emit_op(s, (u8)Op.set_home_object)