dymokomi/luce-js
Source of release 0.6.0
0.6.0 / src / engine / interp_ops_control.lucb
#==============================================================================================
#
# interp_ops_control - Opcodes: jumps, catch and gosub, for-in/for-of and iterators
#
# DESCRIPTION:
# Port of the opcode bodies of JS_CallInternal, quickjs.c lines 18822-19162: OP_goto to
# OP_lnot. 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: Jumps ===================================================================================
## The body of OP_goto in JS_CallInternal.
noinline func interp_goto(st: InterpState*) -> bool:
st.pc += (isize)(i32)cutils.get_u32(st.pc)
js_poll_interrupts(st.ctx) catch: return false
return true
## The body of OP_goto16 in JS_CallInternal.
noinline func interp_goto16(st: InterpState*) -> bool:
st.pc += (isize)(i16)cutils.get_u16(st.pc)
js_poll_interrupts(st.ctx) catch: return false
return true
## The body of OP_goto8 in JS_CallInternal.
noinline func interp_goto8(st: InterpState*) -> bool:
st.pc += (isize)(i8)st.pc[0]
js_poll_interrupts(st.ctx) catch: return false
return true
## The body of OP_if_true in JS_CallInternal.
noinline func interp_if_true(st: InterpState*) -> bool:
let op1 = st.sp[-1]
st.pc += 4
var res = false
if (u64)op1.tag <= (u64)Tag.undefined:
res = js_value_get_int(op1) != 0
else:
res = js_to_bool_free(st.ctx, op1) catch: recover true # C: -1 is true
st.sp -= 1
if res:
st.pc += (isize)((i32)cutils.get_u32(st.pc - 4) - 4)
js_poll_interrupts(st.ctx) catch: return false
return true
## The body of OP_if_false in JS_CallInternal.
noinline func interp_if_false(st: InterpState*) -> bool:
let op1 = st.sp[-1]
st.pc += 4
var res = false
# quick and dirty test for JS_TAG_INT, JS_TAG_BOOL, JS_TAG_NULL and
# JS_TAG_UNDEFINED
if (u64)op1.tag <= (u64)Tag.undefined:
res = js_value_get_int(op1) != 0
else:
res = js_to_bool_free(st.ctx, op1) catch: recover true # C: -1 is true
st.sp -= 1
if not res:
st.pc += (isize)((i32)cutils.get_u32(st.pc - 4) - 4)
js_poll_interrupts(st.ctx) catch: return false
return true
## The body of OP_if_true8 in JS_CallInternal.
noinline func interp_if_true8(st: InterpState*) -> bool:
let op1 = st.sp[-1]
st.pc += 1
var res = false
if (u64)op1.tag <= (u64)Tag.undefined:
res = js_value_get_int(op1) != 0
else:
res = js_to_bool_free(st.ctx, op1) catch: recover true # C: -1 is true
st.sp -= 1
if res:
st.pc += (isize)((i32)(i8)st.pc[-1] - 1)
js_poll_interrupts(st.ctx) catch: return false
return true
## The body of OP_if_false8 in JS_CallInternal.
noinline func interp_if_false8(st: InterpState*) -> bool:
let op1 = st.sp[-1]
st.pc += 1
var res = false
if (u64)op1.tag <= (u64)Tag.undefined:
res = js_value_get_int(op1) != 0
else:
res = js_to_bool_free(st.ctx, op1) catch: recover true # C: -1 is true
st.sp -= 1
if not res:
st.pc += (isize)((i32)(i8)st.pc[-1] - 1)
js_poll_interrupts(st.ctx) catch: return false
return true
# mark: Catch, gosub and ret ====================================================================
## The body of OP_catch in JS_CallInternal.
noinline func interp_catch(st: InterpState*):
let diff = (i32)cutils.get_u32(st.pc)
st.sp[0] = js_new_catch_offset((i32)(st.pc + (isize)diff - st.bc_buf))
st.sp += 1
st.pc += 4
## The body of OP_gosub in JS_CallInternal.
noinline func interp_gosub(st: InterpState*):
let diff = (i32)cutils.get_u32(st.pc)
# XXX: should have a different tag to avoid security flaw
st.sp[0] = js_new_int32((i32)(st.pc + 4 - st.bc_buf))
st.sp += 1
st.pc += (isize)diff
## The body of OP_ret in JS_CallInternal.
noinline func interp_ret(st: InterpState*) -> bool:
let op1 = st.sp[-1]
if op1.tag != Tag.int or (u32)js_value_get_int(op1) >= (u32)st.b.byte_code_len:
# ret_fail
js_throw_internal_error(st.ctx, "invalid ret value") catch: return false
let pos = (u32)js_value_get_int(op1)
st.sp -= 1
st.pc = st.bc_buf + (usize)pos
return true
# mark: For-in, for-of and iterators ============================================================
## The body of OP_for_in_start in JS_CallInternal.
noinline func interp_for_in_start(st: InterpState*) -> bool:
st.sf.cur_pc = st.pc
js_for_in_start(st.ctx, st.sp) catch: return false
return true
## The body of OP_for_in_next in JS_CallInternal.
noinline func interp_for_in_next(st: InterpState*) -> bool:
st.sf.cur_pc = st.pc
js_for_in_next(st.ctx, st.sp) catch: return false
st.sp += 2
return true
## The body of OP_for_of_start in JS_CallInternal.
noinline func interp_for_of_start(st: InterpState*) -> bool:
st.sf.cur_pc = st.pc
js_for_of_start(st.ctx, st.sp, false) catch: return false
st.sp += 1
*st.sp = js_new_catch_offset(0)
st.sp += 1
return true
## The body of OP_for_of_next in JS_CallInternal.
noinline func interp_for_of_next(st: InterpState*) -> bool:
let offset = -3 - (i32)st.pc[0]
st.pc += 1
st.sf.cur_pc = st.pc
js_for_of_next(st.ctx, st.sp, offset) catch: return false
st.sp += 2
return true
## The body of OP_for_await_of_next in JS_CallInternal.
noinline func interp_for_await_of_next(st: InterpState*) -> bool:
st.sf.cur_pc = st.pc
js_for_await_of_next(st.ctx, st.sp) catch: return false
st.sp += 1
return true
## The body of OP_for_await_of_start in JS_CallInternal.
noinline func interp_for_await_of_start(st: InterpState*) -> bool:
st.sf.cur_pc = st.pc
js_for_of_start(st.ctx, st.sp, true) catch: return false
st.sp += 1
*st.sp = js_new_catch_offset(0)
st.sp += 1
return true
## The body of OP_iterator_get_value_done in JS_CallInternal.
noinline func interp_iterator_get_value_done(st: InterpState*) -> bool:
st.sf.cur_pc = st.pc
js_iterator_get_value_done(st.ctx, st.sp) catch: return false
st.sp += 1
return true
## The body of OP_iterator_check_object in JS_CallInternal.
noinline func interp_iterator_check_object(st: InterpState*) -> bool:
if not js_is_object(st.sp[-1]):
js_throw_type_error(st.ctx, "iterator must return an object") catch: return false
return true
## The body of OP_iterator_close in JS_CallInternal.
noinline func interp_iterator_close(st: InterpState*) -> bool:
# iter_obj next catch_offset ->
st.sp -= 1 # drop the catch offset to avoid getting caught by exception
js_free_value(st.ctx, st.sp[-1]) # drop the next method
st.sp -= 1
if not js_is_undefined(st.sp[-1]):
st.sf.cur_pc = st.pc
js_iterator_close(st.ctx, st.sp[-1], false) catch: return false
js_free_value(st.ctx, st.sp[-1])
st.sp -= 1
return true
## The body of OP_nip_catch in JS_CallInternal.
noinline func interp_nip_catch(st: InterpState*) -> bool:
# catch_offset ... ret_val -> ret_eval
st.sp -= 1
let nip_val = *st.sp
while st.sp > st.stack_buf and st.sp[-1].tag != Tag.catch_offset:
st.sp -= 1
js_free_value(st.ctx, *st.sp)
if st.sp == st.stack_buf:
js_throw_internal_error(st.ctx, "nip_catch") catch:
js_free_value(st.ctx, nip_val)
return false
st.sp[-1] = nip_val
return true
## The body of OP_iterator_next in JS_CallInternal.
noinline func interp_iterator_next(st: InterpState*) -> bool:
# stack: iter_obj next catch_offset val
st.sf.cur_pc = st.pc
let ret = js_call(st.ctx, st.sp[-3], st.sp[-4], 1, st.sp - 1) catch: return false
js_free_value(st.ctx, st.sp[-1])
st.sp[-1] = ret
return true
## The body of OP_iterator_call in JS_CallInternal.
noinline func interp_iterator_call(st: InterpState*) -> bool:
# stack: iter_obj next catch_offset val
let iter_flags = (i32)*st.pc
st.pc += 1
st.sf.cur_pc = st.pc
let method = js_get_property(st.ctx, st.sp[-4], atom_throw if (iter_flags & 1) != 0 else atom_return) catch: return false
var ret_flag = false
if js_is_undefined(method) or js_is_null(method):
ret_flag = true
else:
var ret = js_undefined()
if (iter_flags & 2) != 0:
# no argument
ret = js_call_free(st.ctx, method, st.sp[-4], 0, st.sp) catch: return false
else:
ret = js_call_free(st.ctx, method, st.sp[-4], 1, st.sp - 1) catch: return false
js_free_value(st.ctx, st.sp[-1])
st.sp[-1] = ret
ret_flag = false
st.sp[0] = js_new_bool(ret_flag)
st.sp += 1
return true
## The body of OP_lnot in JS_CallInternal.
noinline func interp_lnot(st: InterpState*):
let op1 = st.sp[-1]
var res = false
if (u64)op1.tag <= (u64)Tag.undefined:
res = js_value_get_int(op1) != 0
else:
res = js_to_bool_free(st.ctx, op1) catch: recover true # C: -1 is true
st.sp[-1] = js_new_bool(not res)