summaryrefslogtreecommitdiffhomepage
path: root/vm.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-10-12 09:25:35 +0200
committerGitHub <noreply@github.com>2021-10-12 09:25:35 +0200
commit496b4f3c371f19b955d260fb1c8c8ba819f6a7b2 (patch)
tree3d46e9a3863728644a6bc0ce26320f4bfea0c490 /vm.c
parentce4a7d96a648d37a95ebe07dfef522073c1b2fae (diff)
parent4ee06d8138a107908a9fb45220fea32055b3c48a (diff)
Merge pull request #22 from jow-/introduce-optional-chaining-operators
syntax: introduce optional chaining operators
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c40
1 files changed, 36 insertions, 4 deletions
diff --git a/vm.c b/vm.c
index 65d6c81..3674c53 100644
--- a/vm.c
+++ b/vm.c
@@ -67,7 +67,9 @@ static const int8_t insn_operand_bytes[__I_MAX] = {
[I_COPY] = 1,
[I_CALL] = 4,
- [I_MCALL] = 4
+ [I_MCALL] = 4,
+ [I_QCALL] = 4,
+ [I_QMCALL] = 4,
};
static const char *exception_type_strings[] = {
@@ -1029,9 +1031,12 @@ uc_vm_insn_load_val(uc_vm_t *vm, uc_vm_insn_t insn)
break;
default:
- uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
- "left-hand side expression is %s",
- v ? "not an array or object" : "null");
+ if (insn == I_QLVAL)
+ uc_vm_stack_push(vm, NULL);
+ else
+ uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
+ "left-hand side expression is %s",
+ v ? "not an array or object" : "null");
break;
}
@@ -1898,11 +1903,32 @@ uc_vm_insn_close_upval(uc_vm_t *vm, uc_vm_insn_t insn)
}
static void
+uc_vm_skip_call(uc_vm_t *vm, bool mcall)
+{
+ uc_callframe_t *frame = uc_vm_current_frame(vm);
+ size_t i;
+
+ /* pop all function arguments, the function itself and the associated
+ * function context off the stack */
+ for (i = 0; i < 1 + mcall + (vm->arg.u32 & 0xffff); i++)
+ ucv_put(uc_vm_stack_pop(vm));
+
+ /* skip all encoded spread value indexes */
+ for (i = 0; i < (vm->arg.u32 >> 16); i++)
+ frame->ip += 2;
+
+ uc_vm_stack_push(vm, NULL);
+}
+
+static void
uc_vm_insn_call(uc_vm_t *vm, uc_vm_insn_t insn)
{
uc_value_t *fno = ucv_get(uc_vm_stack_peek(vm, vm->arg.u32 & 0xffff));
uc_value_t *ctx = NULL;
+ if (!ucv_is_callable(fno) && insn == I_QCALL)
+ return uc_vm_skip_call(vm, false);
+
if (!ucv_is_arrowfn(fno))
ctx = NULL;
else if (vm->callframes.count > 0)
@@ -1919,6 +1945,9 @@ uc_vm_insn_mcall(uc_vm_t *vm, uc_vm_insn_t insn)
uc_value_t *key = vm->stack.entries[key_slot];
uc_value_t *fno = ucv_key_get(vm, ctx, key);
+ if (!ucv_is_callable(fno) && insn == I_QMCALL)
+ return uc_vm_skip_call(vm, true);
+
uc_vm_stack_set(vm, key_slot, fno);
/* arrow functions as method calls inherit the parent ctx */
@@ -2084,6 +2113,7 @@ uc_vm_execute_chunk(uc_vm_t *vm)
break;
case I_LVAL:
+ case I_QLVAL:
uc_vm_insn_load_val(vm, insn);
break;
@@ -2229,12 +2259,14 @@ uc_vm_execute_chunk(uc_vm_t *vm)
break;
case I_CALL:
+ case I_QCALL:
uc_vm_insn_call(vm, insn);
frame = uc_vm_current_frame(vm);
chunk = frame->closure ? uc_vm_frame_chunk(frame) : NULL;
break;
case I_MCALL:
+ case I_QMCALL:
uc_vm_insn_mcall(vm, insn);
frame = uc_vm_current_frame(vm);
chunk = frame->closure ? uc_vm_frame_chunk(frame) : NULL;