dymokomi/luce-js
Source of release 0.6.1
0.6.1 / src / engine / interpreter_support.lucb
#==============================================================================================
#
# interpreter_support - The interpreter's constants and its inline opcode helpers
#
# DESCRIPTION:
# The constants defined inside JS_CallInternal (quickjs.c lines 17746-20704): the
# OP_throw_error kinds and the OP_define_method flags, and how the dispatch loop
# leaves a frame. The interpreter's state (InterpState), the record its opcode
# bodies share. (The call flags, OPSpecialObjectEnum and FUNC_RET_*, which sit
# just before the interpreter, are in calls.lucb.)
#
# Then the bodies of QuickJS's interpreter macros that are too large to repeat in
# every opcode arm (GET_FIELD_INLINE, GET_ARRAY_EL_INLINE, OP_CMP_EQ and
# OP_CMP_STRICT_EQ) as `inline` functions: the macro's parameters that are values
# (`keep`, `inv`) stay with the caller or become parameters, and its `goto
# exception` is the function's failure.
#
#==============================================================================================
# mark: Constants ===============================================================================
## The kinds of OP_throw_error (JS_THROW_*).
let throw_var_ro: i32 = 0
let throw_var_redecl: i32 = 1
let throw_var_uninitialized: i32 = 2
let throw_error_delete_super: i32 = 3
let throw_error_iterator_throw: i32 = 4
## The operand flags of OP_define_method (OP_DEFINE_METHOD_*): the low 2 bits are the
## kind, bit 2 makes the property enumerable.
let op_define_method_method: i32 = 0
let op_define_method_getter: i32 = 1
let op_define_method_setter: i32 = 2
let op_define_method_enumerable: i32 = 4
## How the dispatch loop of JS_CallInternal left the frame: the three labels its C
## version jumps to.
enum FrameExit:
## `done`: the function returned `ret_val`; its locals are released.
done
## `done_generator`: a generator or async function suspended; its frame is kept.
done_generator
## `exception`: no handler caught the pending exception.
exception
# mark: Interpreter state =======================================================================
## The locals of C's JS_CallInternal that its opcodes read and write: the interpreter keeps
## them in one record so the bodies of the less frequent opcodes can be functions of their
## own (interp_ops_*.lucb) that take a pointer to it. Keeping those bodies out of
## js_call_internal keeps its native frame small, and so the depth of JavaScript recursion
## the stack allows.
struct InterpState:
var caller_ctx: Context*
## The function's realm once the frame is set up.
var ctx: Context*
var rt: Runtime*
var func_obj: Value
var this_obj: Value
var new_target: Value
var argc: i32
var argv: Value*
## The function object and its bytecode.
var p: Object*
var b: FunctionBytecode*
var sf: StackFrame*
var pc: const u8*
var sp: Value*
var opcode: Op
var ret_val: Value
var local_buf: Value*
var stack_buf: Value*
var var_buf: Value*
var arg_buf: Value*
var var_refs: VarRef*?*?
## b.byte_code_buf.
var bc_buf: const u8*
## The frame when it does not fit js_call_internal's frame_space.
var heap_frame: void*?
## Why interp_enter failed (InterpEntry.failed).
var failure_code: ErrorCode
var failure_message: str
# mark: Frame access ============================================================================
## `b->cpool[idx]`: the constant pool is present whenever the bytecode refers to it.
inline func cpool_at(b: FunctionBytecode*, idx: u32) -> Value:
let cpool = b.cpool else trap("bytecode refers to a missing constant pool")
return cpool[(usize)idx]
## `var_refs[idx]` of a closure: every closure variable of a created closure is set. It
## reads the array as C does (the nullable pointers are read through their address as
## plain ones), so that it is expanded inline: a trap on a missing array or variable would
## make it a call.
inline func var_ref_at(var_refs: VarRef*?*?, idx: usize) -> VarRef*:
var refs = var_refs
return (*(VarRef***)&refs)[idx]
## `&b->closure_var[idx]`.
func closure_var_at(b: FunctionBytecode*, idx: i32) -> ClosureVar*:
let cvs = b.closure_var else trap("bytecode refers to missing closure variables")
return cvs + (usize)idx
# mark: The dispatch loop's fast paths ==========================================================
# What the fast paths of the dispatch loop (interpreter.lucb) use. They write a value's tag
# and payload in place and test the reference count where it lies rather than through
# js_new_int32, js_dup_value, js_free_value or small helpers of their own: each expanded
# call of a function with parameters or a result keeps a frame slot of js_call_internal
# for them, and that frame is the stack one level of JavaScript recursion takes.
## A 16-bit and a 32-bit operand of an opcode, read in place at any alignment (cutils'
## get_u16 and get_u32).
packed struct Operand16:
var v: u16
packed struct Operand32:
var v: u32
## Free the value of tag `tag` and payload `bits`, whose last reference the caller released
## (__JS_FreeValue): the one call of a fast path's JS_FreeValue.
noinline func free_counted(ctx: Context*, tag: Tag, bits: u64):
var v: Value = ---
v.u.uint64 = bits
v.tag = tag
js_free_value_raw(ctx, v)
# mark: Interpreter macros ======================================================================
## GET_FIELD_INLINE: the property `atom` of `this_obj`. Plain objects are searched along
## their prototype chain without leaving the interpreter; accessors, exotic objects and
## primitives take the slow path, JS_GetPropertyInternal. `pc` is recorded in `sf` for
## the slow path's backtrace.
inline func get_field_inline(ctx: Context*, sf: StackFrame*, pc: const u8*, this_obj: Value, atom: Atom) -> Value!:
var obj = this_obj
if obj.tag == Tag.object:
var p = obj.u.obj
var fast = true
while fast:
var pr: Property*? = none
if let prs = find_own_property(&pr, p, atom):
# found
if ((i32)prs.flags & prop_tmask) != 0:
fast = false
else:
let found = pr else trap("find_own_property found no property")
return js_dup_value(ctx, found.u.value)
elif p.is_exotic:
# XXX: should avoid the slow path for arrays and typed arrays by ensuring
# that 'prop' is not numeric
obj = js_mkptr(Tag.object, p)
fast = false
elif let proto = p.shape.proto:
p = proto
else:
return js_undefined()
# slow path
sf.cur_pc = pc
return try js_get_property_internal(ctx, obj, atom, this_obj, false)
## GET_ARRAY_EL_INLINE: the element `prop` of `obj`. An integer index inside a fast
## array is read in place; anything else takes JS_GetPropertyValue, which frees `prop`
## (also when it fails).
inline func get_array_el_inline(ctx: Context*, sf: StackFrame*, pc: const u8*, obj: Value, prop: Value) -> Value!:
if obj.tag == Tag.object and prop.tag == Tag.int:
let p = obj.u.obj
let idx = (u32)js_value_get_int(prop)
if p.class_id == ClassId.array and idx < p.u.array.count:
return js_dup_value(ctx, p.u.array.u.values[(usize)idx])
# slow path
sf.cur_pc = pc
return try js_get_property_value(ctx, obj, prop)
## OP_CMP_EQ: `sp[-2] == sp[-1]` (`inv`: `!=`) into sp[-2], releasing both operands; the
## caller pops one slot. Numbers, objects against objects or null/undefined, and strings
## are compared here; the rest takes js_eq_slow.
inline func eq_inline(ctx: Context*, sf: StackFrame*, pc: const u8*, sp: Value*, inv: bool) -> !:
let op1 = sp[-2]
let op2 = sp[-1]
let tag1 = op1.tag
let tag2 = op2.tag
var res = false
var slow = false
if tag1 == Tag.int:
if tag2 == Tag.int:
res = js_value_get_int(op1) == js_value_get_int(op2)
elif js_tag_is_float64(tag2):
res = f64(js_value_get_int(op1)) == js_value_get_float64(op2)
else:
slow = true
elif js_tag_is_float64(tag1):
if tag2 == Tag.int:
res = js_value_get_float64(op1) == f64(js_value_get_int(op2))
elif js_tag_is_float64(tag2):
res = js_value_get_float64(op1) == js_value_get_float64(op2)
else:
slow = true
elif tag1 == Tag.object:
if tag2 == Tag.null or tag2 == Tag.undefined:
res = op1.u.obj.is_htmldda
js_free_value(ctx, op1)
elif tag2 == Tag.object:
res = op1.u.obj == op2.u.obj
js_free_value(ctx, op1)
js_free_value(ctx, op2)
else:
slow = true
elif tag1 == Tag.null or tag1 == Tag.undefined:
if tag2 == Tag.null or tag2 == Tag.undefined:
res = true
elif tag2 == Tag.object:
res = op2.u.obj.is_htmldda
js_free_value(ctx, op2)
else:
slow = true
elif tag1 == Tag.string and tag2 == Tag.string:
res = js_string_eq(ctx, js_value_get_string(op1), js_value_get_string(op2))
js_free_value(ctx, op1)
js_free_value(ctx, op2)
else:
slow = true
if slow:
sf.cur_pc = pc
_ = try js_eq_slow(ctx, sp, inv)
return
sp[-2] = js_new_bool(res != inv)
## OP_CMP_STRICT_EQ: `sp[-2] === sp[-1]` (`inv`: `!==`) into sp[-2], releasing both
## operands; the caller pops one slot. It cannot fail.
inline func strict_eq_inline(ctx: Context*, sp: Value*, inv: bool):
let op1 = sp[-2]
let op2 = sp[-1]
let tag1 = op1.tag
let tag2 = op2.tag
var res = false
if tag1 == Tag.int:
if tag2 == Tag.int:
res = js_value_get_int(op1) == js_value_get_int(op2)
elif js_tag_is_float64(tag2):
res = f64(js_value_get_int(op1)) == js_value_get_float64(op2)
else:
js_free_value(ctx, op2)
res = false
elif js_tag_is_float64(tag1):
if tag2 == Tag.int:
res = js_value_get_float64(op1) == f64(js_value_get_int(op2))
elif js_tag_is_float64(tag2):
res = js_value_get_float64(op1) == js_value_get_float64(op2)
else:
js_free_value(ctx, op2)
res = false
elif tag1 == Tag.object:
if tag2 == Tag.object:
res = op1.u.obj == op2.u.obj
else:
res = false
js_free_value(ctx, op1)
js_free_value(ctx, op2)
elif tag1 == Tag.null or tag1 == Tag.undefined:
res = tag1 == tag2
js_free_value(ctx, op2)
elif tag1 == Tag.string and tag2 == Tag.string:
res = js_string_eq(ctx, js_value_get_string(op1), js_value_get_string(op2))
js_free_value(ctx, op1)
js_free_value(ctx, op2)
else:
res = js_strict_eq2(ctx, op1, op2, StrictEqMode.strict)
js_free_value(ctx, op1)
js_free_value(ctx, op2)
sp[-2] = js_new_bool(res != inv)
## The two hex digits of a byte, for the "invalid opcode" message (C's `%02x`).
func hex_byte_prefix(byte: u8) -> str:
return "0" if byte < 16 else ""
## C's result of a function returning "-1 exception / FALSE / TRUE" from the port's
## `bool!` form with its failure recovered as none: `bool_status(f() catch: recover
## none)` is -1, 0 or 1, for the opcodes whose C code tests the result after cleanup.
inline func bool_status(r: bool?) -> i32:
if let v = r:
return (i32)v
return -1