dymokomi/luce-js
Source of release 0.6.0
0.6.0 / src / engine / parser_assign.lucb
#==============================================================================================
#
# parser_assign - Yield, assignment and comma expressions
#
# DESCRIPTION:
# Port of quickjs.c lines 27999-28316: js_parse_assign_expr2 with 'yield' and
# 'yield*', the assignments (plain, compound and logical), and the comma expression
# (js_parse_expr2, js_parse_expr).
#
# A parser function that answers -1 on a syntax error in QuickJS is fallible here
# (`-> !`); the SyntaxError is pending in the context, as in C.
#
#==============================================================================================
# mark: yield ===================================================================================
# 'yield' and 'yield*' (the TOK_YIELD case of js_parse_assign_expr2).
func js_parse_yield_expr(s: ParseState*, parse_flags: i32) -> !:
var is_star = false
if (s.fd().func_kind & js_func_generator) == 0:
try js_parse_error(s, "unexpected 'yield' keyword")
if not s.fd().in_function_body:
try js_parse_error(s, "yield in default expression")
try next_token(s)
# XXX: is there a better method to detect 'yield' without parameters ?
let tok = s.token.val
if (tok != TokenType.semicolon and tok != TokenType.rparen and tok != TokenType.rbracket and tok != TokenType.rbrace and tok != TokenType.comma and tok != TokenType.colon and not s.got_lf):
if s.token.val == TokenType.star:
is_star = true
try next_token(s)
try js_parse_assign_expr2(s, parse_flags)
else:
emit_op(s, (u8)Op.undefined)
let is_async = s.fd().func_kind == js_func_async_generator
if is_star:
let label_loop = new_label(s)
let label_yield = new_label(s)
emit_op(s, (u8)Op.for_await_of_start if is_async else (u8)Op.for_of_start)
# remove the catch offset (XXX: could avoid pushing back undefined)
emit_op(s, (u8)Op.drop)
emit_op(s, (u8)Op.undefined)
# initial value
emit_op(s, (u8)Op.undefined)
_ = emit_label(s, label_loop)
emit_op(s, (u8)Op.iterator_next)
if is_async:
emit_op(s, (u8)Op.await)
emit_op(s, (u8)Op.iterator_check_object)
emit_op(s, (u8)Op.get_field2)
emit_atom(s, atom_done)
# end of loop
let label_next = emit_goto(s, (i32)Op.if_true, -1)
_ = emit_label(s, label_yield)
if is_async:
# OP_async_yield_star takes the value as parameter
emit_op(s, (u8)Op.get_field)
emit_atom(s, atom_value)
emit_op(s, (u8)Op.async_yield_star)
else:
# OP_yield_star takes (value, done) as parameter
emit_op(s, (u8)Op.yield_star)
emit_op(s, (u8)Op.dup)
let label_return = emit_goto(s, (i32)Op.if_true, -1)
emit_op(s, (u8)Op.drop)
_ = emit_goto(s, (i32)Op.goto_, label_loop)
_ = emit_label(s, label_return)
emit_op(s, (u8)Op.push_i32)
emit_u32(s, 2)
emit_op(s, (u8)Op.strict_eq)
let label_throw = emit_goto(s, (i32)Op.if_true, -1)
# return handling
if is_async:
emit_op(s, (u8)Op.await)
emit_op(s, (u8)Op.iterator_call)
emit_u8(s, 0)
let label_return1 = emit_goto(s, (i32)Op.if_true, -1)
if is_async:
emit_op(s, (u8)Op.await)
emit_op(s, (u8)Op.iterator_check_object)
emit_op(s, (u8)Op.get_field2)
emit_atom(s, atom_done)
_ = emit_goto(s, (i32)Op.if_false, label_yield)
emit_op(s, (u8)Op.get_field)
emit_atom(s, atom_value)
_ = emit_label(s, label_return1)
emit_op(s, (u8)Op.nip)
emit_op(s, (u8)Op.nip)
emit_op(s, (u8)Op.nip)
emit_return(s, true)
# throw handling
_ = emit_label(s, label_throw)
emit_op(s, (u8)Op.iterator_call)
emit_u8(s, 1)
let label_throw1 = emit_goto(s, (i32)Op.if_true, -1)
if is_async:
emit_op(s, (u8)Op.await)
emit_op(s, (u8)Op.iterator_check_object)
emit_op(s, (u8)Op.get_field2)
emit_atom(s, atom_done)
_ = emit_goto(s, (i32)Op.if_false, label_yield)
_ = emit_goto(s, (i32)Op.goto_, label_next)
# close the iterator and throw a type error exception
_ = emit_label(s, label_throw1)
emit_op(s, (u8)Op.iterator_call)
emit_u8(s, 2)
let label_throw2 = emit_goto(s, (i32)Op.if_true, -1)
if is_async:
emit_op(s, (u8)Op.await)
_ = emit_label(s, label_throw2)
emit_op(s, (u8)Op.throw_error)
emit_atom(s, no_atom)
emit_u8(s, (u8)throw_error_iterator_throw)
_ = emit_label(s, label_next)
emit_op(s, (u8)Op.get_field)
emit_atom(s, atom_value)
# keep the value associated with done = true
emit_op(s, (u8)Op.nip)
emit_op(s, (u8)Op.nip)
emit_op(s, (u8)Op.nip)
else:
if is_async:
emit_op(s, (u8)Op.await)
emit_op(s, (u8)Op.yield)
let label_next = emit_goto(s, (i32)Op.if_false, -1)
emit_return(s, true)
_ = emit_label(s, label_next)
# mark: Assignment ==============================================================================
# The binary opcode of each compound assignment, from TOK_MUL_ASSIGN to TOK_POW_ASSIGN.
let assign_opcodes: u8[12] = [
(u8)Op.mul, (u8)Op.div, (u8)Op.mod, (u8)Op.add, (u8)Op.sub,
(u8)Op.shl, (u8)Op.sar, (u8)Op.shr, (u8)Op.and_, (u8)Op.xor, (u8)Op.or_,
(u8)Op.pow,
]
# Whether an arrow function, an async arrow function or a destructuring assignment
# starts here: if so it is parsed and the answer is true. An 'async' that starts none
# of them is left current.
func js_parse_assign_special(s: ParseState*) -> bool!:
var bits_unused: i32 = 0
if (s.token.val == TokenType.lparen and (try js_parse_skip_parens_token(s, &bits_unused, true)) == TokenType.arrow):
try js_parse_function_decl(s, ParseFunctionKind.arrow, js_func_normal, no_atom,
s.token.ptr)
return true
elif token_is_pseudo_keyword(s, atom_async):
# fast test
let tok = peek_token(s, true)
if tok == TokenType.function or tok == TokenType.newline:
return false
let source_ptr = s.token.ptr
var pos: ParsePos = ---
_ = js_parse_get_pos(s, &pos)
try next_token(s)
if ((s.token.val == TokenType.lparen and (try js_parse_skip_parens_token(s, &bits_unused, true)) == TokenType.arrow) or (s.token.val == TokenType.ident and not s.token.u.ident.is_reserved and peek_token(s, true) == TokenType.arrow)):
try js_parse_function_decl(s, ParseFunctionKind.arrow, js_func_async, no_atom,
source_ptr)
return true
else:
# undo the token parsing
try js_parse_seek_token(s, &pos)
elif s.token.val == TokenType.ident and peek_token(s, true) == TokenType.arrow:
try js_parse_function_decl(s, ParseFunctionKind.arrow, js_func_normal, no_atom,
s.token.ptr)
return true
elif s.token.val == TokenType.lbrace or s.token.val == TokenType.lbracket:
var skip_bits: i32 = 0
if (try js_parse_skip_parens_token(s, &skip_bits, false)) == TokenType.assign:
_ = try js_parse_destructuring_element(s, (TokenType)0, false, false, skip_bits & skip_has_ellipsis,
true, false)
return true
return false
## allowed parse_flags: PF_IN_ACCEPTED
func js_parse_assign_expr2(s: ParseState*, parse_flags: i32) -> !:
var name0 = no_atom
if s.token.val == TokenType.yield:
try js_parse_yield_expr(s, parse_flags)
return
if try js_parse_assign_special(s):
return
# next:
if s.token.val == TokenType.ident:
# name0 is used to check for OP_set_name pattern, not duplicated
name0 = s.token.u.ident.atom
try js_parse_cond_expr(s, parse_flags)
let op = s.token.val
var opcode: i32 = 0
var scope: i32 = 0
var name = no_atom
var label: i32 = 0
if (op == TokenType.assign or ((i32)op >= (i32)TokenType.mul_assign and (i32)op <= (i32)TokenType.pow_assign)):
let op_token_ptr = s.token.ptr
try next_token(s)
var depth_unused: i32 = 0
try get_lvalue(s, &opcode, &scope, &name, &label, &depth_unused, op != TokenType.assign,
op)
js_parse_assign_expr2(s, parse_flags) catch failure:
js_free_atom(s.ctx, name)
error(failure.code, failure.message)
if op == TokenType.assign:
if ((opcode == (i32)Op.get_ref_value or opcode == (i32)TempOp.scope_get_var) and name == name0):
set_object_name(s, name)
else:
let assign_op = assign_opcodes[(usize)((i32)op - (i32)TokenType.mul_assign)]
emit_source_pos(s, op_token_ptr)
emit_op(s, assign_op)
put_lvalue(s, opcode, scope, name, label, PutLValueKind.keep_top, false)
elif ((i32)op >= (i32)TokenType.land_assign and (i32)op <= (i32)TokenType.double_question_mark_assign):
try next_token(s)
var depth_lvalue: i32 = 0
try get_lvalue(s, &opcode, &scope, &name, &label, &depth_lvalue, true, op)
emit_op(s, (u8)Op.dup)
if op == TokenType.double_question_mark_assign:
emit_op(s, (u8)Op.is_undefined_or_null)
let jump = (i32)Op.if_true if op == TokenType.lor_assign else (i32)Op.if_false
let label1 = emit_goto(s, jump, -1)
emit_op(s, (u8)Op.drop)
js_parse_assign_expr2(s, parse_flags) catch failure:
js_free_atom(s.ctx, name)
error(failure.code, failure.message)
if ((opcode == (i32)Op.get_ref_value or opcode == (i32)TempOp.scope_get_var) and name == name0):
set_object_name(s, name)
match depth_lvalue:
0: emit_op(s, (u8)Op.dup)
1: emit_op(s, (u8)Op.insert2)
2: emit_op(s, (u8)Op.insert3)
3: emit_op(s, (u8)Op.insert4)
_: trap("js_parse_assign_expr2: invalid lvalue depth")
# XXX: we disable the OP_put_ref_value optimization by not using put_lvalue()
# otherwise depth_lvalue is not correct
put_lvalue(s, opcode, scope, name, label, PutLValueKind.nokeep_depth, false)
let label2 = emit_goto(s, (i32)Op.goto_, -1)
_ = emit_label(s, label1)
# remove the lvalue stack entries
while depth_lvalue != 0:
emit_op(s, (u8)Op.nip)
depth_lvalue -= 1
_ = emit_label(s, label2)
func js_parse_assign_expr(s: ParseState*) -> !:
try js_parse_assign_expr2(s, pf_in_accepted)
# mark: Comma expressions =======================================================================
## allowed parse_flags: PF_IN_ACCEPTED
func js_parse_expr2(s: ParseState*, parse_flags: i32) -> !:
var comma = false
while true:
try js_parse_assign_expr2(s, parse_flags)
if comma:
# prevent get_lvalue from using the last expression as an lvalue. This also
# prevents the conversion of of get_var to get_ref for method lookup in
# function call inside `with` statement.
s.fd().last_opcode_pos = -1
if s.token.val != TokenType.comma:
break
comma = true
try next_token(s)
emit_op(s, (u8)Op.drop)
func js_parse_expr(s: ParseState*) -> !:
try js_parse_expr2(s, pf_in_accepted)