• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/vm.c

  1 /*
  2  * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #include <stdarg.h>
 18 #include <string.h>
 19 #include <assert.h>
 20 #include <ctype.h>
 21 #include <math.h>
 22 #include <errno.h>
 23 #include <limits.h>
 24 #include <fcntl.h>
 25 #include <unistd.h>
 26 
 27 #include "ucode/vm.h"
 28 #include "ucode/compiler.h"
 29 #include "ucode/program.h"
 30 #include "ucode/lib.h" /* uc_error_context_format() */
 31 #include "ucode/platform.h"
 32 
 33 #undef __insn
 34 #define __insn(_name) #_name,
 35 
 36 static const char *insn_names[__I_MAX] = {
 37         __insns
 38 };
 39 
 40 static const int8_t insn_operand_bytes[__I_MAX] = {
 41         [I_LOAD] = 4,
 42         [I_LOAD8] = 1,
 43         [I_LOAD16] = 2,
 44         [I_LOAD32] = 4,
 45 
 46         [I_LREXP] = 4,
 47 
 48         [I_LLOC] = 4,
 49         [I_LVAR] = 4,
 50         [I_LUPV] = 4,
 51 
 52         [I_CLFN] = 4,
 53         [I_ARFN] = 4,
 54 
 55         [I_SLOC] = 4,
 56         [I_SUPV] = 4,
 57         [I_SVAR] = 4,
 58 
 59         [I_ULOC] = 4,
 60         [I_UUPV] = 4,
 61         [I_UVAR] = 4,
 62         [I_UVAL] = 1,
 63 
 64         [I_NARR] = 4,
 65         [I_PARR] = 4,
 66 
 67         [I_NOBJ] = 4,
 68         [I_SOBJ] = 4,
 69 
 70         [I_JMP] = -4,
 71         [I_JMPZ] = -4,
 72         [I_JMPNT] = 4,
 73 
 74         [I_COPY] = 1,
 75 
 76         [I_CALL] = 4,
 77 
 78         [I_IMPORT] = 4,
 79         [I_EXPORT] = 4,
 80         [I_DYNLOAD] = 4
 81 };
 82 
 83 static const char *exception_type_strings[] = {
 84         [EXCEPTION_SYNTAX] = "Syntax error",
 85         [EXCEPTION_RUNTIME] = "Runtime error",
 86         [EXCEPTION_TYPE] = "Type error",
 87         [EXCEPTION_REFERENCE] = "Reference error",
 88         [EXCEPTION_USER] = "Error",
 89         [EXCEPTION_EXIT] = "Exit"
 90 };
 91 
 92 
 93 static const char *
 94 uc_vm_insn_to_name(uc_vm_insn_t insn)
 95 {
 96         if (insn < 0 || insn >= __I_MAX)
 97                 return "(unknown)";
 98 
 99         return insn_names[insn];
100 }
101 
102 static int8_t
103 uc_vm_insn_to_argtype(uc_vm_insn_t insn)
104 {
105         if (insn < 0 || insn >= __I_MAX)
106                 return 0;
107 
108         return insn_operand_bytes[insn];
109 }
110 
111 static void
112 uc_vm_reset_stack(uc_vm_t *vm)
113 {
114         while (vm->stack.count > 0) {
115                 vm->stack.count--;
116                 ucv_put(vm->stack.entries[vm->stack.count]);
117                 vm->stack.entries[vm->stack.count] = NULL;
118         }
119 }
120 
121 static uc_value_t *
122 uc_vm_callframe_pop(uc_vm_t *vm);
123 
124 static void
125 uc_vm_reset_callframes(uc_vm_t *vm)
126 {
127         while (vm->callframes.count > 0)
128                 ucv_put(uc_vm_callframe_pop(vm));
129 }
130 
131 static uc_value_t *
132 uc_vm_alloc_global_scope(uc_vm_t *vm)
133 {
134         uc_value_t *scope, *arr;
135         size_t i;
136 
137         scope = ucv_object_new(vm);
138 
139         /* build default require() search path */
140         arr = ucv_array_new(vm);
141 
142         for (i = 0; i < vm->config->module_search_path.count; i++)
143                 ucv_array_push(arr, ucv_string_new(vm->config->module_search_path.entries[i]));
144 
145         /* register module related constants */
146         ucv_object_add(scope, "REQUIRE_SEARCH_PATH", arr);
147         ucv_object_add(scope, "modules", ucv_object_new(vm));
148 
149         /* register scope math constants */
150         ucv_object_add(scope, "NaN", ucv_double_new(NAN));
151         ucv_object_add(scope, "Infinity", ucv_double_new(INFINITY));
152 
153         /* register global property */
154         ucv_object_add(scope, "global", ucv_get(scope));
155 
156         uc_vm_scope_set(vm, scope);
157 
158         return scope;
159 }
160 
161 static void
162 uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex);
163 
164 static void
165 uc_vm_signal_handler(int sig)
166 {
167         uc_vm_t *vm = uc_thread_context_get()->signal_handler_vm;
168 
169         assert(vm);
170 
171         uc_vm_signal_raise(vm, sig);
172 }
173 
174 static void
175 uc_vm_signal_handlers_setup(uc_vm_t *vm)
176 {
177         uc_thread_context_t *tctx;
178 
179         memset(&vm->signal, 0, sizeof(vm->signal));
180 
181         vm->signal.sigpipe[0] = -1;
182         vm->signal.sigpipe[1] = -1;
183 
184         if (!vm->config->setup_signal_handlers)
185                 return;
186 
187         tctx = uc_thread_context_get();
188 
189         if (tctx->signal_handler_vm)
190                 return;
191 
192         if (pipe2(vm->signal.sigpipe, O_CLOEXEC | O_NONBLOCK) != 0)
193                 return;
194 
195         vm->signal.handler = ucv_array_new_length(vm, UC_SYSTEM_SIGNAL_COUNT);
196 
197         vm->signal.sa.sa_handler = uc_vm_signal_handler;
198         vm->signal.sa.sa_flags = SA_RESTART | SA_ONSTACK;
199         sigemptyset(&vm->signal.sa.sa_mask);
200 
201         tctx->signal_handler_vm = vm;
202 }
203 
204 static void
205 uc_vm_signal_handlers_reset(uc_vm_t *vm)
206 {
207         uc_thread_context_t *tctx = uc_thread_context_get();
208         struct sigaction sa = { 0 };
209         size_t i, signo;
210 
211         if (vm != tctx->signal_handler_vm)
212                 return;
213 
214         sa.sa_handler = SIG_DFL;
215         sigemptyset(&sa.sa_mask);
216 
217         for (signo = 0; signo < ucv_array_length(vm->signal.handler); signo++)
218                 if (ucv_is_callable(ucv_array_get(vm->signal.handler, signo)))
219                         sigaction(signo, &sa, NULL);
220 
221         for (i = 0; i < ARRAY_SIZE(vm->signal.sigpipe); i++) {
222                 if (vm->signal.sigpipe[i] > STDERR_FILENO)
223                         close(vm->signal.sigpipe[i]);
224 
225                 vm->signal.sigpipe[i] = -1;
226         }
227 
228         tctx->signal_handler_vm = NULL;
229 }
230 
231 void uc_vm_init(uc_vm_t *vm, uc_parse_config_t *config)
232 {
233         vm->exception.type = EXCEPTION_NONE;
234         vm->exception.message = NULL;
235 
236         vm->config = config ? config : &uc_default_parse_config;
237 
238         vm->open_upvals = NULL;
239 
240         vm->values.prev = &vm->values;
241         vm->values.next = &vm->values;
242 
243         vm->strbuf = NULL;
244 
245         vm->output = stdout;
246 
247         uc_vm_reset_stack(vm);
248 
249         uc_vm_alloc_global_scope(vm);
250 
251         uc_vm_exception_handler_set(vm, uc_vm_output_exception);
252 
253         uc_vm_trace_set(vm, 0);
254 
255         uc_vm_signal_handlers_setup(vm);
256 }
257 
258 void uc_vm_free(uc_vm_t *vm)
259 {
260         uc_upvalref_t *ref;
261         size_t i;
262 
263         uc_vm_signal_handlers_reset(vm);
264 
265         ucv_put(vm->exception.stacktrace);
266         free(vm->exception.message);
267 
268         while (vm->open_upvals) {
269                 ref = vm->open_upvals->next;
270                 ucv_put(&vm->open_upvals->header);
271                 vm->open_upvals = ref;
272         }
273 
274         for (i = 0; i < vm->restypes.count; i++)
275                 ucv_put(vm->restypes.entries[i]->proto);
276 
277         uc_vm_reset_callframes(vm);
278         uc_vm_reset_stack(vm);
279         uc_vector_clear(&vm->stack);
280         uc_vector_clear(&vm->callframes);
281 
282         printbuf_free(vm->strbuf);
283 
284         ucv_freeall(vm);
285 
286         for (i = 0; i < vm->restypes.count; i++)
287                 free(vm->restypes.entries[i]);
288 
289         uc_vector_clear(&vm->restypes);
290 }
291 
292 static uc_chunk_t *
293 uc_vm_frame_chunk(uc_callframe_t *frame)
294 {
295         return frame->closure ? &frame->closure->function->chunk : NULL;
296 }
297 
298 static uc_program_t *
299 uc_vm_frame_program(uc_callframe_t *frame)
300 {
301         return frame->closure ? frame->closure->function->program : NULL;
302 }
303 
304 static uc_source_t *
305 uc_vm_frame_source(uc_callframe_t *frame)
306 {
307         return frame->closure ? uc_program_function_source(frame->closure->function) : NULL;
308 }
309 
310 static uc_callframe_t *
311 uc_vm_current_frame(uc_vm_t *vm)
312 {
313         return uc_vector_last(&vm->callframes);
314 }
315 
316 static uc_program_t *
317 uc_vm_current_program(uc_vm_t *vm)
318 {
319         return uc_vm_frame_program(uc_vm_current_frame(vm));
320 }
321 
322 static bool
323 uc_vm_is_strict(uc_vm_t *vm)
324 {
325         return uc_vm_current_frame(vm)->strict;
326 }
327 
328 static uc_vm_insn_t
329 uc_vm_decode_insn(uc_vm_t *vm, uc_callframe_t *frame, uc_chunk_t *chunk)
330 {
331         uc_vm_insn_t insn;
332         int8_t argtype;
333 
334 #ifndef NDEBUG
335         uint8_t *end = chunk->entries + chunk->count;
336 #endif
337 
338         assert(frame->ip < end);
339 
340         insn = frame->ip[0];
341         frame->ip++;
342 
343         argtype = uc_vm_insn_to_argtype(insn);
344 
345         assert(frame->ip + abs(argtype) <= end);
346 
347         switch (argtype) {
348         case 0:
349                 break;
350 
351         case -4:
352                 vm->arg.s32 = (
353                         frame->ip[0] * 0x1000000UL +
354                         frame->ip[1] * 0x10000UL +
355                         frame->ip[2] * 0x100UL +
356                         frame->ip[3]
357                 ) - 0x7fffffff;
358                 frame->ip += 4;
359                 break;
360 
361         case 1:
362                 vm->arg.u8 = frame->ip[0];
363                 frame->ip++;
364                 break;
365 
366         case 2:
367                 vm->arg.u16 = (
368                         frame->ip[0] * 0x100 +
369                         frame->ip[1]
370                 );
371                 frame->ip += 2;
372                 break;
373 
374         case 4:
375                 vm->arg.u32 = (
376                         frame->ip[0] * 0x1000000UL +
377                         frame->ip[1] * 0x10000UL +
378                         frame->ip[2] * 0x100UL +
379                         frame->ip[3]
380                 );
381                 frame->ip += 4;
382                 break;
383 
384         default:
385                 fprintf(stderr, "Unhandled operand format: %" PRId8 "\n", argtype);
386                 abort();
387         }
388 
389         return insn;
390 }
391 
392 
393 static char *
394 uc_vm_format_val(uc_vm_t *vm, uc_value_t *val)
395 {
396         if (!vm->strbuf)
397                 vm->strbuf = xprintbuf_new();
398         else
399                 printbuf_reset(vm->strbuf);
400 
401         ucv_to_stringbuf(NULL, vm->strbuf, val, true);
402 
403         if (printbuf_length(vm->strbuf) >= 64) {
404                 printbuf_memset(vm->strbuf, 60, '.', 3);
405                 printbuf_memset(vm->strbuf, 63, 0, 1);
406         }
407 
408         return vm->strbuf->buf;
409 }
410 
411 static void
412 uc_vm_frame_dump(uc_vm_t *vm, uc_callframe_t *frame)
413 {
414         uc_chunk_t *chunk = uc_vm_frame_chunk(frame);
415         uc_function_t *function;
416         uc_closure_t *closure;
417         uc_upvalref_t *ref;
418         uc_value_t *v;
419         size_t i;
420 
421         fprintf(stderr, "  [*] CALLFRAME[%zx]\n",
422                 frame - vm->callframes.entries);
423 
424         fprintf(stderr, "   |- stackframe %zu/%zu\n",
425                 frame->stackframe, vm->stack.count);
426 
427         fprintf(stderr, "   |- ctx %s\n",
428                 uc_vm_format_val(vm, frame->ctx));
429 
430         if (chunk) {
431                 closure = frame->closure;
432                 function = closure->function;
433 
434                 fprintf(stderr, "   `- %zu upvalues\n",
435                         function->nupvals);
436 
437                 for (i = 0; i < function->nupvals; i++) {
438                         ref = closure->upvals[i];
439                         v = uc_chunk_debug_get_variable(chunk, 0, i, true);
440 
441                         fprintf(stderr, "     [%zu] <%p> %s ",
442                                 i, (void *)ref, uc_vm_format_val(vm, v));
443 
444                         if (!ref) {
445                                 fprintf(stderr, "{unresolved}\n");
446                         }
447                         else if (ref->closed) {
448                                 fprintf(stderr, "{closed} %s\n",
449                                         uc_vm_format_val(vm, ref->value));
450                         }
451                         else {
452                                 fprintf(stderr, "{open[%zu]} %s\n",
453                                         ref->slot,
454                                         uc_vm_format_val(vm, vm->stack.entries[ref->slot]));
455                         }
456 
457                         ucv_put(v);
458                 }
459         }
460 }
461 
462 static uc_value_t *
463 uc_vm_resolve_upval(uc_vm_t *vm, uc_value_t *value)
464 {
465         uc_upvalref_t *ref;
466 
467 #ifdef __clang_analyzer__
468         /* Clang static analyzer does not understand that ucv_type(NULL) can't
469          * possibly yield UC_UPVALUE. Nudge it. */
470         if (value != NULL && ucv_type(value) == UC_UPVALUE)
471 #else
472         if (ucv_type(value) == UC_UPVALUE)
473 #endif
474         {
475                 ref = (uc_upvalref_t *)value;
476 
477                 if (ref->closed)
478                         return ucv_get(ref->value);
479                 else
480                         return ucv_get(vm->stack.entries[ref->slot]);
481         }
482 
483         return value;
484 }
485 
486 void
487 uc_vm_stack_push(uc_vm_t *vm, uc_value_t *value)
488 {
489         uc_vector_push(&vm->stack, uc_vm_resolve_upval(vm, value));
490 
491         if (vm->trace) {
492                 fprintf(stderr, "  [+%zd] %s\n",
493                         vm->stack.count - 1,
494                         uc_vm_format_val(vm, vm->stack.entries[vm->stack.count - 1]));
495         }
496 }
497 
498 uc_value_t *
499 uc_vm_stack_pop(uc_vm_t *vm)
500 {
501         uc_value_t *rv;
502 
503         vm->stack.count--;
504         rv = vm->stack.entries[vm->stack.count];
505         vm->stack.entries[vm->stack.count] = NULL;
506 
507         if (vm->trace) {
508                 fprintf(stderr, "  [-%zd] %s\n",
509                         vm->stack.count,
510                         uc_vm_format_val(vm, rv));
511         }
512 
513         return rv;
514 }
515 
516 uc_value_t *
517 uc_vm_stack_peek(uc_vm_t *vm, size_t offset)
518 {
519         return vm->stack.entries[vm->stack.count + (-1 - offset)];
520 }
521 
522 static void
523 uc_vm_stack_set(uc_vm_t *vm, size_t offset, uc_value_t *value)
524 {
525         if (vm->trace) {
526                 fprintf(stderr, "  [!%zu] %s\n",
527                         offset,
528                         uc_vm_format_val(vm, value));
529         }
530 
531         ucv_put(vm->stack.entries[offset]);
532         vm->stack.entries[offset] = value;
533 }
534 
535 static void
536 uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall, size_t nargs)
537 {
538         uc_value_t *res = NULL;
539         uc_callframe_t *frame;
540 
541         /* add new callframe */
542         frame = uc_vector_push(&vm->callframes, {
543                 .stackframe = vm->stack.count - nargs - 1,
544                 .cfunction = fptr,
545                 .closure = NULL,
546                 .ctx = ctx,
547                 .mcall = mcall
548         });
549 
550         if (vm->trace)
551                 uc_vm_frame_dump(vm, frame);
552 
553         res = fptr->cfn(vm, nargs);
554 
555         /* Reset stack, check for callframe depth since an uncatched exception in managed
556          * code executed by fptr->cfn() could've reset the callframe stack already. */
557         if (vm->callframes.count > 0)
558                 ucv_put(uc_vm_callframe_pop(vm));
559 
560         /* push return value */
561         if (!vm->exception.type)
562                 uc_vm_stack_push(vm, res);
563         else
564                 ucv_put(res);
565 }
566 
567 static bool
568 uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, size_t argspec)
569 {
570         size_t i, j, stackoff, nargs = argspec & 0xffff;
571         size_t nspreads = (argspec >> 16) & 0x7fff;
572         uc_callframe_t *frame = NULL;
573         uc_value_t *ellip, *arg;
574         uc_function_t *function;
575         uc_closure_t *closure;
576         uint16_t slot, tmp;
577         char *s;
578 
579         /* XXX: make dependent on stack size */
580         if (vm->callframes.count >= 1000) {
581                 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Too much recursion");
582                 ucv_put(ctx);
583                 ucv_put(fno);
584 
585                 return false;
586         }
587 
588         stackoff = vm->stack.count - nargs - 1;
589 
590         /* argument list contains spread operations, we need to reshuffle the stack */
591         if (nspreads > 0) {
592                 frame = uc_vm_current_frame(vm);
593 
594                 /* create temporary array */
595                 ellip = ucv_array_new_length(vm, nargs);
596 
597                 /* pop original stack values and push to temp array in reverse order */
598                 for (i = 0; i < nargs; i++)
599                         ucv_array_push(ellip, uc_vm_stack_pop(vm));
600 
601                 /* for each spread value index ... */
602                 for (i = 0, slot = nargs; i < nspreads; i++) {
603                         /* decode stack depth value */
604                         tmp = frame->ip[0] * 0x100 + frame->ip[1];
605                         frame->ip += 2;
606 
607                         /* push each preceeding non-spread value to the stack */
608                         for (j = slot; j > tmp + 1UL; j--)
609                                 uc_vm_stack_push(vm, ucv_get(ucv_array_get(ellip, j - 1)));
610 
611                         /* read spread value at index... */
612                         slot = tmp;
613                         arg = ucv_get(ucv_array_get(ellip, slot));
614 
615                         /* ... ensure that it is an array type ... */
616                         if (ucv_type(arg) != UC_ARRAY) {
617                                 s = ucv_to_string(vm, arg);
618                                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s);
619                                 free(s);
620                                 ucv_put(ctx);
621                                 ucv_put(fno);
622                                 ucv_put(ellip);
623 
624                                 return false;
625                         }
626 
627                         /* ... and push each spread array value as argument to the stack */
628                         for (j = 0; j < ucv_array_length(arg); j++)
629                                 uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j)));
630 
631                         ucv_put(arg);
632                 }
633 
634                 /* push remaining non-spread arguments to the stack */
635                 for (i = slot; i > 0; i--)
636                         uc_vm_stack_push(vm, ucv_get(ucv_array_get(ellip, i - 1)));
637 
638                 /* free temp array */
639                 ucv_put(ellip);
640 
641                 /* update arg count */
642                 nargs = vm->stack.count - stackoff - 1;
643         }
644 
645         /* is a native function */
646         if (ucv_type(fno) == UC_CFUNCTION) {
647                 uc_vm_call_native(vm, ctx, (uc_cfunction_t *)fno, mcall, nargs);
648 
649                 return true;
650         }
651 
652         if (ucv_type(fno) != UC_CLOSURE) {
653                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "left-hand side is not a function");
654                 ucv_put(ctx);
655                 ucv_put(fno);
656 
657                 return false;
658         }
659 
660         closure = (uc_closure_t *)fno;
661         function = closure->function;
662 
663         /* fewer arguments on stack than function expects => pad */
664         if (nargs < function->nargs) {
665                 for (i = nargs; i < function->nargs; i++) {
666                         if (function->vararg && (i + 1) == function->nargs)
667                                 uc_vm_stack_push(vm, ucv_array_new_length(vm, 0));
668                         else
669                                 uc_vm_stack_push(vm, NULL);
670                 }
671         }
672 
673         /* more arguments on stack than function expects... */
674         else if (nargs > function->nargs - function->vararg) {
675                 /* is a vararg function => pass excess args as array */
676                 if (function->vararg) {
677                         ellip = ucv_array_new_length(vm, nargs - (function->nargs - 1));
678 
679                         for (i = function->nargs; i <= nargs; i++)
680                                 ucv_array_push(ellip, uc_vm_stack_peek(vm, nargs - i));
681 
682                         for (i = function->nargs; i <= nargs; i++)
683                                 uc_vm_stack_pop(vm);
684 
685                         uc_vm_stack_push(vm, ellip);
686                 }
687 
688                 /* static amount of args => drop excess values */
689                 else {
690                         for (i = function->nargs; i < nargs; i++)
691                                 ucv_put(uc_vm_stack_pop(vm));
692                 }
693         }
694 
695         frame = uc_vector_push(&vm->callframes, {
696                 .stackframe = stackoff,
697                 .cfunction = NULL,
698                 .closure = closure,
699                 .ctx = ctx,
700                 .ip = function->chunk.entries,
701                 .mcall = mcall,
702                 .strict = function->strict
703         });
704 
705         if (vm->trace)
706                 uc_vm_frame_dump(vm, frame);
707 
708         return true;
709 }
710 
711 static uc_source_t *last_source = NULL;
712 static size_t last_srcpos = 0;
713 
714 static void
715 uc_dump_insn(uc_vm_t *vm, uint8_t *pos, uc_vm_insn_t insn)
716 {
717         uc_callframe_t *frame = uc_vm_current_frame(vm);
718         uc_chunk_t *chunk = uc_vm_frame_chunk(frame);
719         uc_stringbuf_t *buf = NULL;
720         uc_value_t *cnst = NULL;
721         uc_source_t *source;
722         int8_t argtype;
723         size_t srcpos;
724 
725         srcpos = uc_program_function_srcpos(frame->closure->function, pos - chunk->entries);
726         source = uc_vm_frame_source(frame);
727 
728         if (last_srcpos == 0 || last_source != source || srcpos != last_srcpos) {
729                 buf = xprintbuf_new();
730 
731                 uc_source_context_format(buf, source, srcpos, true);
732                 fwrite(buf->buf, 1, printbuf_length(buf), stderr);
733                 printbuf_free(buf);
734 
735                 last_source = source;
736                 last_srcpos = srcpos;
737         }
738 
739         fprintf(stderr, "%08zx  %s", pos - chunk->entries,
740                 uc_vm_insn_to_name(insn));
741 
742         argtype = uc_vm_insn_to_argtype(insn);
743 
744         switch (argtype) {
745         case 0:
746                 break;
747 
748         case -1:
749                 fprintf(stderr, " {%s%hhd}", vm->arg.s8 < 0 ? "" : "+", vm->arg.s8);
750                 break;
751 
752         case -2:
753                 fprintf(stderr, " {%c0x%hx}",
754                         vm->arg.s16 < 0 ? '-' : '+',
755                         (uint16_t)(vm->arg.s16 < 0 ? -vm->arg.s16 : vm->arg.s16));
756                 break;
757 
758         case -4:
759                 fprintf(stderr, " {%c0x%x}",
760                         vm->arg.s32 < 0 ? '-' : '+',
761                         (uint32_t)(vm->arg.s32 < 0 ? -vm->arg.s32 : vm->arg.s32));
762                 break;
763 
764         case 1:
765                 fprintf(stderr, " {%hhu}", vm->arg.u8);
766                 break;
767 
768         case 2:
769                 fprintf(stderr, " {0x%hx}", vm->arg.u16);
770                 break;
771 
772         case 4:
773                 fprintf(stderr, " {0x%x}", vm->arg.u32);
774                 break;
775 
776         default:
777                 fprintf(stderr, " (unknown operand format: %" PRId8 ")", argtype);
778                 break;
779         }
780 
781         switch (insn) {
782         case I_LOAD:
783         case I_LVAR:
784         case I_SVAR:
785                 cnst = uc_program_get_constant(uc_vm_frame_program(uc_vector_last(&vm->callframes)), vm->arg.u32);
786 
787                 fprintf(stderr, "\t; %s",
788                         cnst ? uc_vm_format_val(vm, cnst) : "(?)");
789 
790                 ucv_put(cnst);
791                 break;
792 
793         case I_LLOC:
794         case I_LUPV:
795         case I_SLOC:
796         case I_SUPV:
797                 cnst = uc_chunk_debug_get_variable(chunk, pos - chunk->entries, vm->arg.u32, (insn == I_LUPV || insn == I_SUPV));
798 
799                 fprintf(stderr, "\t; %s",
800                         cnst ? uc_vm_format_val(vm, cnst) : "(?)");
801 
802                 ucv_put(cnst);
803                 break;
804 
805         case I_ULOC:
806         case I_UUPV:
807                 cnst = uc_chunk_debug_get_variable(chunk, pos - chunk->entries, vm->arg.u32 & 0x00ffffff, (insn == I_UUPV));
808                 /* fall through */
809 
810         case I_UVAR:
811                 if (!cnst)
812                         cnst = uc_program_get_constant(uc_vm_frame_program(uc_vector_last(&vm->callframes)), vm->arg.u32 & 0x00ffffff);
813 
814                 fprintf(stderr, "\t; %s (%s)",
815                         cnst ? uc_vm_format_val(vm, cnst) : "(?)",
816                         uc_vm_insn_to_name(vm->arg.u32 >> 24));
817 
818                 ucv_put(cnst);
819                 break;
820 
821         case I_UVAL:
822                 fprintf(stderr, "\t; (%s)", uc_vm_insn_to_name(vm->arg.u8));
823                 break;
824 
825         default:
826                 break;
827         }
828 
829         fprintf(stderr, "\n");
830 }
831 
832 static uc_value_t *
833 uc_vm_exception_tostring(uc_vm_t *vm, size_t nargs)
834 {
835         uc_callframe_t *frame = uc_vm_current_frame(vm);
836         uc_value_t *message = ucv_object_get(frame->ctx, "message", NULL);
837 
838         return message ? ucv_get(message) : ucv_string_new("Exception");
839 }
840 
841 uc_value_t *
842 uc_vm_exception_object(uc_vm_t *vm)
843 {
844         uc_exception_type_t type = vm->exception.type;
845         const char *message = vm->exception.message;
846         uc_value_t *stacktrace = vm->exception.stacktrace;
847         uc_value_t *exception_prototype = uc_vm_registry_get(vm, "vm.exception.proto");
848         uc_value_t *exo;
849 
850         if (exception_prototype == NULL) {
851                 exception_prototype = ucv_object_new(vm);
852 
853                 ucv_object_add(exception_prototype, "tostring",
854                         ucv_cfunction_new("tostring", uc_vm_exception_tostring));
855 
856                 uc_vm_registry_set(vm, "vm.exception.proto", exception_prototype);
857         }
858 
859         exo = ucv_object_new(vm);
860 
861         ucv_object_add(exo, "type", ucv_string_new(exception_type_strings[type]));
862         ucv_object_add(exo, "message", ucv_string_new(message));
863         ucv_object_add(exo, "stacktrace", ucv_get(stacktrace));
864 
865         ucv_prototype_set(exo, ucv_get(exception_prototype));
866 
867         return exo;
868 }
869 
870 static void
871 uc_vm_clear_exception(uc_vm_t *vm)
872 {
873         vm->exception.type = EXCEPTION_NONE;
874 
875         ucv_put(vm->exception.stacktrace);
876         vm->exception.stacktrace = NULL;
877 
878         free(vm->exception.message);
879         vm->exception.message = NULL;
880 }
881 
882 static bool
883 uc_vm_handle_exception(uc_vm_t *vm)
884 {
885         uc_callframe_t *frame = NULL;
886         uc_chunk_t *chunk = NULL;
887         uc_value_t *exo;
888         size_t i, pos;
889 
890         if (vm->callframes.count)
891                 frame = uc_vm_current_frame(vm);
892 
893         if (!frame || !frame->closure)
894                 return false;
895 
896         chunk = uc_vm_frame_chunk(frame);
897         pos = frame->ip - chunk->entries;
898 
899         /* iterate the known exception ranges, see if the current ip falls into any of them */
900         for (i = 0; i < chunk->ehranges.count; i++) {
901                 /* skip nonmatching ranges */
902                 if (pos < chunk->ehranges.entries[i].from ||
903                     pos >= chunk->ehranges.entries[i].to)
904                         continue;
905 
906                 /* we found a matching range... first unwind stack */
907                 while (vm->stack.count > frame->stackframe + chunk->ehranges.entries[i].slot)
908                         ucv_put(uc_vm_stack_pop(vm));
909 
910                 /* prepare exception object and expose it to user handler code */
911                 exo = uc_vm_exception_object(vm);
912 
913                 uc_vm_stack_push(vm, exo);
914 
915                 /* reset exception information */
916                 uc_vm_clear_exception(vm);
917 
918                 /* jump to exception handler */
919                 if (chunk->ehranges.entries[i].target >= chunk->count) {
920                         uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range");
921                         return false;
922                 }
923 
924 #if 0
925                 if (vm->trace && chunk->entries + chunk->ehranges.entries[i].target > frame->ip) {
926                         while (frame->ip < chunk->entries + chunk->ehranges.entries[i].target) {
927                                 fprintf(stderr, "(eh:skip) [%p:%zu] ", chunk, frame->ip - chunk->entries);
928                                 uc_dump_insn(vm, frame->ip, uc_vm_decode_insn(vm, frame, chunk));
929                         }
930                 }
931 #endif
932 
933                 frame->ip = chunk->entries + chunk->ehranges.entries[i].target;
934 
935                 return true;
936         }
937 
938         return false;
939 }
940 
941 static uc_value_t *
942 uc_vm_capture_stacktrace(uc_vm_t *vm, size_t i)
943 {
944         uc_value_t *stacktrace, *entry, *last = NULL;
945         uc_function_t *function;
946         uc_callframe_t *frame;
947         uc_source_t *source;
948         size_t off, srcpos;
949         char *name;
950 
951         stacktrace = ucv_array_new(vm);
952 
953         for (; i > 0; i--) {
954                 frame = &vm->callframes.entries[i - 1];
955                 entry = ucv_object_new(vm);
956 
957                 if (frame->closure) {
958                         function = frame->closure->function;
959                         source = uc_program_function_source(function);
960 
961                         off = (frame->ip - uc_vm_frame_chunk(frame)->entries) - 1;
962                         srcpos = uc_program_function_srcpos(function, off);
963 
964                         ucv_object_add(entry, "filename", ucv_string_new(source->filename));
965                         ucv_object_add(entry, "line", ucv_int64_new(uc_source_get_line(source, &srcpos)));
966                         ucv_object_add(entry, "byte", ucv_int64_new(srcpos));
967                 }
968 
969                 if (i > 1) {
970                         if (frame->closure) {
971                                 if (frame->closure->function->name[0])
972                                         name = frame->closure->function->name;
973                                 else if (frame->closure->is_arrow)
974                                         name = "[arrow function]";
975                                 else
976                                         name = "[anonymous function]";
977                         }
978                         else {
979                                 name = frame->cfunction->name;
980                         }
981 
982                         ucv_object_add(entry, "function", ucv_string_new(name));
983                 }
984 
985                 if (!ucv_is_equal(last, entry)) {
986                         ucv_array_push(stacktrace, entry);
987                         last = entry;
988                 }
989                 else {
990                         ucv_put(entry);
991                 }
992         }
993 
994         return stacktrace;
995 }
996 
997 static uc_value_t *
998 uc_vm_get_error_context(uc_vm_t *vm)
999 {
1000         size_t offset, i, byte, line;
1001         uc_value_t *stacktrace;
1002         uc_callframe_t *frame;
1003         uc_stringbuf_t *buf;
1004         uc_chunk_t *chunk;
1005 
1006         /* skip to first non-native function call frame */
1007         for (i = vm->callframes.count; i > 1; i--)
1008                 if (vm->callframes.entries[i - 1].closure)
1009                         break;
1010 
1011         frame = &vm->callframes.entries[i - 1];
1012 
1013         if (!frame->closure)
1014                 return NULL;
1015 
1016         chunk = uc_vm_frame_chunk(frame);
1017         offset = uc_program_function_srcpos(frame->closure->function, (frame->ip - chunk->entries) - 1);
1018         stacktrace = uc_vm_capture_stacktrace(vm, i);
1019 
1020         buf = ucv_stringbuf_new();
1021 
1022         byte = offset;
1023         line = uc_source_get_line(uc_program_function_source(frame->closure->function), &byte);
1024 
1025         if (line)
1026                 uc_error_context_format(buf, uc_vm_frame_source(frame), stacktrace, offset);
1027         else if (frame->ip != chunk->entries)
1028                 ucv_stringbuf_printf(buf, "At instruction %zu", (frame->ip - chunk->entries) - 1);
1029         else
1030                 ucv_stringbuf_append(buf, "At start of program");
1031 
1032         ucv_object_add(ucv_array_get(stacktrace, 0), "context", ucv_stringbuf_finish(buf));
1033 
1034         return stacktrace;
1035 }
1036 
1037 void __attribute__((format(printf, 3, 0)))
1038 uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, ...)
1039 {
1040         va_list ap;
1041 
1042         vm->exception.type = type;
1043 
1044         free(vm->exception.message);
1045 
1046         va_start(ap, fmt);
1047         xvasprintf(&vm->exception.message, fmt, ap);
1048         va_end(ap);
1049 
1050         ucv_put(vm->exception.stacktrace);
1051         vm->exception.stacktrace = uc_vm_get_error_context(vm);
1052 }
1053 
1054 static bool
1055 uc_vm_test_strict_equality(uc_value_t *v1, uc_value_t *v2, bool nan_equal)
1056 {
1057         uc_type_t t1 = ucv_type(v1);
1058         uc_type_t t2 = ucv_type(v2);
1059         double d1, d2;
1060 
1061         if (t1 != t2)
1062                 return false;
1063 
1064         switch (t1) {
1065         case UC_DOUBLE:
1066                 d1 = ((uc_double_t *)v1)->dbl;
1067                 d2 = ((uc_double_t *)v2)->dbl;
1068 
1069                 if (isnan(d1) && isnan(d2))
1070                         return nan_equal;
1071 
1072                 return (d1 == d2);
1073 
1074         case UC_NULL:
1075         case UC_BOOLEAN:
1076         case UC_INTEGER:
1077         case UC_STRING:
1078                 return ucv_is_equal(v1, v2);
1079 
1080         default:
1081                 return (v1 == v2);
1082         }
1083 }
1084 
1085 
1086 static void
1087 uc_vm_insn_load(uc_vm_t *vm, uc_vm_insn_t insn)
1088 {
1089         switch (insn) {
1090         case I_LOAD:
1091                 uc_vm_stack_push(vm, uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32));
1092                 break;
1093 
1094         case I_LOAD8:
1095                 uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u8));
1096                 break;
1097 
1098         case I_LOAD16:
1099                 uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u16));
1100                 break;
1101 
1102         case I_LOAD32:
1103                 uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u32));
1104                 break;
1105 
1106         default:
1107                 break;
1108         }
1109 }
1110 
1111 static void
1112 uc_vm_insn_load_regexp(uc_vm_t *vm, uc_vm_insn_t insn)
1113 {
1114         uc_value_t *re, *jstr = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32);
1115         bool icase = false, newline = false, global = false;
1116         char *str, *err = NULL;
1117 
1118         if (ucv_type(jstr) != UC_STRING || ucv_string_length(jstr) < 2) {
1119                 uc_vm_stack_push(vm, NULL);
1120                 ucv_put(jstr);
1121 
1122                 return;
1123         }
1124 
1125         str = ucv_string_get(jstr);
1126 
1127         global  = (*str & (1 << 0));
1128         icase   = (*str & (1 << 1));
1129         newline = (*str & (1 << 2));
1130 
1131         re = ucv_regexp_new(++str, icase, newline, global, &err);
1132 
1133         ucv_put(jstr);
1134 
1135         if (re)
1136                 uc_vm_stack_push(vm, re);
1137         else
1138                 uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "%s", err);
1139 
1140         free(err);
1141 }
1142 
1143 static void
1144 uc_vm_insn_load_null(uc_vm_t *vm, uc_vm_insn_t insn)
1145 {
1146         uc_vm_stack_push(vm, NULL);
1147 }
1148 
1149 static void
1150 uc_vm_insn_load_bool(uc_vm_t *vm, uc_vm_insn_t insn)
1151 {
1152         uc_vm_stack_push(vm, ucv_boolean_new(insn == I_LTRUE));
1153 }
1154 
1155 static void
1156 uc_vm_insn_load_var(uc_vm_t *vm, uc_vm_insn_t insn)
1157 {
1158         uc_value_t *name, *val = NULL;
1159         uc_value_t *scope, *next;
1160         bool found;
1161 
1162         scope = vm->globals;
1163         name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32);
1164 
1165         while (ucv_type(name) == UC_STRING) {
1166                 val = ucv_object_get(scope, ucv_string_get(name), &found);
1167 
1168                 if (found)
1169                         break;
1170 
1171                 next = ucv_prototype_get(scope);
1172 
1173                 if (!next) {
1174                         if (uc_vm_is_strict(vm)) {
1175                                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
1176                                                       "access to undeclared variable %s",
1177                                                       ucv_string_get(name));
1178                         }
1179 
1180                         break;
1181                 }
1182 
1183                 scope = next;
1184         }
1185 
1186         ucv_put(name);
1187 
1188         uc_vm_stack_push(vm, ucv_get(val));
1189 }
1190 
1191 static void
1192 uc_vm_insn_load_val(uc_vm_t *vm, uc_vm_insn_t insn)
1193 {
1194         uc_value_t *k = uc_vm_stack_pop(vm);
1195         uc_value_t *v = uc_vm_stack_pop(vm);
1196 
1197         switch (ucv_type(v)) {
1198         case UC_RESOURCE:
1199         case UC_OBJECT:
1200         case UC_ARRAY:
1201                 uc_vm_stack_push(vm, ucv_key_get(vm, v, k));
1202                 break;
1203 
1204         default:
1205                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
1206                                       "left-hand side expression is %s",
1207                                       v ? "not an array or object" : "null");
1208 
1209                 break;
1210         }
1211 
1212         ucv_put(k);
1213         ucv_put(v);
1214 }
1215 
1216 static void
1217 uc_vm_insn_peek_val(uc_vm_t *vm, uc_vm_insn_t insn)
1218 {
1219         uc_value_t *k = uc_vm_stack_pop(vm);
1220         uc_value_t *v = uc_vm_stack_peek(vm, 0);
1221 
1222         switch (ucv_type(v)) {
1223         case UC_RESOURCE:
1224         case UC_OBJECT:
1225         case UC_ARRAY:
1226                 uc_vm_stack_push(vm, ucv_key_get(vm, v, k));
1227                 break;
1228 
1229         default:
1230                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
1231                                                                 "left-hand side expression is %s",
1232                                                                 v ? "not an array or object" : "null");
1233 
1234                 break;
1235         }
1236 
1237         ucv_put(k);
1238 }
1239 
1240 static void
1241 uc_vm_insn_load_upval(uc_vm_t *vm, uc_vm_insn_t insn)
1242 {
1243         uc_callframe_t *frame = uc_vm_current_frame(vm);
1244         uc_upvalref_t *ref = frame->closure->upvals[vm->arg.u32];
1245 
1246         if (ref->closed)
1247                 uc_vm_stack_push(vm, ucv_get(ref->value));
1248         else
1249                 uc_vm_stack_push(vm, ucv_get(vm->stack.entries[ref->slot]));
1250 }
1251 
1252 static void
1253 uc_vm_insn_load_local(uc_vm_t *vm, uc_vm_insn_t insn)
1254 {
1255         uc_callframe_t *frame = uc_vm_current_frame(vm);
1256 
1257         uc_vm_stack_push(vm, ucv_get(vm->stack.entries[frame->stackframe + vm->arg.u32]));
1258 }
1259 
1260 static uc_upvalref_t *
1261 uc_vm_capture_upval(uc_vm_t *vm, size_t slot)
1262 {
1263         uc_upvalref_t *curr = vm->open_upvals;
1264         uc_upvalref_t *prev = NULL;
1265         uc_upvalref_t *created;
1266         char *s;
1267 
1268         while (curr && curr->slot > slot) {
1269                 prev = curr;
1270                 curr = curr->next;
1271         }
1272 
1273         if (curr && curr->slot == slot) {
1274                 if (vm->trace) {
1275                         s = ucv_to_string(NULL, vm->stack.entries[slot]);
1276                         fprintf(stderr, "  {+%zu} <%p> %s\n", slot, (void *)curr, s);
1277                         free(s);
1278                 }
1279 
1280                 return curr;
1281         }
1282 
1283         created = (uc_upvalref_t *)ucv_upvalref_new(slot);
1284         created->next = curr;
1285 
1286         if (vm->trace) {
1287                 s = ucv_to_string(NULL, vm->stack.entries[slot]);
1288                 fprintf(stderr, "  {*%zu} <%p> %s\n", slot, (void *)created, s);
1289                 free(s);
1290         }
1291 
1292         if (prev)
1293                 prev->next = created;
1294         else
1295                 vm->open_upvals = created;
1296 
1297         return created;
1298 }
1299 
1300 static void
1301 uc_vm_close_upvals(uc_vm_t *vm, size_t slot)
1302 {
1303         uc_upvalref_t *ref;
1304 
1305         while (vm->open_upvals && vm->open_upvals->slot >= slot) {
1306                 ref = vm->open_upvals;
1307                 ref->value = ucv_get(vm->stack.entries[ref->slot]);
1308                 ref->closed = true;
1309 
1310                 if (vm->trace) {
1311                         fprintf(stderr, "  {!%zu} <%p> %s\n", ref->slot,
1312                                 (void *)ref,
1313                                 uc_vm_format_val(vm, ref->value));
1314                 }
1315 
1316                 vm->open_upvals = ref->next;
1317                 ucv_put(&ref->header);
1318         }
1319 }
1320 
1321 static void
1322 uc_vm_insn_load_closure(uc_vm_t *vm, uc_vm_insn_t insn)
1323 {
1324         uc_callframe_t *frame = uc_vm_current_frame(vm);
1325         uc_function_t *function = uc_program_function_load(uc_vm_current_program(vm), vm->arg.u32);
1326         uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, function, insn == I_ARFN);
1327         volatile int32_t uv;
1328         size_t i;
1329 
1330         uc_vm_stack_push(vm, &closure->header);
1331 
1332         if (function->module)
1333                 return;
1334 
1335         for (i = 0; i < function->nupvals; i++) {
1336                 uv = (
1337                         frame->ip[0] * 0x1000000 +
1338                         frame->ip[1] * 0x10000 +
1339                         frame->ip[2] * 0x100 +
1340                         frame->ip[3]
1341                 ) - 0x7fffffff;
1342 
1343                 if (uv < 0)
1344                         closure->upvals[i] = uc_vm_capture_upval(vm, frame->stackframe - (uv + 1));
1345                 else
1346                         closure->upvals[i] = frame->closure->upvals[uv];
1347 
1348                 ucv_get(&closure->upvals[i]->header);
1349 
1350                 frame->ip += 4;
1351         }
1352 }
1353 
1354 static void
1355 uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn)
1356 {
1357         uc_value_t *name, *v = uc_vm_stack_pop(vm);
1358         uc_value_t *scope, *next;
1359         bool found;
1360 
1361         scope = vm->globals;
1362         name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32);
1363 
1364         while (ucv_type(name) == UC_STRING) {
1365                 ucv_object_get(scope, ucv_string_get(name), &found);
1366 
1367                 if (found)
1368                         break;
1369 
1370                 next = ucv_prototype_get(scope);
1371 
1372                 if (!next) {
1373                         if (uc_vm_is_strict(vm)) {
1374                                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
1375                                                       "access to undeclared variable %s",
1376                                                       ucv_string_get(name));
1377                         }
1378 
1379                         break;
1380                 }
1381 
1382                 scope = next;
1383         }
1384 
1385         if (scope && ucv_type(name) == UC_STRING)
1386                 ucv_object_add(scope, ucv_string_get(name), ucv_get(v));
1387 
1388         ucv_put(name);
1389         uc_vm_stack_push(vm, v);
1390 }
1391 
1392 static bool
1393 assert_mutable_value(uc_vm_t *vm, uc_value_t *val)
1394 {
1395         if (ucv_is_constant(val)) {
1396                 uc_vm_stack_push(vm, NULL);
1397                 uc_vm_raise_exception(vm, EXCEPTION_TYPE,
1398                                       "%s value is immutable",
1399                                       ucv_typename(val));
1400 
1401                 return false;
1402         }
1403 
1404         return true;
1405 }
1406 
1407 static void
1408 uc_vm_insn_store_val(uc_vm_t *vm, uc_vm_insn_t insn)
1409 {
1410         uc_value_t *v = uc_vm_stack_pop(vm);
1411         uc_value_t *k = uc_vm_stack_pop(vm);
1412         uc_value_t *o = uc_vm_stack_pop(vm);
1413 
1414         switch (ucv_type(o)) {
1415         case UC_OBJECT:
1416         case UC_ARRAY:
1417                 if (assert_mutable_value(vm, o))
1418                         uc_vm_stack_push(vm, ucv_key_set(vm, o, k, v));
1419 
1420                 break;
1421 
1422         default:
1423                 uc_vm_raise_exception(vm, EXCEPTION_TYPE,
1424                                       "attempt to set property on %s value",
1425                                       ucv_typename(o));
1426         }
1427 
1428         ucv_put(o);
1429         ucv_put(k);
1430 }
1431 
1432 static void
1433 uc_vm_insn_store_upval(uc_vm_t *vm, uc_vm_insn_t insn)
1434 {
1435         uc_callframe_t *frame = uc_vm_current_frame(vm);
1436         uc_upvalref_t *ref = frame->closure->upvals[vm->arg.u32];
1437         uc_value_t *val = ucv_get(uc_vm_stack_peek(vm, 0));
1438 
1439         if (ref->closed) {
1440                 ucv_put(ref->value);
1441                 ref->value = val;
1442         }
1443         else {
1444                 uc_vm_stack_set(vm, ref->slot, val);
1445         }
1446 }
1447 
1448 static void
1449 uc_vm_insn_store_local(uc_vm_t *vm, uc_vm_insn_t insn)
1450 {
1451         uc_callframe_t *frame = uc_vm_current_frame(vm);
1452         uc_value_t *val = ucv_get(uc_vm_stack_peek(vm, 0));
1453 
1454         uc_vm_stack_set(vm, frame->stackframe + vm->arg.u32, val);
1455 }
1456 
1457 static int64_t
1458 int64(uc_value_t *nv, uint64_t *u64)
1459 {
1460         int64_t n;
1461 
1462         n = ucv_int64_get(nv);
1463         *u64 = 0;
1464 
1465         if (errno == ERANGE) {
1466                 n = INT64_MAX;
1467                 *u64 = ucv_uint64_get(nv);
1468         }
1469 
1470         return n;
1471 }
1472 
1473 static uint64_t
1474 abs64(int64_t n)
1475 {
1476         if (n == INT64_MIN)
1477                 return 0x8000000000000000ULL;
1478 
1479         if (n < 0)
1480                 return -n;
1481 
1482         return n;
1483 }
1484 
1485 
1486 static uc_value_t *
1487 uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand)
1488 {
1489         uc_value_t *nv1, *nv2, *rv = NULL;
1490         uint64_t u1, u2;
1491         int64_t n1, n2;
1492 
1493         nv1 = ucv_to_number(value);
1494         nv2 = ucv_to_number(operand);
1495 
1496         n1 = int64(nv1, &u1);
1497         n2 = int64(nv2, &u2);
1498 
1499         if (n1 < 0 || n2 < 0) {
1500                 switch (operation) {
1501                 case I_LSHIFT:
1502                         rv = ucv_int64_new(n1 << n2);
1503                         break;
1504 
1505                 case I_RSHIFT:
1506                         rv = ucv_int64_new(n1 >> n2);
1507                         break;
1508 
1509                 case I_BAND:
1510                         rv = ucv_int64_new(n1 & n2);
1511                         break;
1512 
1513                 case I_BXOR:
1514                         rv = ucv_int64_new(n1 ^ n2);
1515                         break;
1516 
1517                 case I_BOR:
1518                         rv = ucv_int64_new(n1 | n2);
1519                         break;
1520 
1521                 default:
1522                         break;
1523                 }
1524         }
1525         else {
1526                 if (!u1) u1 = (uint64_t)n1;
1527                 if (!u2) u2 = (uint64_t)n2;
1528 
1529                 switch (operation) {
1530                 case I_LSHIFT:
1531                         rv = ucv_uint64_new(u1 << (u2 % (sizeof(uint64_t) * CHAR_BIT)));
1532                         break;
1533 
1534                 case I_RSHIFT:
1535                         rv = ucv_uint64_new(u1 >> (u2 % (sizeof(uint64_t) * CHAR_BIT)));
1536                         break;
1537 
1538                 case I_BAND:
1539                         rv = ucv_uint64_new(u1 & u2);
1540                         break;
1541 
1542                 case I_BXOR:
1543                         rv = ucv_uint64_new(u1 ^ u2);
1544                         break;
1545 
1546                 case I_BOR:
1547                         rv = ucv_uint64_new(u1 | u2);
1548                         break;
1549 
1550                 default:
1551                         break;
1552                 }
1553         }
1554 
1555         ucv_put(nv1);
1556         ucv_put(nv2);
1557 
1558         return rv;
1559 }
1560 
1561 static uc_value_t *
1562 uc_vm_string_concat(uc_vm_t *vm, uc_value_t *v1, uc_value_t *v2)
1563 {
1564         char buf[sizeof(void *)], *s1, *s2;
1565         uc_stringbuf_t *sbuf;
1566         size_t l1, l2;
1567 
1568         /* optimize cases for string+string concat... */
1569         if (ucv_type(v1) == UC_STRING && ucv_type(v2) == UC_STRING) {
1570                 s1 = ucv_string_get(v1);
1571                 s2 = ucv_string_get(v2);
1572                 l1 = ucv_string_length(v1);
1573                 l2 = ucv_string_length(v2);
1574 
1575                 /* ... result fits into a tagged pointer */
1576                 if (l1 + l2 + 1 < sizeof(buf)) {
1577                         memcpy(&buf[0], s1, l1);
1578                         memcpy(&buf[l1], s2, l2);
1579 
1580                         return ucv_string_new_length(buf, l1 + l2);
1581                 }
1582         }
1583 
1584         sbuf = ucv_stringbuf_new();
1585 
1586         ucv_to_stringbuf(vm, sbuf, v1, false);
1587         ucv_to_stringbuf(vm, sbuf, v2, false);
1588 
1589         return ucv_stringbuf_finish(sbuf);
1590 }
1591 
1592 static uint64_t
1593 upow64(uint64_t base, uint64_t exponent)
1594 {
1595         uint64_t result = 1;
1596 
1597         while (exponent) {
1598                 if (exponent & 1)
1599                         result *= base;
1600 
1601                 exponent >>= 1;
1602                 base *= base;
1603         }
1604 
1605         return result;
1606 }
1607 
1608 static uc_value_t *
1609 uc_vm_value_arith(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand)
1610 {
1611         uc_value_t *nv1, *nv2, *rv = NULL;
1612         uint64_t u1, u2;
1613         int64_t n1, n2;
1614         double d1, d2;
1615 
1616         if (operation == I_LSHIFT || operation == I_RSHIFT ||
1617             operation == I_BAND || operation == I_BXOR || operation == I_BOR)
1618                 return uc_vm_value_bitop(vm, operation, value, operand);
1619 
1620         if (operation == I_ADD && (ucv_type(value) == UC_STRING || ucv_type(operand) == UC_STRING))
1621                 return uc_vm_string_concat(vm, value, operand);
1622 
1623         nv1 = ucv_to_number(value);
1624         nv2 = ucv_to_number(operand);
1625 
1626         /* any operation involving NaN results in NaN */
1627         if (!nv1 || !nv2) {
1628                 ucv_put(nv1);
1629                 ucv_put(nv2);
1630 
1631                 return ucv_double_new(NAN);
1632         }
1633         if (ucv_type(nv1) == UC_DOUBLE || ucv_type(nv2) == UC_DOUBLE) {
1634                 d1 = ucv_double_get(nv1);
1635                 d2 = ucv_double_get(nv2);
1636 
1637                 switch (operation) {
1638                 case I_ADD:
1639                 case I_PLUS:
1640                         rv = ucv_double_new(d1 + d2);
1641                         break;
1642 
1643                 case I_SUB:
1644                 case I_MINUS:
1645                         rv = ucv_double_new(d1 - d2);
1646                         break;
1647 
1648                 case I_MUL:
1649                         rv = ucv_double_new(d1 * d2);
1650                         break;
1651 
1652                 case I_DIV:
1653                         if (d2 == 0.0)
1654                                 rv = ucv_double_new(INFINITY);
1655                         else if (isnan(d2))
1656                                 rv = ucv_double_new(NAN);
1657                         else if (!isfinite(d2))
1658                                 rv = ucv_double_new(isfinite(d1) ? 0.0 : NAN);
1659                         else
1660                                 rv = ucv_double_new(d1 / d2);
1661 
1662                         break;
1663 
1664                 case I_MOD:
1665                         rv = ucv_double_new(fmod(d1, d2));
1666                         break;
1667 
1668                 case I_EXP:
1669                         rv = ucv_double_new(pow(d1, d2));
1670                         break;
1671 
1672                 default:
1673                         uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
1674                                               "undefined arithmetic operation %d",
1675                                               operation);
1676                         break;
1677                 }
1678         }
1679         else {
1680                 n1 = int64(nv1, &u1);
1681                 n2 = int64(nv2, &u2);
1682 
1683                 switch (operation) {
1684                 case I_ADD:
1685                 case I_PLUS:
1686                         if (n1 < 0 || n2 < 0) {
1687                                 if (u1)
1688                                         rv = ucv_uint64_new(u1 - abs64(n2));
1689                                 else if (u2)
1690                                         rv = ucv_uint64_new(u2 - abs64(n1));
1691                                 else
1692                                         rv = ucv_int64_new(n1 + n2);
1693                         }
1694                         else {
1695                                 if (!u1) u1 = (uint64_t)n1;
1696                                 if (!u2) u2 = (uint64_t)n2;
1697 
1698                                 rv = ucv_uint64_new(u1 + u2);
1699                         }
1700 
1701                         break;
1702 
1703                 case I_SUB:
1704                 case I_MINUS:
1705                         if (n1 < 0 && n2 < 0) {
1706                                 if (n1 > n2)
1707                                         rv = ucv_uint64_new(abs64(n2) - abs64(n1));
1708                                 else
1709                                         rv = ucv_int64_new(n1 - n2);
1710                         }
1711                         else if (n1 >= 0 && n2 >= 0) {
1712                                 if (!u1) u1 = (uint64_t)n1;
1713                                 if (!u2) u2 = (uint64_t)n2;
1714 
1715                                 if (u2 > u1)
1716                                         rv = ucv_int64_new(-(u2 - u1));
1717                                 else
1718                                         rv = ucv_uint64_new(u1 - u2);
1719                         }
1720                         else if (n1 >= 0) {
1721                                 if (!u1) u1 = (uint64_t)n1;
1722 
1723                                 rv = ucv_uint64_new(u1 + abs64(n2));
1724                         }
1725                         else {
1726                                 rv = ucv_int64_new(n1 - n2);
1727                         }
1728 
1729                         break;
1730 
1731                 case I_MUL:
1732                         if (n1 < 0 && n2 < 0) {
1733                                 rv = ucv_uint64_new(abs64(n1) * abs64(n2));
1734                         }
1735                         else if (n1 >= 0 && n2 >= 0) {
1736                                 if (!u1) u1 = (uint64_t)n1;
1737                                 if (!u2) u2 = (uint64_t)n2;
1738 
1739                                 rv = ucv_uint64_new(u1 * u2);
1740                         }
1741                         else {
1742                                 rv = ucv_int64_new(n1 * n2);
1743                         }
1744 
1745                         break;
1746 
1747                 case I_DIV:
1748                         if (n2 == 0) {
1749                                 rv = ucv_double_new(INFINITY);
1750                         }
1751                         else if (n1 < 0 || n2 < 0) {
1752                                 rv = ucv_int64_new(n1 / n2);
1753                         }
1754                         else {
1755                                 if (!u1) u1 = (uint64_t)n1;
1756                                 if (!u2) u2 = (uint64_t)n2;
1757 
1758                                 rv = ucv_uint64_new(u1 / u2);
1759                         }
1760 
1761                         break;
1762 
1763                 case I_MOD:
1764                         if (n2 == 0) {
1765                                 rv = ucv_double_new(NAN);
1766                         }
1767                         else if (n1 < 0 || n2 < 0) {
1768                                 rv = ucv_int64_new(n1 % n2);
1769                         }
1770                         else {
1771                                 if (!u1) u1 = (uint64_t)n1;
1772                                 if (!u2) u2 = (uint64_t)n2;
1773 
1774                                 rv = ucv_uint64_new(u1 % u2);
1775                         }
1776 
1777                         break;
1778 
1779                 case I_EXP:
1780                         if (n1 < 0 || n2 < 0) {
1781                                 if (n1 < 0 && n2 < 0)
1782                                         rv = ucv_double_new(-(1.0 / (double)upow64(abs64(n1), abs64(n2))));
1783                                 else if (n2 < 0)
1784                                         rv = ucv_double_new(1.0 / (double)upow64(abs64(n1), abs64(n2)));
1785                                 else
1786                                         rv = ucv_int64_new(-upow64(abs64(n1), abs64(n2)));
1787                         }
1788                         else {
1789                                 if (!u1) u1 = (uint64_t)n1;
1790                                 if (!u2) u2 = (uint64_t)n2;
1791 
1792                                 rv = ucv_uint64_new(upow64(u1, u2));
1793                         }
1794 
1795                         break;
1796 
1797                 default:
1798                         uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
1799                                               "undefined arithmetic operation %d",
1800                                               operation);
1801                         break;
1802                 }
1803         }
1804 
1805         ucv_put(nv1);
1806         ucv_put(nv2);
1807 
1808         return rv;
1809 }
1810 
1811 static void
1812 uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn)
1813 {
1814         uc_value_t *name, *val = NULL, *inc = uc_vm_stack_pop(vm);
1815         uc_value_t *scope, *next;
1816         bool found;
1817 
1818         scope = vm->globals;
1819         name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32 & 0x00FFFFFF);
1820 
1821         assert(ucv_type(name) == UC_STRING);
1822 
1823         while (true) {
1824                 val = ucv_object_get(scope, ucv_string_get(name), &found);
1825 
1826                 if (found)
1827                         break;
1828 
1829                 next = ucv_prototype_get(scope);
1830 
1831                 if (!next) {
1832                         if (uc_vm_is_strict(vm)) {
1833                                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
1834                                                       "access to undeclared variable %s",
1835                                                       ucv_string_get(name));
1836                         }
1837 
1838                         break;
1839                 }
1840 
1841                 scope = next;
1842         }
1843 
1844         val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, val, inc);
1845 
1846         ucv_object_add(scope, ucv_string_get(name), ucv_get(val));
1847         uc_vm_stack_push(vm, val);
1848 
1849         ucv_put(name);
1850         ucv_put(inc);
1851 }
1852 
1853 static void
1854 uc_vm_insn_update_val(uc_vm_t *vm, uc_vm_insn_t insn)
1855 {
1856         uc_value_t *inc = uc_vm_stack_pop(vm);
1857         uc_value_t *k = uc_vm_stack_pop(vm);
1858         uc_value_t *v = uc_vm_stack_pop(vm);
1859         uc_value_t *val = NULL;
1860 
1861         switch (ucv_type(v)) {
1862         case UC_OBJECT:
1863         case UC_ARRAY:
1864                 if (assert_mutable_value(vm, v)) {
1865                         val = ucv_key_get(vm, v, k);
1866                         uc_vm_stack_push(vm, ucv_key_set(vm, v, k, uc_vm_value_arith(vm, vm->arg.u8, val, inc)));
1867                 }
1868 
1869                 break;
1870 
1871         default:
1872                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
1873                                       "left-hand side expression is %s",
1874                                       v ? "not an array or object" : "null");
1875 
1876                 break;
1877         }
1878 
1879         ucv_put(val);
1880         ucv_put(inc);
1881         ucv_put(v);
1882         ucv_put(k);
1883 }
1884 
1885 static void
1886 uc_vm_insn_update_upval(uc_vm_t *vm, uc_vm_insn_t insn)
1887 {
1888         uc_callframe_t *frame = uc_vm_current_frame(vm);
1889         size_t slot = vm->arg.u32 & 0x00FFFFFF;
1890         uc_upvalref_t *ref = frame->closure->upvals[slot];
1891         uc_value_t *inc = uc_vm_stack_pop(vm);
1892         uc_value_t *val;
1893 
1894         if (ref->closed)
1895                 val = ref->value;
1896         else
1897                 val = vm->stack.entries[ref->slot];
1898 
1899         val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, val, inc);
1900 
1901         uc_vm_stack_push(vm, val);
1902 
1903         ucv_put(inc);
1904 
1905         if (ref->closed) {
1906                 ucv_put(ref->value);
1907                 ref->value = ucv_get(uc_vm_stack_peek(vm, 0));
1908         }
1909         else {
1910                 uc_vm_stack_set(vm, ref->slot, ucv_get(uc_vm_stack_peek(vm, 0)));
1911         }
1912 }
1913 
1914 static void
1915 uc_vm_insn_update_local(uc_vm_t *vm, uc_vm_insn_t insn)
1916 {
1917         uc_callframe_t *frame = uc_vm_current_frame(vm);
1918         size_t slot = vm->arg.u32 & 0x00FFFFFF;
1919         uc_value_t *inc = uc_vm_stack_pop(vm);
1920         uc_value_t *val;
1921 
1922         val = uc_vm_value_arith(vm, vm->arg.u32 >> 24,
1923                                 vm->stack.entries[frame->stackframe + slot], inc);
1924 
1925         uc_vm_stack_push(vm, val);
1926 
1927         ucv_put(inc);
1928         uc_vm_stack_set(vm, frame->stackframe + slot, ucv_get(uc_vm_stack_peek(vm, 0)));
1929 }
1930 
1931 static void
1932 uc_vm_insn_narr(uc_vm_t *vm, uc_vm_insn_t insn)
1933 {
1934         uc_value_t *arr = ucv_array_new_length(vm, vm->arg.u32);
1935 
1936         uc_vm_stack_push(vm, arr);
1937 }
1938 
1939 static void
1940 uc_vm_insn_parr(uc_vm_t *vm, uc_vm_insn_t insn)
1941 {
1942         uc_value_t *arr = uc_vm_stack_peek(vm, vm->arg.u32);
1943         size_t idx;
1944 
1945         for (idx = 0; idx < vm->arg.u32; idx++)
1946                 ucv_array_push(arr, uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1));
1947 
1948         for (idx = 0; idx < vm->arg.u32; idx++)
1949                 uc_vm_stack_pop(vm);
1950 
1951         //uc_vm_shrink(state, vm->arg.u32);
1952 }
1953 
1954 static void
1955 uc_vm_insn_marr(uc_vm_t *vm, uc_vm_insn_t insn)
1956 {
1957         uc_value_t *src = uc_vm_stack_pop(vm);
1958         uc_value_t *dst = uc_vm_stack_peek(vm, 0);
1959         size_t i;
1960         char *s;
1961 
1962         if (ucv_type(src) != UC_ARRAY) {
1963                 s = ucv_to_string(vm, src);
1964                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s);
1965                 ucv_put(src);
1966                 free(s);
1967 
1968                 return;
1969         }
1970 
1971         for (i = 0; i < ucv_array_length(src); i++)
1972                 ucv_array_push(dst, ucv_get(ucv_array_get(src, i)));
1973 
1974         ucv_put(src);
1975 }
1976 
1977 static void
1978 uc_vm_insn_nobj(uc_vm_t *vm, uc_vm_insn_t insn)
1979 {
1980         uc_value_t *obj = ucv_object_new(vm);
1981 
1982         uc_vm_stack_push(vm, obj);
1983 }
1984 
1985 static void
1986 uc_vm_insn_sobj(uc_vm_t *vm, uc_vm_insn_t insn)
1987 {
1988         uc_value_t *obj = uc_vm_stack_peek(vm, vm->arg.u32);
1989         size_t idx;
1990 
1991         for (idx = 0; idx < vm->arg.u32; idx += 2)
1992                 ucv_key_set(vm, obj,
1993                         uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1),
1994                         uc_vm_stack_peek(vm, vm->arg.u32 - idx - 2));
1995 
1996         for (idx = 0; idx < vm->arg.u32; idx++)
1997                 ucv_put(uc_vm_stack_pop(vm));
1998 }
1999 
2000 static void
2001 uc_vm_insn_mobj(uc_vm_t *vm, uc_vm_insn_t insn)
2002 {
2003         uc_value_t *src = uc_vm_stack_pop(vm);
2004         uc_value_t *dst = uc_vm_stack_peek(vm, 0);
2005         size_t i;
2006         char *s;
2007 
2008         switch (ucv_type(src)) {
2009         case UC_OBJECT:
2010                 ; /* a label can only be part of a statement and a declaration is not a statement */
2011                 ucv_object_foreach(src, k, v)
2012                         ucv_object_add(dst, k, ucv_get(v));
2013 
2014                 ucv_put(src);
2015                 break;
2016 
2017         case json_type_array:
2018                 for (i = 0; i < ucv_array_length(src); i++) {
2019                         xasprintf(&s, "%zu", i);
2020                         ucv_object_add(dst, s, ucv_get(ucv_array_get(src, i)));
2021                         free(s);
2022                 }
2023 
2024                 ucv_put(src);
2025                 break;
2026 
2027         default:
2028                 s = ucv_to_string(vm, src);
2029                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Value (%s) is not iterable", s);
2030                 free(s);
2031 
2032                 break;
2033         }
2034 }
2035 
2036 static void
2037 uc_vm_insn_arith(uc_vm_t *vm, uc_vm_insn_t insn)
2038 {
2039         uc_value_t *r2 = uc_vm_stack_pop(vm);
2040         uc_value_t *r1 = uc_vm_stack_pop(vm);
2041         uc_value_t *rv;
2042 
2043         rv = uc_vm_value_arith(vm, insn, r1, r2);
2044 
2045         ucv_put(r1);
2046         ucv_put(r2);
2047 
2048         uc_vm_stack_push(vm, rv);
2049 }
2050 
2051 static void
2052 uc_vm_insn_plus_minus(uc_vm_t *vm, uc_vm_insn_t insn)
2053 {
2054         uc_value_t *v = uc_vm_stack_pop(vm), *nv;
2055         bool is_sub = (insn == I_MINUS);
2056         int64_t n;
2057         double d;
2058 
2059         if (ucv_type(v) == UC_STRING) {
2060                 nv = uc_number_parse(ucv_string_get(v), NULL);
2061 
2062                 if (nv) {
2063                         ucv_put(v);
2064                         v = nv;
2065                 }
2066         }
2067 
2068         switch (ucv_type(v)) {
2069         case UC_INTEGER:
2070                 n = ucv_int64_get(v);
2071 
2072                 /* numeric value is in range 9223372036854775808..18446744073709551615 */
2073                 if (errno == ERANGE) {
2074                         if (is_sub)
2075                                 /* make negation of large numeric value result in smallest negative value */
2076                                 uc_vm_stack_push(vm, ucv_int64_new(INT64_MIN));
2077                         else
2078                                 /* for positive number coercion return value as-is */
2079                                 uc_vm_stack_push(vm, ucv_get(v));
2080                 }
2081 
2082                 /* numeric value is in range -9223372036854775808..9223372036854775807 */
2083                 else {
2084                         if (is_sub) {
2085                                 if (n == INT64_MIN)
2086                                         /* make negation of minimum value result in maximum signed positive value */
2087                                         uc_vm_stack_push(vm, ucv_int64_new(INT64_MAX));
2088                                 else
2089                                         /* for all other values flip the sign */
2090                                         uc_vm_stack_push(vm, ucv_int64_new(-n));
2091                         }
2092                         else {
2093                                 /* for positive number coercion return value as-is */
2094                                 uc_vm_stack_push(vm, ucv_get(v));
2095                         }
2096                 }
2097 
2098                 break;
2099 
2100         case UC_DOUBLE:
2101                 d = ucv_double_get(v);
2102                 uc_vm_stack_push(vm, ucv_double_new(is_sub ? -d : d));
2103                 break;
2104 
2105         case UC_BOOLEAN:
2106                 n = (int64_t)ucv_boolean_get(v);
2107                 uc_vm_stack_push(vm, ucv_int64_new(is_sub ? -n : n));
2108                 break;
2109 
2110         case UC_NULL:
2111                 uc_vm_stack_push(vm, ucv_int64_new(0));
2112                 break;
2113 
2114         default:
2115                 uc_vm_stack_push(vm, ucv_double_new(NAN));
2116         }
2117 
2118         ucv_put(v);
2119 }
2120 
2121 static void
2122 uc_vm_insn_bitop(uc_vm_t *vm, uc_vm_insn_t insn)
2123 {
2124         uc_value_t *r2 = uc_vm_stack_pop(vm);
2125         uc_value_t *r1 = uc_vm_stack_pop(vm);
2126         uc_value_t *rv;
2127 
2128         rv = uc_vm_value_bitop(vm, insn, r1, r2);
2129 
2130         ucv_put(r1);
2131         ucv_put(r2);
2132 
2133         uc_vm_stack_push(vm, rv);
2134 }
2135 
2136 static void
2137 uc_vm_insn_complement(uc_vm_t *vm, uc_vm_insn_t insn)
2138 {
2139         uc_value_t *v = uc_vm_stack_pop(vm);
2140         uc_value_t *nv;
2141         uint64_t u;
2142         int64_t n;
2143 
2144         nv = ucv_to_number(v);
2145         n = int64(nv, &u);
2146 
2147         if (n < 0) {
2148                 uc_vm_stack_push(vm, ucv_int64_new(~n));
2149         }
2150         else {
2151                 if (!u) u = (uint64_t)n;
2152 
2153                 uc_vm_stack_push(vm, ucv_uint64_new(~u));
2154         }
2155 
2156         ucv_put(nv);
2157         ucv_put(v);
2158 }
2159 
2160 static void
2161 uc_vm_insn_rel(uc_vm_t *vm, uc_vm_insn_t insn)
2162 {
2163         uc_value_t *r2 = uc_vm_stack_pop(vm);
2164         uc_value_t *r1 = uc_vm_stack_pop(vm);
2165 
2166         bool res = ucv_compare(insn, r1, r2, NULL);
2167 
2168         ucv_put(r1);
2169         ucv_put(r2);
2170 
2171         uc_vm_stack_push(vm, ucv_boolean_new(res));
2172 }
2173 
2174 static void
2175 uc_vm_insn_in(uc_vm_t *vm, uc_vm_insn_t insn)
2176 {
2177         uc_value_t *r2 = uc_vm_stack_pop(vm);
2178         uc_value_t *r1 = uc_vm_stack_pop(vm);
2179         uc_value_t *item;
2180         size_t arrlen, arridx;
2181         bool found = false;
2182 
2183         switch (ucv_type(r2)) {
2184         case UC_ARRAY:
2185                 for (arridx = 0, arrlen = ucv_array_length(r2);
2186                      arridx < arrlen; arridx++) {
2187                         item = ucv_array_get(r2, arridx);
2188 
2189                         if (uc_vm_test_strict_equality(r1, item, true)) {
2190                                 found = true;
2191                                 break;
2192                         }
2193                 }
2194 
2195                 break;
2196 
2197         case UC_OBJECT:
2198                 if (ucv_type(r1) == UC_STRING)
2199                         ucv_object_get(r2, ucv_string_get(r1), &found);
2200 
2201                 break;
2202 
2203         default:
2204                 found = false;
2205         }
2206 
2207         ucv_put(r1);
2208         ucv_put(r2);
2209 
2210         uc_vm_stack_push(vm, ucv_boolean_new(found));
2211 }
2212 
2213 static void
2214 uc_vm_insn_equality(uc_vm_t *vm, uc_vm_insn_t insn)
2215 {
2216         uc_value_t *r2 = uc_vm_stack_pop(vm);
2217         uc_value_t *r1 = uc_vm_stack_pop(vm);
2218         bool equal = uc_vm_test_strict_equality(r1, r2, false);
2219 
2220         ucv_put(r1);
2221         ucv_put(r2);
2222 
2223         uc_vm_stack_push(vm, ucv_boolean_new((insn == I_EQS) ? equal : !equal));
2224 }
2225 
2226 static void
2227 uc_vm_insn_not(uc_vm_t *vm, uc_vm_insn_t insn)
2228 {
2229         uc_value_t *r1 = uc_vm_stack_pop(vm);
2230 
2231         uc_vm_stack_push(vm, ucv_boolean_new(!ucv_is_truish(r1)));
2232         ucv_put(r1);
2233 }
2234 
2235 static void
2236 uc_vm_insn_jmp(uc_vm_t *vm, uc_vm_insn_t insn)
2237 {
2238         uc_callframe_t *frame = uc_vm_current_frame(vm);
2239         uc_chunk_t *chunk = uc_vm_frame_chunk(frame);
2240         int32_t addr = vm->arg.s32;
2241 
2242         /* ip already has been incremented */
2243         addr -= 5;
2244 
2245         if (frame->ip + addr < chunk->entries ||
2246             frame->ip + addr >= chunk->entries + chunk->count) {
2247                 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range");
2248                 return;
2249         }
2250 
2251         frame->ip += addr;
2252 }
2253 
2254 static void
2255 uc_vm_insn_jmpz(uc_vm_t *vm, uc_vm_insn_t insn)
2256 {
2257         uc_callframe_t *frame = uc_vm_current_frame(vm);
2258         uc_chunk_t *chunk = uc_vm_frame_chunk(frame);
2259         uc_value_t *v = uc_vm_stack_pop(vm);
2260         int32_t addr = vm->arg.s32;
2261 
2262         /* ip already has been incremented */
2263         addr -= 5;
2264 
2265         if (frame->ip + addr < chunk->entries ||
2266             frame->ip + addr >= chunk->entries + chunk->count) {
2267                 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range");
2268                 return;
2269         }
2270 
2271         if (!ucv_is_truish(v))
2272                 frame->ip += addr;
2273 
2274         ucv_put(v);
2275 }
2276 
2277 static void
2278 uc_vm_insn_jmpnt(uc_vm_t *vm, uc_vm_insn_t insn)
2279 {
2280         uc_callframe_t *frame = uc_vm_current_frame(vm);
2281         uc_chunk_t *chunk = uc_vm_frame_chunk(frame);
2282         int16_t addr = (vm->arg.u32 & 0xffff) - 0x7fff;
2283         uint16_t types = (vm->arg.u32 >> 16) & 0x1fff;
2284         uint8_t depth = (vm->arg.u32 >> 29) & 0x7;
2285         uc_value_t *v = uc_vm_stack_peek(vm, 0);
2286         size_t i;
2287 
2288         /* ip already has been incremented */
2289         addr -= 5;
2290 
2291         if (frame->ip + addr < chunk->entries ||
2292             frame->ip + addr >= chunk->entries + chunk->count) {
2293                 uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range");
2294                 return;
2295         }
2296 
2297         if (!(types & (1u << ucv_type(v)))) {
2298                 for (i = 0; i <= depth; i++)
2299                         ucv_put(uc_vm_stack_pop(vm));
2300 
2301                 uc_vm_stack_push(vm, NULL);
2302                 frame->ip += addr;
2303         }
2304 }
2305 
2306 
2307 static void
2308 uc_vm_object_iterator_free(void *ud)
2309 {
2310         uc_object_iterator_t *iter = ud;
2311 
2312         uc_list_remove(&iter->list);
2313 }
2314 
2315 static uc_resource_type_t uc_vm_object_iterator_type = {
2316         .name = "object iterator",
2317         .free = uc_vm_object_iterator_free
2318 };
2319 
2320 static bool
2321 uc_vm_object_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn,
2322                            uc_value_t *k, uc_value_t *v)
2323 {
2324         uc_resource_t *res = (uc_resource_t *)k;
2325         uc_object_t *obj = (uc_object_t *)v;
2326         uc_object_iterator_t *iter;
2327 
2328         if (!res) {
2329                 /* object is empty */
2330                 if (!obj->table->head)
2331                         return false;
2332 
2333                 res = xalloc(sizeof(*res) + sizeof(uc_object_iterator_t));
2334                 res->header.type = UC_RESOURCE;
2335                 res->header.refcount = 1;
2336                 res->type = &uc_vm_object_iterator_type;
2337 
2338                 iter = res->data = (char *)res + sizeof(*res);
2339                 iter->table = obj->table;
2340                 iter->u.pos = obj->table->head;
2341 
2342                 uc_list_insert(&uc_thread_context_get()->object_iterators, &iter->list);
2343         }
2344         else if (ucv_type(k) == UC_RESOURCE &&
2345                  res->type == &uc_vm_object_iterator_type && res->data != NULL) {
2346 
2347                 iter = res->data;
2348         }
2349         else {
2350                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid object iterator");
2351 
2352                 return false;
2353         }
2354 
2355         /* no next key */
2356         if (!iter->u.pos) {
2357                 uc_list_remove(&iter->list);
2358 
2359                 return false;
2360         }
2361 
2362         uc_vm_stack_push(vm, ucv_string_new(iter->u.pos->k));
2363 
2364         if (insn == I_NEXTKV)
2365                 uc_vm_stack_push(vm, ucv_get((uc_value_t *)iter->u.pos->v));
2366 
2367         uc_vm_stack_push(vm, &res->header);
2368         ucv_put(v);
2369 
2370         iter->u.pos = iter->u.pos->next;
2371 
2372         return true;
2373 }
2374 
2375 static bool
2376 uc_vm_array_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn,
2377                           uc_value_t *k, uc_value_t *v)
2378 {
2379         uint64_t n;
2380 
2381         if (!k) {
2382                 /* array is empty */
2383                 if (!ucv_array_length(v))
2384                         return false;
2385 
2386                 k = ucv_resource_new(NULL, NULL);
2387                 n = 0;
2388         }
2389         else if (ucv_type(k) == UC_RESOURCE) {
2390                 n = (uintptr_t)ucv_resource_data(k, NULL);
2391         }
2392         else {
2393                 uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid array iterator");
2394 
2395                 return false;
2396         }
2397 
2398         /* no next index */
2399         if (n >= ucv_array_length(v))
2400                 return false;
2401 
2402         if (insn == I_NEXTKV)
2403                 uc_vm_stack_push(vm, ucv_uint64_new(n));
2404 
2405         uc_vm_stack_push(vm, ucv_get(ucv_array_get(v, n)));
2406 
2407         uc_vm_stack_push(vm, k);
2408         ucv_put(v);
2409 
2410         ((uc_resource_t *)k)->data = (void *)(uintptr_t)(n + 1);
2411 
2412         return true;
2413 }
2414 
2415 static void
2416 uc_vm_insn_next(uc_vm_t *vm, uc_vm_insn_t insn)
2417 {
2418         uc_value_t *k = uc_vm_stack_pop(vm);
2419         uc_value_t *v = uc_vm_stack_pop(vm);
2420 
2421         switch (ucv_type(v)) {
2422         case UC_OBJECT:
2423                 if (uc_vm_object_iterator_next(vm, insn, k, v))
2424                         return;
2425 
2426                 break;
2427 
2428         case UC_ARRAY:
2429                 if (uc_vm_array_iterator_next(vm, insn, k, v))
2430                         return;
2431 
2432                 break;
2433 
2434         default:
2435                 break;
2436         }
2437 
2438         uc_vm_stack_push(vm, NULL);
2439         uc_vm_stack_push(vm, NULL);
2440 
2441         if (insn == I_NEXTKV)
2442                 uc_vm_stack_push(vm, NULL);
2443 
2444         ucv_put(k);
2445         ucv_put(v);
2446 }
2447 
2448 static void
2449 uc_vm_insn_close_upval(uc_vm_t *vm, uc_vm_insn_t insn)
2450 {
2451         uc_vm_close_upvals(vm, vm->stack.count - 1);
2452         ucv_put(uc_vm_stack_pop(vm));
2453 }
2454 
2455 static void
2456 uc_vm_insn_call(uc_vm_t *vm, uc_vm_insn_t insn)
2457 {
2458         bool mcall = (vm->arg.u32 & 0x80000000);
2459         size_t nargs = (vm->arg.u32 & 0xffff);
2460         uc_value_t *fno = uc_vm_stack_peek(vm, nargs);
2461         uc_value_t *ctx = NULL;
2462 
2463         if (!ucv_is_arrowfn(fno))
2464                 ctx = mcall ? uc_vm_stack_peek(vm, nargs + 1) : NULL;
2465         else if (vm->callframes.count > 0)
2466                 ctx = uc_vm_current_frame(vm)->ctx;
2467 
2468         uc_vm_call_function(vm, ucv_get(ctx), ucv_get(fno), mcall, vm->arg.u32);
2469 }
2470 
2471 static void
2472 uc_vm_insn_print(uc_vm_t *vm, uc_vm_insn_t insn)
2473 {
2474         uc_value_t *v = uc_vm_stack_pop(vm);
2475         char *p;
2476 
2477         switch (ucv_type(v)) {
2478         case UC_OBJECT:
2479         case UC_ARRAY:
2480                 p = ucv_to_jsonstring(vm, v);
2481                 fwrite(p, 1, strlen(p), vm->output);
2482                 free(p);
2483                 break;
2484 
2485         case UC_STRING:
2486                 fwrite(ucv_string_get(v), 1, ucv_string_length(v), vm->output);
2487                 break;
2488 
2489         case UC_NULL:
2490                 break;
2491 
2492         default:
2493                 p = ucv_to_string(vm, v);
2494                 fwrite(p, 1, strlen(p), vm->output);
2495                 free(p);
2496         }
2497 
2498         ucv_put(v);
2499 }
2500 
2501 static void
2502 uc_vm_insn_delete(uc_vm_t *vm, uc_vm_insn_t insn)
2503 {
2504         uc_value_t *k = uc_vm_stack_pop(vm);
2505         uc_value_t *v = uc_vm_stack_pop(vm);
2506         bool rv;
2507 
2508         switch (ucv_type(v)) {
2509         case UC_OBJECT:
2510                 if (assert_mutable_value(vm, v)) {
2511                         rv = ucv_key_delete(vm, v, k);
2512                         uc_vm_stack_push(vm, ucv_boolean_new(rv));
2513                 }
2514 
2515                 break;
2516 
2517         default:
2518                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
2519                                       "left-hand side expression is %s",
2520                                       v ? "not an object" : "null");
2521 
2522                 break;
2523         }
2524 
2525         ucv_put(k);
2526         ucv_put(v);
2527 }
2528 
2529 static void
2530 uc_vm_insn_import(uc_vm_t *vm, uc_vm_insn_t insn)
2531 {
2532         uc_callframe_t *frame = uc_vm_current_frame(vm);
2533         uc_program_t *prog = uc_vm_current_program(vm);
2534         uint16_t from = vm->arg.u32 & 0xffff;
2535         uint16_t to = vm->arg.u32 >> 16;
2536         uc_value_t *name, *modobj;
2537         uint32_t cidx;
2538 
2539         /* is a wildcard import * from ... */
2540         if (to == 0xffff) {
2541                 to = from;
2542                 modobj = ucv_object_new(vm);
2543 
2544                 /* instruction is followed by u16 containing the offset of the
2545                  * first module export and `from` times u32 values containing
2546                  * the constant indexes of the names */
2547                 for (from = frame->ip[0] * 0x100 + frame->ip[1], frame->ip += 2;
2548                      from < prog->exports.count && to > 0;
2549                      from++, to--) {
2550 
2551                         cidx = (
2552                                 frame->ip[0] * 0x1000000UL +
2553                                 frame->ip[1] * 0x10000UL +
2554                                 frame->ip[2] * 0x100UL +
2555                                 frame->ip[3]
2556                         );
2557 
2558                         frame->ip += 4;
2559 
2560                         name = uc_program_get_constant(uc_vm_current_program(vm), cidx);
2561 
2562                         if (ucv_type(name) == UC_STRING && prog->exports.entries[from])
2563                                 ucv_object_add(modobj, ucv_string_get(name),
2564                                         ucv_get(&prog->exports.entries[from]->header));
2565 
2566                         ucv_put(name);
2567                 }
2568 
2569                 ucv_set_constant(modobj, true);
2570 
2571                 uc_vm_stack_push(vm, modobj);
2572         }
2573 
2574         /* module export available, patch into upvalue */
2575         else if (from <= prog->exports.count && prog->exports.entries[from]) {
2576                 frame->closure->upvals[to] = prog->exports.entries[from];
2577                 ucv_get(&prog->exports.entries[from]->header);
2578         }
2579 
2580         /* module export missing, e.g. due to premature return in module,
2581          * patch up dummy upvalue ref with `null` value */
2582         else {
2583                 frame->closure->upvals[to] = (uc_upvalref_t *)ucv_upvalref_new(0);
2584                 frame->closure->upvals[to]->closed = true;
2585         }
2586 }
2587 
2588 static void
2589 uc_vm_insn_export(uc_vm_t *vm, uc_vm_insn_t insn)
2590 {
2591         uc_callframe_t *frame = uc_vm_current_frame(vm);
2592         uc_program_t *prog = uc_vm_current_program(vm);
2593         uc_upvalref_t *ref = uc_vm_capture_upval(vm, frame->stackframe + vm->arg.u32);
2594 
2595         uc_vector_push(&prog->exports, ref);
2596         ucv_get(&ref->header);
2597 }
2598 
2599 static void
2600 uc_vm_insn_dynload(uc_vm_t *vm, uc_vm_insn_t insn)
2601 {
2602         uc_callframe_t *frame = uc_vm_current_frame(vm);
2603         uc_value_t *name, *export, *modscope, *modobj;
2604         uint16_t count = vm->arg.u32 & 0xffff;
2605         uint16_t to = vm->arg.u32 >> 16;
2606         uint32_t cidx;
2607         bool found;
2608 
2609         /* instruction is followed by u32 containing the constant index of the
2610          * module name string to import and `count` times u32 values containing
2611          * the import name constant indexes */
2612 
2613         cidx = (
2614                 frame->ip[0] * 0x1000000UL +
2615                 frame->ip[1] * 0x10000UL +
2616                 frame->ip[2] * 0x100UL +
2617                 frame->ip[3]
2618         );
2619 
2620         frame->ip += 4;
2621 
2622         /* push module name onto stack, then attempt to load module and pop
2623          * name value again. Will raise exception on error */
2624         name = uc_program_get_constant(uc_vm_current_program(vm), cidx);
2625         modscope = uc_require_library(vm, name, true);
2626         ucv_put(name);
2627 
2628         if (!modscope)
2629                 return;
2630 
2631         /* If count is zero, we're doing a wildcard import. Shallow copy module
2632          * object, mark it constant and patch into the target upvalue. */
2633         if (count == 0) {
2634                 modobj = ucv_object_new(vm);
2635 
2636                 ucv_object_foreach(modscope, k, v)
2637                         ucv_object_add(modobj, k, ucv_get(v));
2638 
2639                 ucv_set_constant(modobj, true);
2640 
2641                 uc_vm_stack_push(vm, modobj);
2642         }
2643 
2644         /* ... otherwise we're importing a specific list of names */
2645         else {
2646                 while (count > 0) {
2647                         cidx = (
2648                                 frame->ip[0] * 0x1000000UL +
2649                                 frame->ip[1] * 0x10000UL +
2650                                 frame->ip[2] * 0x100UL +
2651                                 frame->ip[3]
2652                         );
2653 
2654                         frame->ip += 4;
2655 
2656                         name = uc_program_get_constant(uc_vm_current_program(vm), cidx);
2657                         export = ucv_object_get(modscope, ucv_string_get(name), &found);
2658 
2659                         if (!found) {
2660                                 uc_vm_raise_exception(vm, EXCEPTION_REFERENCE,
2661                                                       "Module does not export %s",
2662                                                       ucv_string_get(name));
2663 
2664                                 ucv_put(name);
2665 
2666                                 return;
2667                         }
2668 
2669                         ucv_put(name);
2670 
2671                         frame->closure->upvals[to] = (uc_upvalref_t *)ucv_upvalref_new(0);
2672                         frame->closure->upvals[to]->closed = true;
2673                         frame->closure->upvals[to]->value = ucv_get(export);
2674 
2675                         count--;
2676                         to++;
2677                 }
2678         }
2679 }
2680 
2681 static void
2682 uc_vm_gc_step(uc_vm_t *vm)
2683 {
2684         size_t curr_count = 0, prev_count = 0;
2685         uc_weakref_t *ref;
2686 
2687         if (!(vm->gc_flags & GC_ENABLED))
2688                 return;
2689 
2690         if (vm->alloc_refs >= vm->gc_interval) {
2691                 if (vm->trace) {
2692                         for (ref = vm->values.next; ref != &vm->values; ref = ref->next)
2693                                 prev_count++;
2694 
2695                         ucv_gc(vm);
2696 
2697                         for (ref = vm->values.next; ref != &vm->values; ref = ref->next)
2698                                 curr_count++;
2699 
2700                         fprintf(stderr, "! GC reclaimed %zu object(s)\n", prev_count - curr_count);
2701                 }
2702                 else {
2703                         ucv_gc(vm);
2704                 }
2705         }
2706 }
2707 
2708 static uc_value_t *
2709 uc_vm_callframe_pop(uc_vm_t *vm)
2710 {
2711         uc_callframe_t *frame = uc_vm_current_frame(vm);
2712         uc_value_t *retval;
2713 
2714         /* close upvalues */
2715         uc_vm_close_upvals(vm, frame->stackframe);
2716 
2717         if (vm->stack.count > frame->stackframe)
2718                 retval = uc_vm_stack_pop(vm);
2719         else
2720                 retval = NULL;
2721 
2722         /* reset function stack frame */
2723         while (vm->stack.count > frame->stackframe)
2724                 ucv_put(uc_vm_stack_pop(vm));
2725 
2726         /* for method calls, release context as well */
2727         if (frame->mcall)
2728                 ucv_put(uc_vm_stack_pop(vm));
2729 
2730         /* release function */
2731         if (frame->closure)
2732                 ucv_put(&frame->closure->header);
2733 
2734         if (frame->cfunction)
2735                 ucv_put(&frame->cfunction->header);
2736 
2737         /* release context */
2738         ucv_put(frame->ctx);
2739 
2740         vm->callframes.count--;
2741 
2742         return retval;
2743 }
2744 
2745 static void
2746 uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex)
2747 {
2748         uc_value_t *ctx;
2749 
2750         if (ex->type == EXCEPTION_USER)
2751                 fprintf(stderr, "%s\n", ex->message);
2752         else
2753                 fprintf(stderr, "%s: %s\n",
2754                             exception_type_strings[ex->type] ? exception_type_strings[ex->type] : "Error",
2755                             ex->message);
2756 
2757         ctx = ucv_object_get(ucv_array_get(ex->stacktrace, 0), "context", NULL);
2758 
2759         if (ctx)
2760                 fprintf(stderr, "%s\n", ucv_string_get(ctx));
2761 
2762         fprintf(stderr, "\n");
2763 }
2764 
2765 uc_exception_type_t
2766 uc_vm_signal_dispatch(uc_vm_t *vm)
2767 {
2768         uc_exception_type_t ex;
2769         uc_value_t *handler;
2770         uint64_t mask;
2771         size_t i, j;
2772         int sig, rv;
2773 
2774         if (!vm->config->setup_signal_handlers)
2775                 return EXCEPTION_NONE;
2776 
2777         for (i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) {
2778                 if (!vm->signal.raised[i])
2779                         continue;
2780 
2781                 do {
2782                         rv = read(vm->signal.sigpipe[0], &sig, sizeof(sig));
2783                 } while (rv > 0 || (rv == -1 && errno == EINTR));
2784 
2785                 for (j = 0; j < 64; j++) {
2786                         mask = 1ull << j;
2787 
2788                         if (vm->signal.raised[i] & mask) {
2789                                 vm->signal.raised[i] &= ~mask;
2790 
2791                                 sig = i * 64 + j;
2792                                 handler = ucv_array_get(vm->signal.handler, sig);
2793 
2794                                 if (ucv_is_callable(handler)) {
2795                                         uc_vm_stack_push(vm, ucv_get(handler));
2796                                         uc_vm_stack_push(vm, ucv_int64_new(sig));
2797 
2798                                         ex = uc_vm_call(vm, false, 1);
2799 
2800                                         if (ex != EXCEPTION_NONE)
2801                                                 return ex;
2802 
2803                                         ucv_put(uc_vm_stack_pop(vm));
2804                                 }
2805                         }
2806                 }
2807         }
2808 
2809         return EXCEPTION_NONE;
2810 }
2811 
2812 static uc_vm_status_t
2813 uc_vm_execute_chunk(uc_vm_t *vm)
2814 {
2815         uc_callframe_t *frame = NULL;
2816         uc_chunk_t *chunk = NULL;
2817         size_t caller = vm->callframes.count - 1;
2818         uc_value_t *retval;
2819         uc_vm_insn_t insn;
2820         uint8_t *ip;
2821 
2822         while (vm->callframes.count > caller) {
2823                 frame = &vm->callframes.entries[vm->callframes.count - 1];
2824                 chunk = uc_vm_frame_chunk(frame);
2825 
2826                 if (!chunk)
2827                         break;
2828 
2829                 if (vm->trace) {
2830                         ip = frame->ip;
2831                         insn = uc_vm_decode_insn(vm, frame, chunk);
2832                         uc_dump_insn(vm, ip, insn);
2833                 }
2834                 else {
2835                         insn = uc_vm_decode_insn(vm, frame, chunk);
2836                 }
2837 
2838                 switch (insn) {
2839                 case I_LOAD:
2840                 case I_LOAD8:
2841                 case I_LOAD16:
2842                 case I_LOAD32:
2843                         uc_vm_insn_load(vm, insn);
2844                         break;
2845 
2846                 case I_LREXP:
2847                         uc_vm_insn_load_regexp(vm, insn);
2848                         break;
2849 
2850                 case I_LNULL:
2851                         uc_vm_insn_load_null(vm, insn);
2852                         break;
2853 
2854                 case I_LTRUE:
2855                 case I_LFALSE:
2856                         uc_vm_insn_load_bool(vm, insn);
2857                         break;
2858 
2859                 case I_LTHIS:
2860                         uc_vm_stack_push(vm, ucv_get(frame->ctx));
2861                         break;
2862 
2863                 case I_LVAR:
2864                         uc_vm_insn_load_var(vm, insn);
2865                         break;
2866 
2867                 case I_LVAL:
2868                         uc_vm_insn_load_val(vm, insn);
2869                         break;
2870 
2871                 case I_PVAL:
2872                         uc_vm_insn_peek_val(vm, insn);
2873                         break;
2874 
2875                 case I_LUPV:
2876                         uc_vm_insn_load_upval(vm, insn);
2877                         break;
2878 
2879                 case I_LLOC:
2880                         uc_vm_insn_load_local(vm, insn);
2881                         break;
2882 
2883                 case I_CLFN:
2884                 case I_ARFN:
2885                         uc_vm_insn_load_closure(vm, insn);
2886                         break;
2887 
2888                 case I_NARR:
2889                         uc_vm_insn_narr(vm, insn);
2890                         break;
2891 
2892                 case I_PARR:
2893                         uc_vm_insn_parr(vm, insn);
2894                         break;
2895 
2896                 case I_MARR:
2897                         uc_vm_insn_marr(vm, insn);
2898                         break;
2899 
2900                 case I_NOBJ:
2901                         uc_vm_insn_nobj(vm, insn);
2902                         break;
2903 
2904                 case I_SOBJ:
2905                         uc_vm_insn_sobj(vm, insn);
2906                         break;
2907 
2908                 case I_MOBJ:
2909                         uc_vm_insn_mobj(vm, insn);
2910                         break;
2911 
2912                 case I_SVAR:
2913                         uc_vm_insn_store_var(vm, insn);
2914                         break;
2915 
2916                 case I_SVAL:
2917                         uc_vm_insn_store_val(vm, insn);
2918                         break;
2919 
2920                 case I_SUPV:
2921                         uc_vm_insn_store_upval(vm, insn);
2922                         break;
2923 
2924                 case I_SLOC:
2925                         uc_vm_insn_store_local(vm, insn);
2926                         break;
2927 
2928                 case I_UVAR:
2929                         uc_vm_insn_update_var(vm, insn);
2930                         break;
2931 
2932                 case I_UVAL:
2933                         uc_vm_insn_update_val(vm, insn);
2934                         break;
2935 
2936                 case I_UUPV:
2937                         uc_vm_insn_update_upval(vm, insn);
2938                         break;
2939 
2940                 case I_ULOC:
2941                         uc_vm_insn_update_local(vm, insn);
2942                         break;
2943 
2944                 case I_ADD:
2945                 case I_SUB:
2946                 case I_MUL:
2947                 case I_DIV:
2948                 case I_MOD:
2949                 case I_EXP:
2950                         uc_vm_insn_arith(vm, insn);
2951                         break;
2952 
2953                 case I_PLUS:
2954                 case I_MINUS:
2955                         uc_vm_insn_plus_minus(vm, insn);
2956                         break;
2957 
2958                 case I_LSHIFT:
2959                 case I_RSHIFT:
2960                 case I_BAND:
2961                 case I_BXOR:
2962                 case I_BOR:
2963                         uc_vm_insn_bitop(vm, insn);
2964                         break;
2965 
2966                 case I_COMPL:
2967                         uc_vm_insn_complement(vm, insn);
2968                         break;
2969 
2970                 case I_EQS:
2971                 case I_NES:
2972                         uc_vm_insn_equality(vm, insn);
2973                         break;
2974 
2975                 case I_EQ:
2976                 case I_NE:
2977                 case I_LT:
2978                 case I_LE:
2979                 case I_GT:
2980                 case I_GE:
2981                         uc_vm_insn_rel(vm, insn);
2982                         break;
2983 
2984                 case I_IN:
2985                         uc_vm_insn_in(vm, insn);
2986                         break;
2987 
2988                 case I_NOT:
2989                         uc_vm_insn_not(vm, insn);
2990                         break;
2991 
2992                 case I_JMP:
2993                         uc_vm_insn_jmp(vm, insn);
2994                         break;
2995 
2996                 case I_JMPZ:
2997                         uc_vm_insn_jmpz(vm, insn);
2998                         break;
2999 
3000                 case I_JMPNT:
3001                         uc_vm_insn_jmpnt(vm, insn);
3002                         break;
3003 
3004                 case I_NEXTK:
3005                 case I_NEXTKV:
3006                         uc_vm_insn_next(vm, insn);
3007                         break;
3008 
3009                 case I_COPY:
3010                         uc_vm_stack_push(vm, ucv_get(uc_vm_stack_peek(vm, vm->arg.u8)));
3011                         break;
3012 
3013                 case I_POP:
3014                         ucv_put(uc_vm_stack_pop(vm));
3015                         uc_vm_gc_step(vm);
3016                         break;
3017 
3018                 case I_CUPV:
3019                         uc_vm_insn_close_upval(vm, insn);
3020                         break;
3021 
3022                 case I_CALL:
3023                         uc_vm_insn_call(vm, insn);
3024                         break;
3025 
3026                 case I_RETURN:
3027                         retval = uc_vm_callframe_pop(vm);
3028 
3029                         uc_vm_stack_push(vm, retval);
3030 
3031                         if (vm->callframes.count == 0)
3032                                 return STATUS_OK;
3033                         break;
3034 
3035                 case I_PRINT:
3036                         uc_vm_insn_print(vm, insn);
3037                         break;
3038 
3039                 case I_DELETE:
3040                         uc_vm_insn_delete(vm, insn);
3041                         break;
3042 
3043                 case I_IMPORT:
3044                         uc_vm_insn_import(vm, insn);
3045                         break;
3046 
3047                 case I_EXPORT:
3048                         uc_vm_insn_export(vm, insn);
3049                         break;
3050 
3051                 case I_DYNLOAD:
3052                         uc_vm_insn_dynload(vm, insn);
3053                         break;
3054 
3055                 default:
3056                         uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "unknown opcode %d", insn);
3057                         break;
3058                 }
3059 
3060 exception:
3061                 /* previous instruction raised exception */
3062                 if (vm->exception.type != EXCEPTION_NONE) {
3063                         /* VM termination was requested */
3064                         if (vm->exception.type == EXCEPTION_EXIT) {
3065                                 uc_vm_reset_callframes(vm);
3066 
3067                                 return STATUS_EXIT;
3068                         }
3069 
3070                         /* walk up callframes until something handles the exception or the original caller is reached */
3071                         while (!uc_vm_handle_exception(vm)) {
3072                                 /* no further callframe, report unhandled exception and terminate */
3073                                 if (vm->callframes.count == 0)
3074                                         return ERROR_RUNTIME;
3075 
3076                                 /* if VM returned into native function, don't bubble up */
3077                                 if (!vm->callframes.entries[vm->callframes.count - 1].closure)
3078                                         return ERROR_RUNTIME;
3079 
3080                                 /* no exception handler in current function, pop callframe */
3081                                 ucv_put(uc_vm_callframe_pop(vm));
3082 
3083                                 /* do not bubble past original call depth */
3084                                 if (vm->callframes.count <= caller)
3085                                         return ERROR_RUNTIME;
3086                         }
3087                 }
3088 
3089                 /* run handler for signal(s) delivered during previous instruction */
3090                 if (uc_vm_signal_dispatch(vm) != EXCEPTION_NONE)
3091                         goto exception;
3092         }
3093 
3094         return STATUS_OK;
3095 }
3096 
3097 uc_vm_status_t
3098 uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval)
3099 {
3100         uc_function_t *fn = uc_program_entry(program);
3101         uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, fn, false);
3102         uc_vm_status_t status;
3103         uc_callframe_t *frame;
3104         uc_stringbuf_t *buf;
3105         uc_value_t *val;
3106 
3107         frame = uc_vector_push(&vm->callframes, {
3108                 .closure = closure,
3109                 .stackframe = 0,
3110                 .ip = closure->function->chunk.entries,
3111                 .strict = fn->strict
3112         });
3113 
3114         if (vm->trace) {
3115                 buf = xprintbuf_new();
3116 
3117                 uc_source_context_format(buf, uc_vm_frame_source(frame), 0, true);
3118 
3119                 fwrite(buf->buf, 1, printbuf_length(buf), stderr);
3120                 printbuf_free(buf);
3121 
3122                 uc_vm_frame_dump(vm, frame);
3123         }
3124 
3125         //uc_vm_stack_push(vm, closure->header.jso);
3126         uc_vm_stack_push(vm, NULL);
3127 
3128         status = uc_vm_execute_chunk(vm);
3129 
3130         switch (status) {
3131         case STATUS_OK:
3132                 val = uc_vm_stack_pop(vm);
3133 
3134                 if (retval)
3135                         *retval = val;
3136                 else
3137                         ucv_put(val);
3138 
3139                 break;
3140 
3141         case STATUS_EXIT:
3142                 if (retval)
3143                         *retval = ucv_int64_new(vm->arg.s32);
3144 
3145                 break;
3146 
3147         default:
3148                 if (vm->exhandler)
3149                         vm->exhandler(vm, &vm->exception);
3150 
3151                 if (retval)
3152                         *retval = NULL;
3153 
3154                 break;
3155         }
3156 
3157         return status;
3158 }
3159 
3160 uc_exception_type_t
3161 uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs)
3162 {
3163         uc_value_t *ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL;
3164         uc_value_t *fno = ucv_get(uc_vm_stack_peek(vm, nargs));
3165 
3166         uc_vm_clear_exception(vm);
3167 
3168         if (uc_vm_call_function(vm, ctx, fno, mcall, nargs & 0xffff)) {
3169                 if (ucv_type(fno) != UC_CFUNCTION)
3170                         uc_vm_execute_chunk(vm);
3171         }
3172 
3173         return vm->exception.type;
3174 }
3175 
3176 uc_value_t *
3177 uc_vm_scope_get(uc_vm_t *vm)
3178 {
3179         return vm->globals;
3180 }
3181 
3182 void
3183 uc_vm_scope_set(uc_vm_t *vm, uc_value_t *ctx)
3184 {
3185         ucv_put(vm->globals);
3186         vm->globals = ctx;
3187 }
3188 
3189 uc_value_t *
3190 uc_vm_invoke(uc_vm_t *vm, const char *fname, size_t nargs, ...)
3191 {
3192         uc_exception_type_t ex;
3193         uc_value_t *fno, *arg;
3194         va_list ap;
3195         size_t i;
3196 
3197         fno = ucv_property_get(vm->globals, fname);
3198 
3199         if (!ucv_is_callable(fno))
3200                 return NULL;
3201 
3202         uc_vm_stack_push(vm, ucv_get(fno));
3203 
3204         va_start(ap, nargs);
3205 
3206         for (i = 0; i < nargs; i++) {
3207                 arg = va_arg(ap, uc_value_t *);
3208                 uc_vm_stack_push(vm, ucv_get(arg));
3209         }
3210 
3211         va_end(ap);
3212 
3213         ex = uc_vm_call(vm, false, nargs);
3214 
3215         if (ex) {
3216                 if (vm->exhandler)
3217                         vm->exhandler(vm, &vm->exception);
3218 
3219                 return NULL;
3220         }
3221 
3222         return uc_vm_stack_pop(vm);
3223 }
3224 
3225 uc_exception_handler_t *
3226 uc_vm_exception_handler_get(uc_vm_t *vm)
3227 {
3228         return vm->exhandler;
3229 }
3230 
3231 void
3232 uc_vm_exception_handler_set(uc_vm_t *vm, uc_exception_handler_t *exhandler)
3233 {
3234         vm->exhandler = exhandler;
3235 }
3236 
3237 uint32_t
3238 uc_vm_trace_get(uc_vm_t *vm)
3239 {
3240         return vm->trace;
3241 }
3242 
3243 void
3244 uc_vm_trace_set(uc_vm_t *vm, uint32_t level)
3245 {
3246         vm->trace = level;
3247 }
3248 
3249 bool
3250 uc_vm_registry_exists(uc_vm_t *vm, const char *key)
3251 {
3252         bool exists;
3253 
3254         ucv_object_get(vm->registry, key, &exists);
3255 
3256         return exists;
3257 }
3258 
3259 uc_value_t *
3260 uc_vm_registry_get(uc_vm_t *vm, const char *key)
3261 {
3262         return ucv_object_get(vm->registry, key, NULL);
3263 }
3264 
3265 void
3266 uc_vm_registry_set(uc_vm_t *vm, const char *key, uc_value_t *value)
3267 {
3268         if (!vm->registry)
3269                 vm->registry = ucv_object_new(vm);
3270 
3271         ucv_object_add(vm->registry, key, value);
3272 }
3273 
3274 bool
3275 uc_vm_registry_delete(uc_vm_t *vm, const char *key)
3276 {
3277         return ucv_object_delete(vm->registry, key);
3278 }
3279 
3280 bool
3281 uc_vm_gc_start(uc_vm_t *vm, uint16_t interval)
3282 {
3283         bool changed = false;
3284 
3285         if (vm->gc_interval != interval) {
3286                 vm->gc_interval = interval;
3287                 changed = true;
3288         }
3289 
3290         if (!(vm->gc_flags & GC_ENABLED)) {
3291                 vm->gc_flags |= GC_ENABLED;
3292                 changed = true;
3293         }
3294 
3295         return changed;
3296 }
3297 
3298 bool
3299 uc_vm_gc_stop(uc_vm_t *vm)
3300 {
3301         if (!(vm->gc_flags & GC_ENABLED))
3302                 return false;
3303 
3304         vm->gc_flags &= ~GC_ENABLED;
3305 
3306         return true;
3307 }
3308 
3309 void
3310 uc_vm_signal_raise(uc_vm_t *vm, int signo)
3311 {
3312         uint8_t signum = signo;
3313 
3314         if (signo <= 0 || signo >= UC_SYSTEM_SIGNAL_COUNT)
3315                 return;
3316 
3317         vm->signal.raised[signo / 64] |= (1ull << (signo % 64));
3318 
3319         if (write(vm->signal.sigpipe[1], &signum, sizeof(signum)) == -1) {}
3320 }
3321 
3322 int
3323 uc_vm_signal_notifyfd(uc_vm_t *vm)
3324 {
3325         return vm->signal.sigpipe[0];
3326 }
3327 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt