From fcd2b0732260c165d098f72ad2267a87f8ae9ed8 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Thu, 29 Oct 2026 07:57:18 +0100 Subject: [PATCH 01/11] vm: release only the call frames of the current invocation on exit On exit(), uc_vm_execute_chunk() released the call frames of all active invocations, also those of the C functions that called back into ucode code and still run. A C function that pushed values before it called uc_vm_call(), such as include() or call() with a scope, then popped from an empty stack and wrote below it, which crashed the VM later. Release the call frames only down to the depth of the invocation, as an uncaught error already does. Each caller then sees the exit exception and unwinds its own frames. uc_require_ucode() no longer needs to skip its stack cleanup after an exit. Signed-off-by: John Crispin --- lib.c | 20 +++---- .../99_bugs/64_exit_from_scoped_include | 52 +++++++++++++++++++ vm.c | 10 ++-- 3 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 tests/custom/99_bugs/64_exit_from_scoped_include diff --git a/lib.c b/lib.c index 508c4cd1..6a802227 100644 --- a/lib.c +++ b/lib.c @@ -3044,19 +3044,15 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** *res = uc_callfunc(vm, 3); - if (vm->exception.type != EXCEPTION_EXIT) { - if (module_mode) { - ucv_put(*res); - *res = uc_require_imports(vm, closure); - } - - if (vm->stack.count >= 3) { - uc_vm_stack_pop(vm); - uc_vm_stack_pop(vm); - uc_vm_stack_pop(vm); - ucv_put(closure); - } + if (module_mode && vm->exception.type != EXCEPTION_EXIT) { + ucv_put(*res); + *res = uc_require_imports(vm, closure); } + + uc_vm_stack_pop(vm); + uc_vm_stack_pop(vm); + uc_vm_stack_pop(vm); + ucv_put(closure); } vm->config = prev_config; diff --git a/tests/custom/99_bugs/64_exit_from_scoped_include b/tests/custom/99_bugs/64_exit_from_scoped_include new file mode 100644 index 00000000..fd1946d8 --- /dev/null +++ b/tests/custom/99_bugs/64_exit_from_scoped_include @@ -0,0 +1,52 @@ +When exit() was called within code that a C function such as include() +or call() invoked with a scope, the VM released the call frames of all +active invocations, so the C function popped a value from an empty stack +afterwards and corrupted the memory below it. + +-- Testcase -- +{% + try { + include("files/part.uc", { value: 7 }); + } + catch (e) { + print("caught\n"); + } +%} +-- End -- + +-- File part.uc -- +{% + let o = {}; + o.self = o; + gc(); + print(value, "\n"); + exit(value); +%} +-- End -- + +-- Expect stdout -- +7 +-- End -- + +-- Expect exitcode -- +7 +-- End -- + + +Calling exit() within a function that call() runs with a scope ends the +program with the given code. + +-- Testcase -- +{% + call(function() { print(a, "\n"); exit(3); }, null, { a: 1 }); + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +1 +-- End -- + +-- Expect exitcode -- +3 +-- End -- diff --git a/vm.c b/vm.c index f70bf7ff..e9e2dea1 100644 --- a/vm.c +++ b/vm.c @@ -128,9 +128,9 @@ static void uc_vm_close_upvals(uc_vm_t *vm, size_t slot); static void -uc_vm_reset_callframes(uc_vm_t *vm) +uc_vm_reset_callframes(uc_vm_t *vm, size_t depth) { - while (vm->callframes.count > 0) + while (vm->callframes.count > depth) ucv_put(uc_vm_callframe_pop(vm)); } @@ -318,7 +318,7 @@ void uc_vm_free(uc_vm_t *vm) for (i = 0; i < vm->restypes.count; i++) ucv_put(vm->restypes.entries[i]->proto); - uc_vm_reset_callframes(vm); + uc_vm_reset_callframes(vm, 0); uc_vm_reset_stack(vm); uc_vector_clear(&vm->stack); uc_vector_clear(&vm->callframes); @@ -3718,7 +3718,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) if (vm->exception.type != EXCEPTION_NONE) { /* VM termination was requested */ if (vm->exception.type == EXCEPTION_EXIT) { - uc_vm_reset_callframes(vm); + uc_vm_reset_callframes(vm, caller); return STATUS_EXIT; } @@ -3740,7 +3740,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) /* "quit" was issued from within the breakpoint's * CLI session */ if (vm->exception.type == EXCEPTION_EXIT) { - uc_vm_reset_callframes(vm); + uc_vm_reset_callframes(vm, caller); return STATUS_EXIT; } From 9c0399e8862dd08a3cfe781dd93a802d3c795c40 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 12:30:07 +0100 Subject: [PATCH 02/11] vm: run no interpreter after a C function that __call__ resolves to uc_vm_call() decided by the type of the called value whether the interpreter runs after the call. For an object whose __call__ metamethod is a C function, the C function already ran in uc_vm_call_function(), and the interpreter then continued the bytecode frame on top of the stack, which is the frame of the caller, and corrupted the memory. A signal handler of this kind made the interpreter abort. The interpreter now runs only if the call left a new frame behind, which only a closure does. Signed-off-by: John Crispin --- .../custom/99_bugs/66_call_metamethod_cfunction | 17 +++++++++++++++++ vm.c | 7 +++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/custom/99_bugs/66_call_metamethod_cfunction diff --git a/tests/custom/99_bugs/66_call_metamethod_cfunction b/tests/custom/99_bugs/66_call_metamethod_cfunction new file mode 100644 index 00000000..5e2dbf0d --- /dev/null +++ b/tests/custom/99_bugs/66_call_metamethod_cfunction @@ -0,0 +1,17 @@ +When C code such as the signal dispatch called an object whose __call__ +metamethod is a C function, the VM ran the interpreter after the call as +if a closure had been called, which continued the bytecode frame on top +of the stack from the wrong position and corrupted the memory. + +-- Testcase -- +{% + signal("SIGUSR1", proto({}, { __call__: print })); + system("kill -USR1 $PPID"); + print("\nafter\n"); +%} +-- End -- + +-- Expect stdout -- +10 +after +-- End -- diff --git a/vm.c b/vm.c index e9e2dea1..a080e2fc 100644 --- a/vm.c +++ b/vm.c @@ -3870,11 +3870,14 @@ uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) { uc_value_t *ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL; uc_value_t *fno = ucv_get(uc_vm_stack_peek(vm, nargs)); - bool native = (ucv_type(fno) == UC_CFUNCTION); + size_t depth = vm->callframes.count; uc_vm_clear_exception(vm); - if (uc_vm_call_function(vm, ctx, fno, mcall, nargs & 0xffff, false) && !native) + /* only a closure leaves a new frame behind, a C function that __call__ + * resolves to already ran */ + if (uc_vm_call_function(vm, ctx, fno, mcall, nargs & 0xffff, false) && + vm->callframes.count > depth) uc_vm_execute_chunk(vm); return vm->exception.type; From cfa922adf9ba422f703bd44a2674cae4c83717f8 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 12:34:39 +0100 Subject: [PATCH 03/11] vm: keep the arguments of a spread call on the stack uc_vm_call_function() moved the arguments of a call with spread arguments into a temporary array that the garbage collector did not see. A tostring method that called gc() while the VM formatted the error for a spread value that is not an array freed the array. The error path also leaked a reference to the spread value. The arguments now stay on the stack while the VM pushes their expansion above them, and the expanded arguments then replace them. Signed-off-by: John Crispin --- .../99_bugs/67_spread_argument_lifetime | 29 ++++++ vm.c | 99 ++++++++++++------- 2 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 tests/custom/99_bugs/67_spread_argument_lifetime diff --git a/tests/custom/99_bugs/67_spread_argument_lifetime b/tests/custom/99_bugs/67_spread_argument_lifetime new file mode 100644 index 00000000..9e25a9a8 --- /dev/null +++ b/tests/custom/99_bugs/67_spread_argument_lifetime @@ -0,0 +1,29 @@ +When a call spread its arguments, the VM moved them into a temporary array +that the garbage collector did not see. A tostring method that ran gc() +while the error for a spread value that is not an array was formatted freed +the array, and the error path leaked a reference to that spread value. + +-- Testcase -- +{% + let o = proto({}, { tostring: function() { gc(); return "custom"; } }); + + try { + print(...[ {} ], ...o); + } + catch (e) { + print(e.message, "\n"); + } + + try { + print(.../x/); + } + catch (e) { + print(e.message, "\n"); + } +%} +-- End -- + +-- Expect stdout -- +(custom) is not iterable +(/x/) is not iterable +-- End -- diff --git a/vm.c b/vm.c index a080e2fc..ff2f4df0 100644 --- a/vm.c +++ b/vm.c @@ -811,18 +811,76 @@ uc_vm_callee_expr(uc_vm_t *vm) uc_program_function_srcpos(function, insn)); } +/* The `nargs` arguments of a call stay at `base` of the stack while their + * expansion is pushed above them, so that the GC sees them when a tostring + * method runs. `from` and `spread` count arguments from the last one, as the + * bytecode encodes the spread positions. */ +static uc_value_t * +uc_vm_spread_arg(uc_vm_t *vm, size_t base, size_t nargs, size_t idx) +{ + return vm->stack.entries[base + nargs - 1 - idx]; +} + +static bool +uc_vm_spread_push(uc_vm_t *vm, size_t base, size_t nargs, size_t from, size_t spread) +{ + uc_value_t *arg; + size_t j; + char *s; + + /* push each preceeding non-spread value to the stack */ + for (j = from; j > spread + 1UL; j--) + uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, j - 1))); + + /* read spread value at index... */ + arg = uc_vm_spread_arg(vm, base, nargs, spread); + + /* ... ensure that it is an array type ... */ + if (ucv_type(arg) != UC_ARRAY) { + s = ucv_to_string(vm, arg); + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); + free(s); + + return false; + } + + /* ... and push each spread array value as argument to the stack */ + for (j = 0; j < ucv_array_length(arg); j++) + uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j))); + + return true; +} + +/* Pushes the arguments after the last spread one, then replaces the original + * arguments with the expanded ones */ +static void +uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from) +{ + size_t i, count; + + for (i = from; i > 0; i--) + uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, i - 1))); + + for (i = 0; i < nargs; i++) + ucv_put(vm->stack.entries[base + i]); + + count = vm->stack.count - base - nargs; + memmove(&vm->stack.entries[base], &vm->stack.entries[base + nargs], + count * sizeof(vm->stack.entries[0])); + vm->stack.count = base + count; +} + static bool uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, size_t argspec, bool tail) { - size_t i, j, stackoff, nargs = argspec & 0xffff; + size_t i, stackoff, nargs = argspec & 0xffff; size_t nspreads = (argspec >> 16) & 0x7fff; uc_callframe_t *frame = NULL; - uc_value_t *ellip, *arg; uc_function_t *function; uc_closure_t *closure; + uc_value_t *ellip; uint16_t slot, tmp; - char *s; /* a tail invocation reuses the current frame, so it never grows the stack */ if (!tail && vm->callframes.count >= 1000) { @@ -839,52 +897,23 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, if (nspreads > 0) { frame = uc_vm_current_frame(vm); - /* create temporary array */ - ellip = ucv_array_new_length(vm, nargs); - - /* pop original stack values and push to temp array in reverse order */ - for (i = 0; i < nargs; i++) - ucv_array_push(ellip, uc_vm_stack_pop(vm)); - /* for each spread value index ... */ for (i = 0, slot = nargs; i < nspreads; i++) { /* decode stack depth value */ tmp = frame->ip[0] * 0x100 + frame->ip[1]; frame->ip += 2; - /* push each preceeding non-spread value to the stack */ - for (j = slot; j > tmp + 1UL; j--) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(ellip, j - 1))); - - /* read spread value at index... */ - slot = tmp; - arg = ucv_get(ucv_array_get(ellip, slot)); - - /* ... ensure that it is an array type ... */ - if (ucv_type(arg) != UC_ARRAY) { - s = ucv_to_string(vm, arg); - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); - free(s); + if (!uc_vm_spread_push(vm, stackoff + 1, nargs, slot, tmp)) { ucv_put(ctx); ucv_put(fno); - ucv_put(ellip); return false; } - /* ... and push each spread array value as argument to the stack */ - for (j = 0; j < ucv_array_length(arg); j++) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j))); - - ucv_put(arg); + slot = tmp; } - /* push remaining non-spread arguments to the stack */ - for (i = slot; i > 0; i--) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(ellip, i - 1))); - - /* free temp array */ - ucv_put(ellip); + uc_vm_spread_finish(vm, stackoff + 1, nargs, slot); /* update arg count */ nargs = vm->stack.count - stackoff - 1; From 6c895e3eb866495316591f62e9c28a6a58786d2a Mon Sep 17 00:00:00 2001 From: John Crispin Date: Wed, 28 Oct 2026 20:25:52 +0100 Subject: [PATCH 04/11] cli: share the -D, -F, -L and -l option handling Move the parsing of the global definition, search path and library preload options from main.c into libucode, so that other programs than the ucode CLI can accept them with the same behaviour. No behaviour change. Signed-off-by: John Crispin --- CMakeLists.txt | 2 +- cli.c | 233 +++++++++++++++++++++++++++++++++++ include/ucode/internal/cli.h | 40 ++++++ main.c | 222 +-------------------------------- 4 files changed, 280 insertions(+), 217 deletions(-) create mode 100644 cli.c create mode 100644 include/ucode/internal/cli.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bcfacb71..0f5c2902 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -243,7 +243,7 @@ else() include_directories(${JSONC_INCLUDE_DIRS}) endif() -set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c platform.c) +set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c platform.c cli.c) if(UCODE_WASM) add_library(libucode STATIC ${UCODE_SOURCES}) set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode) diff --git a/cli.c b/cli.c new file mode 100644 index 00000000..36380dec --- /dev/null +++ b/cli.c @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2020-2021 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* Command line options shared by the ucode CLI and native programs. */ + +#include +#include +#include +#include + +#include "ucode/lib.h" +#include "ucode/vm.h" +#include "ucode/internal/cli.h" +#include "ucode/internal/json-c-compat.h" + +bool +uc_cli_define_file(char *opt, uc_value_t *globals, FILE **stdin_unused) +{ + enum json_tokener_error err = json_tokener_continue; + char buf[128], *name = NULL, *p; + struct json_tokener *tok; + json_object *jso = NULL; + size_t rlen; + FILE *fp; + + p = strchr(opt, '='); + + if (p) { + name = opt; + *p++ = 0; + } + else { + p = opt; + } + + if (!strcmp(p, "-")) { + if (!*stdin_unused) { + fprintf(stderr, "The stdin can only be read once\n"); + + return false; + } + + fp = *stdin_unused; + *stdin_unused = NULL; + } + else + fp = fopen(p, "r"); + + if (!fp) { + fprintf(stderr, "Unable to open definition file \"%s\": %s\n", + p, strerror(errno)); + + return true; + } + + tok = xjs_new_tokener(); + + while (true) { + rlen = fread(buf, 1, sizeof(buf), fp); + + if (rlen == 0) + break; + + jso = json_tokener_parse_ex(tok, buf, rlen); + err = json_tokener_get_error(tok); + + if (err != json_tokener_continue) + break; + } + + json_tokener_free(tok); + fclose(fp); + + if (err != json_tokener_success || !json_object_is_type(jso, json_type_object)) { + json_object_put(jso); + + fprintf(stderr, "Invalid definition file \"%s\": %s\n", + p, (err != json_tokener_success) + ? "JSON parse failure" : "Not a valid JSON object"); + + return false; + } + + if (name && *name) { + ucv_object_add(globals, name, ucv_from_json(NULL, jso)); + } + else { + json_object_object_foreach(jso, key, val) + ucv_object_add(globals, key, ucv_from_json(NULL, val)); + } + + json_object_put(jso); + + return true; +} + +bool +uc_cli_define_string(char *opt, uc_value_t *globals) +{ + enum json_tokener_error err; + struct json_tokener *tok; + json_object *jso = NULL; + char *name = NULL, *p; + bool rv = false; + size_t len; + + p = strchr(opt, '='); + + if (p) { + name = opt; + *p++ = 0; + } + else { + p = opt; + } + + len = strlen(p); + tok = xjs_new_tokener(); + + /* NB: the len + 1 here is intentional to pass the terminating \0 byte + * to the json-c parser. This is required to work-around upstream + * issue #681 */ + jso = json_tokener_parse_ex(tok, p, len + 1); + + err = json_tokener_get_error(tok); + + /* Treat trailing bytes after a parsed value as error */ + if (err == json_tokener_success && json_tokener_get_parse_end(tok) < len) + err = json_tokener_error_parse_unexpected; + + json_tokener_free(tok); + + if (err != json_tokener_success) { + json_object_put(jso); + + if (!name || !*name) { + fprintf(stderr, "Invalid -D option value \"%s\": %s\n", + p, json_tokener_error_desc(err)); + + return false; + } + + ucv_object_add(globals, name, ucv_string_new(p)); + + return true; + } + + if (name && *name) { + ucv_object_add(globals, name, ucv_from_json(NULL, jso)); + rv = true; + } + else if (json_object_is_type(jso, json_type_object)) { + json_object_object_foreach(jso, key, val) + ucv_object_add(globals, key, ucv_from_json(NULL, val)); + rv = true; + } + else { + fprintf(stderr, "Invalid -D option value \"%s\": Not a valid JSON object\n", p); + } + + json_object_put(jso); + + return rv; +} + +void +uc_cli_search_path(char *pattern, uc_parse_config_t *config) +{ + size_t len; + char *p; + + if (strchr(pattern, '*')) { + uc_search_path_add(&config->module_search_path, pattern); + return; + } + + len = strlen(pattern); + + if (!len) + return; + + while (pattern[len-1] == '/') + pattern[--len] = 0; + + xasprintf(&p, "%s/*.so", pattern); + uc_search_path_add(&config->module_search_path, p); + free(p); + + xasprintf(&p, "%s/*.uc", pattern); + uc_search_path_add(&config->module_search_path, p); + free(p); +} + +bool +uc_cli_library_load(char *opt, uc_vm_t *vm) +{ + char *name = NULL, *p; + uc_value_t *lib, *ctx; + + p = strchr(opt, '='); + + if (p) { + name = opt; + *p++ = 0; + } + else { + p = opt; + } + + lib = ucv_string_new(p); + ctx = uc_vm_invoke(vm, "require", 1, lib); + ucv_put(lib); + + if (!ctx) + return vm->exception.type == EXCEPTION_NONE; + + ucv_object_add(uc_vm_scope_get(vm), name ? name : p, ctx); + + return true; +} diff --git a/include/ucode/internal/cli.h b/include/ucode/internal/cli.h new file mode 100644 index 00000000..07eb2298 --- /dev/null +++ b/include/ucode/internal/cli.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2020-2021 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* Command line options shared by the ucode CLI and native programs. */ + +#ifndef UCODE_INTERNAL_CLI_H +#define UCODE_INTERNAL_CLI_H + +#include +#include + +#include "ucode/types.h" +#include "ucode/compiler.h" + +/* -D [name=]value */ +bool uc_cli_define_string(char *opt, uc_value_t *globals); + +/* -F [name=]path; `stdin_unused` is cleared once stdin has been consumed */ +bool uc_cli_define_file(char *opt, uc_value_t *globals, FILE **stdin_unused); + +/* -L pattern */ +void uc_cli_search_path(char *pattern, uc_parse_config_t *config); + +/* -l [name=]library */ +bool uc_cli_library_load(char *opt, uc_vm_t *vm); + +#endif /* UCODE_INTERNAL_CLI_H */ diff --git a/main.c b/main.c index 18f64156..baeb8b5f 100644 --- a/main.c +++ b/main.c @@ -30,7 +30,7 @@ #include "ucode/source.h" #include "ucode/program.h" -#include "ucode/internal/json-c-compat.h" +#include "ucode/internal/cli.h" #ifdef __GLIBC__ # define POSIXLY_CORRECT_FLAG "+" @@ -128,9 +128,6 @@ print_usage(const char *app) app); } -static bool -parse_library_load(char *opt, uc_vm_t *vm); - static uc_value_t * debug_lookup_fn(uc_vm_t *vm, const char *name) { @@ -159,7 +156,6 @@ typedef struct { debug_mode_t debug; char *breakpoint; } compile_opts_t; - static int compile(uc_vm_t *vm, uc_source_t *src, FILE *precompile, const compile_opts_t *opts) { @@ -195,7 +191,7 @@ compile(uc_vm_t *vm, uc_source_t *src, FILE *precompile, const compile_opts_t *o if (debug != DEBUG_MODE_NONE) { uc_value_t *entryfn; - if (!parse_library_load("debug", vm)) { + if (!uc_cli_library_load("debug", vm)) { fprintf(stderr, "Unable to load debug module\n"); rc = -2; goto out; @@ -456,212 +452,6 @@ parse_compile_flags(char *opt, char **interp, uc_search_path_t *dynlink_list, } } -static bool -parse_define_file(char *opt, uc_value_t *globals) -{ - enum json_tokener_error err = json_tokener_continue; - char buf[128], *name = NULL, *p; - struct json_tokener *tok; - json_object *jso = NULL; - size_t rlen; - FILE *fp; - - p = strchr(opt, '='); - - if (p) { - name = opt; - *p++ = 0; - } - else { - p = opt; - } - - if (!strcmp(p, "-")) { - if (!stdin_unused) { - fprintf(stderr, "The stdin can only be read once\n"); - - return false; - } - - fp = stdin_unused; - stdin_unused = NULL; - } - else - fp = fopen(p, "r"); - - if (!fp) { - fprintf(stderr, "Unable to open definition file \"%s\": %s\n", - p, strerror(errno)); - - return true; - } - - tok = xjs_new_tokener(); - - while (true) { - rlen = fread(buf, 1, sizeof(buf), fp); - - if (rlen == 0) - break; - - jso = json_tokener_parse_ex(tok, buf, rlen); - err = json_tokener_get_error(tok); - - if (err != json_tokener_continue) - break; - } - - json_tokener_free(tok); - fclose(fp); - - if (err != json_tokener_success || !json_object_is_type(jso, json_type_object)) { - json_object_put(jso); - - fprintf(stderr, "Invalid definition file \"%s\": %s\n", - p, (err != json_tokener_success) - ? "JSON parse failure" : "Not a valid JSON object"); - - return false; - } - - if (name && *name) { - ucv_object_add(globals, name, ucv_from_json(NULL, jso)); - } - else { - json_object_object_foreach(jso, key, val) - ucv_object_add(globals, key, ucv_from_json(NULL, val)); - } - - json_object_put(jso); - - return true; -} - -static bool -parse_define_string(char *opt, uc_value_t *globals) -{ - enum json_tokener_error err; - struct json_tokener *tok; - json_object *jso = NULL; - char *name = NULL, *p; - bool rv = false; - size_t len; - - p = strchr(opt, '='); - - if (p) { - name = opt; - *p++ = 0; - } - else { - p = opt; - } - - len = strlen(p); - tok = xjs_new_tokener(); - - /* NB: the len + 1 here is intentional to pass the terminating \0 byte - * to the json-c parser. This is required to work-around upstream - * issue #681 */ - jso = json_tokener_parse_ex(tok, p, len + 1); - - err = json_tokener_get_error(tok); - - /* Treat trailing bytes after a parsed value as error */ - if (err == json_tokener_success && json_tokener_get_parse_end(tok) < len) - err = json_tokener_error_parse_unexpected; - - json_tokener_free(tok); - - if (err != json_tokener_success) { - json_object_put(jso); - - if (!name || !*name) { - fprintf(stderr, "Invalid -D option value \"%s\": %s\n", - p, json_tokener_error_desc(err)); - - return false; - } - - ucv_object_add(globals, name, ucv_string_new(p)); - - return true; - } - - if (name && *name) { - ucv_object_add(globals, name, ucv_from_json(NULL, jso)); - rv = true; - } - else if (json_object_is_type(jso, json_type_object)) { - json_object_object_foreach(jso, key, val) - ucv_object_add(globals, key, ucv_from_json(NULL, val)); - rv = true; - } - else { - fprintf(stderr, "Invalid -D option value \"%s\": Not a valid JSON object\n", p); - } - - json_object_put(jso); - - return rv; -} - -static void -parse_search_path(char *pattern, uc_parse_config_t *config) -{ - size_t len; - char *p; - - if (strchr(pattern, '*')) { - uc_search_path_add(&config->module_search_path, pattern); - return; - } - - len = strlen(pattern); - - if (!len) - return; - - while (pattern[len-1] == '/') - pattern[--len] = 0; - - xasprintf(&p, "%s/*.so", pattern); - uc_search_path_add(&config->module_search_path, p); - free(p); - - xasprintf(&p, "%s/*.uc", pattern); - uc_search_path_add(&config->module_search_path, p); - free(p); -} - -static bool -parse_library_load(char *opt, uc_vm_t *vm) -{ - char *name = NULL, *p; - uc_value_t *lib, *ctx; - - p = strchr(opt, '='); - - if (p) { - name = opt; - *p++ = 0; - } - else { - p = opt; - } - - lib = ucv_string_new(p); - ctx = uc_vm_invoke(vm, "require", 1, lib); - ucv_put(lib); - - if (!ctx) - return vm->exception.type == EXCEPTION_NONE; - - ucv_object_add(uc_vm_scope_get(vm), name ? name : p, ctx); - - return true; -} - static const char * appname(const char *argv0) { @@ -723,7 +513,7 @@ main(int argc, char **argv) { switch (opt) { case 'L': - parse_search_path(optarg, &config); + uc_cli_search_path(optarg, &config); break; case 'S': @@ -784,7 +574,7 @@ main(int argc, char **argv) break; case 'D': - if (!parse_define_string(optarg, uc_vm_scope_get(&vm))) { + if (!uc_cli_define_string(optarg, uc_vm_scope_get(&vm))) { rv = 1; goto out; } @@ -792,7 +582,7 @@ main(int argc, char **argv) break; case 'F': - if (!parse_define_file(optarg, uc_vm_scope_get(&vm))) { + if (!uc_cli_define_file(optarg, uc_vm_scope_get(&vm), &stdin_unused)) { rv = 1; goto out; } @@ -804,7 +594,7 @@ main(int argc, char **argv) break; case 'l': - if (!parse_library_load(optarg, &vm)) { + if (!uc_cli_library_load(optarg, &vm)) { rv = 1; goto out; } From ced185b2d41663cc86b7e10b4b1123f336ad46f7 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Thu, 29 Oct 2026 15:00:15 +0100 Subject: [PATCH 05/11] types, json: replace json-c with own objects, string buffers and parser Objects were json-c hash tables: 40 bytes per entry, 16 entries allocated up front, a copy of each key, calls through function pointers, and keys that ended at a NUL byte, so that "a\0b" and "a" were the same key. - Objects keep their entries in insertion order, each a key string value and a value, so that short keys live in the pointer and keys from constants are shared. Objects with more than eight keys add a hash index; the hash is SipHash-1-3 with a seed per process. Reads and writes of own keys skip the generic key path. - The string buffer is uc_stringbuf_t in ucode/util.h. - json.c parses JSON into ucode values incrementally, with the grammar, errors and stopping positions of the json-c 0.18 tokener. 90000 generated inputs, whole and in chunks, give the same results as before, except that keys keep their NUL bytes. Its API is in ucode/json.h. - uniq() and the debug module collect values in uc_set_t. - lc() and uc() convert a string argument without a string buffer. ucv_from_json() and ucv_to_json() are removed, ucv_object_foreach() iterates with ucv_object_next(), and ucode/types.h no longer includes json-c. Bindings must be rebuilt, and those that use json-c must include it themselves. 100k objects, peak RSS: empty 78.4 -> 11.0 MB, three keys 45.2 -> 17.9 MB. Parsing 8 MB of JSON: 1.41 -> 0.40 s, peak RSS 216 -> 53 MB. Signed-off-by: John Crispin --- CMakeLists.txt | 44 +- cli.c | 100 +- cmake/emscripten-deps.cmake | 52 +- cmake/emscripten.cmake | 2 +- compiler.c | 4 +- include/ucode/internal/json-c-compat.h | 50 - include/ucode/internal/types.h | 45 +- include/ucode/json.h | 59 ++ include/ucode/types.h | 47 +- include/ucode/util.h | 127 ++- json.c | 850 +++++++++++++++++ lib.c | 266 +++--- lib/debug.c | 119 +-- lib/debug_proto.c | 41 +- lib/ffi/uc_cparse.c | 6 +- lib/ffi/uc_cparse.h | 2 +- lib/ffi/uc_ctype.c | 2 +- lib/fs.c | 20 +- lib/resolv.c | 2 +- lib/socket.c | 12 +- lib/uloop.c | 62 +- lib/zlib.c | 16 +- main.c | 4 +- program.c | 6 +- tests/custom/06_metamethods/90_own_key_access | 79 ++ tests/custom/99_bugs/65_object_key_nul_bytes | 50 + types.c | 884 +++++++++++------- udbg.c | 362 +++---- vm.c | 57 +- 29 files changed, 2334 insertions(+), 1036 deletions(-) delete mode 100644 include/ucode/internal/json-c-compat.h create mode 100644 include/ucode/json.h create mode 100644 json.c create mode 100644 tests/custom/06_metamethods/90_own_key_access create mode 100644 tests/custom/99_bugs/65_object_key_nul_bytes diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f5c2902..19edaaca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,7 +122,6 @@ if(NOT UCODE_WASM) find_library(libuci NAMES uci) find_library(libubox NAMES ubox) find_library(libubus NAMES ubus) - find_library(libblobmsg_json NAMES blobmsg_json) find_package(ZLIB) find_library(libmd NAMES libmd.a md) find_library(libffi NAMES ffi) @@ -139,7 +138,7 @@ if(NOT UCODE_WASM) set(DEFAULT_UCI_SUPPORT ON) endif() - if(libubus AND libblobmsg_json) + if(libubus) set(DEFAULT_UBUS_SUPPORT ON) endif() @@ -231,19 +230,11 @@ endif() if(UCODE_WASM) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/emscripten-deps.cmake) - set(JSONC_INCLUDE_DIRS ${UCODE_DEPS_PREFIX}/json-c/dist/include) - set(JSONC_LINK_LIBRARIES libjson-c) - include_directories(${JSONC_INCLUDE_DIRS}) - # json-c is pinned by emscripten-deps.cmake; the features checked for - # below are all present in it - target_compile_definitions(uc_defines INTERFACE HAVE_PARSE_END HAVE_ARRAY_EXT HAVE_JSON_UINT64) else() include(FindPkgConfig) - pkg_check_modules(JSONC REQUIRED json-c) - include_directories(${JSONC_INCLUDE_DIRS}) endif() -set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c program.c platform.c cli.c) +set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c json.c program.c platform.c cli.c) if(UCODE_WASM) add_library(libucode STATIC ${UCODE_SOURCES}) set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode) @@ -252,7 +243,7 @@ else() set(SOVERSION 0 CACHE STRING "Override ucode library version") set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode SOVERSION ${SOVERSION}) endif() -target_link_libraries(libucode uc_defines ${JSONC_LINK_LIBRARIES}) +target_link_libraries(libucode uc_defines) if(NOT UCODE_WASM) set(CLI_SOURCES main.c) @@ -301,23 +292,6 @@ if(NOT UCODE_WASM) endif() endif() -if(NOT UCODE_WASM) - list(APPEND CMAKE_REQUIRED_INCLUDES ${JSONC_INCLUDE_DIRS}) - list(APPEND CMAKE_REQUIRED_LIBRARIES ${JSONC_LINK_LIBRARIES}) - check_symbol_exists(json_tokener_get_parse_end "json-c/json.h" HAVE_PARSE_END) - if(HAVE_PARSE_END) - target_compile_definitions(uc_defines INTERFACE HAVE_PARSE_END) - endif() - check_symbol_exists(json_object_new_array_ext "json-c/json.h" HAVE_ARRAY_EXT) - if(HAVE_ARRAY_EXT) - target_compile_definitions(uc_defines INTERFACE HAVE_ARRAY_EXT) - endif() - check_symbol_exists(json_object_new_uint64 "json-c/json.h" HAVE_JSON_UINT64) - if(HAVE_JSON_UINT64) - target_compile_definitions(uc_defines INTERFACE HAVE_JSON_UINT64) - endif() -endif() - if(NOT UCODE_WASM) set(LIBRARIES "") @@ -367,7 +341,7 @@ if(NOT UCODE_WASM) add_library(ubus_lib MODULE lib/ubus.c) set_target_properties(ubus_lib PROPERTIES OUTPUT_NAME ubus PREFIX "") target_link_options(ubus_lib PRIVATE ${UCODE_MODULE_LINK_OPTIONS}) - target_link_libraries(ubus_lib ${libubus} ${libblobmsg_json}) + target_link_libraries(ubus_lib ${libubus}) list(APPEND CMAKE_REQUIRED_LIBRARIES ${libubox} ${libubus}) file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/test.c" " #include @@ -595,9 +569,7 @@ if(UCODE_WASM) foreach(mod IN LISTS UCODE_WASM_MODULES) add_library(ucode_mod_${mod} OBJECT lib/${mod}.c) target_compile_definitions(ucode_mod_${mod} PRIVATE uc_module_init=uc_module_init_${mod}) - target_include_directories(ucode_mod_${mod} PRIVATE include ${JSONC_INCLUDE_DIRS}) - # the json-c headers only exist once the external project has built - add_dependencies(ucode_mod_${mod} json-c-build) + target_include_directories(ucode_mod_${mod} PRIVATE include) if(mod STREQUAL "zlib") target_compile_options(ucode_mod_${mod} PRIVATE -sUSE_ZLIB=1) endif() @@ -615,7 +587,7 @@ if(UCODE_WASM) add_library(ucode-modules STATIC ${UCODE_MODULE_OBJECTS}) set_target_properties(ucode-modules PROPERTIES PREFIX "") - target_link_libraries(ucode-modules PUBLIC libucode libjson-c) + target_link_libraries(ucode-modules PUBLIC libucode) if("digest" IN_LIST UCODE_WASM_MODULES) target_link_libraries(ucode-modules PUBLIC libmd) endif() @@ -634,7 +606,7 @@ if(NOT UCODE_WASM) if(CMAKE_C_COMPILER_ID STREQUAL "Clang") add_executable(ucode-san ${CLI_SOURCES} ${UCODE_SOURCES}) set_property(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1) - target_link_libraries(ucode-san uc_defines ${JSONC_LINK_LIBRARIES}) + target_link_libraries(ucode-san uc_defines) target_compile_options(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) target_link_options(ucode-san PRIVATE -fsanitize=undefined,address,leak) endif() @@ -643,7 +615,7 @@ endif() # UCODE_WASM if(NOT UCODE_WASM) add_executable(udbg udbg.c debug_highlight.c debug_lineedit.c) - target_link_libraries(udbg PRIVATE libucode ${JSONC_LINK_LIBRARIES}) + target_link_libraries(udbg PRIVATE libucode) endif() if(UCODE_WASM) diff --git a/cli.c b/cli.c index 36380dec..786717ce 100644 --- a/cli.c +++ b/cli.c @@ -24,15 +24,26 @@ #include "ucode/lib.h" #include "ucode/vm.h" #include "ucode/internal/cli.h" -#include "ucode/internal/json-c-compat.h" +#include "ucode/internal/types.h" +#include "ucode/json.h" + +/* Adds the properties of the object `obj` to `globals` and releases it */ +static void +uc_cli_define_object(uc_value_t *globals, uc_value_t *obj) +{ + ucv_object_foreach_entry(obj, e) + ucv_object_insert_string(globals, e->key, ucv_get(e->value)); + + ucv_put(obj); +} bool uc_cli_define_file(char *opt, uc_value_t *globals, FILE **stdin_unused) { - enum json_tokener_error err = json_tokener_continue; + uc_json_error_t err = UC_JSON_CONTINUE; char buf[128], *name = NULL, *p; - struct json_tokener *tok; - json_object *jso = NULL; + uc_json_parser_t *parser; + uc_value_t *val = NULL; size_t rlen; FILE *fp; @@ -66,7 +77,7 @@ uc_cli_define_file(char *opt, uc_value_t *globals, FILE **stdin_unused) return true; } - tok = xjs_new_tokener(); + parser = uc_json_parser_new(NULL); while (true) { rlen = fread(buf, 1, sizeof(buf), fp); @@ -74,35 +85,30 @@ uc_cli_define_file(char *opt, uc_value_t *globals, FILE **stdin_unused) if (rlen == 0) break; - jso = json_tokener_parse_ex(tok, buf, rlen); - err = json_tokener_get_error(tok); + val = uc_json_parse(parser, buf, rlen); + err = uc_json_parser_error(parser); - if (err != json_tokener_continue) + if (err != UC_JSON_CONTINUE) break; } - json_tokener_free(tok); + uc_json_parser_free(parser); fclose(fp); - if (err != json_tokener_success || !json_object_is_type(jso, json_type_object)) { - json_object_put(jso); + if (err != UC_JSON_SUCCESS || ucv_type(val) != UC_OBJECT) { + ucv_put(val); fprintf(stderr, "Invalid definition file \"%s\": %s\n", - p, (err != json_tokener_success) + p, (err != UC_JSON_SUCCESS) ? "JSON parse failure" : "Not a valid JSON object"); return false; } - if (name && *name) { - ucv_object_add(globals, name, ucv_from_json(NULL, jso)); - } - else { - json_object_object_foreach(jso, key, val) - ucv_object_add(globals, key, ucv_from_json(NULL, val)); - } - - json_object_put(jso); + if (name && *name) + ucv_object_add(globals, name, val); + else + uc_cli_define_object(globals, val); return true; } @@ -110,11 +116,10 @@ uc_cli_define_file(char *opt, uc_value_t *globals, FILE **stdin_unused) bool uc_cli_define_string(char *opt, uc_value_t *globals) { - enum json_tokener_error err; - struct json_tokener *tok; - json_object *jso = NULL; + uc_json_parser_t *parser; char *name = NULL, *p; - bool rv = false; + uc_value_t *val; + uc_json_error_t err; size_t len; p = strchr(opt, '='); @@ -128,27 +133,26 @@ uc_cli_define_string(char *opt, uc_value_t *globals) } len = strlen(p); - tok = xjs_new_tokener(); + parser = uc_json_parser_new(NULL); - /* NB: the len + 1 here is intentional to pass the terminating \0 byte - * to the json-c parser. This is required to work-around upstream - * issue #681 */ - jso = json_tokener_parse_ex(tok, p, len + 1); + /* NB: the len + 1 here is intentional, the terminating \0 byte ends the + * input, so that values without delimiter such as `null` complete */ + val = uc_json_parse(parser, p, len + 1); - err = json_tokener_get_error(tok); + err = uc_json_parser_error(parser); /* Treat trailing bytes after a parsed value as error */ - if (err == json_tokener_success && json_tokener_get_parse_end(tok) < len) - err = json_tokener_error_parse_unexpected; + if (err == UC_JSON_SUCCESS && uc_json_parser_end(parser) < len) + err = UC_JSON_ERROR_UNEXPECTED; - json_tokener_free(tok); + uc_json_parser_free(parser); - if (err != json_tokener_success) { - json_object_put(jso); + if (err != UC_JSON_SUCCESS) { + ucv_put(val); if (!name || !*name) { fprintf(stderr, "Invalid -D option value \"%s\": %s\n", - p, json_tokener_error_desc(err)); + p, uc_json_error_desc(err)); return false; } @@ -159,21 +163,21 @@ uc_cli_define_string(char *opt, uc_value_t *globals) } if (name && *name) { - ucv_object_add(globals, name, ucv_from_json(NULL, jso)); - rv = true; - } - else if (json_object_is_type(jso, json_type_object)) { - json_object_object_foreach(jso, key, val) - ucv_object_add(globals, key, ucv_from_json(NULL, val)); - rv = true; + ucv_object_add(globals, name, val); + + return true; } - else { - fprintf(stderr, "Invalid -D option value \"%s\": Not a valid JSON object\n", p); + + if (ucv_type(val) == UC_OBJECT) { + uc_cli_define_object(globals, val); + + return true; } - json_object_put(jso); + ucv_put(val); + fprintf(stderr, "Invalid -D option value \"%s\": Not a valid JSON object\n", p); - return rv; + return false; } void diff --git a/cmake/emscripten-deps.cmake b/cmake/emscripten-deps.cmake index 14869a6a..0af46489 100644 --- a/cmake/emscripten-deps.cmake +++ b/cmake/emscripten-deps.cmake @@ -1,18 +1,9 @@ # External dependencies for the emscripten/wasm32 build. # -# The emscripten sysroot ships neither json-c nor libmd, so both are -# fetched and built here (included from CMakeLists.txt when UCODE_WASM -# is on; toolchain files cannot host this because they are re-included -# by every try_compile() probe build). -# -# json-c uses its own CMake build with two overrides: -# -Wno-macro-redefined its math_compat.h redefines NAN/INFINITY/isnan/ -# isinf, which emscripten's clang already provides -# (a warning with plain emcc, an error under its -# -Werror) -# -DHAVE_SNPRINTF=1 its snprintf feature check is gated on -# "UNIX OR MINGW OR CYGWIN", which the Generic -# (wasm32) system does not match +# The emscripten sysroot does not ship libmd, so it is fetched and built +# here (included from CMakeLists.txt when UCODE_WASM is on; toolchain files +# cannot host this because they are re-included by every try_compile() +# probe build). # # libmd is autotools-only, so the cmake/deps/libmd wrapper CMakeLists # compiles its sources directly. @@ -21,10 +12,9 @@ include(ExternalProject) set(UCODE_DEPS_PREFIX ${CMAKE_BINARY_DIR}/deps) -# The imported targets below reference these include directories, which -# only get their contents when the external projects build; create them -# now so CMake's generate-time path validation passes. -file(MAKE_DIRECTORY ${UCODE_DEPS_PREFIX}/json-c/dist/include) +# The imported target below references this include directory, which only +# gets its contents when the external project builds; create it now so +# CMake's generate-time path validation passes. file(MAKE_DIRECTORY ${UCODE_DEPS_PREFIX}/libmd/dist/include) set(_ucode_toolchain_args "") @@ -32,34 +22,6 @@ if(CMAKE_TOOLCHAIN_FILE) list(APPEND _ucode_toolchain_args -DCMAKE_TOOLCHAIN_FILE:FILEPATH=${CMAKE_TOOLCHAIN_FILE}) endif() -# --------------------------------------------------------------------------- -# json-c -# --------------------------------------------------------------------------- -ExternalProject_Add(json-c-build - PREFIX ${UCODE_DEPS_PREFIX}/json-c - GIT_REPOSITORY https://github.com/json-c/json-c.git - GIT_TAG json-c-0.18-20240915 - GIT_SHALLOW 1 - CMAKE_ARGS - -DCMAKE_POLICY_VERSION_MINIMUM=3.5 - ${_ucode_toolchain_args} - -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} - -DCMAKE_C_FLAGS:STRING=-Wno-macro-redefined - CMAKE_CACHE_ARGS - -DCMAKE_INSTALL_PREFIX:STRING=${UCODE_DEPS_PREFIX}/json-c/dist - -DCMAKE_INSTALL_LIBDIR:STRING=lib - -DBUILD_SHARED_LIBS:STRING=OFF - -DBUILD_TESTING:STRING=OFF - -DDISABLE_EXTRA_LIBS:STRING=ON - -DHAVE_SNPRINTF:STRING=1 - UPDATE_COMMAND "" -) - -add_library(libjson-c STATIC IMPORTED GLOBAL) -set_property(TARGET libjson-c PROPERTY IMPORTED_LOCATION ${UCODE_DEPS_PREFIX}/json-c/dist/lib/libjson-c.a) -set_property(TARGET libjson-c PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${UCODE_DEPS_PREFIX}/json-c/dist/include) -add_dependencies(libjson-c json-c-build) - # --------------------------------------------------------------------------- # libmd # --------------------------------------------------------------------------- diff --git a/cmake/emscripten.cmake b/cmake/emscripten.cmake index 37887c7b..504ad188 100644 --- a/cmake/emscripten.cmake +++ b/cmake/emscripten.cmake @@ -7,7 +7,7 @@ # libucode.a and ucode-modules.a for static linking into an embedder's # executable; see the UCODE_WASM section of CMakeLists.txt. # -# Note: the dependency setup (json-c, libmd) cannot live in this file -- +# Note: the dependency setup (libmd) cannot live in this file -- # toolchain files are re-included by every try_compile() probe build -- # so CMakeLists.txt pulls in cmake/emscripten-deps.cmake when it detects # the emscripten compiler. diff --git a/compiler.c b/compiler.c index 8b89585d..3aea7be4 100644 --- a/compiler.c +++ b/compiler.c @@ -189,7 +189,7 @@ uc_compiler_syntax_error(uc_compiler_t *compiler, size_t off, const char *fmt, . compiler->parser->synchronizing = true; if (!buf) - buf = compiler->parser->error = xprintbuf_new(); + buf = compiler->parser->error = uc_stringbuf_new(); if (!off) off = uc_program_function_srcpos(compiler->function, @@ -5037,7 +5037,7 @@ uc_compile_from_source(uc_parse_config_t *config, uc_source_t *source, uc_progra free(parser.error); } else { - printbuf_free(parser.error); + uc_stringbuf_free(parser.error); } uc_lexer_free(&parser.lex); diff --git a/include/ucode/internal/json-c-compat.h b/include/ucode/internal/json-c-compat.h deleted file mode 100644 index da24a0f0..00000000 --- a/include/ucode/internal/json-c-compat.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2022 Jo-Philipp Wich - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef JSON_C_COMPAT_H -#define JSON_C_COMPAT_H - -#include -#include -#include - - -/* json-c compat */ - -#ifndef HAVE_PARSE_END -static inline size_t json_tokener_get_parse_end(struct json_tokener *tok) { - return (size_t)tok->char_offset; -} -#endif - -#ifndef HAVE_ARRAY_EXT -static inline struct json_object *json_object_new_array_ext(int size) { - (void) size; - return json_object_new_array(); -} -#endif - -#ifndef HAVE_JSON_UINT64 -static inline struct json_object *json_object_new_uint64(uint64_t i) { - return json_object_new_int64((int64_t)i); -} - -static inline uint64_t json_object_get_uint64(const struct json_object *obj) { - return (uint64_t)json_object_get_int64(obj); -} -#endif - -#endif /* JSON_C_COMPAT_H */ diff --git a/include/ucode/internal/types.h b/include/ucode/internal/types.h index 04fcd2ba..5a6c5e11 100644 --- a/include/ucode/internal/types.h +++ b/include/ucode/internal/types.h @@ -108,14 +108,8 @@ struct uc_function { typedef struct { uc_list_t list; - struct lh_table *table; - union { - struct lh_entry *pos; - struct { - const void *k; - unsigned long hash; - } kh; - } u; + uc_object_t *object; + size_t pos; } uc_object_iterator_t; @@ -180,6 +174,41 @@ __hidden void ucv_unref(uc_weakref_t *); __hidden void ucv_ref(uc_weakref_t *, uc_weakref_t *); __hidden void ucv_freeall(uc_vm_t *); +/* ucv_object_get(), ucv_object_add() and ucv_object_delete() for keys of a + * given length, which may contain NUL bytes */ +__hidden uc_value_t *ucv_object_lookup(uc_value_t *, const char *, size_t, bool *); +__hidden bool ucv_object_insert(uc_value_t *, const char *, size_t, uc_value_t *); +__hidden bool ucv_object_remove(uc_value_t *, const char *, size_t); + +/* ucv_object_get() and ucv_object_add() with the key as a string value, + * which the object shares */ +__hidden uc_value_t *ucv_object_lookup_string(uc_value_t *, uc_value_t *, bool *); +__hidden bool ucv_object_insert_string(uc_value_t *, uc_value_t *, uc_value_t *); + +__hidden void ucv_object_sort_entries(uc_value_t *, + int (*)(const uc_object_entry_t *, const uc_object_entry_t *, void *), void *); + +/* Returns the entry after `e`, or the first one if `e` is NULL, skipping + * deleted entries; NULL at the end or if `uv` is no object. */ +static inline uc_object_entry_t * +ucv_object_entry_next(uc_value_t *uv, uc_object_entry_t *e) +{ + uc_object_t *object = (uc_object_t *)uv; + + if (ucv_type(uv) != UC_OBJECT) + return NULL; + + for (e = e ? e + 1 : object->entries; e && e < object->entries + object->used; e++) + if (e->key) + return e; + + return NULL; +} + +#define ucv_object_foreach_entry(obj, e) \ + for (uc_object_entry_t *e = ucv_object_entry_next((obj), NULL); e; \ + e = ucv_object_entry_next((obj), e)) + /* Type-cast helpers */ #define ucv_as_integer(v) ((uc_integer_t *) (v)) #define ucv_as_double(v) ((uc_double_t *) (v)) diff --git a/include/ucode/json.h b/include/ucode/json.h new file mode 100644 index 00000000..27058bce --- /dev/null +++ b/include/ucode/json.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef UCODE_JSON_H +#define UCODE_JSON_H + +#include + +#include "ucode/types.h" + +typedef enum { + UC_JSON_SUCCESS, + UC_JSON_CONTINUE, + UC_JSON_ERROR_DEPTH, + UC_JSON_ERROR_EOF, + UC_JSON_ERROR_UNEXPECTED, + UC_JSON_ERROR_NULL, + UC_JSON_ERROR_BOOLEAN, + UC_JSON_ERROR_NUMBER, + UC_JSON_ERROR_ARRAY, + UC_JSON_ERROR_KEY_NAME, + UC_JSON_ERROR_KEY_SEP, + UC_JSON_ERROR_VALUE_SEP, + UC_JSON_ERROR_STRING, + UC_JSON_ERROR_COMMENT, +} uc_json_error_t; + +typedef struct uc_json_parser uc_json_parser_t; + +uc_json_parser_t *uc_json_parser_new(uc_vm_t *vm); +void uc_json_parser_free(uc_json_parser_t *parser); + +/* Parses `len` more bytes of input. A value is complete when + * uc_json_parser_error() is UC_JSON_SUCCESS afterwards; the call then + * returns it, and the parser starts over with the next one. Otherwise it + * returns NULL, and the parser waits for more input (UC_JSON_CONTINUE) or + * failed. */ +uc_value_t *uc_json_parse(uc_json_parser_t *parser, const char *buf, size_t len); +uc_json_error_t uc_json_parser_error(uc_json_parser_t *parser); + +/* the number of input bytes the last call consumed */ +size_t uc_json_parser_end(uc_json_parser_t *parser); + +const char *uc_json_error_desc(uc_json_error_t err); + +#endif /* UCODE_JSON_H */ diff --git a/include/ucode/types.h b/include/ucode/types.h index ddc8e61d..cb2d9d07 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -128,11 +127,25 @@ typedef struct { uc_value_t **entries; } uc_array_t; +/* `key` is a string value, NULL in place of a deleted entry */ +typedef struct { + uc_value_t *key; + uc_value_t *value; +} uc_object_entry_t; + +/* The entries are in insertion order; `used` counts them with the deleted + * ones, `count` without. Objects with many keys also have a hash index of + * `mask` + 1 slots that hold entry positions plus one. */ typedef struct { uc_value_t header; uc_weakref_t ref; uc_value_t *proto; - struct lh_table *table; + uc_object_entry_t *entries; + uint32_t *index; + uint32_t count; + uint32_t used; + uint32_t size; + uint32_t mask; } uc_object_t; typedef struct { @@ -239,8 +252,6 @@ uc_declare_vector(uc_callframes_t, uc_callframe_t); uc_declare_vector(uc_stack_t, uc_value_t *); uc_declare_vector(uc_breakpoints_t, uc_breakpoint_t *); -typedef struct printbuf uc_stringbuf_t; - typedef void (uc_exception_handler_t)(uc_vm_t *, uc_exception_t *); uc_declare_vector(uc_modexports_t, uc_upvalref_t *); @@ -305,12 +316,9 @@ char *_ucv_string_get(uc_value_t **); uc_stringbuf_t *ucv_stringbuf_new(void); uc_value_t *ucv_stringbuf_finish(uc_stringbuf_t *); -void _ucv_stringbuf_append(uc_stringbuf_t *, const char *, size_t); - -#define _ucv_is_literal(str) ("" str) -#define ucv_stringbuf_append(buf, str) _ucv_stringbuf_append(buf, _ucv_is_literal(str), sizeof(str) - 1) -#define ucv_stringbuf_addstr(buf, str, len) _ucv_stringbuf_append(buf, str, len) -#define ucv_stringbuf_printf(buf, fmt, ...) sprintbuf(buf, fmt, __VA_ARGS__) +#define ucv_stringbuf_append(buf, str) uc_stringbuf_append(buf, str) +#define ucv_stringbuf_addstr(buf, str, len) uc_stringbuf_add(buf, str, len) +#define ucv_stringbuf_printf(buf, fmt, ...) uc_stringbuf_printf(buf, fmt, __VA_ARGS__) uc_value_t *ucv_int64_new(int64_t); uc_value_t *ucv_uint64_new(uint64_t); @@ -341,18 +349,12 @@ void ucv_object_sort(uc_value_t *, int (*)(const void *, const void *)); void ucv_object_sort_r(uc_value_t *, int (*)(const char *, uc_value_t *, const char *, uc_value_t *, void *), void *); bool ucv_object_delete(uc_value_t *, const char *); size_t ucv_object_length(uc_value_t *); +bool ucv_object_next(uc_value_t *, size_t *, char **, uc_value_t **); -#define ucv_object_foreach(obj, key, val) \ - char *key = NULL; \ - uc_value_t *val = NULL; \ - struct lh_entry *entry##key; \ - struct lh_entry *entry_next##key = NULL; \ - for (entry##key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL; \ - (entry##key ? (key = (char *)lh_entry_k(entry##key), \ - val = (uc_value_t *)lh_entry_v(entry##key), \ - entry_next##key = entry##key->next, entry##key) \ - : 0); \ - entry##key = entry_next##key) +#define ucv_object_foreach(obj, key, val) \ + char *key = NULL; \ + uc_value_t *val = NULL; \ + for (size_t pos##key = 0; ucv_object_next((obj), &pos##key, &key, &val); ) uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t); @@ -474,9 +476,6 @@ bool ucv_prototype_set(uc_value_t *, uc_value_t *); uc_value_t *ucv_property_get(uc_value_t *, const char *); -uc_value_t *ucv_from_json(uc_vm_t *, json_object *); -json_object *ucv_to_json(uc_value_t *); - char *ucv_to_string(uc_vm_t *, uc_value_t *); char *ucv_to_jsonstring_formatted(uc_vm_t *, uc_value_t *, char, size_t); void ucv_to_stringbuf_formatted(uc_vm_t *, uc_stringbuf_t *, uc_value_t *, size_t, char, size_t); diff --git a/include/ucode/util.h b/include/ucode/util.h index 010b03db..a9a1ef91 100644 --- a/include/ucode/util.h +++ b/include/ucode/util.h @@ -21,11 +21,11 @@ #include #include +#include #include #include #include /* va_start(), va_end(), va_list */ #include /* strdup() */ -#include /* alignment & array size */ @@ -78,17 +78,6 @@ static inline char *xstrdup(const char *s) { return ptr; } -static inline struct json_tokener *xjs_new_tokener(void) { - struct json_tokener *tok = json_tokener_new(); - - if (!tok) { - fprintf(stderr, "Out of memory\n"); - abort(); - } - - return tok; -} - __attribute__((format(printf, 2, 0))) static inline int xasprintf(char **strp, const char *fmt, ...) { va_list ap; @@ -118,17 +107,35 @@ static inline int xvasprintf(char **strp, const char *fmt, va_list ap) { return len; } -static inline struct printbuf *xprintbuf_new(void) { - struct printbuf *pb = printbuf_new(); +/* string buffers; `buf` is NUL-terminated after `bpos` bytes */ - if (!pb) { - fprintf(stderr, "Out of memory\n"); - abort(); - } +typedef struct { + char *buf; + int bpos; + int size; +} uc_stringbuf_t; + +uc_stringbuf_t *uc_stringbuf_new(void); +void uc_stringbuf_free(uc_stringbuf_t *sb); +void uc_stringbuf_grow(uc_stringbuf_t *sb, size_t size); +int uc_stringbuf_memset(uc_stringbuf_t *sb, int offset, int c, size_t len); +void uc_stringbuf_reset(uc_stringbuf_t *sb); +int uc_stringbuf_printf(uc_stringbuf_t *sb, const char *fmt, ...); + +static inline void +uc_stringbuf_add(uc_stringbuf_t *sb, const char *s, size_t len) +{ + if ((size_t)sb->bpos + len + 1 > (size_t)sb->size) + uc_stringbuf_grow(sb, (size_t)sb->bpos + len + 1); - return pb; + memcpy(sb->buf + sb->bpos, s, len); + sb->bpos += len; + sb->buf[sb->bpos] = 0; } +#define uc_stringbuf_length(sb) ((sb)->bpos) +#define uc_stringbuf_append(sb, str) uc_stringbuf_add((sb), "" str, sizeof(str) - 1) + /* linked lists */ @@ -156,6 +163,88 @@ static inline void uc_list_remove(uc_list_t *item) for (uc_list_t *item = (list)->next; item != (list); item = item->next) +/* sets of pointers, kept in insertion order in `entries`; items are equal + * by `equal`, or as pointers if it is NULL, and `hash` must agree with it */ + +typedef struct { + size_t count; + void **entries; + size_t *index; + size_t mask; + unsigned long (*hash)(const void *); + int (*equal)(const void *, const void *); +} uc_set_t; + +static inline unsigned long +uc_set_hash(uc_set_t *set, const void *item) +{ + return set->hash ? set->hash(item) : (unsigned long)((uintptr_t)item * 2654435761u); +} + +static inline bool +uc_set_has(uc_set_t *set, const void *item) +{ + if (!set->index) + return false; + + for (size_t i = uc_set_hash(set, item) & set->mask; set->index[i]; i = (i + 1) & set->mask) { + void *e = set->entries[set->index[i] - 1]; + + if (set->equal ? set->equal(e, item) : (e == item)) + return true; + } + + return false; +} + +static inline void +uc_set_index_add(uc_set_t *set, size_t pos) +{ + size_t i = uc_set_hash(set, set->entries[pos]) & set->mask; + + while (set->index[i]) + i = (i + 1) & set->mask; + + set->index[i] = pos + 1; +} + +/* Adds `item` unless the set has it; returns whether it was added */ +static inline bool +uc_set_add(uc_set_t *set, void *item) +{ + size_t slots = set->index ? set->mask + 1 : 0; + + if (uc_set_has(set, item)) + return false; + + if ((set->count + 1) * 4 > slots * 3) { + slots = slots ? slots * 2 : 16; + free(set->index); + set->index = xcalloc(slots, sizeof(*set->index)); + set->mask = slots - 1; + set->entries = xrealloc(set->entries, slots * sizeof(*set->entries)); + + for (size_t pos = 0; pos < set->count; pos++) + uc_set_index_add(set, pos); + } + + set->entries[set->count] = item; + uc_set_index_add(set, set->count++); + + return true; +} + +static inline void +uc_set_free(uc_set_t *set) +{ + free(set->entries); + free(set->index); + set->entries = NULL; + set->index = NULL; + set->count = 0; +} + + /* vector macros */ #define UC_VECTOR_INIT_SIZE 8 diff --git a/json.c b/json.c new file mode 100644 index 00000000..1292885b --- /dev/null +++ b/json.c @@ -0,0 +1,850 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* Incremental JSON parser that builds ucode values. Its grammar, its errors + * and the positions where it stops follow the tokener of json-c 0.18 in its + * default mode, which json() used before, so that scripts see the same + * results: comments, single quoted strings, NaN, Infinity, literals in any + * case and trailing commas are accepted, and a NUL byte ends the input. + * Unlike json-c, object keys keep NUL bytes. */ + +#include +#include +#include +#include +#include + +#include "ucode/json.h" +#include "ucode/internal/types.h" + +#define UC_JSON_MAX_DEPTH 32 + +typedef enum { + STATE_EATWS, + STATE_START, + STATE_FINISH, + STATE_NULL, + STATE_COMMENT_START, + STATE_COMMENT, + STATE_COMMENT_EOL, + STATE_COMMENT_END, + STATE_STRING, + STATE_STRING_ESCAPE, + STATE_ESCAPE_UNICODE, + STATE_ESCAPE_UNICODE_NEED_ESCAPE, + STATE_ESCAPE_UNICODE_NEED_U, + STATE_BOOLEAN, + STATE_NUMBER, + STATE_ARRAY, + STATE_ARRAY_AFTER_SEP, + STATE_ARRAY_ADD, + STATE_ARRAY_SEP, + STATE_OBJECT_FIELD_START, + STATE_OBJECT_FIELD_START_AFTER_SEP, + STATE_OBJECT_FIELD, + STATE_OBJECT_FIELD_END, + STATE_OBJECT_VALUE, + STATE_OBJECT_VALUE_ADD, + STATE_OBJECT_SEP, + STATE_INF, +} uc_json_state_t; + +typedef struct { + uc_json_state_t state, saved_state; + uc_value_t *current; + uc_value_t *key; +} uc_json_level_t; + +struct uc_json_parser { + uc_vm_t *vm; + uc_json_level_t stack[UC_JSON_MAX_DEPTH]; + int depth; + uc_json_error_t err; + size_t offset; + uc_stringbuf_t pb; + char quote; + int st_pos; + bool is_double; + unsigned int ucs_char, high_surrogate; +}; + +static const char *uc_json_errors[] = { + "success", + "continue", + "nesting too deep", + "unexpected end of data", + "unexpected character", + "null expected", + "boolean expected", + "number expected", + "array value separator ',' expected", + "quoted object property name expected", + "object property name separator ':' expected", + "object value separator ',' expected", + "invalid string sequence", + "expected comment", +}; + +static const char replacement_char[3] = { (char)0xef, (char)0xbf, (char)0xbd }; + +const char * +uc_json_error_desc(uc_json_error_t err) +{ + if ((size_t)err >= sizeof(uc_json_errors) / sizeof(uc_json_errors[0])) + return "unknown error"; + + return uc_json_errors[err]; +} + +static bool +uc_json_is_ws(char c) +{ + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; +} + +static bool +uc_json_is_hex(char c) +{ + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); +} + +static unsigned int +uc_json_hex(char c) +{ + return (c <= '9') ? c - '0' : (c & 7) + 9; +} + +static void +uc_json_level_reset(uc_json_parser_t *p, int depth) +{ + uc_json_level_t *l = &p->stack[depth]; + + l->state = STATE_EATWS; + l->saved_state = STATE_START; + ucv_put(l->current); + l->current = NULL; + ucv_put(l->key); + l->key = NULL; +} + +static void +uc_json_reset(uc_json_parser_t *p) +{ + for (int i = p->depth; i >= 0; i--) + uc_json_level_reset(p, i); + + p->depth = 0; +} + +uc_json_parser_t * +uc_json_parser_new(uc_vm_t *vm) +{ + uc_json_parser_t *p = xalloc(sizeof(*p)); + + p->vm = vm; + uc_json_reset(p); + + return p; +} + +void +uc_json_parser_free(uc_json_parser_t *p) +{ + if (!p) + return; + + uc_json_reset(p); + free(p->pb.buf); + free(p); +} + +uc_json_error_t +uc_json_parser_error(uc_json_parser_t *p) +{ + return p->err; +} + +size_t +uc_json_parser_end(uc_json_parser_t *p) +{ + return p->offset; +} + +static void +uc_json_utf8_add(uc_stringbuf_t *pb, unsigned int c) +{ + char u[4]; + + if (c < 0x80) { + u[0] = c; + uc_stringbuf_add(pb, u, 1); + } + else if (c < 0x800) { + u[0] = 0xc0 | (c >> 6); + u[1] = 0x80 | (c & 0x3f); + uc_stringbuf_add(pb, u, 2); + } + else if (c < 0x10000) { + u[0] = 0xe0 | (c >> 12); + u[1] = 0x80 | ((c >> 6) & 0x3f); + u[2] = 0x80 | (c & 0x3f); + uc_stringbuf_add(pb, u, 3); + } + else if (c < 0x110000) { + u[0] = 0xf0 | ((c >> 18) & 0x07); + u[1] = 0x80 | ((c >> 12) & 0x3f); + u[2] = 0x80 | ((c >> 6) & 0x3f); + u[3] = 0x80 | (c & 0x3f); + uc_stringbuf_add(pb, u, 4); + } + else { + uc_stringbuf_add(pb, replacement_char, 3); + } +} + +/* Converts the collected number text, as json_parse_int64(), + * json_parse_uint64() and strtod() did for json-c and ucv_from_json() did + * with the result; returns false if it is no number. */ +static bool +uc_json_number(uc_json_parser_t *p, uc_value_t **res) +{ + const char *s = p->pb.buf; + uint64_t u64; + int64_t n64; + double d; + char *end; + + if (p->is_double) { + d = strtod(s, &end); + + if (end != s + p->pb.bpos) + return false; + + *res = ucv_double_new(d); + + return true; + } + + errno = 0; + + if (*s == '-') { + n64 = strtoll(s, &end, 10); + + if (end == s || (n64 == 0 && errno != 0)) + return false; + + *res = ucv_int64_new(n64); + + return true; + } + + u64 = strtoull(s, &end, 10); + + if (end == s || (u64 == 0 && errno != 0)) + return false; + + *res = (u64 < INT64_MAX) ? ucv_int64_new((int64_t)u64) : ucv_uint64_new(u64); + + return true; +} + +#define state p->stack[p->depth].state +#define saved_state p->stack[p->depth].saved_state +#define current p->stack[p->depth].current +#define field_key p->stack[p->depth].key + +/* Takes the next input byte into `dest`; at the end of the input, sets + * the result of the call, which is complete if a top-level value is. */ +#define PEEK_CHAR(dest) \ + ((p->offset == len) \ + ? ((p->depth == 0 && state == STATE_EATWS && saved_state == STATE_FINISH) \ + ? ((p->err = UC_JSON_SUCCESS), 0) \ + : ((p->err = UC_JSON_CONTINUE), 0)) \ + : (((dest) = *str), 1)) + +/* Advances to the next byte; yields the previous byte, 0 at a NUL byte */ +#define ADVANCE_CHAR() (++str, p->offset++, c) + +uc_value_t * +uc_json_parse(uc_json_parser_t *p, const char *str, size_t len) +{ + uc_value_t *obj = NULL, *res; + const char *case_start; + size_t case_len, size; + bool is_exponent, neg_sign_ok, pos_sign_ok; + char c = '\1', *e_loc, last; + + p->offset = 0; + p->err = UC_JSON_SUCCESS; + + while (PEEK_CHAR(c)) { +redo_char: + switch (state) { + case STATE_EATWS: + while (uc_json_is_ws(c)) + if (!ADVANCE_CHAR() || !PEEK_CHAR(c)) + goto out; + + if (c == '/') { + uc_stringbuf_reset(&p->pb); + uc_stringbuf_add(&p->pb, &c, 1); + state = STATE_COMMENT_START; + } + else { + state = saved_state; + goto redo_char; + } + + break; + + case STATE_START: + switch (c) { + case '{': + state = STATE_EATWS; + saved_state = STATE_OBJECT_FIELD_START; + current = ucv_object_new(p->vm); + break; + + case '[': + state = STATE_EATWS; + saved_state = STATE_ARRAY; + current = ucv_array_new(p->vm); + break; + + case 'I': + case 'i': + state = STATE_INF; + uc_stringbuf_reset(&p->pb); + p->st_pos = 0; + goto redo_char; + + case 'N': + case 'n': + state = STATE_NULL; + uc_stringbuf_reset(&p->pb); + p->st_pos = 0; + goto redo_char; + + case '\'': + case '"': + state = STATE_STRING; + uc_stringbuf_reset(&p->pb); + p->quote = c; + break; + + case 'T': + case 't': + case 'F': + case 'f': + state = STATE_BOOLEAN; + uc_stringbuf_reset(&p->pb); + p->st_pos = 0; + goto redo_char; + + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + case '-': + state = STATE_NUMBER; + uc_stringbuf_reset(&p->pb); + p->is_double = false; + goto redo_char; + + default: + p->err = UC_JSON_ERROR_UNEXPECTED; + goto out; + } + + break; + + case STATE_FINISH: + if (p->depth == 0) + goto out; + + obj = current; + current = NULL; + uc_json_level_reset(p, p->depth); + p->depth--; + goto redo_char; + + case STATE_INF: + while (p->st_pos < 8) { + if (*str != "Infinity"[p->st_pos] && *str != "iNFINITY"[p->st_pos]) { + p->err = UC_JSON_ERROR_UNEXPECTED; + goto out; + } + + p->st_pos++; + (void)ADVANCE_CHAR(); + + if (!PEEK_CHAR(c)) + goto out; + } + + current = ucv_double_new((p->pb.bpos > 0 && p->pb.buf[0] == '-') ? -INFINITY : INFINITY); + saved_state = STATE_FINISH; + state = STATE_EATWS; + goto redo_char; + + case STATE_NULL: + uc_stringbuf_add(&p->pb, &c, 1); + size = (p->st_pos + 1 < 4) ? p->st_pos + 1 : 4; + + if (strncasecmp("null", p->pb.buf, size) == 0) { + if (p->st_pos == 4) { + current = NULL; + saved_state = STATE_FINISH; + state = STATE_EATWS; + goto redo_char; + } + } + else if (strncasecmp("NaN", p->pb.buf, (p->st_pos + 1 < 3) ? p->st_pos + 1 : 3) == 0) { + if (p->st_pos == 3) { + current = ucv_double_new(NAN); + saved_state = STATE_FINISH; + state = STATE_EATWS; + goto redo_char; + } + } + else { + p->err = UC_JSON_ERROR_NULL; + goto out; + } + + p->st_pos++; + break; + + case STATE_COMMENT_START: + if (c == '*') { + state = STATE_COMMENT; + } + else if (c == '/') { + state = STATE_COMMENT_EOL; + } + else { + p->err = UC_JSON_ERROR_COMMENT; + goto out; + } + + uc_stringbuf_add(&p->pb, &c, 1); + break; + + case STATE_COMMENT: + case_start = str; + + while (c != '*') { + if (!ADVANCE_CHAR() || !PEEK_CHAR(c)) { + uc_stringbuf_add(&p->pb, case_start, str - case_start); + goto out; + } + } + + uc_stringbuf_add(&p->pb, case_start, 1 + str - case_start); + state = STATE_COMMENT_END; + break; + + case STATE_COMMENT_EOL: + case_start = str; + + while (c != '\n') { + if (!ADVANCE_CHAR() || !PEEK_CHAR(c)) { + uc_stringbuf_add(&p->pb, case_start, str - case_start); + goto out; + } + } + + uc_stringbuf_add(&p->pb, case_start, str - case_start); + state = STATE_EATWS; + break; + + case STATE_COMMENT_END: + uc_stringbuf_add(&p->pb, &c, 1); + state = (c == '/') ? STATE_EATWS : STATE_COMMENT; + break; + + case STATE_STRING: + case STATE_OBJECT_FIELD: + case_start = str; + + while (true) { + if (c == p->quote) { + uc_stringbuf_add(&p->pb, case_start, str - case_start); + + if (state == STATE_STRING) { + current = ucv_string_new_length(p->pb.buf, p->pb.bpos); + saved_state = STATE_FINISH; + } + else { + field_key = ucv_string_new_length(p->pb.buf, p->pb.bpos); + saved_state = STATE_OBJECT_FIELD_END; + } + + state = STATE_EATWS; + break; + } + else if (c == '\\') { + uc_stringbuf_add(&p->pb, case_start, str - case_start); + saved_state = state; + state = STATE_STRING_ESCAPE; + break; + } + + if (!ADVANCE_CHAR() || !PEEK_CHAR(c)) { + uc_stringbuf_add(&p->pb, case_start, str - case_start); + goto out; + } + } + + break; + + case STATE_STRING_ESCAPE: + switch (c) { + case '"': + case '\\': + case '/': + uc_stringbuf_add(&p->pb, &c, 1); + state = saved_state; + break; + + case 'b': uc_stringbuf_add(&p->pb, "\b", 1); state = saved_state; break; + case 'n': uc_stringbuf_add(&p->pb, "\n", 1); state = saved_state; break; + case 'r': uc_stringbuf_add(&p->pb, "\r", 1); state = saved_state; break; + case 't': uc_stringbuf_add(&p->pb, "\t", 1); state = saved_state; break; + case 'f': uc_stringbuf_add(&p->pb, "\f", 1); state = saved_state; break; + + case 'u': + p->ucs_char = 0; + p->st_pos = 0; + state = STATE_ESCAPE_UNICODE; + break; + + default: + p->err = UC_JSON_ERROR_STRING; + goto out; + } + + break; + + case STATE_ESCAPE_UNICODE: + while (true) { + if (!c || !uc_json_is_hex(c)) { + p->err = UC_JSON_ERROR_STRING; + goto out; + } + + p->ucs_char |= uc_json_hex(c) << ((3 - p->st_pos) * 4); + p->st_pos++; + + if (p->st_pos >= 4) + break; + + (void)ADVANCE_CHAR(); + + if (!PEEK_CHAR(c)) + goto out; + } + + p->st_pos = 0; + + if (p->high_surrogate) { + if ((p->ucs_char & 0xfc00) == 0xdc00) + p->ucs_char = ((p->high_surrogate & 0x3ff) << 10) + + (p->ucs_char & 0x3ff) + 0x10000; + else + uc_stringbuf_add(&p->pb, replacement_char, 3); + + p->high_surrogate = 0; + } + + if ((p->ucs_char & 0xfc00) == 0xd800) { + p->high_surrogate = p->ucs_char; + p->ucs_char = 0; + state = STATE_ESCAPE_UNICODE_NEED_ESCAPE; + break; + } + + if ((p->ucs_char & 0xfc00) == 0xdc00) + uc_stringbuf_add(&p->pb, replacement_char, 3); + else + uc_json_utf8_add(&p->pb, p->ucs_char); + + state = saved_state; + break; + + case STATE_ESCAPE_UNICODE_NEED_ESCAPE: + if (!c || c != '\\') { + uc_stringbuf_add(&p->pb, replacement_char, 3); + p->high_surrogate = 0; + p->ucs_char = 0; + p->st_pos = 0; + state = saved_state; + goto redo_char; + } + + state = STATE_ESCAPE_UNICODE_NEED_U; + break; + + case STATE_ESCAPE_UNICODE_NEED_U: + if (!c || c != 'u') { + uc_stringbuf_add(&p->pb, replacement_char, 3); + p->high_surrogate = 0; + p->ucs_char = 0; + p->st_pos = 0; + state = STATE_STRING_ESCAPE; + goto redo_char; + } + + state = STATE_ESCAPE_UNICODE; + break; + + case STATE_BOOLEAN: + uc_stringbuf_add(&p->pb, &c, 1); + + if (strncasecmp("true", p->pb.buf, (p->st_pos + 1 < 4) ? p->st_pos + 1 : 4) == 0) { + if (p->st_pos == 4) { + current = ucv_boolean_new(true); + saved_state = STATE_FINISH; + state = STATE_EATWS; + goto redo_char; + } + } + else if (strncasecmp("false", p->pb.buf, (p->st_pos + 1 < 5) ? p->st_pos + 1 : 5) == 0) { + if (p->st_pos == 5) { + current = ucv_boolean_new(false); + saved_state = STATE_FINISH; + state = STATE_EATWS; + goto redo_char; + } + } + else { + p->err = UC_JSON_ERROR_BOOLEAN; + goto out; + } + + p->st_pos++; + break; + + case STATE_NUMBER: + case_start = str; + case_len = 0; + is_exponent = false; + neg_sign_ok = true; + pos_sign_ok = false; + + /* resume the flags of a number that the last input ended in */ + if (p->pb.bpos > 0) { + e_loc = memchr(p->pb.buf, 'e', p->pb.bpos); + + if (!e_loc) + e_loc = memchr(p->pb.buf, 'E', p->pb.bpos); + + if (e_loc) { + is_exponent = true; + pos_sign_ok = neg_sign_ok = (e_loc == &p->pb.buf[p->pb.bpos - 1]); + } + } + + while (c && ((c >= '0' && c <= '9') || + (!is_exponent && (c == 'e' || c == 'E')) || + (neg_sign_ok && c == '-') || (pos_sign_ok && c == '+') || + (!p->is_double && c == '.'))) { + pos_sign_ok = neg_sign_ok = false; + case_len++; + + if (c == '.') { + p->is_double = true; + pos_sign_ok = neg_sign_ok = true; + } + else if (c == 'e' || c == 'E') { + is_exponent = true; + p->is_double = true; + pos_sign_ok = neg_sign_ok = true; + } + + if (!ADVANCE_CHAR() || !PEEK_CHAR(c)) { + uc_stringbuf_add(&p->pb, case_start, case_len); + goto out; + } + } + + /* within a container, a byte that cannot follow a number is + * an error; at the top level, it may start the next input */ + if (p->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' && + c != 'I' && c != 'i' && !uc_json_is_ws(c)) { + p->err = UC_JSON_ERROR_NUMBER; + goto out; + } + + if (case_len > 0) + uc_stringbuf_add(&p->pb, case_start, case_len); + + if (p->pb.buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I')) { + state = STATE_INF; + p->st_pos = 0; + goto redo_char; + } + + /* "123e+" and the like parse as their digits */ + while (p->is_double && p->pb.bpos > 1) { + last = p->pb.buf[p->pb.bpos - 1]; + + if (last != 'e' && last != 'E' && last != '-' && last != '+') + break; + + p->pb.buf[--p->pb.bpos] = 0; + } + + if (!uc_json_number(p, &res)) { + p->err = UC_JSON_ERROR_NUMBER; + goto out; + } + + current = res; + saved_state = STATE_FINISH; + state = STATE_EATWS; + goto redo_char; + + case STATE_ARRAY_AFTER_SEP: + case STATE_ARRAY: + if (c == ']') { + saved_state = STATE_FINISH; + state = STATE_EATWS; + } + else { + if (p->depth >= UC_JSON_MAX_DEPTH - 1) { + p->err = UC_JSON_ERROR_DEPTH; + goto out; + } + + state = STATE_ARRAY_ADD; + p->depth++; + uc_json_level_reset(p, p->depth); + goto redo_char; + } + + break; + + case STATE_ARRAY_ADD: + ucv_array_push(current, obj); + obj = NULL; + saved_state = STATE_ARRAY_SEP; + state = STATE_EATWS; + goto redo_char; + + case STATE_ARRAY_SEP: + if (c == ']') { + saved_state = STATE_FINISH; + state = STATE_EATWS; + } + else if (c == ',') { + saved_state = STATE_ARRAY_AFTER_SEP; + state = STATE_EATWS; + } + else { + p->err = UC_JSON_ERROR_ARRAY; + goto out; + } + + break; + + case STATE_OBJECT_FIELD_START: + case STATE_OBJECT_FIELD_START_AFTER_SEP: + if (c == '}') { + saved_state = STATE_FINISH; + state = STATE_EATWS; + } + else if (c == '"' || c == '\'') { + p->quote = c; + uc_stringbuf_reset(&p->pb); + state = STATE_OBJECT_FIELD; + } + else { + p->err = UC_JSON_ERROR_KEY_NAME; + goto out; + } + + break; + + case STATE_OBJECT_FIELD_END: + if (c == ':') { + saved_state = STATE_OBJECT_VALUE; + state = STATE_EATWS; + } + else { + p->err = UC_JSON_ERROR_KEY_SEP; + goto out; + } + + break; + + case STATE_OBJECT_VALUE: + if (p->depth >= UC_JSON_MAX_DEPTH - 1) { + p->err = UC_JSON_ERROR_DEPTH; + goto out; + } + + state = STATE_OBJECT_VALUE_ADD; + p->depth++; + uc_json_level_reset(p, p->depth); + goto redo_char; + + case STATE_OBJECT_VALUE_ADD: + ucv_object_insert_string(current, field_key, obj); + obj = NULL; + ucv_put(field_key); + field_key = NULL; + saved_state = STATE_OBJECT_SEP; + state = STATE_EATWS; + goto redo_char; + + case STATE_OBJECT_SEP: + if (c == '}') { + saved_state = STATE_FINISH; + state = STATE_EATWS; + } + else if (c == ',') { + saved_state = STATE_OBJECT_FIELD_START_AFTER_SEP; + state = STATE_EATWS; + } + else { + p->err = UC_JSON_ERROR_VALUE_SEP; + goto out; + } + + break; + } + + (void)ADVANCE_CHAR(); + + if (!c) + break; + } + +out: + /* a NUL byte ends the input */ + if (!c && state != STATE_FINISH && saved_state != STATE_FINISH) + p->err = UC_JSON_ERROR_EOF; + + ucv_put(obj); + + if (p->err != UC_JSON_SUCCESS) + return NULL; + + res = current; + current = NULL; + uc_json_reset(p); + + return res; +} diff --git a/lib.c b/lib.c index 6a802227..6b676ec2 100644 --- a/lib.c +++ b/lib.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -50,7 +51,7 @@ #include "ucode/internal/source.h" #include "ucode/internal/program.h" #include "ucode/internal/platform.h" -#include "ucode/internal/json-c-compat.h" +#include "ucode/json.h" /* Appends the printed form of `line`, followed by a marker underlining the * byte range [sbegin, send) of the enclosing statement within it. When no @@ -278,7 +279,7 @@ uc_error_message_indent(char **msg) { if (!msg || !*msg) return; - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); s = *msg; len = strlen(s); @@ -995,7 +996,7 @@ uc_exists(uc_vm_t *vm, size_t nargs) k = uc_cast_string(vm, &key, &freeable); - ucv_object_get(obj, k, &found); + ucv_object_lookup(obj, k, freeable ? strlen(k) : ucv_string_length(key), &found); if (freeable) free(k); @@ -1379,14 +1380,53 @@ uc_keys(uc_vm_t *vm, size_t nargs) arr = ucv_array_new(vm); - ucv_object_foreach(obj, key, val) { - (void)val; - ucv_array_push(arr, ucv_string_new(key)); - } + ucv_object_foreach_entry(obj, e) + ucv_array_push(arr, ucv_get(e->key)); return arr; } +/* The string value of the argument with its ASCII letters in lower case, or + * in upper case; a string converts into the result without a buffer */ +static uc_value_t * +uc_case_convert(uc_vm_t *vm, size_t nargs, bool upper) +{ + uc_value_t *arg = uc_fn_arg(0), *rv = NULL; + uc_stringbuf_t *buf = NULL; + char tmp[sizeof(void *)], *s, *d; + size_t i, len; + + if (ucv_type(arg) == UC_STRING) { + s = ucv_string_get(arg); + len = ucv_string_length(arg); + } + else { + buf = uc_stringbuf_new(); + ucv_to_stringbuf(vm, buf, arg, false); + s = buf->buf; + len = uc_stringbuf_length(buf); + } + + if (len + 1 < sizeof(void *)) + d = tmp; + else + rv = ucv_string_alloc(&d, len); + + for (i = 0; i < len; i++) { + if (upper) + d[i] = (s[i] >= 'a' && s[i] <= 'z') ? (s[i] & ~32) : s[i]; + else + d[i] = (s[i] >= 'A' && s[i] <= 'Z') ? (s[i] | 32) : s[i]; + } + + if (!rv) + rv = ucv_string_new_length(tmp, len); + + uc_stringbuf_free(buf); + + return rv; +} + /** * Convert the given string to lowercase and return the resulting string. * @@ -1406,25 +1446,7 @@ uc_keys(uc_vm_t *vm, size_t nargs) static uc_value_t * uc_lc(uc_vm_t *vm, size_t nargs) { - uc_stringbuf_t *buf = xprintbuf_new(); - uc_value_t *rv; - size_t i, len; - char *s; - - ucv_to_stringbuf(vm, buf, uc_fn_arg(0), false); - - s = buf->buf; - len = printbuf_length(buf); - - for (i = 0; i < len; i++) - if (s[i] >= 'A' && s[i] <= 'Z') - s[i] |= 32; - - rv = ucv_string_new_length(s, len); - - printbuf_free(buf); - - return rv; + return uc_case_convert(vm, nargs, false); } /** @@ -1757,9 +1779,10 @@ array_sort_fn(uc_value_t *v1, uc_value_t *v2, void *ud) } static int -object_sort_fn(const char *k1, uc_value_t *v1, const char *k2, uc_value_t *v2, - void *ud) +object_sort_fn(const uc_object_entry_t *e1, const uc_object_entry_t *e2, void *ud) { + size_t l1 = ucv_string_length(e1->key), l2 = ucv_string_length(e2->key); + uc_value_t *k1 = e1->key, *k2 = e2->key; uc_value_t *rv, *null = ucv_int64_new(0); sort_ctx_t *ctx = ud; int res; @@ -1767,15 +1790,18 @@ object_sort_fn(const char *k1, uc_value_t *v1, const char *k2, uc_value_t *v2, if (ctx->ex) return 0; - if (!ctx->fn) - return strcmp(k1, k2); + if (!ctx->fn) { + res = memcmp(ucv_string_get(k1), ucv_string_get(k2), l1 < l2 ? l1 : l2); + + return res ? res : (l1 > l2) - (l1 < l2); + } uc_vm_ctx_push(ctx->vm); uc_vm_stack_push(ctx->vm, ucv_get(ctx->fn)); - uc_vm_stack_push(ctx->vm, ucv_string_new(k1)); - uc_vm_stack_push(ctx->vm, ucv_string_new(k2)); - uc_vm_stack_push(ctx->vm, ucv_get(v1)); - uc_vm_stack_push(ctx->vm, ucv_get(v2)); + uc_vm_stack_push(ctx->vm, ucv_get(k1)); + uc_vm_stack_push(ctx->vm, ucv_get(k2)); + uc_vm_stack_push(ctx->vm, ucv_get(e1->value)); + uc_vm_stack_push(ctx->vm, ucv_get(e2->value)); if (uc_vm_call(ctx->vm, true, 4)) { ctx->ex = true; @@ -1844,7 +1870,7 @@ uc_sort(uc_vm_t *vm, size_t nargs) break; case UC_OBJECT: - ucv_object_sort_r(val, object_sort_fn, &ctx); + ucv_object_sort_entries(val, object_sort_fn, &ctx); break; default: @@ -2292,25 +2318,7 @@ uc_time(uc_vm_t *vm, size_t nargs) static uc_value_t * uc_uc(uc_vm_t *vm, size_t nargs) { - uc_stringbuf_t *buf = xprintbuf_new(); - uc_value_t *rv; - size_t i, len; - char *s; - - ucv_to_stringbuf(vm, buf, uc_fn_arg(0), false); - - s = buf->buf; - len = printbuf_length(buf); - - for (i = 0; i < len; i++) - if (s[i] >= 'a' && s[i] <= 'z') - s[i] &= ~32; - - rv = ucv_string_new_length(s, len); - - printbuf_free(buf); - - return rv; + return uc_case_convert(vm, nargs, true); } /** @@ -2920,14 +2928,14 @@ uc_sprintf(uc_vm_t *vm, size_t nargs) static uc_value_t * uc_printf(uc_vm_t *vm, size_t nargs) { - uc_stringbuf_t *buf = xprintbuf_new(); + uc_stringbuf_t *buf = uc_stringbuf_new(); size_t len; uc_printf_common(vm, nargs, buf); - len = fwrite(buf->buf, 1, printbuf_length(buf), vm->output); + len = fwrite(buf->buf, 1, uc_stringbuf_length(buf), vm->output); - printbuf_free(buf); + uc_stringbuf_free(buf); return ucv_int64_new(len); } @@ -3064,7 +3072,7 @@ static bool uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, uc_value_t **res, bool module_mode) { - uc_stringbuf_t *buf = xprintbuf_new(); + uc_stringbuf_t *buf = uc_stringbuf_new(); const char *p, *q, *last; uc_value_t *modtable; bool rv; @@ -3110,7 +3118,7 @@ uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, ucv_object_add(modtable, name, ucv_get(*res)); out: - printbuf_free(buf); + uc_stringbuf_free(buf); return rv; } @@ -3766,13 +3774,13 @@ uc_replace(uc_vm_t *vm, size_t nargs) return ucv_stringbuf_finish(resbuf); } -static struct json_tokener * -uc_json_from_object(uc_vm_t *vm, uc_value_t *obj, json_object **jso) +static uc_json_parser_t * +uc_json_from_object(uc_vm_t *vm, uc_value_t *obj, uc_value_t **val) { bool trail = false, eof = false; - enum json_tokener_error err; - struct json_tokener *tok = NULL; + uc_json_parser_t *parser = NULL; uc_value_t *rfn, *rbuf = NULL; + uc_json_error_t err; uc_stringbuf_t *buf; rfn = ucv_get(ucv_property_get(obj, "read")); @@ -3784,7 +3792,7 @@ uc_json_from_object(uc_vm_t *vm, uc_value_t *obj, json_object **jso) goto out; } - tok = xjs_new_tokener(); + parser = uc_json_parser_new(vm); while (true) { uc_vm_stack_push(vm, ucv_get(obj)); @@ -3801,16 +3809,16 @@ uc_json_from_object(uc_vm_t *vm, uc_value_t *obj, json_object **jso) /* on EOF, stop parsing unless trailing garbage was detected which handled below */ if (eof && !trail) { - /* Didn't parse a complete object yet, possibly a non-delimited atomic value - such as `null`, `true` etc. - nudge parser by sending final zero byte. - See json-c issue #681 */ - if (json_tokener_get_error(tok) == json_tokener_continue) - *jso = json_tokener_parse_ex(tok, "\0", 1); + /* Didn't parse a complete value yet, possibly a non-delimited atomic + value such as `null`, `true` etc. - nudge the parser by sending a + final zero byte, which ends the input. */ + if (uc_json_parser_error(parser) == UC_JSON_CONTINUE) + *val = uc_json_parse(parser, "\0", 1); break; } - if (trail || *jso) { + if (trail || *val) { uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "Trailing garbage after JSON data"); @@ -3818,74 +3826,73 @@ uc_json_from_object(uc_vm_t *vm, uc_value_t *obj, json_object **jso) } if (ucv_type(rbuf) != UC_STRING) { - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); ucv_to_stringbuf_formatted(vm, buf, rbuf, 0, '\0', 0); - *jso = json_tokener_parse_ex(tok, buf->buf, printbuf_length(buf)); + *val = uc_json_parse(parser, buf->buf, uc_stringbuf_length(buf)); - trail = (json_tokener_get_error(tok) == json_tokener_success && - json_tokener_get_parse_end(tok) < (size_t)printbuf_length(buf)); + trail = (uc_json_parser_error(parser) == UC_JSON_SUCCESS && + uc_json_parser_end(parser) < (size_t)uc_stringbuf_length(buf)); - printbuf_free(buf); + uc_stringbuf_free(buf); } else { - *jso = json_tokener_parse_ex(tok, ucv_string_get(rbuf), ucv_string_length(rbuf)); + *val = uc_json_parse(parser, ucv_string_get(rbuf), ucv_string_length(rbuf)); - trail = (json_tokener_get_error(tok) == json_tokener_success && - json_tokener_get_parse_end(tok) < ucv_string_length(rbuf)); + trail = (uc_json_parser_error(parser) == UC_JSON_SUCCESS && + uc_json_parser_end(parser) < ucv_string_length(rbuf)); } ucv_put(rbuf); rbuf = NULL; - err = json_tokener_get_error(tok); + err = uc_json_parser_error(parser); - if (err != json_tokener_success && err != json_tokener_continue) + if (err != UC_JSON_SUCCESS && err != UC_JSON_CONTINUE) break; } goto out; fail: - json_tokener_free(tok); - tok = NULL; + uc_json_parser_free(parser); + parser = NULL; out: ucv_put(rfn); ucv_put(rbuf); - return tok; + return parser; } -static struct json_tokener * -uc_json_from_string(uc_vm_t *vm, uc_value_t *str, json_object **jso) +static uc_json_parser_t * +uc_json_from_string(uc_vm_t *vm, uc_value_t *str, uc_value_t **val) { - struct json_tokener *tok = xjs_new_tokener(); + uc_json_parser_t *parser = uc_json_parser_new(vm); size_t i; char *p; - /* NB: the len + 1 here is intentional to pass the terminating \0 byte - * to the json-c parser. This is required to work-around upstream - * issue #681 */ - *jso = json_tokener_parse_ex(tok, ucv_string_get(str), ucv_string_length(str) + 1); + /* NB: the len + 1 here is intentional, the terminating \0 byte ends the + * input, so that values without delimiter such as `null` complete */ + *val = uc_json_parse(parser, ucv_string_get(str), ucv_string_length(str) + 1); - if (json_tokener_get_error(tok) == json_tokener_success) { + if (uc_json_parser_error(parser) == UC_JSON_SUCCESS) { p = ucv_string_get(str); - for (i = json_tokener_get_parse_end(tok); i < ucv_string_length(str); i++) { + for (i = uc_json_parser_end(parser); i < ucv_string_length(str); i++) { if (!isspace(p[i])) { uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "Trailing garbage after JSON data"); - json_tokener_free(tok); + uc_json_parser_free(parser); return NULL; } } } - return tok; + return parser; } /** @@ -3929,20 +3936,19 @@ uc_json_from_string(uc_vm_t *vm, uc_value_t *str, json_object **jso) static uc_value_t * uc_json(uc_vm_t *vm, size_t nargs) { - uc_value_t *rv = NULL, *src = uc_fn_arg(0); - struct json_tokener *tok = NULL; - enum json_tokener_error err; - json_object *jso = NULL; + uc_value_t *rv = NULL, *val = NULL, *src = uc_fn_arg(0); + uc_json_parser_t *parser = NULL; + uc_json_error_t err; switch (ucv_type(src)) { case UC_STRING: - tok = uc_json_from_string(vm, src, &jso); + parser = uc_json_from_string(vm, src, &val); break; case UC_RESOURCE: case UC_OBJECT: case UC_ARRAY: - tok = uc_json_from_object(vm, src, &jso); + parser = uc_json_from_object(vm, src, &val); break; default: @@ -3950,32 +3956,31 @@ uc_json(uc_vm_t *vm, size_t nargs) "Passed value is neither a string nor an object"); } - if (!tok) + if (!parser) goto out; - err = json_tokener_get_error(tok); + err = uc_json_parser_error(parser); - if (err == json_tokener_continue) { + if (err == UC_JSON_CONTINUE) { uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "Unexpected end of string in JSON data"); goto out; } - else if (err != json_tokener_success) { + else if (err != UC_JSON_SUCCESS) { uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "Failed to parse JSON string: %s", - json_tokener_error_desc(err)); + uc_json_error_desc(err)); goto out; } - rv = ucv_from_json(vm, jso); + rv = val; + val = NULL; out: - if (tok) - json_tokener_free(tok); - - json_object_put(jso); + uc_json_parser_free(parser); + ucv_put(val); return rv; } @@ -4057,8 +4062,8 @@ uc_include_common(uc_vm_t *vm, size_t nargs, bool raw_mode) else if (scope) { sc = ucv_object_new(vm); - ucv_object_foreach(scope, key, val) - ucv_object_add(sc, key, ucv_get(val)); + ucv_object_foreach_entry(scope, e) + ucv_object_insert_string(sc, e->key, ucv_get(e->value)); ucv_prototype_set(sc, ucv_get(uc_vm_scope_get(vm))); } @@ -4987,13 +4992,13 @@ uc_b64dec(uc_vm_t *vm, size_t nargs) buf = ucv_stringbuf_new(); src = ucv_string_get(str); - off = printbuf_length(buf); + off = uc_stringbuf_length(buf); state = BYTE1; /* memset the last expected output char to pre-grow the output buffer */ - if (printbuf_memset(buf, off + ((ucv_string_length(str) + 3) / 4) * 3, 0, 1)) { - printbuf_free(buf); + if (uc_stringbuf_memset(buf, off + ((ucv_string_length(str) + 3) / 4) * 3, 0, 1)) { + uc_stringbuf_free(buf); return NULL; } @@ -5100,7 +5105,7 @@ uc_b64dec(uc_vm_t *vm, size_t nargs) return ucv_stringbuf_finish(buf); err: - printbuf_free(buf); + uc_stringbuf_free(buf); return NULL; } @@ -5220,7 +5225,7 @@ uc_uniq_ucv_hash(const void *k) } while (len > 0) { - h = h * 129 + (*u8++) + LH_PRIME; + h = h * 129 + (*u8++) + 0x9e370001UL; len--; } @@ -5265,31 +5270,24 @@ static uc_value_t * uc_uniq(uc_vm_t *vm, size_t nargs) { uc_value_t *list = uc_fn_arg(0); + uc_set_t seen = { .hash = uc_uniq_ucv_hash, .equal = uc_uniq_ucv_equal }; uc_value_t *uniq = NULL; - struct lh_table *seen; - unsigned long hash; uc_value_t *item; size_t i, len; if (ucv_type(list) != UC_ARRAY) return NULL; - seen = lh_table_new(16, NULL, uc_uniq_ucv_hash, uc_uniq_ucv_equal); uniq = ucv_array_new(vm); - assert(seen && uniq); - for (i = 0, len = ucv_array_length(list); i < len; i++) { item = ucv_array_get(list, i); - hash = lh_get_hash(seen, item); - if (!lh_table_lookup_entry_w_hash(seen, item, hash)) { - lh_table_insert_w_hash(seen, item, NULL, hash, 0); + if (uc_set_add(&seen, item)) ucv_array_push(uniq, ucv_get(item)); - } } - lh_table_free(seen); + uc_set_free(&seen); return uniq; } @@ -5586,14 +5584,14 @@ uc_hexenc(uc_vm_t *vm, size_t nargs) return NULL; buf = ucv_stringbuf_new(); - off = printbuf_length(buf); + off = uc_stringbuf_length(buf); ucv_to_stringbuf(vm, buf, input, false); - len = printbuf_length(buf) - off; + len = uc_stringbuf_length(buf) - off; /* memset the last expected output char to grow the output buffer */ - printbuf_memset(buf, off + len * 2, 0, 1); + uc_stringbuf_memset(buf, off + len * 2, 0, 1); /* translate string into hex back to front to reuse the same buffer */ while (len > 0) { @@ -5671,10 +5669,10 @@ uc_hexdec(uc_vm_t *vm, size_t nargs) return NULL; buf = ucv_stringbuf_new(); - off = printbuf_length(buf); + off = uc_stringbuf_length(buf); /* preallocate the output buffer */ - printbuf_memset(buf, off, 0, n / 2 + 1); + uc_stringbuf_memset(buf, off, 0, n / 2 + 1); for (i = 0, n = 0; i < len; i++) { if (!isxdigit(p[i])) @@ -6096,8 +6094,8 @@ uc_callfunc(uc_vm_t *vm, size_t nargs) else if (scope) { fn_scope = ucv_object_new(vm); - ucv_object_foreach(scope, k, v) - ucv_object_add(fn_scope, k, ucv_get(v)); + ucv_object_foreach_entry(scope, e) + ucv_object_insert_string(fn_scope, e->key, ucv_get(e->value)); ucv_prototype_set(fn_scope, ucv_get(uc_vm_scope_get(vm))); } diff --git a/lib/debug.c b/lib/debug.c index 89b32377..3687e8f3 100644 --- a/lib/debug.c +++ b/lib/debug.c @@ -57,6 +57,7 @@ */ #include +#include #include #include #include @@ -90,13 +91,13 @@ extern bool debug_remote_has_active_connection(void); #include "ucode/compiler.h" #include "ucode/vm.h" -/* Append a NUL-terminated string; like printbuf_strappend() but for - * non-literal strings (printbuf_memappend_fast() compares its size +/* Append a NUL-terminated string; like uc_stringbuf_append() but for + * non-literal strings (uc_stringbuf_add() compares its size * argument against the buffer's int fields, hence the cast). */ static void printbuf_strcat(uc_stringbuf_t *sb, const char *s) { - printbuf_memappend_fast(sb, s, (int)strlen(s)); + uc_stringbuf_add(sb, s, (int)strlen(s)); } static char *memdump_signal = "USR2"; @@ -105,7 +106,7 @@ static char *memdump_directory = "/tmp"; struct memdump_walk_ctx { FILE *out; uc_closure_t *current_closure; - struct lh_table *seen; + uc_set_t seen; }; static uc_callframe_t * @@ -127,10 +128,10 @@ debuginfo_stackslot_to_callframe(uc_vm_t *vm, size_t slot) } static void -uc_debug_discover_ucv(uc_value_t *uv, struct lh_table *seen); +uc_debug_discover_ucv(uc_value_t *uv, uc_set_t *seen); static void -uc_debug_discover_ucv(uc_value_t *uv, struct lh_table *seen) +uc_debug_discover_ucv(uc_value_t *uv, uc_set_t *seen) { uc_function_t *function; uc_closure_t *closure; @@ -139,20 +140,14 @@ uc_debug_discover_ucv(uc_value_t *uv, struct lh_table *seen) uc_array_t *array; uc_resource_type_t *restype; uc_program_t *program; - struct lh_entry *entry; - unsigned long hash; size_t i; - hash = lh_get_hash(seen, uv); - if (ucv_is_scalar(uv)) return; - if (lh_table_lookup_entry_w_hash(seen, uv, hash)) + if (!uc_set_add(seen, uv)) return; - lh_table_insert_w_hash(seen, uv, NULL, hash, 0); - switch (ucv_type(uv)) { case UC_ARRAY: array = ucv_as_array(uv); @@ -169,8 +164,8 @@ uc_debug_discover_ucv(uc_value_t *uv, struct lh_table *seen) uc_debug_discover_ucv(object->proto, seen); - lh_foreach(object->table, entry) - uc_debug_discover_ucv((uc_value_t *)lh_entry_v(entry), seen); + for (i = 0; i < object->used; i++) + uc_debug_discover_ucv(object->entries[i].value, seen); break; @@ -216,11 +211,11 @@ uc_debug_discover_ucv(uc_value_t *uv, struct lh_table *seen) } static void -print_value(FILE *out, size_t pad, struct lh_table *seen, +print_value(FILE *out, size_t pad, uc_set_t *seen, uc_vm_t *vm, uc_value_t *uv); static void -print_value(FILE *out, size_t pad, struct lh_table *seen, +print_value(FILE *out, size_t pad, uc_set_t *seen, uc_vm_t *vm, uc_value_t *uv) { uc_resource_type_t *restype; @@ -241,7 +236,7 @@ print_value(FILE *out, size_t pad, struct lh_table *seen, if (!ucv_is_scalar(uv)) fprintf(out, "; %" PRIu32 " refs", uv->refcount); - if (!lh_table_lookup_entry(seen, uv)) + if (!uc_set_has(seen, uv)) fprintf(out, "; unreachable"); if (ucv_is_constant(uv)) @@ -406,13 +401,6 @@ print_memdump(uc_vm_t *vm, FILE *out) char *s; ctx.out = out; - ctx.seen = lh_kptr_table_new(16, NULL); - - if (!ctx.seen) { - fprintf(stderr, "Unable to allocate lookup table: %m\n"); - - return; - } fprintf(ctx.out, "STACK\n"); @@ -471,7 +459,7 @@ print_memdump(uc_vm_t *vm, FILE *out) fprintf(ctx.out, "\n"); - uc_debug_discover_ucv(vm->stack.entries[i], ctx.seen); + uc_debug_discover_ucv(vm->stack.entries[i], &ctx.seen); s = ucv_to_string(NULL, vm->stack.entries[i]); fprintf(ctx.out, " #value = %s\n", s); @@ -488,13 +476,13 @@ print_memdump(uc_vm_t *vm, FILE *out) fprintf(ctx.out, "\n"); uc_debug_discover_ucv(vm->callframes.entries[i].ctx, - ctx.seen); + &ctx.seen); uc_debug_discover_ucv(&vm->callframes.entries[i].closure->header, - ctx.seen); + &ctx.seen); uc_debug_discover_ucv(&vm->callframes.entries[i].cfunction->header, - ctx.seen); + &ctx.seen); s = ucv_to_string(NULL, vm->callframes.entries[i].ctx); fprintf(ctx.out, " #context = %s\n", s); @@ -519,7 +507,7 @@ print_memdump(uc_vm_t *vm, FILE *out) fprintf(ctx.out, "---\n\n"); fprintf(ctx.out, "GLOBALS\n"); - uc_debug_discover_ucv(vm->globals, ctx.seen); + uc_debug_discover_ucv(vm->globals, &ctx.seen); i = 0; ucv_object_foreach(vm->globals, gk, gv) { s = ucv_to_string(NULL, gv); @@ -530,7 +518,7 @@ print_memdump(uc_vm_t *vm, FILE *out) fprintf(ctx.out, "---\n\n"); fprintf(ctx.out, "REGISTRY\n"); - uc_debug_discover_ucv(vm->registry, ctx.seen); + uc_debug_discover_ucv(vm->registry, &ctx.seen); i = 0; ucv_object_foreach(vm->registry, rk, rv) { s = ucv_to_string(NULL, rv); @@ -541,7 +529,7 @@ print_memdump(uc_vm_t *vm, FILE *out) fprintf(ctx.out, "---\n\n"); fprintf(ctx.out, "EXCEPTION\n"); - uc_debug_discover_ucv(vm->exception.stacktrace, ctx.seen); + uc_debug_discover_ucv(vm->exception.stacktrace, &ctx.seen); s = ucv_to_string(NULL, vm->exception.stacktrace); fprintf(ctx.out, "%s\n", s); free(s); @@ -553,7 +541,7 @@ print_memdump(uc_vm_t *vm, FILE *out) fprintf(ctx.out, "[%zu] %s\n", i, vm->restypes.entries[i]->name); - uc_debug_discover_ucv(vm->restypes.entries[i]->proto, ctx.seen); + uc_debug_discover_ucv(vm->restypes.entries[i]->proto, &ctx.seen); s = ucv_to_string(NULL, vm->restypes.entries[i]->proto); fprintf(ctx.out, " #prototype = %s\n", s); @@ -571,10 +559,10 @@ print_memdump(uc_vm_t *vm, FILE *out) uv = (uc_value_t *)((uintptr_t)ref - offsetof(uc_array_t, ref)); fprintf(ctx.out, "[%zu] ", i); - print_value(ctx.out, 0, ctx.seen, vm, uv); + print_value(ctx.out, 0, &ctx.seen, vm, uv); } - lh_table_free(ctx.seen); + uc_set_free(&ctx.seen); } static uc_value_t * @@ -1412,10 +1400,9 @@ uc_getinfo(uc_vm_t *vm, size_t nargs) uvobj = ucv_as_object(uv); ucv_object_add(rv, "address", - ucv_uint64_new((uintptr_t)uvobj->table)); + ucv_uint64_new((uintptr_t)uvobj->entries)); - ucv_object_add(rv, "count", - ucv_uint64_new(lh_table_length(uvobj->table))); + ucv_object_add(rv, "count", ucv_uint64_new(uvobj->count)); ucv_object_add(rv, "constant", ucv_boolean_new(uv->ext_flag)); ucv_object_add(rv, "prototype", ucv_get(uvobj->proto)); @@ -2076,7 +2063,7 @@ printbuf_truncate(uc_stringbuf_t *sb, size_t off, size_t maxcols, bool tail) /* Reserve enough additional space for ellipsis mb sequence. */ if (trunclen < elliplen) - printbuf_memset(sb, -1, ' ', elliplen - trunclen); + uc_stringbuf_memset(sb, -1, ' ', elliplen - trunclen); memmove(sb->buf + off + elliplen, s, keeplen); memcpy(sb->buf + off, "…", elliplen); @@ -2095,7 +2082,7 @@ printbuf_truncate(uc_stringbuf_t *sb, size_t off, size_t maxcols, bool tail) } sb->bpos = s - sb->buf; - printbuf_strappend(sb, "…"); + uc_stringbuf_append(sb, "…"); return maxcols; } @@ -2152,7 +2139,7 @@ printbuf_append_funcname(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, if (v == val) { printbuf_strcat(sb, rt->name); - printbuf_strappend(sb, "#"); + uc_stringbuf_append(sb, "#"); goto name; } } @@ -2166,7 +2153,7 @@ printbuf_append_funcname(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, if (symval == val) { printbuf_strcat(sb, modname); - printbuf_strappend(sb, "."); + uc_stringbuf_append(sb, "."); goto name; } } @@ -2211,7 +2198,7 @@ printbuf_append_funcname(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, ucv_object_foreach(&obj->header, k, v) { if (v == val) { printbuf_strcat(sb, k); - printbuf_strappend(sb, ":"); + uc_stringbuf_append(sb, ":"); break; } } @@ -2232,7 +2219,7 @@ printbuf_append_function(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, if (t == UC_CFUNCTION) { printbuf_append_funcname(sb, vm, val, SIZE_MAX); - printbuf_strappend(sb, "("); + uc_stringbuf_append(sb, "("); if (frame) { size_t prev_frame = vm->stack.count; @@ -2246,7 +2233,7 @@ printbuf_append_function(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, for (size_t j = 1; j < prev_frame - frame->stackframe; j++) { if (j > 1) - printbuf_strappend(sb, ", "); + uc_stringbuf_append(sb, ", "); uc_value_t *argval = (frame->stackframe + j < vm->stack.count) @@ -2257,7 +2244,7 @@ printbuf_append_function(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, } } - printbuf_strappend(sb, ")"); + uc_stringbuf_append(sb, ")"); } else if (t == UC_CLOSURE) { uc_closure_t *cl = ucv_as_closure(val); @@ -2265,12 +2252,12 @@ printbuf_append_function(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, if (cl->function->module) { char *s = filename_to_modulename(vm, source->filename); - sprintbuf(sb, "module(%s)", s ? s : ""); + uc_stringbuf_printf(sb, "module(%s)", s ? s : ""); free(s); } else { printbuf_append_funcname(sb, vm, val, SIZE_MAX); - printbuf_strappend(sb, "("); + uc_stringbuf_append(sb, "("); if (frame) { for (size_t i = 0; i < cl->function->nargs; i++) { @@ -2278,19 +2265,19 @@ printbuf_append_function(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, &cl->function->chunk, i, i + 1, false); if (i > 0) - printbuf_strappend(sb, ", "); + uc_stringbuf_append(sb, ", "); if (i + 1 == cl->function->nargs && cl->function->vararg) - printbuf_strappend(sb, "..."); + uc_stringbuf_append(sb, "..."); if (argname) { printbuf_strcat(sb, ucv_string_get(argname)); - printbuf_strappend(sb, "="); + uc_stringbuf_append(sb, "="); ucv_put(argname); } else { - sprintbuf(sb, "$%zu=", i + 1); + uc_stringbuf_printf(sb, "$%zu=", i + 1); } uc_value_t *argval = @@ -2302,7 +2289,7 @@ printbuf_append_function(uc_stringbuf_t *sb, uc_vm_t *vm, uc_value_t *val, } } - printbuf_strappend(sb, ")"); + uc_stringbuf_append(sb, ")"); } } @@ -2314,7 +2301,7 @@ printbuf_append_srcpath(uc_stringbuf_t *sb, uc_source_t *source, size_t maxcols) { int off = sb->bpos; - printbuf_memset(sb, off + PATH_MAX, 0, 1); + uc_stringbuf_memset(sb, off + PATH_MAX, 0, 1); if (realpath(source->filename, sb->buf + off)) { size_t pathlen = strlen(sb->buf + off); @@ -2609,7 +2596,7 @@ lookup_source(uc_vm_t *vm, location_t *loc) } } - printbuf_reset(&filename); + uc_stringbuf_reset(&filename); } } @@ -4301,11 +4288,10 @@ proto_cmd_variables(uc_vm_t *vm, debug_breakpoint_t *dbk, uc_value_t *payload, i static void proto_cmd_sources(uc_vm_t *vm, debug_breakpoint_t *dbk, uc_value_t *payload, int fd, bool *proceed) { - struct lh_table *sources = lh_kptr_table_new(16, NULL); uc_value_t *items = ucv_array_new(vm); - struct lh_entry *e; + uc_set_t sources = { 0 }; uc_weakref_t *ref; - size_t i = 0; + size_t i; for (ref = vm->values.next; ref != &vm->values; ref = ref->next) { uc_closure_t *uc = @@ -4317,25 +4303,20 @@ proto_cmd_sources(uc_vm_t *vm, debug_breakpoint_t *dbk, uc_value_t *payload, int if (!uc->function || !uc->function->program) continue; - for (size_t j = 0; j < uc->function->program->sources.count; j++) { - uc_source_t *source = uc->function->program->sources.entries[j]; - unsigned long hash = lh_get_hash(sources, source); - - if (!lh_table_lookup_entry_w_hash(sources, source, hash)) - lh_table_insert_w_hash(sources, source, NULL, hash, 0); - } + for (size_t j = 0; j < uc->function->program->sources.count; j++) + uc_set_add(&sources, uc->function->program->sources.entries[j]); } - lh_foreach(sources, e) { - uc_source_t *source = lh_entry_k(e); + for (i = 0; i < sources.count; i++) { + uc_source_t *source = sources.entries[i]; uc_value_t *item = ucv_object_new(vm); - ucv_object_add(item, "index", ucv_uint64_new(i++)); + ucv_object_add(item, "index", ucv_uint64_new(i)); ucv_object_add(item, "file", ucv_string_new(source->filename)); ucv_array_push(items, item); } - lh_table_free(sources); + uc_set_free(&sources); uc_value_t *obj = ucv_object_new(vm); ucv_object_add(obj, "items", items); diff --git a/lib/debug_proto.c b/lib/debug_proto.c index cf159499..3765e95e 100644 --- a/lib/debug_proto.c +++ b/lib/debug_proto.c @@ -20,17 +20,18 @@ #include #include "ucode/util.h" +#include "ucode/json.h" #include "debug_proto.h" #define DEBUG_PROTO_READ_CHUNK 1024 -/* Append a NUL-terminated string; like printbuf_strappend() but for - * non-literal strings (printbuf_memappend_fast() compares its size +/* Append a NUL-terminated string; like uc_stringbuf_append() but for + * non-literal strings (uc_stringbuf_add() compares its size * argument against the buffer's int fields, hence the cast). */ static void printbuf_strcat(uc_stringbuf_t *sb, const char *s) { - printbuf_memappend_fast(sb, s, (int)strlen(s)); + uc_stringbuf_add(sb, s, (int)strlen(s)); } void @@ -53,7 +54,7 @@ debug_proto_buf_free(debug_proto_buf_t *buf) void debug_proto_write(int fd, uc_vm_t *vm, const char *verb, uc_value_t *payload) { - uc_stringbuf_t *sb = xprintbuf_new(); + uc_stringbuf_t *sb = uc_stringbuf_new(); const char *p; size_t remaining; ssize_t n; @@ -65,16 +66,16 @@ debug_proto_write(int fd, uc_vm_t *vm, const char *verb, uc_value_t *payload) json = ucv_to_jsonstring(vm, payload); if (json) { - printbuf_memappend_fast(sb, " ", 1); + uc_stringbuf_add(sb, " ", 1); printbuf_strcat(sb, json); free(json); } } - printbuf_memappend_fast(sb, "\n", 1); + uc_stringbuf_add(sb, "\n", 1); p = sb->buf; - remaining = (size_t)printbuf_length(sb); + remaining = (size_t)uc_stringbuf_length(sb); while (remaining > 0) { n = write(fd, p, remaining); @@ -90,7 +91,7 @@ debug_proto_write(int fd, uc_vm_t *vm, const char *verb, uc_value_t *payload) remaining -= (size_t)n; } - printbuf_free(sb); + uc_stringbuf_free(sb); } /* Append `len` bytes to the tail of buf, growing its backing storage as @@ -137,8 +138,8 @@ debug_proto_read(int fd, debug_proto_buf_t *buf, uc_vm_t *vm, { char chunk[DEBUG_PROTO_READ_CHUNK]; char *line, *sp, *verb; - struct json_tokener *tok; - json_object *jso; + uc_json_parser_t *parser; + uc_value_t *val; size_t line_len, verb_len, json_len; ssize_t n; @@ -198,26 +199,24 @@ debug_proto_read(int fd, debug_proto_buf_t *buf, uc_vm_t *vm, json_len = sp ? line_len - verb_len - 1 : 0; if (json_len > 0) { - tok = xjs_new_tokener(); + parser = uc_json_parser_new(vm); - /* len + 1 to include the trailing NUL: works around json-c - * treating a lone atomic value (e.g. `true`) as incomplete - * without a following delimiter, see json-c issue #681. */ - jso = json_tokener_parse_ex(tok, sp + 1, (int)json_len + 1); + /* len + 1 to include the trailing NUL, which ends the input, so + * that a lone atomic value (e.g. `true`) completes */ + val = uc_json_parse(parser, sp + 1, json_len + 1); - if (json_tokener_get_error(tok) != json_tokener_success) { - json_tokener_free(tok); - json_object_put(jso); + if (uc_json_parser_error(parser) != UC_JSON_SUCCESS) { + uc_json_parser_free(parser); + ucv_put(val); free(verb); free(line); return -2; } - *payload_out = ucv_from_json(vm, jso); + *payload_out = val; - json_tokener_free(tok); - json_object_put(jso); + uc_json_parser_free(parser); } free(line); diff --git a/lib/ffi/uc_cparse.c b/lib/ffi/uc_cparse.c index 1f47ebb6..7009a527 100644 --- a/lib/ffi/uc_cparse.c +++ b/lib/ffi/uc_cparse.c @@ -151,7 +151,7 @@ static UC_NOINLINE CPChar cp_get_bs(CPState *cp) /* Save character in buffer. */ static UC_AINLINE void cp_save(CPState *cp, CPChar c) { - sprintbuf(&cp->pb, "%c", c); + uc_stringbuf_printf(&cp->pb, "%c", c); } /* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ @@ -489,7 +489,7 @@ static CPToken cp_string(CPState *cp) } else { - if (printbuf_length(&cp->pb) != 1) + if (uc_stringbuf_length(&cp->pb) != 1) cp_err_token(cp, '\''); cp->val.i32 = (int32_t)(char)*cp->pb.buf; cp->val.id = CTID_INT32; @@ -530,7 +530,7 @@ static CPToken cp_next_(CPState *cp) { //lj_buf_reset(&cp->sb); if (cp->pb.buf) - printbuf_reset(&cp->pb); + uc_stringbuf_reset(&cp->pb); for (;;) { diff --git a/lib/ffi/uc_cparse.h b/lib/ffi/uc_cparse.h index 1e722c5b..e9664c08 100644 --- a/lib/ffi/uc_cparse.h +++ b/lib/ffi/uc_cparse.h @@ -62,7 +62,7 @@ typedef struct CPState { uint8_t packstack[CPARSE_MAX_PACKSTACK]; /* Stack for pack pragmas. */ uint8_t curpack; /* Current position in pack pragma stack. */ uc_vm_t *uv_vm; - struct printbuf pb; + uc_stringbuf_t pb; uc_value_t *uv_str; uc_value_t **uv_param; char *error; diff --git a/lib/ffi/uc_ctype.c b/lib/ffi/uc_ctype.c index 06e581f0..72487f9d 100644 --- a/lib/ffi/uc_ctype.c +++ b/lib/ffi/uc_ctype.c @@ -191,7 +191,7 @@ ct_hashname(uc_value_t *uv) while (len > 0) { - h = h * 129 + (*u8++) + LH_PRIME; + h = h * 129 + (*u8++) + 0x9e370001UL; len--; } diff --git a/lib/fs.c b/lib/fs.c index 2cc88a5c..9067227d 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -3053,15 +3053,15 @@ uc_fs_readfile(uc_vm_t *vm, size_t nargs) if (limit > 0 && blen > (size_t)limit) blen = (size_t)limit; - if (printbuf_length(buf) > INT_MAX - (ssize_t)blen) { + if (uc_stringbuf_length(buf) > INT_MAX - (ssize_t)blen) { fclose(fp); - printbuf_free(buf); + uc_stringbuf_free(buf); err_return(EFBIG); } - if (printbuf_memset(buf, printbuf_length(buf) + blen - 1, 0, 1)) { + if (uc_stringbuf_memset(buf, uc_stringbuf_length(buf) + blen - 1, 0, 1)) { fclose(fp); - printbuf_free(buf); + uc_stringbuf_free(buf); err_return(ENOMEM); } @@ -3078,14 +3078,14 @@ uc_fs_readfile(uc_vm_t *vm, size_t nargs) if (ferror(fp)) { fclose(fp); - printbuf_free(buf); + uc_stringbuf_free(buf); err_return(errno); } fclose(fp); /* add sentinel null byte but don't count it towards the string length */ - printbuf_memappend_fast(buf, "\0", 1); + uc_stringbuf_add(buf, "\0", 1); res = ucv_stringbuf_finish(buf); ((uc_string_t *)res)->length--; @@ -3167,18 +3167,18 @@ uc_fs_writefile(uc_vm_t *vm, size_t nargs) err_return(errno); if (data && ucv_type(data) != UC_STRING) { - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); ucv_to_stringbuf_formatted(vm, buf, data, 0, '\0', 0); - if (limit < 0 || limit > printbuf_length(buf)) - limit = printbuf_length(buf); + if (limit < 0 || limit > uc_stringbuf_length(buf)) + limit = uc_stringbuf_length(buf); wlen = fwrite(buf->buf, 1, limit, fp); if (wlen < (size_t)limit) err = errno; - printbuf_free(buf); + uc_stringbuf_free(buf); } else if (data) { if (limit < 0 || (size_t)limit > ucv_string_length(data)) diff --git a/lib/resolv.c b/lib/resolv.c index cfbe959f..43873980 100644 --- a/lib/resolv.c +++ b/lib/resolv.c @@ -527,7 +527,7 @@ parse_reply(uc_vm_t *vm, uc_value_t *res_obj, const unsigned char *msg, size_t l if (n > rdlen) { set_error(EBADMSG, "TXT string exceeds record length"); - printbuf_free(buf); + uc_stringbuf_free(buf); return -1; } diff --git a/lib/socket.c b/lib/socket.c index 16eae444..fe27e859 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -247,7 +247,7 @@ args_get_(uc_vm_t *vm, size_t nargs, int *fdptr, ...) static void strbuf_free(uc_stringbuf_t *sb) { - printbuf_free(sb); + uc_stringbuf_free(sb); } static bool @@ -257,7 +257,7 @@ strbuf_grow(uc_stringbuf_t *sb, size_t size) if (size > (size_t)INT_MAX - sizeof(uc_string_t)) err_return(EOVERFLOW, "Requested size too large"); - if (printbuf_memset(sb, sizeof(uc_string_t) + size - 1, '\0', 1)) + if (uc_stringbuf_memset(sb, sizeof(uc_string_t) + size - 1, '\0', 1)) err_return(ENOMEM, "Out of memory"); } @@ -307,7 +307,7 @@ strbuf_alloc(size_t size) uc_stringbuf_t *sb = ucv_stringbuf_new(); if (!strbuf_grow(sb, size)) { - printbuf_free(sb); + uc_stringbuf_free(sb); return NULL; } @@ -4260,7 +4260,7 @@ uc_socket_inst_sendmsg(uc_vm_t *vm, size_t nargs) msg.msg_iov[i].iov_len = ucv_string_length(item); } else if (item) { - struct printbuf *pb = xprintbuf_new(); + uc_stringbuf_t *pb = uc_stringbuf_new(); uc_vector_push(&sbarr, pb); ucv_to_stringbuf(vm, pb, item, false); msg.msg_iov[i].iov_base = pb->buf; @@ -4275,7 +4275,7 @@ uc_socket_inst_sendmsg(uc_vm_t *vm, size_t nargs) vec.iov_len = ucv_string_length(data); } else if (data) { - struct printbuf *pb = xprintbuf_new(); + uc_stringbuf_t *pb = uc_stringbuf_new(); uc_vector_push(&sbarr, pb); ucv_to_stringbuf(vm, pb, data, false); msg.msg_iovlen = 1; @@ -4296,7 +4296,7 @@ uc_socket_inst_sendmsg(uc_vm_t *vm, size_t nargs) } while (ret == -1 && errno == EINTR); while (sbarr.count > 0) - printbuf_free(sbarr.entries[--sbarr.count]); + uc_stringbuf_free(sbarr.entries[--sbarr.count]); uc_vector_clear(&sbarr); diff --git a/lib/uloop.c b/lib/uloop.c index 942d4550..0b862f6e 100644 --- a/lib/uloop.c +++ b/lib/uloop.c @@ -73,6 +73,7 @@ #include #include "ucode/module.h" +#include "ucode/json.h" #include "ucode/internal/platform.h" #define ok_return(expr) do { last_error = 0; return (expr); } while(0) @@ -1220,7 +1221,7 @@ uc_uloop_process_envp(uc_vm_t *vm, uc_value_t *env_arg) return NULL; ucv_object_foreach(env_arg, envk, envv) { - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); ucv_stringbuf_printf(buf, "%s=", envk); ucv_to_stringbuf(vm, buf, envv, false); @@ -1455,17 +1456,17 @@ uc_uloop_pipe_send_common(uc_vm_t *vm, uc_value_t *msg, int fd) size_t len; bool rv; - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); - printbuf_memset(buf, 0, 0, sizeof(len)); + uc_stringbuf_memset(buf, 0, 0, sizeof(len)); ucv_to_stringbuf(vm, buf, msg, true); - len = printbuf_length(buf); + len = uc_stringbuf_length(buf); memcpy(buf->buf, &len, sizeof(len)); rv = writeall(fd, buf->buf, len); - printbuf_free(buf); + uc_stringbuf_free(buf); if (!rv) err_return(errno); @@ -1516,57 +1517,56 @@ uc_uloop_pipe_send(uc_vm_t *vm, size_t nargs) } typedef struct { - json_tokener *tok; - json_object *jso; - enum json_tokener_error err; + uc_json_parser_t *json; + uc_value_t *val; + uc_json_error_t err; } uc_uloop_pipe_parser_t; static void -uc_uloop_pipe_parser_init(uc_uloop_pipe_parser_t *parser) +uc_uloop_pipe_parser_init(uc_uloop_pipe_parser_t *parser, uc_vm_t *vm) { - parser->tok = xjs_new_tokener(); - parser->jso = NULL; - parser->err = json_tokener_continue; + parser->json = uc_json_parser_new(vm); + parser->val = NULL; + parser->err = UC_JSON_CONTINUE; } static void uc_uloop_pipe_parser_feed(uc_uloop_pipe_parser_t *parser, const char *buf, size_t len) { - if (!parser->tok || parser->err != json_tokener_continue) + if (!parser->json || parser->err != UC_JSON_CONTINUE) return; - parser->jso = json_tokener_parse_ex(parser->tok, buf, len); - parser->err = json_tokener_get_error(parser->tok); + parser->val = uc_json_parse(parser->json, buf, len); + parser->err = uc_json_parser_error(parser->json); } static void uc_uloop_pipe_parser_free(uc_uloop_pipe_parser_t *parser) { - if (parser->tok) - json_tokener_free(parser->tok); - - json_object_put(parser->jso); - parser->tok = NULL; - parser->jso = NULL; + uc_json_parser_free(parser->json); + ucv_put(parser->val); + parser->json = NULL; + parser->val = NULL; } static bool -uc_uloop_pipe_parser_finish(uc_vm_t *vm, uc_uloop_pipe_parser_t *parser, - uc_value_t **res) +uc_uloop_pipe_parser_finish(uc_uloop_pipe_parser_t *parser, uc_value_t **res) { bool rv; *res = NULL; - if (!parser->tok) + if (!parser->json) return false; uc_uloop_pipe_parser_feed(parser, "\0", 1); - rv = (parser->err == json_tokener_success); + rv = (parser->err == UC_JSON_SUCCESS); - if (rv) - *res = ucv_from_json(vm, parser->jso); + if (rv) { + *res = parser->val; + parser->val = NULL; + } uc_uloop_pipe_parser_free(parser); @@ -1596,7 +1596,7 @@ uc_uloop_pipe_receive_common(uc_vm_t *vm, int fd, uc_value_t **res) err_return(EINVAL); len -= sizeof(len); - uc_uloop_pipe_parser_init(&parser); + uc_uloop_pipe_parser_init(&parser, vm); while (len > 0) { rlen = read(fd, buf, len < sizeof(buf) ? len : sizeof(buf)); @@ -1614,7 +1614,7 @@ uc_uloop_pipe_receive_common(uc_vm_t *vm, int fd, uc_value_t **res) len -= rlen; } - if (!uc_uloop_pipe_parser_finish(vm, &parser, res)) + if (!uc_uloop_pipe_parser_finish(&parser, res)) err_return(EINVAL); return true; @@ -1970,7 +1970,7 @@ uc_uloop_task_header(uc_uloop_task_t *task) task->rx.left = len - sizeof(len); if (task->output_cb) - uc_uloop_pipe_parser_init(&task->rx.parser); + uc_uloop_pipe_parser_init(&task->rx.parser, task->cb.vm); return true; } @@ -1984,7 +1984,7 @@ uc_uloop_task_payload(uc_uloop_task_t *task, const char *buf, size_t len) task->rx.left -= len; if (task->rx.left == 0 && - uc_uloop_pipe_parser_finish(task->cb.vm, &task->rx.parser, &msg)) + uc_uloop_pipe_parser_finish(&task->rx.parser, &msg)) uc_uloop_task_output(task, msg); } diff --git a/lib/zlib.c b/lib/zlib.c index f7e66a4a..59a82f5b 100644 --- a/lib/zlib.c +++ b/lib/zlib.c @@ -92,7 +92,7 @@ def_chunks(zstrm_t * const zstrm) /* run deflate() on input until output buffer not full */ do { - if (printbuf_memset(zstrm->outbuf, -1, 0, CHUNK)) + if (uc_stringbuf_memset(zstrm->outbuf, -1, 0, CHUNK)) return Z_MEM_ERROR; zstrm->outbuf->bpos -= CHUNK; @@ -274,14 +274,14 @@ uc_zlib_deflate(uc_vm_t * const vm, const size_t nargs) default: uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Passed value is neither a string nor an object"); - printbuf_free(zstrm.outbuf); + uc_stringbuf_free(zstrm.outbuf); goto out; } if (!success) { if (vm->exception.type == EXCEPTION_NONE) // do not clobber previous exception uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Zlib error: %s", zstrm.strm.msg); - printbuf_free(zstrm.outbuf); + uc_stringbuf_free(zstrm.outbuf); goto out; } @@ -299,7 +299,7 @@ inf_chunks(zstrm_t * const zstrm) /* run inflate() on input until output buffer not full */ do { - if (printbuf_memset(zstrm->outbuf, -1, 0, CHUNK)) + if (uc_stringbuf_memset(zstrm->outbuf, -1, 0, CHUNK)) return Z_MEM_ERROR; zstrm->outbuf->bpos -= CHUNK; @@ -475,14 +475,14 @@ uc_zlib_inflate(uc_vm_t * const vm, const size_t nargs) default: uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Passed value is neither a string nor an object"); - printbuf_free(zstrm.outbuf); + uc_stringbuf_free(zstrm.outbuf); goto out; } if (!success) { if (vm->exception.type == EXCEPTION_NONE) // do not clobber previous exception uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Zlib error: %s", zstrm.strm.msg); - printbuf_free(zstrm.outbuf); + uc_stringbuf_free(zstrm.outbuf); goto out; } @@ -925,7 +925,7 @@ static void destroy_zstrmd(void *z) if (zstrm) { (void)deflateEnd(&zstrm->strm); - printbuf_free(zstrm->outbuf); + uc_stringbuf_free(zstrm->outbuf); free(zstrm); } } @@ -936,7 +936,7 @@ static void destroy_zstrmi(void *z) if (zstrm) { (void)inflateEnd(&zstrm->strm); - printbuf_free(zstrm->outbuf); + uc_stringbuf_free(zstrm->outbuf); free(zstrm); } } diff --git a/main.c b/main.c index baeb8b5f..d5cbfd42 100644 --- a/main.c +++ b/main.c @@ -290,11 +290,11 @@ compile(uc_vm_t *vm, uc_source_t *src, FILE *precompile, const compile_opts_t *o fwrite(ucv_string_get(res), ucv_string_length(res), 1, stdout); } else { - uc_stringbuf_t *pb = xprintbuf_new(); + uc_stringbuf_t *pb = uc_stringbuf_new(); ucv_to_stringbuf_formatted(vm, pb, res, 0, '\t', 1); fwrite(pb->buf, pb->bpos, 1, stdout); - printbuf_free(pb); + uc_stringbuf_free(pb); } } diff --git a/program.c b/program.c index c949c372..747d7c07 100644 --- a/program.c +++ b/program.c @@ -717,8 +717,8 @@ read_exports(uc_source_t *in, uc_source_t *source, uint32_t flags, const char *s buf = ucv_stringbuf_new(); - if (printbuf_memset(buf, -1, 0, len + 1)) { - printbuf_free(buf); + if (uc_stringbuf_memset(buf, -1, 0, len + 1)) { + uc_stringbuf_free(buf); if (errp) xasprintf(errp, "Out of memory reading %s\n", subjbuf); @@ -728,7 +728,7 @@ read_exports(uc_source_t *in, uc_source_t *source, uint32_t flags, const char *s if (!read_string(in, buf->buf + buf->bpos - len - 1, len, subjbuf, errp)) { - printbuf_free(buf); + uc_stringbuf_free(buf); return false; } diff --git a/tests/custom/06_metamethods/90_own_key_access b/tests/custom/06_metamethods/90_own_key_access new file mode 100644 index 00000000..d1eb8e06 --- /dev/null +++ b/tests/custom/06_metamethods/90_own_key_access @@ -0,0 +1,79 @@ +Reading and writing the own key of an object takes a shorter path than +other property accesses, which must give the same results: own keys +shadow the prototype, __get__ only runs for missing keys, and objects +with many keys find their keys by hash. + +-- Testcase -- +{% + let base = { a: "proto-a", b: "proto-b" }; + let o = proto({ a: "own-a" }, base); + print(o.a, " ", o.b, "\n"); + + let m = proto({ a: 1 }, { __get__: function(k) { return "meta-" + k; } }); + print(m.a, " ", m.missing, "\n"); + + let big = {}; + for (let i = 0; i < 20; i++) + big["k" + i] = i; + print(big.k7, " ", big.k19, " ", big.k20 ?? "none", "\n"); +%} +-- End -- + +-- Expect stdout -- +own-a proto-b +1 meta-missing +7 19 none +-- End -- + + +Assigning a property its own value keeps the value alive. + +-- Testcase -- +{% + let f = { a: [ 1, 2 ] }; + f.a = f.a; + f.s = "a string that is " + "not a constant"; + f.s = f.s; + print(f, "\n"); +%} +-- End -- + +-- Expect stdout -- +{ "a": [ 1, 2 ], "s": "a string that is not a constant" } +-- End -- + + +The values of a module namespace follow the exports, and the namespace +cannot be changed. + +-- Testcase -- +import * as ns from "./files/counter.uc"; + +ns.inc(); +ns.inc(); +print(ns.count, "\n"); + +try { + ns.count = 5; +} +catch (e) { + print(e.message, "\n"); +} + +print(ns.count, "\n"); +-- End -- + +-- File counter.uc -- +export let count = 0; +export function inc() { count++; }; +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +2 +object value is immutable +2 +-- End -- diff --git a/tests/custom/99_bugs/65_object_key_nul_bytes b/tests/custom/99_bugs/65_object_key_nul_bytes new file mode 100644 index 00000000..3f24d14f --- /dev/null +++ b/tests/custom/99_bugs/65_object_key_nul_bytes @@ -0,0 +1,50 @@ +Object keys were C strings, so a key with a NUL byte addressed the key +before the NUL: a script that copied keys from its input into an object +could replace other properties. + +-- Testcase -- +{% + let o = { admin: false }; + + o["admin\u0000x"] = true; + print(o.admin, " ", length(o), " ", exists(o, "admin\u0000x"), " ", "admin\u0000y" in o, "\n"); + + for (let k, v in o) + print(length(k), " ", v, "\n"); + + print(map(sort(keys(o)), length), " ", sprintf("%J", o), "\n"); + + delete o["admin\u0000x"]; + print(o, "\n"); + + let p = { ...{ "a\u0000b": 1 }, a: 2 }; + print(length(p), " ", p["a\u0000b"], " ", p.a, "\n"); + + print([ 10, 11 ]["1\u0000x"], "\n"); +%} +-- End -- + +-- Expect stdout -- +false 2 true false +5 false +7 true +[ 5, 7 ] { "admin": false, "admin\u0000x": true } +{ "admin": false } +2 1 2 + +-- End -- + + +The same applied to keys that json() parsed: "a\u0000b" replaced the +key "a". + +-- Testcase -- +{% + let v = json('{ "a\\u0000b": 1, "a": 2 }'); + print(length(v), " ", v["a\u0000b"], " ", v.a, "\n"); +%} +-- End -- + +-- Expect stdout -- +2 1 2 +-- End -- diff --git a/types.c b/types.c index 72a6ae0f..2f05e34c 100644 --- a/types.c +++ b/types.c @@ -19,14 +19,18 @@ #include #include #include - +#include +#include +#include +#include +#include +#include #include "ucode/internal/types.h" #include "ucode/internal/vm.h" /* ISA: I_* opcodes */ #include "ucode/internal/program.h" #include "ucode/internal/source.h" #include "ucode/internal/chunk.h" -#include "ucode/internal/json-c-compat.h" /* initial capacity step of array entry vectors; must be at least 2 */ #define UC_ARRAY_INIT_SIZE 2 @@ -181,7 +185,6 @@ ucv_gc_mark(uc_value_t *uv) uc_export_instance_t *instance; uc_program_t *program; uc_value_t **values; - struct lh_entry *entry; size_t i; if (ucv_is_gc_marked(uv)) @@ -209,8 +212,8 @@ ucv_gc_mark(uc_value_t *uv) ucv_gc_mark(object->proto); - lh_foreach(object->table, entry) - ucv_gc_mark((uc_value_t *)lh_entry_v(entry)); + for (i = 0; i < object->used; i++) + ucv_gc_mark(object->entries[i].value); break; @@ -334,7 +337,14 @@ ucv_free(uc_value_t *uv, bool retain) object = ucv_as_object(uv); ref = &object->ref; ucv_put_value(object->proto, retain); - lh_table_free(object->table); + + for (i = 0; i < object->used; i++) { + ucv_put(object->entries[i].key); + ucv_put(object->entries[i].value); + } + + free(object->entries); + free(object->index); break; case UC_REGEXP: @@ -511,10 +521,109 @@ ucv_string_new_length(const char *str, size_t length) return ustr; } +uc_stringbuf_t * +uc_stringbuf_new(void) +{ + uc_stringbuf_t *sb = xalloc(sizeof(*sb)); + + sb->size = 32; + sb->buf = xalloc(sb->size); + + return sb; +} + +void +uc_stringbuf_free(uc_stringbuf_t *sb) +{ + if (!sb) + return; + + free(sb->buf); + free(sb); +} + +/* Grows the buffer to hold at least `size` bytes; a zeroed buffer is an + * empty one without storage */ +void +uc_stringbuf_grow(uc_stringbuf_t *sb, size_t size) +{ + size_t next = sb->size ? sb->size : 32; + + if (size > INT_MAX) { + fprintf(stderr, "Out of memory\n"); + abort(); + } + + while (next < size) + next = (next > INT_MAX / 2) ? INT_MAX : next * 2; + + sb->buf = xrealloc(sb->buf, next); + sb->size = next; +} + +/* Fills `len` bytes from `offset`, or from the end if `offset` is -1, with + * `c`, and pads the bytes between the end and `offset` with NUL; returns 0, + * as the buffer aborts the program when it cannot grow */ +int +uc_stringbuf_memset(uc_stringbuf_t *sb, int offset, int c, size_t len) +{ + size_t off = (offset < 0) ? (size_t)sb->bpos : (size_t)offset; + + if (off + len + 1 > (size_t)sb->size) + uc_stringbuf_grow(sb, off + len + 1); + + if ((size_t)sb->bpos < off) + memset(sb->buf + sb->bpos, 0, off - sb->bpos); + + memset(sb->buf + off, c, len); + + if ((size_t)sb->bpos < off + len) + sb->bpos = off + len; + + sb->buf[sb->bpos] = 0; + + return 0; +} + +void +uc_stringbuf_reset(uc_stringbuf_t *sb) +{ + sb->bpos = 0; + + if (sb->buf) + sb->buf[0] = 0; +} + +int +uc_stringbuf_printf(uc_stringbuf_t *sb, const char *fmt, ...) +{ + size_t room = sb->size - sb->bpos; + va_list ap; + int len; + + va_start(ap, fmt); + len = vsnprintf(sb->buf + sb->bpos, room, fmt, ap); + va_end(ap); + + if (len < 0) + return -1; + + if ((size_t)len >= room) { + uc_stringbuf_grow(sb, (size_t)sb->bpos + len + 1); + va_start(ap, fmt); + vsnprintf(sb->buf + sb->bpos, len + 1, fmt, ap); + va_end(ap); + } + + sb->bpos += len; + + return len; +} + uc_stringbuf_t * ucv_stringbuf_new(void) { - uc_stringbuf_t *sb = xprintbuf_new(); + uc_stringbuf_t *sb = uc_stringbuf_new(); uc_string_t ustr = { .header = { .type = UC_STRING, @@ -522,7 +631,7 @@ ucv_stringbuf_new(void) } }; - printbuf_memappend_fast(sb, (char *)&ustr, (int)sizeof(ustr)); + uc_stringbuf_add(sb, (char *)&ustr, (int)sizeof(ustr)); return sb; } @@ -532,7 +641,7 @@ ucv_stringbuf_finish(uc_stringbuf_t *sb) { uc_string_t *ustr = (uc_string_t *)sb->buf; - ustr->length = printbuf_length(sb) - offsetof(uc_string_t, str); + ustr->length = uc_stringbuf_length(sb) - offsetof(uc_string_t, str); free(sb); @@ -996,190 +1105,353 @@ ucv_array_length(uc_value_t *uv) } -static void -ucv_free_object_entry(struct lh_entry *entry) +/* Objects keep their entries in insertion order, with the key as a string + * value, so that keys may contain NUL bytes and keys from constants or + * other strings are shared. Small objects are searched entry by entry; + * above UC_OBJECT_INDEX_MIN entries a hash index of open addressing finds + * them. A deleted entry stays as a gap until the array grows, so that the + * positions of iterations stay valid. */ + +#define UC_OBJECT_INDEX_MIN 8 + +static uint64_t ucv_object_hash_seed[2]; + +/* The hash is keyed with a random seed as the one of json-c, so that keys + * chosen to collide cannot be prepared in advance. */ +__attribute__((constructor)) static void +ucv_object_hash_init(void) { - uc_list_foreach(item, &uc_thread_context_get()->object_iterators) { - uc_object_iterator_t *iter = (uc_object_iterator_t *)item; + struct timespec ts; + ssize_t n = -1; + int fd; + + fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); - if (iter->u.pos == entry) - iter->u.pos = entry->next; + if (fd >= 0) { + n = read(fd, ucv_object_hash_seed, sizeof(ucv_object_hash_seed)); + close(fd); } - free(lh_entry_k(entry)); - ucv_put(lh_entry_v(entry)); + if (n == (ssize_t)sizeof(ucv_object_hash_seed)) + return; + + clock_gettime(CLOCK_MONOTONIC, &ts); + ucv_object_hash_seed[0] = (uint64_t)ts.tv_nsec * 0x9e3779b97f4a7c15ULL ^ (uint64_t)getpid(); + ucv_object_hash_seed[1] = (uint64_t)ts.tv_sec ^ (uint64_t)(uintptr_t)&ts; } -uc_value_t * -ucv_object_new(uc_vm_t *vm) +#define UCV_ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) + +static inline void +ucv_object_hash_round(uint64_t *v) { - return ucv_object_new_size(vm, 16); + v[0] += v[1]; v[1] = UCV_ROTL(v[1], 13); v[1] ^= v[0]; v[0] = UCV_ROTL(v[0], 32); + v[2] += v[3]; v[3] = UCV_ROTL(v[3], 16); v[3] ^= v[2]; + v[0] += v[3]; v[3] = UCV_ROTL(v[3], 21); v[3] ^= v[0]; + v[2] += v[1]; v[1] = UCV_ROTL(v[1], 17); v[1] ^= v[2]; v[2] = UCV_ROTL(v[2], 32); } -uc_value_t * -ucv_object_new_size(uc_vm_t *vm, size_t size) +/* SipHash-1-3 */ +static uint64_t +ucv_object_hash_bytes(const char *p, size_t len) { - struct lh_table *table; - uc_object_t *object; + uint64_t v[4] = { + 0x736f6d6570736575ULL ^ ucv_object_hash_seed[0], + 0x646f72616e646f6dULL ^ ucv_object_hash_seed[1], + 0x6c7967656e657261ULL ^ ucv_object_hash_seed[0], + 0x7465646279746573ULL ^ ucv_object_hash_seed[1] + }; + uint64_t m, b = (uint64_t)len << 56; + size_t i, tail = len & 7; + + for (i = 0; i + 8 <= len; i += 8) { + memcpy(&m, p + i, 8); + v[3] ^= m; + ucv_object_hash_round(v); + v[0] ^= m; + } - table = lh_kchar_table_new(size, ucv_free_object_entry); + while (tail--) + b |= (uint64_t)(uint8_t)p[i + tail] << (8 * tail); - if (!table) { - fprintf(stderr, "Out of memory\n"); - abort(); + v[3] ^= b; + ucv_object_hash_round(v); + v[0] ^= b; + v[2] ^= 0xff; + ucv_object_hash_round(v); + ucv_object_hash_round(v); + ucv_object_hash_round(v); + + return v[0] ^ v[1] ^ v[2] ^ v[3]; +} + +static const char * +ucv_object_key_get(uc_object_entry_t *e, size_t *len) +{ + *len = ucv_string_length(e->key); + + return _ucv_string_get(&e->key); +} + +/* Whether entry `e` has the key `s` of `len` bytes; `kv` is the string value + * of the key if the caller has one, which matches by pointer. */ +static bool +ucv_object_key_match(uc_object_entry_t *e, const char *s, size_t len, uc_value_t *kv) +{ + const char *k; + size_t klen; + + if (!e->key) + return false; + + if (e->key == kv) + return true; + + k = ucv_object_key_get(e, &klen); + + return klen == len && !memcmp(k, s, len); +} + +static uc_object_entry_t * +ucv_object_find(uc_object_t *object, const char *s, size_t len, uc_value_t *kv) +{ + uint32_t i, n; + + if (!object->index) { + for (n = 0; n < object->used; n++) + if (ucv_object_key_match(&object->entries[n], s, len, kv)) + return &object->entries[n]; + + return NULL; } - object = xalloc(sizeof(*object)); - object->header.type = UC_OBJECT; - object->header.refcount = 1; - object->table = table; + for (i = ucv_object_hash_bytes(s, len) & object->mask; + (n = object->index[i]) != 0; + i = (i + 1) & object->mask) + if (ucv_object_key_match(&object->entries[n - 1], s, len, kv)) + return &object->entries[n - 1]; - if (vm) { - ucv_ref(&vm->values, &object->ref); - vm->alloc_refs++; + return NULL; +} + +static void +ucv_object_index_add(uc_object_t *object, uint32_t n) +{ + size_t len; + const char *k = ucv_object_key_get(&object->entries[n], &len); + uint32_t i = ucv_object_hash_bytes(k, len) & object->mask; + + while (object->index[i]) + i = (i + 1) & object->mask; + + object->index[i] = n + 1; +} + +/* Builds the index for the capacity of the entry array, at most three + * quarters full when the array is. */ +static void +ucv_object_index_build(uc_object_t *object) +{ + uint32_t slots = 16; + + while (slots * 3 < object->size * 4) + slots *= 2; + + free(object->index); + object->index = xcalloc(slots, sizeof(*object->index)); + object->mask = slots - 1; + + for (uint32_t n = 0; n < object->used; n++) + if (object->entries[n].key) + ucv_object_index_add(object, n); +} + +/* Drops the gaps of deleted entries and moves the iterations of the object + * to the positions of their next entries. */ +static void +ucv_object_compact(uc_object_t *object) +{ + uint32_t from, to = 0; + + uc_list_foreach(item, &uc_thread_context_get()->object_iterators) { + uc_object_iterator_t *iter = (uc_object_iterator_t *)item; + size_t live = 0; + + if (iter->object != object) + continue; + + for (from = 0; from < iter->pos && from < object->used; from++) + live += (object->entries[from].key != NULL); + + iter->pos = live; } - return &object->header; + for (from = 0; from < object->used; from++) + if (object->entries[from].key) + object->entries[to++] = object->entries[from]; + + object->used = to; } -bool -ucv_object_add(uc_value_t *uv, const char *key, uc_value_t *val) +/* Makes room for one more entry: drops the gaps if they are a quarter of + * the entries, or else doubles the array. */ +static void +ucv_object_grow(uc_object_t *object) +{ + if (object->used < object->size) + return; + + if (object->used && (object->used - object->count) * 4 >= object->used) { + ucv_object_compact(object); + } + else { + object->size = object->size ? object->size * 2 : 4; + object->entries = xrealloc(object->entries, object->size * sizeof(*object->entries)); + } + + if (object->index) + ucv_object_index_build(object); +} + +/* Stores `val` under the key `s` of `len` bytes, whose string value `kv` the + * object shares if given; consumes `val` only on success. */ +static bool +ucv_object_store(uc_value_t *uv, const char *s, size_t len, uc_value_t *kv, uc_value_t *val) { uc_object_t *object = ucv_as_object(uv); - struct lh_entry *existing_entry; - uc_value_t *existing_value; - unsigned long hash; - void *k; + uc_object_entry_t *e; if (ucv_type(uv) != UC_OBJECT || uv->ext_flag) return false; - hash = lh_get_hash(object->table, (const void *)key); - existing_entry = lh_table_lookup_entry_w_hash(object->table, (const void *)key, hash); - - if (existing_entry == NULL) { - bool rehash = (object->table->count >= object->table->size * LH_LOAD_FACTOR); + e = ucv_object_find(object, s, len, kv); - /* insert will rehash table, backup affected iterator states */ - if (rehash) { - uc_list_foreach(item, &uc_thread_context_get()->object_iterators) { - uc_object_iterator_t *iter = (uc_object_iterator_t *)item; + if (e) { + ucv_put(e->value); + e->value = val; - if (iter->table != object->table) - continue; + return true; + } - if (iter->u.pos == NULL) - continue; + ucv_object_grow(object); - iter->u.kh.k = iter->u.pos->k; - iter->u.kh.hash = lh_get_hash(iter->table, iter->u.kh.k); - } - } + e = &object->entries[object->used]; + e->key = kv ? ucv_get(kv) : ucv_string_new_length(s, len); + e->value = val; - k = xstrdup(key); + if (object->index) + ucv_object_index_add(object, object->used); - if (lh_table_insert_w_hash(object->table, k, val, hash, 0) != 0) { - free(k); + object->used++; + object->count++; - return false; - } + if (!object->index && object->count > UC_OBJECT_INDEX_MIN) + ucv_object_index_build(object); - /* restore affected iterator state pointer after rehash */ - if (rehash) { - uc_list_foreach(item, &uc_thread_context_get()->object_iterators) { - uc_object_iterator_t *iter = (uc_object_iterator_t *)item; + return true; +} - if (iter->table != object->table) - continue; +uc_value_t * +ucv_object_new(uc_vm_t *vm) +{ + return ucv_object_new_size(vm, 0); +} - if (iter->u.kh.k == NULL) - continue; +uc_value_t * +ucv_object_new_size(uc_vm_t *vm, size_t size) +{ + uc_object_t *object = xalloc(sizeof(*object)); - iter->u.pos = lh_table_lookup_entry_w_hash(iter->table, - iter->u.kh.k, - iter->u.kh.hash); - } - } + object->header.type = UC_OBJECT; + object->header.refcount = 1; - return true; + if (size) { + object->size = size; + object->entries = xalloc(size * sizeof(*object->entries)); } - existing_value = (uc_value_t *)lh_entry_v(existing_entry); + if (vm) { + ucv_ref(&vm->values, &object->ref); + vm->alloc_refs++; + } - if (existing_value) - ucv_put(existing_value); + return &object->header; +} - existing_entry->v = val; +bool +ucv_object_insert(uc_value_t *uv, const char *key, size_t len, uc_value_t *val) +{ + return ucv_object_store(uv, key, len, NULL, val); +} - return true; +bool +ucv_object_insert_string(uc_value_t *uv, uc_value_t *key, uc_value_t *val) +{ + return ucv_object_store(uv, ucv_string_get(key), ucv_string_length(key), key, val); } +bool +ucv_object_add(uc_value_t *uv, const char *key, uc_value_t *val) +{ + return ucv_object_store(uv, key, strlen(key), NULL, val); +} typedef struct { int (*cmp)(const void *, const void *); int (*cmpr)(const char *, uc_value_t *, const char *, uc_value_t *, void *); + int (*cmpe)(const uc_object_entry_t *, const uc_object_entry_t *, void *); void *ud; } object_sort_ctx_t; -static uc_vector_sort_cb(ucv_object_sort_cb, const void *, object_sort_ctx_t *, { +static uc_vector_sort_cb(ucv_object_sort_cb, uc_object_entry_t, object_sort_ctx_t *, { + uc_object_entry_t *e1 = (uc_object_entry_t *)k1, *e2 = (uc_object_entry_t *)k2; + (void)v1; (void)v2; - return ctx->cmp(k1, k2); -}); + if (ctx->cmpe) + return ctx->cmpe(e1, e2, ctx->ud); -static uc_vector_sort_cb(ucv_object_sort_r_cb, const struct lh_entry *, object_sort_ctx_t *, { - return ctx->cmpr( - v1 ? lh_entry_k(v1) : NULL, v1 ? lh_entry_v(v1) : NULL, - v2 ? lh_entry_k(v2) : NULL, v2 ? lh_entry_v(v2) : NULL, - ctx->ud); + if (ctx->cmpr) + return ctx->cmpr(_ucv_string_get(&e1->key), e1->value, + _ucv_string_get(&e2->key), e2->value, ctx->ud); + + return ctx->cmp(e1, e2); }); static void ucv_object_sort_common(uc_value_t *uv, object_sort_ctx_t *ctx) { uc_object_t *object = ucv_as_object(uv); - struct lh_table *t; - struct lh_entry *e; - size_t i; - struct { - struct lh_entry **entries; size_t count; - } keys = { 0 }; + uc_object_entry_t *entries; + } entries; - if (ucv_type(uv) != UC_OBJECT || ucv_is_constant(uv) || - lh_table_length(object->table) <= 1) + if (ucv_type(uv) != UC_OBJECT || ucv_is_constant(uv) || object->count <= 1) return; - for (t = object->table, e = t->head; e; e = e->next) - uc_vector_push(&keys, e); + ucv_object_compact(object); + entries.count = object->used; + entries.entries = object->entries; - if (!keys.entries) - return; - - /* Keep the saved hash table entry pointers valid until relinking. */ + /* comparators must not change the object while its entries move */ ucv_set_constant(uv, true); - uc_vector_sort(&keys, - ctx->cmpr ? ucv_object_sort_r_cb : ucv_object_sort_cb, ctx); + uc_vector_sort(&entries, ucv_object_sort_cb, ctx); + ucv_set_constant(uv, false); - for (i = 0; i < keys.count; i++) { - e = keys.entries[i]; + if (object->index) + ucv_object_index_build(object); +} - if (i == 0) { - t->head = t->tail = e; - e->next = e->prev = NULL; - } - else { - t->tail->next = e; - e->prev = t->tail; - e->next = NULL; - t->tail = e; - } - } +void +ucv_object_sort_entries(uc_value_t *uv, + int (*cmp)(const uc_object_entry_t *, const uc_object_entry_t *, void *), + void *ud) +{ + object_sort_ctx_t ctx = { .cmpe = cmp, .ud = ud }; - uc_vector_clear(&keys); - ucv_set_constant(uv, false); + ucv_object_sort_common(uv, &ctx); } void @@ -1188,49 +1460,104 @@ ucv_object_sort_r(uc_value_t *uv, const char *, uc_value_t *, void *), void *ud) { - object_sort_ctx_t ctx = { .cmp = NULL, .cmpr = cmp, .ud = ud }; + object_sort_ctx_t ctx = { .cmpr = cmp, .ud = ud }; ucv_object_sort_common(uv, &ctx); } +/* `cmp` receives pointers to two uc_object_entry_t */ void ucv_object_sort(uc_value_t *uv, int (*cmp)(const void *, const void *)) { - object_sort_ctx_t ctx = { .cmp = cmp, .cmpr = NULL, .ud = NULL }; + object_sort_ctx_t ctx = { .cmp = cmp }; ucv_object_sort_common(uv, &ctx); } bool -ucv_object_delete(uc_value_t *uv, const char *key) +ucv_object_remove(uc_value_t *uv, const char *key, size_t len) { uc_object_t *object = ucv_as_object(uv); + uc_object_entry_t *e; if (ucv_type(uv) != UC_OBJECT || uv->ext_flag) return false; - return (lh_table_delete(object->table, key) == 0); + e = ucv_object_find(object, key, len, NULL); + + if (!e) + return false; + + ucv_put(e->key); + ucv_put(e->value); + e->key = NULL; + e->value = NULL; + object->count--; + + return true; +} + +bool +ucv_object_delete(uc_value_t *uv, const char *key) +{ + return ucv_object_remove(uv, key, strlen(key)); +} + +/* Looks up the key `s` of `len` bytes, with its string value `kv` if given */ +static uc_value_t * +ucv_object_lookup_key(uc_value_t *uv, const char *s, size_t len, uc_value_t *kv, bool *found) +{ + uc_object_entry_t *e = NULL; + + if (ucv_type(uv) == UC_OBJECT) + e = ucv_object_find(ucv_as_object(uv), s, len, kv); + + if (found != NULL) + *found = (e != NULL); + + return e ? e->value : NULL; +} + +uc_value_t * +ucv_object_lookup(uc_value_t *uv, const char *key, size_t len, bool *found) +{ + return ucv_object_lookup_key(uv, key, len, NULL, found); +} + +uc_value_t * +ucv_object_lookup_string(uc_value_t *uv, uc_value_t *key, bool *found) +{ + return ucv_object_lookup_key(uv, ucv_string_get(key), ucv_string_length(key), key, found); } uc_value_t * ucv_object_get(uc_value_t *uv, const char *key, bool *found) { - uc_object_t *object = ucv_as_object(uv); - uc_value_t *val = NULL; - bool rv; + return ucv_object_lookup(uv, key, strlen(key), found); +} - if (found != NULL) - *found = false; +bool +ucv_object_next(uc_value_t *uv, size_t *pos, char **key, uc_value_t **val) +{ + uc_object_t *object = ucv_as_object(uv); + uc_object_entry_t *e; if (ucv_type(uv) != UC_OBJECT) - return NULL; + return false; - rv = lh_table_lookup_ex(object->table, (const void *)key, (void **)&val); + while (*pos < object->used) { + e = &object->entries[(*pos)++]; - if (found != NULL) - *found = rv; + if (!e->key) + continue; - return val; + *key = _ucv_string_get(&e->key); + *val = e->value; + + return true; + } + + return false; } size_t @@ -1241,7 +1568,7 @@ ucv_object_length(uc_value_t *uv) if (ucv_type(uv) != UC_OBJECT) return 0; - return lh_table_length(object->table); + return object->count; } @@ -1671,157 +1998,6 @@ ucv_property_get(uc_value_t *uv, const char *key) } -uc_value_t * -ucv_from_json(uc_vm_t *vm, json_object *jso) -{ - //uc_array_t *arr; - uc_value_t *uv, *item; - int64_t n; - size_t i; - - switch (json_object_get_type(jso)) { - case json_type_null: - return NULL; - - case json_type_boolean: - return ucv_boolean_new(json_object_get_boolean(jso)); - - case json_type_double: - return ucv_double_new(json_object_get_double(jso)); - - case json_type_int: - n = json_object_get_int64(jso); - - if (n == INT64_MAX) - return ucv_uint64_new(json_object_get_uint64(jso)); - - return ucv_int64_new(n); - - case json_type_object: - uv = ucv_object_new(vm); - - json_object_object_foreach(jso, key, val) { - item = ucv_from_json(vm, val); - - if (!ucv_object_add(uv, key, item)) - ucv_put(item); - -#ifdef __clang_analyzer__ - /* Clang static analyzer does not understand that the object retains - * our item so pretend to free it here to suppress the false positive - * memory leak warning. */ - ucv_put(item); -#endif - } - - return uv; - - case json_type_array: - /* XXX - arr = (uc_array_t *)ucv_array_new_length(vm, json_object_array_length(jso)); - - for (i = 0; i < arr->count; i++) - arr->entries[i] = ucv_from_json(vm, json_object_array_get_idx(jso, i)); - - return &arr->header; - */ - uv = ucv_array_new(vm); - - for (i = 0; i < json_object_array_length(jso); i++) { - item = ucv_from_json(vm, json_object_array_get_idx(jso, i)); - - if (!ucv_array_push(uv, item)) - ucv_put(item); - -#ifdef __clang_analyzer__ - /* Clang static analyzer does not understand that the array retains - * our item so pretend to free it here to suppress the false positive - * memory leak warning. */ - ucv_put(item); -#endif - } - - return uv; - - case json_type_string: - return ucv_string_new_length(json_object_get_string(jso), json_object_get_string_len(jso)); - } - - return NULL; -} - -json_object * -ucv_to_json(uc_value_t *uv) -{ - uc_regexp_t *regexp; - uc_array_t *array; - json_object *jso; - size_t i; - char *s; - - switch (ucv_type(uv)) { - case UC_BOOLEAN: - return json_object_new_boolean(ucv_boolean_get(uv)); - - case UC_INTEGER: - if (ucv_is_u64(uv)) - return json_object_new_uint64(ucv_uint64_get(uv)); - - return json_object_new_int64(ucv_int64_get(uv)); - - case UC_DOUBLE: - return json_object_new_double(ucv_double_get(uv)); - - case UC_STRING: - return json_object_new_string_len(ucv_string_get(uv), ucv_string_length(uv)); - - case UC_ARRAY: - array = ucv_as_array(uv); - jso = json_object_new_array_ext(array->count); - - for (i = 0; i < array->count; i++) - json_object_array_put_idx(jso, i, ucv_to_json(array->entries[i])); - - return jso; - - case UC_OBJECT: - jso = json_object_new_object(); - - ucv_object_foreach(uv, key, val) - json_object_object_add(jso, key, ucv_to_json(val)); - - return jso; - - case UC_REGEXP: - regexp = ucv_as_regexp(uv); - i = asprintf(&s, "/%s/%s%s%s", - regexp->source, - regexp->global ? "g" : "", - regexp->icase ? "i" : "", - regexp->newline ? "s" : ""); - - if (i <= 0) - return NULL; - - jso = json_object_new_string_len(s, i); - - free(s); - - return jso; - - case UC_CLOSURE: - case UC_CFUNCTION: - case UC_RESOURCE: - case UC_UPVALUE: - case UC_PROGRAM: - case UC_SOURCE: - case UC_NULL: - return NULL; - } - - return NULL; -} - /* * Length of the UTF-8 sequence starting at s, or zero if the bytes there * do not form a valid RFC 3629 sequence: no overlong forms, no surrogates, @@ -2004,18 +2180,12 @@ ucv_call_tostring(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, bool json) return true; } -void -_ucv_stringbuf_append(uc_stringbuf_t *pb, const char *str, size_t len) -{ - printbuf_memappend_fast(pb, str, (int)len); -} - static void ucv_to_stringbuf_add_padding(uc_stringbuf_t *pb, char pad_char, size_t pad_size) { if (pad_char != '\0' && pad_char != '\1') { ucv_stringbuf_append(pb, "\n"); - printbuf_memset(pb, -1, pad_char, pad_size); + uc_stringbuf_memset(pb, -1, pad_char, pad_size); } else { ucv_stringbuf_append(pb, " "); @@ -2163,15 +2333,20 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size case UC_OBJECT: ucv_stringbuf_append(pb, "{"); - i = 0; - ucv_object_foreach(uv, key, val) { - if (i++) + /* by position, as conversions of values may change the object */ + for (size_t pos = 0, n = 0; pos < ucv_as_object(uv)->used; pos++) { + uc_object_entry_t *e = &ucv_as_object(uv)->entries[pos]; + + if (!e->key) + continue; + + if (n++) ucv_stringbuf_append(pb, ","); ucv_to_stringbuf_add_padding(pb, pad_char, (depth + 1) * pad_size); - ucv_to_string_json_encoded(pb, key, strlen(key), false); + ucv_to_string_json_encoded(pb, _ucv_string_get(&e->key), ucv_string_length(e->key), false); ucv_stringbuf_append(pb, ": "); - ucv_to_stringbuf_formatted(vm, pb, val, depth + 1, pad_char ? pad_char : '\1', pad_size); + ucv_to_stringbuf_formatted(vm, pb, e->value, depth + 1, pad_char ? pad_char : '\1', pad_size); if (vm != NULL && !pending && vm->exception.type != EXCEPTION_NONE) break; @@ -2321,7 +2496,7 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size static char * ucv_to_string_any(uc_vm_t *vm, uc_value_t *uv, char pad_char, size_t pad_size) { - uc_stringbuf_t *pb = xprintbuf_new(); + uc_stringbuf_t *pb = uc_stringbuf_new(); char *rv; ucv_to_stringbuf_formatted(vm, pb, uv, 0, pad_char, pad_size); @@ -2424,14 +2599,13 @@ ucv_is_equal(uc_value_t *uv1, uc_value_t *uv2) if (u1 != u2) return false; - ucv_object_foreach(uv1, key, val) { - if (!ucv_is_equal(val, ucv_object_get(uv2, key, NULL))) + ucv_object_foreach_entry(uv1, e) { + if (!ucv_is_equal(e->value, ucv_object_lookup_string(uv2, e->key, NULL))) return false; } - ucv_object_foreach(uv2, key2, val2) { - (void)val2; - ucv_object_get(uv1, key2, &b1); + ucv_object_foreach_entry(uv2, e) { + ucv_object_lookup_string(uv1, e->key, &b1); if (!b1) return false; @@ -2660,13 +2834,24 @@ ucv_compare(int how, uc_value_t *v1, uc_value_t *v2, int *deltap) } -static char * -ucv_key_to_string(uc_vm_t *vm, uc_value_t *val) +/* Returns the bytes of the key value `*val` and their number in `len`; a + * value that is no string is converted to a string in `copy`, which the + * caller releases. */ +static const char * +ucv_key_bytes(uc_vm_t *vm, uc_value_t **val, size_t *len, char **copy) { - if (ucv_type(val) != UC_STRING) - return ucv_to_string(vm, val); + *copy = NULL; - return NULL; + if (ucv_type(*val) == UC_STRING) { + *len = ucv_string_length(*val); + + return _ucv_string_get(val); + } + + *copy = ucv_to_string(vm, *val); + *len = strlen(*copy); + + return *copy; } static int64_t @@ -2694,7 +2879,7 @@ ucv_key_to_index(uc_value_t *val) k = ucv_string_get(val); idx = strtoll(k, &e, 0); - if (errno != 0 || e == k || *e != 0) + if (errno != 0 || e == k || e != k + ucv_string_length(val)) return INT64_MIN; return idx; @@ -2712,9 +2897,11 @@ ucv_key_to_index(uc_value_t *val) static uc_value_t * ucv_key_get_raw(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, bool *found) { - uc_value_t *o, *v = NULL, *inst_proto; + uc_value_t *o, *v = NULL, *inst_proto, *kv; + const char *k; + char *copy; int64_t idx; - char *k; + size_t len; *found = false; @@ -2731,7 +2918,8 @@ ucv_key_get_raw(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, bool *found) } if (!*found) { - k = ucv_key_to_string(vm, key); + k = ucv_key_bytes(vm, &key, &len, ©); + kv = copy ? NULL : key; /* Check resource type prototype first */ if (ucv_type(scope) == UC_RESOURCE) { @@ -2744,17 +2932,17 @@ ucv_key_get_raw(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, bool *found) * ucv_resource_new_prototyped); returns NULL for resources * without an instance proto. */ if ((inst_proto = ucv_resource_proto_get(scope)) != NULL) { - v = ucv_object_get(inst_proto, k ? k : ucv_string_get(key), found); + v = ucv_object_lookup_key(inst_proto, k, len, kv, found); } /* Then fall back to type prototype */ if (!*found && ext->type && ext->type->proto) { - v = ucv_object_get(ext->type->proto, k ? k : ucv_string_get(key), found); + v = ucv_object_lookup_key(ext->type->proto, k, len, kv, found); } } else { uc_resource_t *res = ucv_as_resource(scope); if (res->type && res->type->proto) { - v = ucv_object_get(res->type->proto, k ? k : ucv_string_get(key), found); + v = ucv_object_lookup_key(res->type->proto, k, len, kv, found); } } } @@ -2765,14 +2953,14 @@ ucv_key_get_raw(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, bool *found) if (ucv_type(o) != UC_OBJECT) continue; - v = ucv_object_get(o, k ? k : ucv_string_get(key), found); + v = ucv_object_lookup_key(o, k, len, kv, found); if (*found) break; } } - free(k); + free(copy); } return v; @@ -2897,11 +3085,14 @@ ucv_meta_invoke(uc_vm_t *vm, uc_value_t *scope, uc_value_t *meta, static bool ucv_object_has_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key) { - char *s = ucv_key_to_string(vm, key); bool found = false; + const char *k; + char *copy; + size_t len; - ucv_object_get(obj, s ? s : ucv_string_get(key), &found); - free(s); + k = ucv_key_bytes(vm, &key, &len, ©); + ucv_object_lookup_key(obj, k, len, copy ? NULL : key, &found); + free(copy); return found; } @@ -2909,11 +3100,14 @@ ucv_object_has_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key) static bool ucv_object_put_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key, uc_value_t *val) { - char *s = ucv_key_to_string(vm, key); + const char *k; + char *copy; + size_t len; bool rv; - rv = ucv_object_add(obj, s ? s : ucv_string_get(key), val); - free(s); + k = ucv_key_bytes(vm, &key, &len, ©); + rv = ucv_object_store(obj, k, len, copy ? NULL : key, val); + free(copy); if (!rv) ucv_put(val); @@ -2924,11 +3118,14 @@ ucv_object_put_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key, uc_value_t *va static bool ucv_object_del_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key) { - char *s = ucv_key_to_string(vm, key); + const char *k; + char *copy; + size_t len; bool rv; - rv = ucv_object_delete(obj, s ? s : ucv_string_get(key)); - free(s); + k = ucv_key_bytes(vm, &key, &len, ©); + rv = ucv_object_remove(obj, k, len); + free(copy); return rv; } @@ -2959,11 +3156,22 @@ uc_value_t * ucv_key_get(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key) { uc_value_t *meta, *val; + uc_object_entry_t *e; bool found; if (!scope || !key) return NULL; + /* a string key of the object itself, which the lookup below finds + * first as well; a missing one may still be found there or by __get__ */ + if (ucv_type(scope) == UC_OBJECT && ucv_type(key) == UC_STRING) { + e = ucv_object_find(ucv_as_object(scope), _ucv_string_get(&key), + ucv_string_length(key), key); + + if (e) + return ucv_key_resolve_upval(vm, e->value); + } + val = ucv_key_get_raw(vm, scope, key, &found); if (found) @@ -2994,7 +3202,8 @@ ucv_key_get(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key) uc_value_t * ucv_key_set(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, uc_value_t *val) { - uc_value_t *meta = NULL; + uc_value_t *meta = NULL, *old; + uc_object_entry_t *e; int64_t idx; /* only a null key is invalid, a null value stores null */ @@ -3028,6 +3237,21 @@ ucv_key_set(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, uc_value_t *val) break; case UC_OBJECT: + /* replace the value of an own string key of an object that is not + * frozen in place, as ucv_object_put_key() would */ + if (ucv_type(key) == UC_STRING && !scope->ext_flag) { + e = ucv_object_find(ucv_as_object(scope), _ucv_string_get(&key), + ucv_string_length(key), key); + + if (e) { + old = e->value; + e->value = ucv_get(val); + ucv_put(old); + + return ucv_get(val); + } + } + if (ucv_object_has_key(vm, scope, key)) return ucv_object_put_key(vm, scope, key, ucv_get(val)) ? ucv_get(val) : NULL; diff --git a/udbg.c b/udbg.c index be9c69c8..b1013f91 100644 --- a/udbg.c +++ b/udbg.c @@ -50,7 +50,8 @@ #include #include -#include +#include "ucode/types.h" +#include "ucode/json.h" #include "debug_highlight.h" #include "debug_lineedit.h" @@ -74,21 +75,20 @@ /* -- wire framing ---------------------------------------------------------- * - * Mirrors lib/debug_proto.c's framing without depending on it: this client - * has no ucode VM of its own to hand `ucv_*` helpers, so it talks the wire - * format directly in terms of json-c objects instead. */ + * Mirrors lib/debug_proto.c's framing without depending on it; payloads are + * ucode values without a VM. */ static void -proto_write(int fd, const char *verb, struct json_object *payload) +proto_write(int fd, const char *verb, uc_value_t *payload) { - const char *json; + char *json = NULL; char *line; size_t len; ssize_t n; const char *p; if (payload) { - json = json_object_to_json_string_ext(payload, JSON_C_TO_STRING_PLAIN); + json = ucv_to_jsonstring(NULL, payload); len = strlen(verb) + 1 + strlen(json) + 1; line = malloc(len + 1); snprintf(line, len + 1, "%s %s\n", verb, json); @@ -116,6 +116,7 @@ proto_write(int fd, const char *verb, struct json_object *payload) } free(line); + free(json); } /* Growable line-buffered reader, one instance per connection. */ @@ -148,10 +149,27 @@ linebuf_append(linebuf_t *lb, const char *data, size_t n) return true; } +/* Parses one payload; NULL if it is no valid JSON */ +static uc_value_t * +jparse(const char *s) +{ + uc_json_parser_t *parser = uc_json_parser_new(NULL); + uc_value_t *val = uc_json_parse(parser, s, strlen(s) + 1); + + if (uc_json_parser_error(parser) != UC_JSON_SUCCESS) { + ucv_put(val); + val = NULL; + } + + uc_json_parser_free(parser); + + return val; +} + /* Extract one already-buffered "VERB [json]" line, if any, without touching * the fd. Returns false if no full line is buffered yet. */ static bool -linebuf_pop(linebuf_t *lb, char **verb_out, struct json_object **payload_out) +linebuf_pop(linebuf_t *lb, char **verb_out, uc_value_t **payload_out) { char *nl = memchr(lb->data, '\n', lb->len); size_t linelen, verblen; @@ -181,7 +199,7 @@ linebuf_pop(linebuf_t *lb, char **verb_out, struct json_object **payload_out) *payload_out = NULL; if (sp && *(sp + 1)) - *payload_out = json_tokener_parse(sp + 1); + *payload_out = jparse(sp + 1); free(line); @@ -195,26 +213,84 @@ linebuf_pop(linebuf_t *lb, char **verb_out, struct json_object **payload_out) * ever answers strictly in request order while paused, so the first message * to arrive is the one we asked for, barring the rare case of an async * EVENT interleaving, which is not handled specially here. */ -static const char * -jstr(struct json_object *obj, const char *key, const char *dflt) +/* Returns the slot of the value of `key` in the object `obj`, or NULL. The + * strings of payloads are read from their slots, since a short string lives + * in the value pointer itself. */ +static uc_value_t ** +jslot(uc_value_t *obj, const char *key) { - struct json_object *v; + uc_object_t *object = (uc_object_t *)obj; + size_t len = strlen(key); + + if (ucv_type(obj) != UC_OBJECT) + return NULL; - if (obj && json_object_object_get_ex(obj, key, &v) && json_object_is_type(v, json_type_string)) - return json_object_get_string(v); + for (uint32_t i = 0; i < object->used; i++) { + uc_object_entry_t *e = &object->entries[i]; + + if (e->key && ucv_string_length(e->key) == len && !memcmp(ucv_string_get(e->key), key, len)) + return &e->value; + } + + return NULL; +} + +/* Returns the slot of item `i` of the array `arr`, or NULL */ +static uc_value_t ** +jitem(uc_value_t *arr, size_t i) +{ + if (ucv_type(arr) != UC_ARRAY || i >= ucv_array_length(arr)) + return NULL; + + return &((uc_array_t *)arr)->entries[i]; +} + +static uc_value_t * +jget(uc_value_t *obj, const char *key) +{ + uc_value_t **slot = jslot(obj, key); + + return slot ? *slot : NULL; +} + +static bool +jhas(uc_value_t *obj, const char *key) +{ + return jslot(obj, key) != NULL; +} + +static const char * +jslot_str(uc_value_t **slot, const char *dflt) +{ + if (slot && ucv_type(*slot) == UC_STRING) + return _ucv_string_get(slot); return dflt; } +static const char * +jstr(uc_value_t *obj, const char *key, const char *dflt) +{ + return jslot_str(jslot(obj, key), dflt); +} + static int64_t -jint(struct json_object *obj, const char *key, int64_t dflt) +jint(uc_value_t *obj, const char *key, int64_t dflt) { - struct json_object *v; + uc_value_t **slot = jslot(obj, key); - if (obj && json_object_object_get_ex(obj, key, &v)) - return json_object_get_int64(v); + return slot ? ucv_to_integer(*slot) : dflt; +} - return dflt; +/* Returns a new object with the string `value` under `key` */ +static uc_value_t * +jobject_str(const char *key, const char *value) +{ + uc_value_t *obj = ucv_object_new(NULL); + + ucv_object_add(obj, key, ucv_string_new(value)); + + return obj; } /* Every "col"/"from_col"/"to_col" field the protocol sends is 1-based (see @@ -419,7 +495,7 @@ static void request_source(int fd, const char *file, int64_t from, int64_t to, const debug_highlight_span_t *hl, size_t left_pad) { - struct json_object *payload; + uc_value_t *payload; /* A fetch for this exact file is already in flight (e.g. the initial * PAUSED's own auto-context request hasn't resolved yet when a @@ -440,10 +516,9 @@ request_source(int fd, const char *file, int64_t from, int64_t to, return; } - payload = json_object_new_object(); - json_object_object_add(payload, "file", json_object_new_string(file)); + payload = jobject_str("file", file); proto_write(fd, "SOURCE", payload); - json_object_put(payload); + ucv_put(payload); free(pending_source.file); pending_source.active = true; @@ -571,10 +646,10 @@ render_source_lines(int fd, const char *file, int64_t from, int64_t to, /* Join a JSON array of strings with " \xc2\xbb " (U+00BB, " » "), matching * format_context_breadcrumb()'s separator. Caller frees the result. */ static char * -join_breadcrumb(struct json_object *arr) +join_breadcrumb(uc_value_t *arr) { static const char sep[] = " \xc2\xbb "; /* U+00BB RIGHT-POINTING GUILLEMET */ - size_t n = arr ? json_object_array_length(arr) : 0; + size_t n = ucv_array_length(arr); size_t len = 0, i; char *out, *p; @@ -582,13 +657,13 @@ join_breadcrumb(struct json_object *arr) return strdup(""); for (i = 0; i < n; i++) - len += strlen(json_object_get_string(json_object_array_get_idx(arr, i))); + len += strlen(jslot_str(jitem(arr, i), "")); len += (n - 1) * (sizeof(sep) - 1); out = p = malloc(len + 1); for (i = 0; i < n; i++) { - const char *s = json_object_get_string(json_object_array_get_idx(arr, i)); + const char *s = jslot_str(jitem(arr, i), ""); size_t l = strlen(s); if (i > 0) { @@ -606,7 +681,7 @@ join_breadcrumb(struct json_object *arr) } static void -render_paused(int fd, struct json_object *p) +render_paused(int fd, uc_value_t *p) { int64_t line = jint(p, "line", 0); const char *file = jstr(p, "file", NULL); @@ -618,14 +693,14 @@ render_paused(int fd, struct json_object *p) line, jint(p, "col", 0)); - if (json_object_object_get_ex(p, "breakpoint_id", NULL)) + if (jhas(p, "breakpoint_id")) printf(" " C_GREEN "breakpoint #%" PRId64 C_RESET "\n", jint(p, "breakpoint_id", 0)); - if (json_object_object_get_ex(p, "exception_message", NULL)) + if (jhas(p, "exception_message")) printf(" " C_RED "exception: %s" C_RESET "\n", jstr(p, "exception_message", "")); if (file && line > 0) { - struct json_object *breadcrumb_arr = json_object_object_get(p, "breadcrumb"); + uc_value_t *breadcrumb_arr = jget(p, "breadcrumb"); char *breadcrumb = join_breadcrumb(breadcrumb_arr); int64_t col = jint(p, "col", 0); debug_highlight_span_t hl = { @@ -642,13 +717,10 @@ render_paused(int fd, struct json_object *p) } static void -render_breakpoints(struct json_object *p) +render_breakpoints(uc_value_t *p) { - struct json_object *items = NULL; - size_t i, n; - - json_object_object_get_ex(p, "items", &items); - n = items ? json_object_array_length(items) : 0; + uc_value_t *items = jget(p, "items"); + size_t i, n = ucv_array_length(items); if (n == 0) { printf("No breakpoints set\n"); @@ -656,15 +728,14 @@ render_breakpoints(struct json_object *p) } for (i = 0; i < n; i++) { - struct json_object *it = json_object_array_get_idx(items, i); - struct json_object *idv = NULL; + uc_value_t *it = ucv_array_get(items, i); - if (json_object_object_get_ex(it, "id", &idv)) - printf(C_BOLD "#%-4" PRId64 C_RESET " ", json_object_get_int64(idv)); + if (jhas(it, "id")) + printf(C_BOLD "#%-4" PRId64 C_RESET " ", jint(it, "id", 0)); else printf(C_DIM "(%-4s)" C_RESET " ", jstr(it, "kind", "?")); - if (json_object_object_get_ex(it, "file", NULL)) + if (jhas(it, "file")) printf("%s:%" PRId64 ":%" PRId64 " - %s\n", jstr(it, "file", "?"), jint(it, "line", 0), jint(it, "col", 0), jstr(it, "function", "?")); @@ -674,19 +745,19 @@ render_breakpoints(struct json_object *p) } static void -render_variables_array(struct json_object *items, const char *indent) +render_variables_array(uc_value_t *items, const char *indent) { - size_t i, n = items ? json_object_array_length(items) : 0; + size_t i, n = ucv_array_length(items); debug_variable_t *vars = calloc(n ? n : 1, sizeof(*vars)); for (i = 0; i < n; i++) { - struct json_object *it = json_object_array_get_idx(items, i); - struct json_object *shadowed_j = json_object_object_get(it, "shadowed"); + uc_value_t *it = ucv_array_get(items, i); + uc_value_t *shadowed_j = jget(it, "shadowed"); vars[i].name = jstr(it, "name", "?"); vars[i].kind = jstr(it, "kind", ""); vars[i].value_repr = jstr(it, "value_repr", ""); - vars[i].shadowed = shadowed_j && json_object_get_boolean(shadowed_j); + vars[i].shadowed = ucv_is_truish(shadowed_j); } debug_highlight_print_variables(stdout, vars, n, indent, term_columns()); @@ -700,7 +771,7 @@ render_variables_array(struct json_object *items, const char *indent) * one at a time, and only actually prints once all of them have arrived. */ typedef struct { bool active; - struct json_object *payload; + uc_value_t *payload; char **files; size_t nfiles, next; } pending_backtrace_t; @@ -710,25 +781,20 @@ static pending_backtrace_t pending_backtrace = { 0 }; static void request_backtrace_file(int fd, const char *file) { - struct json_object *payload = json_object_new_object(); + uc_value_t *payload = jobject_str("file", file); - json_object_object_add(payload, "file", json_object_new_string(file)); proto_write(fd, "SOURCE", payload); - json_object_put(payload); + ucv_put(payload); } static void -render_backtrace_final(int fd, struct json_object *p) +render_backtrace_final(int fd, uc_value_t *p) { - struct json_object *frames = NULL; - size_t i, n; - - json_object_object_get_ex(p, "frames", &frames); - n = frames ? json_object_array_length(frames) : 0; + uc_value_t *frames = jget(p, "frames"); + size_t i, n = ucv_array_length(frames); for (i = 0; i < n; i++) { - struct json_object *fr = json_object_array_get_idx(frames, i); - struct json_object *vars = NULL; + uc_value_t *fr = ucv_array_get(frames, i); const char *file = jstr(fr, "file", NULL); int64_t line = jint(fr, "line", 0); int64_t col = jint(fr, "col", 0); @@ -769,34 +835,27 @@ render_backtrace_final(int fd, struct json_object *p) /* tail call optimization reuses this frame for the callee, so the * intermediate frames of the tail calls are absent from the stack - * show the gap (see uc_vm_frame_reinit() in vm.c). */ - { - struct json_object *tco_j = json_object_object_get(fr, "tco"); - - if (tco_j && json_object_get_int64(tco_j) > 0) - printf(C_DIM " (%" PRId64 " tail call frames omitted)" C_RESET "\n", - json_object_get_int64(tco_j)); - } + if (jint(fr, "tco", 0) > 0) + printf(C_DIM " (%" PRId64 " tail call frames omitted)" C_RESET "\n", + jint(fr, "tco", 0)); - if (json_object_object_get_ex(fr, "variables", &vars)) - render_variables_array(vars, " - "); + if (jhas(fr, "variables")) + render_variables_array(jget(fr, "variables"), " - "); printf("\n"); } } static void -render_backtrace(int fd, struct json_object *p) +render_backtrace(int fd, uc_value_t *p) { - struct json_object *frames = NULL; - size_t i, n; + uc_value_t *frames = jget(p, "frames"); + size_t i, n = ucv_array_length(frames); char **missing = NULL; size_t n_missing = 0, cap = 0; - json_object_object_get_ex(p, "frames", &frames); - n = frames ? json_object_array_length(frames) : 0; - for (i = 0; i < n; i++) { - struct json_object *fr = json_object_array_get_idx(frames, i); + uc_value_t *fr = ucv_array_get(frames, i); const char *file = jstr(fr, "file", NULL); size_t dummy; size_t j; @@ -833,7 +892,7 @@ render_backtrace(int fd, struct json_object *p) } pending_backtrace.active = true; - pending_backtrace.payload = json_object_get(p); + pending_backtrace.payload = ucv_get(p); pending_backtrace.files = missing; pending_backtrace.nfiles = n_missing; pending_backtrace.next = 0; @@ -842,10 +901,10 @@ render_backtrace(int fd, struct json_object *p) } static void -render_source_range(int fd, struct json_object *p) +render_source_range(int fd, uc_value_t *p) { const char *file = jstr(p, "file", NULL); - struct json_object *cursor = json_object_object_get(p, "cursor"); + uc_value_t *cursor = jget(p, "cursor"); int64_t from = jint(p, "from", 0); int64_t to = jint(p, "to", 0); debug_highlight_span_t hl; @@ -881,36 +940,33 @@ render_source_range(int fd, struct json_object *p) * already point at storage for at least `cap` bytes; only the first * min(array length, cap) entries are filled. */ static void -jbytes(struct json_object *arr, unsigned char *dst, size_t cap) +jbytes(uc_value_t *arr, unsigned char *dst, size_t cap) { - size_t n = arr ? json_object_array_length(arr) : 0; + size_t n = ucv_array_length(arr); if (n > cap) n = cap; for (size_t i = 0; i < n; i++) - dst[i] = (unsigned char)json_object_get_int64(json_object_array_get_idx(arr, i)); + dst[i] = (unsigned char)ucv_to_integer(ucv_array_get(arr, i)); } static void -render_disassembly(struct json_object *p) +render_disassembly(uc_value_t *p) { - struct json_object *insns_j = NULL; + uc_value_t *insns_j = jget(p, "instructions"); + size_t n = ucv_array_length(insns_j); debug_disasm_insn_t *insns; - size_t n; - json_object_object_get_ex(p, "instructions", &insns_j); - n = insns_j ? json_object_array_length(insns_j) : 0; insns = calloc(n, sizeof(*insns)); for (size_t i = 0; i < n; i++) { - struct json_object *ins = json_object_array_get_idx(insns_j, i); - struct json_object *bytes_j = json_object_object_get(ins, "bytes"); - struct json_object *constant_j = NULL; - struct json_object *captures_j = json_object_object_get(ins, "captures"); - struct json_object *unpacks_j = json_object_object_get(ins, "unpacks"); + uc_value_t *ins = ucv_array_get(insns_j, i); + uc_value_t *bytes_j = jget(ins, "bytes"); + uc_value_t *captures_j = jget(ins, "captures"); + uc_value_t *unpacks_j = jget(ins, "unpacks"); debug_disasm_insn_t *d = &insns[i]; - size_t nbytes = bytes_j ? json_object_array_length(bytes_j) : 0; + size_t nbytes = ucv_array_length(bytes_j); unsigned char *bytes = malloc(nbytes ? nbytes : 1); jbytes(bytes_j, bytes, nbytes); @@ -922,60 +978,54 @@ render_disassembly(struct json_object *p) d->nbytes = nbytes; d->operand = jint(ins, "operand", 0); - if (json_object_object_get_ex(ins, "constant", &constant_j)) { + if (jhas(ins, "constant")) { d->have_constant = true; - d->constant_repr = json_object_to_json_string(constant_j); - d->constant_is_string = (json_object_get_type(constant_j) == json_type_string); + d->constant_repr = ucv_to_jsonstring(NULL, jget(ins, "constant")); + d->constant_is_string = (ucv_type(jget(ins, "constant")) == UC_STRING); } - if (json_object_object_get_ex(ins, "variable_kind", NULL)) { + if (jhas(ins, "variable_kind")) { d->variable_kind = jstr(ins, "variable_kind", NULL); d->variable_name = jstr(ins, "variable_name", NULL); } - if (json_object_object_get_ex(ins, "closure_index", NULL)) { + if (jhas(ins, "closure_index")) { d->have_closure = true; d->closure_kind = jstr(ins, "closure_kind", "closure"); d->closure_index = (uint32_t)jint(ins, "closure_index", 0); } - if (json_object_object_get_ex(ins, "call_nargs", NULL)) { - struct json_object *mcall_j = json_object_object_get(ins, "call_mcall"); - struct json_object *tail_j = json_object_object_get(ins, "call_tail"); - struct json_object *stdlib_j = json_object_object_get(ins, "stdlib_name"); - + if (jhas(ins, "call_nargs")) { d->have_call = true; - d->call_mcall = mcall_j && json_object_get_boolean(mcall_j); + d->call_mcall = ucv_is_truish(jget(ins, "call_mcall")); d->call_nargs = (uint32_t)jint(ins, "call_nargs", 0); - d->call_tail = tail_j && json_object_get_boolean(tail_j); - - if (stdlib_j && json_object_get_type(stdlib_j) == json_type_string) - d->stdlib_name = json_object_get_string(stdlib_j); + d->call_tail = ucv_is_truish(jget(ins, "call_tail")); + d->stdlib_name = jstr(ins, "stdlib_name", NULL); } - if (json_object_object_get_ex(ins, "return_tailcall", NULL)) + if (jhas(ins, "return_tailcall")) d->return_tailcall = true; - d->ncaptures = captures_j ? json_object_array_length(captures_j) : 0; + d->ncaptures = ucv_array_length(captures_j); d->captures = calloc(d->ncaptures ? d->ncaptures : 1, sizeof(*d->captures)); for (size_t j = 0; j < d->ncaptures; j++) { - struct json_object *cap = json_object_array_get_idx(captures_j, j); + uc_value_t *cap = ucv_array_get(captures_j, j); d->captures[j].slot = jint(cap, "slot", 0); d->captures[j].upval = !strcmp(jstr(cap, "kind", ""), "upval"); d->captures[j].name = jstr(cap, "name", "(unknown)"); - jbytes(json_object_object_get(cap, "bytes"), d->captures[j].bytes, 4); + jbytes(jget(cap, "bytes"), d->captures[j].bytes, 4); } - d->nunpacks = unpacks_j ? json_object_array_length(unpacks_j) : 0; + d->nunpacks = ucv_array_length(unpacks_j); d->unpacks = calloc(d->nunpacks ? d->nunpacks : 1, sizeof(*d->unpacks)); for (size_t j = 0; j < d->nunpacks; j++) { - struct json_object *u = json_object_array_get_idx(unpacks_j, j); + uc_value_t *u = ucv_array_get(unpacks_j, j); d->unpacks[j].slot = (uint16_t)jint(u, "slot", 0); - jbytes(json_object_object_get(u, "bytes"), d->unpacks[j].bytes, 2); + jbytes(jget(u, "bytes"), d->unpacks[j].bytes, 2); } } @@ -983,6 +1033,7 @@ render_disassembly(struct json_object *p) for (size_t i = 0; i < n; i++) { free((void *)insns[i].bytes); + free((void *)insns[i].constant_repr); free(insns[i].captures); free(insns[i].unpacks); } @@ -994,14 +1045,14 @@ render_disassembly(struct json_object *p) * time, unprompted by anything the user typed - set in a faint italic * style to visually set them apart from direct command responses. */ static void -render_event(struct json_object *p) +render_event(uc_value_t *p) { const char *event = jstr(p, "event", "?"); printf(C_EVENT); if (!strcmp(event, "exception")) { - struct json_object *exc = json_object_object_get(p, "exception"); + uc_value_t *exc = jget(p, "exception"); printf("*** exception: %s: %s ***", jstr(exc, "type", "Error"), jstr(exc, "message", "?")); @@ -1016,7 +1067,7 @@ render_event(struct json_object *p) printf("*** program exited (code %" PRId64 ") ***", jint(p, "code", 0)); } else { - struct json_object *exc = json_object_object_get(p, "exception"); + uc_value_t *exc = jget(p, "exception"); if (exc) printf("*** program terminated: %s: %s ***", @@ -1026,22 +1077,24 @@ render_event(struct json_object *p) } } else { - printf("*** event: %s %s ***", event, - json_object_to_json_string_ext(p, JSON_C_TO_STRING_SPACED)); + char *json = ucv_to_jsonstring(NULL, p); + + printf("*** event: %s %s ***", event, json); + free(json); } printf(C_RESET "\n"); } static void -render_response(int fd, const char *verb, struct json_object *payload) +render_response(int fd, const char *verb, uc_value_t *payload) { if (!strcmp(verb, "PAUSED")) render_paused(fd, payload); else if (!strcmp(verb, "BREAKPOINTS")) render_breakpoints(payload); else if (!strcmp(verb, "VARIABLES")) - render_variables_array(json_object_object_get(payload, "vars"), ""); + render_variables_array(jget(payload, "vars"), ""); else if (!strcmp(verb, "BACKTRACE")) render_backtrace(fd, payload); else if (!strcmp(verb, "SOURCE_RANGE")) @@ -1079,7 +1132,7 @@ render_response(int fd, const char *verb, struct json_object *payload) size_t i; render_backtrace_final(fd, pending_backtrace.payload); - json_object_put(pending_backtrace.payload); + ucv_put(pending_backtrace.payload); for (i = 0; i < pending_backtrace.nfiles; i++) free(pending_backtrace.files[i]); @@ -1119,7 +1172,7 @@ render_response(int fd, const char *verb, struct json_object *payload) * rest - just print what we have (unavailable files will * fall back to "no snippet" for that frame). */ render_backtrace_final(fd, pending_backtrace.payload); - json_object_put(pending_backtrace.payload); + ucv_put(pending_backtrace.payload); for (i = 0; i < pending_backtrace.nfiles; i++) free(pending_backtrace.files[i]); @@ -1130,21 +1183,21 @@ render_response(int fd, const char *verb, struct json_object *payload) } } else if (!strcmp(verb, "HELP")) { - struct json_object *cmds = json_object_object_get(payload, "commands"); - size_t i, n = cmds ? json_object_array_length(cmds) : 0; + uc_value_t *cmds = jget(payload, "commands"); + size_t i, n = ucv_array_length(cmds); for (i = 0; i < n; i++) { - struct json_object *c = json_object_array_get_idx(cmds, i); + uc_value_t *c = ucv_array_get(cmds, i); printf("%-16s %s\n", jstr(c, "verb", "?"), jstr(c, "help", "")); } } else if (!strcmp(verb, "SOURCES")) { - struct json_object *items = json_object_object_get(payload, "items"); - size_t i, n = items ? json_object_array_length(items) : 0; + uc_value_t *items = jget(payload, "items"); + size_t i, n = ucv_array_length(items); for (i = 0; i < n; i++) { - struct json_object *it = json_object_array_get_idx(items, i); + uc_value_t *it = ucv_array_get(items, i); printf("#%-2" PRId64 " %s\n", jint(it, "index", 0), jstr(it, "file", "?")); } @@ -1156,7 +1209,10 @@ render_response(int fd, const char *verb, struct json_object *payload) printf("(resumed)\n"); } else if (payload) { - printf("%s %s\n", verb, json_object_to_json_string_ext(payload, JSON_C_TO_STRING_SPACED)); + char *json = ucv_to_jsonstring(NULL, payload); + + printf("%s %s\n", verb, json); + free(json); } else { printf("%s\n", verb); @@ -1448,7 +1504,7 @@ static bool send_command(int fd, char *line, bool *resuming, bool *sent) { char *cmd = shift_word(&line); - struct json_object *payload = NULL; + uc_value_t *payload = NULL; *resuming = false; *sent = true; @@ -1463,14 +1519,13 @@ send_command(int fd, char *line, bool *resuming, bool *sent) *sent = false; } else if (match_cmd("break\0b\0", cmd)) { - payload = json_object_new_object(); - json_object_object_add(payload, "spec", json_object_new_string(line)); + payload = jobject_str("spec", line); proto_write(fd, "BREAK", payload); } else if (match_cmd("delete\0d\0", cmd)) { if (*line) { - payload = json_object_new_object(); - json_object_object_add(payload, "id", json_object_new_int64(strtoll(line, NULL, 10))); + payload = ucv_object_new(NULL); + ucv_object_add(payload, "id", ucv_int64_new(strtoll(line, NULL, 10))); } proto_write(fd, "DELETE", payload); @@ -1495,9 +1550,8 @@ send_command(int fd, char *line, bool *resuming, bool *sent) *resuming = true; } else if (match_cmd("backtrace\0bt\0", cmd)) { - payload = json_object_new_object(); - json_object_object_add(payload, "full", - json_object_new_boolean(!strcmp(trim(line), "full"))); + payload = ucv_object_new(NULL); + ucv_object_add(payload, "full", ucv_boolean_new(!strcmp(trim(line), "full"))); proto_write(fd, "BACKTRACE", payload); } else if (match_cmd("variables\0vars\0", cmd)) { @@ -1507,13 +1561,11 @@ send_command(int fd, char *line, bool *resuming, bool *sent) proto_write(fd, "SOURCES", NULL); } else if (match_cmd("print\0p\0", cmd)) { - payload = json_object_new_object(); - json_object_object_add(payload, "expr", json_object_new_string(line)); + payload = jobject_str("expr", line); proto_write(fd, "PRINT", payload); } else if (match_cmd("eval\0e\0", cmd)) { - payload = json_object_new_object(); - json_object_object_add(payload, "expr", json_object_new_string(line)); + payload = jobject_str("expr", line); proto_write(fd, "EVAL", payload); } else if (match_cmd("lines\0ln\0", cmd)) { @@ -1521,16 +1573,16 @@ send_command(int fd, char *line, bool *resuming, bool *sent) char *before = shift_word(&line); char *after = shift_word(&line); - payload = json_object_new_object(); + payload = ucv_object_new(NULL); if (*spec) - json_object_object_add(payload, "spec", json_object_new_string(spec)); + ucv_object_add(payload, "spec", ucv_string_new(spec)); if (*before) - json_object_object_add(payload, "before", json_object_new_int64(strtoll(before, NULL, 10))); + ucv_object_add(payload, "before", ucv_int64_new(strtoll(before, NULL, 10))); if (*after) - json_object_object_add(payload, "after", json_object_new_int64(strtoll(after, NULL, 10))); + ucv_object_add(payload, "after", ucv_int64_new(strtoll(after, NULL, 10))); proto_write(fd, "LINES", payload); } @@ -1549,31 +1601,29 @@ send_command(int fd, char *line, bool *resuming, bool *sent) } } - payload = json_object_new_object(); + payload = ucv_object_new(NULL); if (is_type && *line) { - json_object_object_add(payload, "type", json_object_new_string(first)); - json_object_object_add(payload, "message", json_object_new_string(line)); + ucv_object_add(payload, "type", ucv_string_new(first)); + ucv_object_add(payload, "message", ucv_string_new(line)); } else { char *msg = *line ? line : first; - json_object_object_add(payload, "message", json_object_new_string(msg)); + ucv_object_add(payload, "message", ucv_string_new(msg)); } proto_write(fd, "THROW", payload); } else if (match_cmd("disassemble\0disasm\0", cmd)) { if (*line) { - payload = json_object_new_object(); - json_object_object_add(payload, "spec", json_object_new_string(line)); + payload = jobject_str("spec", line); } proto_write(fd, "DISASSEMBLE", payload); } else if (match_cmd("source\0", cmd)) { - payload = json_object_new_object(); - json_object_object_add(payload, "file", json_object_new_string(line)); + payload = jobject_str("file", line); proto_write(fd, "SOURCE", payload); } else if (match_cmd("quit\0q\0", cmd)) { @@ -1856,7 +1906,7 @@ main(int argc, char **argv) for (;;) { char *verb; - struct json_object *payload; + uc_value_t *payload; while (linebuf_pop(&lb, &verb, &payload)) { render_response(fd, verb, payload); @@ -1868,7 +1918,7 @@ main(int argc, char **argv) paused = false; free(verb); - json_object_put(payload); + ucv_put(payload); } /* Only accept (and select on) stdin for actual command input while diff --git a/vm.c b/vm.c index ff2f4df0..d9e7c7e6 100644 --- a/vm.c +++ b/vm.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -323,7 +324,7 @@ void uc_vm_free(uc_vm_t *vm) uc_vector_clear(&vm->stack); uc_vector_clear(&vm->callframes); - printbuf_free(vm->strbuf); + uc_stringbuf_free(vm->strbuf); ucv_freeall(vm); @@ -451,15 +452,15 @@ static char * uc_vm_format_val(uc_vm_t *vm, uc_value_t *val) { if (!vm->strbuf) - vm->strbuf = xprintbuf_new(); + vm->strbuf = uc_stringbuf_new(); else - printbuf_reset(vm->strbuf); + uc_stringbuf_reset(vm->strbuf); ucv_to_stringbuf(NULL, vm->strbuf, val, true); - if (printbuf_length(vm->strbuf) >= 64) { - printbuf_memset(vm->strbuf, 60, '.', 3); - printbuf_memset(vm->strbuf, 63, 0, 1); + if (uc_stringbuf_length(vm->strbuf) >= 64) { + uc_stringbuf_memset(vm->strbuf, 60, '.', 3); + uc_stringbuf_memset(vm->strbuf, 63, 0, 1); } return vm->strbuf->buf; @@ -1052,11 +1053,11 @@ uc_dump_insn(uc_vm_t *vm, uint8_t *pos, uc_vm_insn_t insn) source = uc_vm_frame_source(frame); if (last_srcpos == 0 || last_source != source || srcpos != last_srcpos) { - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); uc_source_context_format(buf, source, srcpos, true); - fwrite(buf->buf, 1, printbuf_length(buf), stderr); - printbuf_free(buf); + fwrite(buf->buf, 1, uc_stringbuf_length(buf), stderr); + uc_stringbuf_free(buf); last_source = source; last_srcpos = srcpos; @@ -2519,8 +2520,7 @@ static void uc_vm_insn_nobj(uc_vm_t *vm, uc_vm_insn_t insn) { size_t nkeys = vm->arg.u32 / 2; - uc_value_t *obj = nkeys ? ucv_object_new_size(vm, nkeys / LH_LOAD_FACTOR + 1) - : ucv_object_new(vm); + uc_value_t *obj = ucv_object_new_size(vm, nkeys); uc_vm_stack_push(vm, obj); } @@ -2595,8 +2595,8 @@ uc_vm_insn_mobj(uc_vm_t *vm, uc_vm_insn_t insn) switch (ucv_type(src)) { case UC_OBJECT: ; /* a label can only be part of a statement and a declaration is not a statement */ - ucv_object_foreach(src, k, v) - ucv_object_add(dst, k, ucv_get(v)); + ucv_object_foreach_entry(src, e) + ucv_object_insert_string(dst, e->key, ucv_get(e->value)); ucv_put(src); break; @@ -2771,7 +2771,7 @@ uc_vm_key_exists(uc_value_t *scope, uc_value_t *key) return false; for (o = scope; o != NULL && !found; o = ucv_prototype_get(o)) - ucv_object_get(o, ucv_string_get(key), &found); + ucv_object_lookup(o, ucv_string_get(key), ucv_string_length(key), &found); return found; } @@ -2933,10 +2933,11 @@ uc_vm_object_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, uc_resource_t *res = ucv_as_resource(k); uc_object_t *obj = ucv_as_object(v); uc_object_iterator_t *iter; + uc_object_entry_t *e; if (!res) { /* object is empty */ - if (!obj->table->head) + if (!obj->count) return false; res = xalloc(sizeof(*res) + sizeof(uc_object_iterator_t)); @@ -2945,8 +2946,8 @@ uc_vm_object_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, res->type = &uc_vm_object_iterator_type; iter = res->data = (char *)res + sizeof(*res); - iter->table = obj->table; - iter->u.pos = obj->table->head; + iter->object = obj; + iter->pos = 0; uc_list_insert(&uc_thread_context_get()->object_iterators, &iter->list); } @@ -2961,23 +2962,25 @@ uc_vm_object_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, return false; } + while (iter->pos < obj->used && !obj->entries[iter->pos].key) + iter->pos++; + /* no next key */ - if (!iter->u.pos) { + if (iter->pos >= obj->used) { uc_list_remove(&iter->list); return false; } - uc_vm_stack_push(vm, ucv_string_new(iter->u.pos->k)); + e = &obj->entries[iter->pos++]; + uc_vm_stack_push(vm, ucv_get(e->key)); if (insn == I_NEXTKV) - uc_vm_stack_push(vm, ucv_get((uc_value_t *)iter->u.pos->v)); + uc_vm_stack_push(vm, ucv_get(e->value)); uc_vm_stack_push(vm, &res->header); ucv_put(v); - iter->u.pos = iter->u.pos->next; - return true; } @@ -3283,8 +3286,8 @@ uc_vm_insn_dynload(uc_vm_t *vm, uc_vm_insn_t insn) if (count == 0) { modobj = ucv_object_new(vm); - ucv_object_foreach(modscope, k, v) - ucv_object_add(modobj, k, ucv_get(v)); + ucv_object_foreach_entry(modscope, e) + ucv_object_insert_string(modobj, e->key, ucv_get(e->value)); ucv_set_constant(modobj, true); @@ -3842,12 +3845,12 @@ uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) uc_vm_run_begin(closure, !kept); if (vm->trace) { - buf = xprintbuf_new(); + buf = uc_stringbuf_new(); uc_source_context_format(buf, uc_vm_frame_source(frame), 0, true); - fwrite(buf->buf, 1, printbuf_length(buf), stderr); - printbuf_free(buf); + fwrite(buf->buf, 1, uc_stringbuf_length(buf), stderr); + uc_stringbuf_free(buf); uc_vm_frame_dump(vm, frame); } From 90067c31097b6b449745e1bf789025b6d8784fb4 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Fri, 30 Oct 2026 08:21:00 +0100 Subject: [PATCH 06/11] util: keep the capacity of a vector instead of deriving it from the count uc_vector_extend() computed the capacity from the count with a loop at each push. After a pop the derived capacity was smaller than the allocation, so each push across a capacity boundary reallocated and cleared the spare entries again; the call frames of a recursion that goes up and down at such a depth did this at every call. Vectors now have a capacity field. Entries set to NULL by code outside the macros are allocated anew, and a capacity of 0 next to allocated entries causes a reallocation, so code that sets the entries itself keeps working. The hand written vector structs get the field as well. This changes the layout of uc_vm_t, uc_array_t and other public structs. Cycles: fib -3 %, strings -5 %. 100k empty and 100k three element arrays: peak RSS 21208 -> 21428 kB. Signed-off-by: John Crispin --- include/ucode/internal/compiler.h | 2 +- include/ucode/internal/lexer.h | 2 ++ include/ucode/internal/types.h | 2 ++ include/ucode/types.h | 1 + include/ucode/util.h | 38 +++++++++++++++++++------------ lib/debug.c | 4 ++-- lib/ffi/ffi.c | 4 ++++ lib/ffi/uc_cparse.h | 2 +- lib/ffi/uc_ctype.h | 2 ++ lib/socket.c | 8 +++---- 10 files changed, 43 insertions(+), 22 deletions(-) diff --git a/include/ucode/internal/compiler.h b/include/ucode/internal/compiler.h index fabe0df6..9cddec47 100644 --- a/include/ucode/internal/compiler.h +++ b/include/ucode/internal/compiler.h @@ -69,7 +69,7 @@ typedef enum { typedef struct uc_patchlist { struct uc_patchlist *parent; - size_t depth, count, *entries; + size_t depth, count, *entries, capacity; uc_tokentype_t token; } uc_patchlist_t; diff --git a/include/ucode/internal/lexer.h b/include/ucode/internal/lexer.h index ca382af4..40d10b39 100644 --- a/include/ucode/internal/lexer.h +++ b/include/ucode/internal/lexer.h @@ -171,10 +171,12 @@ typedef struct { struct { size_t count; size_t *entries; + size_t capacity; } templates; struct { size_t count; char *entries; + size_t capacity; } buffer; const uint8_t *rbuf; size_t rlen, rpos, off; diff --git a/include/ucode/internal/types.h b/include/ucode/internal/types.h index 5a6c5e11..d9cd9577 100644 --- a/include/ucode/internal/types.h +++ b/include/ucode/internal/types.h @@ -54,6 +54,7 @@ struct uc_source { struct { size_t count, offset; uc_value_t **entries; + size_t capacity; } exports; }; @@ -80,6 +81,7 @@ uc_declare_vector(uc_offsetinfo_t, uc_offset_t); typedef struct { size_t count; uint8_t *entries; + size_t capacity; uc_ehranges_t ehranges; struct { uc_variables_t variables; diff --git a/include/ucode/types.h b/include/ucode/types.h index cb2d9d07..3c2e107f 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -125,6 +125,7 @@ typedef struct { size_t count; uc_value_t *proto; uc_value_t **entries; + size_t capacity; } uc_array_t; /* `key` is a string value, NULL in place of a deleted entry */ diff --git a/include/ucode/util.h b/include/ucode/util.h index a9a1ef91..af1f2611 100644 --- a/include/ucode/util.h +++ b/include/ucode/util.h @@ -249,10 +249,14 @@ uc_set_free(uc_set_t *set) #define UC_VECTOR_INIT_SIZE 8 +/* `capacity` is the number of entries allocated. Code that sets `entries` + * itself leaves it as it is: NULL entries are allocated anew, and a capacity + * of 0 next to allocated entries causes a reallocation at the next extend. */ #define uc_declare_vector(name, type) \ typedef struct { \ size_t count; \ type *entries; \ + size_t capacity; \ } name static inline size_t @@ -270,7 +274,8 @@ uc_vector_capacity(size_t init, size_t count) } static inline void -uc_vector_reduce_init_(char **base, size_t itemsize, size_t init, size_t count, size_t remove) +uc_vector_reduce_init_(char **base, size_t *capacity, size_t itemsize, size_t init, + size_t count, size_t remove) { if (*base == NULL) return; @@ -280,19 +285,23 @@ uc_vector_reduce_init_(char **base, size_t itemsize, size_t init, size_t count, size_t next_capacity = uc_vector_capacity(init, count - remove); - if (uc_vector_capacity(next_capacity, count) != next_capacity) - *base = (__typeof__(*base))xrealloc(*base, itemsize * next_capacity); + if (*capacity && next_capacity >= *capacity) + return; + + *base = (__typeof__(*base))xrealloc(*base, itemsize * next_capacity); + *capacity = next_capacity; } static inline void * -uc_vector_extend_init_(char **base, size_t itemsize, size_t init, size_t count, size_t add) +uc_vector_extend_init_(char **base, size_t *capacity, size_t itemsize, size_t init, + size_t count, size_t add) { - size_t curr_capacity = uc_vector_capacity(init, count); - - if (*base == NULL || count + add >= curr_capacity) { - size_t next_capacity = uc_vector_capacity(curr_capacity, count + add); + if (*base == NULL || count + add >= *capacity) { + size_t next_capacity = uc_vector_capacity((*base && *capacity) ? *capacity : init, + count + add); *base = (__typeof__(*base))xrealloc(*base, itemsize * next_capacity); + *capacity = next_capacity; memset(*base + itemsize * count, 0, itemsize * (next_capacity - count)); @@ -302,24 +311,24 @@ uc_vector_extend_init_(char **base, size_t itemsize, size_t init, size_t count, } static inline void -uc_vector_reduce_(char **base, size_t itemsize, size_t count, size_t remove) +uc_vector_reduce_(char **base, size_t *capacity, size_t itemsize, size_t count, size_t remove) { - uc_vector_reduce_init_(base, itemsize, UC_VECTOR_INIT_SIZE, count, remove); + uc_vector_reduce_init_(base, capacity, itemsize, UC_VECTOR_INIT_SIZE, count, remove); } static inline void * -uc_vector_extend_(char **base, size_t itemsize, size_t count, size_t add) +uc_vector_extend_(char **base, size_t *capacity, size_t itemsize, size_t count, size_t add) { - return uc_vector_extend_init_(base, itemsize, UC_VECTOR_INIT_SIZE, count, add); + return uc_vector_extend_init_(base, capacity, itemsize, UC_VECTOR_INIT_SIZE, count, add); } #define uc_vector_reduce_init(vec, init, remove) \ - uc_vector_reduce_init_((char **)&(vec)->entries, \ + uc_vector_reduce_init_((char **)&(vec)->entries, &(vec)->capacity, \ sizeof((vec)->entries[0]), (init), (vec)->count, (remove)) #define uc_vector_extend_init(vec, init, add) \ (__typeof__((vec)->entries + 0)) uc_vector_extend_init_( \ - (char **)&(vec)->entries, \ + (char **)&(vec)->entries, &(vec)->capacity, \ sizeof((vec)->entries[0]), \ (init), (vec)->count, (add)) @@ -337,6 +346,7 @@ uc_vector_extend_(char **base, size_t itemsize, size_t count, size_t add) free((vec)->entries); \ (vec)->entries = NULL; \ (vec)->count = 0; \ + (vec)->capacity = 0; \ } while(0) #define uc_vector_first(vec) \ diff --git a/lib/debug.c b/lib/debug.c index 3687e8f3..30984ac5 100644 --- a/lib/debug.c +++ b/lib/debug.c @@ -2670,7 +2670,7 @@ lookup_stmt_boundary(uc_vm_t *vm, location_t *loc, insn_span_t *sp) if (!lookup_function(vm, loc)) return false; - struct { insn_span_t *entries; size_t count; } sp_stack = { 0 }; + struct { insn_span_t *entries; size_t count, capacity; } sp_stack = { 0 }; uc_chunk_t *chunk = &loc->function->chunk; uc_offsetinfo_t *offsets = &chunk->debuginfo.offsets; size_t bytes = loc->function->srcpos; @@ -2762,7 +2762,7 @@ next_parent(uc_vm_t *vm, uc_function_t **fnp) static bool find_statement_boundaries(uc_function_t *fn, uint8_t *ip, size_t depth, insn_span_t *sp) { - struct { insn_span_t *entries; size_t count; } sp_stack = { 0 }; + struct { insn_span_t *entries; size_t count, capacity; } sp_stack = { 0 }; uc_offsetinfo_t *offsets = &fn->chunk.debuginfo.offsets; size_t off = ip - fn->chunk.entries; size_t i = 0, bytes = 0, insns = 0; diff --git a/lib/ffi/ffi.c b/lib/ffi/ffi.c index 24232efa..5211cabd 100644 --- a/lib/ffi/ffi.c +++ b/lib/ffi/ffi.c @@ -1112,6 +1112,7 @@ typedef struct { typedef struct { path_token *entries; size_t count; + size_t capacity; } path_tokens; /* Free path tokens */ @@ -2327,16 +2328,19 @@ uc_ctype_call(uc_vm_t *vm, size_t nargs) struct { size_t count; ffi_type **entries; + size_t capacity; } argtypes = { 0 }; struct { size_t count; void **entries; + size_t capacity; } argvalues = { 0 }; struct { size_t count; void **entries; + size_t capacity; } argmem = { 0 }; uc_value_t *rv = NULL; diff --git a/lib/ffi/uc_cparse.h b/lib/ffi/uc_cparse.h index e9664c08..805a8736 100644 --- a/lib/ffi/uc_cparse.h +++ b/lib/ffi/uc_cparse.h @@ -66,7 +66,7 @@ typedef struct CPState { uc_value_t *uv_str; uc_value_t **uv_param; char *error; - struct cp_func_ids_buf { size_t count; CTypeID *entries; } func_ids_buf; /* Optional buffer to collect function type IDs */ + struct cp_func_ids_buf { size_t count; CTypeID *entries; size_t capacity; } func_ids_buf; /* Optional buffer to collect function type IDs */ struct cp_func_ids_buf *func_ids; /* Pointer to func_ids_buf or external buffer */ } CPState; diff --git a/lib/ffi/uc_ctype.h b/lib/ffi/uc_ctype.h index db2bf494..4da39e95 100644 --- a/lib/ffi/uc_ctype.h +++ b/lib/ffi/uc_ctype.h @@ -185,6 +185,7 @@ typedef struct CType { typedef struct { CTypeID1 *entries; size_t count; + size_t capacity; } vcbid_t; /* C type state. */ @@ -193,6 +194,7 @@ typedef struct CTState { struct { size_t count; CType *entries; + size_t capacity; } vtab; vcbid_t vcbid; /* Callback type table (used by ffi.import) */ uc_vm_t *vm; diff --git a/lib/socket.c b/lib/socket.c index fe27e859..39eb2d35 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -2868,7 +2868,7 @@ uc_socket_addrinfo(uc_vm_t *vm, size_t nargs) static uc_value_t * uc_socket_poll(uc_vm_t *vm, size_t nargs) { - struct { struct pollfd *entries; size_t count; } pfds = { 0 }; + struct { struct pollfd *entries; size_t count, capacity; } pfds = { 0 }; uc_value_t *timeoutarg, *rv, *item; int64_t timeout; int ret; @@ -2984,8 +2984,8 @@ uc_socket_connect(uc_vm_t *vm, size_t nargs) int fd; } *ap; - struct { struct address *entries; size_t count; } addresses = { 0 }; - struct { struct pollfd *entries; size_t count; } pollfds = { 0 }; + struct { struct address *entries; size_t count, capacity; } addresses = { 0 }; + struct { struct pollfd *entries; size_t count, capacity; } pollfds = { 0 }; struct addrinfo *ai_results, *ai_hints, *ai; uc_value_t *host, *serv, *hints, *timeout; const char *errmsg = NULL; @@ -3998,7 +3998,7 @@ estimate_cmsg_size(uc_value_t *uv) static bool encode_cmsg(uc_vm_t *vm, uc_value_t *uv, struct cmsghdr *cmsg) { - struct { int *entries; size_t count; } fds = { 0 }; + struct { int *entries; size_t count, capacity; } fds = { 0 }; void *dataptr = NULL; socklen_t datasz = 0; char *st = NULL; From 6a0c376ddd40ce6a167f5e42ef24d3c50e0751a3 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Thu, 29 Oct 2026 08:08:17 +0100 Subject: [PATCH 07/11] vm: split the bytecode independent VM core into vmcore.c A runtime for native programs must share the VM state handling with the interpreter without containing the interpreter, the compiler or the bytecode format. Move the code that reads no bytecode from vm.c to vmcore.c: VM setup and teardown, globals, signals, the stack, registry, exceptions and stack traces, value, iterator, property and container operations, global variable access, closure and module operations, and uc_vm_call(). The parts that depend on a bytecode interpreter or a compiler are reached through hooks that the respective code registers when it is linked: - uc_vm_call() enters bytecode closures through an interpreter hook, which vm.c installs from a constructor, and native closures through a hook of the native runtime. - The loaders compile source through a function that the compiler registers; without it, loading source fails as with COMPILE_SUPPORT off. A runtime can register scripts that it contains, which the loaders take before the file system, and their compile errors. - A runtime can register a function that marks the values it holds outside the VM during a collection. The bytecode reader and writer move to bytecode.c, so that a runtime can leave them out. A static program that uses libucode.a must reference a function of vm.o, for example uc_vm_execute(), to run bytecode closures through uc_vm_call(). Signed-off-by: John Crispin --- CMakeLists.txt | 2 +- bytecode.c | 890 +++++++++ compiler.c | 6 + include/ucode/internal/lib.h | 28 + include/ucode/internal/types.h | 7 + include/ucode/internal/vmcore.h | 150 ++ lib.c | 156 +- program.c | 862 --------- types.c | 3 + vm.c | 3002 +++---------------------------- vmcore.c | 2736 ++++++++++++++++++++++++++++ 11 files changed, 4239 insertions(+), 3603 deletions(-) create mode 100644 bytecode.c create mode 100644 include/ucode/internal/vmcore.h create mode 100644 vmcore.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 19edaaca..3ae6e157 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -234,7 +234,7 @@ else() include(FindPkgConfig) endif() -set(UCODE_SOURCES lexer.c lib.c vm.c chunk.c vallist.c compiler.c source.c types.c json.c program.c platform.c cli.c) +set(UCODE_SOURCES lexer.c lib.c vm.c vmcore.c chunk.c vallist.c compiler.c source.c types.c json.c program.c bytecode.c platform.c cli.c) if(UCODE_WASM) add_library(libucode STATIC ${UCODE_SOURCES}) set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode) diff --git a/bytecode.c b/bytecode.c new file mode 100644 index 00000000..ced6090a --- /dev/null +++ b/bytecode.c @@ -0,0 +1,890 @@ +/* + * Copyright (C) 2022 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* Reading and writing of programs as bytecode. A runtime without bytecode + * leaves this file out. */ + +#include +#include +#include + +#include "ucode/internal/program.h" +#include "ucode/internal/source.h" +#include "ucode/internal/chunk.h" +#include "ucode/internal/platform.h" + + +static void +write_u16(size_t value, FILE *file) +{ + uint16_t n; + + if (sizeof(value) > sizeof(n)) + assert(value <= UINT16_MAX); + + n = htobe16((uint16_t)value); + + fwrite(&n, 1, sizeof(n), file); +} + +static void +write_u32(size_t value, FILE *file) +{ + uint32_t n; + + if (sizeof(value) > sizeof(n)) + assert(value <= UINT32_MAX); + + n = htobe32((uint32_t)value); + + fwrite(&n, 1, sizeof(n), file); +} + +static void +write_u64(uint64_t value, FILE *file) +{ + uint64_t n; + + if (sizeof(value) > sizeof(n)) + assert(value <= UINT64_MAX); + + n = htobe64((uint64_t)value); + + fwrite(&n, 1, sizeof(n), file); +} + +static void +_write_vector(size_t count, size_t itemsize, void *data, FILE *file) +{ + size_t pad = (~(count * itemsize) + 1) & (sizeof(uint32_t) - 1); + char z[sizeof(uint32_t) - 1] = { 0 }; + + write_u32(count, file); + fwrite(data, itemsize, count, file); + fwrite(z, 1, pad, file); +} + +#define write_vector(vec, file) \ + _write_vector((vec)->count, sizeof((vec)->entries[0]), (vec)->entries, file) + +#define write_string(str, file) \ + _write_vector(strlen(str) + 1, 1, str, file) + +static void +write_vallist(uc_value_list_t *vallist, FILE *file) +{ + size_t i; + + /* write index */ + write_u32(vallist->isize, file); + + for (i = 0; i < vallist->isize; i++) + write_u64(vallist->index[i], file); + + /* write data */ + write_u32(vallist->dsize, file); + + if (vallist->dsize) + fwrite(vallist->data, 1, vallist->dsize, file); +} + +enum { + UC_PROGRAM_F_DEBUG = (1 << 0), + UC_PROGRAM_F_SOURCEINFO = (1 << 1), + UC_PROGRAM_F_SOURCEBUF = (1 << 2), + UC_PROGRAM_F_EXPORTS = (1 << 3), +}; + +enum { + UC_FUNCTION_F_IS_ARROW = (1 << 0), + UC_FUNCTION_F_IS_VARARG = (1 << 1), + UC_FUNCTION_F_IS_STRICT = (1 << 2), + UC_FUNCTION_F_HAS_EXCEPTIONS = (1 << 3), + UC_FUNCTION_F_HAS_NAME = (1 << 4), + UC_FUNCTION_F_HAS_VARDBG = (1 << 5), + UC_FUNCTION_F_HAS_OFFSETDBG = (1 << 6), + UC_FUNCTION_F_IS_MODULE = (1 << 7), +}; + +static void +write_chunk(uc_chunk_t *chunk, FILE *file, uint32_t flags) +{ + size_t i, slot; + + /* write bytecode data */ + write_vector(chunk, file); + + /* write exception ranges */ + if (flags & UC_FUNCTION_F_HAS_EXCEPTIONS) { + write_u32(chunk->ehranges.count, file); + + for (i = 0; i < chunk->ehranges.count; i++) { + write_u32(chunk->ehranges.entries[i].from, file); + write_u32(chunk->ehranges.entries[i].to, file); + write_u32(chunk->ehranges.entries[i].target, file); + write_u32(chunk->ehranges.entries[i].slot, file); + } + } + + /* write variable info */ + if (flags & UC_FUNCTION_F_HAS_VARDBG) { + write_u32(chunk->debuginfo.variables.count, file); + + for (i = 0; i < chunk->debuginfo.variables.count; i++) { + slot = chunk->debuginfo.variables.entries[i].slot; + + if (slot >= ((size_t)-1 / 2)) + slot = ((uint32_t)-1 / 2) + (slot - ((size_t)-1 / 2)); + + write_u32(chunk->debuginfo.variables.entries[i].from, file); + write_u32(chunk->debuginfo.variables.entries[i].to, file); + write_u32(slot, file); + write_u32(chunk->debuginfo.variables.entries[i].nameidx, file); + } + + write_vallist(&chunk->debuginfo.varnames, file); + } + + /* write offset info */ + if (flags & UC_FUNCTION_F_HAS_OFFSETDBG) + write_vector(&chunk->debuginfo.offsets, file); +} + +static void +write_function(uc_function_t *func, FILE *file, bool debug) +{ + uint32_t flags = 0; + + if (func->arrow) + flags |= UC_FUNCTION_F_IS_ARROW; + + if (func->vararg) + flags |= UC_FUNCTION_F_IS_VARARG; + + if (func->strict) + flags |= UC_FUNCTION_F_IS_STRICT; + + if (func->module) + flags |= UC_FUNCTION_F_IS_MODULE; + + if (func->chunk.ehranges.count) + flags |= UC_FUNCTION_F_HAS_EXCEPTIONS; + + if (debug && func->name[0]) + flags |= UC_FUNCTION_F_HAS_NAME; + + if (debug && func->chunk.debuginfo.variables.count) + flags |= UC_FUNCTION_F_HAS_VARDBG; + + if (debug && func->chunk.debuginfo.offsets.count) + flags |= UC_FUNCTION_F_HAS_OFFSETDBG; + + write_u32(flags, file); + + if (flags & UC_FUNCTION_F_HAS_NAME) + write_string(func->name, file); + + write_u16(func->nargs, file); + write_u16(func->nupvals, file); + write_u32(func->srcidx, file); + write_u32(func->srcpos, file); + + write_chunk(&func->chunk, file, flags); +} + +static bool +write_exports(FILE *file, uc_source_t *source, uint32_t flags, const char *subj) +{ + size_t i, num_exports = source->exports.count - 1; + uc_value_t *sym; + + if (flags & UC_PROGRAM_F_EXPORTS) + { + /* write export count */ + write_u32(num_exports, file); + + /* write each export name */ + for (i = 1; i <= num_exports; i++) + { + sym = source->exports.entries[i]; + + if (ucv_type(sym) == UC_STRING) + _write_vector(ucv_string_length(sym) + 1, 1, + ucv_string_get(sym), file); + else if (ucv_type(sym) == UC_NULL) + write_u32(0xffffffff, file); + else + assert(0); + } + } + + return true; +} + +void +uc_program_write(uc_program_t *prog, FILE *file, bool debug) +{ + /* the version this program requires, the oldest one which can run it unless + * the compiler recorded a newer one, see uc_program_require_version() */ + uint32_t flags = (prog->version & 0xff) << 24; + size_t i = 0; + + if (debug) + flags |= UC_PROGRAM_F_DEBUG; + + if (debug && prog->sources.count) + flags |= UC_PROGRAM_F_SOURCEINFO; + + if (prog->sources.count && prog->sources.entries[0]->exports.count) + flags |= UC_PROGRAM_F_EXPORTS; + + /* magic word + flags */ + write_u32(UC_PRECOMPILED_BYTECODE_MAGIC, file); + write_u32(flags, file); + + /* write source information */ + if (flags & UC_PROGRAM_F_SOURCEINFO) { + write_u32(prog->sources.count, file); + + for (i = 0; i < prog->sources.count; i++) { + /* write source file name */ + write_string(prog->sources.entries[i]->filename, file); + + /* include source buffer if program was compiled from stdin */ + if (prog->sources.entries[i]->buffer) + write_string(prog->sources.entries[i]->buffer, file); + else + //write_string("", file); + write_u32(0, file); + + /* write lineinfo data */ + write_vector(&prog->sources.entries[i]->lineinfo, file); + } + } + + /* write constants */ + write_vallist(&prog->constants, file); + + /* write export symbols */ + write_exports(file, prog->sources.entries[0], flags, "exports"); + + /* write program sections */ + i = 0; + uc_program_function_foreach(prog, fn1) { + (void)fn1; + i++; + } + + write_u32(i, file); + + uc_program_function_foreach(prog, fn2) + write_function(fn2, file, debug); +} + +static bool +read_error(uc_source_t *in, char **errp, const char *subject, size_t rlen, size_t len) +{ + const char *reason; + + if (!errno) + reason = "Premature EOF"; + else + reason = strerror(errno); + + if (errp) + xasprintf(errp, + "%s while reading %s at offset %zu, got %zu of %zu bytes\n", + reason, subject, uc_source_offset(in) - rlen, rlen, len); + + return false; +} + +static bool +skip_padding(uc_source_t *in, size_t len, char **errp) +{ + size_t pad = (~len + 1) & (sizeof(uint32_t) - 1), rlen; + char buf[sizeof(uint32_t) - 1]; + + if (pad != 0) { + rlen = uc_source_read(in, buf, pad); + + if (rlen != pad) + return read_error(in, errp, "padding", rlen, pad); + } + + return true; +} + +static bool +read_u32(uc_source_t *in, uint32_t *n, const char *subj, char **errp) +{ + size_t rlen = uc_source_read(in, n, sizeof(*n)); + + if (rlen != sizeof(*n)) { + *n = 0; + + return read_error(in, errp, subj ? subj : "uint32_t", rlen, sizeof(*n)); + } + + *n = be32toh(*n); + + return true; +} + +static bool +read_u64(uc_source_t *in, uint64_t *n, const char *subj, char **errp) +{ + size_t rlen = uc_source_read(in, n, sizeof(*n)); + + if (rlen != sizeof(*n)) { + *n = 0; + + return read_error(in, errp, subj ? subj : "uint64_t", rlen, sizeof(*n)); + } + + *n = be64toh(*n); + + return true; +} + +static bool +read_size_t(uc_source_t *in, size_t *n, size_t size, const char *subj, char **errp); + +/* A count or length taken from the file sizes an allocation, and every item + * it counts is then read from that same file, so a value larger than the + * bytes remaining cannot be honest. Refusing it here keeps a crafted 32-bit + * field from reaching xalloc(), which aborts the process rather than + * returning. */ +static bool +length_fits(uc_source_t *in, size_t n, const char *subj, char **errp) +{ + if (n > uc_source_remaining(in)) { + if (errp) + xasprintf(errp, "Invalid length %zu for %s at offset %zu\n", + n, subj, uc_source_offset(in)); + + return false; + } + + return true; +} + +static bool +read_count(uc_source_t *in, size_t *n, size_t itemsize, const char *subj, char **errp) +{ + if (!read_size_t(in, n, sizeof(uint32_t), subj, errp)) + return false; + + if (*n > uc_source_remaining(in) / (itemsize ? itemsize : 1)) { + if (errp) + xasprintf(errp, "Invalid count %zu for %s at offset %zu\n", + *n, subj, uc_source_offset(in)); + + return false; + } + + return true; +} + +#define read_length(in, n, subj, errp) read_count(in, n, 1, subj, errp) + +static bool +read_size_t(uc_source_t *in, size_t *n, size_t size, const char *subj, char **errp) +{ + union { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; } nval; + size_t rlen; + + rlen = uc_source_read(in, &nval.u64, size); + + if (rlen != size) { + *n = 0; + + if (!subj) { + switch (size) { + case 1: subj = "uint8_t"; break; + case 2: subj = "uint16_t"; break; + case 4: subj = "uint32_t"; break; + case 8: subj = "uint64_t"; break; + } + } + + return read_error(in, errp, subj, rlen, sizeof(nval)); + } + + switch (size) { + case 1: *n = (size_t) nval.u8; break; + case 2: *n = (size_t)be16toh(nval.u16); break; + case 4: *n = (size_t)be32toh(nval.u32); break; + case 8: *n = (size_t)be64toh(nval.u64); break; + } + + return true; +} + +static bool +_read_vector(uc_source_t *in, void *ptr, size_t itemsize, const char *subj, char **errp) +{ + struct { size_t count; void *data; } *vec = ptr; + size_t rlen, len; + char subjbuf[64]; + + snprintf(subjbuf, sizeof(subjbuf), "%s vector size", subj); + + if (!read_count(in, &vec->count, itemsize, subjbuf, errp)) + return false; + + vec->data = xcalloc(vec->count, itemsize); + + len = vec->count; + rlen = uc_source_read(in, vec->data, len * itemsize); + + if (rlen != len * itemsize) { + free(vec->data); + + vec->count = 0; + vec->data = NULL; + + snprintf(subjbuf, sizeof(subjbuf), "%s vector data", subj); + + return read_error(in, errp, subjbuf, rlen, len * itemsize); + } + + return skip_padding(in, vec->count * itemsize, errp); +} + +#define read_vector(in, vec, subj, errp) \ + _read_vector(in, vec, sizeof((vec)->entries[0]), subj, errp) + +static bool +read_string(uc_source_t *in, char *dst, size_t len, const char *subj, char **errp) +{ + size_t rlen; + + rlen = uc_source_read(in, dst, len); + + if (rlen != len) + return read_error(in, errp, subj, rlen, len); + + return skip_padding(in, len, errp); +} + +static bool +read_vallist(uc_source_t *in, uc_value_list_t *vallist, const char *subj, char **errp) +{ + char subjbuf[64]; + size_t i; + + /* read index */ + snprintf(subjbuf, sizeof(subjbuf), "%s index size", subj); + + if (!read_count(in, &vallist->isize, sizeof(uint64_t), subjbuf, errp)) + goto out; + + vallist->index = xcalloc(sizeof(vallist->index[0]), vallist->isize); + + for (i = 0; i < vallist->isize; i++) { + snprintf(subjbuf, sizeof(subjbuf), "%s index entry %zu of %zu", subj, i, vallist->isize); + + if (!read_u64(in, &vallist->index[i], subjbuf, errp)) + goto out; + } + + /* read data */ + snprintf(subjbuf, sizeof(subjbuf), "%s data size", subj); + + if (!read_length(in, &vallist->dsize, subjbuf, errp)) + goto out; + + vallist->data = xalloc(vallist->dsize); + + snprintf(subjbuf, sizeof(subjbuf), "%s data", subj); + + if (!read_string(in, vallist->data, vallist->dsize, subj, errp)) + goto out; + + return true; + +out: + free(vallist->index); + free(vallist->data); + + vallist->isize = 0; + vallist->index = NULL; + + vallist->dsize = 0; + vallist->data = NULL; + + return false; +} + +static bool +read_exports(uc_source_t *in, uc_source_t *source, uint32_t flags, const char *subj, char **errp) +{ + size_t i, num_exports, len; + uc_stringbuf_t *buf; + uc_value_t *symname; + char subjbuf[64]; + + if (flags & UC_PROGRAM_F_EXPORTS) + { + /* read export count */ + snprintf(subjbuf, sizeof(subjbuf), "%s count", subj); + + if (!read_count(in, &num_exports, sizeof(uint32_t), subjbuf, errp)) + return false; + + /* read export symbol names */ + for (i = 0; i < num_exports; i++) + { + snprintf(subjbuf, sizeof(subjbuf), "%s entry %zu of %zu size", + subj, i, num_exports); + + if (!read_size_t(in, &len, sizeof(uint32_t), subjbuf, errp)) + return false; + + if (len < 0xffffffff) { + snprintf(subjbuf, sizeof(subjbuf), "%s entry %zu of %zu name", + subj, i, num_exports); + + if (!length_fits(in, len, subjbuf, errp)) + return false; + + buf = ucv_stringbuf_new(); + + if (uc_stringbuf_memset(buf, -1, 0, len + 1)) { + uc_stringbuf_free(buf); + + if (errp) + xasprintf(errp, "Out of memory reading %s\n", subjbuf); + + return false; + } + + if (!read_string(in, buf->buf + buf->bpos - len - 1, len, subjbuf, errp)) + { + uc_stringbuf_free(buf); + return false; + } + + symname = ucv_stringbuf_finish(buf); + } + else { + symname = NULL; + } + + uc_source_export_add(source, symname); + ucv_put(symname); + } + } + + return true; +} + +static uc_source_t * +read_sourceinfo(uc_source_t *input, uint32_t flags, char **errp, uc_program_t *program) +{ + char *path = NULL, *code = NULL; + uc_source_t *source = NULL; + size_t len, count; + + if (flags & UC_PROGRAM_F_SOURCEINFO) { + if (!read_count(input, &count, sizeof(uint32_t) * 2, + "amount of source entries", errp)) + return NULL; + + while (count > 0) { + if (!read_length(input, &len, "sourceinfo filename length", errp)) + return NULL; + + path = xalloc(len + 1); + + if (!read_string(input, path, len, "sourceinfo filename", errp)) { + free(path); + + return NULL; + } + + if (!read_length(input, &len, "sourceinfo code buffer length", errp)) { + free(path); + + return NULL; + } + + if (len > 0) { + code = xalloc(len); + + if (!read_string(input, code, len, "sourceinfo code buffer data", errp)) { + free(code); + free(path); + + return NULL; + } + + source = uc_source_new_buffer(path, code, len); + } + else { + source = uc_source_new_path(path); + } + + if (!read_vector(input, &source->lineinfo, "sourceinfo lineinfo", errp)) { + uc_source_put(source); + free(path); + + return NULL; + } + + uc_source_runpath_set(source, input->runpath); + uc_vector_push(&program->sources, source); + + free(path); + + count--; + } + } + else { + source = uc_source_new_buffer("[no source]", xstrdup(""), 0); + + uc_source_runpath_set(source, input->runpath); + uc_vector_push(&program->sources, source); + } + + return source; +} + +static bool +read_chunk(uc_source_t *in, uc_chunk_t *chunk, uint32_t flags, const char *subj, char **errp) +{ + uc_varrange_t *varrange; + uc_ehrange_t *ehrange; + char subjbuf[192]; + size_t i; + + /* read bytecode data */ + snprintf(subjbuf, sizeof(subjbuf), "%s byte code", subj); + + if (!read_vector(in, chunk, subjbuf, errp)) + goto out; + + /* read exception ranges */ + if (flags & UC_FUNCTION_F_HAS_EXCEPTIONS) { + snprintf(subjbuf, sizeof(subjbuf), "%s exception ranges count", subj); + + if (!read_count(in, &chunk->ehranges.count, + sizeof(uint32_t) * 4, subjbuf, errp)) + goto out; + + chunk->ehranges.entries = xcalloc( + sizeof(chunk->ehranges.entries[0]), + chunk->ehranges.count); + + for (i = 0; i < chunk->ehranges.count; i++) { + snprintf(subjbuf, sizeof(subjbuf), "%s exception range %zu of %zu offset", + subj, i, chunk->ehranges.count); + + ehrange = &chunk->ehranges.entries[i]; + + if (!read_size_t(in, &ehrange->from, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &ehrange->to, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &ehrange->target, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &ehrange->slot, sizeof(uint32_t), subjbuf, errp)) + goto out; + } + } + + /* read variable info */ + if (flags & UC_FUNCTION_F_HAS_VARDBG) { + snprintf(subjbuf, sizeof(subjbuf), "%s variable scopes count", subj); + + if (!read_count(in, &chunk->debuginfo.variables.count, + sizeof(uint32_t) * 4, subjbuf, errp)) + goto out; + + chunk->debuginfo.variables.entries = xcalloc( + sizeof(chunk->debuginfo.variables.entries[0]), + chunk->debuginfo.variables.count); + + for (i = 0; i < chunk->debuginfo.variables.count; i++) { + snprintf(subjbuf, sizeof(subjbuf), "%s variable scope %zu of %zu offset", + subj, i, chunk->debuginfo.variables.count); + + varrange = &chunk->debuginfo.variables.entries[i]; + + if (!read_size_t(in, &varrange->from, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &varrange->to, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &varrange->slot, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &varrange->nameidx, sizeof(uint32_t), subjbuf, errp)) + goto out; + + if (varrange->slot >= ((uint32_t)-1 / 2)) + varrange->slot = ((size_t)-1 / 2) + (varrange->slot - ((uint32_t)-1 / 2)); + } + + snprintf(subjbuf, sizeof(subjbuf), "%s variable names", subj); + + if (!read_vallist(in, &chunk->debuginfo.varnames, subjbuf, errp)) + goto out; + } + + /* read offset info */ + if (flags & UC_FUNCTION_F_HAS_OFFSETDBG) { + snprintf(subjbuf, sizeof(subjbuf), "%s source offsets", subj); + + if (!read_vector(in, &chunk->debuginfo.offsets, subjbuf, errp)) + goto out; + } + + return true; + +out: + uc_vallist_free(&chunk->debuginfo.varnames); + + free(chunk->entries); + free(chunk->ehranges.entries); + free(chunk->debuginfo.variables.entries); + + chunk->count = 0; + chunk->entries = NULL; + + chunk->ehranges.count = 0; + chunk->ehranges.entries = NULL; + + chunk->debuginfo.variables.count = 0; + chunk->debuginfo.variables.entries = NULL; + + return false; +} + +static bool +read_function(uc_source_t *in, uc_program_t *program, size_t idx, char **errp) +{ + size_t nargs, nupvals, srcidx, srcpos, namelen = 0; + char subjbuf[64], *name = NULL; + uc_function_t *func = NULL; + uc_source_t *source; + uint32_t flags; + + snprintf(subjbuf, sizeof(subjbuf), "function #%zu flags", idx); + + if (!read_u32(in, &flags, subjbuf, errp)) + goto out; + + if (flags & UC_FUNCTION_F_HAS_NAME) { + snprintf(subjbuf, sizeof(subjbuf), "function #%zu name length", idx); + + if (!read_length(in, &namelen, subjbuf, errp)) + goto out; + + name = xalloc(namelen + 1); + + snprintf(subjbuf, sizeof(subjbuf), "function #%zu name", idx); + + if (!read_string(in, name, namelen, subjbuf, errp)) + goto out; + } + + snprintf(subjbuf, sizeof(subjbuf), "function #%zu (%s) arg count and offset", idx, name ? name : "-"); + + if (!read_size_t(in, &nargs, sizeof(uint16_t), subjbuf, errp) || + !read_size_t(in, &nupvals, sizeof(uint16_t), subjbuf, errp) || + !read_size_t(in, &srcidx, sizeof(uint32_t), subjbuf, errp) || + !read_size_t(in, &srcpos, sizeof(uint32_t), subjbuf, errp)) { + goto out; + } + + // FIXME + if (srcidx < program->sources.count) + source = program->sources.entries[srcidx]; + else + source = program->sources.entries[0]; + + func = (uc_function_t *)uc_program_function_new(program, name, source, srcpos); + func->arrow = (flags & UC_FUNCTION_F_IS_ARROW); + func->vararg = (flags & UC_FUNCTION_F_IS_VARARG); + func->strict = (flags & UC_FUNCTION_F_IS_STRICT); + func->module = (flags & UC_FUNCTION_F_IS_MODULE); + func->nargs = nargs; + func->nupvals = nupvals; + + snprintf(subjbuf, sizeof(subjbuf), "function #%zu (%s) body", idx, name ? name : "-"); + + if (!read_chunk(in, &func->chunk, flags, subjbuf, errp)) + goto out; + + free(name); + + return true; + +out: + free(name); + + return false; +} + +uc_program_t * +uc_program_load(uc_source_t *input, char **errp) +{ + uc_program_t *program = NULL; + uint32_t flags, i; + size_t nfuncs; + + if (!read_u32(input, &i, "file magic", errp)) + goto out; + + if (i != UC_PRECOMPILED_BYTECODE_MAGIC) { + xasprintf(errp, "Invalid file magic\n"); + goto out; + } + + if (!read_u32(input, &flags, "program flags", errp)) + goto out; + + if (!uc_bytecode_version_supported(flags >> 24)) { + xasprintf(errp, + "Bytecode version mismatch, got 0x%02x, expected 0x%02x to 0x%02x\n", + flags >> 24, UCODE_BYTECODE_VERSION_MIN, UCODE_BYTECODE_VERSION); + goto out; + } + + program = uc_program_new(); + + /* keep the version the bytecode came with, so that writing it out again + * does not raise it beyond what the program actually uses */ + uc_program_require_version(program, flags >> 24); + + if (!read_sourceinfo(input, flags, errp, program)) + goto out; + + if (!read_vallist(input, &program->constants, "constants", errp)) + goto out; + + if (!read_exports(input, program->sources.entries[0], flags, + "exports", errp)) + goto out; + + if (!read_count(input, &nfuncs, sizeof(uint32_t), "function count", errp)) + goto out; + + for (i = 0; i < nfuncs; i++) + if (!read_function(input, program, i, errp)) + goto out; + + return program; + +out: + uc_program_put(program); + + return NULL; +} diff --git a/compiler.c b/compiler.c index 3aea7be4..fa91dca9 100644 --- a/compiler.c +++ b/compiler.c @@ -5108,3 +5108,9 @@ uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp) return prog; } + +static void __attribute__((constructor)) +uc_compiler_register(void) +{ + uc_loader_compile_set(uc_compile); +} diff --git a/include/ucode/internal/lib.h b/include/ucode/internal/lib.h index 85e4b495..a18f1846 100644 --- a/include/ucode/internal/lib.h +++ b/include/ucode/internal/lib.h @@ -135,4 +135,32 @@ __hidden void uc_error_message_indent(char **msg); __hidden uc_value_t *uc_require_library(uc_vm_t *vm, uc_value_t *nameval, bool module_mode); +/* Appends the path that the search path entry `path_template` gives for module + * `name` to `buf`, as require() looks it up. Returns the file extension of the + * entry, or NULL if the entry or the name are not usable. */ +__hidden const char *uc_require_path_expand(const char *path_template, const char *name, + uc_stringbuf_t *buf); + +__hidden char *uc_include_path(const char *curpath, const char *incpath); + +/* Normalises `path` lexically to an absolute path, without resolving links, + * so that it also names a file which does not exist. */ +__hidden char *uc_path_normalize(const char *path); + +/* The compiler registers itself through uc_loader_compile_set(), so that a + * runtime linking this file without the compiler has no compiler. */ +typedef uc_program_t *(*uc_loader_compile_t)(uc_parse_config_t *, uc_source_t *, char **); + +__hidden void uc_loader_compile_set(uc_loader_compile_t compile); + +/* Scripts that a native program contains: the loaders find them by path + * before they look at the file system. */ +typedef struct { + uc_value_t *(*unit_closure)(uc_vm_t *vm, const char *path, bool raw_mode, bool module_mode); + char *(*unit_path)(const char *path); + char *(*source_path)(const char *runpath); +} uc_loader_units_t; + +__hidden void uc_loader_units_set(const uc_loader_units_t *units); + #endif /* UCODE_INTERNAL_LIB_H */ diff --git a/include/ucode/internal/types.h b/include/ucode/internal/types.h index d9cd9577..96886c7b 100644 --- a/include/ucode/internal/types.h +++ b/include/ucode/internal/types.h @@ -102,6 +102,9 @@ struct uc_function { size_t srcpos; uc_chunk_t chunk; struct uc_program *program; + /* the native code of a function of a native program, which has no + * bytecode */ + struct uc_native_function_info *native; char name[]; }; @@ -158,6 +161,10 @@ typedef struct { /* Object iteration */ uc_list_t object_iterators; + + /* Marks the values that a runtime holds outside of the VM state, such + * as the locals of native code */ + void (*gc_roots_mark)(uc_vm_t *vm, void (*mark)(uc_value_t *)); } uc_thread_context_t; __hidden uc_thread_context_t *uc_thread_context_get(void); diff --git a/include/ucode/internal/vmcore.h b/include/ucode/internal/vmcore.h new file mode 100644 index 00000000..b310ba8c --- /dev/null +++ b/include/ucode/internal/vmcore.h @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2020-2021 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* VM operations that do not depend on bytecode. Operands that the interpreter + * decodes from the instruction stream are passed as parameters. */ + +#ifndef UCODE_INTERNAL_VMCORE_H +#define UCODE_INTERNAL_VMCORE_H + +#include "ucode/internal/vm.h" +#include "ucode/internal/program.h" +#include "ucode/internal/util.h" + +extern const char *exception_type_strings[]; + +typedef char uc_vm_main_key_t[sizeof(UC_PROGRAM_MAIN_KEY) + sizeof("0x") + 2 * sizeof(void *)]; + +typedef struct { + void (*frame_push)(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, size_t stackoff); + uc_vm_status_t (*execute)(uc_vm_t *vm); +} uc_vm_interpreter_t; + +/* Runs a closure of native code that uc_vm_call() found on the stack with its + * arguments, and leaves its result there as the interpreter does. */ +typedef void (*uc_vm_native_call_t)(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, size_t nargs); + +static inline uc_chunk_t * +uc_vm_frame_chunk(uc_callframe_t *frame) +{ + return frame->closure ? &frame->closure->function->chunk : NULL; +} + +static inline uc_program_t * +uc_vm_frame_program(uc_callframe_t *frame) +{ + return frame->closure ? frame->closure->function->program : NULL; +} + +static inline uc_source_t * +uc_vm_frame_source(uc_callframe_t *frame) +{ + return frame->closure ? uc_program_function_source(frame->closure->function) : NULL; +} + +static inline uc_callframe_t * +uc_vm_current_frame(uc_vm_t *vm) +{ + return uc_vector_last(&vm->callframes); +} + +static inline uc_program_t * +uc_vm_current_program(uc_vm_t *vm) +{ + return uc_vm_frame_program(uc_vm_current_frame(vm)); +} + +__hidden const char *uc_vm_insn_to_name(uc_vm_insn_t insn); + +__hidden void uc_vm_reset_callframes(uc_vm_t *vm, size_t depth); + +__hidden char *uc_vm_format_val(uc_vm_t *vm, uc_value_t *val); +__hidden void uc_vm_frame_dump(uc_vm_t *vm, uc_callframe_t *frame); + +__hidden void uc_vm_stack_set(uc_vm_t *vm, size_t offset, uc_value_t *value); + +__hidden void uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall, size_t nargs); +__hidden uc_value_t *uc_vm_callframe_pop(uc_vm_t *vm); + +__hidden void uc_vm_run_begin(uc_closure_t *closure, bool keep); +__hidden void uc_vm_run_end(uc_closure_t *closure); + +__hidden char *uc_vm_callee_expr(uc_vm_t *vm); +__hidden void uc_vm_clear_exception(uc_vm_t *vm); + +__hidden void uc_vm_close_upvals(uc_vm_t *vm, size_t slot); + +__hidden void uc_vm_gc_step(uc_vm_t *vm); + +__hidden uc_value_t *uc_vm_value_arith(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand); +__hidden uc_value_t *uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand); +__hidden uc_value_t *uc_vm_value_unary(uc_vm_insn_t operation, uc_value_t *value); +__hidden uc_value_t *uc_vm_value_complement(uc_value_t *value); +__hidden bool uc_vm_value_compare(uc_vm_insn_t operation, uc_value_t *v1, uc_value_t *v2); +__hidden bool uc_vm_value_equality(uc_vm_insn_t operation, uc_value_t *v1, uc_value_t *v2); +__hidden bool uc_vm_value_in(uc_value_t *r1, uc_value_t *r2); +__hidden bool uc_vm_value_not(uc_value_t *value); + +__hidden bool uc_vm_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, uc_value_t *v, + uc_value_t **state, uc_value_t **key, uc_value_t **val); + +__hidden bool uc_vm_property_get(uc_vm_t *vm, uc_value_t *v, uc_value_t *k, uc_value_t **res); +__hidden bool uc_vm_property_set(uc_vm_t *vm, uc_value_t *o, uc_value_t *k, uc_value_t *v, uc_value_t **res); +__hidden bool uc_vm_property_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *v, + uc_value_t *k, uc_value_t *inc, uc_value_t **res); +__hidden bool uc_vm_property_delete(uc_vm_t *vm, uc_value_t *v, uc_value_t *k, uc_value_t **res); + +__hidden void uc_vm_array_append(uc_value_t *arr, uc_value_t **values, size_t count); +__hidden bool uc_vm_array_spread(uc_vm_t *vm, uc_value_t *dst, uc_value_t *src); +__hidden uc_value_t *uc_vm_object_new(uc_vm_t *vm, size_t nkeys); +__hidden void uc_vm_object_init_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *k, uc_value_t *v); +__hidden bool uc_vm_object_spread(uc_vm_t *vm, uc_value_t *dst, uc_value_t *src); + +__hidden bool uc_vm_spread_push(uc_vm_t *vm, size_t base, size_t nargs, size_t from, size_t spread); +__hidden void uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from); + +__hidden uc_value_t *uc_vm_global_get(uc_vm_t *vm, uc_value_t *name, bool strict); +__hidden bool uc_vm_global_set(uc_vm_t *vm, uc_value_t *name, uc_value_t *v, bool strict); +__hidden bool uc_vm_global_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *name, + uc_value_t *inc, bool strict, uc_value_t **res); + +__hidden const char *uc_vm_main_key(uc_program_t *program, uc_vm_main_key_t key); + +__hidden uc_upvalref_t *uc_vm_capture_upval(uc_vm_t *vm, size_t slot); + +__hidden bool uc_vm_regexp_load(uc_vm_t *vm, uc_value_t *jstr, uc_value_t **re); +__hidden void uc_vm_closure_upval_set(uc_vm_t *vm, uc_closure_t *closure, size_t idx, int32_t uv); +__hidden void uc_vm_stcall(uc_vm_t *vm, size_t slot, size_t nargs); +__hidden void uc_vm_value_print(uc_vm_t *vm, uc_value_t *v); + +__hidden void uc_vm_import(uc_vm_t *vm, size_t from, size_t to); +__hidden uc_value_t *uc_vm_import_namespace(uc_vm_t *vm, size_t from, const uint32_t *names, size_t nnames); +__hidden uc_modexports_t *uc_vm_export_target(uc_vm_t *vm); +__hidden void uc_vm_export_add(uc_modexports_t *exports, uc_upvalref_t *ref); +__hidden uc_value_t *uc_vm_dynload_module(uc_vm_t *vm, uc_value_t *name); +__hidden uc_value_t *uc_vm_dynload_namespace(uc_vm_t *vm, uc_value_t *modscope); +__hidden bool uc_vm_dynload_bind(uc_vm_t *vm, uc_value_t *modscope, uint32_t cidx, size_t to); + +__hidden void uc_vm_interpreter_set(const uc_vm_interpreter_t *interpreter); +__hidden void uc_vm_native_call_set(uc_vm_native_call_t call); +__hidden uc_value_t *uc_vm_resolve_upval(uc_vm_t *vm, uc_value_t *value); +__hidden bool uc_vm_call_depth_check(uc_vm_t *vm, bool tail); +__hidden bool uc_vm_call_dispatch(uc_vm_t *vm, uc_value_t **ctx, uc_value_t **fno, bool mcall, size_t nargs); +__hidden void uc_vm_call_args_adjust(uc_vm_t *vm, uc_function_t *function, size_t nargs); + +#endif /* UCODE_INTERNAL_VMCORE_H */ diff --git a/lib.c b/lib.c index 6b676ec2..8edc2eed 100644 --- a/lib.c +++ b/lib.c @@ -3021,14 +3021,73 @@ uc_require_imports(uc_vm_t *vm, uc_value_t *closure) return ns; } +static uc_loader_compile_t uc_loader_compile; +static const uc_loader_units_t *uc_loader_units; + +void +uc_loader_compile_set(uc_loader_compile_t compile) +{ + uc_loader_compile = compile; +} + +void +uc_loader_units_set(const uc_loader_units_t *units) +{ + uc_loader_units = units; +} + +char * +uc_path_normalize(const char *path) +{ + char *cwd = NULL, *joined, *res, *seg, *save = NULL, *end; + + if (*path != '/') { + cwd = getcwd(NULL, 0); + + if (!cwd) + return NULL; + } + + xasprintf(&joined, "%s/%s", cwd ? cwd : "", path); + free(cwd); + + res = xalloc(strlen(joined) + 2); + end = res; + + for (seg = strtok_r(joined, "/", &save); seg; seg = strtok_r(NULL, "/", &save)) { + if (!strcmp(seg, ".")) + continue; + + if (!strcmp(seg, "..")) { + while (end > res && *--end != '/') + ; + + *end = 0; + continue; + } + + end += sprintf(end, "/%s", seg); + } + + if (end == res) + strcpy(res, "/"); + + free(joined); + + return res; +} + static bool uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **res, bool raw_mode, bool module_mode) { uc_parse_config_t config = *vm->config, *prev_config = vm->config; - uc_value_t *closure; + uc_value_t *closure = NULL; struct stat st; - if (stat(path, &st)) + if (uc_loader_units) + closure = uc_loader_units->unit_closure(vm, path, raw_mode, module_mode); + + if (!closure && stat(path, &st)) return false; config.raw_mode = raw_mode; @@ -3037,7 +3096,8 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** uc_vm_stack_push(vm, ucv_string_new(path)); - closure = uc_loadfile(vm, 1); + if (!closure) + closure = uc_loadfile(vm, 1); /* the module body runs with this configuration; a file that it loads is * not a module */ @@ -3068,25 +3128,16 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** return true; } -static bool -uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, - uc_value_t **res, bool module_mode) +const char * +uc_require_path_expand(const char *path_template, const char *name, + uc_stringbuf_t *buf) { - uc_stringbuf_t *buf = uc_stringbuf_new(); const char *p, *q, *last; - uc_value_t *modtable; - bool rv; - - modtable = ucv_property_get(uc_vm_scope_get(vm), "modules"); - *res = ucv_get(ucv_object_get(modtable, name, &rv)); - - if (rv) - goto out; p = strchr(path_template, '*'); if (!p) - goto out; + return NULL; ucv_stringbuf_addstr(buf, path_template, p - path_template); @@ -3105,13 +3156,36 @@ uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, last = q + 1; } else if (!isalnum(*q) && *q != '_') { - goto out; + return NULL; } } - if (!strcmp(p + 1, ".so")) + return p + 1; +} + +static bool +uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, + uc_value_t **res, bool module_mode) +{ + uc_stringbuf_t *buf = uc_stringbuf_new(); + uc_value_t *modtable; + const char *ext; + bool rv; + + modtable = ucv_property_get(uc_vm_scope_get(vm), "modules"); + *res = ucv_get(ucv_object_get(modtable, name, &rv)); + + if (rv) + goto out; + + ext = uc_require_path_expand(path_template, name, buf); + + if (!ext) + goto out; + + if (!strcmp(ext, ".so")) rv = uc_require_so(vm, buf->buf, res); - else if (!strcmp(p + 1, ".uc")) + else if (!strcmp(ext, ".uc")) rv = uc_require_ucode(vm, buf->buf, NULL, res, true, module_mode); if (rv && vm->exception.type == EXCEPTION_NONE) @@ -3985,28 +4059,29 @@ uc_json(uc_vm_t *vm, size_t nargs) return rv; } -static char * -include_path(const char *curpath, const char *incpath) +char * +uc_include_path(const char *curpath, const char *incpath) { const char *slash; char *dup, *res; - int len; - if (*incpath == '/') - return realpath(incpath, NULL); - - slash = curpath ? strrchr(curpath, '/') : NULL; - - if (slash) - len = asprintf(&res, "%.*s/%s", (int)(slash - curpath), curpath, incpath); - else - len = asprintf(&res, "./%s", incpath); + if (*incpath == '/') { + res = xstrdup(incpath); + } + else { + slash = curpath ? strrchr(curpath, '/') : NULL; - if (len == -1) - return NULL; + if (slash) + xasprintf(&res, "%.*s/%s", (int)(slash - curpath), curpath, incpath); + else + xasprintf(&res, "./%s", incpath); + } dup = realpath(res, NULL); + if (!dup && uc_loader_units) + dup = uc_loader_units->unit_path(res); + free(res); return dup; @@ -4047,7 +4122,7 @@ uc_include_common(uc_vm_t *vm, size_t nargs, bool raw_mode) if (!closure) return NULL; - p = include_path(uc_program_function_source(closure->function)->runpath, ucv_string_get(path)); + p = uc_include_path(uc_program_function_source(closure->function)->runpath, ucv_string_get(path)); if (!p) { uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, @@ -4800,6 +4875,10 @@ uc_sourcepath(uc_vm_t *vm, size_t nargs) } path = realpath(uc_program_function_source(frame->closure->function)->runpath, NULL); + + if (!path && uc_loader_units) + path = uc_loader_units->source_path(uc_program_function_source(frame->closure->function)->runpath); + break; } @@ -5860,7 +5939,14 @@ uc_load_common(uc_vm_t *vm, size_t nargs, uc_source_t *source, bool is_file) uc_compile_parse_config(&conf, uc_fn_arg(1)); - program = uc_compile(&conf, source, &err); + if (uc_loader_compile) { + program = uc_loader_compile(&conf, source, &err); + } + else { + program = NULL; + xasprintf(&err, "Source code compilation not supported\n"); + } + closure = program ? uc_program_main(vm, program) : NULL; uc_program_put(program); diff --git a/program.c b/program.c index 747d7c07..d769885a 100644 --- a/program.c +++ b/program.c @@ -180,868 +180,6 @@ uc_program_add_constant(uc_program_t *prog, uc_value_t *val) return uc_vallist_add(&prog->constants, val); } -static void -write_u16(size_t value, FILE *file) -{ - uint16_t n; - - if (sizeof(value) > sizeof(n)) - assert(value <= UINT16_MAX); - - n = htobe16((uint16_t)value); - - fwrite(&n, 1, sizeof(n), file); -} - -static void -write_u32(size_t value, FILE *file) -{ - uint32_t n; - - if (sizeof(value) > sizeof(n)) - assert(value <= UINT32_MAX); - - n = htobe32((uint32_t)value); - - fwrite(&n, 1, sizeof(n), file); -} - -static void -write_u64(uint64_t value, FILE *file) -{ - uint64_t n; - - if (sizeof(value) > sizeof(n)) - assert(value <= UINT64_MAX); - - n = htobe64((uint64_t)value); - - fwrite(&n, 1, sizeof(n), file); -} - -static void -_write_vector(size_t count, size_t itemsize, void *data, FILE *file) -{ - size_t pad = (~(count * itemsize) + 1) & (sizeof(uint32_t) - 1); - char z[sizeof(uint32_t) - 1] = { 0 }; - - write_u32(count, file); - fwrite(data, itemsize, count, file); - fwrite(z, 1, pad, file); -} - -#define write_vector(vec, file) \ - _write_vector((vec)->count, sizeof((vec)->entries[0]), (vec)->entries, file) - -#define write_string(str, file) \ - _write_vector(strlen(str) + 1, 1, str, file) - -static void -write_vallist(uc_value_list_t *vallist, FILE *file) -{ - size_t i; - - /* write index */ - write_u32(vallist->isize, file); - - for (i = 0; i < vallist->isize; i++) - write_u64(vallist->index[i], file); - - /* write data */ - write_u32(vallist->dsize, file); - - if (vallist->dsize) - fwrite(vallist->data, 1, vallist->dsize, file); -} - -enum { - UC_PROGRAM_F_DEBUG = (1 << 0), - UC_PROGRAM_F_SOURCEINFO = (1 << 1), - UC_PROGRAM_F_SOURCEBUF = (1 << 2), - UC_PROGRAM_F_EXPORTS = (1 << 3), -}; - -enum { - UC_FUNCTION_F_IS_ARROW = (1 << 0), - UC_FUNCTION_F_IS_VARARG = (1 << 1), - UC_FUNCTION_F_IS_STRICT = (1 << 2), - UC_FUNCTION_F_HAS_EXCEPTIONS = (1 << 3), - UC_FUNCTION_F_HAS_NAME = (1 << 4), - UC_FUNCTION_F_HAS_VARDBG = (1 << 5), - UC_FUNCTION_F_HAS_OFFSETDBG = (1 << 6), - UC_FUNCTION_F_IS_MODULE = (1 << 7), -}; - -static void -write_chunk(uc_chunk_t *chunk, FILE *file, uint32_t flags) -{ - size_t i, slot; - - /* write bytecode data */ - write_vector(chunk, file); - - /* write exception ranges */ - if (flags & UC_FUNCTION_F_HAS_EXCEPTIONS) { - write_u32(chunk->ehranges.count, file); - - for (i = 0; i < chunk->ehranges.count; i++) { - write_u32(chunk->ehranges.entries[i].from, file); - write_u32(chunk->ehranges.entries[i].to, file); - write_u32(chunk->ehranges.entries[i].target, file); - write_u32(chunk->ehranges.entries[i].slot, file); - } - } - - /* write variable info */ - if (flags & UC_FUNCTION_F_HAS_VARDBG) { - write_u32(chunk->debuginfo.variables.count, file); - - for (i = 0; i < chunk->debuginfo.variables.count; i++) { - slot = chunk->debuginfo.variables.entries[i].slot; - - if (slot >= ((size_t)-1 / 2)) - slot = ((uint32_t)-1 / 2) + (slot - ((size_t)-1 / 2)); - - write_u32(chunk->debuginfo.variables.entries[i].from, file); - write_u32(chunk->debuginfo.variables.entries[i].to, file); - write_u32(slot, file); - write_u32(chunk->debuginfo.variables.entries[i].nameidx, file); - } - - write_vallist(&chunk->debuginfo.varnames, file); - } - - /* write offset info */ - if (flags & UC_FUNCTION_F_HAS_OFFSETDBG) - write_vector(&chunk->debuginfo.offsets, file); -} - -static void -write_function(uc_function_t *func, FILE *file, bool debug) -{ - uint32_t flags = 0; - - if (func->arrow) - flags |= UC_FUNCTION_F_IS_ARROW; - - if (func->vararg) - flags |= UC_FUNCTION_F_IS_VARARG; - - if (func->strict) - flags |= UC_FUNCTION_F_IS_STRICT; - - if (func->module) - flags |= UC_FUNCTION_F_IS_MODULE; - - if (func->chunk.ehranges.count) - flags |= UC_FUNCTION_F_HAS_EXCEPTIONS; - - if (debug && func->name[0]) - flags |= UC_FUNCTION_F_HAS_NAME; - - if (debug && func->chunk.debuginfo.variables.count) - flags |= UC_FUNCTION_F_HAS_VARDBG; - - if (debug && func->chunk.debuginfo.offsets.count) - flags |= UC_FUNCTION_F_HAS_OFFSETDBG; - - write_u32(flags, file); - - if (flags & UC_FUNCTION_F_HAS_NAME) - write_string(func->name, file); - - write_u16(func->nargs, file); - write_u16(func->nupvals, file); - write_u32(func->srcidx, file); - write_u32(func->srcpos, file); - - write_chunk(&func->chunk, file, flags); -} - -static bool -write_exports(FILE *file, uc_source_t *source, uint32_t flags, const char *subj) -{ - size_t i, num_exports = source->exports.count - 1; - uc_value_t *sym; - - if (flags & UC_PROGRAM_F_EXPORTS) - { - /* write export count */ - write_u32(num_exports, file); - - /* write each export name */ - for (i = 1; i <= num_exports; i++) - { - sym = source->exports.entries[i]; - - if (ucv_type(sym) == UC_STRING) - _write_vector(ucv_string_length(sym) + 1, 1, - ucv_string_get(sym), file); - else if (ucv_type(sym) == UC_NULL) - write_u32(0xffffffff, file); - else - assert(0); - } - } - - return true; -} - -void -uc_program_write(uc_program_t *prog, FILE *file, bool debug) -{ - /* the version this program requires, the oldest one which can run it unless - * the compiler recorded a newer one, see uc_program_require_version() */ - uint32_t flags = (prog->version & 0xff) << 24; - size_t i = 0; - - if (debug) - flags |= UC_PROGRAM_F_DEBUG; - - if (debug && prog->sources.count) - flags |= UC_PROGRAM_F_SOURCEINFO; - - if (prog->sources.count && prog->sources.entries[0]->exports.count) - flags |= UC_PROGRAM_F_EXPORTS; - - /* magic word + flags */ - write_u32(UC_PRECOMPILED_BYTECODE_MAGIC, file); - write_u32(flags, file); - - /* write source information */ - if (flags & UC_PROGRAM_F_SOURCEINFO) { - write_u32(prog->sources.count, file); - - for (i = 0; i < prog->sources.count; i++) { - /* write source file name */ - write_string(prog->sources.entries[i]->filename, file); - - /* include source buffer if program was compiled from stdin */ - if (prog->sources.entries[i]->buffer) - write_string(prog->sources.entries[i]->buffer, file); - else - //write_string("", file); - write_u32(0, file); - - /* write lineinfo data */ - write_vector(&prog->sources.entries[i]->lineinfo, file); - } - } - - /* write constants */ - write_vallist(&prog->constants, file); - - /* write export symbols */ - write_exports(file, prog->sources.entries[0], flags, "exports"); - - /* write program sections */ - i = 0; - uc_program_function_foreach(prog, fn1) { - (void)fn1; - i++; - } - - write_u32(i, file); - - uc_program_function_foreach(prog, fn2) - write_function(fn2, file, debug); -} - -static bool -read_error(uc_source_t *in, char **errp, const char *subject, size_t rlen, size_t len) -{ - const char *reason; - - if (!errno) - reason = "Premature EOF"; - else - reason = strerror(errno); - - if (errp) - xasprintf(errp, - "%s while reading %s at offset %zu, got %zu of %zu bytes\n", - reason, subject, uc_source_offset(in) - rlen, rlen, len); - - return false; -} - -static bool -skip_padding(uc_source_t *in, size_t len, char **errp) -{ - size_t pad = (~len + 1) & (sizeof(uint32_t) - 1), rlen; - char buf[sizeof(uint32_t) - 1]; - - if (pad != 0) { - rlen = uc_source_read(in, buf, pad); - - if (rlen != pad) - return read_error(in, errp, "padding", rlen, pad); - } - - return true; -} - -static bool -read_u32(uc_source_t *in, uint32_t *n, const char *subj, char **errp) -{ - size_t rlen = uc_source_read(in, n, sizeof(*n)); - - if (rlen != sizeof(*n)) { - *n = 0; - - return read_error(in, errp, subj ? subj : "uint32_t", rlen, sizeof(*n)); - } - - *n = be32toh(*n); - - return true; -} - -static bool -read_u64(uc_source_t *in, uint64_t *n, const char *subj, char **errp) -{ - size_t rlen = uc_source_read(in, n, sizeof(*n)); - - if (rlen != sizeof(*n)) { - *n = 0; - - return read_error(in, errp, subj ? subj : "uint64_t", rlen, sizeof(*n)); - } - - *n = be64toh(*n); - - return true; -} - -static bool -read_size_t(uc_source_t *in, size_t *n, size_t size, const char *subj, char **errp); - -/* A count or length taken from the file sizes an allocation, and every item - * it counts is then read from that same file, so a value larger than the - * bytes remaining cannot be honest. Refusing it here keeps a crafted 32-bit - * field from reaching xalloc(), which aborts the process rather than - * returning. */ -static bool -length_fits(uc_source_t *in, size_t n, const char *subj, char **errp) -{ - if (n > uc_source_remaining(in)) { - if (errp) - xasprintf(errp, "Invalid length %zu for %s at offset %zu\n", - n, subj, uc_source_offset(in)); - - return false; - } - - return true; -} - -static bool -read_count(uc_source_t *in, size_t *n, size_t itemsize, const char *subj, char **errp) -{ - if (!read_size_t(in, n, sizeof(uint32_t), subj, errp)) - return false; - - if (*n > uc_source_remaining(in) / (itemsize ? itemsize : 1)) { - if (errp) - xasprintf(errp, "Invalid count %zu for %s at offset %zu\n", - *n, subj, uc_source_offset(in)); - - return false; - } - - return true; -} - -#define read_length(in, n, subj, errp) read_count(in, n, 1, subj, errp) - -static bool -read_size_t(uc_source_t *in, size_t *n, size_t size, const char *subj, char **errp) -{ - union { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; } nval; - size_t rlen; - - rlen = uc_source_read(in, &nval.u64, size); - - if (rlen != size) { - *n = 0; - - if (!subj) { - switch (size) { - case 1: subj = "uint8_t"; break; - case 2: subj = "uint16_t"; break; - case 4: subj = "uint32_t"; break; - case 8: subj = "uint64_t"; break; - } - } - - return read_error(in, errp, subj, rlen, sizeof(nval)); - } - - switch (size) { - case 1: *n = (size_t) nval.u8; break; - case 2: *n = (size_t)be16toh(nval.u16); break; - case 4: *n = (size_t)be32toh(nval.u32); break; - case 8: *n = (size_t)be64toh(nval.u64); break; - } - - return true; -} - -static bool -_read_vector(uc_source_t *in, void *ptr, size_t itemsize, const char *subj, char **errp) -{ - struct { size_t count; void *data; } *vec = ptr; - size_t rlen, len; - char subjbuf[64]; - - snprintf(subjbuf, sizeof(subjbuf), "%s vector size", subj); - - if (!read_count(in, &vec->count, itemsize, subjbuf, errp)) - return false; - - vec->data = xcalloc(vec->count, itemsize); - - len = vec->count; - rlen = uc_source_read(in, vec->data, len * itemsize); - - if (rlen != len * itemsize) { - free(vec->data); - - vec->count = 0; - vec->data = NULL; - - snprintf(subjbuf, sizeof(subjbuf), "%s vector data", subj); - - return read_error(in, errp, subjbuf, rlen, len * itemsize); - } - - return skip_padding(in, vec->count * itemsize, errp); -} - -#define read_vector(in, vec, subj, errp) \ - _read_vector(in, vec, sizeof((vec)->entries[0]), subj, errp) - -static bool -read_string(uc_source_t *in, char *dst, size_t len, const char *subj, char **errp) -{ - size_t rlen; - - rlen = uc_source_read(in, dst, len); - - if (rlen != len) - return read_error(in, errp, subj, rlen, len); - - return skip_padding(in, len, errp); -} - -static bool -read_vallist(uc_source_t *in, uc_value_list_t *vallist, const char *subj, char **errp) -{ - char subjbuf[64]; - size_t i; - - /* read index */ - snprintf(subjbuf, sizeof(subjbuf), "%s index size", subj); - - if (!read_count(in, &vallist->isize, sizeof(uint64_t), subjbuf, errp)) - goto out; - - vallist->index = xcalloc(sizeof(vallist->index[0]), vallist->isize); - - for (i = 0; i < vallist->isize; i++) { - snprintf(subjbuf, sizeof(subjbuf), "%s index entry %zu of %zu", subj, i, vallist->isize); - - if (!read_u64(in, &vallist->index[i], subjbuf, errp)) - goto out; - } - - /* read data */ - snprintf(subjbuf, sizeof(subjbuf), "%s data size", subj); - - if (!read_length(in, &vallist->dsize, subjbuf, errp)) - goto out; - - vallist->data = xalloc(vallist->dsize); - - snprintf(subjbuf, sizeof(subjbuf), "%s data", subj); - - if (!read_string(in, vallist->data, vallist->dsize, subj, errp)) - goto out; - - return true; - -out: - free(vallist->index); - free(vallist->data); - - vallist->isize = 0; - vallist->index = NULL; - - vallist->dsize = 0; - vallist->data = NULL; - - return false; -} - -static bool -read_exports(uc_source_t *in, uc_source_t *source, uint32_t flags, const char *subj, char **errp) -{ - size_t i, num_exports, len; - uc_stringbuf_t *buf; - uc_value_t *symname; - char subjbuf[64]; - - if (flags & UC_PROGRAM_F_EXPORTS) - { - /* read export count */ - snprintf(subjbuf, sizeof(subjbuf), "%s count", subj); - - if (!read_count(in, &num_exports, sizeof(uint32_t), subjbuf, errp)) - return false; - - /* read export symbol names */ - for (i = 0; i < num_exports; i++) - { - snprintf(subjbuf, sizeof(subjbuf), "%s entry %zu of %zu size", - subj, i, num_exports); - - if (!read_size_t(in, &len, sizeof(uint32_t), subjbuf, errp)) - return false; - - if (len < 0xffffffff) { - snprintf(subjbuf, sizeof(subjbuf), "%s entry %zu of %zu name", - subj, i, num_exports); - - if (!length_fits(in, len, subjbuf, errp)) - return false; - - buf = ucv_stringbuf_new(); - - if (uc_stringbuf_memset(buf, -1, 0, len + 1)) { - uc_stringbuf_free(buf); - - if (errp) - xasprintf(errp, "Out of memory reading %s\n", subjbuf); - - return false; - } - - if (!read_string(in, buf->buf + buf->bpos - len - 1, len, subjbuf, errp)) - { - uc_stringbuf_free(buf); - return false; - } - - symname = ucv_stringbuf_finish(buf); - } - else { - symname = NULL; - } - - uc_source_export_add(source, symname); - ucv_put(symname); - } - } - - return true; -} - -static uc_source_t * -read_sourceinfo(uc_source_t *input, uint32_t flags, char **errp, uc_program_t *program) -{ - char *path = NULL, *code = NULL; - uc_source_t *source = NULL; - size_t len, count; - - if (flags & UC_PROGRAM_F_SOURCEINFO) { - if (!read_count(input, &count, sizeof(uint32_t) * 2, - "amount of source entries", errp)) - return NULL; - - while (count > 0) { - if (!read_length(input, &len, "sourceinfo filename length", errp)) - return NULL; - - path = xalloc(len + 1); - - if (!read_string(input, path, len, "sourceinfo filename", errp)) { - free(path); - - return NULL; - } - - if (!read_length(input, &len, "sourceinfo code buffer length", errp)) { - free(path); - - return NULL; - } - - if (len > 0) { - code = xalloc(len); - - if (!read_string(input, code, len, "sourceinfo code buffer data", errp)) { - free(code); - free(path); - - return NULL; - } - - source = uc_source_new_buffer(path, code, len); - } - else { - source = uc_source_new_path(path); - } - - if (!read_vector(input, &source->lineinfo, "sourceinfo lineinfo", errp)) { - uc_source_put(source); - free(path); - - return NULL; - } - - uc_source_runpath_set(source, input->runpath); - uc_vector_push(&program->sources, source); - - free(path); - - count--; - } - } - else { - source = uc_source_new_buffer("[no source]", xstrdup(""), 0); - - uc_source_runpath_set(source, input->runpath); - uc_vector_push(&program->sources, source); - } - - return source; -} - -static bool -read_chunk(uc_source_t *in, uc_chunk_t *chunk, uint32_t flags, const char *subj, char **errp) -{ - uc_varrange_t *varrange; - uc_ehrange_t *ehrange; - char subjbuf[192]; - size_t i; - - /* read bytecode data */ - snprintf(subjbuf, sizeof(subjbuf), "%s byte code", subj); - - if (!read_vector(in, chunk, subjbuf, errp)) - goto out; - - /* read exception ranges */ - if (flags & UC_FUNCTION_F_HAS_EXCEPTIONS) { - snprintf(subjbuf, sizeof(subjbuf), "%s exception ranges count", subj); - - if (!read_count(in, &chunk->ehranges.count, - sizeof(uint32_t) * 4, subjbuf, errp)) - goto out; - - chunk->ehranges.entries = xcalloc( - sizeof(chunk->ehranges.entries[0]), - chunk->ehranges.count); - - for (i = 0; i < chunk->ehranges.count; i++) { - snprintf(subjbuf, sizeof(subjbuf), "%s exception range %zu of %zu offset", - subj, i, chunk->ehranges.count); - - ehrange = &chunk->ehranges.entries[i]; - - if (!read_size_t(in, &ehrange->from, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &ehrange->to, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &ehrange->target, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &ehrange->slot, sizeof(uint32_t), subjbuf, errp)) - goto out; - } - } - - /* read variable info */ - if (flags & UC_FUNCTION_F_HAS_VARDBG) { - snprintf(subjbuf, sizeof(subjbuf), "%s variable scopes count", subj); - - if (!read_count(in, &chunk->debuginfo.variables.count, - sizeof(uint32_t) * 4, subjbuf, errp)) - goto out; - - chunk->debuginfo.variables.entries = xcalloc( - sizeof(chunk->debuginfo.variables.entries[0]), - chunk->debuginfo.variables.count); - - for (i = 0; i < chunk->debuginfo.variables.count; i++) { - snprintf(subjbuf, sizeof(subjbuf), "%s variable scope %zu of %zu offset", - subj, i, chunk->debuginfo.variables.count); - - varrange = &chunk->debuginfo.variables.entries[i]; - - if (!read_size_t(in, &varrange->from, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &varrange->to, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &varrange->slot, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &varrange->nameidx, sizeof(uint32_t), subjbuf, errp)) - goto out; - - if (varrange->slot >= ((uint32_t)-1 / 2)) - varrange->slot = ((size_t)-1 / 2) + (varrange->slot - ((uint32_t)-1 / 2)); - } - - snprintf(subjbuf, sizeof(subjbuf), "%s variable names", subj); - - if (!read_vallist(in, &chunk->debuginfo.varnames, subjbuf, errp)) - goto out; - } - - /* read offset info */ - if (flags & UC_FUNCTION_F_HAS_OFFSETDBG) { - snprintf(subjbuf, sizeof(subjbuf), "%s source offsets", subj); - - if (!read_vector(in, &chunk->debuginfo.offsets, subjbuf, errp)) - goto out; - } - - return true; - -out: - uc_vallist_free(&chunk->debuginfo.varnames); - - free(chunk->entries); - free(chunk->ehranges.entries); - free(chunk->debuginfo.variables.entries); - - chunk->count = 0; - chunk->entries = NULL; - - chunk->ehranges.count = 0; - chunk->ehranges.entries = NULL; - - chunk->debuginfo.variables.count = 0; - chunk->debuginfo.variables.entries = NULL; - - return false; -} - -static bool -read_function(uc_source_t *in, uc_program_t *program, size_t idx, char **errp) -{ - size_t nargs, nupvals, srcidx, srcpos, namelen = 0; - char subjbuf[64], *name = NULL; - uc_function_t *func = NULL; - uc_source_t *source; - uint32_t flags; - - snprintf(subjbuf, sizeof(subjbuf), "function #%zu flags", idx); - - if (!read_u32(in, &flags, subjbuf, errp)) - goto out; - - if (flags & UC_FUNCTION_F_HAS_NAME) { - snprintf(subjbuf, sizeof(subjbuf), "function #%zu name length", idx); - - if (!read_length(in, &namelen, subjbuf, errp)) - goto out; - - name = xalloc(namelen + 1); - - snprintf(subjbuf, sizeof(subjbuf), "function #%zu name", idx); - - if (!read_string(in, name, namelen, subjbuf, errp)) - goto out; - } - - snprintf(subjbuf, sizeof(subjbuf), "function #%zu (%s) arg count and offset", idx, name ? name : "-"); - - if (!read_size_t(in, &nargs, sizeof(uint16_t), subjbuf, errp) || - !read_size_t(in, &nupvals, sizeof(uint16_t), subjbuf, errp) || - !read_size_t(in, &srcidx, sizeof(uint32_t), subjbuf, errp) || - !read_size_t(in, &srcpos, sizeof(uint32_t), subjbuf, errp)) { - goto out; - } - - // FIXME - if (srcidx < program->sources.count) - source = program->sources.entries[srcidx]; - else - source = program->sources.entries[0]; - - func = (uc_function_t *)uc_program_function_new(program, name, source, srcpos); - func->arrow = (flags & UC_FUNCTION_F_IS_ARROW); - func->vararg = (flags & UC_FUNCTION_F_IS_VARARG); - func->strict = (flags & UC_FUNCTION_F_IS_STRICT); - func->module = (flags & UC_FUNCTION_F_IS_MODULE); - func->nargs = nargs; - func->nupvals = nupvals; - - snprintf(subjbuf, sizeof(subjbuf), "function #%zu (%s) body", idx, name ? name : "-"); - - if (!read_chunk(in, &func->chunk, flags, subjbuf, errp)) - goto out; - - free(name); - - return true; - -out: - free(name); - - return false; -} - -uc_program_t * -uc_program_load(uc_source_t *input, char **errp) -{ - uc_program_t *program = NULL; - uint32_t flags, i; - size_t nfuncs; - - if (!read_u32(input, &i, "file magic", errp)) - goto out; - - if (i != UC_PRECOMPILED_BYTECODE_MAGIC) { - xasprintf(errp, "Invalid file magic\n"); - goto out; - } - - if (!read_u32(input, &flags, "program flags", errp)) - goto out; - - if (!uc_bytecode_version_supported(flags >> 24)) { - xasprintf(errp, - "Bytecode version mismatch, got 0x%02x, expected 0x%02x to 0x%02x\n", - flags >> 24, UCODE_BYTECODE_VERSION_MIN, UCODE_BYTECODE_VERSION); - goto out; - } - - program = uc_program_new(); - - /* keep the version the bytecode came with, so that writing it out again - * does not raise it beyond what the program actually uses */ - uc_program_require_version(program, flags >> 24); - - if (!read_sourceinfo(input, flags, errp, program)) - goto out; - - if (!read_vallist(input, &program->constants, "constants", errp)) - goto out; - - if (!read_exports(input, program->sources.entries[0], flags, - "exports", errp)) - goto out; - - if (!read_count(input, &nfuncs, sizeof(uint32_t), "function count", errp)) - goto out; - - for (i = 0; i < nfuncs; i++) - if (!read_function(input, program, i, errp)) - goto out; - - return program; - -out: - uc_program_put(program); - - return NULL; -} - uc_function_t * uc_program_entry(uc_program_t *program) { diff --git a/types.c b/types.c index 2f05e34c..49443066 100644 --- a/types.c +++ b/types.c @@ -3478,6 +3478,9 @@ ucv_gc_common(uc_vm_t *vm, bool final) for (i = 0; i < vm->stack.count; i++) ucv_gc_mark(vm->stack.entries[i]); + if (uc_thread_context_get()->gc_roots_mark) + uc_thread_context_get()->gc_roots_mark(vm, ucv_gc_mark); + for (i = 0; i < vm->restypes.count; i++) ucv_gc_mark(vm->restypes.entries[i]->proto); diff --git a/vm.c b/vm.c index d9e7c7e6..51ce2899 100644 --- a/vm.c +++ b/vm.c @@ -22,360 +22,12 @@ #include #include -#include "ucode/internal/vm.h" /* ISA: I_*, uc_vm_insn_t, __insns */ -#include "ucode/internal/program.h" +#include "ucode/internal/vmcore.h" #include "ucode/internal/chunk.h" #include "ucode/internal/lib.h" /* uc_error_context_format() */ #include "ucode/internal/source.h" /* uc_source_callee_expr() */ #include "ucode/internal/platform.h" -/* maximum length of the value rendered into a "not a function" error message */ -#define UC_VALUE_HINT_MAXLEN 64 - -#undef __insn -#define __insn(_name) #_name, - -static const char *insn_names[__I_MAX] = { - __insns -}; - -const int8_t uc_vm_insn_format[__I_MAX] = { - [I_LOAD] = 4, - [I_LOAD8] = 1, - [I_LOAD16] = 2, - [I_LOAD32] = 4, - - [I_LREXP] = 4, - - [I_LLOC] = 4, - [I_LVAR] = 4, - [I_LUPV] = 4, - - [I_CLFN] = 4, - [I_ARFN] = 4, - - [I_SLOC] = 4, - [I_SUPV] = 4, - [I_SVAR] = 4, - - [I_ULOC] = 4, - [I_UUPV] = 4, - [I_UVAR] = 4, - [I_UVAL] = 1, - - [I_NARR] = 4, - [I_PARR] = 4, - - [I_NOBJ] = 4, - [I_SOBJ] = 4, - - [I_JMP] = -4, - [I_JMPZ] = -4, - [I_JMPNT] = 4, - - [I_COPY] = 1, - - [I_CALL] = 4, - - [I_IMPORT] = 4, - [I_EXPORT] = 4, - [I_DYNLOAD] = 4, - - [I_STCALL] = 4 -}; - -const char *exception_type_strings[] = { - [EXCEPTION_SYNTAX] = "Syntax error", - [EXCEPTION_RUNTIME] = "Runtime error", - [EXCEPTION_TYPE] = "Type error", - [EXCEPTION_REFERENCE] = "Reference error", - [EXCEPTION_USER] = "Error", - [EXCEPTION_EXIT] = "Exit" -}; - - -static const char * -uc_vm_insn_to_name(uc_vm_insn_t insn) -{ - if (insn < 0 || insn >= __I_MAX) - return "(unknown)"; - - return insn_names[insn]; -} - -int8_t -uc_vm_insn_to_argtype(uc_vm_insn_t insn) -{ - if (insn < 0 || insn >= __I_MAX) - return 0; - - return uc_vm_insn_format[insn]; -} - -static void -uc_vm_reset_stack(uc_vm_t *vm) -{ - while (vm->stack.count > 0) { - vm->stack.count--; - ucv_put(vm->stack.entries[vm->stack.count]); - vm->stack.entries[vm->stack.count] = NULL; - } -} - -static uc_value_t * -uc_vm_callframe_pop(uc_vm_t *vm); - -static void -uc_vm_close_upvals(uc_vm_t *vm, size_t slot); - -static void -uc_vm_reset_callframes(uc_vm_t *vm, size_t depth) -{ - while (vm->callframes.count > depth) - ucv_put(uc_vm_callframe_pop(vm)); -} - -static uc_value_t * -uc_vm_alloc_global_scope(uc_vm_t *vm) -{ - uc_value_t *scope, *arr; - size_t i; - - scope = ucv_object_new(vm); - - /* build default require() search path */ - arr = ucv_array_new(vm); - - for (i = 0; i < vm->config->module_search_path.count; i++) - ucv_array_push(arr, ucv_string_new(vm->config->module_search_path.entries[i])); - - /* register module related constants */ - ucv_object_add(scope, "REQUIRE_SEARCH_PATH", arr); - ucv_object_add(scope, "modules", ucv_object_new(vm)); - - /* register scope math constants */ - ucv_object_add(scope, "NaN", ucv_double_new(NAN)); - ucv_object_add(scope, "Infinity", ucv_double_new(INFINITY)); - - /* register global property */ - ucv_object_add(scope, "global", ucv_get(scope)); - - uc_vm_scope_set(vm, scope); - - return scope; -} - -static void -uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex); - -static void -uc_vm_signal_handler(int sig) -{ - uc_vm_t *vm = uc_thread_context_get()->signal_handler_vm; - - assert(vm); - - uc_vm_signal_raise(vm, sig); -} - -/* Actually wire up the self-pipe/handler array/sigaction template needed - * for ucode-level signal() callbacks to work, independent of whether the - * embedding host opted into this via config->setup_signal_handlers. Safe - * to call more than once (a no-op once already set up for this thread). */ -void -uc_vm_signal_handlers_ensure(uc_vm_t *vm) -{ - uc_thread_context_t *tctx = uc_thread_context_get(); - - if (tctx->signal_handler_vm) - return; - - if (pipe2(vm->signal.sigpipe, O_CLOEXEC | O_NONBLOCK) != 0) - return; - - vm->signal.handler = ucv_array_new_length(vm, UC_SYSTEM_SIGNAL_COUNT); - - vm->signal.sa.sa_handler = uc_vm_signal_handler; - vm->signal.sa.sa_flags = SA_RESTART | SA_ONSTACK; - sigemptyset(&vm->signal.sa.sa_mask); - - tctx->signal_handler_vm = vm; -} - -static void -uc_vm_signal_handlers_setup(uc_vm_t *vm) -{ - memset(&vm->signal, 0, sizeof(vm->signal)); - - vm->signal.sigpipe[0] = -1; - vm->signal.sigpipe[1] = -1; - - if (!vm->config->setup_signal_handlers) - return; - - uc_vm_signal_handlers_ensure(vm); -} - -static void -uc_vm_signal_handlers_reset(uc_vm_t *vm) -{ - uc_thread_context_t *tctx = uc_thread_context_get(); - struct sigaction sa = { 0 }; - size_t i, signo; - - if (vm != tctx->signal_handler_vm) - return; - - sa.sa_handler = SIG_DFL; - sigemptyset(&sa.sa_mask); - - for (signo = 0; signo < ucv_array_length(vm->signal.handler); signo++) - if (ucv_is_callable(ucv_array_get(vm->signal.handler, signo))) - sigaction(signo, &sa, NULL); - - for (i = 0; i < ARRAY_SIZE(vm->signal.sigpipe); i++) { - if (vm->signal.sigpipe[i] > STDERR_FILENO) - close(vm->signal.sigpipe[i]); - - vm->signal.sigpipe[i] = -1; - } - - tctx->signal_handler_vm = NULL; -} - -void uc_vm_break_init(uc_vm_t *vm) -{ - vm->break_requested = false; - vm->break_notifyfd[0] = -1; - vm->break_notifyfd[1] = -1; - - if (pipe2(vm->break_notifyfd, O_CLOEXEC | O_NONBLOCK) == 0) { - /* pipe created successfully */ - } -} - -void uc_vm_break_cleanup(uc_vm_t *vm) -{ - for (size_t i = 0; i < ARRAY_SIZE(vm->break_notifyfd); i++) { - if (vm->break_notifyfd[i] > STDERR_FILENO) - close(vm->break_notifyfd[i]); - vm->break_notifyfd[i] = -1; - } -} - -void uc_vm_init(uc_vm_t *vm, uc_parse_config_t *config) -{ - vm->exception.type = EXCEPTION_NONE; - vm->exception.message = NULL; - - vm->config = config ? config : &uc_default_parse_config; - - vm->open_upvals = NULL; - - vm->values.prev = &vm->values; - vm->values.next = &vm->values; - - vm->strbuf = NULL; - - vm->output = stdout; - - uc_vm_reset_stack(vm); - - uc_vm_alloc_global_scope(vm); - - uc_vm_exception_handler_set(vm, uc_vm_output_exception); - - uc_vm_trace_set(vm, 0); - - uc_vm_signal_handlers_setup(vm); - - uc_vm_break_init(vm); - - uc_thread_context_get()->refcount++; -} - -void uc_vm_free(uc_vm_t *vm) -{ - uc_thread_context_t *ctx; - uc_upvalref_t *ref; - size_t i; - - uc_vm_signal_handlers_reset(vm); - - uc_vm_break_cleanup(vm); - - ucv_put(vm->exception.stacktrace); - free(vm->exception.message); - - while (vm->open_upvals) { - ref = vm->open_upvals->next; - ucv_put(&vm->open_upvals->header); - vm->open_upvals = ref; - } - - /* Resource type prototypes are part of the VM value pool, so release the - * restype refs before the final GC; any references still held by - * GC-managed values are dropped by ucv_freeall() itself. */ - for (i = 0; i < vm->restypes.count; i++) - ucv_put(vm->restypes.entries[i]->proto); - - uc_vm_reset_callframes(vm, 0); - uc_vm_reset_stack(vm); - uc_vector_clear(&vm->stack); - uc_vector_clear(&vm->callframes); - - uc_stringbuf_free(vm->strbuf); - - ucv_freeall(vm); - - for (i = 0; i < vm->restypes.count; i++) - free(vm->restypes.entries[i]); - - uc_vector_clear(&vm->restypes); - - for (i = 0; i < vm->breakpoints.count; i++) - free(vm->breakpoints.entries[i]); - - uc_vector_clear(&vm->breakpoints); - - ctx = uc_thread_context_get(); - - assert(ctx->refcount > 0); - - if (--ctx->refcount == 0) - uc_thread_context_free(); -} - -static uc_chunk_t * -uc_vm_frame_chunk(uc_callframe_t *frame) -{ - return frame->closure ? &frame->closure->function->chunk : NULL; -} - -static uc_program_t * -uc_vm_frame_program(uc_callframe_t *frame) -{ - return frame->closure ? frame->closure->function->program : NULL; -} - -static uc_source_t * -uc_vm_frame_source(uc_callframe_t *frame) -{ - return frame->closure ? uc_program_function_source(frame->closure->function) : NULL; -} - -static uc_callframe_t * -uc_vm_current_frame(uc_vm_t *vm) -{ - return uc_vector_last(&vm->callframes); -} - -static uc_program_t * -uc_vm_current_program(uc_vm_t *vm) -{ - return uc_vm_frame_program(uc_vm_current_frame(vm)); -} - static uc_vm_insn_t uc_vm_decode_insn(uc_vm_t *vm, uc_callframe_t *frame, uc_chunk_t *chunk) { @@ -448,427 +100,111 @@ uc_vm_decode_insn(uc_vm_t *vm, uc_callframe_t *frame, uc_chunk_t *chunk) } -static char * -uc_vm_format_val(uc_vm_t *vm, uc_value_t *val) -{ - if (!vm->strbuf) - vm->strbuf = uc_stringbuf_new(); - else - uc_stringbuf_reset(vm->strbuf); - - ucv_to_stringbuf(NULL, vm->strbuf, val, true); - - if (uc_stringbuf_length(vm->strbuf) >= 64) { - uc_stringbuf_memset(vm->strbuf, 60, '.', 3); - uc_stringbuf_memset(vm->strbuf, 63, 0, 1); - } - - return vm->strbuf->buf; -} - +/* Replaces the current call frame by an invocation frame for the given closure, + * reusing the stack slots formerly occupied by the current frame. + * + * Since neither the call frame vector nor the value stack grow when invoking the + * callee, this allows unbounded tail recursion. + * + * On entry, the stack holds the invoked function value and its arguments at the + * top, optionally preceded by the receiver value of a method invocation. */ static void -uc_vm_frame_dump(uc_vm_t *vm, uc_callframe_t *frame) +uc_vm_frame_reinit(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, size_t nargs) { - uc_chunk_t *chunk = uc_vm_frame_chunk(frame); - uc_function_t *function; - uc_closure_t *closure; - uc_upvalref_t *ref; - uc_value_t *v; - size_t i; - - fprintf(stderr, " [*] CALLFRAME[%zx]\n", - frame - vm->callframes.entries); + size_t i, blocksrc, blockdst, blocklen, blockofs; + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_value_t *oldclosure, *oldctx, *val; - fprintf(stderr, " |- stackframe %zu/%zu\n", - frame->stackframe, vm->stack.count); + /* the slot the invoking code expects the eventual return value at, which is + * the slot of the current frame's function value, or - for method calls - of + * the receiver value immediately below it */ + blockdst = frame->stackframe - (frame->mcall ? 1 : 0); - fprintf(stderr, " |- ctx %s\n", - uc_vm_format_val(vm, frame->ctx)); + /* number of stack slots occupied by the receiver, function and arguments */ + blocklen = nargs + 1 + (mcall ? 1 : 0); - if (frame->tco) - fprintf(stderr, " |- tailcall (%u callframes omitted)\n", - frame->tco); + /* slot of the invoked function value, or of the receiver value preceding it */ + blocksrc = vm->stack.count - blocklen; - if (chunk) { - closure = frame->closure; - function = closure->function; + /* the value block is relocated towards the start of the stack, so the slots + * it occupies are always read before being overwritten */ + blockofs = blocksrc - blockdst; - fprintf(stderr, " `- %zu upvalues\n", - function->nupvals); + /* close upvalues referring to the current frame's stack slots before overwriting them */ + uc_vm_close_upvals(vm, frame->stackframe); - for (i = 0; i < function->nupvals; i++) { - ref = closure->upvals[i]; - v = uc_chunk_debug_get_variable(chunk, 0, i, true); + /* remember the value references owned by the current frame */ + oldclosure = frame->closure ? &frame->closure->header : NULL; + oldctx = frame->ctx; - fprintf(stderr, " [%zu] <%p> %s ", - i, (void *)ref, uc_vm_format_val(vm, v)); + /* move receiver, function value and arguments down into the slots of the frame + * being replaced, releasing the local variable, temporary and receiver values + * formerly held by them and clearing stale slots left above the new frame */ + for (i = blockdst; i < vm->stack.count; i++) { + val = (i - blockdst < blocklen) ? vm->stack.entries[i + blockofs] : NULL; - if (!ref) { - fprintf(stderr, "{unresolved}\n"); - } - else if (ref->closed) { - fprintf(stderr, "{closed} %s\n", - uc_vm_format_val(vm, ref->value)); - } - else { - fprintf(stderr, "{open[%zu]} %s\n", - ref->slot, - uc_vm_format_val(vm, vm->stack.entries[ref->slot])); - } + /* the slot holds a value owned by the current frame unless it is part of + * the value block being relocated */ + if (i < blocksrc) + ucv_put(vm->stack.entries[i]); - ucv_put(v); - } + vm->stack.entries[i] = val; } -} -static uc_value_t * -uc_vm_resolve_upval(uc_vm_t *vm, uc_value_t *value) -{ - uc_upvalref_t *ref; - uc_value_t *rv; - -#ifdef __clang_analyzer__ - /* Clang static analyzer does not understand that ucv_type(NULL) can't - * possibly yield UC_UPVALUE. Nudge it. */ - if (value != NULL && ucv_type(value) == UC_UPVALUE) -#else - if (ucv_type(value) == UC_UPVALUE) -#endif - { - ref = ucv_as_upvalue(value); + vm->stack.count = blockdst + blocklen; - if (ref->closed) - rv = ucv_get(ref->value); - else - rv = ucv_get(vm->stack.entries[ref->slot]); + /* reinitialize the call frame in place; the receiver value of a method call + * occupies the slot below the invoked function value. the tco counter records + * that this frame replaced the tail calling one, so stack traces can indicate + * the collapsed frames */ + if (frame->closure) + uc_vm_run_end(frame->closure); - ucv_put(value); + uc_vm_run_begin(closure, false); - return rv; - } + frame->stackframe = blockdst + (mcall ? 1 : 0); + frame->cfunction = NULL; + frame->closure = closure; + frame->ctx = ctx; + frame->ip = closure->function->chunk.entries; + frame->mcall = mcall; + frame->strict = closure->function->strict; - return value; -} + /* saturate rather than wrap, so the count stays a valid (if capped) number + * of omitted frames */ + if (frame->tco < 0xffff) + frame->tco++; -void -uc_vm_stack_push(uc_vm_t *vm, uc_value_t *value) -{ - uc_vector_push(&vm->stack, uc_vm_resolve_upval(vm, value)); + if (vm->trace) + uc_vm_frame_dump(vm, frame); - if (vm->trace) { - fprintf(stderr, " [+%zd] %s\n", - vm->stack.count - 1, - uc_vm_format_val(vm, vm->stack.entries[vm->stack.count - 1])); - } + /* release the references formerly owned by the current frame */ + ucv_put(oldclosure); + ucv_put(oldctx); } -uc_value_t * -uc_vm_stack_pop(uc_vm_t *vm) +static void +uc_vm_frame_push(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, size_t stackoff) { - uc_value_t *rv; + uc_function_t *function = closure->function; + uc_callframe_t *frame; - vm->stack.count--; - rv = vm->stack.entries[vm->stack.count]; - vm->stack.entries[vm->stack.count] = NULL; - - if (vm->trace) { - fprintf(stderr, " [-%zd] %s\n", - vm->stack.count, - uc_vm_format_val(vm, rv)); - } - - return rv; -} - -uc_value_t * -uc_vm_stack_peek(uc_vm_t *vm, size_t offset) -{ - if (offset >= vm->stack.count) - return NULL; - - return vm->stack.entries[vm->stack.count + (-1 - offset)]; -} - -static void -uc_vm_stack_set(uc_vm_t *vm, size_t offset, uc_value_t *value) -{ - if (vm->trace) { - fprintf(stderr, " [!%zu] %s\n", - offset, - uc_vm_format_val(vm, value)); - } - - ucv_put(vm->stack.entries[offset]); - vm->stack.entries[offset] = value; -} - -static void -uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall, size_t nargs) -{ - uc_value_t *res = NULL; - uc_callframe_t *frame; - - /* add new callframe */ - frame = uc_vector_push(&vm->callframes, { - .stackframe = vm->stack.count - nargs - 1, - .cfunction = fptr, - .closure = NULL, - .ctx = ctx, - .mcall = mcall - }); - - if (vm->trace) - uc_vm_frame_dump(vm, frame); - - res = fptr->cfn(vm, nargs); - - /* Reset stack, check for callframe depth since an uncatched exception in managed - * code executed by fptr->cfn() could've reset the callframe stack already. */ - if (vm->callframes.count > 0) - ucv_put(uc_vm_callframe_pop(vm)); - - /* push return value */ - if (!vm->exception.type) - uc_vm_stack_push(vm, res); - else - ucv_put(res); -} - -/* The exports of the modules that a program links statically belong to the - * closure of its entry function that runs them, and live as long as that - * closure. An exported function holds the program, so exports that the program - * owned itself would keep the program alive. */ -static bool -uc_vm_closure_is_entry(uc_closure_t *closure) -{ - uc_function_t *function = closure->function; - - return function->program->functions.prev == &function->progref; -} - -static void -uc_vm_run_begin(uc_closure_t *closure, bool keep) -{ - if (uc_vm_closure_is_entry(closure)) - uc_vector_push(&closure->function->program->runs, { .closure = closure, .keep = keep }); -} - -static void -uc_vm_run_end(uc_closure_t *closure) -{ - if (uc_vm_closure_is_entry(closure)) - uc_vector_pop(&closure->function->program->runs); -} - -typedef char uc_vm_main_key_t[sizeof(UC_PROGRAM_MAIN_KEY) + sizeof("0x") + 2 * sizeof(void *)]; - -static const char * -uc_vm_main_key(uc_program_t *program, uc_vm_main_key_t key) -{ - snprintf(key, sizeof(uc_vm_main_key_t), UC_PROGRAM_MAIN_KEY "%p", (void *)program); - - return key; -} - -/* The caller of uc_vm_execute() holds the program only. Keep the closure that - * runs in the registry once it has module instances, so that a later execution - * in this VM runs the same closure and finds them. */ -static void -uc_vm_main_keep(uc_vm_t *vm, uc_program_t *program, uc_program_run_t *run) -{ - uc_vm_main_key_t key; - - if (!run->keep) - return; - - run->keep = false; - uc_vm_registry_set(vm, uc_vm_main_key(program, key), ucv_get(&run->closure->header)); -} - -static uc_modexports_t * -uc_vm_run_exports(uc_program_t *program, uc_program_run_t *run, bool create) -{ - uc_export_instance_t *instance; - - if (!run) - return NULL; - - instance = uc_program_instance_get(program, run->closure, create); - - return instance ? &instance->exports : NULL; -} - -/* Replaces the current call frame by an invocation frame for the given closure, - * reusing the stack slots formerly occupied by the current frame. - * - * Since neither the call frame vector nor the value stack grow when invoking the - * callee, this allows unbounded tail recursion. - * - * On entry, the stack holds the invoked function value and its arguments at the - * top, optionally preceded by the receiver value of a method invocation. */ -static void -uc_vm_frame_reinit(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, - bool mcall, size_t nargs) -{ - size_t i, blocksrc, blockdst, blocklen, blockofs; - uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_value_t *oldclosure, *oldctx, *val; - - /* the slot the invoking code expects the eventual return value at, which is - * the slot of the current frame's function value, or - for method calls - of - * the receiver value immediately below it */ - blockdst = frame->stackframe - (frame->mcall ? 1 : 0); - - /* number of stack slots occupied by the receiver, function and arguments */ - blocklen = nargs + 1 + (mcall ? 1 : 0); - - /* slot of the invoked function value, or of the receiver value preceding it */ - blocksrc = vm->stack.count - blocklen; - - /* the value block is relocated towards the start of the stack, so the slots - * it occupies are always read before being overwritten */ - blockofs = blocksrc - blockdst; - - /* close upvalues referring to the current frame's stack slots before overwriting them */ - uc_vm_close_upvals(vm, frame->stackframe); - - /* remember the value references owned by the current frame */ - oldclosure = frame->closure ? &frame->closure->header : NULL; - oldctx = frame->ctx; - - /* move receiver, function value and arguments down into the slots of the frame - * being replaced, releasing the local variable, temporary and receiver values - * formerly held by them and clearing stale slots left above the new frame */ - for (i = blockdst; i < vm->stack.count; i++) { - val = (i - blockdst < blocklen) ? vm->stack.entries[i + blockofs] : NULL; - - /* the slot holds a value owned by the current frame unless it is part of - * the value block being relocated */ - if (i < blocksrc) - ucv_put(vm->stack.entries[i]); - - vm->stack.entries[i] = val; - } - - vm->stack.count = blockdst + blocklen; - - /* reinitialize the call frame in place; the receiver value of a method call - * occupies the slot below the invoked function value. the tco counter records - * that this frame replaced the tail calling one, so stack traces can indicate - * the collapsed frames */ - if (frame->closure) - uc_vm_run_end(frame->closure); + frame = uc_vector_push(&vm->callframes, { + .stackframe = stackoff, + .cfunction = NULL, + .closure = closure, + .ctx = ctx, + .ip = function->chunk.entries, + .mcall = mcall, + .strict = function->strict + }); uc_vm_run_begin(closure, false); - frame->stackframe = blockdst + (mcall ? 1 : 0); - frame->cfunction = NULL; - frame->closure = closure; - frame->ctx = ctx; - frame->ip = closure->function->chunk.entries; - frame->mcall = mcall; - frame->strict = closure->function->strict; - - /* saturate rather than wrap, so the count stays a valid (if capped) number - * of omitted frames */ - if (frame->tco < 0xffff) - frame->tco++; - if (vm->trace) uc_vm_frame_dump(vm, frame); - - /* release the references formerly owned by the current frame */ - ucv_put(oldclosure); - ucv_put(oldctx); -} - -/* Returns the expression invoked by the call that the instruction the VM is - * about to execute belongs to, or NULL if it cannot be determined. The result - * is meant for error messages describing a failed call, and must be - * released using free(). */ -static char * -uc_vm_callee_expr(uc_vm_t *vm) -{ - uc_function_t *function; - uc_callframe_t *frame; - size_t insn; - - /* the topmost frame is the one performing the failing call; if it belongs - * to a native function, the call did not originate from source code */ - if (vm->callframes.count == 0) - return NULL; - - frame = &vm->callframes.entries[vm->callframes.count - 1]; - - if (!frame->closure) - return NULL; - - function = frame->closure->function; - insn = (frame->ip - uc_vm_frame_chunk(frame)->entries) - 1; - - /* a call instruction refers to the closing parenthesis of the arguments - * passed to it */ - return uc_source_callee_expr(uc_program_function_source(function), - uc_program_function_srcpos(function, insn)); -} - -/* The `nargs` arguments of a call stay at `base` of the stack while their - * expansion is pushed above them, so that the GC sees them when a tostring - * method runs. `from` and `spread` count arguments from the last one, as the - * bytecode encodes the spread positions. */ -static uc_value_t * -uc_vm_spread_arg(uc_vm_t *vm, size_t base, size_t nargs, size_t idx) -{ - return vm->stack.entries[base + nargs - 1 - idx]; -} - -static bool -uc_vm_spread_push(uc_vm_t *vm, size_t base, size_t nargs, size_t from, size_t spread) -{ - uc_value_t *arg; - size_t j; - char *s; - - /* push each preceeding non-spread value to the stack */ - for (j = from; j > spread + 1UL; j--) - uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, j - 1))); - - /* read spread value at index... */ - arg = uc_vm_spread_arg(vm, base, nargs, spread); - - /* ... ensure that it is an array type ... */ - if (ucv_type(arg) != UC_ARRAY) { - s = ucv_to_string(vm, arg); - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); - free(s); - - return false; - } - - /* ... and push each spread array value as argument to the stack */ - for (j = 0; j < ucv_array_length(arg); j++) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j))); - - return true; -} - -/* Pushes the arguments after the last spread one, then replaces the original - * arguments with the expanded ones */ -static void -uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from) -{ - size_t i, count; - - for (i = from; i > 0; i--) - uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, i - 1))); - - for (i = 0; i < nargs; i++) - ucv_put(vm->stack.entries[base + i]); - - count = vm->stack.count - base - nargs; - memmove(&vm->stack.entries[base], &vm->stack.entries[base + nargs], - count * sizeof(vm->stack.entries[0])); - vm->stack.count = base + count; } static bool @@ -878,14 +214,10 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, size_t i, stackoff, nargs = argspec & 0xffff; size_t nspreads = (argspec >> 16) & 0x7fff; uc_callframe_t *frame = NULL; - uc_function_t *function; uc_closure_t *closure; - uc_value_t *ellip; uint16_t slot, tmp; - /* a tail invocation reuses the current frame, so it never grows the stack */ - if (!tail && vm->callframes.count >= 1000) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Too much recursion"); + if (!uc_vm_call_depth_check(vm, tail)) { ucv_put(ctx); ucv_put(fno); @@ -920,93 +252,15 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, nargs = vm->stack.count - stackoff - 1; } - /* __call__ dispatch: if fno is not itself callable (not a closure or - * cfunction) but carries a __call__ metamethod somewhere in its prototype - * chain, invoke that method with the instance as `this`. The instance - * becomes the new ctx; the original ctx (if any) is discarded. The tail - * flag is preserved so `return foo()` still TCOs through the dispatch. */ - if (ucv_type(fno) != UC_CFUNCTION && ucv_type(fno) != UC_CLOSURE) { - uc_value_t *meta = ucv_metamethod_lookup(fno, "__call__"); - - if (ucv_is_callable(meta)) { - ucv_put(ctx); - - /* the instance becomes `this` and is invoked in place of the - * __call__ method, so hand the reference to ctx over */ - ctx = fno; - fno = ucv_get(meta); - } - } - - /* is a native function; such functions are always invoked in a fresh frame, - * even when requested as tail call, since the foreign code may rely on the - * stack layout of an ordinary invocation */ - if (ucv_type(fno) == UC_CFUNCTION) { - uc_vm_call_native(vm, ctx, ucv_as_cfunction(fno), mcall, nargs); + if (!uc_vm_call_dispatch(vm, &ctx, &fno, mcall, nargs)) + return false; + if (!fno) return true; - } - - if (ucv_type(fno) != UC_CLOSURE) { - /* name the invoked expression where it can be recovered from the source, - * and render the value it held as JSON, so that the message identifies - * both the offending expression and the value that was not a function; - * a NULL vm skips __tostring__ dispatch, so describing the value has no - * side effects */ - char *callee = uc_vm_callee_expr(vm); - char *hint = ucv_to_jsonstring(NULL, fno); - size_t hlen = strlen(hint); - - if (hlen > UC_VALUE_HINT_MAXLEN) - memcpy(hint + UC_VALUE_HINT_MAXLEN - 3, "...", 4); - - if (callee) - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "`%s` is not a function, got %s instead", callee, hint); - else - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "left-hand side is not a function, got %s instead", hint); - - free(callee); - free(hint); - ucv_put(ctx); - ucv_put(fno); - - return false; - } closure = ucv_as_closure(fno); - function = closure->function; - - /* fewer arguments on stack than function expects => pad */ - if (nargs < function->nargs) { - for (i = nargs; i < function->nargs; i++) { - if (function->vararg && (i + 1) == function->nargs) - uc_vm_stack_push(vm, ucv_array_new_length(vm, 0)); - else - uc_vm_stack_push(vm, NULL); - } - } - - /* more arguments on stack than function expects... */ - else if (nargs > function->nargs - function->vararg) { - /* is a vararg function => pass excess args as array */ - if (function->vararg) { - ellip = ucv_array_new_length(vm, nargs - (function->nargs - 1)); - for (i = function->nargs; i <= nargs; i++) - ucv_array_push(ellip, uc_vm_stack_peek(vm, nargs - i)); - - for (i = function->nargs; i <= nargs; i++) - uc_vm_stack_pop(vm); - - uc_vm_stack_push(vm, ellip); - } - - /* static amount of args => drop excess values */ - else { - for (i = function->nargs; i < nargs; i++) - ucv_put(uc_vm_stack_pop(vm)); - } - } + uc_vm_call_args_adjust(vm, closure->function, nargs); /* tail call: reuse the current call frame instead of pushing a new one. The * number of values above the invoked function may differ from the encoded @@ -1017,20 +271,7 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, return true; } - frame = uc_vector_push(&vm->callframes, { - .stackframe = stackoff, - .cfunction = NULL, - .closure = closure, - .ctx = ctx, - .ip = function->chunk.entries, - .mcall = mcall, - .strict = function->strict - }); - - uc_vm_run_begin(closure, false); - - if (vm->trace) - uc_vm_frame_dump(vm, frame); + uc_vm_frame_push(vm, closure, ctx, mcall, stackoff); return true; } @@ -1165,56 +406,6 @@ uc_dump_insn(uc_vm_t *vm, uint8_t *pos, uc_vm_insn_t insn) fprintf(stderr, "\n"); } -static uc_value_t * -uc_vm_exception_tostring(uc_vm_t *vm, size_t nargs) -{ - uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_value_t *message = ucv_object_get(frame->ctx, "message", NULL); - - return message ? ucv_get(message) : ucv_string_new("Exception"); -} - -uc_value_t * -uc_vm_exception_object(uc_vm_t *vm) -{ - uc_exception_type_t type = vm->exception.type; - const char *message = vm->exception.message; - uc_value_t *stacktrace = vm->exception.stacktrace; - uc_value_t *exception_prototype = uc_vm_registry_get(vm, "vm.exception.proto"); - uc_value_t *exo; - - if (exception_prototype == NULL) { - exception_prototype = ucv_object_new(vm); - - ucv_object_add(exception_prototype, "tostring", - ucv_cfunction_new("tostring", uc_vm_exception_tostring)); - - uc_vm_registry_set(vm, "vm.exception.proto", exception_prototype); - } - - exo = ucv_object_new(vm); - - ucv_object_add(exo, "type", ucv_string_new(exception_type_strings[type])); - ucv_object_add(exo, "message", ucv_string_new(message)); - ucv_object_add(exo, "stacktrace", ucv_get(stacktrace)); - - ucv_prototype_set(exo, ucv_get(exception_prototype)); - - return exo; -} - -static void -uc_vm_clear_exception(uc_vm_t *vm) -{ - vm->exception.type = EXCEPTION_NONE; - - ucv_put(vm->exception.stacktrace); - vm->exception.stacktrace = NULL; - - free(vm->exception.message); - vm->exception.message = NULL; -} - /* Well-known sentinel `uc_breakpoint_t.ip` value identifying the dedicated * "break on uncaught exception" system breakpoint (see debug.c's BK_UNCAUGHT). * It deliberately isn't a real bytecode address, so the ordinary @@ -1317,219 +508,25 @@ uc_vm_handle_exception(uc_vm_t *vm) return false; } -/* Adds the line and byte column of the source position `off` to `object`, under - * the keys `linekey` and `bytekey`. */ -static void -uc_vm_stacktrace_add_position(uc_value_t *object, uc_source_t *source, size_t off, - const char *linekey, const char *bytekey) -{ - size_t pos = off; - - ucv_object_add(object, linekey, ucv_int64_new(uc_source_get_line(source, &pos))); - ucv_object_add(object, bytekey, ucv_int64_new(pos)); -} - -/* Adds the bounds of the statement enclosing instruction `off` of `function`, - * so that consumers rendering a stacktrace can highlight the whole statement - * rather than only the failing position. */ static void -uc_vm_stacktrace_add_statement(uc_vm_t *vm, uc_value_t *entry, uc_function_t *function, size_t off) +uc_vm_insn_load(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_source_t *source = uc_program_function_source(function); - size_t startoff, endoff; - uc_value_t *statement; + switch (insn) { + case I_LOAD: + uc_vm_stack_push(vm, uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32)); + break; - if (!uc_program_function_stmt_bounds(function, off, &startoff, &endoff)) - return; + case I_LOAD8: + uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u8)); + break; - statement = ucv_object_new(vm); + case I_LOAD16: + uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u16)); + break; - uc_vm_stacktrace_add_position(statement, source, startoff, "line", "byte"); - uc_vm_stacktrace_add_position(statement, source, endoff, "endline", "endbyte"); - - ucv_object_add(entry, "statement", statement); -} - -static uc_value_t * -uc_vm_capture_stacktrace(uc_vm_t *vm, size_t i) -{ - uc_value_t *stacktrace, *entry, *last = NULL; - uc_function_t *function; - uc_callframe_t *frame; - uc_source_t *source; - size_t off, srcpos; - char *name; - - stacktrace = ucv_array_new(vm); - - for (; i > 0; i--) { - frame = &vm->callframes.entries[i - 1]; - entry = ucv_object_new(vm); - - if (frame->closure) { - function = frame->closure->function; - source = uc_program_function_source(function); - - off = (frame->ip - uc_vm_frame_chunk(frame)->entries) - 1; - srcpos = uc_program_function_srcpos(function, off); - - ucv_object_add(entry, "filename", ucv_string_new(source->filename)); - uc_vm_stacktrace_add_position(entry, source, srcpos, "line", "byte"); - - uc_vm_stacktrace_add_statement(vm, entry, function, off); - } - - if (i > 1) { - if (frame->closure) { - if (frame->closure->function->name[0]) - name = frame->closure->function->name; - else if (frame->closure->is_arrow) - name = "[arrow function]"; - else - name = "[anonymous function]"; - } - else { - name = frame->cfunction->name; - } - - ucv_object_add(entry, "function", ucv_string_new(name)); - } - - /* record the number of call frames collapsed into this one by tail call - * optimization, so consumers can indicate the missing frames */ - if (frame->tco) - ucv_object_add(entry, "tco", ucv_int64_new(frame->tco)); - - if (!ucv_is_equal(last, entry)) { - ucv_array_push(stacktrace, entry); - last = entry; - } - else { - ucv_put(entry); - } - } - - return stacktrace; -} - -static uc_value_t * -uc_vm_get_error_context(uc_vm_t *vm) -{ - size_t offset, insn, i, byte, line, startoff = 0, endoff = 0; - uc_value_t *stacktrace; - uc_callframe_t *frame; - uc_stringbuf_t *buf; - uc_chunk_t *chunk; - - if (vm->callframes.count == 0) - return NULL; - - /* skip to first non-native function call frame */ - for (i = vm->callframes.count; i > 1; i--) - if (vm->callframes.entries[i - 1].closure) - break; - - frame = &vm->callframes.entries[i - 1]; - - if (!frame->closure) - return NULL; - - chunk = uc_vm_frame_chunk(frame); - insn = (frame->ip - chunk->entries) - 1; - offset = uc_program_function_srcpos(frame->closure->function, insn); - stacktrace = uc_vm_capture_stacktrace(vm, i); - - buf = ucv_stringbuf_new(); - - byte = offset; - line = uc_source_get_line(uc_program_function_source(frame->closure->function), &byte); - - if (line) { - /* underline the statement the failing instruction belongs to, where one - * can be resolved, rather than only marking that instruction */ - uc_program_function_stmt_bounds(frame->closure->function, insn, &startoff, &endoff); - - uc_error_context_format(buf, uc_vm_frame_source(frame), stacktrace, - startoff, endoff, offset); - } - else if (frame->ip != chunk->entries) - ucv_stringbuf_printf(buf, "At instruction %zu", (frame->ip - chunk->entries) - 1); - else - ucv_stringbuf_append(buf, "At start of program"); - - ucv_object_add(ucv_array_get(stacktrace, 0), "context", ucv_stringbuf_finish(buf)); - - return stacktrace; -} - -void __attribute__((format(printf, 3, 0))) -uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, ...) -{ - va_list ap; - - vm->exception.type = type; - - free(vm->exception.message); - - va_start(ap, fmt); - xvasprintf(&vm->exception.message, fmt, ap); - va_end(ap); - - ucv_put(vm->exception.stacktrace); - vm->exception.stacktrace = uc_vm_get_error_context(vm); -} - -static bool -uc_vm_test_strict_equality(uc_value_t *v1, uc_value_t *v2, bool nan_equal) -{ - uc_type_t t1 = ucv_type(v1); - uc_type_t t2 = ucv_type(v2); - double d1, d2; - - if (t1 != t2) - return false; - - switch (t1) { - case UC_DOUBLE: - d1 = ucv_as_double(v1)->dbl; - d2 = ucv_as_double(v2)->dbl; - - if (isnan(d1) && isnan(d2)) - return nan_equal; - - return (d1 == d2); - - case UC_NULL: - case UC_BOOLEAN: - case UC_INTEGER: - case UC_STRING: - return ucv_is_equal(v1, v2); - - default: - return (v1 == v2); - } -} - - -static void -uc_vm_insn_load(uc_vm_t *vm, uc_vm_insn_t insn) -{ - switch (insn) { - case I_LOAD: - uc_vm_stack_push(vm, uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32)); - break; - - case I_LOAD8: - uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u8)); - break; - - case I_LOAD16: - uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u16)); - break; - - case I_LOAD32: - uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u32)); - break; + case I_LOAD32: + uc_vm_stack_push(vm, ucv_uint64_new(vm->arg.u32)); + break; default: break; @@ -1540,32 +537,12 @@ static void uc_vm_insn_load_regexp(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *re, *jstr = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); - bool icase = false, newline = false, global = false; - char *str, *err = NULL; - - if (ucv_type(jstr) != UC_STRING || ucv_string_length(jstr) < 2) { - uc_vm_stack_push(vm, NULL); - ucv_put(jstr); - - return; - } - - str = ucv_string_get(jstr); - - global = (*str & (1 << 0)); - icase = (*str & (1 << 1)); - newline = (*str & (1 << 2)); - - re = ucv_regexp_new(++str, icase, newline, global, &err); + bool loaded = uc_vm_regexp_load(vm, jstr, &re); ucv_put(jstr); - if (re) + if (loaded) uc_vm_stack_push(vm, re); - else - uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "%s", err); - - free(err); } static void @@ -1583,37 +560,12 @@ uc_vm_insn_load_bool(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_load_var(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *name, *val = NULL; - uc_value_t *scope, *next; - bool found; - - scope = vm->globals; - name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); - - while (ucv_type(name) == UC_STRING) { - val = ucv_object_get(scope, ucv_string_get(name), &found); - - if (found) - break; - - next = ucv_prototype_get(scope); - - if (!next) { - if (uc_vm_is_strict(vm)) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "access to undeclared variable %s", - ucv_string_get(name)); - } - - break; - } - - scope = next; - } + uc_value_t *name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); + uc_value_t *val = uc_vm_global_get(vm, name, uc_vm_is_strict(vm)); ucv_put(name); - uc_vm_stack_push(vm, ucv_get(val)); + uc_vm_stack_push(vm, val); } static void @@ -1621,22 +573,10 @@ uc_vm_insn_load_val(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *k = uc_vm_stack_pop(vm); uc_value_t *v = uc_vm_stack_pop(vm); + uc_value_t *res; - switch (ucv_type(v)) { - case UC_RESOURCE: - case UC_OBJECT: - case UC_ARRAY: - /* aware read: dispatches __get__ on a missing key */ - uc_vm_stack_push(vm, ucv_key_get(vm, v, k)); - break; - - default: - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "left-hand side expression is %s", - v ? "not an array or object" : "null"); - - break; - } + if (uc_vm_property_get(vm, v, k, &res)) + uc_vm_stack_push(vm, res); ucv_put(k); ucv_put(v); @@ -1647,22 +587,10 @@ uc_vm_insn_peek_val(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *k = uc_vm_stack_pop(vm); uc_value_t *v = uc_vm_stack_peek(vm, 0); + uc_value_t *res; - switch (ucv_type(v)) { - case UC_RESOURCE: - case UC_OBJECT: - case UC_ARRAY: - /* aware read: dispatches __get__ on a missing key */ - uc_vm_stack_push(vm, ucv_key_get(vm, v, k)); - break; - - default: - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "left-hand side expression is %s", - v ? "not an array or object" : "null"); - - break; - } + if (uc_vm_property_get(vm, v, k, &res)) + uc_vm_stack_push(vm, res); ucv_put(k); } @@ -1701,67 +629,6 @@ uc_vm_insn_load_local(uc_vm_t *vm, uc_vm_insn_t insn) uc_vm_stack_push(vm, ucv_get(vm->stack.entries[frame->stackframe + vm->arg.u32])); } -static uc_upvalref_t * -uc_vm_capture_upval(uc_vm_t *vm, size_t slot) -{ - uc_upvalref_t *curr = vm->open_upvals; - uc_upvalref_t *prev = NULL; - uc_upvalref_t *created; - char *s; - - while (curr && curr->slot > slot) { - prev = curr; - curr = curr->next; - } - - if (curr && curr->slot == slot) { - if (vm->trace) { - s = ucv_to_string(NULL, vm->stack.entries[slot]); - fprintf(stderr, " {+%zu} <%p> %s\n", slot, (void *)curr, s); - free(s); - } - - return curr; - } - - created = (uc_upvalref_t *)ucv_upvalref_new(slot); - created->next = curr; - - if (vm->trace) { - s = ucv_to_string(NULL, vm->stack.entries[slot]); - fprintf(stderr, " {*%zu} <%p> %s\n", slot, (void *)created, s); - free(s); - } - - if (prev) - prev->next = created; - else - vm->open_upvals = created; - - return created; -} - -static void -uc_vm_close_upvals(uc_vm_t *vm, size_t slot) -{ - uc_upvalref_t *ref; - - while (vm->open_upvals && vm->open_upvals->slot >= slot) { - ref = vm->open_upvals; - ref->value = ucv_get(vm->stack.entries[ref->slot]); - ref->closed = true; - - if (vm->trace) { - fprintf(stderr, " {!%zu} <%p> %s\n", ref->slot, - (void *)ref, - uc_vm_format_val(vm, ref->value)); - } - - vm->open_upvals = ref->next; - ucv_put(&ref->header); - } -} - static void uc_vm_insn_load_closure(uc_vm_t *vm, uc_vm_insn_t insn) { @@ -1769,582 +636,109 @@ uc_vm_insn_load_closure(uc_vm_t *vm, uc_vm_insn_t insn) uc_function_t *function = uc_program_function_load(uc_vm_current_program(vm), vm->arg.u32); uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, function, insn == I_ARFN); volatile int32_t uv; - size_t i; - - uc_vm_stack_push(vm, &closure->header); - - if (function->module) - return; - - for (i = 0; i < function->nupvals; i++) { - uv = ( - frame->ip[0] * 0x1000000 + - frame->ip[1] * 0x10000 + - frame->ip[2] * 0x100 + - frame->ip[3] - ) - 0x7fffffff; - - if (uv < 0) - closure->upvals[i] = uc_vm_capture_upval(vm, frame->stackframe - (uv + 1)); - else - closure->upvals[i] = frame->closure->upvals[uv]; - - ucv_get(&closure->upvals[i]->header); - - frame->ip += 4; - } -} - -static void -uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn) -{ - uc_value_t *name, *v = uc_vm_stack_pop(vm); - uc_value_t *scope, *next; - bool found; - - scope = vm->globals; - name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); - - while (ucv_type(name) == UC_STRING) { - ucv_object_get(scope, ucv_string_get(name), &found); - - if (found) - break; - - next = ucv_prototype_get(scope); - - if (!next) { - if (uc_vm_is_strict(vm)) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "access to undeclared variable %s", - ucv_string_get(name)); - ucv_put(name); - ucv_put(v); - - return; - } - - break; - } - - scope = next; - } - - if (scope && ucv_type(name) == UC_STRING) - ucv_object_add(scope, ucv_string_get(name), ucv_get(v)); - - ucv_put(name); - uc_vm_stack_push(vm, v); -} - -static bool -assert_mutable_value(uc_vm_t *vm, uc_value_t *val) -{ - if (ucv_is_constant(val)) { - uc_vm_stack_push(vm, NULL); - uc_vm_raise_exception(vm, EXCEPTION_TYPE, - "%s value is immutable", - ucv_typename(val)); - - return false; - } - - return true; -} - -static void -uc_vm_insn_store_val(uc_vm_t *vm, uc_vm_insn_t insn) -{ - uc_value_t *v = uc_vm_stack_pop(vm); - uc_value_t *k = uc_vm_stack_pop(vm); - uc_value_t *o = uc_vm_stack_pop(vm); - - switch (ucv_type(o)) { - case UC_OBJECT: - case UC_ARRAY: - case UC_RESOURCE: - if (assert_mutable_value(vm, o)) { - /* ucv_key_set() retains the stored value on its own and returns a - * reference to it, which becomes the value of the assignment; for - * resources it dispatches a __set__ metamethod when present. In - * strict mode it raises a type error when a non-index property - * store on an array would otherwise be silently dropped. */ - uc_vm_stack_push(vm, ucv_key_set(vm, o, k, v)); - } - - break; - - default: - uc_vm_raise_exception(vm, EXCEPTION_TYPE, - "attempt to set property on %s value", - ucv_typename(o)); - } - - ucv_put(v); - ucv_put(o); - ucv_put(k); -} - -static void -uc_vm_insn_store_upval(uc_vm_t *vm, uc_vm_insn_t insn) -{ - uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_upvalref_t *ref; - uc_value_t *val; - - if (vm->arg.u32 >= frame->closure->function->nupvals) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "upvalue index out of range"); - - return; - } - - ref = frame->closure->upvals[vm->arg.u32]; - val = ucv_get(uc_vm_stack_peek(vm, 0)); - - if (ref->closed) { - ucv_put(ref->value); - ref->value = val; - } - else { - uc_vm_stack_set(vm, ref->slot, val); - } -} - -static void -uc_vm_insn_store_local(uc_vm_t *vm, uc_vm_insn_t insn) -{ - uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_value_t *val; - - if (frame->stackframe + vm->arg.u32 >= vm->stack.count) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "local slot out of range"); - - return; - } - - val = ucv_get(uc_vm_stack_peek(vm, 0)); - - uc_vm_stack_set(vm, frame->stackframe + vm->arg.u32, val); -} - -static int64_t -int64(uc_value_t *nv, uint64_t *u64) -{ - int64_t n; - - n = ucv_int64_get(nv); - *u64 = 0; - - if (errno == ERANGE) { - n = INT64_MAX; - *u64 = ucv_uint64_get(nv); - } - - return n; -} - -static uint64_t -abs64(int64_t n) -{ - if (n == INT64_MIN) - return 0x8000000000000000ULL; - - if (n < 0) - return -n; - - return n; -} - - -static uc_value_t * -uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) -{ - uc_value_t *nv1, *nv2, *rv = NULL; - uint64_t u1, u2; - int64_t n1, n2; - - nv1 = ucv_to_number(value); - nv2 = ucv_to_number(operand); - - n1 = int64(nv1, &u1); - n2 = int64(nv2, &u2); - - if (n1 < 0 || n2 < 0) { - switch (operation) { - case I_LSHIFT: - rv = ucv_int64_new(n1 << n2); - break; - - case I_RSHIFT: - rv = ucv_int64_new(n1 >> n2); - break; - - case I_BAND: - rv = ucv_int64_new(n1 & n2); - break; - - case I_BXOR: - rv = ucv_int64_new(n1 ^ n2); - break; - - case I_BOR: - rv = ucv_int64_new(n1 | n2); - break; - - default: - break; - } - } - else { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - - switch (operation) { - case I_LSHIFT: - rv = ucv_uint64_new(u1 << (u2 % (sizeof(uint64_t) * CHAR_BIT))); - break; - - case I_RSHIFT: - rv = ucv_uint64_new(u1 >> (u2 % (sizeof(uint64_t) * CHAR_BIT))); - break; - - case I_BAND: - rv = ucv_uint64_new(u1 & u2); - break; - - case I_BXOR: - rv = ucv_uint64_new(u1 ^ u2); - break; - - case I_BOR: - rv = ucv_uint64_new(u1 | u2); - break; - - default: - break; - } - } - - ucv_put(nv1); - ucv_put(nv2); - - return rv; -} - -static uc_value_t * -uc_vm_string_concat(uc_vm_t *vm, uc_value_t *v1, uc_value_t *v2) -{ - char buf[sizeof(void *)], *s1, *s2, *str; - uc_value_t *ustr; - uc_stringbuf_t *sbuf; - size_t l1, l2; - - /* optimize cases for string+string concat... */ - if (ucv_type(v1) == UC_STRING && ucv_type(v2) == UC_STRING) { - s1 = ucv_string_get(v1); - s2 = ucv_string_get(v2); - l1 = ucv_string_length(v1); - l2 = ucv_string_length(v2); - - /* ... result fits into a tagged pointer */ - if (l1 + l2 + 1 < sizeof(buf)) { - memcpy(&buf[0], s1, l1); - memcpy(&buf[l1], s2, l2); - - return ucv_string_new_length(buf, l1 + l2); - } - else { - ustr = ucv_string_alloc(&str, l1 + l2); - memcpy(&str[0], s1, l1); - memcpy(&str[l1], s2, l2); - - return ustr; - } - } - - sbuf = ucv_stringbuf_new(); - - ucv_to_stringbuf(vm, sbuf, v1, false); - ucv_to_stringbuf(vm, sbuf, v2, false); - - return ucv_stringbuf_finish(sbuf); -} - -static uint64_t -upow64(uint64_t base, uint64_t exponent) -{ - uint64_t result = 1; - - while (exponent) { - if (exponent & 1) - result *= base; - - exponent >>= 1; - base *= base; - } - - return result; -} - -static uc_value_t * -uc_vm_value_arith(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) -{ - uc_value_t *nv1, *nv2, *rv = NULL; - uint64_t u1, u2; - int64_t n1, n2; - double d1, d2; - - if (operation == I_LSHIFT || operation == I_RSHIFT || - operation == I_BAND || operation == I_BXOR || operation == I_BOR) - return uc_vm_value_bitop(vm, operation, value, operand); - - if (operation == I_ADD && (ucv_type(value) == UC_STRING || ucv_type(operand) == UC_STRING)) - return uc_vm_string_concat(vm, value, operand); - - nv1 = ucv_to_number(value); - nv2 = ucv_to_number(operand); - - /* any operation involving NaN results in NaN */ - if (!nv1 || !nv2) { - ucv_put(nv1); - ucv_put(nv2); - - return ucv_double_new(NAN); - } - if (ucv_type(nv1) == UC_DOUBLE || ucv_type(nv2) == UC_DOUBLE) { - d1 = ucv_double_get(nv1); - d2 = ucv_double_get(nv2); - - switch (operation) { - case I_ADD: - case I_PLUS: - rv = ucv_double_new(d1 + d2); - break; - - case I_SUB: - case I_MINUS: - rv = ucv_double_new(d1 - d2); - break; - - case I_MUL: - rv = ucv_double_new(d1 * d2); - break; - - case I_DIV: - if (d2 == 0.0) - rv = ucv_double_new(d1 / d2); - else if (isnan(d2)) - rv = ucv_double_new(NAN); - else if (!isfinite(d2)) - rv = ucv_double_new(isfinite(d1) ? 0.0 : NAN); - else - rv = ucv_double_new(d1 / d2); - - break; - - case I_MOD: - rv = ucv_double_new(fmod(d1, d2)); - break; - - case I_EXP: - rv = ucv_double_new(pow(d1, d2)); - break; - - default: - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, - "undefined arithmetic operation %d", - operation); - break; - } - } - else { - n1 = int64(nv1, &u1); - n2 = int64(nv2, &u2); - - switch (operation) { - case I_ADD: - case I_PLUS: - if (n1 < 0 || n2 < 0) { - if (u1) - rv = ucv_uint64_new(u1 - abs64(n2)); - else if (u2) - rv = ucv_uint64_new(u2 - abs64(n1)); - else - rv = ucv_int64_new(n1 + n2); - } - else { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - - rv = ucv_uint64_new(u1 + u2); - } - - break; - - case I_SUB: - case I_MINUS: - if (n1 < 0 && n2 < 0) { - if (n1 > n2) - rv = ucv_uint64_new(abs64(n2) - abs64(n1)); - else - rv = ucv_int64_new(n1 - n2); - } - else if (n1 >= 0 && n2 >= 0) { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - - if (u2 > u1) - rv = ucv_int64_new(-(u2 - u1)); - else - rv = ucv_uint64_new(u1 - u2); - } - else if (n1 >= 0) { - if (!u1) u1 = (uint64_t)n1; - - rv = ucv_uint64_new(u1 + abs64(n2)); - } - else { - rv = ucv_int64_new(n1 - n2); - } - - break; - - case I_MUL: - if (n1 < 0 && n2 < 0) { - rv = ucv_uint64_new(abs64(n1) * abs64(n2)); - } - else if (n1 >= 0 && n2 >= 0) { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - - rv = ucv_uint64_new(u1 * u2); - } - else { - rv = ucv_int64_new(n1 * n2); - } - - break; - - case I_DIV: - if (n2 == 0) { - if (n1 == 0 && !u1) - rv = ucv_double_new(NAN); - else - rv = ucv_double_new((n1 < 0) ? -INFINITY : INFINITY); - } - else if (n1 == INT64_MIN && n2 == -1) { - /* the mathematical result 2^63 only fits into uint64_t; - * the signed division would trap with SIGFPE */ - rv = ucv_uint64_new((uint64_t)INT64_MAX + 1); - } - else if (n1 < 0 || n2 < 0) { - rv = ucv_int64_new(n1 / n2); - } - else { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - - rv = ucv_uint64_new(u1 / u2); - } - - break; - - case I_MOD: - if (n2 == 0) { - rv = ucv_double_new(NAN); - } - else if (n1 == INT64_MIN && n2 == -1) { - /* the signed modulo would trap with SIGFPE */ - rv = ucv_int64_new(0); - } - else if (n1 < 0 || n2 < 0) { - rv = ucv_int64_new(n1 % n2); - } - else { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - - rv = ucv_uint64_new(u1 % u2); - } + size_t i; - break; + uc_vm_stack_push(vm, &closure->header); - case I_EXP: - if (n1 < 0 || n2 < 0) { - /* the result is negative iff the base is negative and the - * exponent is odd */ - if (n2 < 0) - rv = ucv_double_new(((n1 < 0 && (n2 & 1)) ? -1.0 : 1.0) / - (double)upow64(abs64(n1), abs64(n2))); - else if (n2 & 1) - rv = ucv_int64_new((int64_t)-upow64(abs64(n1), abs64(n2))); - else - rv = ucv_uint64_new(upow64(abs64(n1), abs64(n2))); - } - else { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; + if (function->module) + return; - rv = ucv_uint64_new(upow64(u1, u2)); - } + for (i = 0; i < function->nupvals; i++) { + uv = ( + frame->ip[0] * 0x1000000 + + frame->ip[1] * 0x10000 + + frame->ip[2] * 0x100 + + frame->ip[3] + ) - 0x7fffffff; - break; + uc_vm_closure_upval_set(vm, closure, i, uv); - default: - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, - "undefined arithmetic operation %d", - operation); - break; - } + frame->ip += 4; } +} + +static void +uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn) +{ + uc_value_t *v = uc_vm_stack_pop(vm); + uc_value_t *name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); + bool stored = uc_vm_global_set(vm, name, v, uc_vm_is_strict(vm)); - ucv_put(nv1); - ucv_put(nv2); + ucv_put(name); - return rv; + if (stored) + uc_vm_stack_push(vm, v); + else + ucv_put(v); } static void -uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) +uc_vm_insn_store_val(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *name, *val = NULL, *inc = uc_vm_stack_pop(vm); - uc_value_t *scope, *next; - bool found; + uc_value_t *v = uc_vm_stack_pop(vm); + uc_value_t *k = uc_vm_stack_pop(vm); + uc_value_t *o = uc_vm_stack_pop(vm); + uc_value_t *res; - scope = vm->globals; - name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32 & 0x00FFFFFF); + if (uc_vm_property_set(vm, o, k, v, &res)) + uc_vm_stack_push(vm, res); - assert(ucv_type(name) == UC_STRING); + ucv_put(v); + ucv_put(o); + ucv_put(k); +} - while (true) { - val = ucv_object_get(scope, ucv_string_get(name), &found); +static void +uc_vm_insn_store_upval(uc_vm_t *vm, uc_vm_insn_t insn) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_upvalref_t *ref; + uc_value_t *val; - if (found) - break; + if (vm->arg.u32 >= frame->closure->function->nupvals) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "upvalue index out of range"); - next = ucv_prototype_get(scope); + return; + } - if (!next) { - if (uc_vm_is_strict(vm)) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "access to undeclared variable %s", - ucv_string_get(name)); - ucv_put(name); - ucv_put(inc); + ref = frame->closure->upvals[vm->arg.u32]; + val = ucv_get(uc_vm_stack_peek(vm, 0)); - return; - } + if (ref->closed) { + ucv_put(ref->value); + ref->value = val; + } + else { + uc_vm_stack_set(vm, ref->slot, val); + } +} - break; - } +static void +uc_vm_insn_store_local(uc_vm_t *vm, uc_vm_insn_t insn) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_value_t *val; + + if (frame->stackframe + vm->arg.u32 >= vm->stack.count) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "local slot out of range"); - scope = next; + return; } - val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, val, inc); + val = ucv_get(uc_vm_stack_peek(vm, 0)); - ucv_object_add(scope, ucv_string_get(name), ucv_get(val)); - uc_vm_stack_push(vm, val); + uc_vm_stack_set(vm, frame->stackframe + vm->arg.u32, val); +} + +static void +uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) +{ + uc_value_t *inc = uc_vm_stack_pop(vm); + uc_value_t *name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32 & 0x00FFFFFF); + uc_value_t *val; + + if (uc_vm_global_update(vm, vm->arg.u32 >> 24, name, inc, uc_vm_is_strict(vm), &val)) + uc_vm_stack_push(vm, val); ucv_put(name); ucv_put(inc); @@ -2356,52 +750,11 @@ uc_vm_insn_update_val(uc_vm_t *vm, uc_vm_insn_t insn) uc_value_t *inc = uc_vm_stack_pop(vm); uc_value_t *k = uc_vm_stack_pop(vm); uc_value_t *v = uc_vm_stack_pop(vm); - uc_value_t *val = NULL; - uc_vm_insn_t op; - - switch (ucv_type(v)) { - case UC_OBJECT: - case UC_ARRAY: - case UC_RESOURCE: - if (assert_mutable_value(vm, v)) { - uc_value_t *nv, *rv; - - /* reading the property may dispatch a __get__ metamethod which runs - * on the current stack, clobbers vm->arg and may collect garbage, so - * remember the operation and keep the operands reachable */ - op = vm->arg.u8; - uc_vm_stack_push(vm, ucv_get(inc)); - - val = ucv_key_get(vm, v, k); - uc_vm_stack_push(vm, ucv_get(val)); - - nv = uc_vm_value_arith(vm, op, val, inc); - - ucv_put(uc_vm_stack_pop(vm)); - ucv_put(uc_vm_stack_pop(vm)); - - /* an aborted read leaves nothing to store; the exception it raised is - * left to propagate instead of overwriting the property */ - rv = vm->exception.type != EXCEPTION_NONE ? NULL : ucv_key_set(vm, v, k, nv); - - /* the store keeps a reference of its own; rv is the value of the - * update expression, null if storing failed */ - ucv_put(nv); - - uc_vm_stack_push(vm, rv); - } - - break; + uc_value_t *res; - default: - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "left-hand side expression is %s", - v ? "not an array or object" : "null"); - - break; - } + if (uc_vm_property_update(vm, vm->arg.u8, v, k, inc, &res)) + uc_vm_stack_push(vm, res); - ucv_put(val); ucv_put(inc); ucv_put(v); ucv_put(k); @@ -2484,8 +837,7 @@ uc_vm_insn_parr(uc_vm_t *vm, uc_vm_insn_t insn) uc_value_t *arr = uc_vm_stack_peek(vm, vm->arg.u32); size_t idx; - for (idx = 0; idx < vm->arg.u32; idx++) - ucv_array_push(arr, uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1)); + uc_vm_array_append(arr, &vm->stack.entries[vm->stack.count - vm->arg.u32], vm->arg.u32); for (idx = 0; idx < vm->arg.u32; idx++) uc_vm_stack_pop(vm); @@ -2498,73 +850,20 @@ uc_vm_insn_marr(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *src = uc_vm_stack_pop(vm); uc_value_t *dst = uc_vm_stack_peek(vm, 0); - size_t i; - char *s; - - if (ucv_type(src) != UC_ARRAY) { - s = ucv_to_string(vm, src); - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); - ucv_put(src); - free(s); - return; - } - - for (i = 0; i < ucv_array_length(src); i++) - ucv_array_push(dst, ucv_get(ucv_array_get(src, i))); - - ucv_put(src); + uc_vm_array_spread(vm, dst, src); } static void uc_vm_insn_nobj(uc_vm_t *vm, uc_vm_insn_t insn) { - size_t nkeys = vm->arg.u32 / 2; - uc_value_t *obj = ucv_object_new_size(vm, nkeys); - - uc_vm_stack_push(vm, obj); + uc_vm_stack_push(vm, uc_vm_object_new(vm, vm->arg.u32 / 2)); } -/* Invoke a standard library function without pushing a call frame. - * - * The arguments occupy the top `nargs` stack entries, which is exactly the - * layout uc_fn_arg() expects, so the callee can be entered as an ordinary C - * function; it restores the stack to the argument boundary itself when it - * calls back into managed code. - * - * The callee is addressed by its slot in the order-frozen - * uc_stdlib_functions[] table rather than by name, so a shadowing `slice`, - * `type` or `die` in the runtime namespace cannot affect the call. - * - * Since no frame is involved, an exception raised by the callee reports the - * position of this instruction in the calling chunk, not of a synthetic one. */ static void uc_vm_insn_stcall(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_cfn_ptr_t fptr = uc_stdlib_function_by_slot(vm->arg.u32 >> 16); - size_t nargs = vm->arg.u32 & 0xffff; - size_t keep = vm->stack.count - nargs; - uc_value_t *res = NULL; - - if (!fptr) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, - "unknown standard library slot %zu", vm->arg.u32 >> 16); - } - else { - res = fptr(vm, nargs); - } - - /* a callee reads the arguments with uc_fn_arg() but does not consume them - - * ordinary calls consume them through the callframe pop - so drop them here, - * along with anything the callee left above them */ - while (vm->stack.count > keep) - ucv_put(uc_vm_stack_pop(vm)); - - /* as in uc_vm_call_native(), a result is only kept when no exception fired */ - if (vm->exception.type) - ucv_put(res); - else - uc_vm_stack_push(vm, res); + uc_vm_stcall(vm, vm->arg.u32 >> 16, vm->arg.u32 & 0xffff); } static void @@ -2573,12 +872,10 @@ uc_vm_insn_sobj(uc_vm_t *vm, uc_vm_insn_t insn) uc_value_t *obj = uc_vm_stack_peek(vm, vm->arg.u32); size_t idx; - /* object literals create own keys, a __set__ metamethod of the object under - * construction must not intercept the keys it is built from */ for (idx = 0; idx < vm->arg.u32; idx += 2) - ucv_put(ucv_key_rawset(vm, obj, + uc_vm_object_init_key(vm, obj, uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1), - uc_vm_stack_peek(vm, vm->arg.u32 - idx - 2))); + uc_vm_stack_peek(vm, vm->arg.u32 - idx - 2)); for (idx = 0; idx < vm->arg.u32; idx++) ucv_put(uc_vm_stack_pop(vm)); @@ -2589,35 +886,8 @@ uc_vm_insn_mobj(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *src = uc_vm_stack_pop(vm); uc_value_t *dst = uc_vm_stack_peek(vm, 0); - size_t i; - char *s; - - switch (ucv_type(src)) { - case UC_OBJECT: - ; /* a label can only be part of a statement and a declaration is not a statement */ - ucv_object_foreach_entry(src, e) - ucv_object_insert_string(dst, e->key, ucv_get(e->value)); - - ucv_put(src); - break; - - case UC_ARRAY: - for (i = 0; i < ucv_array_length(src); i++) { - xasprintf(&s, "%zu", i); - ucv_object_add(dst, s, ucv_get(ucv_array_get(src, i))); - free(s); - } - - ucv_put(src); - break; - default: - s = ucv_to_string(vm, src); - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Value (%s) is not iterable", s); - free(s); - - break; - } + uc_vm_object_spread(vm, dst, src); } static void @@ -2638,70 +908,9 @@ uc_vm_insn_arith(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_plus_minus(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *v = uc_vm_stack_pop(vm), *nv; - bool is_sub = (insn == I_MINUS); - int64_t n; - double d; - - if (ucv_type(v) == UC_STRING) { - nv = uc_number_parse(ucv_string_get(v), NULL); - - if (nv) { - ucv_put(v); - v = nv; - } - } - - switch (ucv_type(v)) { - case UC_INTEGER: - n = ucv_int64_get(v); - - /* numeric value is in range 9223372036854775808..18446744073709551615 */ - if (errno == ERANGE) { - if (is_sub) - /* make negation of large numeric value result in smallest negative value */ - uc_vm_stack_push(vm, ucv_int64_new(INT64_MIN)); - else - /* for positive number coercion return value as-is */ - uc_vm_stack_push(vm, ucv_get(v)); - } - - /* numeric value is in range -9223372036854775808..9223372036854775807 */ - else { - if (is_sub) { - if (n == INT64_MIN) - /* make negation of minimum value result in maximum signed positive value */ - uc_vm_stack_push(vm, ucv_int64_new(INT64_MAX)); - else - /* for all other values flip the sign */ - uc_vm_stack_push(vm, ucv_int64_new(-n)); - } - else { - /* for positive number coercion return value as-is */ - uc_vm_stack_push(vm, ucv_get(v)); - } - } - - break; - - case UC_DOUBLE: - d = ucv_double_get(v); - uc_vm_stack_push(vm, ucv_double_new(is_sub ? -d : d)); - break; - - case UC_BOOLEAN: - n = (int64_t)ucv_boolean_get(v); - uc_vm_stack_push(vm, ucv_int64_new(is_sub ? -n : n)); - break; - - case UC_NULL: - uc_vm_stack_push(vm, ucv_int64_new(0)); - break; - - default: - uc_vm_stack_push(vm, ucv_double_new(NAN)); - } + uc_value_t *v = uc_vm_stack_pop(vm); + uc_vm_stack_push(vm, uc_vm_value_unary(insn, v)); ucv_put(v); } @@ -2724,23 +933,8 @@ static void uc_vm_insn_complement(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *v = uc_vm_stack_pop(vm); - uc_value_t *nv; - uint64_t u; - int64_t n; - - nv = ucv_to_number(v); - n = int64(nv, &u); - - if (n < 0) { - uc_vm_stack_push(vm, ucv_int64_new(~n)); - } - else { - if (!u) u = (uint64_t)n; - - uc_vm_stack_push(vm, ucv_uint64_new(~u)); - } - ucv_put(nv); + uc_vm_stack_push(vm, uc_vm_value_complement(v)); ucv_put(v); } @@ -2750,7 +944,7 @@ uc_vm_insn_rel(uc_vm_t *vm, uc_vm_insn_t insn) uc_value_t *r2 = uc_vm_stack_pop(vm); uc_value_t *r1 = uc_vm_stack_pop(vm); - bool res = ucv_compare(insn, r1, r2, NULL); + bool res = uc_vm_value_compare(insn, r1, r2); ucv_put(r1); ucv_put(r2); @@ -2758,60 +952,12 @@ uc_vm_insn_rel(uc_vm_t *vm, uc_vm_insn_t insn) uc_vm_stack_push(vm, ucv_boolean_new(res)); } -/* Test whether `key` names a property of `scope` or of anything in its - * prototype chain, the way a property read resolves it - but without - * dispatching __get__, so a purely virtual property stays hidden. */ -static bool -uc_vm_key_exists(uc_value_t *scope, uc_value_t *key) -{ - uc_value_t *o; - bool found = false; - - if (ucv_type(key) != UC_STRING) - return false; - - for (o = scope; o != NULL && !found; o = ucv_prototype_get(o)) - ucv_object_lookup(o, ucv_string_get(key), ucv_string_length(key), &found); - - return found; -} - static void uc_vm_insn_in(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *r2 = uc_vm_stack_pop(vm); uc_value_t *r1 = uc_vm_stack_pop(vm); - uc_value_t *item; - size_t arrlen, arridx; - bool found = false; - - switch (ucv_type(r2)) { - case UC_ARRAY: - /* value membership: is r1 one of the array elements? */ - for (arridx = 0, arrlen = ucv_array_length(r2); - arridx < arrlen; arridx++) { - item = ucv_array_get(r2, arridx); - - if (uc_vm_test_strict_equality(r1, item, true)) { - found = true; - break; - } - } - - /* arrays can carry a prototype providing the tested key as well */ - if (!found) - found = uc_vm_key_exists(r2, r1); - - break; - - case UC_OBJECT: - found = uc_vm_key_exists(r2, r1); - - break; - - default: - found = false; - } + bool found = uc_vm_value_in(r1, r2); ucv_put(r1); ucv_put(r2); @@ -2824,12 +970,12 @@ uc_vm_insn_equality(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *r2 = uc_vm_stack_pop(vm); uc_value_t *r1 = uc_vm_stack_pop(vm); - bool equal = uc_vm_test_strict_equality(r1, r2, false); + bool equal = uc_vm_value_equality(insn, r1, r2); ucv_put(r1); ucv_put(r2); - uc_vm_stack_push(vm, ucv_boolean_new((insn == I_EQS) ? equal : !equal)); + uc_vm_stack_push(vm, ucv_boolean_new(equal)); } static void @@ -2837,7 +983,7 @@ uc_vm_insn_not(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *r1 = uc_vm_stack_pop(vm); - uc_vm_stack_push(vm, ucv_boolean_new(!ucv_is_truish(r1))); + uc_vm_stack_push(vm, ucv_boolean_new(uc_vm_value_not(r1))); ucv_put(r1); } @@ -2886,165 +1032,50 @@ uc_vm_insn_jmpz(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_jmpnt(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_chunk_t *chunk = uc_vm_frame_chunk(frame); - int16_t addr = (vm->arg.u32 & 0xffff) - 0x7fff; - uint16_t types = (vm->arg.u32 >> 16) & 0x1fff; - uint8_t depth = (vm->arg.u32 >> 29) & 0x7; - uc_value_t *v = uc_vm_stack_peek(vm, 0); - size_t i; - - /* ip already has been incremented */ - addr -= 5; - - if (frame->ip + addr < chunk->entries || - frame->ip + addr >= chunk->entries + chunk->count) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range"); - return; - } - - if (!(types & (1u << ucv_type(v)))) { - for (i = 0; i <= depth; i++) - ucv_put(uc_vm_stack_pop(vm)); - - uc_vm_stack_push(vm, NULL); - frame->ip += addr; - } -} - - -static void -uc_vm_object_iterator_free(void *ud) -{ - uc_object_iterator_t *iter = ud; - - uc_list_remove(&iter->list); -} - -static uc_resource_type_t uc_vm_object_iterator_type = { - .name = "object iterator", - .free = uc_vm_object_iterator_free -}; - -static bool -uc_vm_object_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, - uc_value_t *k, uc_value_t *v) -{ - uc_resource_t *res = ucv_as_resource(k); - uc_object_t *obj = ucv_as_object(v); - uc_object_iterator_t *iter; - uc_object_entry_t *e; - - if (!res) { - /* object is empty */ - if (!obj->count) - return false; - - res = xalloc(sizeof(*res) + sizeof(uc_object_iterator_t)); - res->header.type = UC_RESOURCE; - res->header.refcount = 1; - res->type = &uc_vm_object_iterator_type; - - iter = res->data = (char *)res + sizeof(*res); - iter->object = obj; - iter->pos = 0; - - uc_list_insert(&uc_thread_context_get()->object_iterators, &iter->list); - } - else if (ucv_type(k) == UC_RESOURCE && - res->type == &uc_vm_object_iterator_type && res->data != NULL) { - - iter = res->data; - } - else { - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid object iterator"); - - return false; - } - - while (iter->pos < obj->used && !obj->entries[iter->pos].key) - iter->pos++; - - /* no next key */ - if (iter->pos >= obj->used) { - uc_list_remove(&iter->list); - - return false; - } - - e = &obj->entries[iter->pos++]; - uc_vm_stack_push(vm, ucv_get(e->key)); - - if (insn == I_NEXTKV) - uc_vm_stack_push(vm, ucv_get(e->value)); - - uc_vm_stack_push(vm, &res->header); - ucv_put(v); - - return true; -} - -static bool -uc_vm_array_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, - uc_value_t *k, uc_value_t *v) -{ - uint64_t n; - - if (!k) { - /* array is empty */ - if (!ucv_array_length(v)) - return false; - - k = ucv_resource_new(NULL, NULL); - n = 0; - } - else if (ucv_type(k) == UC_RESOURCE) { - n = (uintptr_t)ucv_resource_data(k, NULL); - } - else { - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid array iterator"); - - return false; - } - - /* no next index */ - if (n >= ucv_array_length(v)) - return false; - - if (insn == I_NEXTKV) - uc_vm_stack_push(vm, ucv_uint64_new(n)); + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_chunk_t *chunk = uc_vm_frame_chunk(frame); + int16_t addr = (vm->arg.u32 & 0xffff) - 0x7fff; + uint16_t types = (vm->arg.u32 >> 16) & 0x1fff; + uint8_t depth = (vm->arg.u32 >> 29) & 0x7; + uc_value_t *v = uc_vm_stack_peek(vm, 0); + size_t i; - uc_vm_stack_push(vm, ucv_get(ucv_array_get(v, n))); + /* ip already has been incremented */ + addr -= 5; - uc_vm_stack_push(vm, k); - ucv_put(v); + if (frame->ip + addr < chunk->entries || + frame->ip + addr >= chunk->entries + chunk->count) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range"); + return; + } - ucv_as_resource(k)->data = (void *)(uintptr_t)(n + 1); + if (!(types & (1u << ucv_type(v)))) { + for (i = 0; i <= depth; i++) + ucv_put(uc_vm_stack_pop(vm)); - return true; + uc_vm_stack_push(vm, NULL); + frame->ip += addr; + } } + static void uc_vm_insn_next(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *k = uc_vm_stack_pop(vm); uc_value_t *v = uc_vm_stack_pop(vm); + uc_value_t *key = NULL, *val = NULL; - switch (ucv_type(v)) { - case UC_OBJECT: - if (uc_vm_object_iterator_next(vm, insn, k, v)) - return; - - break; + if (uc_vm_iterator_next(vm, insn, v, &k, &key, &val)) { + uc_vm_stack_push(vm, key); - case UC_ARRAY: - if (uc_vm_array_iterator_next(vm, insn, k, v)) - return; + if (insn == I_NEXTKV) + uc_vm_stack_push(vm, val); - break; + uc_vm_stack_push(vm, k); + ucv_put(v); - default: - break; + return; } uc_vm_stack_push(vm, NULL); @@ -3094,29 +1125,8 @@ static void uc_vm_insn_print(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *v = uc_vm_stack_pop(vm); - char *p; - - switch (ucv_type(v)) { - case UC_OBJECT: - case UC_ARRAY: - p = ucv_to_jsonstring(vm, v); - fwrite(p, 1, strlen(p), vm->output); - free(p); - break; - - case UC_STRING: - fwrite(ucv_string_get(v), 1, ucv_string_length(v), vm->output); - break; - - case UC_NULL: - break; - - default: - p = ucv_to_string(vm, v); - fwrite(p, 1, strlen(p), vm->output); - free(p); - } + uc_vm_value_print(vm, v); ucv_put(v); } @@ -3125,357 +1135,119 @@ uc_vm_insn_delete(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *k = uc_vm_stack_pop(vm); uc_value_t *v = uc_vm_stack_pop(vm); - bool rv; - - switch (ucv_type(v)) { - case UC_OBJECT: - case UC_ARRAY: - case UC_RESOURCE: - if (assert_mutable_value(vm, v)) { - /* ucv_key_delete() removes own object keys directly and consults the - * __delete__ metamethod for keys the value has no own storage for. - * Arrays and resources have no own-key storage at all, so there the - * metamethod is the only way to handle a key; without one it raises - * the same reference error the default case below reports */ - rv = ucv_key_delete(vm, v, k); - uc_vm_stack_push(vm, ucv_boolean_new(rv)); - } - - break; + uc_value_t *res; - default: - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "left-hand side expression is %s", - v ? "not an object" : "null"); - - break; - } + if (uc_vm_property_delete(vm, v, k, &res)) + uc_vm_stack_push(vm, res); ucv_put(k); ucv_put(v); } -/* a closure that runs again holds the binding of its previous run */ -static void -uc_vm_import_bind(uc_closure_t *closure, size_t slot, uc_upvalref_t *ref) -{ - if (closure->upvals[slot]) - ucv_put(&closure->upvals[slot]->header); - - closure->upvals[slot] = ref; -} - static void uc_vm_insn_import(uc_vm_t *vm, uc_vm_insn_t insn) { uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_program_t *prog = uc_vm_current_program(vm); - uc_modexports_t *exports = uc_vm_run_exports(prog, uc_vector_last(&prog->runs), false); - size_t count = exports ? exports->count : 0; size_t from = vm->arg.u32 & 0xffff; uint16_t to = vm->arg.u32 >> 16; - uc_value_t *name, *modobj; - uint32_t cidx; - - /* is a wildcard import * from ... */ - if (to == 0xffff) { - to = from; - modobj = ucv_object_new(vm); - - /* instruction is followed by u16 containing the offset of the - * first module export and `from` times u32 values containing - * the constant indexes of the names */ - from = frame->ip[0] * 0x100 + frame->ip[1]; + uint32_t *names = NULL; + size_t count, i; - /* read all name operands, also those of a missing export */ - for (frame->ip += 2; to > 0; from++, to--) { - cidx = ( - frame->ip[0] * 0x1000000UL + - frame->ip[1] * 0x10000UL + - frame->ip[2] * 0x100UL + - frame->ip[3] - ); + if (to != 0xffff) { + uc_vm_import(vm, from, to); - frame->ip += 4; - - if (from >= count || !exports->entries[from]) - continue; + return; + } - name = uc_program_get_constant(uc_vm_current_program(vm), cidx); + /* is a wildcard import * from ... */ + count = from; - if (ucv_type(name) == UC_STRING) - ucv_object_add(modobj, ucv_string_get(name), - ucv_get(&exports->entries[from]->header)); + if (count) + names = xalloc(count * sizeof(*names)); - ucv_put(name); - } + /* instruction is followed by u16 containing the offset of the + * first module export and `count` times u32 values containing + * the constant indexes of the names */ + from = frame->ip[0] * 0x100 + frame->ip[1]; - ucv_set_constant(modobj, true); + /* read all name operands, also those of a missing export */ + for (frame->ip += 2, i = 0; i < count; i++) { + names[i] = ( + frame->ip[0] * 0x1000000UL + + frame->ip[1] * 0x10000UL + + frame->ip[2] * 0x100UL + + frame->ip[3] + ); - uc_vm_stack_push(vm, modobj); + frame->ip += 4; } - /* module export available, patch into upvalue */ - else if (from < count && exports->entries[from]) { - ucv_get(&exports->entries[from]->header); - uc_vm_import_bind(frame->closure, to, exports->entries[from]); - } + uc_vm_stack_push(vm, uc_vm_import_namespace(vm, from, names, count)); - /* module export missing, e.g. due to premature return in module, - * patch up dummy upvalue ref with `null` value */ - else { - uc_vm_import_bind(frame->closure, to, (uc_upvalref_t *)ucv_upvalref_new(0)); - frame->closure->upvals[to]->closed = true; - } + free(names); } static void uc_vm_insn_export(uc_vm_t *vm, uc_vm_insn_t insn) { uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_program_t *prog = uc_vm_current_program(vm); - uc_program_run_t *run = uc_vector_last(&prog->runs); - uc_modexports_t *exports = uc_vm_run_exports(prog, run, true); - uc_upvalref_t *ref; + uc_modexports_t *exports = uc_vm_export_target(vm); if (!exports) return; - uc_vm_main_keep(vm, prog, run); - - /* A later run of the closure runs the module bodies again. Its imports - * resolve to the instances of the first run, which stay in place. */ - if (run->cursor++ < exports->count) - return; - - ref = uc_vm_capture_upval(vm, frame->stackframe + vm->arg.u32); - - uc_vector_push(exports, ref); - ucv_get(&ref->header); + uc_vm_export_add(exports, uc_vm_capture_upval(vm, frame->stackframe + vm->arg.u32)); } static void uc_vm_insn_dynload(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *name, *export, *modscope, *modobj; uint16_t count = vm->arg.u32 & 0xffff; uint16_t to = vm->arg.u32 >> 16; + uc_value_t *name, *modscope; uc_callframe_t *frame; uint32_t cidx; - bool found; - /* Attempt to load module. Will raise exception on error */ name = uc_vm_stack_pop(vm); - modscope = uc_require_library(vm, name, true); + modscope = uc_vm_dynload_module(vm, name); ucv_put(name); - if (!modscope || vm->exception.type != EXCEPTION_NONE) { - ucv_put(modscope); - - return; - } - - if (vm->exception.type != EXCEPTION_NONE) { - ucv_put(modscope); - + if (!modscope) return; - } /* If count is zero, we're doing a wildcard import. Shallow copy module * object, mark it constant and patch into the target upvalue. */ if (count == 0) { - modobj = ucv_object_new(vm); - - ucv_object_foreach_entry(modscope, e) - ucv_object_insert_string(modobj, e->key, ucv_get(e->value)); - - ucv_set_constant(modobj, true); + uc_vm_stack_push(vm, uc_vm_dynload_namespace(vm, modscope)); + ucv_put(modscope); - uc_vm_stack_push(vm, modobj); + return; } /* ... otherwise we're importing a specific list of names */ - else { - frame = uc_vm_current_frame(vm); - - /* Instruction is followed by `count` times u32 values containing - * the import name constant indexes */ - while (count > 0) { - cidx = ( - frame->ip[0] * 0x1000000UL + - frame->ip[1] * 0x10000UL + - frame->ip[2] * 0x100UL + - frame->ip[3] - ); - - frame->ip += 4; - - name = uc_program_get_constant(uc_vm_current_program(vm), cidx); - export = ucv_object_get(modscope, ucv_string_get(name), &found); - - if (!found) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "Module does not export %s", - ucv_string_get(name)); - - ucv_put(name); - ucv_put(modscope); - - return; - } - - ucv_put(name); + frame = uc_vm_current_frame(vm); + + /* Instruction is followed by `count` times u32 values containing + * the import name constant indexes. The error context of a missing + * name derives from the instruction pointer, so decode each name only + * when it is bound. */ + for (; count > 0; count--, to++) { + cidx = ( + frame->ip[0] * 0x1000000UL + + frame->ip[1] * 0x10000UL + + frame->ip[2] * 0x100UL + + frame->ip[3] + ); - uc_vm_import_bind(frame->closure, to, (uc_upvalref_t *)ucv_upvalref_new(0)); - frame->closure->upvals[to]->closed = true; - frame->closure->upvals[to]->value = ucv_get(export); + frame->ip += 4; - count--; - to++; - } + if (!uc_vm_dynload_bind(vm, modscope, cidx, to)) + break; } ucv_put(modscope); } -static void -uc_vm_gc_step(uc_vm_t *vm) -{ - size_t curr_count = 0, prev_count = 0; - uc_weakref_t *ref; - - if (!(vm->gc_flags & UC_GC_ENABLED)) - return; - - if (vm->alloc_refs >= vm->gc_interval) { - if (vm->trace) { - for (ref = vm->values.next; ref != &vm->values; ref = ref->next) - prev_count++; - - ucv_gc(vm); - - for (ref = vm->values.next; ref != &vm->values; ref = ref->next) - curr_count++; - - fprintf(stderr, "! GC reclaimed %zu object(s)\n", prev_count - curr_count); - } - else { - ucv_gc(vm); - } - } -} - -static uc_value_t * -uc_vm_callframe_pop(uc_vm_t *vm) -{ - uc_callframe_t *frame = uc_vm_current_frame(vm); - uc_value_t *retval; - - /* close upvalues */ - uc_vm_close_upvals(vm, frame->stackframe); - - if (vm->stack.count > frame->stackframe) - retval = uc_vm_stack_pop(vm); - else - retval = NULL; - - /* reset function stack frame */ - while (vm->stack.count > frame->stackframe) - ucv_put(uc_vm_stack_pop(vm)); - - /* for method calls, release context as well */ - if (frame->mcall) - ucv_put(uc_vm_stack_pop(vm)); - - /* release function */ - if (frame->closure) { - uc_vm_run_end(frame->closure); - ucv_put(&frame->closure->header); - } - - if (frame->cfunction) - ucv_put(&frame->cfunction->header); - - /* release context */ - ucv_put(frame->ctx); - - vm->callframes.count--; - - return retval; -} - -static void -uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex) -{ - uc_value_t *ctx; - - if (ex->type == EXCEPTION_USER) - fprintf(stderr, "%s\n", ex->message); - else - fprintf(stderr, "%s: %s\n", - exception_type_strings[ex->type] ? exception_type_strings[ex->type] : "Error", - ex->message); - - ctx = ucv_object_get(ucv_array_get(ex->stacktrace, 0), "context", NULL); - - if (ctx) - fprintf(stderr, "%s\n", ucv_string_get(ctx)); - - fprintf(stderr, "\n"); -} - -uc_exception_type_t -uc_vm_signal_dispatch(uc_vm_t *vm) -{ - uc_exception_type_t ex; - uc_value_t *handler; - uint64_t mask; - size_t i, j; - int sig, rv; - - /* Check whether the signal self-pipe was actually set up, rather than - * re-checking config->setup_signal_handlers directly: the pipe may - * have been lazily initialized on demand via - * uc_vm_signal_handlers_ensure() after the fact (see lib/debug.c), - * independent of what the original config requested. */ - if (vm->signal.sigpipe[0] < 0) - return EXCEPTION_NONE; - - for (i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) { - if (!vm->signal.raised[i]) - continue; - - do { - rv = read(vm->signal.sigpipe[0], &sig, sizeof(sig)); - } while (rv > 0 || (rv == -1 && errno == EINTR)); - - for (j = 0; j < 64; j++) { - mask = 1ull << j; - - if (vm->signal.raised[i] & mask) { - vm->signal.raised[i] &= ~mask; - - sig = i * 64 + j; - handler = ucv_array_get(vm->signal.handler, sig); - - if (ucv_is_callable(handler)) { - uc_vm_stack_push(vm, ucv_get(handler)); - uc_vm_stack_push(vm, ucv_int64_new(sig)); - - ex = uc_vm_call(vm, false, 1); - - if (ex != EXCEPTION_NONE) - return ex; - - ucv_put(uc_vm_stack_pop(vm)); - } - } - } - } - - return EXCEPTION_NONE; -} - static uc_vm_status_t uc_vm_exception_type_to_status(uc_vm_t *vm) { @@ -3897,197 +1669,17 @@ uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) return status; } -uc_exception_type_t -uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) -{ - uc_value_t *ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL; - uc_value_t *fno = ucv_get(uc_vm_stack_peek(vm, nargs)); - size_t depth = vm->callframes.count; - - uc_vm_clear_exception(vm); - - /* only a closure leaves a new frame behind, a C function that __call__ - * resolves to already ran */ - if (uc_vm_call_function(vm, ctx, fno, mcall, nargs & 0xffff, false) && - vm->callframes.count > depth) - uc_vm_execute_chunk(vm); - - return vm->exception.type; -} - -uc_value_t * -uc_vm_scope_get(uc_vm_t *vm) -{ - return vm->globals; -} - -void -uc_vm_scope_set(uc_vm_t *vm, uc_value_t *ctx) -{ - ucv_put(vm->globals); - vm->globals = ctx; -} - -uc_value_t * -uc_vm_invoke(uc_vm_t *vm, const char *fname, size_t nargs, ...) -{ - uc_exception_type_t ex; - uc_value_t *fno, *arg; - va_list ap; - size_t i; - - fno = ucv_property_get(vm->globals, fname); - - if (!ucv_is_callable(fno)) - return NULL; - - uc_vm_stack_push(vm, ucv_get(fno)); - - va_start(ap, nargs); - - for (i = 0; i < nargs; i++) { - arg = va_arg(ap, uc_value_t *); - uc_vm_stack_push(vm, ucv_get(arg)); - } - - va_end(ap); - - ex = uc_vm_call(vm, false, nargs); - - if (ex) { - if (vm->exhandler) - vm->exhandler(vm, &vm->exception); - - return NULL; - } - - return uc_vm_stack_pop(vm); -} - -uc_exception_handler_t * -uc_vm_exception_handler_get(uc_vm_t *vm) -{ - return vm->exhandler; -} - -void -uc_vm_exception_handler_set(uc_vm_t *vm, uc_exception_handler_t *exhandler) -{ - vm->exhandler = exhandler; -} - -uint32_t -uc_vm_trace_get(uc_vm_t *vm) -{ - return vm->trace; -} - -void -uc_vm_trace_set(uc_vm_t *vm, uint32_t level) -{ - vm->trace = level; -} - -bool -uc_vm_registry_exists(uc_vm_t *vm, const char *key) -{ - bool exists; - - ucv_object_get(vm->registry, key, &exists); - - return exists; -} - -uc_value_t * -uc_vm_registry_get(uc_vm_t *vm, const char *key) -{ - return ucv_object_get(vm->registry, key, NULL); -} - -void -uc_vm_registry_set(uc_vm_t *vm, const char *key, uc_value_t *value) -{ - if (!vm->registry) - vm->registry = ucv_object_new(vm); - - ucv_object_add(vm->registry, key, value); -} - -bool -uc_vm_registry_delete(uc_vm_t *vm, const char *key) -{ - return ucv_object_delete(vm->registry, key); -} - -bool -uc_vm_gc_start(uc_vm_t *vm, uint16_t interval) -{ - bool changed = false; - - if (vm->gc_interval != interval) { - vm->gc_interval = interval; - changed = true; - } - - if (!(vm->gc_flags & UC_GC_ENABLED)) { - vm->gc_flags |= UC_GC_ENABLED; - changed = true; - } - - return changed; -} - -bool -uc_vm_gc_stop(uc_vm_t *vm) -{ - if (!(vm->gc_flags & UC_GC_ENABLED)) - return false; - - vm->gc_flags &= ~UC_GC_ENABLED; - - return true; -} - -void -uc_vm_signal_raise(uc_vm_t *vm, int signo) -{ - uint8_t signum = signo; - - if (signo <= 0 || signo >= UC_SYSTEM_SIGNAL_COUNT) - return; - - vm->signal.raised[signo / 64] |= (1ull << (signo % 64)); - - if (write(vm->signal.sigpipe[1], &signum, sizeof(signum)) == -1) {} -} - -int -uc_vm_signal_notifyfd(uc_vm_t *vm) -{ - return vm->signal.sigpipe[0]; -} - -bool -uc_vm_break_requested(uc_vm_t *vm) -{ - return vm->break_requested; -} - -void -uc_vm_break_request(uc_vm_t *vm) -{ - vm->break_requested = true; - - if (vm->break_notifyfd[1] >= 0) { - char c = 'B'; - if (write(vm->break_notifyfd[1], &c, 1) == -1) {} - } -} +static const uc_vm_interpreter_t uc_vm_bytecode_interpreter = { + .frame_push = uc_vm_frame_push, + .execute = uc_vm_execute_chunk +}; -int -uc_vm_break_notifyfd(uc_vm_t *vm) +/* uc_vm_call() in vmcore.c enters bytecode closures through this hook, so + * that a runtime which links vmcore.c without this file has no interpreter */ +static void __attribute__((constructor)) +uc_vm_bytecode_interpreter_register(void) { - return vm->break_notifyfd[0]; + uc_vm_interpreter_set(&uc_vm_bytecode_interpreter); } uc_vm_status_t diff --git a/vmcore.c b/vmcore.c new file mode 100644 index 00000000..0d101908 --- /dev/null +++ b/vmcore.c @@ -0,0 +1,2736 @@ +/* + * Copyright (C) 2020-2021 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include + +#include "ucode/internal/vmcore.h" +#include "ucode/internal/chunk.h" +#include "ucode/internal/lib.h" /* uc_error_context_format() */ +#include "ucode/internal/source.h" /* uc_source_callee_expr() */ +#include "ucode/internal/platform.h" + +/* maximum length of the value rendered into a "not a function" error message */ +#define UC_VALUE_HINT_MAXLEN 64 + +#undef __insn +#define __insn(_name) #_name, + +static const char *insn_names[__I_MAX] = { + __insns +}; + +const int8_t uc_vm_insn_format[__I_MAX] = { + [I_LOAD] = 4, + [I_LOAD8] = 1, + [I_LOAD16] = 2, + [I_LOAD32] = 4, + + [I_LREXP] = 4, + + [I_LLOC] = 4, + [I_LVAR] = 4, + [I_LUPV] = 4, + + [I_CLFN] = 4, + [I_ARFN] = 4, + + [I_SLOC] = 4, + [I_SUPV] = 4, + [I_SVAR] = 4, + + [I_ULOC] = 4, + [I_UUPV] = 4, + [I_UVAR] = 4, + [I_UVAL] = 1, + + [I_NARR] = 4, + [I_PARR] = 4, + + [I_NOBJ] = 4, + [I_SOBJ] = 4, + + [I_JMP] = -4, + [I_JMPZ] = -4, + [I_JMPNT] = 4, + + [I_COPY] = 1, + + [I_CALL] = 4, + + [I_IMPORT] = 4, + [I_EXPORT] = 4, + [I_DYNLOAD] = 4, + + [I_STCALL] = 4 +}; + +const char *exception_type_strings[] = { + [EXCEPTION_SYNTAX] = "Syntax error", + [EXCEPTION_RUNTIME] = "Runtime error", + [EXCEPTION_TYPE] = "Type error", + [EXCEPTION_REFERENCE] = "Reference error", + [EXCEPTION_USER] = "Error", + [EXCEPTION_EXIT] = "Exit" +}; + +const char * +uc_vm_insn_to_name(uc_vm_insn_t insn) +{ + if (insn < 0 || insn >= __I_MAX) + return "(unknown)"; + + return insn_names[insn]; +} + +int8_t +uc_vm_insn_to_argtype(uc_vm_insn_t insn) +{ + if (insn < 0 || insn >= __I_MAX) + return 0; + + return uc_vm_insn_format[insn]; +} + +static void +uc_vm_reset_stack(uc_vm_t *vm) +{ + while (vm->stack.count > 0) { + vm->stack.count--; + ucv_put(vm->stack.entries[vm->stack.count]); + vm->stack.entries[vm->stack.count] = NULL; + } +} + +void +uc_vm_reset_callframes(uc_vm_t *vm, size_t depth) +{ + while (vm->callframes.count > depth) + ucv_put(uc_vm_callframe_pop(vm)); +} + +static void +uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex); + +static uc_value_t * +uc_vm_alloc_global_scope(uc_vm_t *vm) +{ + uc_value_t *scope, *arr; + size_t i; + + scope = ucv_object_new(vm); + + /* build default require() search path */ + arr = ucv_array_new(vm); + + for (i = 0; i < vm->config->module_search_path.count; i++) + ucv_array_push(arr, ucv_string_new(vm->config->module_search_path.entries[i])); + + /* register module related constants */ + ucv_object_add(scope, "REQUIRE_SEARCH_PATH", arr); + ucv_object_add(scope, "modules", ucv_object_new(vm)); + + /* register scope math constants */ + ucv_object_add(scope, "NaN", ucv_double_new(NAN)); + ucv_object_add(scope, "Infinity", ucv_double_new(INFINITY)); + + /* register global property */ + ucv_object_add(scope, "global", ucv_get(scope)); + + uc_vm_scope_set(vm, scope); + + return scope; +} + +static void +uc_vm_signal_handler(int sig) +{ + uc_vm_t *vm = uc_thread_context_get()->signal_handler_vm; + + assert(vm); + + uc_vm_signal_raise(vm, sig); +} + +/* Actually wire up the self-pipe/handler array/sigaction template needed + * for ucode-level signal() callbacks to work, independent of whether the + * embedding host opted into this via config->setup_signal_handlers. Safe + * to call more than once (a no-op once already set up for this thread). */ +void +uc_vm_signal_handlers_ensure(uc_vm_t *vm) +{ + uc_thread_context_t *tctx = uc_thread_context_get(); + + if (tctx->signal_handler_vm) + return; + + if (pipe2(vm->signal.sigpipe, O_CLOEXEC | O_NONBLOCK) != 0) + return; + + vm->signal.handler = ucv_array_new_length(vm, UC_SYSTEM_SIGNAL_COUNT); + + vm->signal.sa.sa_handler = uc_vm_signal_handler; + vm->signal.sa.sa_flags = SA_RESTART | SA_ONSTACK; + sigemptyset(&vm->signal.sa.sa_mask); + + tctx->signal_handler_vm = vm; +} + +static void +uc_vm_signal_handlers_setup(uc_vm_t *vm) +{ + memset(&vm->signal, 0, sizeof(vm->signal)); + + vm->signal.sigpipe[0] = -1; + vm->signal.sigpipe[1] = -1; + + if (!vm->config->setup_signal_handlers) + return; + + uc_vm_signal_handlers_ensure(vm); +} + +static void +uc_vm_signal_handlers_reset(uc_vm_t *vm) +{ + uc_thread_context_t *tctx = uc_thread_context_get(); + struct sigaction sa = { 0 }; + size_t i, signo; + + if (vm != tctx->signal_handler_vm) + return; + + sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + + for (signo = 0; signo < ucv_array_length(vm->signal.handler); signo++) + if (ucv_is_callable(ucv_array_get(vm->signal.handler, signo))) + sigaction(signo, &sa, NULL); + + for (i = 0; i < ARRAY_SIZE(vm->signal.sigpipe); i++) { + if (vm->signal.sigpipe[i] > STDERR_FILENO) + close(vm->signal.sigpipe[i]); + + vm->signal.sigpipe[i] = -1; + } + + tctx->signal_handler_vm = NULL; +} + +void uc_vm_break_init(uc_vm_t *vm) +{ + vm->break_requested = false; + vm->break_notifyfd[0] = -1; + vm->break_notifyfd[1] = -1; + + if (pipe2(vm->break_notifyfd, O_CLOEXEC | O_NONBLOCK) == 0) { + /* pipe created successfully */ + } +} + +void uc_vm_break_cleanup(uc_vm_t *vm) +{ + for (size_t i = 0; i < ARRAY_SIZE(vm->break_notifyfd); i++) { + if (vm->break_notifyfd[i] > STDERR_FILENO) + close(vm->break_notifyfd[i]); + vm->break_notifyfd[i] = -1; + } +} + +void uc_vm_init(uc_vm_t *vm, uc_parse_config_t *config) +{ + vm->exception.type = EXCEPTION_NONE; + vm->exception.message = NULL; + + vm->config = config ? config : &uc_default_parse_config; + + vm->open_upvals = NULL; + + vm->values.prev = &vm->values; + vm->values.next = &vm->values; + + vm->strbuf = NULL; + + vm->output = stdout; + + uc_vm_reset_stack(vm); + + uc_vm_alloc_global_scope(vm); + + uc_vm_exception_handler_set(vm, uc_vm_output_exception); + + uc_vm_trace_set(vm, 0); + + uc_vm_signal_handlers_setup(vm); + + uc_vm_break_init(vm); + + uc_thread_context_get()->refcount++; +} + +void uc_vm_free(uc_vm_t *vm) +{ + uc_thread_context_t *ctx; + uc_upvalref_t *ref; + size_t i; + + uc_vm_signal_handlers_reset(vm); + + uc_vm_break_cleanup(vm); + + ucv_put(vm->exception.stacktrace); + free(vm->exception.message); + + while (vm->open_upvals) { + ref = vm->open_upvals->next; + ucv_put(&vm->open_upvals->header); + vm->open_upvals = ref; + } + + /* Resource type prototypes are part of the VM value pool, so release the + * restype refs before the final GC; any references still held by + * GC-managed values are dropped by ucv_freeall() itself. */ + for (i = 0; i < vm->restypes.count; i++) + ucv_put(vm->restypes.entries[i]->proto); + + uc_vm_reset_callframes(vm, 0); + uc_vm_reset_stack(vm); + uc_vector_clear(&vm->stack); + uc_vector_clear(&vm->callframes); + + uc_stringbuf_free(vm->strbuf); + + ucv_freeall(vm); + + for (i = 0; i < vm->restypes.count; i++) + free(vm->restypes.entries[i]); + + uc_vector_clear(&vm->restypes); + + for (i = 0; i < vm->breakpoints.count; i++) + free(vm->breakpoints.entries[i]); + + uc_vector_clear(&vm->breakpoints); + + ctx = uc_thread_context_get(); + + assert(ctx->refcount > 0); + + if (--ctx->refcount == 0) + uc_thread_context_free(); +} + +char * +uc_vm_format_val(uc_vm_t *vm, uc_value_t *val) +{ + if (!vm->strbuf) + vm->strbuf = uc_stringbuf_new(); + else + uc_stringbuf_reset(vm->strbuf); + + ucv_to_stringbuf(NULL, vm->strbuf, val, true); + + if (uc_stringbuf_length(vm->strbuf) >= 64) { + uc_stringbuf_memset(vm->strbuf, 60, '.', 3); + uc_stringbuf_memset(vm->strbuf, 63, 0, 1); + } + + return vm->strbuf->buf; +} + +void +uc_vm_frame_dump(uc_vm_t *vm, uc_callframe_t *frame) +{ + uc_chunk_t *chunk = uc_vm_frame_chunk(frame); + uc_function_t *function; + uc_closure_t *closure; + uc_upvalref_t *ref; + uc_value_t *v; + size_t i; + + fprintf(stderr, " [*] CALLFRAME[%zx]\n", + frame - vm->callframes.entries); + + fprintf(stderr, " |- stackframe %zu/%zu\n", + frame->stackframe, vm->stack.count); + + fprintf(stderr, " |- ctx %s\n", + uc_vm_format_val(vm, frame->ctx)); + + if (frame->tco) + fprintf(stderr, " |- tailcall (%u callframes omitted)\n", + frame->tco); + + if (chunk) { + closure = frame->closure; + function = closure->function; + + fprintf(stderr, " `- %zu upvalues\n", + function->nupvals); + + for (i = 0; i < function->nupvals; i++) { + ref = closure->upvals[i]; + v = uc_chunk_debug_get_variable(chunk, 0, i, true); + + fprintf(stderr, " [%zu] <%p> %s ", + i, (void *)ref, uc_vm_format_val(vm, v)); + + if (!ref) { + fprintf(stderr, "{unresolved}\n"); + } + else if (ref->closed) { + fprintf(stderr, "{closed} %s\n", + uc_vm_format_val(vm, ref->value)); + } + else { + fprintf(stderr, "{open[%zu]} %s\n", + ref->slot, + uc_vm_format_val(vm, vm->stack.entries[ref->slot])); + } + + ucv_put(v); + } + } +} + +uc_value_t * +uc_vm_resolve_upval(uc_vm_t *vm, uc_value_t *value) +{ + uc_upvalref_t *ref; + uc_value_t *rv; + +#ifdef __clang_analyzer__ + /* Clang static analyzer does not understand that ucv_type(NULL) can't + * possibly yield UC_UPVALUE. Nudge it. */ + if (value != NULL && ucv_type(value) == UC_UPVALUE) +#else + if (ucv_type(value) == UC_UPVALUE) +#endif + { + ref = ucv_as_upvalue(value); + + if (ref->closed) + rv = ucv_get(ref->value); + else + rv = ucv_get(vm->stack.entries[ref->slot]); + + ucv_put(value); + + return rv; + } + + return value; +} + +void +uc_vm_stack_push(uc_vm_t *vm, uc_value_t *value) +{ + uc_vector_push(&vm->stack, uc_vm_resolve_upval(vm, value)); + + if (vm->trace) { + fprintf(stderr, " [+%zd] %s\n", + vm->stack.count - 1, + uc_vm_format_val(vm, vm->stack.entries[vm->stack.count - 1])); + } +} + +uc_value_t * +uc_vm_stack_pop(uc_vm_t *vm) +{ + uc_value_t *rv; + + vm->stack.count--; + rv = vm->stack.entries[vm->stack.count]; + vm->stack.entries[vm->stack.count] = NULL; + + if (vm->trace) { + fprintf(stderr, " [-%zd] %s\n", + vm->stack.count, + uc_vm_format_val(vm, rv)); + } + + return rv; +} + +uc_value_t * +uc_vm_stack_peek(uc_vm_t *vm, size_t offset) +{ + if (offset >= vm->stack.count) + return NULL; + + return vm->stack.entries[vm->stack.count + (-1 - offset)]; +} + +void +uc_vm_stack_set(uc_vm_t *vm, size_t offset, uc_value_t *value) +{ + if (vm->trace) { + fprintf(stderr, " [!%zu] %s\n", + offset, + uc_vm_format_val(vm, value)); + } + + ucv_put(vm->stack.entries[offset]); + vm->stack.entries[offset] = value; +} + +void +uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall, size_t nargs) +{ + uc_value_t *res = NULL; + uc_callframe_t *frame; + + /* add new callframe */ + frame = uc_vector_push(&vm->callframes, { + .stackframe = vm->stack.count - nargs - 1, + .cfunction = fptr, + .closure = NULL, + .ctx = ctx, + .mcall = mcall + }); + + if (vm->trace) + uc_vm_frame_dump(vm, frame); + + res = fptr->cfn(vm, nargs); + + /* Reset stack, check for callframe depth since an uncatched exception in managed + * code executed by fptr->cfn() could've reset the callframe stack already. */ + if (vm->callframes.count > 0) + ucv_put(uc_vm_callframe_pop(vm)); + + /* push return value */ + if (!vm->exception.type) + uc_vm_stack_push(vm, res); + else + ucv_put(res); +} + +/* The exports of the modules that a program links statically belong to the + * closure of its entry function that runs them, and live as long as that + * closure. An exported function holds the program, so exports that the program + * owned itself would keep the program alive. */ +static bool +uc_vm_closure_is_entry(uc_closure_t *closure) +{ + uc_function_t *function = closure->function; + + return function->program->functions.prev == &function->progref; +} + +void +uc_vm_run_begin(uc_closure_t *closure, bool keep) +{ + if (uc_vm_closure_is_entry(closure)) + uc_vector_push(&closure->function->program->runs, { .closure = closure, .keep = keep }); +} + +void +uc_vm_run_end(uc_closure_t *closure) +{ + if (uc_vm_closure_is_entry(closure)) + uc_vector_pop(&closure->function->program->runs); +} + +/* Returns the expression invoked by the call that the instruction the VM is + * about to execute belongs to, or NULL if it cannot be determined. The result + * is meant for error messages describing a failed call, and must be + * released using free(). */ +char * +uc_vm_callee_expr(uc_vm_t *vm) +{ + uc_function_t *function; + uc_callframe_t *frame; + size_t insn; + + /* the topmost frame is the one performing the failing call; if it belongs + * to a native function, the call did not originate from source code */ + if (vm->callframes.count == 0) + return NULL; + + frame = &vm->callframes.entries[vm->callframes.count - 1]; + + if (!frame->closure) + return NULL; + + function = frame->closure->function; + insn = (frame->ip - uc_vm_frame_chunk(frame)->entries) - 1; + + /* a call instruction refers to the closing parenthesis of the arguments + * passed to it */ + return uc_source_callee_expr(uc_program_function_source(function), + uc_program_function_srcpos(function, insn)); +} + +static uc_value_t * +uc_vm_exception_tostring(uc_vm_t *vm, size_t nargs) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_value_t *message = ucv_object_get(frame->ctx, "message", NULL); + + return message ? ucv_get(message) : ucv_string_new("Exception"); +} + +uc_value_t * +uc_vm_exception_object(uc_vm_t *vm) +{ + uc_exception_type_t type = vm->exception.type; + const char *message = vm->exception.message; + uc_value_t *stacktrace = vm->exception.stacktrace; + uc_value_t *exception_prototype = uc_vm_registry_get(vm, "vm.exception.proto"); + uc_value_t *exo; + + if (exception_prototype == NULL) { + exception_prototype = ucv_object_new(vm); + + ucv_object_add(exception_prototype, "tostring", + ucv_cfunction_new("tostring", uc_vm_exception_tostring)); + + uc_vm_registry_set(vm, "vm.exception.proto", exception_prototype); + } + + exo = ucv_object_new(vm); + + ucv_object_add(exo, "type", ucv_string_new(exception_type_strings[type])); + ucv_object_add(exo, "message", ucv_string_new(message)); + ucv_object_add(exo, "stacktrace", ucv_get(stacktrace)); + + ucv_prototype_set(exo, ucv_get(exception_prototype)); + + return exo; +} + +void +uc_vm_clear_exception(uc_vm_t *vm) +{ + vm->exception.type = EXCEPTION_NONE; + + ucv_put(vm->exception.stacktrace); + vm->exception.stacktrace = NULL; + + free(vm->exception.message); + vm->exception.message = NULL; +} + +/* Adds the line and byte column of the source position `off` to `object`, under + * the keys `linekey` and `bytekey`. */ +static void +uc_vm_stacktrace_add_position(uc_value_t *object, uc_source_t *source, size_t off, + const char *linekey, const char *bytekey) +{ + size_t pos = off; + + ucv_object_add(object, linekey, ucv_int64_new(uc_source_get_line(source, &pos))); + ucv_object_add(object, bytekey, ucv_int64_new(pos)); +} + +/* Adds the bounds of the statement enclosing instruction `off` of `function`, + * so that consumers rendering a stacktrace can highlight the whole statement + * rather than only the failing position. */ +static void +uc_vm_stacktrace_add_statement(uc_vm_t *vm, uc_value_t *entry, uc_function_t *function, size_t off) +{ + uc_source_t *source = uc_program_function_source(function); + size_t startoff, endoff; + uc_value_t *statement; + + if (!uc_program_function_stmt_bounds(function, off, &startoff, &endoff)) + return; + + statement = ucv_object_new(vm); + + uc_vm_stacktrace_add_position(statement, source, startoff, "line", "byte"); + uc_vm_stacktrace_add_position(statement, source, endoff, "endline", "endbyte"); + + ucv_object_add(entry, "statement", statement); +} + +static uc_value_t * +uc_vm_capture_stacktrace(uc_vm_t *vm, size_t i) +{ + uc_value_t *stacktrace, *entry, *last = NULL; + uc_function_t *function; + uc_callframe_t *frame; + uc_source_t *source; + size_t off, srcpos; + char *name; + + stacktrace = ucv_array_new(vm); + + for (; i > 0; i--) { + frame = &vm->callframes.entries[i - 1]; + entry = ucv_object_new(vm); + + if (frame->closure) { + function = frame->closure->function; + source = uc_program_function_source(function); + + off = (frame->ip - uc_vm_frame_chunk(frame)->entries) - 1; + srcpos = uc_program_function_srcpos(function, off); + + ucv_object_add(entry, "filename", ucv_string_new(source->filename)); + uc_vm_stacktrace_add_position(entry, source, srcpos, "line", "byte"); + + uc_vm_stacktrace_add_statement(vm, entry, function, off); + } + + if (i > 1) { + if (frame->closure) { + if (frame->closure->function->name[0]) + name = frame->closure->function->name; + else if (frame->closure->is_arrow) + name = "[arrow function]"; + else + name = "[anonymous function]"; + } + else { + name = frame->cfunction->name; + } + + ucv_object_add(entry, "function", ucv_string_new(name)); + } + + /* record the number of call frames collapsed into this one by tail call + * optimization, so consumers can indicate the missing frames */ + if (frame->tco) + ucv_object_add(entry, "tco", ucv_int64_new(frame->tco)); + + if (!ucv_is_equal(last, entry)) { + ucv_array_push(stacktrace, entry); + last = entry; + } + else { + ucv_put(entry); + } + } + + return stacktrace; +} + +static uc_value_t * +uc_vm_get_error_context(uc_vm_t *vm) +{ + size_t offset, insn, i, byte, line, startoff = 0, endoff = 0; + uc_value_t *stacktrace; + uc_callframe_t *frame; + uc_stringbuf_t *buf; + uc_chunk_t *chunk; + + if (vm->callframes.count == 0) + return NULL; + + /* skip to first non-native function call frame */ + for (i = vm->callframes.count; i > 1; i--) + if (vm->callframes.entries[i - 1].closure) + break; + + frame = &vm->callframes.entries[i - 1]; + + if (!frame->closure) + return NULL; + + chunk = uc_vm_frame_chunk(frame); + insn = (frame->ip - chunk->entries) - 1; + offset = uc_program_function_srcpos(frame->closure->function, insn); + stacktrace = uc_vm_capture_stacktrace(vm, i); + + buf = ucv_stringbuf_new(); + + byte = offset; + line = uc_source_get_line(uc_program_function_source(frame->closure->function), &byte); + + if (line) { + /* underline the statement the failing instruction belongs to, where one + * can be resolved, rather than only marking that instruction */ + uc_program_function_stmt_bounds(frame->closure->function, insn, &startoff, &endoff); + + uc_error_context_format(buf, uc_vm_frame_source(frame), stacktrace, + startoff, endoff, offset); + } + else if (frame->ip != chunk->entries) + ucv_stringbuf_printf(buf, "At instruction %zu", (frame->ip - chunk->entries) - 1); + else + ucv_stringbuf_append(buf, "At start of program"); + + ucv_object_add(ucv_array_get(stacktrace, 0), "context", ucv_stringbuf_finish(buf)); + + return stacktrace; +} + +void __attribute__((format(printf, 3, 0))) +uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, ...) +{ + va_list ap; + + vm->exception.type = type; + + free(vm->exception.message); + + va_start(ap, fmt); + xvasprintf(&vm->exception.message, fmt, ap); + va_end(ap); + + ucv_put(vm->exception.stacktrace); + vm->exception.stacktrace = uc_vm_get_error_context(vm); +} + +void +uc_vm_close_upvals(uc_vm_t *vm, size_t slot) +{ + uc_upvalref_t *ref; + + while (vm->open_upvals && vm->open_upvals->slot >= slot) { + ref = vm->open_upvals; + ref->value = ucv_get(vm->stack.entries[ref->slot]); + ref->closed = true; + + if (vm->trace) { + fprintf(stderr, " {!%zu} <%p> %s\n", ref->slot, + (void *)ref, + uc_vm_format_val(vm, ref->value)); + } + + vm->open_upvals = ref->next; + ucv_put(&ref->header); + } +} + +void +uc_vm_gc_step(uc_vm_t *vm) +{ + size_t curr_count = 0, prev_count = 0; + uc_weakref_t *ref; + + if (!(vm->gc_flags & UC_GC_ENABLED)) + return; + + if (vm->alloc_refs >= vm->gc_interval) { + if (vm->trace) { + for (ref = vm->values.next; ref != &vm->values; ref = ref->next) + prev_count++; + + ucv_gc(vm); + + for (ref = vm->values.next; ref != &vm->values; ref = ref->next) + curr_count++; + + fprintf(stderr, "! GC reclaimed %zu object(s)\n", prev_count - curr_count); + } + else { + ucv_gc(vm); + } + } +} + +uc_value_t * +uc_vm_callframe_pop(uc_vm_t *vm) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_value_t *retval; + + /* close upvalues */ + uc_vm_close_upvals(vm, frame->stackframe); + + if (vm->stack.count > frame->stackframe) + retval = uc_vm_stack_pop(vm); + else + retval = NULL; + + /* reset function stack frame */ + while (vm->stack.count > frame->stackframe) + ucv_put(uc_vm_stack_pop(vm)); + + /* for method calls, release context as well */ + if (frame->mcall) + ucv_put(uc_vm_stack_pop(vm)); + + /* release function */ + if (frame->closure) { + uc_vm_run_end(frame->closure); + ucv_put(&frame->closure->header); + } + + if (frame->cfunction) + ucv_put(&frame->cfunction->header); + + /* release context */ + ucv_put(frame->ctx); + + vm->callframes.count--; + + return retval; +} + +static void +uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex) +{ + uc_value_t *ctx; + + if (ex->type == EXCEPTION_USER) + fprintf(stderr, "%s\n", ex->message); + else + fprintf(stderr, "%s: %s\n", + exception_type_strings[ex->type] ? exception_type_strings[ex->type] : "Error", + ex->message); + + ctx = ucv_object_get(ucv_array_get(ex->stacktrace, 0), "context", NULL); + + if (ctx) + fprintf(stderr, "%s\n", ucv_string_get(ctx)); + + fprintf(stderr, "\n"); +} + +uc_exception_type_t +uc_vm_signal_dispatch(uc_vm_t *vm) +{ + uc_exception_type_t ex; + uc_value_t *handler; + uint64_t mask; + size_t i, j; + int sig, rv; + + /* Check whether the signal self-pipe was actually set up, rather than + * re-checking config->setup_signal_handlers directly: the pipe may + * have been lazily initialized on demand via + * uc_vm_signal_handlers_ensure() after the fact (see lib/debug.c), + * independent of what the original config requested. */ + if (vm->signal.sigpipe[0] < 0) + return EXCEPTION_NONE; + + for (i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) { + if (!vm->signal.raised[i]) + continue; + + do { + rv = read(vm->signal.sigpipe[0], &sig, sizeof(sig)); + } while (rv > 0 || (rv == -1 && errno == EINTR)); + + for (j = 0; j < 64; j++) { + mask = 1ull << j; + + if (vm->signal.raised[i] & mask) { + vm->signal.raised[i] &= ~mask; + + sig = i * 64 + j; + handler = ucv_array_get(vm->signal.handler, sig); + + if (ucv_is_callable(handler)) { + uc_vm_stack_push(vm, ucv_get(handler)); + uc_vm_stack_push(vm, ucv_int64_new(sig)); + + ex = uc_vm_call(vm, false, 1); + + if (ex != EXCEPTION_NONE) + return ex; + + ucv_put(uc_vm_stack_pop(vm)); + } + } + } + } + + return EXCEPTION_NONE; +} + +uc_value_t * +uc_vm_scope_get(uc_vm_t *vm) +{ + return vm->globals; +} + +void +uc_vm_scope_set(uc_vm_t *vm, uc_value_t *ctx) +{ + ucv_put(vm->globals); + vm->globals = ctx; +} + +uc_value_t * +uc_vm_invoke(uc_vm_t *vm, const char *fname, size_t nargs, ...) +{ + uc_exception_type_t ex; + uc_value_t *fno, *arg; + va_list ap; + size_t i; + + fno = ucv_property_get(vm->globals, fname); + + if (!ucv_is_callable(fno)) + return NULL; + + uc_vm_stack_push(vm, ucv_get(fno)); + + va_start(ap, nargs); + + for (i = 0; i < nargs; i++) { + arg = va_arg(ap, uc_value_t *); + uc_vm_stack_push(vm, ucv_get(arg)); + } + + va_end(ap); + + ex = uc_vm_call(vm, false, nargs); + + if (ex) { + if (vm->exhandler) + vm->exhandler(vm, &vm->exception); + + return NULL; + } + + return uc_vm_stack_pop(vm); +} + +uc_exception_handler_t * +uc_vm_exception_handler_get(uc_vm_t *vm) +{ + return vm->exhandler; +} + +void +uc_vm_exception_handler_set(uc_vm_t *vm, uc_exception_handler_t *exhandler) +{ + vm->exhandler = exhandler; +} + +uint32_t +uc_vm_trace_get(uc_vm_t *vm) +{ + return vm->trace; +} + +void +uc_vm_trace_set(uc_vm_t *vm, uint32_t level) +{ + vm->trace = level; +} + +bool +uc_vm_registry_exists(uc_vm_t *vm, const char *key) +{ + bool exists; + + ucv_object_get(vm->registry, key, &exists); + + return exists; +} + +uc_value_t * +uc_vm_registry_get(uc_vm_t *vm, const char *key) +{ + return ucv_object_get(vm->registry, key, NULL); +} + +void +uc_vm_registry_set(uc_vm_t *vm, const char *key, uc_value_t *value) +{ + if (!vm->registry) + vm->registry = ucv_object_new(vm); + + ucv_object_add(vm->registry, key, value); +} + +bool +uc_vm_registry_delete(uc_vm_t *vm, const char *key) +{ + return ucv_object_delete(vm->registry, key); +} + +bool +uc_vm_gc_start(uc_vm_t *vm, uint16_t interval) +{ + bool changed = false; + + if (vm->gc_interval != interval) { + vm->gc_interval = interval; + changed = true; + } + + if (!(vm->gc_flags & UC_GC_ENABLED)) { + vm->gc_flags |= UC_GC_ENABLED; + changed = true; + } + + return changed; +} + +bool +uc_vm_gc_stop(uc_vm_t *vm) +{ + if (!(vm->gc_flags & UC_GC_ENABLED)) + return false; + + vm->gc_flags &= ~UC_GC_ENABLED; + + return true; +} + +void +uc_vm_signal_raise(uc_vm_t *vm, int signo) +{ + uint8_t signum = signo; + + if (signo <= 0 || signo >= UC_SYSTEM_SIGNAL_COUNT) + return; + + vm->signal.raised[signo / 64] |= (1ull << (signo % 64)); + + if (write(vm->signal.sigpipe[1], &signum, sizeof(signum)) == -1) {} +} + +int +uc_vm_signal_notifyfd(uc_vm_t *vm) +{ + return vm->signal.sigpipe[0]; +} + +bool +uc_vm_break_requested(uc_vm_t *vm) +{ + return vm->break_requested; +} + +void +uc_vm_break_request(uc_vm_t *vm) +{ + vm->break_requested = true; + + if (vm->break_notifyfd[1] >= 0) { + char c = 'B'; + if (write(vm->break_notifyfd[1], &c, 1) == -1) {} + } +} + +int +uc_vm_break_notifyfd(uc_vm_t *vm) +{ + return vm->break_notifyfd[0]; +} + +static bool +uc_vm_test_strict_equality(uc_value_t *v1, uc_value_t *v2, bool nan_equal) +{ + uc_type_t t1 = ucv_type(v1); + uc_type_t t2 = ucv_type(v2); + double d1, d2; + + if (t1 != t2) + return false; + + switch (t1) { + case UC_DOUBLE: + d1 = ucv_as_double(v1)->dbl; + d2 = ucv_as_double(v2)->dbl; + + if (isnan(d1) && isnan(d2)) + return nan_equal; + + return (d1 == d2); + + case UC_NULL: + case UC_BOOLEAN: + case UC_INTEGER: + case UC_STRING: + return ucv_is_equal(v1, v2); + + default: + return (v1 == v2); + } +} + +static int64_t +int64(uc_value_t *nv, uint64_t *u64) +{ + int64_t n; + + n = ucv_int64_get(nv); + *u64 = 0; + + if (errno == ERANGE) { + n = INT64_MAX; + *u64 = ucv_uint64_get(nv); + } + + return n; +} + +static uint64_t +abs64(int64_t n) +{ + if (n == INT64_MIN) + return 0x8000000000000000ULL; + + if (n < 0) + return -n; + + return n; +} + +uc_value_t * +uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) +{ + uc_value_t *nv1, *nv2, *rv = NULL; + uint64_t u1, u2; + int64_t n1, n2; + + nv1 = ucv_to_number(value); + nv2 = ucv_to_number(operand); + + n1 = int64(nv1, &u1); + n2 = int64(nv2, &u2); + + if (n1 < 0 || n2 < 0) { + switch (operation) { + case I_LSHIFT: + rv = ucv_int64_new(n1 << n2); + break; + + case I_RSHIFT: + rv = ucv_int64_new(n1 >> n2); + break; + + case I_BAND: + rv = ucv_int64_new(n1 & n2); + break; + + case I_BXOR: + rv = ucv_int64_new(n1 ^ n2); + break; + + case I_BOR: + rv = ucv_int64_new(n1 | n2); + break; + + default: + break; + } + } + else { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + switch (operation) { + case I_LSHIFT: + rv = ucv_uint64_new(u1 << (u2 % (sizeof(uint64_t) * CHAR_BIT))); + break; + + case I_RSHIFT: + rv = ucv_uint64_new(u1 >> (u2 % (sizeof(uint64_t) * CHAR_BIT))); + break; + + case I_BAND: + rv = ucv_uint64_new(u1 & u2); + break; + + case I_BXOR: + rv = ucv_uint64_new(u1 ^ u2); + break; + + case I_BOR: + rv = ucv_uint64_new(u1 | u2); + break; + + default: + break; + } + } + + ucv_put(nv1); + ucv_put(nv2); + + return rv; +} + +static uc_value_t * +uc_vm_string_concat(uc_vm_t *vm, uc_value_t *v1, uc_value_t *v2) +{ + char buf[sizeof(void *)], *s1, *s2, *str; + uc_value_t *ustr; + uc_stringbuf_t *sbuf; + size_t l1, l2; + + /* optimize cases for string+string concat... */ + if (ucv_type(v1) == UC_STRING && ucv_type(v2) == UC_STRING) { + s1 = ucv_string_get(v1); + s2 = ucv_string_get(v2); + l1 = ucv_string_length(v1); + l2 = ucv_string_length(v2); + + /* ... result fits into a tagged pointer */ + if (l1 + l2 + 1 < sizeof(buf)) { + memcpy(&buf[0], s1, l1); + memcpy(&buf[l1], s2, l2); + + return ucv_string_new_length(buf, l1 + l2); + } + else { + ustr = ucv_string_alloc(&str, l1 + l2); + memcpy(&str[0], s1, l1); + memcpy(&str[l1], s2, l2); + + return ustr; + } + } + + sbuf = ucv_stringbuf_new(); + + ucv_to_stringbuf(vm, sbuf, v1, false); + ucv_to_stringbuf(vm, sbuf, v2, false); + + return ucv_stringbuf_finish(sbuf); +} + +static uint64_t +upow64(uint64_t base, uint64_t exponent) +{ + uint64_t result = 1; + + while (exponent) { + if (exponent & 1) + result *= base; + + exponent >>= 1; + base *= base; + } + + return result; +} + +uc_value_t * +uc_vm_value_arith(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) +{ + uc_value_t *nv1, *nv2, *rv = NULL; + uint64_t u1, u2; + int64_t n1, n2; + double d1, d2; + + if (operation == I_LSHIFT || operation == I_RSHIFT || + operation == I_BAND || operation == I_BXOR || operation == I_BOR) + return uc_vm_value_bitop(vm, operation, value, operand); + + if (operation == I_ADD && (ucv_type(value) == UC_STRING || ucv_type(operand) == UC_STRING)) + return uc_vm_string_concat(vm, value, operand); + + nv1 = ucv_to_number(value); + nv2 = ucv_to_number(operand); + + /* any operation involving NaN results in NaN */ + if (!nv1 || !nv2) { + ucv_put(nv1); + ucv_put(nv2); + + return ucv_double_new(NAN); + } + if (ucv_type(nv1) == UC_DOUBLE || ucv_type(nv2) == UC_DOUBLE) { + d1 = ucv_double_get(nv1); + d2 = ucv_double_get(nv2); + + switch (operation) { + case I_ADD: + case I_PLUS: + rv = ucv_double_new(d1 + d2); + break; + + case I_SUB: + case I_MINUS: + rv = ucv_double_new(d1 - d2); + break; + + case I_MUL: + rv = ucv_double_new(d1 * d2); + break; + + case I_DIV: + if (d2 == 0.0) + rv = ucv_double_new(d1 / d2); + else if (isnan(d2)) + rv = ucv_double_new(NAN); + else if (!isfinite(d2)) + rv = ucv_double_new(isfinite(d1) ? 0.0 : NAN); + else + rv = ucv_double_new(d1 / d2); + + break; + + case I_MOD: + rv = ucv_double_new(fmod(d1, d2)); + break; + + case I_EXP: + rv = ucv_double_new(pow(d1, d2)); + break; + + default: + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "undefined arithmetic operation %d", + operation); + break; + } + } + else { + n1 = int64(nv1, &u1); + n2 = int64(nv2, &u2); + + switch (operation) { + case I_ADD: + case I_PLUS: + if (n1 < 0 || n2 < 0) { + if (u1) + rv = ucv_uint64_new(u1 - abs64(n2)); + else if (u2) + rv = ucv_uint64_new(u2 - abs64(n1)); + else + rv = ucv_int64_new(n1 + n2); + } + else { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + rv = ucv_uint64_new(u1 + u2); + } + + break; + + case I_SUB: + case I_MINUS: + if (n1 < 0 && n2 < 0) { + if (n1 > n2) + rv = ucv_uint64_new(abs64(n2) - abs64(n1)); + else + rv = ucv_int64_new(n1 - n2); + } + else if (n1 >= 0 && n2 >= 0) { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + if (u2 > u1) + rv = ucv_int64_new(-(u2 - u1)); + else + rv = ucv_uint64_new(u1 - u2); + } + else if (n1 >= 0) { + if (!u1) u1 = (uint64_t)n1; + + rv = ucv_uint64_new(u1 + abs64(n2)); + } + else { + rv = ucv_int64_new(n1 - n2); + } + + break; + + case I_MUL: + if (n1 < 0 && n2 < 0) { + rv = ucv_uint64_new(abs64(n1) * abs64(n2)); + } + else if (n1 >= 0 && n2 >= 0) { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + rv = ucv_uint64_new(u1 * u2); + } + else { + rv = ucv_int64_new(n1 * n2); + } + + break; + + case I_DIV: + if (n2 == 0) { + if (n1 == 0 && !u1) + rv = ucv_double_new(NAN); + else + rv = ucv_double_new((n1 < 0) ? -INFINITY : INFINITY); + } + else if (n1 == INT64_MIN && n2 == -1) { + /* the mathematical result 2^63 only fits into uint64_t; + * the signed division would trap with SIGFPE */ + rv = ucv_uint64_new((uint64_t)INT64_MAX + 1); + } + else if (n1 < 0 || n2 < 0) { + rv = ucv_int64_new(n1 / n2); + } + else { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + rv = ucv_uint64_new(u1 / u2); + } + + break; + + case I_MOD: + if (n2 == 0) { + rv = ucv_double_new(NAN); + } + else if (n1 == INT64_MIN && n2 == -1) { + /* the signed modulo would trap with SIGFPE */ + rv = ucv_int64_new(0); + } + else if (n1 < 0 || n2 < 0) { + rv = ucv_int64_new(n1 % n2); + } + else { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + rv = ucv_uint64_new(u1 % u2); + } + + break; + + case I_EXP: + if (n1 < 0 || n2 < 0) { + /* the result is negative iff the base is negative and the + * exponent is odd */ + if (n2 < 0) + rv = ucv_double_new(((n1 < 0 && (n2 & 1)) ? -1.0 : 1.0) / + (double)upow64(abs64(n1), abs64(n2))); + else if (n2 & 1) + rv = ucv_int64_new((int64_t)-upow64(abs64(n1), abs64(n2))); + else + rv = ucv_uint64_new(upow64(abs64(n1), abs64(n2))); + } + else { + if (!u1) u1 = (uint64_t)n1; + if (!u2) u2 = (uint64_t)n2; + + rv = ucv_uint64_new(upow64(u1, u2)); + } + + break; + + default: + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "undefined arithmetic operation %d", + operation); + break; + } + } + + ucv_put(nv1); + ucv_put(nv2); + + return rv; +} + +/* Test whether `key` names a property of `scope` or of anything in its + * prototype chain, the way a property read resolves it - but without + * dispatching __get__, so a purely virtual property stays hidden. */ +static bool +uc_vm_key_exists(uc_value_t *scope, uc_value_t *key) +{ + uc_value_t *o; + bool found = false; + + if (ucv_type(key) != UC_STRING) + return false; + + for (o = scope; o != NULL && !found; o = ucv_prototype_get(o)) + ucv_object_lookup(o, ucv_string_get(key), ucv_string_length(key), &found); + + return found; +} + +uc_value_t * +uc_vm_value_unary(uc_vm_insn_t operation, uc_value_t *value) +{ + bool is_sub = (operation == I_MINUS); + uc_value_t *nv = NULL, *rv; + int64_t n; + double d; + + if (ucv_type(value) == UC_STRING) { + nv = uc_number_parse(ucv_string_get(value), NULL); + + if (nv) + value = nv; + } + + switch (ucv_type(value)) { + case UC_INTEGER: + n = ucv_int64_get(value); + + /* numeric value is in range 9223372036854775808..18446744073709551615 */ + if (errno == ERANGE) { + if (is_sub) + /* make negation of large numeric value result in smallest negative value */ + rv = ucv_int64_new(INT64_MIN); + else + /* for positive number coercion return value as-is */ + rv = ucv_get(value); + } + + /* numeric value is in range -9223372036854775808..9223372036854775807 */ + else { + if (is_sub) { + if (n == INT64_MIN) + /* make negation of minimum value result in maximum signed positive value */ + rv = ucv_int64_new(INT64_MAX); + else + /* for all other values flip the sign */ + rv = ucv_int64_new(-n); + } + else { + /* for positive number coercion return value as-is */ + rv = ucv_get(value); + } + } + + break; + + case UC_DOUBLE: + d = ucv_double_get(value); + rv = ucv_double_new(is_sub ? -d : d); + break; + + case UC_BOOLEAN: + n = (int64_t)ucv_boolean_get(value); + rv = ucv_int64_new(is_sub ? -n : n); + break; + + case UC_NULL: + rv = ucv_int64_new(0); + break; + + default: + rv = ucv_double_new(NAN); + } + + ucv_put(nv); + + return rv; +} + +uc_value_t * +uc_vm_value_complement(uc_value_t *value) +{ + uc_value_t *nv, *rv; + uint64_t u; + int64_t n; + + nv = ucv_to_number(value); + n = int64(nv, &u); + + if (n < 0) { + rv = ucv_int64_new(~n); + } + else { + if (!u) u = (uint64_t)n; + + rv = ucv_uint64_new(~u); + } + + ucv_put(nv); + + return rv; +} + +bool +uc_vm_value_compare(uc_vm_insn_t operation, uc_value_t *v1, uc_value_t *v2) +{ + return ucv_compare(operation, v1, v2, NULL); +} + +bool +uc_vm_value_equality(uc_vm_insn_t operation, uc_value_t *v1, uc_value_t *v2) +{ + bool equal = uc_vm_test_strict_equality(v1, v2, false); + + return (operation == I_EQS) ? equal : !equal; +} + +bool +uc_vm_value_in(uc_value_t *r1, uc_value_t *r2) +{ + uc_value_t *item; + size_t arrlen, arridx; + bool found = false; + + switch (ucv_type(r2)) { + case UC_ARRAY: + /* value membership: is r1 one of the array elements? */ + for (arridx = 0, arrlen = ucv_array_length(r2); + arridx < arrlen; arridx++) { + item = ucv_array_get(r2, arridx); + + if (uc_vm_test_strict_equality(r1, item, true)) { + found = true; + break; + } + } + + /* arrays can carry a prototype providing the tested key as well */ + if (!found) + found = uc_vm_key_exists(r2, r1); + + break; + + case UC_OBJECT: + found = uc_vm_key_exists(r2, r1); + + break; + + default: + found = false; + } + + return found; +} + +bool +uc_vm_value_not(uc_value_t *value) +{ + return !ucv_is_truish(value); +} + +static void +uc_vm_object_iterator_free(void *ud) +{ + uc_object_iterator_t *iter = ud; + + uc_list_remove(&iter->list); +} + +static uc_resource_type_t uc_vm_object_iterator_type = { + .name = "object iterator", + .free = uc_vm_object_iterator_free +}; + +static bool +uc_vm_object_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, uc_value_t *v, + uc_value_t **state, uc_value_t **key, uc_value_t **val) +{ + uc_value_t *k = *state; + uc_resource_t *res = ucv_as_resource(k); + uc_object_t *obj = ucv_as_object(v); + uc_object_iterator_t *iter; + uc_object_entry_t *e; + + if (!res) { + /* object is empty */ + if (!obj->count) + return false; + + res = xalloc(sizeof(*res) + sizeof(uc_object_iterator_t)); + res->header.type = UC_RESOURCE; + res->header.refcount = 1; + res->type = &uc_vm_object_iterator_type; + + iter = res->data = (char *)res + sizeof(*res); + iter->object = obj; + iter->pos = 0; + + uc_list_insert(&uc_thread_context_get()->object_iterators, &iter->list); + } + else if (ucv_type(k) == UC_RESOURCE && + res->type == &uc_vm_object_iterator_type && res->data != NULL) { + + iter = res->data; + } + else { + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid object iterator"); + + return false; + } + + while (iter->pos < obj->used && !obj->entries[iter->pos].key) + iter->pos++; + + /* no next key */ + if (iter->pos >= obj->used) { + uc_list_remove(&iter->list); + + return false; + } + + e = &obj->entries[iter->pos++]; + *key = ucv_get(e->key); + + if (insn == I_NEXTKV) + *val = ucv_get(e->value); + + *state = &res->header; + + return true; +} + +static bool +uc_vm_array_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, uc_value_t *v, + uc_value_t **state, uc_value_t **key, uc_value_t **val) +{ + uc_value_t *k = *state; + uint64_t n; + + if (!k) { + /* array is empty */ + if (!ucv_array_length(v)) + return false; + + k = ucv_resource_new(NULL, NULL); + n = 0; + } + else if (ucv_type(k) == UC_RESOURCE) { + n = (uintptr_t)ucv_resource_data(k, NULL); + } + else { + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid array iterator"); + + return false; + } + + /* no next index */ + if (n >= ucv_array_length(v)) + return false; + + if (insn == I_NEXTKV) { + *key = ucv_uint64_new(n); + *val = ucv_get(ucv_array_get(v, n)); + } + else { + *key = ucv_get(ucv_array_get(v, n)); + } + + *state = k; + + ucv_as_resource(k)->data = (void *)(uintptr_t)(n + 1); + + return true; +} + +bool +uc_vm_iterator_next(uc_vm_t *vm, uc_vm_insn_t insn, uc_value_t *v, + uc_value_t **state, uc_value_t **key, uc_value_t **val) +{ + switch (ucv_type(v)) { + case UC_OBJECT: + return uc_vm_object_iterator_next(vm, insn, v, state, key, val); + + case UC_ARRAY: + return uc_vm_array_iterator_next(vm, insn, v, state, key, val); + + default: + return false; + } +} + +static bool +assert_mutable_value(uc_vm_t *vm, uc_value_t *val) +{ + if (ucv_is_constant(val)) { + uc_vm_raise_exception(vm, EXCEPTION_TYPE, + "%s value is immutable", + ucv_typename(val)); + + return false; + } + + return true; +} + +bool +uc_vm_property_get(uc_vm_t *vm, uc_value_t *v, uc_value_t *k, uc_value_t **res) +{ + switch (ucv_type(v)) { + case UC_RESOURCE: + case UC_OBJECT: + case UC_ARRAY: + /* aware read: dispatches __get__ on a missing key */ + *res = ucv_key_get(vm, v, k); + + return true; + + default: + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "left-hand side expression is %s", + v ? "not an array or object" : "null"); + + return false; + } +} + +bool +uc_vm_property_set(uc_vm_t *vm, uc_value_t *o, uc_value_t *k, uc_value_t *v, uc_value_t **res) +{ + switch (ucv_type(o)) { + case UC_OBJECT: + case UC_ARRAY: + case UC_RESOURCE: + *res = NULL; + + if (!assert_mutable_value(vm, o)) + return true; + + /* ucv_key_set() retains the stored value on its own and returns a + * reference to it, which becomes the value of the assignment; for + * resources it dispatches a __set__ metamethod when present. In + * strict mode it raises a type error when a non-index property + * store on an array would otherwise be silently dropped. */ + *res = ucv_key_set(vm, o, k, v); + + return true; + + default: + uc_vm_raise_exception(vm, EXCEPTION_TYPE, + "attempt to set property on %s value", + ucv_typename(o)); + + return false; + } +} + +bool +uc_vm_property_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *v, + uc_value_t *k, uc_value_t *inc, uc_value_t **res) +{ + uc_value_t *val, *nv; + + switch (ucv_type(v)) { + case UC_OBJECT: + case UC_ARRAY: + case UC_RESOURCE: + *res = NULL; + + if (!assert_mutable_value(vm, v)) + return true; + + /* reading the property may dispatch a __get__ metamethod which runs + * on the current stack and may collect garbage, so keep the operands + * reachable */ + uc_vm_stack_push(vm, ucv_get(inc)); + + val = ucv_key_get(vm, v, k); + uc_vm_stack_push(vm, ucv_get(val)); + + nv = uc_vm_value_arith(vm, operation, val, inc); + + ucv_put(uc_vm_stack_pop(vm)); + ucv_put(uc_vm_stack_pop(vm)); + + /* an aborted read leaves nothing to store; the exception it raised is + * left to propagate instead of overwriting the property */ + *res = vm->exception.type != EXCEPTION_NONE ? NULL : ucv_key_set(vm, v, k, nv); + + /* the store keeps a reference of its own; the result is the value of + * the update expression, null if storing failed */ + ucv_put(nv); + ucv_put(val); + + return true; + + default: + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "left-hand side expression is %s", + v ? "not an array or object" : "null"); + + return false; + } +} + +bool +uc_vm_property_delete(uc_vm_t *vm, uc_value_t *v, uc_value_t *k, uc_value_t **res) +{ + switch (ucv_type(v)) { + case UC_OBJECT: + case UC_ARRAY: + case UC_RESOURCE: + *res = NULL; + + if (!assert_mutable_value(vm, v)) + return true; + + /* ucv_key_delete() removes own object keys directly and consults the + * __delete__ metamethod for keys the value has no own storage for. + * Arrays and resources have no own-key storage at all, so there the + * metamethod is the only way to handle a key; without one it raises + * the same reference error the default case below reports */ + *res = ucv_boolean_new(ucv_key_delete(vm, v, k)); + + return true; + + default: + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "left-hand side expression is %s", + v ? "not an object" : "null"); + + return false; + } +} + +void +uc_vm_array_append(uc_value_t *arr, uc_value_t **values, size_t count) +{ + size_t idx; + + for (idx = 0; idx < count; idx++) + ucv_array_push(arr, values[idx]); +} + +bool +uc_vm_array_spread(uc_vm_t *vm, uc_value_t *dst, uc_value_t *src) +{ + size_t i; + char *s; + + if (ucv_type(src) != UC_ARRAY) { + s = ucv_to_string(vm, src); + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); + ucv_put(src); + free(s); + + return false; + } + + for (i = 0; i < ucv_array_length(src); i++) + ucv_array_push(dst, ucv_get(ucv_array_get(src, i))); + + ucv_put(src); + + return true; +} + +uc_value_t * +uc_vm_object_new(uc_vm_t *vm, size_t nkeys) +{ + return ucv_object_new_size(vm, nkeys); +} + +void +uc_vm_object_init_key(uc_vm_t *vm, uc_value_t *obj, uc_value_t *k, uc_value_t *v) +{ + /* object literals create own keys, a __set__ metamethod of the object under + * construction must not intercept the keys it is built from */ + ucv_put(ucv_key_rawset(vm, obj, k, v)); +} + +bool +uc_vm_object_spread(uc_vm_t *vm, uc_value_t *dst, uc_value_t *src) +{ + size_t i; + char *s; + + switch (ucv_type(src)) { + case UC_OBJECT: + ; /* a label can only be part of a statement and a declaration is not a statement */ + ucv_object_foreach_entry(src, e) + ucv_object_insert_string(dst, e->key, ucv_get(e->value)); + + ucv_put(src); + + return true; + + case UC_ARRAY: + for (i = 0; i < ucv_array_length(src); i++) { + xasprintf(&s, "%zu", i); + ucv_object_add(dst, s, ucv_get(ucv_array_get(src, i))); + free(s); + } + + ucv_put(src); + + return true; + + default: + s = ucv_to_string(vm, src); + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Value (%s) is not iterable", s); + free(s); + + return false; + } +} + +/* The `nargs` arguments of a call stay at `base` of the stack while their + * expansion is pushed above them, so that the GC sees them when a tostring + * method runs. `from` and `spread` count arguments from the last one, as the + * bytecode encodes the spread positions. */ +static uc_value_t * +uc_vm_spread_arg(uc_vm_t *vm, size_t base, size_t nargs, size_t idx) +{ + return vm->stack.entries[base + nargs - 1 - idx]; +} + +bool +uc_vm_spread_push(uc_vm_t *vm, size_t base, size_t nargs, size_t from, size_t spread) +{ + uc_value_t *arg; + size_t j; + char *s; + + /* push each preceeding non-spread value to the stack */ + for (j = from; j > spread + 1UL; j--) + uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, j - 1))); + + /* read spread value at index... */ + arg = uc_vm_spread_arg(vm, base, nargs, spread); + + /* ... ensure that it is an array type ... */ + if (ucv_type(arg) != UC_ARRAY) { + s = ucv_to_string(vm, arg); + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); + free(s); + + return false; + } + + /* ... and push each spread array value as argument to the stack */ + for (j = 0; j < ucv_array_length(arg); j++) + uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j))); + + return true; +} + +/* Pushes the arguments after the last spread one, then replaces the original + * arguments with the expanded ones */ +void +uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from) +{ + size_t i, count; + + for (i = from; i > 0; i--) + uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, i - 1))); + + for (i = 0; i < nargs; i++) + ucv_put(vm->stack.entries[base + i]); + + count = vm->stack.count - base - nargs; + memmove(&vm->stack.entries[base], &vm->stack.entries[base + nargs], + count * sizeof(vm->stack.entries[0])); + vm->stack.count = base + count; +} + +uc_value_t * +uc_vm_global_get(uc_vm_t *vm, uc_value_t *name, bool strict) +{ + uc_value_t *scope, *next, *val = NULL; + bool found; + + scope = vm->globals; + + while (ucv_type(name) == UC_STRING) { + val = ucv_object_get(scope, ucv_string_get(name), &found); + + if (found) + break; + + next = ucv_prototype_get(scope); + + if (!next) { + if (strict) { + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "access to undeclared variable %s", + ucv_string_get(name)); + } + + break; + } + + scope = next; + } + + return ucv_get(val); +} + +bool +uc_vm_global_set(uc_vm_t *vm, uc_value_t *name, uc_value_t *v, bool strict) +{ + uc_value_t *scope, *next; + bool found; + + scope = vm->globals; + + while (ucv_type(name) == UC_STRING) { + ucv_object_get(scope, ucv_string_get(name), &found); + + if (found) + break; + + next = ucv_prototype_get(scope); + + if (!next) { + if (strict) { + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "access to undeclared variable %s", + ucv_string_get(name)); + + return false; + } + + break; + } + + scope = next; + } + + if (scope && ucv_type(name) == UC_STRING) + ucv_object_add(scope, ucv_string_get(name), ucv_get(v)); + + return true; +} + +bool +uc_vm_global_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *name, + uc_value_t *inc, bool strict, uc_value_t **res) +{ + uc_value_t *scope, *next, *val = NULL; + bool found; + + scope = vm->globals; + + assert(ucv_type(name) == UC_STRING); + + while (true) { + val = ucv_object_get(scope, ucv_string_get(name), &found); + + if (found) + break; + + next = ucv_prototype_get(scope); + + if (!next) { + if (strict) { + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "access to undeclared variable %s", + ucv_string_get(name)); + + return false; + } + + break; + } + + scope = next; + } + + *res = uc_vm_value_arith(vm, operation, val, inc); + + ucv_object_add(scope, ucv_string_get(name), ucv_get(*res)); + + return true; +} + +const char * +uc_vm_main_key(uc_program_t *program, uc_vm_main_key_t key) +{ + snprintf(key, sizeof(uc_vm_main_key_t), UC_PROGRAM_MAIN_KEY "%p", (void *)program); + + return key; +} + +/* The caller of uc_vm_execute() holds the program only. Keep the closure that + * runs in the registry once it has module instances, so that a later execution + * in this VM runs the same closure and finds them. */ +static void +uc_vm_main_keep(uc_vm_t *vm, uc_program_t *program, uc_program_run_t *run) +{ + uc_vm_main_key_t key; + + if (!run->keep) + return; + + run->keep = false; + uc_vm_registry_set(vm, uc_vm_main_key(program, key), ucv_get(&run->closure->header)); +} + +static uc_modexports_t * +uc_vm_run_exports(uc_program_t *program, uc_program_run_t *run, bool create) +{ + uc_export_instance_t *instance; + + if (!run) + return NULL; + + instance = uc_program_instance_get(program, run->closure, create); + + return instance ? &instance->exports : NULL; +} + +uc_upvalref_t * +uc_vm_capture_upval(uc_vm_t *vm, size_t slot) +{ + uc_upvalref_t *curr = vm->open_upvals; + uc_upvalref_t *prev = NULL; + uc_upvalref_t *created; + char *s; + + while (curr && curr->slot > slot) { + prev = curr; + curr = curr->next; + } + + if (curr && curr->slot == slot) { + if (vm->trace) { + s = ucv_to_string(NULL, vm->stack.entries[slot]); + fprintf(stderr, " {+%zu} <%p> %s\n", slot, (void *)curr, s); + free(s); + } + + return curr; + } + + created = (uc_upvalref_t *)ucv_upvalref_new(slot); + created->next = curr; + + if (vm->trace) { + s = ucv_to_string(NULL, vm->stack.entries[slot]); + fprintf(stderr, " {*%zu} <%p> %s\n", slot, (void *)created, s); + free(s); + } + + if (prev) + prev->next = created; + else + vm->open_upvals = created; + + return created; +} + +/* a closure that runs again holds the binding of its previous run */ +static void +uc_vm_import_bind(uc_closure_t *closure, size_t slot, uc_upvalref_t *ref) +{ + if (closure->upvals[slot]) + ucv_put(&closure->upvals[slot]->header); + + closure->upvals[slot] = ref; +} + +bool +uc_vm_regexp_load(uc_vm_t *vm, uc_value_t *jstr, uc_value_t **re) +{ + bool icase = false, newline = false, global = false; + char *str, *err = NULL; + + *re = NULL; + + if (ucv_type(jstr) != UC_STRING || ucv_string_length(jstr) < 2) + return true; + + str = ucv_string_get(jstr); + + global = (*str & (1 << 0)); + icase = (*str & (1 << 1)); + newline = (*str & (1 << 2)); + + *re = ucv_regexp_new(++str, icase, newline, global, &err); + + if (!*re) + uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, "%s", err); + + free(err); + + return (*re != NULL); +} + +void +uc_vm_closure_upval_set(uc_vm_t *vm, uc_closure_t *closure, size_t idx, int32_t uv) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + + if (uv < 0) + closure->upvals[idx] = uc_vm_capture_upval(vm, frame->stackframe - (uv + 1)); + else + closure->upvals[idx] = frame->closure->upvals[uv]; + + ucv_get(&closure->upvals[idx]->header); +} + +/* Invoke a standard library function without pushing a call frame. + * + * The arguments occupy the top `nargs` stack entries, which is exactly the + * layout uc_fn_arg() expects, so the callee can be entered as an ordinary C + * function; it restores the stack to the argument boundary itself when it + * calls back into managed code. + * + * The callee is addressed by its slot in the order-frozen + * uc_stdlib_functions[] table rather than by name, so a shadowing `slice`, + * `type` or `die` in the runtime namespace cannot affect the call. + * + * Since no frame is involved, an exception raised by the callee reports the + * position of this instruction in the calling chunk, not of a synthetic one. */ +void +uc_vm_stcall(uc_vm_t *vm, size_t slot, size_t nargs) +{ + uc_cfn_ptr_t fptr = uc_stdlib_function_by_slot(slot); + size_t keep = vm->stack.count - nargs; + uc_value_t *res = NULL; + + if (!fptr) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "unknown standard library slot %zu", slot); + } + else { + res = fptr(vm, nargs); + } + + /* a callee reads the arguments with uc_fn_arg() but does not consume them - + * ordinary calls consume them through the callframe pop - so drop them here, + * along with anything the callee left above them */ + while (vm->stack.count > keep) + ucv_put(uc_vm_stack_pop(vm)); + + /* as in uc_vm_call_native(), a result is only kept when no exception fired */ + if (vm->exception.type) + ucv_put(res); + else + uc_vm_stack_push(vm, res); +} + +void +uc_vm_value_print(uc_vm_t *vm, uc_value_t *v) +{ + char *p; + + switch (ucv_type(v)) { + case UC_OBJECT: + case UC_ARRAY: + p = ucv_to_jsonstring(vm, v); + fwrite(p, 1, strlen(p), vm->output); + free(p); + break; + + case UC_STRING: + fwrite(ucv_string_get(v), 1, ucv_string_length(v), vm->output); + break; + + case UC_NULL: + break; + + default: + p = ucv_to_string(vm, v); + fwrite(p, 1, strlen(p), vm->output); + free(p); + } +} + +void +uc_vm_import(uc_vm_t *vm, size_t from, size_t to) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_program_t *prog = uc_vm_current_program(vm); + uc_modexports_t *exports = uc_vm_run_exports(prog, uc_vector_last(&prog->runs), false); + size_t count = exports ? exports->count : 0; + + /* module export available, patch into upvalue */ + if (from < count && exports->entries[from]) { + ucv_get(&exports->entries[from]->header); + uc_vm_import_bind(frame->closure, to, exports->entries[from]); + + return; + } + + /* module export missing, e.g. due to premature return in module, + * patch up dummy upvalue ref with `null` value */ + uc_vm_import_bind(frame->closure, to, (uc_upvalref_t *)ucv_upvalref_new(0)); + frame->closure->upvals[to]->closed = true; +} + +uc_value_t * +uc_vm_import_namespace(uc_vm_t *vm, size_t from, const uint32_t *names, size_t nnames) +{ + uc_program_t *prog = uc_vm_current_program(vm); + uc_modexports_t *exports = uc_vm_run_exports(prog, uc_vector_last(&prog->runs), false); + size_t count = exports ? exports->count : 0; + uc_value_t *name, *modobj; + size_t i; + + modobj = ucv_object_new(vm); + + for (i = 0; i < nnames; i++, from++) { + if (from >= count || !exports->entries[from]) + continue; + + name = uc_program_get_constant(prog, names[i]); + + if (ucv_type(name) == UC_STRING) + ucv_object_add(modobj, ucv_string_get(name), + ucv_get(&exports->entries[from]->header)); + + ucv_put(name); + } + + ucv_set_constant(modobj, true); + + return modobj; +} + +uc_modexports_t * +uc_vm_export_target(uc_vm_t *vm) +{ + uc_program_t *prog = uc_vm_current_program(vm); + uc_program_run_t *run = uc_vector_last(&prog->runs); + uc_modexports_t *exports = uc_vm_run_exports(prog, run, true); + + if (!exports) + return NULL; + + uc_vm_main_keep(vm, prog, run); + + /* A later run of the closure runs the module bodies again. Its imports + * resolve to the instances of the first run, which stay in place. */ + if (run->cursor++ < exports->count) + return NULL; + + return exports; +} + +void +uc_vm_export_add(uc_modexports_t *exports, uc_upvalref_t *ref) +{ + uc_vector_push(exports, ref); + ucv_get(&ref->header); +} + +uc_value_t * +uc_vm_dynload_module(uc_vm_t *vm, uc_value_t *name) +{ + /* Attempt to load module. Will raise exception on error */ + uc_value_t *modscope = uc_require_library(vm, name, true); + + if (!modscope || vm->exception.type != EXCEPTION_NONE) { + ucv_put(modscope); + + return NULL; + } + + return modscope; +} + +uc_value_t * +uc_vm_dynload_namespace(uc_vm_t *vm, uc_value_t *modscope) +{ + uc_value_t *modobj = ucv_object_new(vm); + + ucv_object_foreach_entry(modscope, e) + ucv_object_insert_string(modobj, e->key, ucv_get(e->value)); + + ucv_set_constant(modobj, true); + + return modobj; +} + +bool +uc_vm_dynload_bind(uc_vm_t *vm, uc_value_t *modscope, uint32_t cidx, size_t to) +{ + uc_callframe_t *frame = uc_vm_current_frame(vm); + uc_value_t *name, *export; + bool found; + + name = uc_program_get_constant(uc_vm_current_program(vm), cidx); + export = ucv_object_get(modscope, ucv_string_get(name), &found); + + if (!found) { + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "Module does not export %s", + ucv_string_get(name)); + + ucv_put(name); + + return false; + } + + ucv_put(name); + + uc_vm_import_bind(frame->closure, to, (uc_upvalref_t *)ucv_upvalref_new(0)); + frame->closure->upvals[to]->closed = true; + frame->closure->upvals[to]->value = ucv_get(export); + + return true; +} + +static const uc_vm_interpreter_t *uc_vm_interpreter; +static uc_vm_native_call_t uc_vm_native_call; + +void +uc_vm_interpreter_set(const uc_vm_interpreter_t *interpreter) +{ + uc_vm_interpreter = interpreter; +} + +void +uc_vm_native_call_set(uc_vm_native_call_t call) +{ + uc_vm_native_call = call; +} + +bool +uc_vm_call_depth_check(uc_vm_t *vm, bool tail) +{ + /* a tail invocation reuses the current frame, so it never grows the stack */ + if (tail || vm->callframes.count < 1000) + return true; + + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Too much recursion"); + + return false; +} + +bool +uc_vm_call_dispatch(uc_vm_t *vm, uc_value_t **ctx, uc_value_t **fno, bool mcall, size_t nargs) +{ + char *callee, *hint; + size_t hlen; + + /* __call__ dispatch: if fno is not itself callable (not a closure or + * cfunction) but carries a __call__ metamethod somewhere in its prototype + * chain, invoke that method with the instance as `this`. The instance + * becomes the new ctx; the original ctx (if any) is discarded. The tail + * flag is preserved so `return foo()` still TCOs through the dispatch. */ + if (ucv_type(*fno) != UC_CFUNCTION && ucv_type(*fno) != UC_CLOSURE) { + uc_value_t *meta = ucv_metamethod_lookup(*fno, "__call__"); + + if (ucv_is_callable(meta)) { + ucv_put(*ctx); + + /* the instance becomes `this` and is invoked in place of the + * __call__ method, so hand the reference to ctx over */ + *ctx = *fno; + *fno = ucv_get(meta); + } + } + + /* is a native function; such functions are always invoked in a fresh frame, + * even when requested as tail call, since the foreign code may rely on the + * stack layout of an ordinary invocation */ + if (ucv_type(*fno) == UC_CFUNCTION) { + uc_vm_call_native(vm, *ctx, ucv_as_cfunction(*fno), mcall, nargs); + *fno = NULL; + + return true; + } + + if (ucv_type(*fno) == UC_CLOSURE) + return true; + + /* name the invoked expression where it can be recovered from the source, + * and render the value it held as JSON, so that the message identifies + * both the offending expression and the value that was not a function; + * a NULL vm skips __tostring__ dispatch, so describing the value has no + * side effects */ + callee = uc_vm_callee_expr(vm); + hint = ucv_to_jsonstring(NULL, *fno); + hlen = strlen(hint); + + if (hlen > UC_VALUE_HINT_MAXLEN) + memcpy(hint + UC_VALUE_HINT_MAXLEN - 3, "...", 4); + + if (callee) + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "`%s` is not a function, got %s instead", callee, hint); + else + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "left-hand side is not a function, got %s instead", hint); + + free(callee); + free(hint); + ucv_put(*ctx); + ucv_put(*fno); + + return false; +} + +void +uc_vm_call_args_adjust(uc_vm_t *vm, uc_function_t *function, size_t nargs) +{ + uc_value_t *ellip; + size_t i; + + /* fewer arguments on stack than function expects => pad */ + if (nargs < function->nargs) { + for (i = nargs; i < function->nargs; i++) { + if (function->vararg && (i + 1) == function->nargs) + uc_vm_stack_push(vm, ucv_array_new_length(vm, 0)); + else + uc_vm_stack_push(vm, NULL); + } + } + + /* more arguments on stack than function expects... */ + else if (nargs > function->nargs - function->vararg) { + /* is a vararg function => pass excess args as array */ + if (function->vararg) { + ellip = ucv_array_new_length(vm, nargs - (function->nargs - 1)); + + for (i = function->nargs; i <= nargs; i++) + ucv_array_push(ellip, uc_vm_stack_peek(vm, nargs - i)); + + for (i = function->nargs; i <= nargs; i++) + uc_vm_stack_pop(vm); + + uc_vm_stack_push(vm, ellip); + } + + /* static amount of args => drop excess values */ + else { + for (i = function->nargs; i < nargs; i++) + ucv_put(uc_vm_stack_pop(vm)); + } + } +} + +static void +uc_vm_closure_call(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, size_t nargs, size_t stackoff) +{ + if (closure->function->native && uc_vm_native_call) { + uc_vm_native_call(vm, closure, ctx, mcall, nargs); + + return; + } + + if (!uc_vm_interpreter) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "bytecode closures cannot execute in the native runtime"); + ucv_put(ctx); + ucv_put(&closure->header); + + return; + } + + uc_vm_call_args_adjust(vm, closure->function, nargs); + uc_vm_interpreter->frame_push(vm, closure, ctx, mcall, stackoff); + uc_vm_interpreter->execute(vm); +} + +uc_exception_type_t +uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) +{ + uc_value_t *ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL; + uc_value_t *fno = ucv_get(uc_vm_stack_peek(vm, nargs)); + size_t argc = nargs & 0xffff; + size_t stackoff; + + uc_vm_clear_exception(vm); + + if (!uc_vm_call_depth_check(vm, false)) { + ucv_put(ctx); + ucv_put(fno); + + return vm->exception.type; + } + + stackoff = vm->stack.count - argc - 1; + + if (!uc_vm_call_dispatch(vm, &ctx, &fno, mcall, argc)) + return vm->exception.type; + + if (fno) + uc_vm_closure_call(vm, ucv_as_closure(fno), ctx, mcall, argc, stackoff); + + return vm->exception.type; +} From 3c3b41517291aaeed8c816a26d23b093fc0d661c Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 12:45:54 +0100 Subject: [PATCH 08/11] native: compile programs to native code through LLVM IR With the LLVM_SUPPORT build option, `ucode -c,llvm` writes a program and the scripts it loads as LLVM IR. The build also produces the runtime as LLVM bitcode, libucode-native.bc, and the list of its API symbols, libucode-native.api: ucode -c,llvm -o prog.ll prog.uc llvm-link prog.ll libucode-native.bc -o all.bc opt -passes='internalize,default' \ -internalize-public-api-file=libucode-native.api all.bc -o prog.bc llc -relocation-model=pic -filetype=obj prog.bc -o prog.o cc prog.o -lm -ldl -rdynamic -Wl,-rpath,/usr/lib/ucode/native -o prog Every function becomes a native function. A depth analysis of its bytecode gives each value of the operand stack a slot of the native frame, and each instruction becomes a call of a runtime helper, which runs the vmcore.c code of the instruction; frequent sequences of instructions run in one helper call. Branches and exception handlers become native control flow. The output holds no bytecode, only tables of the constants, functions, debug information and sources, which contain no pointers. The runtime pushes a call frame for every native call, so stack traces, the `this` of callbacks and the recursion limit work as in the interpreter, and runs tail calls in place. Native closures call each other without the VM stack. Captured locals move into closed upvalues, and the native frames are GC roots. Constants are created once per program, and string constants keep their hash as object keys, so that a property read with a constant key does not hash it again. A statically or dynamically imported module has one namespace object. Signal handlers run after calls and at backward jumps, where the interpreter runs them after every instruction. Scripts that the program loads by literal names, and those named with the embed= and embed-template= flags, are compiled into it; bundle_files(), a global of native programs only, returns their paths. A program built without the `debug` flag has no source and no positions; its stack traces name function ids and offsets, which `ucode-trace` resolves with the file that the map= flag writes. A library with the SONAME of libucode but without code, installed in lib/ucode/native, lets bindings that are linked against libucode load without the interpreter library. The program finds it through its RUNPATH, and warns if it does not. A program cannot load it by an absolute path, as musl reuses a loaded library only if a search by name found it. The runtime is for Linux. The custom tests run against native programs in the custom-native ctest, under valgrind, and the cram tests cover the command line of native programs. native-ucode.sh builds the programs, also for another target, with the library of the build tree. Signed-off-by: John Crispin --- CMakeLists.txt | 118 ++ cmake/native-api.cmake | 24 + include/ucode/internal/lib.h | 7 +- include/ucode/internal/native.h | 322 ++++ include/ucode/internal/program.h | 1 + include/ucode/internal/types.h | 53 +- lib.c | 39 +- llvm.c | 1800 ++++++++++++++++++++ main.c | 97 +- native.c | 868 ++++++++++ native_abi.c | 22 + native_insn.c | 1354 +++++++++++++++ native_trace.c | 319 ++++ program.c | 39 +- tests/cram/CMakeLists.txt | 22 + tests/cram/native_build.t | 136 ++ tests/cram/test_basic.t | 8 +- tests/custom/04_modules/09_import_wildcard | 50 + tests/custom/CMakeLists.txt | 28 + tests/custom/native-ucode.sh | 112 ++ tests/custom/native-unsupported | 33 + tests/custom/run_tests.uc | 20 +- trace.c | 54 + types.c | 62 +- vmcore.c | 102 +- 25 files changed, 5655 insertions(+), 35 deletions(-) create mode 100644 cmake/native-api.cmake create mode 100644 include/ucode/internal/native.h create mode 100644 llvm.c create mode 100644 native.c create mode 100644 native_abi.c create mode 100644 native_insn.c create mode 100644 native_trace.c create mode 100644 tests/cram/native_build.t create mode 100755 tests/custom/native-ucode.sh create mode 100644 tests/custom/native-unsupported create mode 100644 trace.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ae6e157..8576db3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,11 @@ if(NOT COMPILE_SUPPORT) target_compile_definitions(uc_defines INTERFACE NO_COMPILE) endif() +option(LLVM_SUPPORT "Support native programs: LLVM IR output and runtime (Linux only)" OFF) +if(LLVM_SUPPORT) + target_compile_definitions(uc_defines INTERFACE HAVE_LLVM_SUPPORT) +endif() + if(NOT UCODE_WASM) find_library(libuci NAMES uci) find_library(libubox NAMES ubox) @@ -235,6 +240,9 @@ else() endif() set(UCODE_SOURCES lexer.c lib.c vm.c vmcore.c chunk.c vallist.c compiler.c source.c types.c json.c program.c bytecode.c platform.c cli.c) +if(LLVM_SUPPORT) + list(APPEND UCODE_SOURCES llvm.c native_trace.c) +endif() if(UCODE_WASM) add_library(libucode STATIC ${UCODE_SOURCES}) set_target_properties(libucode PROPERTIES OUTPUT_NAME ucode) @@ -249,6 +257,106 @@ if(NOT UCODE_WASM) set(CLI_SOURCES main.c) add_executable(ucode ${CLI_SOURCES}) target_link_libraries(ucode uc_defines libucode) + + if(LLVM_SUPPORT) + add_executable(ucode-trace trace.c) + target_link_libraries(ucode-trace uc_defines libucode) + endif() +endif() + +if(LLVM_SUPPORT AND NOT UCODE_WASM) + # Bindings that are linked against libucode load this library in its place, + # see native_abi.c. Native programs name its install directory in their + # RUNPATH. musl reuses a loaded library only if a search by name found it, + # so a program cannot load it by an absolute path. + add_library(ucode-native-abi SHARED native_abi.c) + set_target_properties(ucode-native-abi PROPERTIES + OUTPUT_NAME ucode SOVERSION ${SOVERSION} + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/native-abi) + + # The runtime of native programs, as LLVM bitcode: everything of libucode + # except the interpreter and the compiler. The lexer only provides utf8enc() + # there. A native program is linked with it at the IR level, so that LLVM + # inlines the runtime into the generated code. The bitcode must match the + # LLVM version of opt and llc. + find_program(NATIVE_CLANG NAMES clang clang-21 clang-20 clang-19 clang-18 clang-17 clang-16) + find_program(NATIVE_LLVM_LINK NAMES llvm-link llvm-link-21 llvm-link-20 llvm-link-19 + llvm-link-18 llvm-link-17 llvm-link-16) + find_program(NATIVE_LLVM_DIS NAMES llvm-dis llvm-dis-21 llvm-dis-20 llvm-dis-19 + llvm-dis-18 llvm-dis-17 llvm-dis-16) + set(NATIVE_TARGET "" CACHE STRING "Target triple of the runtime bitcode, empty for the host") + + if(NOT NATIVE_CLANG OR NOT NATIVE_LLVM_LINK OR NOT NATIVE_LLVM_DIS) + message(FATAL_ERROR "LLVM_SUPPORT requires clang, llvm-link and llvm-dis") + endif() + + set(native_bc_flags -O2 -std=gnu99 -fPIE -emit-llvm -c + -DUC_NATIVE_ABI_SONAME="libucode.so.${SOVERSION}" + "-D$,$-D>" + "-I$,$-I>") + + if(NATIVE_TARGET) + list(APPEND native_bc_flags --target=${NATIVE_TARGET}) + endif() + + if(CMAKE_SYSROOT) + list(APPEND native_bc_flags --sysroot=${CMAKE_SYSROOT}) + endif() + + set(native_bc_files) + + foreach(src lib.c vmcore.c chunk.c vallist.c source.c types.c json.c program.c platform.c + cli.c native.c native_insn.c lexer.c) + get_filename_component(name ${src} NAME_WE) + set(bc ${CMAKE_BINARY_DIR}/native-bc/${name}.bc) + + if(src STREQUAL "lexer.c") + set(extra -DNO_COMPILE) + else() + set(extra) + endif() + + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.20) + set(deps DEPFILE ${bc}.d) + set(depflags -MD -MF ${bc}.d) + else() + set(deps IMPLICIT_DEPENDS C ${CMAKE_CURRENT_SOURCE_DIR}/${src}) + set(depflags) + endif() + + add_custom_command( + OUTPUT ${bc} + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/native-bc + COMMAND ${NATIVE_CLANG} ${native_bc_flags} ${extra} ${depflags} + ${CMAKE_CURRENT_SOURCE_DIR}/${src} -o ${bc} + DEPENDS ${src} + ${deps} + COMMAND_EXPAND_LISTS + VERBATIM) + + list(APPEND native_bc_files ${bc}) + endforeach() + + add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/libucode-native.bc + COMMAND ${NATIVE_LLVM_LINK} ${native_bc_files} -o ${CMAKE_BINARY_DIR}/libucode-native.bc + DEPENDS ${native_bc_files} + VERBATIM) + + # the symbols to keep when the program is linked with opt's internalize + # pass: -internalize-public-api-file=libucode-native.api + add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/libucode-native.api + COMMAND ${NATIVE_LLVM_DIS} ${CMAKE_BINARY_DIR}/libucode-native.bc + -o ${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll + COMMAND ${CMAKE_COMMAND} -DINPUT=${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll + -DOUTPUT=${CMAKE_BINARY_DIR}/libucode-native.api + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-api.cmake + DEPENDS ${CMAKE_BINARY_DIR}/libucode-native.bc ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-api.cmake + VERBATIM) + + add_custom_target(ucode-native ALL + DEPENDS ${CMAKE_BINARY_DIR}/libucode-native.bc ${CMAKE_BINARY_DIR}/libucode-native.api) endif() # Ensure rpath is used on macOS @@ -622,8 +730,18 @@ if(UCODE_WASM) install(TARGETS libucode ucode-modules ARCHIVE DESTINATION lib) else() install(TARGETS ucode udbg RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + + if(LLVM_SUPPORT) + install(TARGETS ucode-trace RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + endif() + install(TARGETS libucode LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS ${LIBRARIES} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/ucode) + + if(LLVM_SUPPORT) + install(TARGETS ucode-native-abi LIBRARY + DESTINATION ${CMAKE_INSTALL_LIBDIR}/ucode/native NAMELINK_SKIP) + endif() endif() if(NOT UCODE_WASM) diff --git a/cmake/native-api.cmake b/cmake/native-api.cmake new file mode 100644 index 00000000..a0377c4d --- /dev/null +++ b/cmake/native-api.cmake @@ -0,0 +1,24 @@ +# Writes the symbols that a native program must keep exported, one per line: +# its main() and every function or variable of the runtime with default +# visibility, which bindings may call. All other symbols of the runtime can +# become internal when it is linked with a program at the IR level. +# +# Usage: cmake -DINPUT=libucode-native.ll -DOUTPUT=libucode-native.api -P native-api.cmake + +file(READ ${INPUT} ir) + +# matched as a whole, since IR lines do not split into CMake list elements +string(REGEX MATCHALL "\ndefine dso_local [^\n@]*@[A-Za-z0-9_.]+\\(" functions "${ir}") +string(REGEX MATCHALL "\n@[A-Za-z0-9_.]+ = dso_local " variables "${ir}") + +set(symbols "main") + +foreach(match IN LISTS functions variables) + if(match MATCHES "@([A-Za-z0-9_.]+)") + list(APPEND symbols ${CMAKE_MATCH_1}) + endif() +endforeach() + +list(REMOVE_DUPLICATES symbols) +list(JOIN symbols "\n" content) +file(WRITE ${OUTPUT} "${content}\n") diff --git a/include/ucode/internal/lib.h b/include/ucode/internal/lib.h index a18f1846..c60d5167 100644 --- a/include/ucode/internal/lib.h +++ b/include/ucode/internal/lib.h @@ -154,9 +154,12 @@ typedef uc_program_t *(*uc_loader_compile_t)(uc_parse_config_t *, uc_source_t *, __hidden void uc_loader_compile_set(uc_loader_compile_t compile); /* Scripts that a native program contains: the loaders find them by path - * before they look at the file system. */ + * before they look at the file system. unit_closure() returns the closure of + * a script, or sets *errp to the compile error of a script that the program + * contains as failed to compile. */ typedef struct { - uc_value_t *(*unit_closure)(uc_vm_t *vm, const char *path, bool raw_mode, bool module_mode); + uc_value_t *(*unit_closure)(uc_vm_t *vm, const char *path, bool raw_mode, bool module_mode, + char **errp); char *(*unit_path)(const char *path); char *(*source_path)(const char *runpath); } uc_loader_units_t; diff --git a/include/ucode/internal/native.h b/include/ucode/internal/native.h new file mode 100644 index 00000000..ccc641ac --- /dev/null +++ b/include/ucode/internal/native.h @@ -0,0 +1,322 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef UCODE_INTERNAL_NATIVE_H +#define UCODE_INTERNAL_NATIVE_H + +#include +#include + +#include "ucode/compiler.h" +#include "ucode/vm.h" +#include "ucode/internal/types.h" +#include "ucode/internal/util.h" + +/* The tables below are emitted by the LLVM backend as IR constants and read + * by the runtime, so their layout is part of the ABI between both. They hold + * only pointers and fixed-width integers, in an order without padding + * differences between targets. Strings are offsets into the string block of + * the program, and the other arrays of a function or source are indexes into + * the arrays of its unit, so that only the unit and program tables hold + * pointers that the dynamic linker relocates at startup. */ + +#define UC_NATIVE_ABI_VERSION 1 + +#define UC_NATIVE_NONE UINT32_MAX + +#define UC_NATIVE_CONST_STRING 0 +#define UC_NATIVE_CONST_INT 1 +#define UC_NATIVE_CONST_DOUBLE 2 +#define UC_NATIVE_CONST_NULL 3 + +#define UC_NATIVE_FN_ARROW (1u << 0) +#define UC_NATIVE_FN_VARARG (1u << 1) +#define UC_NATIVE_FN_STRICT (1u << 2) +#define UC_NATIVE_FN_MODULE (1u << 3) + +#define UC_NATIVE_VAR_UPVAL (1u << 31) + +#define UC_NATIVE_UNIT_TEMPLATE (1u << 0) +#define UC_NATIVE_UNIT_MODULE (1u << 1) + +typedef struct uc_native_frame uc_native_frame_t; + +typedef void (*uc_native_entry_t)(uc_vm_t *, uc_native_frame_t *); + +/* `length` is the length of a string, and 1 for a negative integer */ +typedef struct { + uint64_t bits; + uint32_t str; + uint32_t type; + uint32_t length; + uint32_t reserved; +} uc_native_constant_t; + +typedef struct { + uint32_t name; + /* debuginfo.offsets as pairs of byte and instruction counts, in the + * bytes of the unit */ + uint32_t offsets; + /* debuginfo.variables as from, to, slot and name index, and + * debuginfo.varnames as strings, in the words of the unit */ + uint32_t variables; + uint32_t varnames; + uint32_t noffsets; + uint32_t nvariables; + uint32_t nvarnames; + uint32_t nargs; + uint32_t nupvals; + uint32_t flags; + uint32_t srcidx; + uint32_t srcpos; +} uc_native_function_t; + +typedef struct { + uint32_t filename; + uint32_t runpath; + uint32_t text; + /* in the bytes of the unit */ + uint32_t lineinfo; + /* the entries of uc_source_t.exports, in the exports of the unit */ + uint32_t exports; + uint32_t textlen; + uint32_t nlineinfo; + uint32_t nexports; + uint32_t flags; +} uc_native_source_t; + +typedef struct { + const uc_native_source_t *sources; + const uc_native_constant_t *constants; + const uc_native_function_t *functions; + /* the native function of each entry of `functions` */ + const uc_native_entry_t *entries; + const uc_native_constant_t *exports; + const uint32_t *words; + const uint8_t *bytes; + /* normalised path that loaders find the unit by; none for the entry */ + uint32_t path; + /* the compile error of a script that failed to compile */ + uint32_t error; + uint32_t nsources; + uint32_t nconstants; + uint32_t nfunctions; + uint32_t flags; +} uc_native_unit_t; + +typedef struct { + const char *strings; + const uc_native_unit_t *units; + uint32_t script_name; + uint32_t version; + uint32_t nunits; + uint32_t flags; +} uc_native_program_t; + +typedef struct { + const char *path; + const char *logical; + bool template; +} uc_llvm_embed_t; + +uc_declare_vector(uc_llvm_embeds_t, uc_llvm_embed_t); + +typedef struct { + const char *triple; + const char *script_name; + uc_parse_config_t *config; + uc_llvm_embeds_t *embeds; + bool debug; + const char *map; +} uc_llvm_options_t; + +bool uc_program_write_llvm(uc_program_t *program, FILE *out, + const uc_llvm_options_t *opts, char **errp); + +#define UC_NATIVE_MAP_MAGIC "ucode-map 1\n" + +/* Copies `in` to `out`, with the stack traces of a native program built + * without debug information resolved through the map file `map`. */ +int uc_native_trace_decode(const char *map, FILE *in, FILE *out); + +/* Runtime state; the generated code passes these structures on by pointer + * and never reads them. */ + +/* Allocated with malloc() and released with the function. The functions of + * the program by id - 1 are part of the allocation for the entry function, + * and the other functions of the program point to them. */ +typedef struct uc_native_function_info { + uc_native_entry_t entry; + uc_function_t **functions; +} uc_native_function_info_t; + +struct uc_native_frame { + uc_native_frame_t *prev; + uc_vm_t *vm; + uc_value_t **slots; + uc_upvalref_t **cells; + size_t nslots; + uc_closure_t *closure; + /* the closure of the caller, which only the caller references once a + * tail call replaced it in `closure` and in the call frame */ + uc_closure_t *kept; + uc_value_t *ctx; + uc_value_t **args; + size_t nargs; + bool mcall; + /* whether a slot has a cell */ + bool captured; + uc_value_t *result; + struct { + uc_value_t *fn; + uc_value_t *ctx; + uc_value_t **args; + size_t nargs; + bool mcall; + bool pending; + } tail; +}; + +__hidden uc_value_t **uc_native_slot(uc_native_frame_t *frame, size_t slot); +__hidden void uc_native_slot_set(uc_native_frame_t *frame, size_t slot, uc_value_t *value); +__hidden uc_value_t *uc_native_slot_take(uc_native_frame_t *frame, size_t slot); +__hidden void uc_native_slot_clear(uc_native_frame_t *frame, size_t slot); +__hidden uc_upvalref_t *uc_native_slot_capture(uc_native_frame_t *frame, size_t slot); +__hidden void uc_native_pc_set(uc_vm_t *vm, uint32_t pc); + +__hidden uc_value_t *uc_native_invoke(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, uc_value_t **args, size_t nargs); + +__hidden int uc_native_main(int argc, char **argv, const uc_native_program_t *program); + +/* uc_vm_signal_dispatch(), without a call while no signal is pending */ +static inline bool +uc_native_signals_dispatch(uc_vm_t *vm) +{ + for (size_t i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) + if (vm->signal.raised[i]) + return uc_vm_signal_dispatch(vm) == EXCEPTION_NONE; + + return true; +} + +/* Small helpers that LLVM inlines into the generated code when the runtime is + * linked with the program as bitcode; the cost model of LLVM would keep them + * out of line because of their many call sites. */ +#ifdef __clang__ +#define UC_NATIVE_INLINE __attribute__((always_inline)) +#else +#define UC_NATIVE_INLINE +#endif + +/* The functions that generated code calls, see native.c and native_insn.c. + * They take the VM from the frame, which saves an argument at each call. */ + +__hidden void uc_native_enter(uc_native_frame_t *frame, uc_value_t **slots, + uc_upvalref_t **cells, uint32_t nslots); +__hidden void uc_native_leave(uc_native_frame_t *frame); +__hidden bool uc_native_catch(uc_native_frame_t *frame, uint32_t slot); +__hidden bool uc_native_safepoint(uc_native_frame_t *frame, uint32_t pc); + +#define UC_NATIVE_INSN_ARGS \ + uc_native_frame_t *f, uint32_t pc, int32_t d, uint32_t arg, const int32_t *x + +__hidden void uc_native_load(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_load8(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_load16(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_load32(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_lthis(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_lrexp(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_lnull(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_ltrue(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_lfalse(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_lloc(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_lupv(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_lvar(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_lval(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_pval(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_clfn(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_arfn(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_sloc(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_supv(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_svar(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_sval(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_uloc(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_uupv(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_uvar(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_uval(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_narr(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_parr(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_marr(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_nobj(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_sobj(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_mobj(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_bor(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_bxor(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_band(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_eqs(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_nes(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_eq(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_ne(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_lt(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_le(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_gt(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_ge(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_in(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_lshift(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_rshift(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_add(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_sub(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_mul(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_div(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_mod(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_exp(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_not(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_compl(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_plus(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_minus(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_jmpz(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_jmpnt(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_copy(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_pop(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_cupv(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_return(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_call(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_tailcall(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_print(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_nextk(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_nextkv(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_delete(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_import(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_export(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_dynload(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_stcall(UC_NATIVE_INSN_ARGS); + +/* Helpers of instruction sequences, see native_insn.c */ + +#define UC_NATIVE_GETK_KEEP (1u << 31) +#define UC_NATIVE_GETK_UPVAL (1u << 31) +#define UC_NATIVE_GETK_STACK UINT32_MAX + +__hidden bool uc_native_getk(uc_native_frame_t *f, uint32_t pc, int32_t d, + uint32_t arg, uint32_t src); +__hidden void uc_native_popn(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_callpop(UC_NATIVE_INSN_ARGS); +__hidden void uc_native_slocpop(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_jnull(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_notjmpz(UC_NATIVE_INSN_ARGS); + +#endif /* UCODE_INTERNAL_NATIVE_H */ diff --git a/include/ucode/internal/program.h b/include/ucode/internal/program.h index 4715f317..856493c9 100644 --- a/include/ucode/internal/program.h +++ b/include/ucode/internal/program.h @@ -70,6 +70,7 @@ __hidden bool uc_program_function_stmt_bounds(uc_function_t *, size_t, size_t *, __hidden void uc_program_function_free(uc_function_t *); __hidden uc_value_t *uc_program_get_constant(uc_program_t *, size_t); +__hidden uint64_t uc_program_get_constant_hash(uc_program_t *, size_t); __hidden ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *); #endif /* UCODE_INTERNAL_PROGRAM_H */ diff --git a/include/ucode/internal/types.h b/include/ucode/internal/types.h index 96886c7b..7c70a1a8 100644 --- a/include/ucode/internal/types.h +++ b/include/ucode/internal/types.h @@ -122,11 +122,20 @@ typedef struct { uc_declare_vector(uc_sources_t, uc_source_t *); +typedef struct { + size_t from, count; + uc_value_t *object; +} uc_namespace_t; + +uc_declare_vector(uc_namespaces_t, uc_namespace_t); + /* The exports of the modules that a program links statically, as created by - * the runs of one closure of its entry function. */ + * the runs of one closure of its entry function, and the namespace objects + * of the wildcard imports of these modules, one per module. */ typedef struct { uc_closure_t *closure; uc_modexports_t exports; + uc_namespaces_t namespaces; } uc_export_instance_t; typedef struct { @@ -137,10 +146,16 @@ typedef struct { uc_declare_vector(uc_export_instances_t, uc_export_instance_t); uc_declare_vector(uc_program_runs_t, uc_program_run_t); +uc_declare_vector(uc_constant_values_t, uc_value_t *); +uc_declare_vector(uc_constant_hashes_t, uint64_t); struct uc_program { uc_value_t header; uc_value_list_t constants; + /* the values of the constants, created on their first use */ + uc_constant_values_t constant_values; + /* their hashes as object keys, 0 until first use */ + uc_constant_hashes_t constant_hashes; uc_weakref_t functions; uc_sources_t sources; uc_export_instances_t instances; @@ -174,6 +189,35 @@ __hidden void uc_thread_context_free(void); /* Internal value entry points */ +/* Whether the value is an integer that the pointer itself holds, which needs + * no reference counting and fits an int64_t with room for one addition */ +static inline bool +ucv_is_small_integer(uc_value_t *uv) +{ + return ((uintptr_t)uv & 3) == UC_INTEGER; +} + +/* The value of an integer that the pointer holds: the bits above the type and + * the sign bit hold its magnitude */ +static inline int64_t +ucv_small_integer_get(uc_value_t *uv) +{ + uintptr_t pv = (uintptr_t)uv; + + if (((pv >> 2) & 1) == 0) + return (int64_t)(pv >> 3); + + return -(int64_t)(pv >> 3); +} + +/* Whether the value has a reference count, unlike null and the values that + * the pointer itself holds */ +static inline bool +ucv_is_counted(uc_value_t *uv) +{ + return uv != NULL && ((uintptr_t)uv & 3) == 0; +} + __hidden bool ucv_compare(int, uc_value_t *, uc_value_t *, int *); __hidden uc_value_t *ucv_closure_new(uc_vm_t *, uc_function_t *, bool); @@ -194,6 +238,13 @@ __hidden bool ucv_object_remove(uc_value_t *, const char *, size_t); __hidden uc_value_t *ucv_object_lookup_string(uc_value_t *, uc_value_t *, bool *); __hidden bool ucv_object_insert_string(uc_value_t *, uc_value_t *, uc_value_t *); +/* The hash of the string `key` as an object key, and ucv_key_get() of an + * own key of the object `obj` with that hash, which returns false if `obj` + * is no object or lacks the key */ +__hidden uint64_t ucv_object_key_hash(uc_value_t *key); +__hidden bool ucv_key_get_hashed(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key, uint64_t hash, + uc_value_t **res); + __hidden void ucv_object_sort_entries(uc_value_t *, int (*)(const uc_object_entry_t *, const uc_object_entry_t *, void *), void *); diff --git a/lib.c b/lib.c index 8edc2eed..2a4b2414 100644 --- a/lib.c +++ b/lib.c @@ -3077,15 +3077,40 @@ uc_path_normalize(const char *path) return res; } +/* Raises the error of a failed compilation of the file `filename`, or of a + * source string if NULL. */ +static void +uc_load_error(uc_vm_t *vm, const char *filename, char **err) +{ + uc_error_message_indent(err); + + if (!filename) + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "Unable to compile source string:\n\n%s", *err); + else + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "Unable to compile source file '%s':\n\n%s", filename, *err); +} + static bool uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **res, bool raw_mode, bool module_mode) { uc_parse_config_t config = *vm->config, *prev_config = vm->config; uc_value_t *closure = NULL; + char *err = NULL; struct stat st; if (uc_loader_units) - closure = uc_loader_units->unit_closure(vm, path, raw_mode, module_mode); + closure = uc_loader_units->unit_closure(vm, path, raw_mode, module_mode, &err); + + /* a script that the native program contains but that failed to compile + * fails to load as it would in the interpreter */ + if (err) { + uc_load_error(vm, path, &err); + free(err); + + return true; + } if (!closure && stat(path, &st)) return false; @@ -5957,16 +5982,8 @@ uc_load_common(uc_vm_t *vm, size_t nargs, uc_source_t *source, bool is_file) if (!vm->config || conf.force_dynlink_list.entries != vm->config->force_dynlink_list.entries) uc_vector_clear(&conf.force_dynlink_list); - if (!closure) { - uc_error_message_indent(&err); - - if (!is_file) - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, - "Unable to compile source string:\n\n%s", err); - else - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, - "Unable to compile source file '%s':\n\n%s", source->filename, err); - } + if (!closure) + uc_load_error(vm, is_file ? source->filename : NULL, &err); uc_source_put(source); free(err); diff --git a/llvm.c b/llvm.c new file mode 100644 index 00000000..6150b637 --- /dev/null +++ b/llvm.c @@ -0,0 +1,1800 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* LLVM IR backend. + * + * Each function becomes a native function that keeps the values of the + * operand stack in an array of slots, one per stack depth, as found by a + * depth analysis of the bytecode. Every instruction becomes a call to a + * runtime helper with the depth and the decoded operands as arguments, and + * the branches and exception handlers become native control flow. The + * bytecode itself is not part of the output. */ + +#include +#include +#include +#include +#include +#include + +#include "ucode/internal/native.h" +#include "ucode/internal/program.h" +#include "ucode/internal/vm.h" +#include "ucode/internal/lib.h" +#include "ucode/internal/source.h" +#include "ucode/internal/vallist.h" +#include "ucode/compiler.h" + +#define LLVM_INSN_FALLIBLE (1u << 0) + +typedef struct { + size_t off, next; + uint32_t arg; + uc_vm_insn_t insn; + bool tail; + /* reached by a branch or an exception handler */ + bool target; + ssize_t depth; + size_t extra, nextra; +} llvm_insn_t; + +uc_declare_vector(llvm_insns_t, llvm_insn_t); +uc_declare_vector(llvm_words_t, uint32_t); +uc_declare_vector(llvm_bytes_t, uint8_t); +uc_declare_vector(llvm_queue_t, size_t); + +typedef struct { + uc_program_t *program; + uc_function_t *fn; + size_t id; + llvm_insns_t insns; + llvm_words_t extra; + size_t nslots; +} llvm_func_t; + +typedef struct { + uc_stringbuf_t *data; + /* offsets by string, of the strings without NUL bytes */ + uc_value_t *index; +} llvm_strings_t; + +/* The arrays of a unit that its function and source tables index */ +typedef struct { + llvm_strings_t *strings; + size_t unit; + bool debug; + llvm_words_t words; + llvm_bytes_t bytes; +} llvm_tables_t; + +typedef struct { + char *path; + uint32_t flags; + bool relocated; + uc_program_t *program; + char *error; + size_t nexports, nwords, nbytes; +} llvm_unit_t; + +uc_declare_vector(llvm_units_t, llvm_unit_t); + +static const char *llvm_insn_names[] = { +#undef __insn +#define __insn(_name) #_name, + __insns +#undef __insn +}; + +static uint32_t +llvm_read_u32(const uint8_t *p) +{ + return p[0] * 0x1000000UL + p[1] * 0x10000UL + p[2] * 0x100UL + p[3]; +} + +static uint32_t +llvm_operand(const uint8_t *p, int8_t argtype) +{ + switch (argtype) { + case 1: return p[0]; + case 2: return p[0] * 0x100 + p[1]; + case 4: return llvm_read_u32(p); + case -4: return llvm_read_u32(p) - 0x7fffffff; + default: return 0; + } +} + +static bool +llvm_fail(char **errp, llvm_func_t *f, llvm_insn_t *in, const char *msg) +{ + if (in) + xasprintf(errp, "Function '%s' at offset %zu (%s): %s\n", + f->fn->name, in->off, llvm_insn_names[in->insn], msg); + else + xasprintf(errp, "Function '%s': %s\n", f->fn->name, msg); + + return false; +} + +/* Reads the operands that the handler of an instruction reads beyond its + * fixed operand, as uc_vm_insn_call(), uc_vm_insn_load_closure(), + * uc_vm_insn_import() and uc_vm_insn_dynload() do, into the extra words of + * the function. */ +static bool +llvm_decode_extra(llvm_func_t *f, llvm_insn_t *in, const uint8_t *p, const uint8_t *end, + char **errp) +{ + uc_function_t *target; + size_t n, i, width = 4; + uint32_t nargs; + + switch (in->insn) { + case I_CALL: + n = (in->arg >> 16) & 0x7fff; + width = 2; + break; + + case I_CLFN: + case I_ARFN: + target = uc_program_function_load(f->program, in->arg); + + if (!target) + return llvm_fail(errp, f, in, "invalid function index"); + + n = target->module ? 0 : target->nupvals; + break; + + case I_IMPORT: + n = ((in->arg >> 16) == 0xffff) ? 1 + (in->arg & 0xffff) : 0; + break; + + case I_DYNLOAD: + n = in->arg & 0xffff; + break; + + default: + n = 0; + break; + } + + in->extra = f->extra.count; + in->nextra = n; + + for (i = 0; i < n; i++) { + width = (in->insn == I_CALL || (in->insn == I_IMPORT && i == 0)) ? 2 : 4; + + if (p + width > end) + return llvm_fail(errp, f, in, "truncated operands"); + + uc_vector_push(&f->extra, (width == 2) ? p[0] * 0x100U + p[1] : llvm_read_u32(p)); + p += width; + } + + in->next = p - f->fn->chunk.entries; + + if (in->insn == I_CALL) { + nargs = in->arg & 0xffff; + + for (i = 0; i < n; i++) { + uint32_t *v = &f->extra.entries[in->extra + i]; + + if (*v >= nargs || (i > 0 && *v >= v[-1])) + return llvm_fail(errp, f, in, "invalid argument spread positions"); + } + + in->tail = (in->next + 2 <= f->fn->chunk.count && + f->fn->chunk.entries[in->next] == I_RETURN && + f->fn->chunk.entries[in->next + 1] == 0); + } + + if (in->insn == I_CLFN || in->insn == I_ARFN) + for (i = 0; i < n; i++) + f->extra.entries[in->extra + i] -= 0x7fffffff; + + return true; +} + +static bool +llvm_decode(llvm_func_t *f, char **errp) +{ + uc_chunk_t *chunk = &f->fn->chunk; + const uint8_t *end = chunk->entries + chunk->count; + size_t off = 0, width; + int8_t argtype; + llvm_insn_t *in, *prev = NULL; + + if (!chunk->count) + return llvm_fail(errp, f, NULL, "empty function"); + + while (off < chunk->count) { + if (chunk->entries[off] >= __I_MAX) { + xasprintf(errp, "Function '%s': invalid opcode %u at offset %zu\n", + f->fn->name, chunk->entries[off], off); + + return false; + } + + in = uc_vector_push(&f->insns, { + .off = off, + .insn = chunk->entries[off], + .depth = -1 + }); + + argtype = uc_vm_insn_to_argtype(in->insn); + width = abs(argtype); + + if (off + 1 + width > chunk->count) + return llvm_fail(errp, f, in, "truncated operand"); + + in->arg = llvm_operand(chunk->entries + off + 1, argtype); + + if (!llvm_decode_extra(f, in, chunk->entries + off + 1 + width, end, errp)) + return false; + + if (in->insn == I_DYNLOAD && (!prev || prev->insn != I_LOAD)) + return llvm_fail(errp, f, in, "module name is not a constant load"); + + off = in->next; + + /* the tail call marker after RETURN is data, not an instruction */ + if (in->insn == I_RETURN && off < chunk->count && chunk->entries[off] == 0) + off++; + + prev = in; + } + + return true; +} + +static ssize_t +llvm_insn_index(llvm_func_t *f, size_t off) +{ + size_t lo = 0, hi = f->insns.count, mid; + + while (lo < hi) { + mid = lo + (hi - lo) / 2; + + if (f->insns.entries[mid].off == off) + return mid; + + if (f->insns.entries[mid].off < off) + lo = mid + 1; + else + hi = mid; + } + + return -1; +} + +static size_t +llvm_jump_target(llvm_insn_t *in) +{ + if (in->insn == I_JMPNT) + return in->off + (in->arg & 0xffff) - 0x7fff; + + return in->off + (int32_t)in->arg; +} + +/* Returns the offset that the instruction pointer of the interpreter holds + * when the instruction raises: the target of a backward jump, whose signal + * handlers run after the jump, and the offset after any other instruction. */ +static size_t +llvm_raise_offset(llvm_insn_t *in) +{ + switch (in->insn) { + case I_JMP: + case I_JMPZ: + case I_JMPNT: + return llvm_jump_target(in); + + default: + return in->next; + } +} + +/* Returns the exception range that covers the instruction, the first one in + * which its raise offset lies, as uc_vm_handle_exception() selects it. */ +static uc_ehrange_t * +llvm_handler(llvm_func_t *f, llvm_insn_t *in) +{ + uc_ehranges_t *ranges = &f->fn->chunk.ehranges; + size_t off = llvm_raise_offset(in); + + for (size_t i = 0; i < ranges->count; i++) + if (ranges->entries[i].from <= off && off < ranges->entries[i].to) + return &ranges->entries[i]; + + return NULL; +} + +/* Returns the number of values the instruction needs on the stack, and in + * `delta` the change of the depth at the instruction that follows it. */ +static size_t +llvm_stack_effect(llvm_insn_t *in, ssize_t *delta) +{ + uint32_t n; + + switch (in->insn) { + case I_LOAD: case I_LOAD8: case I_LOAD16: case I_LOAD32: + case I_LREXP: case I_LTHIS: case I_LNULL: case I_LTRUE: case I_LFALSE: + case I_LVAR: case I_LLOC: case I_LUPV: case I_COPY: + case I_CLFN: case I_ARFN: case I_NARR: case I_NOBJ: + *delta = 1; + return 0; + + case I_SLOC: case I_SUPV: case I_SVAR: + case I_ULOC: case I_UUPV: case I_UVAR: + case I_NOT: case I_COMPL: case I_PLUS: case I_MINUS: + case I_RETURN: + *delta = 0; + return 1; + + case I_BOR: case I_BXOR: case I_BAND: case I_EQS: case I_NES: + case I_EQ: case I_NE: case I_LT: case I_LE: case I_GT: case I_GE: + case I_IN: case I_LSHIFT: case I_RSHIFT: case I_ADD: case I_SUB: + case I_MUL: case I_DIV: case I_MOD: case I_EXP: + case I_LVAL: case I_DELETE: case I_MARR: case I_MOBJ: + *delta = -1; + return 2; + + case I_PVAL: case I_NEXTK: + *delta = 0; + return 2; + + case I_NEXTKV: + *delta = 1; + return 2; + + case I_SVAL: case I_UVAL: + *delta = -2; + return 3; + + case I_POP: case I_CUPV: case I_PRINT: case I_JMPZ: + *delta = -1; + return 1; + + case I_JMPNT: + *delta = 0; + return ((in->arg >> 29) & 7) + 1; + + case I_PARR: case I_SOBJ: + *delta = -(ssize_t)in->arg; + return in->arg + 1; + + case I_CALL: + n = (in->arg & 0xffff) + (in->arg >> 31); + *delta = -(ssize_t)n; + return n + 1; + + case I_STCALL: + n = in->arg & 0xffff; + *delta = 1 - (ssize_t)n; + return n; + + case I_IMPORT: + *delta = ((in->arg >> 16) == 0xffff) ? 1 : 0; + return 0; + + case I_DYNLOAD: + *delta = (in->arg & 0xffff) ? -1 : 0; + return 1; + + default: + *delta = 0; + return 0; + } +} + +static bool +llvm_check_operands(llvm_func_t *f, llvm_insn_t *in, size_t depth, char **errp) +{ + uc_function_t *fn = f->fn; + size_t nconst = f->program->constants.isize; + uint32_t idx = in->arg & 0xffffff; + int32_t cap; + + switch (in->insn) { + case I_LLOC: case I_SLOC: + if (in->arg >= depth) + return llvm_fail(errp, f, in, "local index outside live values"); + break; + + case I_ULOC: + if (idx >= depth) + return llvm_fail(errp, f, in, "local index outside live values"); + break; + + case I_LUPV: case I_SUPV: + if (in->arg >= fn->nupvals) + return llvm_fail(errp, f, in, "invalid upvalue index"); + break; + + case I_UUPV: + if (idx >= fn->nupvals) + return llvm_fail(errp, f, in, "invalid upvalue index"); + break; + + case I_LOAD: case I_LREXP: case I_LVAR: case I_SVAR: + if (in->arg >= nconst) + return llvm_fail(errp, f, in, "invalid constant index"); + break; + + case I_UVAR: + if (idx >= nconst) + return llvm_fail(errp, f, in, "invalid constant index"); + break; + + case I_COPY: + if (in->arg >= depth) + return llvm_fail(errp, f, in, "copy outside live values"); + break; + + case I_EXPORT: + if (in->arg >= depth) + return llvm_fail(errp, f, in, "export outside live values"); + break; + + case I_IMPORT: + if ((in->arg >> 16) != 0xffff && (in->arg >> 16) >= fn->nupvals) + return llvm_fail(errp, f, in, "invalid import upvalue"); + break; + + case I_DYNLOAD: + if ((in->arg & 0xffff) && (in->arg >> 16) + (in->arg & 0xffff) > fn->nupvals) + return llvm_fail(errp, f, in, "invalid import upvalues"); + break; + + case I_CLFN: case I_ARFN: + for (size_t i = 0; i < in->nextra; i++) { + cap = (int32_t)f->extra.entries[in->extra + i]; + + /* a capture of the slot at the current depth is the slot that + * receives the new closure, for recursive named functions */ + if (cap < 0 && (size_t)(-cap - 1) > depth) + return llvm_fail(errp, f, in, "capture outside live values"); + + if (cap >= 0 && (size_t)cap >= fn->nupvals) + return llvm_fail(errp, f, in, "invalid captured upvalue"); + } + break; + + default: + break; + } + + return true; +} + +/* Queues the instruction at `off` for the analysis; `branch` tells that it + * is reached by other means than from the instruction before it. */ +static bool +llvm_enqueue(llvm_func_t *f, llvm_queue_t *queue, size_t off, ssize_t depth, bool branch, + char **errp) +{ + ssize_t idx = llvm_insn_index(f, off); + llvm_insn_t *in; + + if (idx < 0) { + xasprintf(errp, "Function '%s': control flow into offset %zu, which is no instruction\n", + f->fn->name, off); + + return false; + } + + in = &f->insns.entries[idx]; + in->target |= branch; + + if (in->depth >= 0) { + if (in->depth != depth) { + xasprintf(errp, "Function '%s': inconsistent stack depth at offset %zu\n", + f->fn->name, off); + + return false; + } + + return true; + } + + in->depth = depth; + uc_vector_push(queue, idx); + + return true; +} + +static uint32_t llvm_insn_flags(llvm_insn_t *in); + +/* Returns whether the instruction can raise an exception, and so branch to + * the handler of an exception range that covers it: a backward jump can, as + * it dispatches signals to handlers in ucode. */ +static bool +llvm_insn_raises(llvm_insn_t *in) +{ + switch (in->insn) { + case I_JMP: + case I_JMPZ: + case I_JMPNT: + return llvm_jump_target(in) <= in->off; + + case I_RETURN: + return false; + + default: + return llvm_insn_flags(in) & LLVM_INSN_FALLIBLE; + } +} + +/* Assigns each reachable instruction the depth of the operand stack before + * it, which is the slot its first result goes to. */ +static bool +llvm_analyze(llvm_func_t *f, char **errp) +{ + llvm_queue_t queue = { 0 }; + uc_ehranges_t *ranges = &f->fn->chunk.ehranges; + size_t maxdepth = f->fn->nargs + 1, need, i; + llvm_insn_t *in; + uc_ehrange_t *h; + ssize_t delta; + bool ok = true; + + if (f->fn->vararg && !f->fn->nargs) + return llvm_fail(errp, f, NULL, "invalid vararg function"); + + for (i = 0; i < ranges->count; i++) + if (ranges->entries[i].from > ranges->entries[i].to || + ranges->entries[i].to > f->fn->chunk.count || + llvm_insn_index(f, ranges->entries[i].target) < 0) + return llvm_fail(errp, f, NULL, "invalid exception range"); + + ok = llvm_enqueue(f, &queue, 0, f->fn->nargs + 1, true, errp); + + for (i = 0; ok && i < queue.count; i++) { + in = &f->insns.entries[queue.entries[i]]; + need = llvm_stack_effect(in, &delta); + + if ((size_t)in->depth < need) { + ok = llvm_fail(errp, f, in, "operand stack underflow"); + break; + } + + if (in->insn == I_SOBJ && (in->arg & 1)) { + ok = llvm_fail(errp, f, in, "odd object initializer count"); + break; + } + + ok = llvm_check_operands(f, in, in->depth, errp); + + if (in->depth + 1 > (ssize_t)maxdepth) + maxdepth = in->depth + 1; + + if (in->depth + delta > (ssize_t)maxdepth) + maxdepth = in->depth + delta; + + h = llvm_insn_raises(in) ? llvm_handler(f, in) : NULL; + + /* a tail call leaves the frame, so no handler of it can catch */ + if (ok && h && in->insn == I_CALL && in->tail) { + ok = llvm_fail(errp, f, in, "tail call inside an exception range"); + break; + } + + if (ok && h) { + if ((ssize_t)h->slot > in->depth && (ssize_t)h->slot > in->depth + delta) + ok = llvm_fail(errp, f, in, "handler slot outside live values"); + else + ok = llvm_enqueue(f, &queue, h->target, h->slot + 1, true, errp); + } + + if (!ok || in->insn == I_RETURN || (in->insn == I_CALL && in->tail)) + continue; + + if (in->insn == I_JMP || in->insn == I_JMPZ) { + ok = llvm_enqueue(f, &queue, llvm_jump_target(in), in->depth + delta, true, errp); + + if (in->insn == I_JMP) + continue; + } + + if (ok && in->insn == I_JMPNT) + ok = llvm_enqueue(f, &queue, llvm_jump_target(in), + in->depth - ((in->arg >> 29) & 7), true, errp); + + if (ok) + ok = llvm_enqueue(f, &queue, in->next, in->depth + delta, false, errp); + } + + uc_vector_clear(&queue); + f->nslots = maxdepth; + + return ok; +} + +static void +llvm_emit_bytes(FILE *out, const void *data, size_t len) +{ + const unsigned char *p = data; + + fputs("c\"", out); + + for (size_t i = 0; i < len; i++) { + if (p[i] >= 0x20 && p[i] < 0x7f && p[i] != '"' && p[i] != '\\') + fputc(p[i], out); + else + fprintf(out, "\\%02X", p[i]); + } + + fputs("\\00\"", out); +} + +/* Adds a string, terminated by NUL, to the string block and returns its + * offset; strings without NUL bytes are stored once. */ +static uint32_t +llvm_string_add(llvm_strings_t *strings, const char *s, size_t len) +{ + bool plain = !memchr(s, 0, len); + uc_value_t *known = plain ? ucv_object_get(strings->index, s, NULL) : NULL; + uint32_t off; + + if (known) + return ucv_uint64_get(known); + + off = uc_stringbuf_length(strings->data); + ucv_stringbuf_addstr(strings->data, s, len); + ucv_stringbuf_addstr(strings->data, "", 1); + + if (plain) + ucv_object_add(strings->index, s, ucv_uint64_new(off)); + + return off; +} + +static uint32_t +llvm_cstring_add(llvm_strings_t *strings, const char *s) +{ + return s ? llvm_string_add(strings, s, strlen(s)) : UC_NATIVE_NONE; +} + +static uint32_t +llvm_value_string_add(llvm_strings_t *strings, uc_value_t *v) +{ + if (ucv_type(v) != UC_STRING) + return UC_NATIVE_NONE; + + return llvm_string_add(strings, ucv_string_get(v), ucv_string_length(v)); +} + +static void +llvm_emit_strings(FILE *out, llvm_strings_t *strings) +{ + fprintf(out, "@uc_strings = private unnamed_addr constant [%d x i8] ", + uc_stringbuf_length(strings->data) + 1); + llvm_emit_bytes(out, strings->data->buf, uc_stringbuf_length(strings->data)); + fputs("\n", out); +} + +static uint32_t +llvm_insn_flags(llvm_insn_t *in) +{ + switch (in->insn) { + case I_NOOP: case I_LOAD: case I_LOAD8: case I_LOAD16: case I_LOAD32: + case I_LNULL: case I_LTRUE: case I_LFALSE: case I_LTHIS: + case I_LLOC: case I_LUPV: case I_COPY: case I_SLOC: case I_SUPV: + case I_POP: case I_CUPV: case I_NARR: case I_PARR: case I_NOBJ: + case I_CLFN: case I_ARFN: case I_EXPORT: + case I_NOT: case I_COMPL: case I_PLUS: case I_MINUS: case I_IN: + return 0; + + default: + return LLVM_INSN_FALLIBLE; + } +} + +static void +llvm_lower_name(char *buf, size_t size, const char *name) +{ + size_t i; + + for (i = 0; name[i] && i + 1 < size; i++) + buf[i] = (name[i] >= 'A' && name[i] <= 'Z') ? name[i] + 32 : name[i]; + + buf[i] = 0; +} + +static void +llvm_emit_exception_target(FILE *out, llvm_func_t *f, llvm_insn_t *in) +{ + uc_ehrange_t *h = llvm_handler(f, in); + + if (h) + fprintf(out, "%%H%zu", (size_t)(h - f->fn->chunk.ehranges.entries)); + else + fputs("%leave", out); +} + +static void +llvm_emit_branch_to(FILE *out, llvm_func_t *f, llvm_insn_t *in, size_t target) +{ + if (target <= in->off) + fprintf(out, "%%S%zu_%zu", in->off, target); + else + fprintf(out, "%%L%zu", target); +} + +static void +llvm_emit_args(FILE *out, llvm_func_t *f, llvm_insn_t *in, const char *prefix) +{ + fprintf(out, "(ptr %%frame, i32 %zu, i32 %zd, i32 %u, ", + in->next, in->depth, in->arg); + + if (in->nextra) + fprintf(out, "ptr @%s_x%zu)", prefix, in->off); + else + fputs("ptr null)", out); +} + +/* Emits the safepoint block of a backward jump, which dispatches signals + * before the loop runs again, as the interpreter does after every + * instruction. */ +static void +llvm_emit_safepoint(FILE *out, llvm_func_t *f, llvm_insn_t *in, size_t target) +{ + if (target > in->off) + return; + + fprintf(out, "S%zu_%zu:\n", in->off, target); + fprintf(out, " %%sp%zu_%zu = call i1 @uc_native_safepoint(ptr %%frame, i32 %zu)\n", + in->off, target, target); + fprintf(out, " br i1 %%sp%zu_%zu, label %%L%zu, label ", in->off, target, target); + llvm_emit_exception_target(out, f, in); + fputc('\n', out); +} + +static void +llvm_emit_insn(FILE *out, llvm_func_t *f, llvm_insn_t *in, const char *prefix) +{ + char name[16]; + size_t target; + + llvm_lower_name(name, sizeof(name), llvm_insn_names[in->insn]); + fprintf(out, "L%zu:\n", in->off); + + switch (in->insn) { + /* A frame starts with empty slots, and each helper empties the slots of + * the values it consumes, so the slot that LNULL pushes is empty. */ + case I_NOOP: + case I_LNULL: + fprintf(out, " br label %%L%zu\n", in->next); + return; + + case I_JMP: + target = llvm_jump_target(in); + fputs(" br label ", out); + llvm_emit_branch_to(out, f, in, target); + fputc('\n', out); + llvm_emit_safepoint(out, f, in, target); + return; + + case I_JMPZ: + case I_JMPNT: + target = llvm_jump_target(in); + fprintf(out, " %%t%zu = call i1 @uc_native_%s", in->off, name); + llvm_emit_args(out, f, in, prefix); + fprintf(out, "\n br i1 %%t%zu, label ", in->off); + llvm_emit_branch_to(out, f, in, target); + fprintf(out, ", label %%L%zu\n", in->next); + llvm_emit_safepoint(out, f, in, target); + return; + + case I_RETURN: + fputs(" call void @uc_native_return", out); + llvm_emit_args(out, f, in, prefix); + fputs("\n br label %leave\n", out); + return; + + case I_CALL: + if (in->tail) { + fputs(" call i1 @uc_native_tailcall", out); + llvm_emit_args(out, f, in, prefix); + fputs("\n br label %leave\n", out); + return; + } + + break; + + default: + break; + } + + if (!(llvm_insn_flags(in) & LLVM_INSN_FALLIBLE)) { + fprintf(out, " call void @uc_native_%s", name); + llvm_emit_args(out, f, in, prefix); + fprintf(out, "\n br label %%L%zu\n", in->next); + + return; + } + + fprintf(out, " %%r%zu = call i1 @uc_native_%s", in->off, name); + llvm_emit_args(out, f, in, prefix); + fprintf(out, "\n br i1 %%r%zu, label %%L%zu, label ", in->off, in->next); + llvm_emit_exception_target(out, f, in); + fputc('\n', out); +} + +/* Returns the instruction `n` places after the one at index `i` if the ones + * up to it can run as one helper: none of them after the first is reached + * by a branch. */ +static llvm_insn_t * +llvm_fusable(llvm_func_t *f, size_t i, size_t n) +{ + llvm_insn_t *in; + + if (i + n >= f->insns.count) + return NULL; + + for (size_t j = 1; j <= n; j++) { + in = &f->insns.entries[i + j]; + + if (in->target || in->depth < 0) + return NULL; + } + + return &f->insns.entries[i + n]; +} + +static bool +llvm_insn_is(llvm_insn_t *in, uc_vm_insn_t a, uc_vm_insn_t b) +{ + return in && (in->insn == a || in->insn == b); +} + +/* LOAD of a constant key, then LVAL or PVAL, after an LLOC or LUPV or not */ +static size_t +llvm_emit_getk(FILE *out, llvm_func_t *f, size_t i) +{ + llvm_insn_t *first = &f->insns.entries[i], *load = first, *get; + uint32_t src = UC_NATIVE_GETK_STACK; + size_t n = 1; + + if (llvm_insn_is(first, I_LLOC, I_LUPV)) { + if (first->arg >= UC_NATIVE_GETK_UPVAL) + return 0; + + load = llvm_fusable(f, i, 1); + src = first->arg | ((first->insn == I_LUPV) ? UC_NATIVE_GETK_UPVAL : 0); + n = 2; + } + + get = llvm_fusable(f, i, n); + + if (!load || load->insn != I_LOAD || load->arg > 0x00ffffff || + !llvm_insn_is(get, I_LVAL, I_PVAL)) + return 0; + + fprintf(out, "L%zu:\n", first->off); + fprintf(out, " %%r%zu = call i1 @uc_native_getk(ptr %%frame, i32 %zu, i32 %zd, " + "i32 %u, i32 %u)\n", first->off, get->next, first->depth, + load->arg | ((get->insn == I_PVAL) ? UC_NATIVE_GETK_KEEP : 0), src); + fprintf(out, " br i1 %%r%zu, label %%L%zu, label ", first->off, get->next); + llvm_emit_exception_target(out, f, get); + fputc('\n', out); + + return n + 1; +} + +/* A helper `name` for the instruction at `i` up to `last`, called with the + * operands of the instruction `op`, which raises if `fallible` is set, and + * the depth before the first one */ +static void +llvm_emit_fused_call(FILE *out, llvm_func_t *f, size_t i, llvm_insn_t *op, llvm_insn_t *last, + uint32_t arg, const char *name, bool fallible, const char *prefix) +{ + llvm_insn_t *first = &f->insns.entries[i]; + llvm_insn_t in = *op; + + in.depth = first->depth; + in.arg = arg; + + fprintf(out, "L%zu:\n", first->off); + + if (!fallible) { + fprintf(out, " call void @uc_native_%s", name); + llvm_emit_args(out, f, &in, prefix); + fprintf(out, "\n br label %%L%zu\n", last->next); + + return; + } + + fprintf(out, " %%r%zu = call i1 @uc_native_%s", first->off, name); + llvm_emit_args(out, f, &in, prefix); + fprintf(out, "\n br i1 %%r%zu, label %%L%zu, label ", first->off, last->next); + llvm_emit_exception_target(out, f, op); + fputc('\n', out); +} + +/* A helper `name` that runs the instruction at `i` up to the JMPZ `jump` and + * returns whether the jump is taken */ +static void +llvm_emit_fused_jump(FILE *out, llvm_func_t *f, size_t i, llvm_insn_t *jump, uint32_t arg, + const char *name) +{ + llvm_insn_t *first = &f->insns.entries[i]; + size_t target = llvm_jump_target(jump); + + fprintf(out, "L%zu:\n", first->off); + fprintf(out, " %%t%zu = call i1 @uc_native_%s(ptr %%frame, i32 %zu, i32 %zd, " + "i32 %u, ptr null)\n", first->off, name, jump->next, first->depth, arg); + fprintf(out, " br i1 %%t%zu, label ", first->off); + llvm_emit_branch_to(out, f, jump, target); + fprintf(out, ", label %%L%zu\n", jump->next); + llvm_emit_safepoint(out, f, jump, target); +} + +/* Emits the instruction at `i` together with the ones after it as one + * helper call where a helper for the sequence exists, and returns the + * number of instructions it covers, or 0. */ +static size_t +llvm_emit_fused(FILE *out, llvm_func_t *f, size_t i, const char *prefix) +{ + llvm_insn_t *in = &f->insns.entries[i], *next = llvm_fusable(f, i, 1), *jump; + size_t n; + + if ((n = llvm_emit_getk(out, f, i)) != 0) + return n; + + switch (in->insn) { + case I_POP: + for (n = 1; llvm_insn_is(llvm_fusable(f, i, n), I_POP, I_POP); n++) + ; + + if (n < 2) + return 0; + + llvm_emit_fused_call(out, f, i, in, &f->insns.entries[i + n - 1], n, "popn", false, prefix); + + return n; + + case I_CALL: + if (in->tail || !llvm_insn_is(next, I_POP, I_POP)) + return 0; + + llvm_emit_fused_call(out, f, i, in, next, in->arg, "callpop", true, prefix); + + return 2; + + case I_SLOC: + if (!llvm_insn_is(next, I_POP, I_POP)) + return 0; + + llvm_emit_fused_call(out, f, i, in, next, in->arg, "slocpop", false, prefix); + + return 2; + + case I_LNULL: + jump = llvm_fusable(f, i, 2); + + if (!llvm_insn_is(next, I_EQS, I_NES) || !llvm_insn_is(jump, I_JMPZ, I_JMPZ)) + return 0; + + llvm_emit_fused_jump(out, f, i, jump, next->insn == I_NES, "jnull"); + + return 3; + + case I_NOT: + if (!llvm_insn_is(next, I_JMPZ, I_JMPZ)) + return 0; + + llvm_emit_fused_jump(out, f, i, next, 0, "notjmpz"); + + return 2; + + default: + return 0; + } +} + +static void +llvm_emit_function(FILE *out, llvm_func_t *f, size_t unit) +{ + uc_ehranges_t *ranges = &f->fn->chunk.ehranges; + char prefix[48]; + llvm_insn_t *in; + + snprintf(prefix, sizeof(prefix), "uc_u%zu_f%zu", unit, f->id); + + uc_vector_foreach(&f->insns, x) { + if (!x->nextra || x->depth < 0) + continue; + + fprintf(out, "@%s_x%zu = private unnamed_addr constant [%zu x i32] [", prefix, x->off, x->nextra); + + for (size_t i = 0; i < x->nextra; i++) + fprintf(out, "%si32 %d", i ? ", " : "", (int32_t)f->extra.entries[x->extra + i]); + + fputs("]\n", out); + } + + fprintf(out, "define internal void @%s(ptr %%vm, ptr %%frame) {\n", prefix); + fprintf(out, "entry:\n"); + fprintf(out, " %%slots = alloca [%zu x ptr]\n", f->nslots); + fprintf(out, " %%cells = alloca [%zu x ptr]\n", f->nslots); + fprintf(out, " store [%zu x ptr] zeroinitializer, ptr %%slots\n", f->nslots); + fprintf(out, " store [%zu x ptr] zeroinitializer, ptr %%cells\n", f->nslots); + fprintf(out, " call void @uc_native_enter(ptr %%frame, ptr %%slots, ptr %%cells, i32 %zu)\n", + f->nslots); + fprintf(out, " br label %%L0\n"); + + for (size_t i = 0, n; i < f->insns.count; i += n) { + in = &f->insns.entries[i]; + n = 1; + + if (in->depth < 0) + continue; + + n = llvm_emit_fused(out, f, i, prefix); + + if (!n) { + llvm_emit_insn(out, f, in, prefix); + n = 1; + } + } + + for (size_t i = 0; i < ranges->count; i++) { + if (llvm_insn_index(f, ranges->entries[i].target) < 0 || + f->insns.entries[llvm_insn_index(f, ranges->entries[i].target)].depth < 0) + continue; + + fprintf(out, "H%zu:\n", i); + fprintf(out, " %%h%zu = call i1 @uc_native_catch(ptr %%frame, i32 %zu)\n", + i, ranges->entries[i].slot); + fprintf(out, " br i1 %%h%zu, label %%L%zu, label %%leave\n", i, ranges->entries[i].target); + } + + fprintf(out, "leave:\n"); + fprintf(out, " call void @uc_native_leave(ptr %%frame)\n"); + fprintf(out, " ret void\n}\n\n"); +} + +static void +llvm_emit_declarations(FILE *out) +{ + char name[16]; + + fputs("declare void @uc_native_enter(ptr, ptr, ptr, i32)\n" + "declare void @uc_native_leave(ptr)\n" + "declare zeroext i1 @uc_native_catch(ptr, i32)\n" + "declare zeroext i1 @uc_native_safepoint(ptr, i32)\n" + "declare zeroext i1 @uc_native_tailcall(ptr, i32, i32, i32, ptr)\n" + "declare i32 @uc_native_main(i32, ptr, ptr)\n" + "declare zeroext i1 @uc_native_getk(ptr, i32, i32, i32, i32)\n" + "declare void @uc_native_popn(ptr, i32, i32, i32, ptr)\n" + "declare zeroext i1 @uc_native_callpop(ptr, i32, i32, i32, ptr)\n" + "declare void @uc_native_slocpop(ptr, i32, i32, i32, ptr)\n" + "declare zeroext i1 @uc_native_jnull(ptr, i32, i32, i32, ptr)\n" + "declare zeroext i1 @uc_native_notjmpz(ptr, i32, i32, i32, ptr)\n", out); + + for (size_t i = 0; i < __I_MAX; i++) { + llvm_insn_t in = { .insn = i }; + + if (i == I_NOOP || i == I_JMP) + continue; + + llvm_lower_name(name, sizeof(name), llvm_insn_names[i]); + + fprintf(out, "declare %s @uc_native_%s(ptr, i32, i32, i32, ptr)\n", + (i == I_RETURN || !(llvm_insn_flags(&in) & LLVM_INSN_FALLIBLE)) ? "void" : "zeroext i1", + name); + } + + fputs("\n%uc_const = type { i64, i32, i32, i32, i32 }\n" + "%uc_fn = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }\n" + "%uc_src = type { i32, i32, i32, i32, i32, i32, i32, i32, i32 }\n" + "%uc_unit = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32 }\n" + "%uc_prog = type { ptr, ptr, i32, i32, i32, i32 }\n\n", out); +} + +static void +llvm_emit_value(FILE *out, llvm_strings_t *strings, uc_value_t *v) +{ + union { double d; uint64_t u; } bits; + + switch (ucv_type(v)) { + case UC_STRING: + fprintf(out, "%%uc_const { i64 0, i32 %u, i32 %d, i32 %zu, i32 0 }", + llvm_value_string_add(strings, v), UC_NATIVE_CONST_STRING, + ucv_string_length(v)); + break; + + case UC_DOUBLE: + bits.d = ucv_double_get(v); + fprintf(out, "%%uc_const { i64 %" PRIu64 ", i32 %u, i32 %d, i32 0, i32 0 }", + bits.u, UC_NATIVE_NONE, UC_NATIVE_CONST_DOUBLE); + break; + + case UC_INTEGER: + if (ucv_int64_get(v) < 0) + fprintf(out, "%%uc_const { i64 %" PRId64 ", i32 %u, i32 %d, i32 1, i32 0 }", + ucv_int64_get(v), UC_NATIVE_NONE, UC_NATIVE_CONST_INT); + else + fprintf(out, "%%uc_const { i64 %" PRIu64 ", i32 %u, i32 %d, i32 0, i32 0 }", + ucv_uint64_get(v), UC_NATIVE_NONE, UC_NATIVE_CONST_INT); + break; + + default: + fprintf(out, "%%uc_const { i64 0, i32 %u, i32 %d, i32 0, i32 0 }", + UC_NATIVE_NONE, UC_NATIVE_CONST_NULL); + break; + } +} + +static void +llvm_emit_words(FILE *out, const char *name, size_t unit, llvm_words_t *words) +{ + if (!words->count) + return; + + fprintf(out, "@uc_u%zu_%s = private unnamed_addr constant [%zu x i32] [", + unit, name, words->count); + + for (size_t i = 0; i < words->count; i++) + fprintf(out, "%si32 %u", i ? ", " : "", words->entries[i]); + + fputs("]\n", out); +} + +static void +llvm_emit_bytes_array(FILE *out, const char *name, size_t unit, llvm_bytes_t *bytes) +{ + if (!bytes->count) + return; + + fprintf(out, "@uc_u%zu_%s = private unnamed_addr constant [%zu x i8] [", + unit, name, bytes->count); + + for (size_t i = 0; i < bytes->count; i++) + fprintf(out, "%si8 %u", i ? ", " : "", bytes->entries[i]); + + fputs("]\n", out); +} + +static void +llvm_emit_array_ref(FILE *out, const char *name, size_t unit, size_t count) +{ + if (count) + fprintf(out, "ptr @uc_u%zu_%s, ", unit, name); + else + fputs("ptr null, ", out); +} + +static void +llvm_emit_constants(FILE *out, uc_program_t *prog, llvm_tables_t *t) +{ + size_t n = prog->constants.isize; + uc_value_t *v; + + fprintf(out, "@uc_u%zu_consts = private constant [%zu x %%uc_const] [", t->unit, n); + + for (size_t i = 0; i < n; i++) { + v = uc_program_get_constant(prog, i); + fputs(i ? ", " : "", out); + llvm_emit_value(out, t->strings, v); + ucv_put(v); + } + + fputs("]\n", out); +} + +static bool +llvm_variable_wanted(uc_function_t *fn, uc_varrange_t *var, bool debug) +{ + return debug || (var->slot >= 1 && var->slot <= fn->nargs); +} + +static uint32_t +llvm_function_flags(uc_function_t *fn) +{ + return (fn->arrow ? UC_NATIVE_FN_ARROW : 0) | + (fn->vararg ? UC_NATIVE_FN_VARARG : 0) | + (fn->strict ? UC_NATIVE_FN_STRICT : 0) | + (fn->module ? UC_NATIVE_FN_MODULE : 0); +} + +static void +llvm_function_offsets(llvm_tables_t *t, uc_chunk_t *chunk, uc_native_function_t *fd) +{ + fd->offsets = t->bytes.count; + fd->noffsets = chunk->debuginfo.offsets.count; + + uc_vector_foreach(&chunk->debuginfo.offsets, o) { + uc_vector_push(&t->bytes, o->bytes); + uc_vector_push(&t->bytes, o->insns); + } +} + +/* Adds the variables that the function keeps and the names they refer to, + * renumbered in the order of their first use. */ +static void +llvm_function_variables(llvm_tables_t *t, uc_function_t *fn, uc_native_function_t *fd) +{ + uc_chunk_t *chunk = &fn->chunk; + size_t nnames = chunk->debuginfo.varnames.isize, i; + uint32_t *map = xalloc((nnames ? nnames : 1) * sizeof(*map)); + uint32_t *names; + uc_value_t *v; + + for (i = 0; i < nnames; i++) + map[i] = UC_NATIVE_NONE; + + fd->variables = t->words.count; + + uc_vector_foreach(&chunk->debuginfo.variables, var) { + if (!llvm_variable_wanted(fn, var, t->debug)) + continue; + + if (var->nameidx < nnames && map[var->nameidx] == UC_NATIVE_NONE) + map[var->nameidx] = fd->nvarnames++; + + uc_vector_push(&t->words, var->from); + uc_vector_push(&t->words, var->to); + uc_vector_push(&t->words, (var->slot >= (size_t)-1 / 2) + ? (uint32_t)(var->slot - (size_t)-1 / 2) | UC_NATIVE_VAR_UPVAL + : (uint32_t)var->slot); + uc_vector_push(&t->words, var->nameidx < nnames ? map[var->nameidx] : UC_NATIVE_NONE); + fd->nvariables++; + } + + fd->varnames = t->words.count; + names = uc_vector_extend(&t->words, fd->nvarnames); + t->words.count += fd->nvarnames; + + for (i = 0; i < nnames; i++) { + if (map[i] == UC_NATIVE_NONE) + continue; + + v = uc_vallist_get(&chunk->debuginfo.varnames, i); + names[map[i]] = llvm_value_string_add(t->strings, v); + ucv_put(v); + } + + free(map); +} + +static void +llvm_emit_function_table(FILE *out, llvm_func_t *funcs, size_t count, llvm_tables_t *t) +{ + uc_native_function_t fd; + uc_function_t *fn; + size_t i; + + fprintf(out, "@uc_u%zu_fns = private constant [%zu x %%uc_fn] [", t->unit, count); + + for (i = 0; i < count; i++) { + fn = funcs[i].fn; + fd = (uc_native_function_t){ + .name = llvm_cstring_add(t->strings, fn->name), + .nargs = fn->nargs, + .nupvals = fn->nupvals, + .flags = llvm_function_flags(fn), + .srcidx = fn->srcidx, + .srcpos = t->debug ? fn->srcpos : 0 + }; + + if (t->debug) + llvm_function_offsets(t, &fn->chunk, &fd); + + llvm_function_variables(t, fn, &fd); + + fprintf(out, "%s%%uc_fn { i32 %u, i32 %u, i32 %u, i32 %u, i32 %u, i32 %u, " + "i32 %u, i32 %u, i32 %u, i32 %u, i32 %u, i32 %u }", + i ? ",\n " : "\n ", fd.name, fd.offsets, fd.variables, fd.varnames, + fd.noffsets, fd.nvariables, fd.nvarnames, fd.nargs, fd.nupvals, + fd.flags, fd.srcidx, fd.srcpos); + } + + fputs("\n]\n", out); + fprintf(out, "@uc_u%zu_entries = private constant [%zu x ptr] [", t->unit, count); + + for (i = 0; i < count; i++) + fprintf(out, "%sptr @uc_u%zu_f%zu", i ? ", " : "", t->unit, funcs[i].id); + + fputs("]\n", out); +} + +/* Returns the number of exports of all sources. */ +static size_t +llvm_emit_source_table(FILE *out, uc_program_t *prog, llvm_tables_t *t) +{ + size_t n = prog->sources.count, nexports = 0, len, i, j; + uc_native_source_t *descs = xcalloc(n ? n : 1, sizeof(*descs)); + uc_source_t *src; + char *content; + + for (i = 0; i < n; i++) { + src = prog->sources.entries[i]; + content = t->debug ? uc_source_read_content(src, &len) : NULL; + + descs[i] = (uc_native_source_t){ + .filename = llvm_cstring_add(t->strings, src->filename), + .runpath = llvm_cstring_add(t->strings, src->runpath), + .text = content ? llvm_string_add(t->strings, content, len) : UC_NATIVE_NONE, + .textlen = content ? len : 0, + .exports = nexports, + .nexports = src->exports.count + }; + + free(content); + nexports += src->exports.count; + + if (!t->debug) + continue; + + descs[i].lineinfo = t->bytes.count; + descs[i].nlineinfo = src->lineinfo.count; + + for (j = 0; j < src->lineinfo.count; j++) + uc_vector_push(&t->bytes, src->lineinfo.entries[j]); + } + + if (nexports) { + fprintf(out, "@uc_u%zu_exports = private constant [%zu x %%uc_const] [", t->unit, nexports); + + for (i = 0, nexports = 0; i < n; i++) { + src = prog->sources.entries[i]; + + for (j = 0; j < src->exports.count; j++, nexports++) { + fputs(nexports ? ", " : "", out); + llvm_emit_value(out, t->strings, src->exports.entries[j]); + } + } + + fputs("]\n", out); + } + + fprintf(out, "@uc_u%zu_srcs = private constant [%zu x %%uc_src] [", t->unit, n); + + for (i = 0; i < n; i++) + fprintf(out, "%s%%uc_src { i32 %u, i32 %u, i32 %u, i32 %u, i32 %u, i32 %u, i32 %u, " + "i32 %u, i32 0 }", i ? ",\n " : "\n ", + descs[i].filename, descs[i].runpath, descs[i].text, descs[i].lineinfo, + descs[i].exports, descs[i].textlen, descs[i].nlineinfo, descs[i].nexports); + + fputs("\n]\n", out); + free(descs); + + return nexports; +} + +static size_t +llvm_function_count(uc_program_t *prog) +{ + size_t count = 0; + + uc_program_function_foreach(prog, fn) + count++; + + return count; +} + +static bool +llvm_emit_unit(FILE *out, llvm_unit_t *u, llvm_tables_t *t, char **errp) +{ + uc_program_t *prog = u->program; + size_t count = llvm_function_count(prog), i = 0; + llvm_func_t *funcs; + bool ok = true; + + funcs = xcalloc(count ? count : 1, sizeof(*funcs)); + + uc_program_function_foreach(prog, fn) { + funcs[i].program = prog; + funcs[i].fn = fn; + funcs[i].id = i + 1; + i++; + } + + for (i = 0; ok && i < count; i++) + ok = llvm_decode(&funcs[i], errp) && llvm_analyze(&funcs[i], errp); + + for (i = 0; ok && i < count; i++) + llvm_emit_function(out, &funcs[i], t->unit); + + if (ok) { + llvm_emit_constants(out, prog, t); + llvm_emit_function_table(out, funcs, count, t); + u->nexports = llvm_emit_source_table(out, prog, t); + u->nwords = t->words.count; + u->nbytes = t->bytes.count; + llvm_emit_words(out, "words", t->unit, &t->words); + llvm_emit_bytes_array(out, "bytes", t->unit, &t->bytes); + } + + for (i = 0; i < count; i++) { + uc_vector_clear(&funcs[i].insns); + uc_vector_clear(&funcs[i].extra); + } + + uc_vector_clear(&t->words); + uc_vector_clear(&t->bytes); + free(funcs); + + return ok; +} + +static char * +llvm_constant_string(uc_program_t *prog, uint32_t idx) +{ + uc_value_t *v = uc_program_get_constant(prog, idx); + char *s = (ucv_type(v) == UC_STRING) ? xstrdup(ucv_string_get(v)) : NULL; + + ucv_put(v); + + return s; +} + +/* Returns the script that require() finds for module `name` through the + * search path, or NULL if it finds a shared extension or nothing. */ +static char * +llvm_resolve_module(uc_parse_config_t *config, const char *name) +{ + uc_search_path_t *search = &config->module_search_path; + uc_stringbuf_t *buf; + char *path = NULL; + const char *ext; + bool found; + + for (size_t i = 0; i < search->count; i++) { + buf = uc_stringbuf_new(); + ext = uc_require_path_expand(search->entries[i], name, buf); + found = ext && access(buf->buf, F_OK) == 0; + + if (found && !strcmp(ext, ".uc")) + path = xstrdup(buf->buf); + + uc_stringbuf_free(buf); + + if (found) + break; + } + + return path; +} + +/* Compiles the script `path` in the mode its loader uses and adds it to the + * units of the native program. A script that fails to compile is left out, so + * that loading it fails at runtime, as it would in the interpreter. */ +static void +llvm_unit_add(llvm_units_t *units, uc_parse_config_t *config, char *path, + const char *logical, uint32_t flags) +{ + uc_parse_config_t conf = *config; + uc_program_t *program; + uc_source_t *source; + char *key, *err = NULL; + + if (!path) + return; + + key = uc_path_normalize(logical ? logical : path); + + for (size_t i = 1; key && i < units->count; i++) { + if (!strcmp(units->entries[i].path, key) && units->entries[i].flags == flags) { + free(key); + key = NULL; + } + } + + source = key ? uc_source_new_file(path) : NULL; + free(path); + + if (!source) { + free(key); + + return; + } + + conf.raw_mode = !(flags & UC_NATIVE_UNIT_TEMPLATE); + conf.compile_module = flags & UC_NATIVE_UNIT_MODULE; + program = uc_compile(&conf, source, &err); + + uc_source_put(source); + + /* a script that fails to compile is kept with its error, which loading it + * raises at runtime, as the interpreter does */ + uc_vector_push(units, { + .path = key, + .flags = flags, + .relocated = logical != NULL, + .program = program, + .error = program ? NULL : err + }); + + if (program) + free(err); +} + +/* Adds the file `path`, or the scripts below the directory `path`, under the + * logical path `logical` if given. */ +static void +llvm_unit_embed(llvm_units_t *units, uc_parse_config_t *config, + const char *path, const char *logical, bool template) +{ + uint32_t flags = template ? UC_NATIVE_UNIT_TEMPLATE : 0; + char *child, *child_logical = NULL; + struct dirent *e; + struct stat st; + const char *ext; + DIR *d; + + if (stat(path, &st)) + return; + + if (!S_ISDIR(st.st_mode)) { + llvm_unit_add(units, config, xstrdup(path), logical, flags); + + return; + } + + d = opendir(path); + + while (d && (e = readdir(d)) != NULL) { + if (e->d_name[0] == '.') + continue; + + xasprintf(&child, "%s/%s", path, e->d_name); + + if (logical) + xasprintf(&child_logical, "%s/%s", logical, e->d_name); + + ext = strrchr(e->d_name, '.'); + + if (!stat(child, &st) && S_ISDIR(st.st_mode)) + llvm_unit_embed(units, config, child, child_logical, template); + else if (ext && (!strcmp(ext, ".uc") || (template && !strcmp(ext, ".ut")))) + llvm_unit_add(units, config, xstrdup(child), child_logical, flags); + + free(child); + free(child_logical); + child_logical = NULL; + } + + if (d) + closedir(d); +} + +/* Returns the file that include() resolves `literal` to from function `fn` of + * the unit, and in `logical` the path it resolves to at runtime if the unit is + * embedded under a logical path. */ +static char * +llvm_unit_include(llvm_unit_t *unit, uc_function_t *fn, const char *literal, char **logical) +{ + uc_source_t *source = uc_program_function_source(fn); + const char *slash; + + *logical = NULL; + + if (unit->relocated && *literal != '/' && + source == unit->program->sources.entries[0] && + (slash = strrchr(unit->path, '/')) != NULL) + xasprintf(logical, "%.*s/%s", (int)(slash - unit->path), unit->path, literal); + + return uc_include_path(source->runpath, literal); +} + +/* Adds the scripts that the unit loads with literal arguments: a require(), + * include() or render() call whose first argument is a string constant, and + * a dynamically linked import. */ +static void +llvm_unit_discover(llvm_units_t *units, size_t idx, uc_parse_config_t *config) +{ + uc_program_t *prog = units->entries[idx].program; + uint32_t flags = units->entries[idx].flags; + char *loader, *literal, *path, *logical, *err; + llvm_insn_t *a, *b; + llvm_func_t f; + + if (!prog) + return; + + uc_program_function_foreach(prog, fn) { + memset(&f, 0, sizeof(f)); + f.program = prog; + f.fn = fn; + err = NULL; + + if (!llvm_decode(&f, &err)) { + free(err); + uc_vector_clear(&f.insns); + uc_vector_clear(&f.extra); + continue; + } + + for (size_t i = 0; i + 1 < f.insns.count; i++) { + a = &f.insns.entries[i]; + b = &f.insns.entries[i + 1]; + + if (a->insn == I_LOAD && b->insn == I_DYNLOAD) { + literal = llvm_constant_string(prog, a->arg); + + if (literal) + llvm_unit_add(units, config, llvm_resolve_module(config, literal), + NULL, UC_NATIVE_UNIT_MODULE); + + free(literal); + continue; + } + + if (a->insn != I_LVAR || b->insn != I_LOAD) + continue; + + loader = llvm_constant_string(prog, a->arg); + literal = llvm_constant_string(prog, b->arg); + + if (loader && literal && !strcmp(loader, "require")) { + llvm_unit_add(units, config, llvm_resolve_module(config, literal), NULL, 0); + } + else if (loader && literal && + (!strcmp(loader, "include") || !strcmp(loader, "render"))) { + /* the unit list may have grown, so look the unit up again */ + path = llvm_unit_include(&units->entries[idx], fn, literal, &logical); + llvm_unit_add(units, config, path, logical, + !strcmp(loader, "render") ? UC_NATIVE_UNIT_TEMPLATE + : (flags & UC_NATIVE_UNIT_TEMPLATE)); + free(logical); + } + + free(loader); + free(literal); + } + + uc_vector_clear(&f.insns); + uc_vector_clear(&f.extra); + } +} + +/* Writes the map file: the programs of all units with debug information, each + * under the path that stack traces of the native program name it by. */ +static bool +llvm_write_map(llvm_units_t *units, const uc_llvm_options_t *opts, char **errp) +{ + char *buf = NULL; + size_t len = 0; + FILE *map, *mem; + + map = fopen(opts->map, "wb"); + + if (!map) { + xasprintf(errp, "Unable to open map file '%s': %m\n", opts->map); + + return false; + } + + fputs(UC_NATIVE_MAP_MAGIC, map); + + for (size_t i = 0; i < units->count; i++) { + if (!units->entries[i].program) + continue; + + mem = open_memstream(&buf, &len); + + if (!mem) + break; + + uc_program_write(units->entries[i].program, mem, true); + fclose(mem); + + fprintf(map, "%s\n%zu\n", + units->entries[i].path ? units->entries[i].path + : (opts->script_name ? opts->script_name : ""), + len); + fwrite(buf, 1, len, map); + free(buf); + buf = NULL; + } + + if (fclose(map)) { + xasprintf(errp, "Unable to write map file '%s': %m\n", opts->map); + + return false; + } + + return true; +} + +bool +uc_program_write_llvm(uc_program_t *program, FILE *out, + const uc_llvm_options_t *opts, char **errp) +{ + llvm_strings_t strings = { .data = uc_stringbuf_new(), .index = ucv_object_new(NULL) }; + llvm_units_t units = { 0 }; + llvm_tables_t tables; + llvm_unit_t *u; + bool ok = true; + size_t i; + + uc_vector_push(&units, { + .program = uc_program_get(program), + .flags = (opts->config && !opts->config->raw_mode) ? UC_NATIVE_UNIT_TEMPLATE : 0 + }); + + for (i = 0; opts->embeds && i < opts->embeds->count; i++) + llvm_unit_embed(&units, opts->config, opts->embeds->entries[i].path, + opts->embeds->entries[i].logical, opts->embeds->entries[i].template); + + for (i = 0; opts->config && i < units.count; i++) + llvm_unit_discover(&units, i, opts->config); + + if (opts->triple) + fprintf(out, "target triple = \"%s\"\n\n", opts->triple); + + llvm_emit_declarations(out); + + for (i = 0; ok && i < units.count; i++) { + u = &units.entries[i]; + tables = (llvm_tables_t){ .strings = &strings, .unit = i, .debug = opts->debug }; + + if (u->program) + ok = llvm_emit_unit(out, u, &tables, errp); + } + + if (ok) { + fprintf(out, "@uc_units = private constant [%zu x %%uc_unit] [", units.count); + + for (i = 0; i < units.count; i++) { + u = &units.entries[i]; + fputs(i ? ",\n %uc_unit { " : "\n %uc_unit { ", out); + + if (u->program) { + fprintf(out, "ptr @uc_u%zu_srcs, ptr @uc_u%zu_consts, ptr @uc_u%zu_fns, " + "ptr @uc_u%zu_entries, ", i, i, i, i); + llvm_emit_array_ref(out, "exports", i, u->nexports); + llvm_emit_array_ref(out, "words", i, u->nwords); + llvm_emit_array_ref(out, "bytes", i, u->nbytes); + } + else { + fputs("ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null, ", out); + } + + fprintf(out, "i32 %u, i32 %u, i32 %zu, i32 %zu, i32 %zu, i32 %u }", + llvm_cstring_add(&strings, u->path), llvm_cstring_add(&strings, u->error), + u->program ? u->program->sources.count : 0, + u->program ? u->program->constants.isize : 0, + u->program ? llvm_function_count(u->program) : 0, u->flags); + } + + fputs("\n]\n", out); + fprintf(out, "@uc_program = private constant %%uc_prog { ptr @uc_strings, ptr @uc_units, " + "i32 %u, i32 %d, i32 %zu, i32 0 }\n\n", + llvm_cstring_add(&strings, opts->script_name), UC_NATIVE_ABI_VERSION, units.count); + llvm_emit_strings(out, &strings); + fputs("define i32 @main(i32 %argc, ptr %argv) {\n" + " %r = call i32 @uc_native_main(i32 %argc, ptr %argv, ptr @uc_program)\n" + " ret i32 %r\n}\n", out); + } + + if (ok && opts->map) + ok = llvm_write_map(&units, opts, errp); + + for (i = 0; i < units.count; i++) { + free(units.entries[i].path); + free(units.entries[i].error); + + if (units.entries[i].program) + uc_program_put(units.entries[i].program); + } + + uc_vector_clear(&units); + uc_stringbuf_free(strings.data); + ucv_put(strings.index); + + return ok; +} diff --git a/main.c b/main.c index d5cbfd42..e1c9b72f 100644 --- a/main.c +++ b/main.c @@ -31,6 +31,7 @@ #include "ucode/program.h" #include "ucode/internal/cli.h" +#include "ucode/internal/native.h" #ifdef __GLIBC__ # define POSIXLY_CORRECT_FLAG "+" @@ -104,7 +105,13 @@ print_usage(const char *app) " Supported flags: no-interp (omit interpreter line), interp=... (over-\n" " ride interpreter line with ...), dynlink=... (force import from ... to\n" " be treated as shared extensions loaded at runtime), module (build load-\n" - " able library).\n\n" + " able library), llvm (write LLVM IR of a native program, if built with\n" + " LLVM support), target=... (LLVM target triple of the native program),\n" + " embed=[path=]file (also embed `file` or the scripts below it, which the\n" + " program loads as `path` by computed names), embed-template=[path=]file\n" + " (the same for templates), debug (embed debug information and source\n" + " text for complete stack traces), map=... (write the program with debug\n" + " information to ..., for ucode-trace to resolve its stack traces).\n\n" "-o path\n" " Output file path when compiling. If omitted, the compiled byte code\n" @@ -155,7 +162,35 @@ typedef struct { bool autoprint; debug_mode_t debug; char *breakpoint; + bool llvm; + uc_llvm_options_t *native; } compile_opts_t; + +static int +write_llvm(uc_vm_t *vm, uc_program_t *program, FILE *out, const compile_opts_t *opts) +{ +#ifdef HAVE_LLVM_SUPPORT + uc_parse_config_t *config = vm->config; + char *err = NULL; + bool ok; + + opts->native->config = config; + + ok = uc_program_write_llvm(program, out, opts->native, &err); + + if (!ok) { + fprintf(stderr, "%s", err ? err : "Unable to write LLVM IR\n"); + free(err); + } + + return ok ? 0 : -1; +#else + fprintf(stderr, "This ucode was built without LLVM support\n"); + + return -1; +#endif +} + static int compile(uc_vm_t *vm, uc_source_t *src, FILE *precompile, const compile_opts_t *opts) { @@ -176,6 +211,12 @@ compile(uc_vm_t *vm, uc_source_t *src, FILE *precompile, const compile_opts_t *o goto out; } + if (precompile && opts->llvm) { + rc = write_llvm(vm, program, precompile, opts); + fclose(precompile); + goto out; + } + if (precompile) { if (interp) fprintf(precompile, "#!%s\n", interp); @@ -408,7 +449,7 @@ parse_template_modeflags(char *opt, uc_parse_config_t *config) static void parse_compile_flags(char *opt, char **interp, uc_search_path_t *dynlink_list, - bool *module) + bool *module, bool *llvm, uc_llvm_options_t *native) { char *p, *k, *v; @@ -446,6 +487,48 @@ parse_compile_flags(char *opt, char **interp, uc_search_path_t *dynlink_list, *module = true; } + else if (!strcmp(k, "llvm")) { + if (v) + fprintf(stderr, "Compile flag \"%s\" takes no value, ignoring\n", k); + + *llvm = true; + } + else if (!strcmp(k, "target")) { + if (!v) + fprintf(stderr, "Compile flag \"%s\" requires a value, ignoring\n", k); + else + native->triple = v; + } + else if (!strcmp(k, "debug")) { + if (v) + fprintf(stderr, "Compile flag \"%s\" takes no value, ignoring\n", k); + + native->debug = true; + } + else if (!strcmp(k, "map")) { + if (!v) + fprintf(stderr, "Compile flag \"%s\" requires a value, ignoring\n", k); + else + native->map = v; + } + else if (!strcmp(k, "embed") || !strcmp(k, "embed-template")) { + if (!v) { + fprintf(stderr, "Compile flag \"%s\" requires a value, ignoring\n", k); + continue; + } + + /* [logical=]path */ + char *path = strchr(v, '='); + + if (path) + *path++ = 0; + + uc_vector_push(native->embeds, { + .path = path ? path : v, + .logical = path ? v : NULL, + .template = !strcmp(k, "embed-template") + }); + } else { fprintf(stderr, "Unrecognized -c flag \"%s\", ignoring\n", k); } @@ -472,7 +555,9 @@ int main(int argc, char **argv) { const char *optspec = POSIXLY_CORRECT_FLAG "he:p:tg:ST::RD:F:U:l:L:c::o:sx::X::"; - bool strip = false, print_result = false; + bool strip = false, print_result = false, llvm = false; + uc_llvm_embeds_t embeds = { 0 }; + uc_llvm_options_t native = { .embeds = &embeds }; debug_mode_t debug = DEBUG_MODE_NONE; char *breakpoint = NULL; char *interp = "/usr/bin/env ucode"; @@ -534,6 +619,7 @@ main(int argc, char **argv) uc_search_path_init(&config.module_search_path); argv0 = argv[optind]; + native.script_name = argv0; optind = 1; uc_vm_init(&vm, &config); @@ -604,7 +690,7 @@ main(int argc, char **argv) case 'c': outfile = "./uc.out"; parse_compile_flags(optarg, &interp, &config.force_dynlink_list, - &config.compile_module); + &config.compile_module, &llvm, &native); break; case 's': @@ -679,11 +765,14 @@ main(int argc, char **argv) .autoprint = print_result, .debug = debug, .breakpoint = breakpoint, + .llvm = llvm, + .native = &native, })); out: uc_search_path_free(&config.module_search_path); uc_vector_clear(&config.force_dynlink_list); + uc_vector_clear(&embeds); uc_source_put(source); diff --git a/native.c b/native.c new file mode 100644 index 00000000..6ee61f06 --- /dev/null +++ b/native.c @@ -0,0 +1,868 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* Runtime of native programs: frames, calls, exceptions and the setup of the + * program from the tables that the LLVM backend emits. */ + +#include +#include +#include +#include + +#include "ucode/internal/native.h" +#include "ucode/internal/program.h" +#include "ucode/internal/vallist.h" +#include "ucode/internal/chunk.h" +#include "ucode/internal/source.h" +#include "ucode/internal/vmcore.h" +#include "ucode/internal/lib.h" +#include "ucode/internal/cli.h" +#include "ucode/source.h" +#include "ucode/vm.h" +#include "ucode/lib.h" + +static const uc_native_program_t *uc_native_running; +static uc_native_frame_t *uc_native_frames; + +UC_NATIVE_INLINE uc_value_t ** +uc_native_slot(uc_native_frame_t *frame, size_t slot) +{ + if (frame->cells[slot]) + return &frame->cells[slot]->value; + + return &frame->slots[slot]; +} + +UC_NATIVE_INLINE void +uc_native_slot_set(uc_native_frame_t *frame, size_t slot, uc_value_t *value) +{ + uc_value_t **p = uc_native_slot(frame, slot); + uc_value_t *old = *p; + + *p = value; + + if (ucv_is_counted(old)) + ucv_put(old); +} + +UC_NATIVE_INLINE uc_value_t * +uc_native_slot_take(uc_native_frame_t *frame, size_t slot) +{ + uc_value_t **p = uc_native_slot(frame, slot); + uc_value_t *v = *p; + + *p = NULL; + + return v; +} + +UC_NATIVE_INLINE void +uc_native_slot_clear(uc_native_frame_t *frame, size_t slot) +{ + if (frame->cells[slot]) { + ucv_put(&frame->cells[slot]->header); + frame->cells[slot] = NULL; + } + + if (ucv_is_counted(frame->slots[slot])) + ucv_put(frame->slots[slot]); + + frame->slots[slot] = NULL; +} + +/* Returns the cell that a closure shares with the frame for the slot. The + * first capture moves the value of the slot into a closed cell; later + * captures of the same slot get the same cell until the slot is cleared. */ +uc_upvalref_t * +uc_native_slot_capture(uc_native_frame_t *frame, size_t slot) +{ + uc_upvalref_t *cell = frame->cells[slot]; + + if (!cell) { + cell = (uc_upvalref_t *)ucv_upvalref_new(slot); + cell->closed = true; + cell->value = frame->slots[slot]; + frame->slots[slot] = NULL; + frame->cells[slot] = cell; + frame->captured = true; + } + + return cell; +} + +UC_NATIVE_INLINE void +uc_native_pc_set(uc_vm_t *vm, uint32_t pc) +{ + uc_vector_last(&vm->callframes)->ip = (uint8_t *)(uintptr_t)pc; +} + +static void +uc_native_gc_mark(uc_vm_t *vm, void (*mark)(uc_value_t *)) +{ + uc_native_frame_t *frame; + size_t i; + + for (frame = uc_native_frames; frame; frame = frame->prev) { + if (frame->vm != vm) + continue; + + for (i = 0; frame->slots && i < frame->nslots; i++) { + mark(frame->slots[i]); + + if (frame->cells[i]) + mark(&frame->cells[i]->header); + } + + for (i = 0; i < frame->nargs; i++) + mark(frame->args[i]); + + mark(&frame->kept->header); + mark(frame->result); + mark(frame->tail.fn); + mark(frame->tail.ctx); + + for (i = 0; i < frame->tail.nargs; i++) + mark(frame->tail.args[i]); + } +} + +static void +uc_native_args_put(uc_value_t **args, size_t nargs) +{ + for (size_t i = 0; i < nargs; i++) { + ucv_put(args[i]); + args[i] = NULL; + } +} + +static void +uc_native_args_free(uc_value_t **args, size_t nargs) +{ + uc_native_args_put(args, nargs); + free(args); +} + +/* Binds the arguments to the slots of the function as uc_vm_call_function() + * binds them to the stack: missing arguments are null, a vararg function + * gets the excess arguments as an array in its last parameter, excess + * arguments of other functions are dropped. */ +void +uc_native_enter(uc_native_frame_t *frame, uc_value_t **slots, uc_upvalref_t **cells, + uint32_t nslots) +{ + uc_vm_t *vm = frame->vm; + uc_function_t *function = frame->closure->function; + size_t nparams = function->nargs, i, fixed; + uc_value_t *rest; + + frame->slots = slots; + frame->cells = cells; + frame->nslots = nslots; + slots[0] = ucv_get(&frame->closure->header); + + fixed = function->vararg ? nparams - 1 : nparams; + + for (i = 0; i < fixed && i < frame->nargs; i++) { + slots[1 + i] = frame->args[i]; + frame->args[i] = NULL; + } + + if (function->vararg) { + rest = ucv_array_new_length(vm, frame->nargs > fixed ? frame->nargs - fixed : 0); + + for (i = fixed; i < frame->nargs; i++) { + ucv_array_push(rest, frame->args[i]); + frame->args[i] = NULL; + } + + slots[nparams] = rest; + } +} + +void +uc_native_leave(uc_native_frame_t *frame) +{ + for (size_t i = 0; frame->captured && i < frame->nslots; i++) + uc_native_slot_clear(frame, i); + + for (size_t i = 0; !frame->captured && i < frame->nslots; i++) + if (ucv_is_counted(frame->slots[i])) + ucv_put(frame->slots[i]); + + frame->slots = NULL; + frame->nslots = 0; +} + +/* Unwinds the operand stack of the frame to the slot of the exception + * range and puts the exception object there, as uc_vm_handle_exception() + * does. An exit is never caught. */ +bool +uc_native_catch(uc_native_frame_t *frame, uint32_t slot) +{ + uc_vm_t *vm = frame->vm; + + if (vm->exception.type == EXCEPTION_EXIT) + return false; + + for (size_t i = slot; i < frame->nslots; i++) + uc_native_slot_clear(frame, i); + + frame->slots[slot] = uc_vm_exception_object(vm); + uc_vm_clear_exception(vm); + + return true; +} + +bool +uc_native_safepoint(uc_native_frame_t *frame, uint32_t pc) +{ + uc_native_pc_set(frame->vm, pc); + + return uc_native_signals_dispatch(frame->vm); +} + +static uc_native_function_info_t * +uc_native_function_info(uc_function_t *function) +{ + return function->native; +} + +/* Runs a native closure, and the closures it tail calls, in one call frame + * that it pushes as uc_vm_call_function() does, so that the stack trace, + * `this` lookups of callbacks and the recursion limit see it. Takes the + * references to `ctx` and to the arguments, but not the array that holds the + * arguments, which the caller keeps. */ +static uc_value_t * +uc_native_invoke_run(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool mcall, + uc_value_t **args, size_t nargs, bool keep) +{ + uc_native_frame_t frame = { + .prev = uc_native_frames, + .vm = vm, + .closure = closure, + .kept = closure, + .ctx = ctx, + .mcall = mcall, + .args = args, + .nargs = nargs + }; + + uc_native_function_info_t *info; + uc_callframe_t *cf; + uc_value_t *fn; + bool owned = false; + + if (vm->callframes.count >= 1000) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Too much recursion"); + ucv_put(ctx); + uc_native_args_put(args, nargs); + + return NULL; + } + + ucv_get(&closure->header); + uc_native_frames = &frame; + + uc_vector_push(&vm->callframes, { + .stackframe = vm->stack.count, + .closure = closure, + .ctx = ctx, + .ip = closure->function->chunk.entries, + .mcall = mcall, + .strict = closure->function->strict + }); + + uc_vm_run_begin(closure, keep); + + while (true) { + info = uc_native_function_info(frame.closure->function); + info->entry(vm, &frame); + + /* the arguments of a tail call are in an array of the frame */ + if (owned) + uc_native_args_free(frame.args, frame.nargs); + else + uc_native_args_put(frame.args, frame.nargs); + + frame.args = NULL; + frame.nargs = 0; + + if (!frame.tail.pending || vm->exception.type != EXCEPTION_NONE) + break; + + /* the request holds a native closure; C functions run at once */ + fn = frame.tail.fn; + frame.tail.pending = false; + frame.tail.fn = NULL; + + uc_vm_run_end(frame.closure); + ucv_put(&frame.closure->header); + + frame.closure = ucv_as_closure(fn); + frame.ctx = frame.tail.ctx; + frame.mcall = frame.tail.mcall; + frame.args = frame.tail.args; + frame.nargs = frame.tail.nargs; + owned = true; + frame.tail.ctx = NULL; + frame.tail.args = NULL; + frame.tail.nargs = 0; + + cf = uc_vector_last(&vm->callframes); + ucv_put(cf->ctx); + cf->closure = frame.closure; + cf->ctx = frame.ctx; + cf->mcall = frame.mcall; + cf->strict = frame.closure->function->strict; + cf->ip = frame.closure->function->chunk.entries; + + if (cf->tco < 0xffff) + cf->tco++; + + uc_vm_run_begin(frame.closure, false); + } + + ucv_put(frame.tail.fn); + ucv_put(frame.tail.ctx); + uc_native_args_free(frame.tail.args, frame.tail.nargs); + + uc_vm_run_end(frame.closure); + cf = uc_vector_pop(&vm->callframes); + ucv_put(cf->ctx); + ucv_put(&frame.closure->header); + uc_native_frames = frame.prev; + + if (vm->exception.type != EXCEPTION_NONE) { + ucv_put(frame.result); + + return NULL; + } + + return frame.result; +} + +uc_value_t * +uc_native_invoke(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool mcall, + uc_value_t **args, size_t nargs) +{ + return uc_native_invoke_run(vm, closure, ctx, mcall, args, nargs, false); +} + +/* Called by uc_vm_call() for a native closure, with the callee and its + * arguments on the VM stack and the receiver below them for a method call. */ +static void +uc_native_vm_call(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool mcall, + size_t nargs) +{ + uc_value_t **args = nargs ? xalloc(nargs * sizeof(*args)) : NULL; + uc_value_t *res; + size_t i; + + for (i = nargs; i > 0; i--) + args[i - 1] = uc_vm_stack_pop(vm); + + ucv_put(uc_vm_stack_pop(vm)); + + if (mcall) + ucv_put(uc_vm_stack_pop(vm)); + + res = uc_native_invoke(vm, closure, ctx, mcall, args, nargs); + ucv_put(&closure->header); + free(args); + + if (vm->exception.type == EXCEPTION_NONE) + uc_vm_stack_push(vm, res); + else + ucv_put(res); +} + +static const char * +uc_native_string(uint32_t off) +{ + return (off == UC_NATIVE_NONE) ? NULL : uc_native_running->strings + off; +} + +static uc_value_t * +uc_native_constant_value(const uc_native_constant_t *c) +{ + union { uint64_t u; double d; } bits = { .u = c->bits }; + + switch (c->type) { + case UC_NATIVE_CONST_STRING: + return ucv_string_new_length(uc_native_string(c->str), c->length); + + case UC_NATIVE_CONST_DOUBLE: + return ucv_double_new(bits.d); + + case UC_NATIVE_CONST_INT: + return c->length ? ucv_int64_new((int64_t)c->bits) : ucv_uint64_new(c->bits); + + default: + return NULL; + } +} + +/* Creates the source `s`, named `path` if given, as a loader names the file + * it loads by its path. */ +static uc_source_t * +uc_native_source_new(const uc_native_unit_t *unit, const uc_native_source_t *s, + const char *path) +{ + const char *filename = path ? path : uc_native_string(s->filename); + uc_source_t *src; + char *text; + + if (s->text != UC_NATIVE_NONE) { + text = xalloc(s->textlen + 1); + memcpy(text, uc_native_string(s->text), s->textlen); + src = uc_source_new_buffer(filename, text, s->textlen); + } + else { + src = uc_source_new_path(filename); + } + + uc_source_runpath_set(src, path ? path : uc_native_string(s->runpath)); + + for (uint32_t i = 0; i < s->nlineinfo; i++) + uc_vector_push(&src->lineinfo, unit->bytes[s->lineinfo + i]); + + for (uint32_t i = 0; i < s->nexports; i++) + uc_vector_push(&src->exports, uc_native_constant_value(&unit->exports[s->exports + i])); + + return src; +} + +static void +uc_native_function_setup(uc_function_t *fn, const uc_native_unit_t *unit, + const uc_native_function_t *fd) +{ + const uint32_t *variables = unit->words + fd->variables; + const uint8_t *offsets = unit->bytes + fd->offsets; + uc_chunk_t *chunk = &fn->chunk; + uc_value_t *name; + uint32_t i, slot; + const char *s; + + fn->nargs = fd->nargs; + fn->nupvals = fd->nupvals; + fn->arrow = fd->flags & UC_NATIVE_FN_ARROW; + fn->vararg = fd->flags & UC_NATIVE_FN_VARARG; + fn->strict = fd->flags & UC_NATIVE_FN_STRICT; + fn->module = fd->flags & UC_NATIVE_FN_MODULE; + + for (i = 0; i < fd->noffsets; i++) + uc_vector_push(&chunk->debuginfo.offsets, { + .bytes = offsets[i * 2], + .insns = offsets[i * 2 + 1] + }); + + for (i = 0; i < fd->nvarnames; i++) { + s = uc_native_string(unit->words[fd->varnames + i]); + name = s ? ucv_string_new(s) : NULL; + uc_vallist_add(&chunk->debuginfo.varnames, name); + ucv_put(name); + } + + if (!fd->nvariables) + return; + + /* allocated to size and not as a vector, which would reserve at least + * eight entries per function; nothing adds variables to a function that + * the runtime did not compile */ + chunk->debuginfo.variables.entries = + xcalloc(fd->nvariables, sizeof(chunk->debuginfo.variables.entries[0])); + chunk->debuginfo.variables.count = fd->nvariables; + + for (i = 0; i < fd->nvariables; i++) { + slot = variables[i * 4 + 2]; + + chunk->debuginfo.variables.entries[i] = (uc_varrange_t){ + .from = variables[i * 4], + .to = variables[i * 4 + 1], + .slot = (slot & UC_NATIVE_VAR_UPVAL) + ? (slot & ~UC_NATIVE_VAR_UPVAL) + (size_t)-1 / 2 : slot, + .nameidx = variables[i * 4 + 3] + }; + } +} + +/* Creates a program of the unit, as the interpreter compiles one each time it + * loads a script; `path` names its main source as the loader found it. */ +static uc_program_t * +uc_native_program_new(const uc_native_unit_t *desc, const char *path) +{ + uc_program_t *prog = uc_program_new(); + uc_native_function_info_t *info, *entry = NULL; + uc_function_t *fn; + uc_value_t *v; + uint32_t i; + + for (i = 0; i < desc->nsources; i++) + uc_vector_push(&prog->sources, + uc_native_source_new(desc, &desc->sources[i], i ? NULL : path)); + + for (i = 0; i < desc->nconstants; i++) { + v = uc_native_constant_value(&desc->constants[i]); + uc_vallist_add(&prog->constants, v); + ucv_put(v); + } + + for (i = 0; i < desc->nfunctions; i++) { + fn = uc_program_function_new(prog, uc_native_string(desc->functions[i].name), + prog->sources.entries[desc->functions[i].srcidx], + desc->functions[i].srcpos); + + uc_native_function_setup(fn, desc, &desc->functions[i]); + + if (!entry) { + entry = xalloc(sizeof(*entry) + desc->nfunctions * sizeof(uc_function_t *)); + entry->functions = (uc_function_t **)(entry + 1); + info = entry; + } + else { + info = xalloc(sizeof(*info)); + info->functions = entry->functions; + } + + info->entry = desc->entries[i]; + entry->functions[i] = fn; + fn->native = info; + } + + return prog; +} + +static const uc_native_unit_t * +uc_native_unit_find(const char *path, bool any_mode, bool raw_mode, bool module_mode) +{ + uint32_t flags = (raw_mode ? 0 : UC_NATIVE_UNIT_TEMPLATE) | + (module_mode ? UC_NATIVE_UNIT_MODULE : 0); + const uc_native_unit_t *unit, *found = NULL; + char *key; + + if (!uc_native_running) + return NULL; + + key = uc_path_normalize(path); + + for (uint32_t i = 1; key && !found && i < uc_native_running->nunits; i++) { + unit = &uc_native_running->units[i]; + + if (strcmp(uc_native_string(unit->path), key)) + continue; + + if (any_mode || unit->flags == flags) + found = unit; + } + + free(key); + + return found; +} + +static uc_value_t * +uc_native_unit_closure(uc_vm_t *vm, const char *path, bool raw_mode, bool module_mode, + char **errp) +{ + const uc_native_unit_t *unit = uc_native_unit_find(path, false, raw_mode, module_mode); + uc_program_t *prog; + uc_value_t *closure; + + if (!unit) + return NULL; + + if (unit->error != UC_NATIVE_NONE) { + *errp = xstrdup(uc_native_string(unit->error)); + + return NULL; + } + + prog = uc_native_program_new(unit, path); + closure = uc_program_main(vm, prog); + uc_program_put(prog); + + return closure; +} + +static char * +uc_native_unit_path(const char *path) +{ + return uc_native_unit_find(path, true, false, false) ? uc_path_normalize(path) : NULL; +} + +/* The sources of a native program are known by their logical path, which + * stays valid when the files are absent. Sources that are not files, such as + * "[stdin]", have bracketed names. */ +static char * +uc_native_source_path(const char *runpath) +{ + if (!runpath || *runpath == '[') + return NULL; + + return uc_path_normalize(runpath); +} + +static const uc_loader_units_t uc_native_loader_units = { + .unit_closure = uc_native_unit_closure, + .unit_path = uc_native_unit_path, + .source_path = uc_native_source_path +}; + +/* Returns the logical paths of the embedded scripts for include() (the + * default), render() ("template") or module imports ("module"). */ +static uc_value_t * +uc_native_bundle_files(uc_vm_t *vm, size_t nargs) +{ + uc_value_t *kind = uc_fn_arg(0), *list; + const char *k = ucv_string_get(kind); + uint32_t flags = 0; + + if (kind && ucv_type(kind) != UC_STRING) { + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Passed kind is not a string"); + + return NULL; + } + + if (k && !strcmp(k, "template")) + flags = UC_NATIVE_UNIT_TEMPLATE; + else if (k && !strcmp(k, "module")) + flags = UC_NATIVE_UNIT_MODULE; + else if (k && strcmp(k, "include")) { + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Unknown kind '%s'", k); + + return NULL; + } + + list = ucv_array_new(vm); + + for (uint32_t i = 1; i < uc_native_running->nunits; i++) + if (uc_native_running->units[i].flags == flags) + ucv_array_push(list, ucv_string_new(uc_native_string(uc_native_running->units[i].path))); + + return list; +} + +/* Native program options, accepted before the first program argument. They + * use long names, so that the short options of the program itself pass + * through to ARGV unchanged. */ +static const struct { + const char *name; + char opt; +} uc_native_options[] = { + { "--ucode-define", 'D' }, + { "--ucode-define-file", 'F' }, + { "--ucode-undefine", 'U' }, + { "--ucode-library", 'l' }, + { "--ucode-search-path", 'L' }, + { "--ucode-gc", 'g' }, +}; + +static char +uc_native_option(int argc, char **argv, int i) +{ + if (i + 1 >= argc) + return 0; + + for (size_t j = 0; j < ARRAY_SIZE(uc_native_options); j++) + if (!strcmp(argv[i], uc_native_options[j].name)) + return uc_native_options[j].opt; + + return 0; +} + +static int +uc_native_options_apply(uc_vm_t *vm, int argc, char **argv, uc_value_t *args) +{ + FILE *stdin_unused = stdin; + int i; + char opt; + + for (i = 1; i < argc && (opt = uc_native_option(argc, argv, i)) != 0; i += 2) { + switch (opt) { + case 'D': + if (!uc_cli_define_string(argv[i + 1], uc_vm_scope_get(vm))) + return -1; + break; + + case 'F': + if (!uc_cli_define_file(argv[i + 1], uc_vm_scope_get(vm), &stdin_unused)) + return -1; + break; + + case 'U': + ucv_object_delete(uc_vm_scope_get(vm), argv[i + 1]); + break; + + case 'l': + if (!uc_cli_library_load(argv[i + 1], vm)) + return -1; + break; + + case 'g': + vm->gc_interval = atoi(argv[i + 1]); + break; + } + } + + if (i < argc && !strcmp(argv[i], "--")) + i++; + + for (; i < argc; i++) + ucv_array_push(args, ucv_string_new(argv[i])); + + return 0; +} + +/* Runs the entry of the program and returns the exit status as the ucode CLI + * does: 0, the code of exit(), or -2 after an uncaught exception, which the + * exception handler of the VM reports. */ +static int +uc_native_run(uc_vm_t *vm, uc_program_t *program) +{ + uc_value_t *closure = uc_program_main(vm, program); + uc_value_t *res; + int rv = 0; + + /* the entry closure keeps the exports of the modules that the program + * links, as uc_vm_execute() does */ + res = uc_native_invoke_run(vm, ucv_as_closure(closure), NULL, false, NULL, 0, true); + ucv_put(closure); + ucv_put(res); + + switch (vm->exception.type) { + case EXCEPTION_NONE: + break; + + case EXCEPTION_EXIT: + rv = vm->arg.s32; + break; + + default: + if (vm->exhandler) + vm->exhandler(vm, &vm->exception); + + rv = -2; + break; + } + + return rv; +} + +/* Loads the library that stands in for libucode, see native_abi.c, before any + * binding, so that the dependency of a binding on libucode resolves to it by + * its SONAME; the RUNPATH of the executable leads to it. A libucode that is + * already loaded is the interpreter library and must not serve bindings. */ +static bool +uc_native_abi_load(void) +{ + int saved_errno = errno; + void *h = dlopen(UC_NATIVE_ABI_SONAME, RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL); + + if (!h) + h = dlopen(UC_NATIVE_ABI_SONAME, RTLD_NOW | RTLD_GLOBAL); + + /* failed lookups must not leave an error for the dlerror() of a later + * dlopen() to report, nor change the errno that the program starts with */ + dlerror(); + errno = saved_errno; + + /* without it, a binding that depends on libucode loads the interpreter + * library, if one is installed, beside the runtime of the program */ + if (!h) { + fprintf(stderr, "%s not found, bindings that depend on it load the " + "interpreter library\n", UC_NATIVE_ABI_SONAME); + + return true; + } + + if (dlsym(h, "uc_native_abi_provider")) + return true; + + fprintf(stderr, "%s is loaded, but it is not the library of native programs\n", + UC_NATIVE_ABI_SONAME); + + return false; +} + +int +uc_native_main(int argc, char **argv, const uc_native_program_t *np) +{ + uc_parse_config_t config = { + .raw_mode = !(np->units[0].flags & UC_NATIVE_UNIT_TEMPLATE), + .setup_signal_handlers = true + }; + + uc_program_t *program; + uc_vm_t vm = { 0 }; + uc_value_t *args; + char opt; + int i, rv; + + if (np->version != UC_NATIVE_ABI_VERSION || np->nunits == 0) { + fprintf(stderr, "Native program ABI version %u is not supported, expected %u\n", + np->version, UC_NATIVE_ABI_VERSION); + + return 1; + } + + if (!uc_native_abi_load()) + return 1; + + uc_native_running = np; + uc_vm_native_call_set(uc_native_vm_call); + uc_loader_units_set(&uc_native_loader_units); + + /* the search path is part of the configuration the VM starts with; as in + * the CLI, the given entries come before the default ones */ + for (i = 1; i < argc && (opt = uc_native_option(argc, argv, i)) != 0; i += 2) + if (opt == 'L') + uc_cli_search_path(argv[i + 1], &config); + + uc_search_path_init(&config.module_search_path); + + uc_vm_init(&vm, &config); + uc_thread_context_get()->gc_roots_mark = uc_native_gc_mark; + uc_stdlib_load(uc_vm_scope_get(&vm)); + ucv_object_add(uc_vm_scope_get(&vm), "bundle_files", + ucv_cfunction_new("bundle_files", uc_native_bundle_files)); + + program = uc_native_program_new(&np->units[0], NULL); + args = ucv_array_new(&vm); + ucv_object_add(uc_vm_scope_get(&vm), "ARGV", ucv_get(args)); + + if (np->script_name != UC_NATIVE_NONE) + ucv_object_add(uc_vm_scope_get(&vm), "SCRIPT_NAME", + ucv_string_new(uc_native_string(np->script_name))); + + rv = uc_native_options_apply(&vm, argc, argv, args); + + if (rv == 0) { + if (vm.gc_interval) + uc_vm_gc_start(&vm, vm.gc_interval); + + rv = uc_native_run(&vm, program); + } + else { + rv = 1; + } + + ucv_put(args); + uc_program_put(program); + uc_vm_free(&vm); + uc_search_path_free(&config.module_search_path); + + return rv; +} diff --git a/native_abi.c b/native_abi.c new file mode 100644 index 00000000..8637bb7d --- /dev/null +++ b/native_abi.c @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* A library with the SONAME of libucode but without code. Bindings that are + * linked against libucode depend on that SONAME; in a native program this + * library satisfies the dependency, and the executable provides the API, so + * that loading such a binding does not load the interpreter. */ + +const unsigned uc_native_abi_provider = 1; diff --git a/native_insn.c b/native_insn.c new file mode 100644 index 00000000..243a5fe0 --- /dev/null +++ b/native_insn.c @@ -0,0 +1,1354 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* The instructions of native functions. Each helper does what the handler of + * its instruction in vm.c does, through the same vmcore.c functions, on the + * slots of the native frame instead of the VM stack: the operand stack of the + * instruction starts at slot `d`, the depth before the instruction. Helpers + * returning bool report whether the instruction completed without raising an + * exception. */ + +#include + +#include "ucode/internal/native.h" +#include "ucode/internal/vmcore.h" +#include "ucode/internal/program.h" + +#define SLOT(i) (*uc_native_slot(f, (i))) + +static uc_value_t *uc_native_arith(uc_vm_t *vm, uc_vm_insn_t operation, + uc_value_t *value, uc_value_t *operand); + +UC_NATIVE_INLINE static void +uc_native_store(uc_vm_t *vm, uc_native_frame_t *f, size_t slot, uc_value_t *v) +{ + /* only an upvalue reference resolves, and only counted values are one */ + if (ucv_is_counted(v)) + v = uc_vm_resolve_upval(vm, v); + + uc_native_slot_set(f, slot, v); +} + +/* ucv_get() without a call for the values that have no reference count */ +UC_NATIVE_INLINE static uc_value_t * +uc_native_ref(uc_value_t *v) +{ + return ucv_is_counted(v) ? ucv_get(v) : v; +} + +UC_NATIVE_INLINE static uc_value_t * +uc_native_constant(uc_native_frame_t *f, size_t idx) +{ + return uc_program_get_constant(f->closure->function->program, idx); +} + +UC_NATIVE_INLINE static bool +uc_native_ok(uc_vm_t *vm) +{ + return vm->exception.type == EXCEPTION_NONE; +} + +UC_NATIVE_INLINE static bool +uc_native_strict(uc_native_frame_t *f) +{ + return f->closure->function->strict; +} + +UC_NATIVE_INLINE static uc_upvalref_t * +uc_native_upval(uc_native_frame_t *f, size_t idx) +{ + return f->closure->upvals[idx]; +} + +UC_NATIVE_INLINE static uc_value_t * +uc_native_upval_value(uc_vm_t *vm, uc_upvalref_t *ref) +{ + return ref->closed ? ref->value : vm->stack.entries[ref->slot]; +} + +UC_NATIVE_INLINE static void +uc_native_upval_store(uc_vm_t *vm, uc_upvalref_t *ref, uc_value_t *val) +{ + if (ref->closed) { + ucv_put(ref->value); + ref->value = val; + } + else { + uc_vm_stack_set(vm, ref->slot, val); + } +} + +/* The bodies of the instructions that the fused helpers at the end combine; + * the helpers of the single instructions call them as well */ + +UC_NATIVE_INLINE static void +uc_native_insn_load(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + uc_native_store(vm, f, d, uc_native_constant(f, arg)); +} + +UC_NATIVE_INLINE static void +uc_native_insn_lloc(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + uc_native_store(vm, f, d, uc_native_ref(SLOT(arg))); +} + +UC_NATIVE_INLINE static void +uc_native_insn_lupv(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + uc_native_store(vm, f, d, uc_native_ref(uc_native_upval_value(vm, uc_native_upval(f, arg)))); +} + +UC_NATIVE_INLINE static bool +uc_native_insn_lval(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d) +{ + uc_value_t *res; + bool ok; + + uc_native_pc_set(vm, pc); + ok = uc_vm_property_get(vm, SLOT(d - 2), SLOT(d - 1), &res); + uc_native_slot_clear(f, d - 1); + uc_native_slot_clear(f, d - 2); + + if (ok) + uc_native_store(vm, f, d - 2, res); + + return uc_native_ok(vm); +} + +UC_NATIVE_INLINE static bool +uc_native_insn_pval(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d) +{ + uc_value_t *res; + bool ok; + + uc_native_pc_set(vm, pc); + ok = uc_vm_property_get(vm, SLOT(d - 2), SLOT(d - 1), &res); + uc_native_slot_clear(f, d - 1); + + if (ok) + uc_native_store(vm, f, d - 1, res); + + return uc_native_ok(vm); +} + +UC_NATIVE_INLINE static void +uc_native_insn_sloc(uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + uc_native_slot_set(f, arg, uc_native_ref(SLOT(d - 1))); +} + +UC_NATIVE_INLINE static void +uc_native_insn_pop(uc_vm_t *vm, uc_native_frame_t *f, int32_t d) +{ + uc_native_slot_clear(f, d - 1); + uc_vm_gc_step(vm); +} + +UC_NATIVE_INLINE static bool +uc_native_insn_jmpz(uc_native_frame_t *f, int32_t d) +{ + uc_value_t *v = uc_native_slot_take(f, d - 1); + bool falsy; + + /* null and the booleans that conditions mostly test need no call */ + if (v == NULL) + return true; + + if (((uintptr_t)v & 3) == UC_BOOLEAN) + return !(((uintptr_t)v >> 2) & 1); + + falsy = !ucv_is_truish(v); + ucv_put(v); + + return falsy; +} + +void +uc_native_load(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_insn_load(vm, f, d, arg); +} + +void +uc_native_load8(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d, ucv_uint64_new(arg)); +} + +void +uc_native_load16(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d, ucv_uint64_new(arg)); +} + +void +uc_native_load32(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d, ucv_uint64_new(arg)); +} + +void +uc_native_lthis(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d, uc_native_ref(f->ctx)); +} + +bool +uc_native_lrexp(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *re, *jstr = uc_native_constant(f, arg); + bool loaded; + + uc_native_pc_set(vm, pc); + loaded = uc_vm_regexp_load(vm, jstr, &re); + ucv_put(jstr); + + if (loaded) + uc_native_store(vm, f, d, re); + + return uc_native_ok(vm); +} + +void +uc_native_lnull(UC_NATIVE_INSN_ARGS) +{ + uc_native_slot_set(f, d, NULL); +} + +void +uc_native_ltrue(UC_NATIVE_INSN_ARGS) +{ + uc_native_slot_set(f, d, ucv_boolean_new(true)); +} + +void +uc_native_lfalse(UC_NATIVE_INSN_ARGS) +{ + uc_native_slot_set(f, d, ucv_boolean_new(false)); +} + +void +uc_native_lloc(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_insn_lloc(vm, f, d, arg); +} + +void +uc_native_lupv(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_insn_lupv(vm, f, d, arg); +} + +bool +uc_native_lvar(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *name = uc_native_constant(f, arg); + uc_value_t *val; + + uc_native_pc_set(vm, pc); + val = uc_vm_global_get(vm, name, uc_native_strict(f)); + ucv_put(name); + uc_native_store(vm, f, d, val); + + return uc_native_ok(vm); +} + +bool +uc_native_lval(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + return uc_native_insn_lval(vm, f, pc, d); +} + +bool +uc_native_pval(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + return uc_native_insn_pval(vm, f, pc, d); +} + +static void +uc_native_closure(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg, + const int32_t *x, bool arrow) +{ + uc_native_function_info_t *info = f->closure->function->native; + uc_function_t *function = info->functions[arg - 1]; + uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, function, arrow); + uc_upvalref_t *ref; + + uc_native_slot_set(f, d, &closure->header); + + if (function->module) + return; + + for (size_t i = 0; i < function->nupvals; i++) { + if (x[i] < 0) + ref = uc_native_slot_capture(f, -(x[i] + 1)); + else + ref = uc_native_upval(f, x[i]); + + closure->upvals[i] = ref; + ucv_get(&ref->header); + } +} + +void +uc_native_clfn(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_closure(vm, f, d, arg, x, false); +} + +void +uc_native_arfn(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_closure(vm, f, d, arg, x, true); +} + +void +uc_native_sloc(UC_NATIVE_INSN_ARGS) +{ + uc_native_insn_sloc(f, d, arg); +} + +void +uc_native_supv(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_upval_store(vm, uc_native_upval(f, arg), uc_native_ref(SLOT(d - 1))); +} + +bool +uc_native_svar(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *name = uc_native_constant(f, arg); + bool stored; + + uc_native_pc_set(vm, pc); + stored = uc_vm_global_set(vm, name, SLOT(d - 1), uc_native_strict(f)); + ucv_put(name); + + if (!stored) + uc_native_slot_clear(f, d - 1); + + return uc_native_ok(vm); +} + +bool +uc_native_sval(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *res; + bool ok; + + uc_native_pc_set(vm, pc); + ok = uc_vm_property_set(vm, SLOT(d - 3), SLOT(d - 2), SLOT(d - 1), &res); + uc_native_slot_clear(f, d - 1); + uc_native_slot_clear(f, d - 2); + uc_native_slot_clear(f, d - 3); + + if (ok) + uc_native_store(vm, f, d - 3, res); + + return uc_native_ok(vm); +} + +bool +uc_native_uloc(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + size_t slot = arg & 0x00ffffff; + uc_value_t *val; + + uc_native_pc_set(vm, pc); + val = uc_native_arith(vm, arg >> 24, SLOT(slot), SLOT(d - 1)); + uc_native_store(vm, f, d - 1, val); + uc_native_slot_set(f, slot, uc_native_ref(SLOT(d - 1))); + + return uc_native_ok(vm); +} + +bool +uc_native_uupv(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_upvalref_t *ref = uc_native_upval(f, arg & 0x00ffffff); + uc_value_t *val; + + uc_native_pc_set(vm, pc); + val = uc_native_arith(vm, arg >> 24, uc_native_upval_value(vm, ref), SLOT(d - 1)); + uc_native_store(vm, f, d - 1, val); + uc_native_upval_store(vm, ref, uc_native_ref(SLOT(d - 1))); + + return uc_native_ok(vm); +} + +bool +uc_native_uvar(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *name = uc_native_constant(f, arg & 0x00ffffff); + uc_value_t *val; + bool ok; + + uc_native_pc_set(vm, pc); + ok = uc_vm_global_update(vm, arg >> 24, name, SLOT(d - 1), uc_native_strict(f), &val); + ucv_put(name); + uc_native_slot_clear(f, d - 1); + + if (ok) + uc_native_store(vm, f, d - 1, val); + + return uc_native_ok(vm); +} + +bool +uc_native_uval(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *res; + bool ok; + + uc_native_pc_set(vm, pc); + ok = uc_vm_property_update(vm, arg, SLOT(d - 3), SLOT(d - 2), SLOT(d - 1), &res); + uc_native_slot_clear(f, d - 1); + uc_native_slot_clear(f, d - 2); + uc_native_slot_clear(f, d - 3); + + if (ok) + uc_native_store(vm, f, d - 3, res); + + return uc_native_ok(vm); +} + +void +uc_native_narr(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_slot_set(f, d, ucv_array_new_length(vm, arg)); +} + +void +uc_native_parr(UC_NATIVE_INSN_ARGS) +{ + uc_value_t *arr = SLOT(d - arg - 1), *v; + + for (uint32_t i = 0; i < arg; i++) { + v = uc_native_slot_take(f, d - arg + i); + uc_vm_array_append(arr, &v, 1); + } +} + +bool +uc_native_marr(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_pc_set(vm, pc); + + return uc_vm_array_spread(vm, SLOT(d - 2), uc_native_slot_take(f, d - 1)); +} + +void +uc_native_nobj(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_slot_set(f, d, uc_vm_object_new(vm, arg / 2)); +} + +bool +uc_native_sobj(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *obj = SLOT(d - arg - 1); + uint32_t i; + + uc_native_pc_set(vm, pc); + + for (i = 0; i < arg; i += 2) + uc_vm_object_init_key(vm, obj, SLOT(d - arg + i), SLOT(d - arg + i + 1)); + + for (i = 0; i < arg; i++) + uc_native_slot_clear(f, d - arg + i); + + return uc_native_ok(vm); +} + +bool +uc_native_mobj(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_pc_set(vm, pc); + + return uc_vm_object_spread(vm, SLOT(d - 2), uc_native_slot_take(f, d - 1)); +} + +/* Computes with integers that the pointers hold without a call; the result + * equals that of uc_vm_value_arith(), which handles everything else, + * including products that wrap and division by zero. */ +static uc_value_t * +uc_native_arith(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) +{ + int64_t n1, n2, n; + + if (!ucv_is_small_integer(value) || !ucv_is_small_integer(operand)) + return uc_vm_value_arith(vm, operation, value, operand); + + n1 = ucv_small_integer_get(value); + n2 = ucv_small_integer_get(operand); + + switch (operation) { + case I_ADD: + case I_PLUS: + n = n1 + n2; + break; + + case I_SUB: + case I_MINUS: + n = n1 - n2; + break; + + case I_MUL: + if (__builtin_mul_overflow(n1, n2, &n)) + return uc_vm_value_arith(vm, operation, value, operand); + + break; + + case I_DIV: + if (n2 == 0) + return uc_vm_value_arith(vm, operation, value, operand); + + n = n1 / n2; + break; + + case I_MOD: + if (n2 == 0) + return uc_vm_value_arith(vm, operation, value, operand); + + n = n1 % n2; + break; + + default: + return uc_vm_value_arith(vm, operation, value, operand); + } + + return (n < 0) ? ucv_int64_new(n) : ucv_uint64_new(n); +} + +/* Compares integers that the pointers hold without a call; the result equals + * that of uc_vm_value_compare() and uc_vm_value_equality(). */ +static bool +uc_native_compare(uc_vm_insn_t operation, uc_value_t *v1, uc_value_t *v2) +{ + int64_t n1, n2; + + if (!ucv_is_small_integer(v1) || !ucv_is_small_integer(v2)) + return (operation == I_EQS || operation == I_NES) + ? uc_vm_value_equality(operation, v1, v2) + : uc_vm_value_compare(operation, v1, v2); + + n1 = ucv_small_integer_get(v1); + n2 = ucv_small_integer_get(v2); + + switch (operation) { + case I_LT: return n1 < n2; + case I_LE: return n1 <= n2; + case I_GT: return n1 > n2; + case I_GE: return n1 >= n2; + case I_EQ: + case I_EQS: return n1 == n2; + default: return n1 != n2; + } +} + +/* The part of ADD and SUB that LLVM inlines at the instruction: integers that + * the pointers hold, whose sum fits; anything else returns false. Neither + * operand nor the result has a reference count. */ +UC_NATIVE_INLINE static bool +uc_native_arith_inline(uc_native_frame_t *f, int32_t d, uc_vm_insn_t insn) +{ + uc_value_t *v1 = SLOT(d - 2), *v2 = SLOT(d - 1); + int64_t n; + + if (!ucv_is_small_integer(v1) || !ucv_is_small_integer(v2)) + return false; + + if (insn == I_ADD) + n = ucv_small_integer_get(v1) + ucv_small_integer_get(v2); + else + n = ucv_small_integer_get(v1) - ucv_small_integer_get(v2); + + SLOT(d - 2) = (n < 0) ? ucv_int64_new(n) : ucv_uint64_new(n); + SLOT(d - 1) = NULL; + + return true; +} + +/* The part of a comparison that LLVM inlines at the instruction, as + * uc_native_arith_inline() */ +UC_NATIVE_INLINE static bool +uc_native_compare_inline(uc_native_frame_t *f, int32_t d, uc_vm_insn_t insn) +{ + uc_value_t *v1 = SLOT(d - 2), *v2 = SLOT(d - 1); + + if (!ucv_is_small_integer(v1) || !ucv_is_small_integer(v2)) + return false; + + SLOT(d - 2) = ucv_boolean_new(uc_native_compare(insn, v1, v2)); + SLOT(d - 1) = NULL; + + return true; +} + +__attribute__((noinline)) static bool +uc_native_binary(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d, + uc_value_t *(*op)(uc_vm_t *, uc_vm_insn_t, uc_value_t *, uc_value_t *), + uc_vm_insn_t insn) +{ + uc_value_t *rv; + + uc_native_pc_set(vm, pc); + rv = op(vm, insn, SLOT(d - 2), SLOT(d - 1)); + uc_native_slot_clear(f, d - 1); + uc_native_store(vm, f, d - 2, rv); + + return uc_native_ok(vm); +} + +static bool +uc_native_predicate(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d, bool res) +{ + uc_native_slot_clear(f, d - 1); + uc_native_slot_set(f, d - 2, ucv_boolean_new(res)); + + return uc_native_ok(vm); +} + +__attribute__((noinline)) static bool +uc_native_comparison(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d, + uc_vm_insn_t insn) +{ + uc_native_pc_set(vm, pc); + + return uc_native_predicate(vm, f, pc, d, uc_native_compare(insn, SLOT(d - 2), SLOT(d - 1))); +} + +#define UC_NATIVE_ARITH(name, insn) \ + bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ + { return uc_native_binary(f->vm, f, pc, d, uc_native_arith, insn); } + +#define UC_NATIVE_ARITH_INLINE(name, insn) \ + UC_NATIVE_INLINE bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ + { return uc_native_arith_inline(f, d, insn) || \ + uc_native_binary(f->vm, f, pc, d, uc_native_arith, insn); } + +#define UC_NATIVE_BITOP(name, insn) \ + bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ + { return uc_native_binary(f->vm, f, pc, d, uc_vm_value_bitop, insn); } + +#define UC_NATIVE_COMPARE(name, insn) \ + bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ + { return uc_native_comparison(f->vm, f, pc, d, insn); } + +/* the order comparisons of loop conditions are inlined; equality is mostly + * applied to other values than integers, where inlining only costs space */ +#define UC_NATIVE_COMPARE_INLINE(name, insn) \ + UC_NATIVE_INLINE bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ + { return uc_native_compare_inline(f, d, insn) || \ + uc_native_comparison(f->vm, f, pc, d, insn); } + +UC_NATIVE_ARITH_INLINE(add, I_ADD) +UC_NATIVE_ARITH_INLINE(sub, I_SUB) +UC_NATIVE_ARITH(mul, I_MUL) +UC_NATIVE_ARITH(div, I_DIV) +UC_NATIVE_ARITH(mod, I_MOD) +UC_NATIVE_ARITH(exp, I_EXP) +UC_NATIVE_BITOP(lshift, I_LSHIFT) +UC_NATIVE_BITOP(rshift, I_RSHIFT) +UC_NATIVE_BITOP(band, I_BAND) +UC_NATIVE_BITOP(bxor, I_BXOR) +UC_NATIVE_BITOP(bor, I_BOR) +UC_NATIVE_COMPARE(eqs, I_EQS) +UC_NATIVE_COMPARE(nes, I_NES) +UC_NATIVE_COMPARE(eq, I_EQ) +UC_NATIVE_COMPARE(ne, I_NE) +UC_NATIVE_COMPARE_INLINE(lt, I_LT) +UC_NATIVE_COMPARE_INLINE(le, I_LE) +UC_NATIVE_COMPARE_INLINE(gt, I_GT) +UC_NATIVE_COMPARE_INLINE(ge, I_GE) + +void +uc_native_in(UC_NATIVE_INSN_ARGS) +{ + bool res = uc_vm_value_in(SLOT(d - 2), SLOT(d - 1)); + + uc_native_slot_clear(f, d - 1); + uc_native_slot_set(f, d - 2, ucv_boolean_new(res)); +} + +void +uc_native_not(UC_NATIVE_INSN_ARGS) +{ + uc_native_slot_set(f, d - 1, ucv_boolean_new(uc_vm_value_not(SLOT(d - 1)))); +} + +void +uc_native_compl(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d - 1, uc_vm_value_complement(SLOT(d - 1))); +} + +void +uc_native_plus(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d - 1, uc_vm_value_unary(I_PLUS, SLOT(d - 1))); +} + +void +uc_native_minus(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d - 1, uc_vm_value_unary(I_MINUS, SLOT(d - 1))); +} + +/* Returns whether the branch is taken. */ +bool +uc_native_jmpz(UC_NATIVE_INSN_ARGS) +{ + return uc_native_insn_jmpz(f, d); +} + +/* Returns whether the branch is taken; it then drops the values above the + * guarded one and leaves null in its place, as uc_vm_insn_jmpnt() does. */ +bool +uc_native_jmpnt(UC_NATIVE_INSN_ARGS) +{ + uint16_t types = (arg >> 16) & 0x1fff; + uint8_t depth = (arg >> 29) & 0x7; + + if (types & (1u << ucv_type(SLOT(d - 1)))) + return false; + + for (int32_t i = 0; i <= depth; i++) + uc_native_slot_clear(f, d - 1 - i); + + return true; +} + +void +uc_native_copy(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_store(vm, f, d, uc_native_ref(SLOT(d - 1 - arg))); +} + +void +uc_native_pop(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_insn_pop(vm, f, d); +} + +void +uc_native_cupv(UC_NATIVE_INSN_ARGS) +{ + uc_native_slot_clear(f, d - 1); +} + +void +uc_native_return(UC_NATIVE_INSN_ARGS) +{ + ucv_put(f->result); + f->result = uc_native_slot_take(f, d - 1); +} + +/* Pushes the callee of a CALL, its receiver for a method call and its + * arguments, expanding spread arguments, onto the VM stack in the layout that + * uc_vm_call_function() builds. Returns the number of arguments, or -1 if a + * spread argument is not an array. */ +static ssize_t +uc_native_call_push(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg, + const int32_t *x, size_t *stackoff) +{ + size_t nargs = arg & 0xffff, nspreads = (arg >> 16) & 0x7fff, i, slot; + bool mcall = arg & 0x80000000; + int32_t fi = d - nargs - 1; + + if (mcall) + uc_vm_stack_push(vm, ucv_get(SLOT(fi - 1))); + + *stackoff = vm->stack.count; + uc_vm_stack_push(vm, ucv_get(SLOT(fi))); + + for (i = 0; i < nargs; i++) + uc_vm_stack_push(vm, ucv_get(SLOT(fi + 1 + i))); + + if (!nspreads) + return nargs; + + for (i = 0, slot = nargs; i < nspreads; i++) { + if (!uc_vm_spread_push(vm, *stackoff + 1, nargs, slot, x[i])) + return -1; + + slot = x[i]; + } + + uc_vm_spread_finish(vm, *stackoff + 1, nargs, slot); + + return vm->stack.count - *stackoff - 1; +} + +static void +uc_native_stack_unwind(uc_vm_t *vm, size_t count) +{ + while (vm->stack.count > count) + ucv_put(uc_vm_stack_pop(vm)); +} + +static uc_value_t ** +uc_native_stack_args(uc_vm_t *vm, size_t nargs) +{ + uc_value_t **args = nargs ? xalloc(nargs * sizeof(*args)) : NULL; + + for (size_t i = nargs; i > 0; i--) + args[i - 1] = uc_vm_stack_pop(vm); + + return args; +} + +/* Calls the value on the VM stack at `stackoff`, with the receiver below it + * for a method call and `nargs` arguments above it, as uc_vm_call_function() + * does, and leaves the stack as it was before the receiver was pushed. With + * `tail`, a native closure is not called but stored as the tail call request + * of the frame. */ +static bool +uc_native_call_stack(uc_vm_t *vm, uc_native_frame_t *f, uc_value_t *ctx, bool mcall, + size_t stackoff, size_t nargs, bool tail, uc_value_t **res) +{ + size_t base = stackoff - mcall; + uc_value_t *fno = ucv_get(vm->stack.entries[stackoff]); + uc_closure_t *closure; + uc_value_t **args; + + *res = NULL; + + if (!uc_vm_call_depth_check(vm, tail)) { + ucv_put(ctx); + ucv_put(fno); + uc_native_stack_unwind(vm, base); + + return false; + } + + if (!uc_vm_call_dispatch(vm, &ctx, &fno, mcall, nargs)) { + uc_native_stack_unwind(vm, base); + + return false; + } + + /* a C function ran, and its frame pop left the result in place of the + * callee and the receiver */ + if (!fno) { + if (vm->stack.count > base) + *res = uc_vm_stack_pop(vm); + + uc_native_stack_unwind(vm, base); + + return uc_native_ok(vm); + } + + closure = ucv_as_closure(fno); + args = uc_native_stack_args(vm, nargs); + uc_native_stack_unwind(vm, base); + + if (!closure->function->native) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "bytecode closures cannot execute in the native runtime"); + ucv_put(ctx); + ucv_put(fno); + + for (size_t i = 0; i < nargs; i++) + ucv_put(args[i]); + + free(args); + + return false; + } + + if (tail) { + f->tail.fn = fno; + f->tail.ctx = ctx; + f->tail.mcall = mcall; + f->tail.args = args; + f->tail.nargs = nargs; + f->tail.pending = true; + + return true; + } + + *res = uc_native_invoke(vm, closure, ctx, mcall, args, nargs); + ucv_put(fno); + free(args); + + return uc_native_ok(vm); +} + +static uc_value_t * +uc_native_call_ctx(uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + size_t nargs = arg & 0xffff; + int32_t fi = d - nargs - 1; + + if (ucv_is_arrowfn(SLOT(fi))) + return ucv_get(f->ctx); + + return (arg & 0x80000000) ? ucv_get(SLOT(fi - 1)) : NULL; +} + +static bool +uc_native_closure_is_native(uc_value_t *v) +{ + return ucv_type(v) == UC_CLOSURE && ucv_as_closure(v)->function->native; +} + +/* Calls a native closure without the VM stack: the arguments move from the + * slots of the frame into the frame of the callee. */ +static bool +uc_native_call_direct(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + bool mcall = arg & 0x80000000; + size_t nargs = arg & 0xffff, i; + int32_t fi = d - nargs - 1, s; + uc_value_t *buf[16], **args = buf, *ctx, *res; + + if (nargs > ARRAY_SIZE(buf)) + args = xalloc(nargs * sizeof(*args)); + + ctx = uc_native_call_ctx(f, d, arg); + + for (i = 0; i < nargs; i++) + args[i] = uc_native_slot_take(f, fi + 1 + i); + + res = uc_native_invoke(vm, ucv_as_closure(SLOT(fi)), ctx, mcall, args, nargs); + + if (args != buf) + free(args); + + for (s = d - 1; s >= fi - (int32_t)mcall; s--) + uc_native_slot_clear(f, s); + + if (!uc_native_ok(vm)) { + ucv_put(res); + + return false; + } + + uc_native_store(vm, f, fi - mcall, res); + + return uc_native_signals_dispatch(vm); +} + +static bool +uc_native_call_common(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d, + uint32_t arg, const int32_t *x, bool tail) +{ + bool mcall = arg & 0x80000000; + size_t nargs = arg & 0xffff, stackoff; + int32_t fi = d - nargs - 1, i; + uc_value_t *ctx, *res = NULL; + ssize_t n; + bool ok; + + uc_native_pc_set(vm, pc); + + if (!tail && !(arg & 0x7fff0000) && uc_native_closure_is_native(SLOT(fi))) + return uc_native_call_direct(vm, f, d, arg); + + ctx = uc_native_call_ctx(f, d, arg); + n = uc_native_call_push(vm, f, d, arg, x, &stackoff); + + if (n < 0) { + ucv_put(ctx); + uc_native_stack_unwind(vm, stackoff - mcall); + ok = false; + } + else { + ok = uc_native_call_stack(vm, f, ctx, mcall, stackoff, n, tail, &res); + } + + for (i = d - 1; i >= fi - (int32_t)mcall; i--) + uc_native_slot_clear(f, i); + + if (tail && ok && !f->tail.pending) { + ucv_put(f->result); + f->result = res; + } + else if (ok && !tail) { + uc_native_store(vm, f, fi - mcall, res); + } + else { + ucv_put(res); + } + + if (ok && !tail) + ok = uc_native_signals_dispatch(vm); + + return ok; +} + +bool +uc_native_call(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + return uc_native_call_common(vm, f, pc, d, arg, x, false); +} + +bool +uc_native_tailcall(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + return uc_native_call_common(vm, f, pc, d, arg, x, true); +} + +bool +uc_native_stcall(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + size_t nargs = arg & 0xffff, base = d - nargs, keep = vm->stack.count; + uc_value_t *res = NULL; + + uc_native_pc_set(vm, pc); + + for (size_t i = 0; i < nargs; i++) + uc_vm_stack_push(vm, ucv_get(SLOT(base + i))); + + uc_vm_stcall(vm, arg >> 16, nargs); + + if (vm->stack.count > keep) + res = uc_vm_stack_pop(vm); + + uc_native_stack_unwind(vm, keep); + + for (size_t i = 0; i < nargs; i++) + uc_native_slot_clear(f, base + i); + + uc_native_store(vm, f, base, res); + + return uc_native_ok(vm); +} + +bool +uc_native_print(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_pc_set(vm, pc); + uc_vm_value_print(vm, SLOT(d - 1)); + uc_native_slot_clear(f, d - 1); + + return uc_native_ok(vm); +} + +bool +uc_native_nextk(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *k = uc_native_slot_take(f, d - 1); + uc_value_t *v = uc_native_slot_take(f, d - 2); + uc_value_t *key = NULL, *val = NULL; + + uc_native_pc_set(vm, pc); + + if (uc_vm_iterator_next(vm, I_NEXTK, v, &k, &key, &val)) { + uc_native_store(vm, f, d - 2, key); + uc_native_store(vm, f, d - 1, k); + } + else { + ucv_put(k); + } + + ucv_put(v); + + return uc_native_ok(vm); +} + +bool +uc_native_nextkv(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *k = uc_native_slot_take(f, d - 1); + uc_value_t *v = uc_native_slot_take(f, d - 2); + uc_value_t *key = NULL, *val = NULL; + + uc_native_pc_set(vm, pc); + + if (uc_vm_iterator_next(vm, I_NEXTKV, v, &k, &key, &val)) { + uc_native_store(vm, f, d - 2, key); + uc_native_store(vm, f, d - 1, val); + uc_native_store(vm, f, d, k); + } + else { + ucv_put(k); + } + + ucv_put(v); + + return uc_native_ok(vm); +} + +bool +uc_native_delete(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_value_t *res; + bool ok; + + uc_native_pc_set(vm, pc); + ok = uc_vm_property_delete(vm, SLOT(d - 2), SLOT(d - 1), &res); + uc_native_slot_clear(f, d - 1); + uc_native_slot_clear(f, d - 2); + + if (ok) + uc_native_store(vm, f, d - 2, res); + + return uc_native_ok(vm); +} + +bool +uc_native_import(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uint16_t to = arg >> 16; + + uc_native_pc_set(vm, pc); + + if (to != 0xffff) { + uc_vm_import(vm, arg & 0xffff, to); + + return uc_native_ok(vm); + } + + uc_native_store(vm, f, d, + uc_vm_import_namespace(vm, x[0], (const uint32_t *)&x[1], arg & 0xffff)); + + return uc_native_ok(vm); +} + +void +uc_native_export(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uc_modexports_t *exports = uc_vm_export_target(vm); + + if (exports) + uc_vm_export_add(exports, uc_native_slot_capture(f, arg)); +} + +bool +uc_native_dynload(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + uint16_t count = arg & 0xffff, to = arg >> 16; + uc_value_t *name, *modscope; + bool ok = true; + + uc_native_pc_set(vm, pc); + name = uc_native_slot_take(f, d - 1); + modscope = uc_vm_dynload_module(vm, name); + ucv_put(name); + + if (!modscope) + return uc_native_ok(vm); + + if (count == 0) { + uc_native_store(vm, f, d - 1, uc_vm_dynload_namespace(vm, modscope)); + ucv_put(modscope); + + return uc_native_ok(vm); + } + + for (uint16_t i = 0; ok && i < count; i++) + ok = uc_vm_dynload_bind(vm, modscope, x[i], to + i); + + ucv_put(modscope); + + return ok && uc_native_ok(vm); +} + +/* Fused helpers: each runs a sequence of instructions that the backend found + * without a branch into it, with the depth `d` before the first one and the + * position `pc` after the one that can raise. */ + +/* The part of getk for an own key of an object, which needs neither the key + * as a value on the stack nor its hash computed again; returns false for + * anything else, which the generic path handles. */ +static bool +uc_native_getk_own(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) +{ + uc_program_t *prog = f->closure->function->program; + uint32_t k = arg & 0x00ffffff; + uc_value_t *key, *res; + bool found; + + if (ucv_type(SLOT(d - 1)) != UC_OBJECT) + return false; + + key = uc_program_get_constant(prog, k); + + if (ucv_type(key) != UC_STRING) { + ucv_put(key); + + return false; + } + + found = ucv_key_get_hashed(vm, SLOT(d - 1), key, + uc_program_get_constant_hash(prog, k), &res); + ucv_put(key); + + if (!found) + return false; + + if (arg & UC_NATIVE_GETK_KEEP) { + uc_native_store(vm, f, d, res); + } + else { + uc_native_slot_clear(f, d - 1); + uc_native_store(vm, f, d - 1, res); + } + + return true; +} + +/* LOAD of the constant key in the low 24 bits of `arg`, then LVAL, or PVAL + * with UC_NATIVE_GETK_KEEP; with `src` other than UC_NATIVE_GETK_STACK, an + * LLOC of the slot or an LUPV of the upvalue with UC_NATIVE_GETK_UPVAL + * comes first. */ +bool +uc_native_getk(uc_native_frame_t *f, uint32_t pc, int32_t d, uint32_t arg, uint32_t src) +{ + uc_vm_t *vm = f->vm; + + if (src != UC_NATIVE_GETK_STACK) { + if (src & UC_NATIVE_GETK_UPVAL) + uc_native_insn_lupv(vm, f, d, src & ~UC_NATIVE_GETK_UPVAL); + else + uc_native_insn_lloc(vm, f, d, src); + + d++; + } + + if (uc_native_getk_own(vm, f, d, arg)) + return true; + + uc_native_insn_load(vm, f, d, arg & 0x00ffffff); + + if (arg & UC_NATIVE_GETK_KEEP) + return uc_native_insn_pval(vm, f, pc, d + 1); + + return uc_native_insn_lval(vm, f, pc, d + 1); +} + +/* `arg` times POP */ +void +uc_native_popn(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + for (uint32_t i = 0; i < arg; i++) + uc_native_insn_pop(vm, f, d - i); +} + +/* CALL, then POP of its result */ +bool +uc_native_callpop(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + if (!uc_native_call(f, pc, d, arg, x)) + return false; + + uc_native_insn_pop(vm, f, d - (arg & 0xffff) - (arg >> 31)); + + return true; +} + +/* SLOC, then POP */ +void +uc_native_slocpop(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_insn_sloc(f, d, arg); + uc_native_insn_pop(vm, f, d); +} + +/* LNULL, which has the empty slot `d` to push to, then EQS, or NES if `arg` + * is set, then JMPZ; returns whether the branch is taken. Strict equality + * cannot raise. */ +bool +uc_native_jnull(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_comparison(vm, f, pc, d + 1, arg ? I_NES : I_EQS); + + return uc_native_insn_jmpz(f, d); +} + +/* NOT, then JMPZ; returns whether the branch is taken. NOT cannot raise. */ +bool +uc_native_notjmpz(UC_NATIVE_INSN_ARGS) +{ + uc_native_slot_set(f, d - 1, ucv_boolean_new(uc_vm_value_not(SLOT(d - 1)))); + + return uc_native_insn_jmpz(f, d); +} diff --git a/native_trace.c b/native_trace.c new file mode 100644 index 00000000..b511912e --- /dev/null +++ b/native_trace.c @@ -0,0 +1,319 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* Resolution of the stack traces that native programs built without debug + * information print, with the map file written when they were built. It runs + * on the build host, as part of libucode, not in native programs. */ + +#include +#include + +#include "ucode/internal/native.h" +#include "ucode/internal/program.h" +#include "ucode/internal/source.h" +#include "ucode/internal/lib.h" + +/* Reads the program of the unit that stack traces name `path` from the map + * file `file`. After its magic line, the map holds for every unit a line with + * its path, a line with the length of its program and the program itself, + * written with debug information, see uc_program_write_llvm(). A unit loaded + * by a relative path is found by its normalised path. */ +static uc_program_t * +uc_native_map_program(const char *file, const char *path, char **errp) +{ + char *line = NULL, *buf = NULL, *key = uc_path_normalize(path); + uc_program_t *program = NULL; + size_t cap = 0, len = 0; + uc_source_t *input; + bool found = false; + ssize_t n; + FILE *fp; + + fp = fopen(file, "rb"); + + if (!fp) { + xasprintf(errp, "Unable to open map file '%s': %m\n", file); + free(key); + + return NULL; + } + + if (getline(&line, &cap, fp) < 0 || strcmp(line, UC_NATIVE_MAP_MAGIC)) { + xasprintf(errp, "Unable to load map file '%s': not a map file\n", file); + goto out; + } + + while ((n = getline(&line, &cap, fp)) > 0) { + if (line[n - 1] == '\n') + line[n - 1] = 0; + + found = !strcmp(line, path) || (key && !strcmp(line, key)); + + if (getline(&line, &cap, fp) <= 0) { + found = false; + break; + } + + len = strtoul(line, NULL, 10); + + if (found) + break; + + if (fseek(fp, len, SEEK_CUR)) + break; + } + + if (!found) { + xasprintf(errp, "Map file '%s' has no program for '%s'\n", file, path); + goto out; + } + + buf = xalloc(len ? len : 1); + + if (fread(buf, 1, len, fp) != len) { + xasprintf(errp, "Unable to load map file '%s': truncated\n", file); + free(buf); + goto out; + } + + input = uc_source_new_buffer(path, buf, len); + + if (!input) { + free(buf); + goto out; + } + + program = uc_program_load(input, errp); + uc_source_put(input); + + for (size_t i = 0; program && i < program->sources.count; i++) + uc_source_runpath_set(program->sources.entries[i], + program->sources.entries[i]->filename); + +out: + free(key); + free(line); + fclose(fp); + + return program; +} + +typedef struct { + char *path; + uc_program_t *program; +} uc_native_trace_map_t; + +uc_declare_vector(uc_native_trace_maps_t, uc_native_trace_map_t); + +static uc_program_t * +uc_native_trace_program(uc_native_trace_maps_t *maps, const char *file, const char *path) +{ + char *err = NULL; + + for (size_t i = 0; i < maps->count; i++) + if (!strcmp(maps->entries[i].path, path)) + return maps->entries[i].program; + + uc_vector_push(maps, { + .path = xstrdup(path), + .program = uc_native_map_program(file, path, &err) + }); + + if (err) + fprintf(stderr, "%s", err); + + free(err); + + return maps->entries[maps->count - 1].program; +} + +typedef struct { + uc_native_trace_maps_t maps; + const char *file; + uc_value_t *stacktrace; + uc_stringbuf_t *raw; + uc_program_t *program; + size_t id, pc; + bool unresolved; +} uc_native_trace_t; + +/* Fills `entry` from a frame of code without debug information, given by its + * function number and instruction offset in the program loaded as `path`. */ +static bool +uc_native_trace_resolve(uc_native_trace_t *t, uc_value_t *entry, const char *path, + size_t id, size_t pc) +{ + uc_program_t *program = uc_native_trace_program(&t->maps, t->file, path); + uc_function_t *fn = program ? uc_program_function_load(program, id) : NULL; + uc_source_t *source; + size_t pos; + + if (!fn) + return false; + + source = uc_program_function_source(fn); + pos = uc_program_function_srcpos(fn, pc); + + ucv_object_add(entry, "filename", ucv_string_new(source->filename)); + ucv_object_add(entry, "line", ucv_int64_new(uc_source_get_line(source, &pos))); + ucv_object_add(entry, "byte", ucv_int64_new(pos)); + ucv_object_add(entry, "function", + ucv_string_new(fn->name[0] ? fn->name : "[anonymous function]")); + + if (!ucv_array_length(t->stacktrace)) { + t->program = program; + t->id = id; + t->pc = pc; + } + + return true; +} + +/* Adds the frame that `line` describes, in the form that the VM prints for + * code without debug information, to the stack trace collected so far. + * Returns false if `line` is not such a frame. */ +static bool +uc_native_trace_frame(uc_native_trace_t *t, const char *line) +{ + size_t n = ucv_array_length(t->stacktrace), id, pc, lnum, byte; + char path[4096]; + const char *p, *paren, *end; + uc_value_t *entry; + + if (n && sscanf(line, " (%zu tail call frames omitted)", &pc) == 1) { + ucv_object_add(ucv_array_get(t->stacktrace, n - 1), "tco", ucv_int64_new(pc)); + + return true; + } + + if (!n && !strncmp(line, "In function ", 12)) + p = line + 12; + else if (n && !strncmp(line, " called from function ", 23)) + p = line + 23; + else + return false; + + entry = ucv_object_new(NULL); + paren = strrchr(p, '('); + end = strstr(p, " [C]"); + + if (sscanf(p, "#%zu at offset %zu of %4095[^\n]", &id, &pc, path) == 3) { + if (!uc_native_trace_resolve(t, entry, path, id, pc)) + t->unresolved = true; + } + else if (paren && paren > p && + sscanf(paren, "(%4095[^:]:%zu:%zu)", path, &lnum, &byte) == 3) { + ucv_object_add(entry, "filename", ucv_string_new(path)); + ucv_object_add(entry, "line", ucv_int64_new(lnum)); + ucv_object_add(entry, "byte", ucv_int64_new(byte)); + ucv_object_add(entry, "function", ucv_string_new_length(p, paren - p - 1)); + + /* the context of the innermost frame needs its source, which the map + * does not hold for code of other programs */ + if (!n) + t->unresolved = true; + } + else if (end) { + ucv_object_add(entry, "function", ucv_string_new_length(p, end - p)); + + if (!n) + t->unresolved = true; + } + else { + ucv_put(entry); + + return false; + } + + ucv_array_push(t->stacktrace, entry); + ucv_stringbuf_addstr(t->raw, line, strlen(line)); + + return true; +} + +/* Writes the collected stack trace in the form that a program with debug + * information prints, or unchanged if it cannot be resolved. */ +static void +uc_native_trace_flush(uc_native_trace_t *t, FILE *out) +{ + size_t n = ucv_array_length(t->stacktrace), startoff = 0, endoff = 0, srcpos; + uc_stringbuf_t *buf; + uc_function_t *fn; + + if (!n) + return; + + fn = (t->program && !t->unresolved) ? uc_program_function_load(t->program, t->id) : NULL; + + if (fn) { + /* the outermost frame is the program itself, which has no name */ + ucv_object_delete(ucv_array_get(t->stacktrace, n - 1), "function"); + + srcpos = uc_program_function_srcpos(fn, t->pc); + uc_program_function_stmt_bounds(fn, t->pc, &startoff, &endoff); + + buf = uc_stringbuf_new(); + uc_error_context_format(buf, uc_program_function_source(fn), t->stacktrace, + startoff, endoff, srcpos); + fwrite(buf->buf, 1, uc_stringbuf_length(buf), out); + uc_stringbuf_free(buf); + } + else { + fwrite(t->raw->buf, 1, uc_stringbuf_length(t->raw), out); + } + + ucv_array_delete(t->stacktrace, 0, n); + uc_stringbuf_reset(t->raw); + t->program = NULL; + t->unresolved = false; +} + +int +uc_native_trace_decode(const char *file, FILE *in, FILE *out) +{ + uc_native_trace_t t = { + .file = file, + .stacktrace = ucv_array_new(NULL), + .raw = uc_stringbuf_new() + }; + size_t cap = 0; + char *line = NULL; + ssize_t n; + + while ((n = getline(&line, &cap, in)) > 0) { + if (uc_native_trace_frame(&t, line)) + continue; + + uc_native_trace_flush(&t, out); + fwrite(line, 1, n, out); + } + + uc_native_trace_flush(&t, out); + + for (size_t i = 0; i < t.maps.count; i++) { + free(t.maps.entries[i].path); + + if (t.maps.entries[i].program) + uc_program_put(t.maps.entries[i].program); + } + + uc_vector_clear(&t.maps); + ucv_put(t.stacktrace); + uc_stringbuf_free(t.raw); + free(line); + + return 0; +} diff --git a/program.c b/program.c index d769885a..cb2b3b68 100644 --- a/program.c +++ b/program.c @@ -165,13 +165,50 @@ uc_program_function_free(uc_function_t *func) ucv_unref(&func->progref); uc_chunk_free(&func->chunk); + free(func->native); free(func); } +/* Constants are immutable, so each one is created once and shared by all its + * uses, instead of once per evaluation. */ uc_value_t * uc_program_get_constant(uc_program_t *prog, size_t idx) { - return uc_vallist_get(&prog->constants, idx); + uc_constant_values_t *values = &prog->constant_values; + + if (idx >= prog->constants.isize) + return NULL; + + if (idx >= values->count) { + uc_vector_extend(values, prog->constants.isize - values->count); + values->count = prog->constants.isize; + } + + if (!values->entries[idx]) + values->entries[idx] = uc_vallist_get(&prog->constants, idx); + + return ucv_get(values->entries[idx]); +} + +/* The hash of the string constant `idx` as an object key, computed once */ +uint64_t +uc_program_get_constant_hash(uc_program_t *prog, size_t idx) +{ + uc_constant_hashes_t *hashes = &prog->constant_hashes; + uc_value_t *v; + + if (idx >= hashes->count) { + uc_vector_extend(hashes, prog->constants.isize - hashes->count); + hashes->count = prog->constants.isize; + } + + if (!hashes->entries[idx]) { + v = uc_program_get_constant(prog, idx); + hashes->entries[idx] = ucv_object_key_hash(v); + ucv_put(v); + } + + return hashes->entries[idx]; } ssize_t diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt index a93add5a..080fb081 100644 --- a/tests/cram/CMakeLists.txt +++ b/tests/cram/CMakeLists.txt @@ -25,3 +25,25 @@ IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") ELSE() SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "UCODE_BIN=valgrind --quiet --leak-check=full $") ENDIF() + +IF(LLVM_SUPPORT) + FILE(GLOB native_test_cases "native_*.t") + FIND_PROGRAM(LLVM_OPT NAMES opt opt-21 opt-20 opt-19 opt-18 opt-17 opt-16) + FIND_PROGRAM(LLVM_LLC NAMES llc llc-21 llc-20 llc-19 llc-18 llc-17 llc-16) + + IF(LLVM_OPT AND LLVM_LLC) + ADD_TEST( + NAME cram-native + COMMAND ${PYTHON_VENV_CRAM} ${native_test_cases} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + ) + + SET_PROPERTY(TEST cram-native APPEND PROPERTY ENVIRONMENT + "BUILD_BIN_DIR=$" + "LLVM_LINK=${NATIVE_LLVM_LINK}" + "OPT=${LLVM_OPT}" + "LLC=${LLVM_LLC}" + "CC=${CMAKE_C_COMPILER}" + ) + ENDIF() +ENDIF() diff --git a/tests/cram/native_build.t b/tests/cram/native_build.t new file mode 100644 index 00000000..0e781f28 --- /dev/null +++ b/tests/cram/native_build.t @@ -0,0 +1,136 @@ +setup common environment: + + $ [ -n "$BUILD_BIN_DIR" ] && export PATH="$BUILD_BIN_DIR:$PATH" + $ native() { + > out=$1; flags=$2; shift 2 + > "$BUILD_BIN_DIR/ucode" -c,llvm$flags -o "$out.ll" "$@" && + > "$LLVM_LINK" "$out.ll" "$BUILD_BIN_DIR/libucode-native.bc" -o "$out.all.bc" && + > "$OPT" -passes='internalize,default' \ + > -internalize-public-api-file="$BUILD_BIN_DIR/libucode-native.api" \ + > "$out.all.bc" -o "$out.bc" && + > "$LLC" -O1 -relocation-model=pic -filetype=obj "$out.bc" -o "$out.o" && + > "$CC" "$out.o" -lm -ldl -rdynamic -Wl,-rpath,"${RUNPATH-$BUILD_BIN_DIR/native-abi}" -o "$out" + > } + +a native program behaves as the interpreter, also when it fails: + + $ cat > hello.uc <<'EOF' + > function fib(n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); } + > print(fib(15), " ", ARGV, " ", defined, "\n"); + > null.x; + > EOF + + $ native hello ",debug" hello.uc + $ ./hello --ucode-define defined=42 -- -D x + Reference error: left-hand side expression is null + In hello.uc, line 3, byte 6: + + `null.x;` + ~~~~~^~ + + + 610 [ "-D", "x" ] 42 + [254] + +it contains neither the interpreter, the compiler nor the bytecode loader: + + $ nm hello | grep -E ' (uc_vm_execute|uc_vm_execute_chunk|uc_compile_from_source|uc_lexer_init|uc_program_load)$' + [1] + +without the debug flag, a native program carries no debug information and +its stack traces name functions and instruction offsets. The map file +written at build time resolves them later from a log: + + $ native small ",map=hello.map" hello.uc + $ ./small + Reference error: left-hand side expression is null + In function #1 at offset 63 of hello.uc + + + 610 [ ] + [254] + $ ./small > small.log 2>&1 + [254] + $ "$BUILD_BIN_DIR/ucode-trace" hello.map small.log + Reference error: left-hand side expression is null + In hello.uc, line 3, byte 6: + + `null.x;` + ~~~~~^~ + + + 610 [ ] + $ printf 'In function foo [C]\n' | "$BUILD_BIN_DIR/ucode-trace" hello.map + In function foo [C] + +the scripts a program loads with literal names are embedded, so that it +runs without its source files: + + $ mkdir -p app/lib app/inc + $ cat > app/main.uc <<'EOF' + > let lib = require("lib.counter"); + > print(lib.next(), " ", lib.next(), " ", lib === require("lib.counter"), "\n"); + > include("inc/part.uc", { n: 7 }); + > print(render("inc/page.ut", { items: [1, 2] }), "\n"); + > print(sourcepath(), "\n"); + > EOF + $ echo 'let n = 0; return { next: () => ++n };' > app/lib/counter.uc + $ echo 'print("part ", n, "\n"); include("leaf.uc");' > app/inc/part.uc + $ echo 'print("leaf\n");' > app/inc/leaf.uc + $ printf '{%% for (let i in items): %%}[{{ i }}]{%% endfor %%}' > app/inc/page.ut + + $ cd app && native ../bundled "" -L "$PWD/*.uc" main.uc && cd .. + $ mv app app.moved && mkdir app && cd app + $ ../bundled --ucode-search-path "$PWD/*.uc" + 1 2 true + part 7 + leaf + [1][2] + /.*/app/main.uc (re) + $ cd .. && rm -r app && mv app.moved app + +scripts loaded by computed names are embedded with compile flags, under +the path the program loads them by: + + $ cat > plugins.uc <<'EOF' + > for (let path in sort(bundle_files())) + > if (wildcard(path, "/usr/share/demo/*.uc")) + > include(path); + > let name = "plug" + "in"; + > include(`/usr/share/demo/${name}.uc`); + > EOF + $ mkdir demo && echo 'print("plugin\n"); include("helper.uc");' > demo/plugin.uc + $ echo 'print("helper\n");' > demo/helper.uc + + $ native plugins ",embed=/usr/share/demo=$PWD/demo" plugins.uc + $ rm -r demo && ./plugins + helper + plugin + helper + plugin + helper + +a script that fails to compile fails to load as in the interpreter, and +source code compiled at runtime is not supported: + + $ echo 'let = 1;' > broken.uc + $ echo 'try { include("broken.uc"); } catch (e) { print(e.message, "\n"); }' > load.uc + $ native load "" load.uc && ./load + Unable to compile source file '/.*/broken.uc': (re) + + | Syntax error: Expecting variable name + | In line 1, byte 5: + | + | `let = 1;` + | ^-- Near here + + $ echo 'print(loadstring("return 6 * 7;")(), "\n");' > dynamic.uc + $ native dynamic "" dynamic.uc && ./dynamic + Runtime error: Unable to compile source string: + + | Source code compilation not supported + + In function #1 at offset 19 of dynamic.uc + + + [254] diff --git a/tests/cram/test_basic.t b/tests/cram/test_basic.t index ab825f0b..aca35e32 100644 --- a/tests/cram/test_basic.t +++ b/tests/cram/test_basic.t @@ -70,7 +70,13 @@ check that ucode provides exepected help: Supported flags: no-interp (omit interpreter line), interp=... (over- ride interpreter line with ...), dynlink=... (force import from ... to be treated as shared extensions loaded at runtime), module (build load- - able library). + able library), llvm (write LLVM IR of a native program, if built with + LLVM support), target=... (LLVM target triple of the native program), + embed=[path=]file (also embed `file` or the scripts below it, which the + program loads as `path` by computed names), embed-template=[path=]file + (the same for templates), debug (embed debug information and source + text for complete stack traces), map=... (write the program with debug + information to ..., for ucode-trace to resolve its stack traces). -o path Output file path when compiling. If omitted, the compiled byte code diff --git a/tests/custom/04_modules/09_import_wildcard b/tests/custom/04_modules/09_import_wildcard index aa3dc820..6877422e 100644 --- a/tests/custom/04_modules/09_import_wildcard +++ b/tests/custom/04_modules/09_import_wildcard @@ -71,3 +71,53 @@ export default a + b + c; -- Expect stdout -- [ 6, { "a": 1, "b": 2, "c": 3, "default": 6 } ] -- End -- + + +All wildcard imports of a module refer to the same object. + +-- Testcase -- +import * as mod from "./files/test4.uc"; +import { other } from "./files/test5.uc"; + +print([mod === other, mod], "\n"); +-- End -- + +-- File test4.uc -- +export const a = 1; +-- End -- + +-- File test5.uc -- +import * as m from "./test4.uc"; +export const other = m; +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, { "a": 1 } ] +-- End -- + + +This also applies to the wildcard imports of a C module. + +-- Testcase -- +import * as math from 'math'; +import { other } from "./files/test6.uc"; + +print([math === other, type(math.abs)], "\n"); +-- End -- + +-- File test6.uc -- +import * as m from 'math'; +export const other = m; +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, "function" ] +-- End -- diff --git a/tests/custom/CMakeLists.txt b/tests/custom/CMakeLists.txt index 91fdbe00..3aad5249 100644 --- a/tests/custom/CMakeLists.txt +++ b/tests/custom/CMakeLists.txt @@ -21,6 +21,34 @@ IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") ) ENDIF() +IF(LLVM_SUPPORT) + FIND_PROGRAM(LLVM_OPT NAMES opt opt-21 opt-20 opt-19 opt-18 opt-17 opt-16) + FIND_PROGRAM(LLVM_LLC NAMES llc llc-21 llc-20 llc-19 llc-18 llc-17 llc-16) + + IF(LLVM_OPT AND LLVM_LLC) + ADD_TEST( + NAME custom-native + COMMAND $ -L $/*.so -S run_tests.uc + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + ) + + SET_PROPERTY(TEST custom-native APPEND PROPERTY ENVIRONMENT + "UCODE_BIN=${CMAKE_CURRENT_SOURCE_DIR}/native-ucode.sh" + "UCODE_LIB=${CMAKE_BINARY_DIR}" + "UCODE_TEST_SKIP=${CMAKE_CURRENT_SOURCE_DIR}/native-unsupported" + "NATIVE_RUNNER=valgrind --quiet --leak-check=full --child-silent-after-fork=yes --suppressions=${CMAKE_CURRENT_SOURCE_DIR}/valgrind.supp" + "UCODE=$" + "UCODE_NATIVE=${CMAKE_BINARY_DIR}/libucode-native.bc" + "LLVM_LINK=${NATIVE_LLVM_LINK}" + "OPT=${LLVM_OPT}" + "LLC=${LLVM_LLC}" + "CC=${CMAKE_C_COMPILER}" + ) + ELSE() + MESSAGE(WARNING "LLVM opt and llc not found, not running the tests against native programs") + ENDIF() +ENDIF() + # Debugger tests ADD_TEST( NAME debugger diff --git a/tests/custom/native-ucode.sh b/tests/custom/native-ucode.sh new file mode 100755 index 00000000..c09a25a9 --- /dev/null +++ b/tests/custom/native-ucode.sh @@ -0,0 +1,112 @@ +#!/bin/sh +# Drop-in replacement for the ucode CLI that runs the given program as a +# natively compiled executable instead of in the interpreter. The test runner +# uses it through UCODE_BIN to run the test suite against native programs. +# +# Environment: UCODE (the ucode CLI built with LLVM_SUPPORT), UCODE_NATIVE +# (the runtime libucode-native.bc, with libucode-native.api and the +# native-abi directory beside it), +# NATIVE_LIBS (the libraries it needs), NATIVE_TARGET (the target triple when +# cross compiling), LLVM_LINK, OPT, LLC, CC, LLVM_OPT_LEVEL, and +# NATIVE_RUNNER, a command to run the native executable with, such as +# valgrind or qemu. + +ucode=${UCODE:-ucode} +runtime=${UCODE_NATIVE:?UCODE_NATIVE names no libucode-native.bc} +opt=${OPT:-opt} +llc=${LLC:-llc} +cc=${CC:-cc} +level=${LLVM_OPT_LEVEL:-1} +libs=${NATIVE_LIBS:--lm -ldl} + +compile_args= +run_args= +source= + +quote() { + printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")" +} + +add_compile() { compile_args="$compile_args $(quote "$1")"; } +add_run() { run_args="$run_args $(quote "$1")"; } + +# Modes that do not run a program, or that native programs do not provide, +# are left to the interpreter. +for arg in "$@"; do + case "$arg" in + --) break ;; + -h|-p*|-c*|-t|-x*|-X*|-s|-o*) exec $ucode "$@" ;; + -*) ;; + *) break ;; + esac +done + +while [ $# -gt 0 ]; do + case "$1" in + -T*|-R|-S) + add_compile "$1" + shift + ;; + -L) + add_compile -L; add_compile "$2" + add_run --ucode-search-path; add_run "$2" + shift 2 + ;; + -L*) + add_compile "$1" + add_run --ucode-search-path; add_run "${1#-L}" + shift + ;; + -D|-F|-U|-l|-g) + case "$1" in + -D) add_run --ucode-define ;; + -F) add_run --ucode-define-file ;; + -U) add_run --ucode-undefine ;; + -l) add_run --ucode-library ;; + -g) add_run --ucode-gc ;; + esac + add_run "$2" + shift 2 + ;; + -e) + add_compile -e; add_compile "$2" + source=expr + shift 2 + break + ;; + --) + shift + break + ;; + *) + source=$1 + shift + break + ;; + esac +done + +tmp=$(mktemp -d) || exit 1 +trap 'rm -rf "$tmp"' EXIT + +# The test cases load the files of their "-- File --" sections also by +# computed names, so the program embeds all of them. +flags=llvm,debug${NATIVE_TARGET:+,target=$NATIVE_TARGET} + +if [ -d files ]; then + flags="$flags,embed=$PWD/files,embed-template=$PWD/files" +fi + +if [ "$source" = expr ]; then + eval "$ucode $compile_args -c,$flags -o \"\$tmp/prog.ll\"" || exit $? +else + eval "$ucode $compile_args -c,$flags -o \"\$tmp/prog.ll\" \"\$source\"" || exit $? +fi + +${LLVM_LINK:-llvm-link} "$tmp/prog.ll" "$runtime" -o "$tmp/all.bc" && +$opt -passes="internalize,default" -internalize-public-api-file="${runtime%.bc}.api" \ + "$tmp/all.bc" -o "$tmp/prog.bc" && +$llc -O$level -relocation-model=pic -filetype=obj "$tmp/prog.bc" -o "$tmp/prog.o" && +$cc "$tmp/prog.o" $libs -rdynamic -Wl,-rpath,"${runtime%/*}/native-abi" -o "$tmp/prog" || exit 125 + +eval "$NATIVE_RUNNER \"\$tmp/prog\" $run_args -- \"\$@\"" diff --git a/tests/custom/native-unsupported b/tests/custom/native-unsupported new file mode 100644 index 00000000..a8b3c759 --- /dev/null +++ b/tests/custom/native-unsupported @@ -0,0 +1,33 @@ +# Test cases that native programs cannot pass, read through UCODE_TEST_SKIP +# by the custom-native test. Format: category/test case-number reason + +00_syntax/30_call_error_callee 3 compiles source at runtime with loadstring() +03_stdlib/36_render 6 compiles source at runtime with loadstring() +03_stdlib/39_trace 1 expects the instruction trace of the interpreter +03_stdlib/40_proto 8 loads the debug module, which needs vm.c +03_stdlib/40_proto 9 loads the debug module, which needs vm.c +03_stdlib/61_loadstring 1 compiles source at runtime with loadstring() +03_stdlib/61_loadstring 2 compiles source at runtime with loadstring() +03_stdlib/61_loadstring 3 compiles source at runtime with loadstring() +03_stdlib/61_loadstring 4 compiles source at runtime with loadstring() +03_stdlib/61_loadstring 5 compiles source at runtime with loadstring() +03_stdlib/61_loadstring 6 compiles source at runtime with loadstring() +03_stdlib/61_loadstring 7 compiles source at runtime with loadstring() +03_stdlib/62_loadfile 1 compiles source at runtime with loadfile() +03_stdlib/62_loadfile 2 compiles source at runtime with loadfile() +03_stdlib/62_loadfile 3 compiles source at runtime with loadfile() +03_stdlib/62_loadfile 4 compiles source at runtime with loadfile() +03_stdlib/62_loadfile 5 compiles source at runtime with loadfile() +03_stdlib/62_loadfile 6 compiles source at runtime with loadfile() +03_stdlib/62_loadfile 7 runs /proc/self/exe as the ucode CLI +04_modules/19_dynamic_import_module_exception 1 compiles source at runtime with loadstring() +04_modules/19_source_file_closed 1 compiles source at runtime with loadfile() +04_modules/20_dynamic_import_srcpos 1 compiles source at runtime with loadstring() +04_modules/20_repeated_source_compile 1 runs /proc/self/exe as the ucode CLI +04_modules/21_changed_source_compile 1 compiles source at runtime with loadfile() +04_modules/21_dynamic_import_callframe_realloc 1 compiles source at runtime with loadstring() +04_modules/22_unseekable_source_compile 1 runs /proc/self/exe as the ucode CLI +04_modules/23_dynamic_import_failed 1 compiles source at runtime with loadstring() +04_modules/24_load_in_module_body 1 compiles source at runtime with loadfile() +04_modules/26_export_lifetime 1 runs /proc/self/exe as the ucode CLI +04_modules/27_precompiled_module_exports 1 runs /proc/self/exe as the ucode CLI diff --git a/tests/custom/run_tests.uc b/tests/custom/run_tests.uc index b0cade50..2fe9767c 100755 --- a/tests/custom/run_tests.uc +++ b/tests/custom/run_tests.uc @@ -10,6 +10,22 @@ let ucode_bin = getenv('UCODE_BIN') || `${topdir}/build/ucode`; let ucode_lib = getenv('UCODE_LIB') || `${topdir}/build`; let platform = trim(fs.popen('uname -s', 'r').read('all')); +/* UCODE_TEST_SKIP names a file of test cases to skip, one per line in the form + * `category/test case-number reason`, for runs against a UCODE_BIN that does + * not provide every feature of the ucode CLI */ +let skip_cases = {}; + +if (getenv('UCODE_TEST_SKIP')) { + let skip_list = fs.readfile(getenv('UCODE_TEST_SKIP')) ?? die(`Unable to read UCODE_TEST_SKIP: ${fs.error()}\n`); + + for (let entry in split(skip_list, '\n')) { + let m = match(entry, /^([^ #]+) ([0-9]+) /); + + if (m) + skip_cases[`${m[1]} ${m[2]}`] = true; + } +} + function mkdir_p(path) { let parts = split(rtrim(path, '/') || '/', /\/+/); let current = ''; @@ -204,6 +220,7 @@ function run_testcase(num, dir, testcase) { function run_test(file) { let name = fs.basename(file); + let id = `${fs.basename(fs.dirname(file))}/${name}`; printf('%s %s ', name, substr(line, length(name))); let tmpdir = sprintf('/tmp/test.%d', getpid()); @@ -214,7 +231,8 @@ function run_test(file) { try { for (let i, testcase in testcases) { - if (testcase.platform && index(testcase.platform, platform) < 0) { + if (skip_cases[`${id} ${i + 1}`] || + (testcase.platform && index(testcase.platform, platform) < 0)) { skipped++; continue; } diff --git a/trace.c b/trace.c new file mode 100644 index 00000000..db49246f --- /dev/null +++ b/trace.c @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2026 John Crispin + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* ucode-trace: resolves the stack traces that native programs built without + * debug information print, using the map file written at build time. */ + +#include +#include +#include + +#include "ucode/internal/native.h" + +int +main(int argc, char **argv) +{ + FILE *in = stdin; + int rv; + + if (argc < 2 || argc > 3) { + fprintf(stderr, "Usage: %s MAPFILE [LOGFILE]\n", argv[0]); + + return 1; + } + + if (argc == 3) { + in = fopen(argv[2], "r"); + + if (!in) { + fprintf(stderr, "Unable to open %s: %s\n", argv[2], strerror(errno)); + + return 1; + } + } + + rv = uc_native_trace_decode(argv[1], in, stdout); + + if (in != stdin) + fclose(in); + + return rv; +} diff --git a/types.c b/types.c index 49443066..5e3cc8bc 100644 --- a/types.c +++ b/types.c @@ -234,6 +234,9 @@ ucv_gc_mark(uc_value_t *uv) for (i = 0; instance && i < instance->exports.count; i++) ucv_gc_mark(&instance->exports.entries[i]->header); + for (i = 0; instance && i < instance->namespaces.count; i++) + ucv_gc_mark(instance->namespaces.entries[i].object); + ucv_gc_mark(&function->program->header); break; @@ -282,6 +285,7 @@ void uc_program_instance_free(uc_program_t *program, uc_closure_t *closure, bool retain) { uc_export_instance_t *instance = uc_program_instance_get(program, closure, false); + uc_namespaces_t namespaces; uc_modexports_t exports; size_t i; @@ -290,11 +294,16 @@ uc_program_instance_free(uc_program_t *program, uc_closure_t *closure, bool reta /* the release of an export can free another closure of the program */ exports = instance->exports; + namespaces = instance->namespaces; *instance = program->instances.entries[--program->instances.count]; + for (i = 0; i < namespaces.count; i++) + ucv_put_value(namespaces.entries[i].object, retain); + for (i = 0; i < exports.count; i++) ucv_put_value(&exports.entries[i]->header, retain); + uc_vector_clear(&namespaces); uc_vector_clear(&exports); } @@ -408,6 +417,12 @@ ucv_free(uc_value_t *uv, bool retain) uc_vallist_free(&program->constants); + for (i = 0; i < program->constant_values.count; i++) + ucv_put(program->constant_values.entries[i]); + + uc_vector_clear(&program->constant_values); + uc_vector_clear(&program->constant_hashes); + for (i = 0; i < program->sources.count; i++) ucv_put_value(&program->sources.entries[i]->header, retain); @@ -797,12 +812,8 @@ ucv_int64_get(uc_value_t *uv) errno = 0; - if ((pv & 3) == UC_INTEGER) { - if (((pv >> 2) & 1) == 0) - return (int64_t)(pv >> 3); - - return -(int64_t)(pv >> 3); - } + if ((pv & 3) == UC_INTEGER) + return ucv_small_integer_get(uv); switch (ucv_type(uv)) { case UC_INTEGER: @@ -1212,8 +1223,11 @@ ucv_object_key_match(uc_object_entry_t *e, const char *s, size_t len, uc_value_t return klen == len && !memcmp(k, s, len); } +/* Finds the key `s` of `len` bytes, whose hash is `*hash` if `hash` is + * given; only objects with an index need the hash. */ static uc_object_entry_t * -ucv_object_find(uc_object_t *object, const char *s, size_t len, uc_value_t *kv) +ucv_object_find_hashed(uc_object_t *object, const char *s, size_t len, uc_value_t *kv, + const uint64_t *hash) { uint32_t i, n; @@ -1225,7 +1239,7 @@ ucv_object_find(uc_object_t *object, const char *s, size_t len, uc_value_t *kv) return NULL; } - for (i = ucv_object_hash_bytes(s, len) & object->mask; + for (i = (hash ? *hash : ucv_object_hash_bytes(s, len)) & object->mask; (n = object->index[i]) != 0; i = (i + 1) & object->mask) if (ucv_object_key_match(&object->entries[n - 1], s, len, kv)) @@ -1234,6 +1248,18 @@ ucv_object_find(uc_object_t *object, const char *s, size_t len, uc_value_t *kv) return NULL; } +static uc_object_entry_t * +ucv_object_find(uc_object_t *object, const char *s, size_t len, uc_value_t *kv) +{ + return ucv_object_find_hashed(object, s, len, kv, NULL); +} + +uint64_t +ucv_object_key_hash(uc_value_t *key) +{ + return ucv_object_hash_bytes(_ucv_string_get(&key), ucv_string_length(key)); +} + static void ucv_object_index_add(uc_object_t *object, uint32_t n) { @@ -3152,6 +3178,26 @@ ucv_array_set_key(uc_value_t *arr, uc_value_t *key, uc_value_t *val) return false; } +bool +ucv_key_get_hashed(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key, uint64_t hash, + uc_value_t **res) +{ + uc_object_entry_t *e; + + if (ucv_type(obj) != UC_OBJECT) + return false; + + e = ucv_object_find_hashed(ucv_as_object(obj), _ucv_string_get(&key), + ucv_string_length(key), key, &hash); + + if (!e) + return false; + + *res = ucv_key_resolve_upval(vm, e->value); + + return true; +} + uc_value_t * ucv_key_get(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key) { diff --git a/vmcore.c b/vmcore.c index 0d101908..1193b3ed 100644 --- a/vmcore.c +++ b/vmcore.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -689,6 +690,16 @@ uc_vm_capture_stacktrace(uc_vm_t *vm, size_t i) uc_vm_stacktrace_add_position(entry, source, srcpos, "line", "byte"); uc_vm_stacktrace_add_statement(vm, entry, function, off); + + /* without debug information the position is that of the function + * only; record the function and instruction offset, which the debug + * information of a separate copy of the program can resolve */ + if (!function->chunk.debuginfo.offsets.count) { + ucv_object_add(entry, "path", ucv_string_new(source->runpath)); + ucv_object_add(entry, "id", + ucv_uint64_new(uc_program_function_id(function->program, function))); + ucv_object_add(entry, "pc", ucv_uint64_new(off)); + } } if (i > 1) { @@ -724,6 +735,46 @@ uc_vm_capture_stacktrace(uc_vm_t *vm, size_t i) return stacktrace; } +/* Describes the frames of a stack trace of code without debug information by + * their function and instruction offset, in the form that the debug + * information of the program can resolve later, see ucode-trace. */ +static void +uc_vm_stacktrace_format_positions(uc_stringbuf_t *buf, uc_value_t *stacktrace) +{ + uc_value_t *entry, *path, *file, *name, *tco; + const char *prefix, *fn; + + for (size_t i = 0; i < ucv_array_length(stacktrace); i++) { + entry = ucv_array_get(stacktrace, i); + path = ucv_object_get(entry, "path", NULL); + file = ucv_object_get(entry, "filename", NULL); + name = ucv_object_get(entry, "function", NULL); + tco = ucv_object_get(entry, "tco", NULL); + prefix = i ? " called from " : "In "; + fn = name ? ucv_string_get(name) : "[anonymous function]"; + + /* code with debug information, e.g. a closure of another program, is + * described by its source position */ + if (path) + ucv_stringbuf_printf(buf, "%sfunction #%" PRIu64 " at offset %" PRIu64 " of %s\n", + prefix, + ucv_uint64_get(ucv_object_get(entry, "id", NULL)), + ucv_uint64_get(ucv_object_get(entry, "pc", NULL)), + ucv_string_get(path)); + else if (file) + ucv_stringbuf_printf(buf, "%sfunction %s (%s:%" PRId64 ":%" PRId64 ")\n", + prefix, fn, ucv_string_get(file), + ucv_int64_get(ucv_object_get(entry, "line", NULL)), + ucv_int64_get(ucv_object_get(entry, "byte", NULL))); + else + ucv_stringbuf_printf(buf, "%sfunction %s [C]\n", prefix, fn); + + if (tco) + ucv_stringbuf_printf(buf, " (%" PRId64 " tail call frames omitted)\n", + ucv_int64_get(tco)); + } +} + static uc_value_t * uc_vm_get_error_context(uc_vm_t *vm) { @@ -756,7 +807,10 @@ uc_vm_get_error_context(uc_vm_t *vm) byte = offset; line = uc_source_get_line(uc_program_function_source(frame->closure->function), &byte); - if (line) { + if (!chunk->debuginfo.offsets.count && frame->ip != chunk->entries) { + uc_vm_stacktrace_format_positions(buf, stacktrace); + } + else if (line) { /* underline the statement the failing instruction belongs to, where one * can be resolved, rather than only marking that instruction */ uc_program_function_stmt_bounds(frame->closure->function, insn, &startoff, &endoff); @@ -2446,16 +2500,26 @@ uc_value_t * uc_vm_import_namespace(uc_vm_t *vm, size_t from, const uint32_t *names, size_t nnames) { uc_program_t *prog = uc_vm_current_program(vm); - uc_modexports_t *exports = uc_vm_run_exports(prog, uc_vector_last(&prog->runs), false); - size_t count = exports ? exports->count : 0; + uc_program_run_t *run = uc_vector_last(&prog->runs); + uc_export_instance_t *instance = + run ? uc_program_instance_get(prog, run->closure, false) : NULL; + uc_modexports_t *exports = instance ? &instance->exports : NULL; + size_t count = exports ? exports->count : 0, first = from, i; uc_value_t *name, *modobj; - size_t i; + bool complete = true; + + if (instance) + uc_vector_foreach(&instance->namespaces, ns) + if (ns->from == first && ns->count == nnames) + return ucv_get(ns->object); modobj = ucv_object_new(vm); for (i = 0; i < nnames; i++, from++) { - if (from >= count || !exports->entries[from]) + if (from >= count || !exports->entries[from]) { + complete = false; continue; + } name = uc_program_get_constant(prog, names[i]); @@ -2468,6 +2532,14 @@ uc_vm_import_namespace(uc_vm_t *vm, size_t from, const uint32_t *names, size_t n ucv_set_constant(modobj, true); + /* The exports are references to the variables of the module, so all + * wildcard imports of the module can share one object. An export that is + * missing yet, e.g. in a module still in progress, can appear later. */ + if (instance && complete) + uc_vector_push(&instance->namespaces, { + .from = first, .count = nnames, .object = ucv_get(modobj) + }); + return modobj; } @@ -2513,16 +2585,34 @@ uc_vm_dynload_module(uc_vm_t *vm, uc_value_t *name) return modscope; } +/* All wildcard imports of a module scope share one copy. The registry entry + * holds the scope beside the copy, so that the address in the key stays in + * use while the entry exists. A change to the scope after the first import + * does not show in the copy. */ uc_value_t * uc_vm_dynload_namespace(uc_vm_t *vm, uc_value_t *modscope) { - uc_value_t *modobj = ucv_object_new(vm); + uc_value_t *modobj, *entry; + char key[sizeof("namespace.") + 2 * sizeof(void *) + 2]; + + snprintf(key, sizeof(key), "namespace.%p", (void *)modscope); + entry = uc_vm_registry_get(vm, key); + + if (entry) + return ucv_get(ucv_array_get(entry, 1)); + + modobj = ucv_object_new(vm); ucv_object_foreach_entry(modscope, e) ucv_object_insert_string(modobj, e->key, ucv_get(e->value)); ucv_set_constant(modobj, true); + entry = ucv_array_new_length(vm, 2); + ucv_array_push(entry, ucv_get(modscope)); + ucv_array_push(entry, ucv_get(modobj)); + uc_vm_registry_set(vm, key, entry); + return modobj; } From 62de75b91d4dc5313ecffb7056ee7c2cc8a7c704 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 17:26:42 +0100 Subject: [PATCH 09/11] native: make the generated code and the call path faster - Global variables are looked up with the cached hash of their constant name, in the interpreter as well, and a new global shares the name as its key. Native helpers read constants without a reference of their own. - A call of a native closure moves the arguments from the slots of the caller into those of the callee, and calls through the VM stack move them there, instead of copying references. - Two loads of locals, upvalues or constants run in one helper call, as do a load of a local and the call after it, and a copy, the conditional jump after it and the pop on the path that does not jump. A return releases the frame in its own helper call, and only the slots below the returned value. - Functions in which no closure or export can capture a slot share one constant array of empty cells instead of a zeroed array per call. - The runtime bitcode keeps no target CPU and feature attributes, which kept LLVM from inlining any helper into the generated code. Helpers whose inlining costs more space than it saves time stay out of line. - NATIVE_TARGET_FLAGS sets the CPU and the float ABI of the runtime in clang syntax, for example "-march=mips32r2 -msoft-float" for OpenWrt mips_24kc. Without target attributes, the program bitcode must then be compiled for the same CPU and float ABI, for example by clang with these flags; llc without matching options gives code for the default ABI of the target without a warning. native-ucode.sh uses clang when NATIVE_TARGET_FLAGS is set. Cycles at -O2, measured per change: global lookups props -9 %; argument moves fib -9 %; inlining loop -10 %, props -7 %, fib -5 %; shared cells fib -14 %, closures -4 %. MIPS32 big endian under QEMU: native suite 249/250, the failure is a musl regex case. Signed-off-by: John Crispin --- CMakeLists.txt | 36 ++++- cmake/native-strip.cmake | 16 ++ include/ucode/internal/native.h | 34 ++++ include/ucode/internal/program.h | 23 ++- include/ucode/internal/types.h | 7 +- include/ucode/internal/vmcore.h | 36 ++++- llvm.c | 138 ++++++++++++++-- main.c | 4 +- native.c | 83 ++++++---- native_insn.c | 153 +++++++++++++----- program.c | 25 +-- tests/cram/test_basic.t | 4 +- tests/custom/02_runtime/10_global_scope_chain | 32 ++++ tests/custom/native-ucode.sh | 20 ++- types.c | 24 ++- vm.c | 19 ++- vmcore.c | 143 +++++----------- 17 files changed, 568 insertions(+), 229 deletions(-) create mode 100644 cmake/native-strip.cmake create mode 100644 tests/custom/02_runtime/10_global_scope_chain diff --git a/CMakeLists.txt b/CMakeLists.txt index 8576db3b..0d27fec1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -284,10 +284,18 @@ if(LLVM_SUPPORT AND NOT UCODE_WASM) llvm-link-18 llvm-link-17 llvm-link-16) find_program(NATIVE_LLVM_DIS NAMES llvm-dis llvm-dis-21 llvm-dis-20 llvm-dis-19 llvm-dis-18 llvm-dis-17 llvm-dis-16) + find_program(NATIVE_LLVM_AS NAMES llvm-as llvm-as-21 llvm-as-20 llvm-as-19 + llvm-as-18 llvm-as-17 llvm-as-16) set(NATIVE_TARGET "" CACHE STRING "Target triple of the runtime bitcode, empty for the host") + # The triple does not select the CPU or the float ABI of a target, such as + # the soft float of OpenWrt mips_24kc. The runtime bitcode keeps no target + # attributes (see cmake/native-strip.cmake), so a program build passes the + # same flags to clang when it compiles the linked program bitcode. + set(NATIVE_TARGET_FLAGS "" CACHE STRING + "Target CPU and ABI flags of the runtime bitcode in clang syntax, e.g. -march=mips32r2 -msoft-float") - if(NOT NATIVE_CLANG OR NOT NATIVE_LLVM_LINK OR NOT NATIVE_LLVM_DIS) - message(FATAL_ERROR "LLVM_SUPPORT requires clang, llvm-link and llvm-dis") + if(NOT NATIVE_CLANG OR NOT NATIVE_LLVM_LINK OR NOT NATIVE_LLVM_DIS OR NOT NATIVE_LLVM_AS) + message(FATAL_ERROR "LLVM_SUPPORT requires clang, llvm-link, llvm-dis and llvm-as") endif() set(native_bc_flags -O2 -std=gnu99 -fPIE -emit-llvm -c @@ -299,6 +307,11 @@ if(LLVM_SUPPORT AND NOT UCODE_WASM) list(APPEND native_bc_flags --target=${NATIVE_TARGET}) endif() + if(NATIVE_TARGET_FLAGS) + separate_arguments(native_target_flags UNIX_COMMAND "${NATIVE_TARGET_FLAGS}") + list(APPEND native_bc_flags ${native_target_flags}) + endif() + if(CMAKE_SYSROOT) list(APPEND native_bc_flags --sysroot=${CMAKE_SYSROOT}) endif() @@ -338,21 +351,28 @@ if(LLVM_SUPPORT AND NOT UCODE_WASM) endforeach() add_custom_command( - OUTPUT ${CMAKE_BINARY_DIR}/libucode-native.bc - COMMAND ${NATIVE_LLVM_LINK} ${native_bc_files} -o ${CMAKE_BINARY_DIR}/libucode-native.bc - DEPENDS ${native_bc_files} + OUTPUT ${CMAKE_BINARY_DIR}/libucode-native.bc ${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll + COMMAND ${NATIVE_LLVM_LINK} ${native_bc_files} + -o ${CMAKE_BINARY_DIR}/native-bc/linked.bc + COMMAND ${NATIVE_LLVM_DIS} ${CMAKE_BINARY_DIR}/native-bc/linked.bc + -o ${CMAKE_BINARY_DIR}/native-bc/linked.ll + COMMAND ${CMAKE_COMMAND} -DINPUT=${CMAKE_BINARY_DIR}/native-bc/linked.ll + -DOUTPUT=${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-strip.cmake + COMMAND ${NATIVE_LLVM_AS} ${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll + -o ${CMAKE_BINARY_DIR}/libucode-native.bc + DEPENDS ${native_bc_files} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-strip.cmake VERBATIM) # the symbols to keep when the program is linked with opt's internalize # pass: -internalize-public-api-file=libucode-native.api add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/libucode-native.api - COMMAND ${NATIVE_LLVM_DIS} ${CMAKE_BINARY_DIR}/libucode-native.bc - -o ${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll COMMAND ${CMAKE_COMMAND} -DINPUT=${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll -DOUTPUT=${CMAKE_BINARY_DIR}/libucode-native.api -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-api.cmake - DEPENDS ${CMAKE_BINARY_DIR}/libucode-native.bc ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-api.cmake + DEPENDS ${CMAKE_BINARY_DIR}/native-bc/libucode-native.ll + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/native-api.cmake VERBATIM) add_custom_target(ucode-native ALL diff --git a/cmake/native-strip.cmake b/cmake/native-strip.cmake new file mode 100644 index 00000000..1e3dbed3 --- /dev/null +++ b/cmake/native-strip.cmake @@ -0,0 +1,16 @@ +# Removes the target CPU, feature and float ABI attributes from the functions +# of the runtime. LLVM inlines a function only into callers with compatible +# target attributes, and the generated functions of a program have none, so +# with them no helper is inlined. Without them, the code generation of the +# program sets the CPU and the ABI for all functions alike. +# +# The attributes are matched in the textual IR; checked with LLVM 19 only. +# +# Usage: cmake -DINPUT=linked.ll -DOUTPUT=libucode-native.ll -P native-strip.cmake + +file(READ ${INPUT} ir) + +string(REGEX REPLACE " \"(target-cpu|target-features|tune-cpu|use-soft-float)\"=\"[^\"]*\"" "" + ir "${ir}") + +file(WRITE ${OUTPUT} "${ir}") diff --git a/include/ucode/internal/native.h b/include/ucode/internal/native.h index ccc641ac..d81424ca 100644 --- a/include/ucode/internal/native.h +++ b/include/ucode/internal/native.h @@ -222,12 +222,30 @@ uc_native_signals_dispatch(uc_vm_t *vm) #define UC_NATIVE_INLINE #endif +/* Helpers whose body at each call site costs more space than it saves time; + * in ucode-mdns, inlining them grew the code by 25 kB at -Os */ +#define UC_NATIVE_NOINLINE __attribute__((noinline)) + +/* ucv_put() without a call for the values that have no reference count */ +UC_NATIVE_INLINE static inline void +uc_native_unref(uc_value_t *v) +{ + if (ucv_is_counted(v)) + ucv_put(v); +} + /* The functions that generated code calls, see native.c and native_insn.c. * They take the VM from the frame, which saves an argument at each call. */ __hidden void uc_native_enter(uc_native_frame_t *frame, uc_value_t **slots, uc_upvalref_t **cells, uint32_t nslots); +/* the cells of the functions that no closure or export captures from */ +#define UC_NATIVE_NOCELLS 64 + +__hidden extern uc_upvalref_t *const uc_native_nocells[UC_NATIVE_NOCELLS]; + __hidden void uc_native_leave(uc_native_frame_t *frame); +__hidden void uc_native_frame_release(uc_native_frame_t *frame, size_t nused); __hidden bool uc_native_catch(uc_native_frame_t *frame, uint32_t slot); __hidden bool uc_native_safepoint(uc_native_frame_t *frame, uint32_t pc); @@ -316,6 +334,22 @@ __hidden bool uc_native_getk(uc_native_frame_t *f, uint32_t pc, int32_t d, __hidden void uc_native_popn(UC_NATIVE_INSN_ARGS); __hidden bool uc_native_callpop(UC_NATIVE_INSN_ARGS); __hidden void uc_native_slocpop(UC_NATIVE_INSN_ARGS); + +#define UC_NATIVE_LCALL_SLOT 0x7fff0000u +#define UC_NATIVE_COPYJMPZ_POP (1u << 31) + +__hidden bool uc_native_lcall(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_lcallpop(UC_NATIVE_INSN_ARGS); +__hidden bool uc_native_copyjmpz(UC_NATIVE_INSN_ARGS); + +/* each half of the argument of uc_native_load2() is a kind and an index */ +#define UC_NATIVE_LOAD2_KIND_SHIFT 14 +#define UC_NATIVE_LOAD2_INDEX 0x3fff +#define UC_NATIVE_LOAD2_LLOC 0 +#define UC_NATIVE_LOAD2_LUPV 1 +#define UC_NATIVE_LOAD2_CONST 2 + +__hidden void uc_native_load2(UC_NATIVE_INSN_ARGS); __hidden bool uc_native_jnull(UC_NATIVE_INSN_ARGS); __hidden bool uc_native_notjmpz(UC_NATIVE_INSN_ARGS); diff --git a/include/ucode/internal/program.h b/include/ucode/internal/program.h index 856493c9..af31b4c8 100644 --- a/include/ucode/internal/program.h +++ b/include/ucode/internal/program.h @@ -69,8 +69,29 @@ size_t uc_program_function_srcpos(uc_function_t *, size_t); __hidden bool uc_program_function_stmt_bounds(uc_function_t *, size_t, size_t *, size_t *); __hidden void uc_program_function_free(uc_function_t *); +__hidden uc_value_t *uc_program_constant_load(uc_program_t *, size_t); +__hidden uint64_t uc_program_constant_hash_load(uc_program_t *, size_t); __hidden uc_value_t *uc_program_get_constant(uc_program_t *, size_t); -__hidden uint64_t uc_program_get_constant_hash(uc_program_t *, size_t); + +/* The value of constant `idx`, which the program keeps the reference to */ +static inline uc_value_t * +uc_program_peek_constant(uc_program_t *prog, size_t idx) +{ + if (idx < prog->constant_values.count && prog->constant_values.entries[idx]) + return prog->constant_values.entries[idx]; + + return uc_program_constant_load(prog, idx); +} + +/* The hash of the string constant `idx` as an object key */ +static inline uint64_t +uc_program_get_constant_hash(uc_program_t *prog, size_t idx) +{ + if (idx < prog->constant_hashes.count && prog->constant_hashes.entries[idx]) + return prog->constant_hashes.entries[idx]; + + return uc_program_constant_hash_load(prog, idx); +} __hidden ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *); #endif /* UCODE_INTERNAL_PROGRAM_H */ diff --git a/include/ucode/internal/types.h b/include/ucode/internal/types.h index 7c70a1a8..e840b432 100644 --- a/include/ucode/internal/types.h +++ b/include/ucode/internal/types.h @@ -238,10 +238,11 @@ __hidden bool ucv_object_remove(uc_value_t *, const char *, size_t); __hidden uc_value_t *ucv_object_lookup_string(uc_value_t *, uc_value_t *, bool *); __hidden bool ucv_object_insert_string(uc_value_t *, uc_value_t *, uc_value_t *); -/* The hash of the string `key` as an object key, and ucv_key_get() of an - * own key of the object `obj` with that hash, which returns false if `obj` - * is no object or lacks the key */ +/* The hash of the string `key` as an object key, ucv_object_lookup_string() + * with that hash, and ucv_key_get() of an own key of the object `obj` with + * that hash, which returns false if `obj` is no object or lacks the key */ __hidden uint64_t ucv_object_key_hash(uc_value_t *key); +__hidden uc_value_t *ucv_object_lookup_hashed(uc_value_t *, uc_value_t *, uint64_t, bool *); __hidden bool ucv_key_get_hashed(uc_vm_t *vm, uc_value_t *obj, uc_value_t *key, uint64_t hash, uc_value_t **res); diff --git a/include/ucode/internal/vmcore.h b/include/ucode/internal/vmcore.h index b310ba8c..1d0258d7 100644 --- a/include/ucode/internal/vmcore.h +++ b/include/ucode/internal/vmcore.h @@ -81,8 +81,32 @@ __hidden void uc_vm_stack_set(uc_vm_t *vm, size_t offset, uc_value_t *value); __hidden void uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall, size_t nargs); __hidden uc_value_t *uc_vm_callframe_pop(uc_vm_t *vm); -__hidden void uc_vm_run_begin(uc_closure_t *closure, bool keep); -__hidden void uc_vm_run_end(uc_closure_t *closure); +/* The exports of the modules that a program links statically belong to the + * closure of its entry function that runs them, and live as long as that + * closure. An exported function holds the program, so exports that the program + * owned itself would keep the program alive. Inline, as each call of a + * native function begins and ends a run. */ +static inline bool +uc_vm_closure_is_entry(uc_closure_t *closure) +{ + uc_function_t *function = closure->function; + + return function->program->functions.prev == &function->progref; +} + +static inline void +uc_vm_run_begin(uc_closure_t *closure, bool keep) +{ + if (uc_vm_closure_is_entry(closure)) + uc_vector_push(&closure->function->program->runs, { .closure = closure, .keep = keep }); +} + +static inline void +uc_vm_run_end(uc_closure_t *closure) +{ + if (uc_vm_closure_is_entry(closure)) + uc_vector_pop(&closure->function->program->runs); +} __hidden char *uc_vm_callee_expr(uc_vm_t *vm); __hidden void uc_vm_clear_exception(uc_vm_t *vm); @@ -118,10 +142,12 @@ __hidden bool uc_vm_object_spread(uc_vm_t *vm, uc_value_t *dst, uc_value_t *src) __hidden bool uc_vm_spread_push(uc_vm_t *vm, size_t base, size_t nargs, size_t from, size_t spread); __hidden void uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from); -__hidden uc_value_t *uc_vm_global_get(uc_vm_t *vm, uc_value_t *name, bool strict); -__hidden bool uc_vm_global_set(uc_vm_t *vm, uc_value_t *name, uc_value_t *v, bool strict); +/* `hash` is the object key hash of `name`, see uc_program_get_constant_hash() */ +__hidden uc_value_t *uc_vm_global_get(uc_vm_t *vm, uc_value_t *name, uint64_t hash, bool strict); +__hidden bool uc_vm_global_set(uc_vm_t *vm, uc_value_t *name, uint64_t hash, uc_value_t *v, + bool strict); __hidden bool uc_vm_global_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *name, - uc_value_t *inc, bool strict, uc_value_t **res); + uint64_t hash, uc_value_t *inc, bool strict, uc_value_t **res); __hidden const char *uc_vm_main_key(uc_program_t *program, uc_vm_main_key_t key); diff --git a/llvm.c b/llvm.c index 6150b637..2ca91ce5 100644 --- a/llvm.c +++ b/llvm.c @@ -799,7 +799,7 @@ llvm_emit_insn(FILE *out, llvm_func_t *f, llvm_insn_t *in, const char *prefix) case I_RETURN: fputs(" call void @uc_native_return", out); llvm_emit_args(out, f, in, prefix); - fputs("\n br label %leave\n", out); + fputs("\n ret void\n", out); return; case I_CALL: @@ -922,11 +922,12 @@ llvm_emit_fused_call(FILE *out, llvm_func_t *f, size_t i, llvm_insn_t *op, llvm_ fputc('\n', out); } -/* A helper `name` that runs the instruction at `i` up to the JMPZ `jump` and - * returns whether the jump is taken */ +/* A helper `name` that runs the instruction at `i` up to the JMPZ `jump`, + * and up to `last` if the jump is not taken, and returns whether the jump + * is taken */ static void -llvm_emit_fused_jump(FILE *out, llvm_func_t *f, size_t i, llvm_insn_t *jump, uint32_t arg, - const char *name) +llvm_emit_fused_jump(FILE *out, llvm_func_t *f, size_t i, llvm_insn_t *jump, llvm_insn_t *last, + uint32_t arg, const char *name) { llvm_insn_t *first = &f->insns.entries[i]; size_t target = llvm_jump_target(jump); @@ -936,17 +937,75 @@ llvm_emit_fused_jump(FILE *out, llvm_func_t *f, size_t i, llvm_insn_t *jump, uin "i32 %u, ptr null)\n", first->off, name, jump->next, first->depth, arg); fprintf(out, " br i1 %%t%zu, label ", first->off); llvm_emit_branch_to(out, f, jump, target); - fprintf(out, ", label %%L%zu\n", jump->next); + fprintf(out, ", label %%L%zu\n", last->next); llvm_emit_safepoint(out, f, jump, target); } +/* The half of the argument of uc_native_load2() for an LLOC, LUPV or LOAD + * whose index fits */ +static bool +llvm_load2_op(llvm_insn_t *in, uint32_t *op) +{ + uint32_t kind; + + if (!in || in->arg > UC_NATIVE_LOAD2_INDEX) + return false; + + switch (in->insn) { + case I_LLOC: + kind = UC_NATIVE_LOAD2_LLOC; + break; + + case I_LUPV: + kind = UC_NATIVE_LOAD2_LUPV; + break; + + case I_LOAD: + kind = UC_NATIVE_LOAD2_CONST; + break; + + default: + return false; + } + + *op = in->arg | (kind << UC_NATIVE_LOAD2_KIND_SHIFT); + + return true; +} + +/* LLOC, then CALL without spread arguments, then POP or not */ +static size_t +llvm_emit_lcall(FILE *out, llvm_func_t *f, size_t i, const char *prefix) +{ + llvm_insn_t *in = &f->insns.entries[i], *call = llvm_fusable(f, i, 1), *pop; + uint32_t arg; + + if (!llvm_insn_is(call, I_CALL, I_CALL) || call->tail || + (call->arg & UC_NATIVE_LCALL_SLOT) || in->arg > (UC_NATIVE_LCALL_SLOT >> 16)) + return 0; + + arg = call->arg | (in->arg << 16); + pop = llvm_fusable(f, i, 2); + + if (!llvm_insn_is(pop, I_POP, I_POP)) { + llvm_emit_fused_call(out, f, i, call, call, arg, "lcall", true, prefix); + + return 2; + } + + llvm_emit_fused_call(out, f, i, call, pop, arg, "lcallpop", true, prefix); + + return 3; +} + /* Emits the instruction at `i` together with the ones after it as one * helper call where a helper for the sequence exists, and returns the * number of instructions it covers, or 0. */ static size_t llvm_emit_fused(FILE *out, llvm_func_t *f, size_t i, const char *prefix) { - llvm_insn_t *in = &f->insns.entries[i], *next = llvm_fusable(f, i, 1), *jump; + llvm_insn_t *in = &f->insns.entries[i], *next = llvm_fusable(f, i, 1), *jump, *pop; + uint32_t a, b; size_t n; if ((n = llvm_emit_getk(out, f, i)) != 0) @@ -980,13 +1039,28 @@ llvm_emit_fused(FILE *out, llvm_func_t *f, size_t i, const char *prefix) return 2; + case I_LLOC: + if ((n = llvm_emit_lcall(out, f, i, prefix)) != 0) + return n; + + /* fall through */ + + case I_LUPV: + case I_LOAD: + if (!llvm_load2_op(in, &a) || !llvm_load2_op(next, &b)) + return 0; + + llvm_emit_fused_call(out, f, i, in, next, a | (b << 16), "load2", false, prefix); + + return 2; + case I_LNULL: jump = llvm_fusable(f, i, 2); if (!llvm_insn_is(next, I_EQS, I_NES) || !llvm_insn_is(jump, I_JMPZ, I_JMPZ)) return 0; - llvm_emit_fused_jump(out, f, i, jump, next->insn == I_NES, "jnull"); + llvm_emit_fused_jump(out, f, i, jump, jump, next->insn == I_NES, "jnull"); return 3; @@ -994,19 +1068,47 @@ llvm_emit_fused(FILE *out, llvm_func_t *f, size_t i, const char *prefix) if (!llvm_insn_is(next, I_JMPZ, I_JMPZ)) return 0; - llvm_emit_fused_jump(out, f, i, next, 0, "notjmpz"); + llvm_emit_fused_jump(out, f, i, next, next, 0, "notjmpz"); return 2; + case I_COPY: + pop = llvm_fusable(f, i, 2); + + if (!llvm_insn_is(next, I_JMPZ, I_JMPZ) || in->arg >= UC_NATIVE_COPYJMPZ_POP) + return 0; + + if (!llvm_insn_is(pop, I_POP, I_POP)) { + llvm_emit_fused_jump(out, f, i, next, next, in->arg, "copyjmpz"); + + return 2; + } + + llvm_emit_fused_jump(out, f, i, next, pop, in->arg | UC_NATIVE_COPYJMPZ_POP, "copyjmpz"); + + return 3; + default: return 0; } } +/* Whether a closure or an export can capture a slot of the function */ +static bool +llvm_func_captures(llvm_func_t *f) +{ + uc_vector_foreach(&f->insns, in) + if (in->insn == I_CLFN || in->insn == I_ARFN || in->insn == I_EXPORT) + return true; + + return false; +} + static void llvm_emit_function(FILE *out, llvm_func_t *f, size_t unit) { uc_ehranges_t *ranges = &f->fn->chunk.ehranges; + const char *cells = "@uc_native_nocells"; char prefix[48]; llvm_insn_t *in; @@ -1027,11 +1129,16 @@ llvm_emit_function(FILE *out, llvm_func_t *f, size_t unit) fprintf(out, "define internal void @%s(ptr %%vm, ptr %%frame) {\n", prefix); fprintf(out, "entry:\n"); fprintf(out, " %%slots = alloca [%zu x ptr]\n", f->nslots); - fprintf(out, " %%cells = alloca [%zu x ptr]\n", f->nslots); fprintf(out, " store [%zu x ptr] zeroinitializer, ptr %%slots\n", f->nslots); - fprintf(out, " store [%zu x ptr] zeroinitializer, ptr %%cells\n", f->nslots); - fprintf(out, " call void @uc_native_enter(ptr %%frame, ptr %%slots, ptr %%cells, i32 %zu)\n", - f->nslots); + + if (llvm_func_captures(f) || f->nslots > UC_NATIVE_NOCELLS) { + fprintf(out, " %%cells = alloca [%zu x ptr]\n", f->nslots); + fprintf(out, " store [%zu x ptr] zeroinitializer, ptr %%cells\n", f->nslots); + cells = "%cells"; + } + + fprintf(out, " call void @uc_native_enter(ptr %%frame, ptr %%slots, ptr %s, i32 %zu)\n", + cells, f->nslots); fprintf(out, " br label %%L0\n"); for (size_t i = 0, n; i < f->insns.count; i += n) { @@ -1070,6 +1177,7 @@ llvm_emit_declarations(FILE *out) { char name[16]; + fprintf(out, "@uc_native_nocells = external constant [%d x ptr]\n", UC_NATIVE_NOCELLS); fputs("declare void @uc_native_enter(ptr, ptr, ptr, i32)\n" "declare void @uc_native_leave(ptr)\n" "declare zeroext i1 @uc_native_catch(ptr, i32)\n" @@ -1080,6 +1188,10 @@ llvm_emit_declarations(FILE *out) "declare void @uc_native_popn(ptr, i32, i32, i32, ptr)\n" "declare zeroext i1 @uc_native_callpop(ptr, i32, i32, i32, ptr)\n" "declare void @uc_native_slocpop(ptr, i32, i32, i32, ptr)\n" + "declare zeroext i1 @uc_native_lcall(ptr, i32, i32, i32, ptr)\n" + "declare zeroext i1 @uc_native_lcallpop(ptr, i32, i32, i32, ptr)\n" + "declare zeroext i1 @uc_native_copyjmpz(ptr, i32, i32, i32, ptr)\n" + "declare void @uc_native_load2(ptr, i32, i32, i32, ptr)\n" "declare zeroext i1 @uc_native_jnull(ptr, i32, i32, i32, ptr)\n" "declare zeroext i1 @uc_native_notjmpz(ptr, i32, i32, i32, ptr)\n", out); diff --git a/main.c b/main.c index e1c9b72f..e26b40a9 100644 --- a/main.c +++ b/main.c @@ -106,7 +106,9 @@ print_usage(const char *app) " ride interpreter line with ...), dynlink=... (force import from ... to\n" " be treated as shared extensions loaded at runtime), module (build load-\n" " able library), llvm (write LLVM IR of a native program, if built with\n" - " LLVM support), target=... (LLVM target triple of the native program),\n" + " LLVM support; the IR must be compiled for the CPU and float ABI that\n" + " NATIVE_TARGET_FLAGS of the runtime name, as clang with these flags\n" + " does), target=... (LLVM target triple of the native program),\n" " embed=[path=]file (also embed `file` or the scripts below it, which the\n" " program loads as `path` by computed names), embed-template=[path=]file\n" " (the same for templates), debug (embed debug information and source\n" diff --git a/native.c b/native.c index 6ee61f06..8c733904 100644 --- a/native.c +++ b/native.c @@ -34,6 +34,8 @@ #include "ucode/vm.h" #include "ucode/lib.h" +uc_upvalref_t *const uc_native_nocells[UC_NATIVE_NOCELLS]; + static const uc_native_program_t *uc_native_running; static uc_native_frame_t *uc_native_frames; @@ -53,9 +55,7 @@ uc_native_slot_set(uc_native_frame_t *frame, size_t slot, uc_value_t *value) uc_value_t *old = *p; *p = value; - - if (ucv_is_counted(old)) - ucv_put(old); + uc_native_unref(old); } UC_NATIVE_INLINE uc_value_t * @@ -77,9 +77,7 @@ uc_native_slot_clear(uc_native_frame_t *frame, size_t slot) frame->cells[slot] = NULL; } - if (ucv_is_counted(frame->slots[slot])) - ucv_put(frame->slots[slot]); - + uc_native_unref(frame->slots[slot]); frame->slots[slot] = NULL; } @@ -143,7 +141,7 @@ static void uc_native_args_put(uc_value_t **args, size_t nargs) { for (size_t i = 0; i < nargs; i++) { - ucv_put(args[i]); + uc_native_unref(args[i]); args[i] = NULL; } } @@ -158,7 +156,8 @@ uc_native_args_free(uc_value_t **args, size_t nargs) /* Binds the arguments to the slots of the function as uc_vm_call_function() * binds them to the stack: missing arguments are null, a vararg function * gets the excess arguments as an array in its last parameter, excess - * arguments of other functions are dropped. */ + * arguments of other functions are dropped. The entries of the argument + * array are cleared, as they can be slots of the calling frame. */ void uc_native_enter(uc_native_frame_t *frame, uc_value_t **slots, uc_upvalref_t **cells, uint32_t nslots) @@ -175,37 +174,51 @@ uc_native_enter(uc_native_frame_t *frame, uc_value_t **slots, uc_upvalref_t **ce fixed = function->vararg ? nparams - 1 : nparams; - for (i = 0; i < fixed && i < frame->nargs; i++) { + for (i = 0; i < fixed && i < frame->nargs; i++) slots[1 + i] = frame->args[i]; - frame->args[i] = NULL; - } if (function->vararg) { rest = ucv_array_new_length(vm, frame->nargs > fixed ? frame->nargs - fixed : 0); - for (i = fixed; i < frame->nargs; i++) { + for (i = fixed; i < frame->nargs; i++) ucv_array_push(rest, frame->args[i]); - frame->args[i] = NULL; - } slots[nparams] = rest; } + else { + for (i = fixed; i < frame->nargs; i++) + uc_native_unref(frame->args[i]); + } + + for (i = 0; i < frame->nargs; i++) + frame->args[i] = NULL; + + frame->nargs = 0; } +/* Releases the slots of the frame, of which only the first `nused` hold + * values unless a closure captured one of them */ void -uc_native_leave(uc_native_frame_t *frame) +uc_native_frame_release(uc_native_frame_t *frame, size_t nused) { + uc_value_t **slots = frame->slots; + for (size_t i = 0; frame->captured && i < frame->nslots; i++) uc_native_slot_clear(frame, i); - for (size_t i = 0; !frame->captured && i < frame->nslots; i++) - if (ucv_is_counted(frame->slots[i])) - ucv_put(frame->slots[i]); + for (size_t i = 0; !frame->captured && i < nused; i++) + uc_native_unref(slots[i]); frame->slots = NULL; frame->nslots = 0; } +void +uc_native_leave(uc_native_frame_t *frame) +{ + uc_native_frame_release(frame, frame->nslots); +} + /* Unwinds the operand stack of the frame to the slot of the exception * range and puts the exception object there, as uc_vm_handle_exception() * does. An exit is never caught. */ @@ -244,7 +257,7 @@ uc_native_function_info(uc_function_t *function) * that it pushes as uc_vm_call_function() does, so that the stack trace, * `this` lookups of callbacks and the recursion limit see it. Takes the * references to `ctx` and to the arguments, but not the array that holds the - * arguments, which the caller keeps. */ + * arguments, nor the closure, which the caller keeps during the call. */ static uc_value_t * uc_native_invoke_run(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool mcall, uc_value_t **args, size_t nargs, bool keep) @@ -265,15 +278,13 @@ uc_native_invoke_run(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool m uc_value_t *fn; bool owned = false; - if (vm->callframes.count >= 1000) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "Too much recursion"); + if (!uc_vm_call_depth_check(vm, false)) { ucv_put(ctx); uc_native_args_put(args, nargs); return NULL; } - ucv_get(&closure->header); uc_native_frames = &frame; uc_vector_push(&vm->callframes, { @@ -291,14 +302,12 @@ uc_native_invoke_run(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool m info = uc_native_function_info(frame.closure->function); info->entry(vm, &frame); - /* the arguments of a tail call are in an array of the frame */ + /* the entry took the arguments; those of a tail call are in an + * array of the frame, and the frame holds the tail called closure */ if (owned) - uc_native_args_free(frame.args, frame.nargs); - else - uc_native_args_put(frame.args, frame.nargs); + free(frame.args); frame.args = NULL; - frame.nargs = 0; if (!frame.tail.pending || vm->exception.type != EXCEPTION_NONE) break; @@ -309,7 +318,9 @@ uc_native_invoke_run(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool m frame.tail.fn = NULL; uc_vm_run_end(frame.closure); - ucv_put(&frame.closure->header); + + if (owned) + ucv_put(&frame.closure->header); frame.closure = ucv_as_closure(fn); frame.ctx = frame.tail.ctx; @@ -335,14 +346,20 @@ uc_native_invoke_run(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool m uc_vm_run_begin(frame.closure, false); } - ucv_put(frame.tail.fn); - ucv_put(frame.tail.ctx); - uc_native_args_free(frame.tail.args, frame.tail.nargs); + /* a tail call request that an exception left */ + if (frame.tail.fn || frame.tail.ctx || frame.tail.args) { + ucv_put(frame.tail.fn); + ucv_put(frame.tail.ctx); + uc_native_args_free(frame.tail.args, frame.tail.nargs); + } uc_vm_run_end(frame.closure); cf = uc_vector_pop(&vm->callframes); - ucv_put(cf->ctx); - ucv_put(&frame.closure->header); + uc_native_unref(cf->ctx); + + if (owned) + ucv_put(&frame.closure->header); + uc_native_frames = frame.prev; if (vm->exception.type != EXCEPTION_NONE) { diff --git a/native_insn.c b/native_insn.c index 243a5fe0..fc277d23 100644 --- a/native_insn.c +++ b/native_insn.c @@ -49,10 +49,17 @@ uc_native_ref(uc_value_t *v) return ucv_is_counted(v) ? ucv_get(v) : v; } +/* The closure of the frame keeps the program and so its constants alive */ UC_NATIVE_INLINE static uc_value_t * uc_native_constant(uc_native_frame_t *f, size_t idx) { - return uc_program_get_constant(f->closure->function->program, idx); + return uc_program_peek_constant(f->closure->function->program, idx); +} + +UC_NATIVE_INLINE static uint64_t +uc_native_constant_hash(uc_native_frame_t *f, size_t idx) +{ + return uc_program_get_constant_hash(f->closure->function->program, idx); } UC_NATIVE_INLINE static bool @@ -97,7 +104,7 @@ uc_native_upval_store(uc_vm_t *vm, uc_upvalref_t *ref, uc_value_t *val) UC_NATIVE_INLINE static void uc_native_insn_load(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) { - uc_native_store(vm, f, d, uc_native_constant(f, arg)); + uc_native_store(vm, f, d, uc_native_ref(uc_native_constant(f, arg))); } UC_NATIVE_INLINE static void @@ -226,7 +233,6 @@ uc_native_lrexp(UC_NATIVE_INSN_ARGS) uc_native_pc_set(vm, pc); loaded = uc_vm_regexp_load(vm, jstr, &re); - ucv_put(jstr); if (loaded) uc_native_store(vm, f, d, re); @@ -240,13 +246,13 @@ uc_native_lnull(UC_NATIVE_INSN_ARGS) uc_native_slot_set(f, d, NULL); } -void +UC_NATIVE_NOINLINE void uc_native_ltrue(UC_NATIVE_INSN_ARGS) { uc_native_slot_set(f, d, ucv_boolean_new(true)); } -void +UC_NATIVE_NOINLINE void uc_native_lfalse(UC_NATIVE_INSN_ARGS) { uc_native_slot_set(f, d, ucv_boolean_new(false)); @@ -276,8 +282,7 @@ uc_native_lvar(UC_NATIVE_INSN_ARGS) uc_value_t *val; uc_native_pc_set(vm, pc); - val = uc_vm_global_get(vm, name, uc_native_strict(f)); - ucv_put(name); + val = uc_vm_global_get(vm, name, uc_native_constant_hash(f, arg), uc_native_strict(f)); uc_native_store(vm, f, d, val); return uc_native_ok(vm); @@ -324,7 +329,7 @@ uc_native_closure(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg, } } -void +UC_NATIVE_NOINLINE void uc_native_clfn(UC_NATIVE_INSN_ARGS) { uc_vm_t *vm = f->vm; @@ -332,7 +337,7 @@ uc_native_clfn(UC_NATIVE_INSN_ARGS) uc_native_closure(vm, f, d, arg, x, false); } -void +UC_NATIVE_NOINLINE void uc_native_arfn(UC_NATIVE_INSN_ARGS) { uc_vm_t *vm = f->vm; @@ -362,8 +367,8 @@ uc_native_svar(UC_NATIVE_INSN_ARGS) bool stored; uc_native_pc_set(vm, pc); - stored = uc_vm_global_set(vm, name, SLOT(d - 1), uc_native_strict(f)); - ucv_put(name); + stored = uc_vm_global_set(vm, name, uc_native_constant_hash(f, arg), SLOT(d - 1), + uc_native_strict(f)); if (!stored) uc_native_slot_clear(f, d - 1); @@ -429,8 +434,8 @@ uc_native_uvar(UC_NATIVE_INSN_ARGS) bool ok; uc_native_pc_set(vm, pc); - ok = uc_vm_global_update(vm, arg >> 24, name, SLOT(d - 1), uc_native_strict(f), &val); - ucv_put(name); + ok = uc_vm_global_update(vm, arg >> 24, name, uc_native_constant_hash(f, arg & 0x00ffffff), + SLOT(d - 1), uc_native_strict(f), &val); uc_native_slot_clear(f, d - 1); if (ok) @@ -687,7 +692,7 @@ uc_native_comparison(uc_vm_t *vm, uc_native_frame_t *f, uint32_t pc, int32_t d, { return uc_native_binary(f->vm, f, pc, d, uc_vm_value_bitop, insn); } #define UC_NATIVE_COMPARE(name, insn) \ - bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ + UC_NATIVE_NOINLINE bool uc_native_##name(UC_NATIVE_INSN_ARGS) \ { return uc_native_comparison(f->vm, f, pc, d, insn); } /* the order comparisons of loop conditions are inlined; equality is mostly @@ -802,14 +807,15 @@ uc_native_cupv(UC_NATIVE_INSN_ARGS) uc_native_slot_clear(f, d - 1); } -void +UC_NATIVE_NOINLINE void uc_native_return(UC_NATIVE_INSN_ARGS) { ucv_put(f->result); f->result = uc_native_slot_take(f, d - 1); + uc_native_frame_release(f, d - 1); } -/* Pushes the callee of a CALL, its receiver for a method call and its +/* Moves the callee of a CALL, its receiver for a method call and its * arguments, expanding spread arguments, onto the VM stack in the layout that * uc_vm_call_function() builds. Returns the number of arguments, or -1 if a * spread argument is not an array. */ @@ -822,13 +828,13 @@ uc_native_call_push(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg, int32_t fi = d - nargs - 1; if (mcall) - uc_vm_stack_push(vm, ucv_get(SLOT(fi - 1))); + uc_vm_stack_push(vm, uc_native_slot_take(f, fi - 1)); *stackoff = vm->stack.count; - uc_vm_stack_push(vm, ucv_get(SLOT(fi))); + uc_vm_stack_push(vm, uc_native_slot_take(f, fi)); for (i = 0; i < nargs; i++) - uc_vm_stack_push(vm, ucv_get(SLOT(fi + 1 + i))); + uc_vm_stack_push(vm, uc_native_slot_take(f, fi + 1 + i)); if (!nspreads) return nargs; @@ -966,19 +972,24 @@ uc_native_call_direct(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg bool mcall = arg & 0x80000000; size_t nargs = arg & 0xffff, i; int32_t fi = d - nargs - 1, s; - uc_value_t *buf[16], **args = buf, *ctx, *res; - - if (nargs > ARRAY_SIZE(buf)) - args = xalloc(nargs * sizeof(*args)); + /* without cells, the slots of the arguments serve as the array, which + * the callee clears */ + bool in_slots = !f->captured; + uc_value_t *buf[16], **args = in_slots ? &f->slots[fi + 1] : buf, *ctx, *res; ctx = uc_native_call_ctx(f, d, arg); - for (i = 0; i < nargs; i++) - args[i] = uc_native_slot_take(f, fi + 1 + i); + if (!in_slots) { + if (nargs > ARRAY_SIZE(buf)) + args = xalloc(nargs * sizeof(*args)); + + for (i = 0; i < nargs; i++) + args[i] = uc_native_slot_take(f, fi + 1 + i); + } res = uc_native_invoke(vm, ucv_as_closure(SLOT(fi)), ctx, mcall, args, nargs); - if (args != buf) + if (!in_slots && args != buf) free(args); for (s = d - 1; s >= fi - (int32_t)mcall; s--) @@ -1069,7 +1080,7 @@ uc_native_stcall(UC_NATIVE_INSN_ARGS) uc_native_pc_set(vm, pc); for (size_t i = 0; i < nargs; i++) - uc_vm_stack_push(vm, ucv_get(SLOT(base + i))); + uc_vm_stack_push(vm, uc_native_slot_take(f, base + i)); uc_vm_stcall(vm, arg >> 16, nargs); @@ -1234,27 +1245,16 @@ uc_native_dynload(UC_NATIVE_INSN_ARGS) static bool uc_native_getk_own(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t arg) { - uc_program_t *prog = f->closure->function->program; uint32_t k = arg & 0x00ffffff; uc_value_t *key, *res; - bool found; if (ucv_type(SLOT(d - 1)) != UC_OBJECT) return false; - key = uc_program_get_constant(prog, k); + key = uc_native_constant(f, k); - if (ucv_type(key) != UC_STRING) { - ucv_put(key); - - return false; - } - - found = ucv_key_get_hashed(vm, SLOT(d - 1), key, - uc_program_get_constant_hash(prog, k), &res); - ucv_put(key); - - if (!found) + if (ucv_type(key) != UC_STRING || + !ucv_key_get_hashed(vm, SLOT(d - 1), key, uc_native_constant_hash(f, k), &res)) return false; if (arg & UC_NATIVE_GETK_KEEP) { @@ -1321,6 +1321,47 @@ uc_native_callpop(UC_NATIVE_INSN_ARGS) return true; } +/* LLOC of the slot in UC_NATIVE_LCALL_SLOT of `arg`, then CALL without + * spread arguments */ +bool +uc_native_lcall(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_insn_lloc(vm, f, d, (arg & UC_NATIVE_LCALL_SLOT) >> 16); + + return uc_native_call_common(vm, f, pc, d + 1, arg & ~UC_NATIVE_LCALL_SLOT, x, false); +} + +/* LLOC, then CALL as uc_native_lcall(), then POP of its result */ +bool +uc_native_lcallpop(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + if (!uc_native_lcall(f, pc, d, arg, x)) + return false; + + uc_native_insn_pop(vm, f, d + 1 - (arg & 0xffff) - (arg >> 31)); + + return true; +} + +/* COPY, then JMPZ, which test the value `arg` slots below the top without + * taking it, then POP if UC_NATIVE_COPYJMPZ_POP is set and the branch is + * not taken; returns whether the branch is taken. */ +bool +uc_native_copyjmpz(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + bool falsy = !ucv_is_truish(SLOT(d - 1 - (arg & ~UC_NATIVE_COPYJMPZ_POP))); + + if (!falsy && (arg & UC_NATIVE_COPYJMPZ_POP)) + uc_native_insn_pop(vm, f, d); + + return falsy; +} + /* SLOC, then POP */ void uc_native_slocpop(UC_NATIVE_INSN_ARGS) @@ -1331,6 +1372,36 @@ uc_native_slocpop(UC_NATIVE_INSN_ARGS) uc_native_insn_pop(vm, f, d); } +UC_NATIVE_INLINE static void +uc_native_load_kind(uc_vm_t *vm, uc_native_frame_t *f, int32_t d, uint32_t op) +{ + uint32_t idx = op & UC_NATIVE_LOAD2_INDEX; + + switch (op >> UC_NATIVE_LOAD2_KIND_SHIFT) { + case UC_NATIVE_LOAD2_LLOC: + uc_native_insn_lloc(vm, f, d, idx); + break; + + case UC_NATIVE_LOAD2_LUPV: + uc_native_insn_lupv(vm, f, d, idx); + break; + + default: + uc_native_insn_load(vm, f, d, idx); + break; + } +} + +/* Two of LLOC, LUPV and LOAD, the first in the low 16 bits of `arg` */ +void +uc_native_load2(UC_NATIVE_INSN_ARGS) +{ + uc_vm_t *vm = f->vm; + + uc_native_load_kind(vm, f, d, arg & 0xffff); + uc_native_load_kind(vm, f, d + 1, arg >> 16); +} + /* LNULL, which has the empty slot `d` to push to, then EQS, or NES if `arg` * is set, then JMPZ; returns whether the branch is taken. Strict equality * cannot raise. */ diff --git a/program.c b/program.c index cb2b3b68..917617a7 100644 --- a/program.c +++ b/program.c @@ -170,9 +170,9 @@ uc_program_function_free(uc_function_t *func) } /* Constants are immutable, so each one is created once and shared by all its - * uses, instead of once per evaluation. */ + * uses, instead of once per evaluation. The program keeps the reference. */ uc_value_t * -uc_program_get_constant(uc_program_t *prog, size_t idx) +uc_program_constant_load(uc_program_t *prog, size_t idx) { uc_constant_values_t *values = &prog->constant_values; @@ -187,26 +187,31 @@ uc_program_get_constant(uc_program_t *prog, size_t idx) if (!values->entries[idx]) values->entries[idx] = uc_vallist_get(&prog->constants, idx); - return ucv_get(values->entries[idx]); + return values->entries[idx]; +} + +uc_value_t * +uc_program_get_constant(uc_program_t *prog, size_t idx) +{ + return ucv_get(uc_program_peek_constant(prog, idx)); } /* The hash of the string constant `idx` as an object key, computed once */ uint64_t -uc_program_get_constant_hash(uc_program_t *prog, size_t idx) +uc_program_constant_hash_load(uc_program_t *prog, size_t idx) { uc_constant_hashes_t *hashes = &prog->constant_hashes; - uc_value_t *v; + + if (idx >= prog->constants.isize) + return 0; if (idx >= hashes->count) { uc_vector_extend(hashes, prog->constants.isize - hashes->count); hashes->count = prog->constants.isize; } - if (!hashes->entries[idx]) { - v = uc_program_get_constant(prog, idx); - hashes->entries[idx] = ucv_object_key_hash(v); - ucv_put(v); - } + if (!hashes->entries[idx]) + hashes->entries[idx] = ucv_object_key_hash(uc_program_peek_constant(prog, idx)); return hashes->entries[idx]; } diff --git a/tests/cram/test_basic.t b/tests/cram/test_basic.t index aca35e32..d3035562 100644 --- a/tests/cram/test_basic.t +++ b/tests/cram/test_basic.t @@ -71,7 +71,9 @@ check that ucode provides exepected help: ride interpreter line with ...), dynlink=... (force import from ... to be treated as shared extensions loaded at runtime), module (build load- able library), llvm (write LLVM IR of a native program, if built with - LLVM support), target=... (LLVM target triple of the native program), + LLVM support; the IR must be compiled for the CPU and float ABI that + NATIVE_TARGET_FLAGS of the runtime name, as clang with these flags + does), target=... (LLVM target triple of the native program), embed=[path=]file (also embed `file` or the scripts below it, which the program loads as `path` by computed names), embed-template=[path=]file (the same for templates), debug (embed debug information and source diff --git a/tests/custom/02_runtime/10_global_scope_chain b/tests/custom/02_runtime/10_global_scope_chain new file mode 100644 index 00000000..2b93a26f --- /dev/null +++ b/tests/custom/02_runtime/10_global_scope_chain @@ -0,0 +1,32 @@ +Reading, assigning and updating a global variable searches the scope of +the call and then its prototypes: an assignment changes the variable in +the scope that has it, and a new variable goes to the last scope. + +-- Testcase -- +{% + counter = 10; + shadow = "global"; + + function f() { + counter++; + counter += 5; + shadow = shadow + "-scope"; + created = "made"; + + return [ a, k11, counter, shadow ]; + } + + let scope = { a: 1, shadow: "own" }; + + for (let i = 0; i < 12; i++) + scope["k" + i] = i; + + print(call(f, null, scope), "\n"); + print(counter, " ", shadow, " ", created, "\n"); +%} +-- End -- + +-- Expect stdout -- +[ 1, 11, 16, "own-scope" ] +16 global made +-- End -- diff --git a/tests/custom/native-ucode.sh b/tests/custom/native-ucode.sh index c09a25a9..064204ee 100755 --- a/tests/custom/native-ucode.sh +++ b/tests/custom/native-ucode.sh @@ -7,14 +7,16 @@ # (the runtime libucode-native.bc, with libucode-native.api and the # native-abi directory beside it), # NATIVE_LIBS (the libraries it needs), NATIVE_TARGET (the target triple when -# cross compiling), LLVM_LINK, OPT, LLC, CC, LLVM_OPT_LEVEL, and -# NATIVE_RUNNER, a command to run the native executable with, such as -# valgrind or qemu. +# cross compiling), NATIVE_TARGET_FLAGS (the flags the runtime was built with, +# which make clang instead of llc compile the program), LLVM_LINK, OPT, LLC, +# CLANG, CC, LLVM_OPT_LEVEL, and NATIVE_RUNNER, a command to run the native +# executable with, such as valgrind or qemu. ucode=${UCODE:-ucode} runtime=${UCODE_NATIVE:?UCODE_NATIVE names no libucode-native.bc} opt=${OPT:-opt} llc=${LLC:-llc} +clang=${CLANG:-clang} cc=${CC:-cc} level=${LLVM_OPT_LEVEL:-1} libs=${NATIVE_LIBS:--lm -ldl} @@ -103,10 +105,20 @@ else eval "$ucode $compile_args -c,$flags -o \"\$tmp/prog.ll\" \"\$source\"" || exit $? fi +# llc takes the CPU and the float ABI in a syntax of its own +codegen() { + if [ -n "$NATIVE_TARGET_FLAGS" ]; then + $clang ${NATIVE_TARGET:+--target=$NATIVE_TARGET} $NATIVE_TARGET_FLAGS -O$level -fPIC \ + -c "$1" -o "$2" + else + $llc -O$level -relocation-model=pic -filetype=obj "$1" -o "$2" + fi +} + ${LLVM_LINK:-llvm-link} "$tmp/prog.ll" "$runtime" -o "$tmp/all.bc" && $opt -passes="internalize,default" -internalize-public-api-file="${runtime%.bc}.api" \ "$tmp/all.bc" -o "$tmp/prog.bc" && -$llc -O$level -relocation-model=pic -filetype=obj "$tmp/prog.bc" -o "$tmp/prog.o" && +codegen "$tmp/prog.bc" "$tmp/prog.o" && $cc "$tmp/prog.o" $libs -rdynamic -Wl,-rpath,"${runtime%/*}/native-abi" -o "$tmp/prog" || exit 125 eval "$NATIVE_RUNNER \"\$tmp/prog\" $run_args -- \"\$@\"" diff --git a/types.c b/types.c index 5e3cc8bc..9eca7e6b 100644 --- a/types.c +++ b/types.c @@ -1257,6 +1257,9 @@ ucv_object_find(uc_object_t *object, const char *s, size_t len, uc_value_t *kv) uint64_t ucv_object_key_hash(uc_value_t *key) { + if (ucv_type(key) != UC_STRING) + return 0; + return ucv_object_hash_bytes(_ucv_string_get(&key), ucv_string_length(key)); } @@ -1529,14 +1532,16 @@ ucv_object_delete(uc_value_t *uv, const char *key) return ucv_object_remove(uv, key, strlen(key)); } -/* Looks up the key `s` of `len` bytes, with its string value `kv` if given */ +/* Looks up the key `s` of `len` bytes, with its string value `kv` and its + * hash if given */ static uc_value_t * -ucv_object_lookup_key(uc_value_t *uv, const char *s, size_t len, uc_value_t *kv, bool *found) +ucv_object_lookup_key_hashed(uc_value_t *uv, const char *s, size_t len, uc_value_t *kv, + const uint64_t *hash, bool *found) { uc_object_entry_t *e = NULL; if (ucv_type(uv) == UC_OBJECT) - e = ucv_object_find(ucv_as_object(uv), s, len, kv); + e = ucv_object_find_hashed(ucv_as_object(uv), s, len, kv, hash); if (found != NULL) *found = (e != NULL); @@ -1544,6 +1549,12 @@ ucv_object_lookup_key(uc_value_t *uv, const char *s, size_t len, uc_value_t *kv, return e ? e->value : NULL; } +static uc_value_t * +ucv_object_lookup_key(uc_value_t *uv, const char *s, size_t len, uc_value_t *kv, bool *found) +{ + return ucv_object_lookup_key_hashed(uv, s, len, kv, NULL, found); +} + uc_value_t * ucv_object_lookup(uc_value_t *uv, const char *key, size_t len, bool *found) { @@ -1556,6 +1567,13 @@ ucv_object_lookup_string(uc_value_t *uv, uc_value_t *key, bool *found) return ucv_object_lookup_key(uv, ucv_string_get(key), ucv_string_length(key), key, found); } +uc_value_t * +ucv_object_lookup_hashed(uc_value_t *uv, uc_value_t *key, uint64_t hash, bool *found) +{ + return ucv_object_lookup_key_hashed(uv, ucv_string_get(key), ucv_string_length(key), key, + &hash, found); +} + uc_value_t * ucv_object_get(uc_value_t *uv, const char *key, bool *found) { diff --git a/vm.c b/vm.c index 51ce2899..b78eb04c 100644 --- a/vm.c +++ b/vm.c @@ -560,8 +560,10 @@ uc_vm_insn_load_bool(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_load_var(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); - uc_value_t *val = uc_vm_global_get(vm, name, uc_vm_is_strict(vm)); + uc_program_t *prog = uc_vm_current_program(vm); + uc_value_t *name = uc_program_get_constant(prog, vm->arg.u32); + uc_value_t *val = uc_vm_global_get(vm, name, uc_program_get_constant_hash(prog, vm->arg.u32), + uc_vm_is_strict(vm)); ucv_put(name); @@ -661,8 +663,10 @@ static void uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *v = uc_vm_stack_pop(vm); - uc_value_t *name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32); - bool stored = uc_vm_global_set(vm, name, v, uc_vm_is_strict(vm)); + uc_program_t *prog = uc_vm_current_program(vm); + uc_value_t *name = uc_program_get_constant(prog, vm->arg.u32); + bool stored = uc_vm_global_set(vm, name, uc_program_get_constant_hash(prog, vm->arg.u32), v, + uc_vm_is_strict(vm)); ucv_put(name); @@ -734,10 +738,13 @@ static void uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *inc = uc_vm_stack_pop(vm); - uc_value_t *name = uc_program_get_constant(uc_vm_current_program(vm), vm->arg.u32 & 0x00FFFFFF); + uc_program_t *prog = uc_vm_current_program(vm); + size_t idx = vm->arg.u32 & 0x00FFFFFF; + uc_value_t *name = uc_program_get_constant(prog, idx); uc_value_t *val; - if (uc_vm_global_update(vm, vm->arg.u32 >> 24, name, inc, uc_vm_is_strict(vm), &val)) + if (uc_vm_global_update(vm, vm->arg.u32 >> 24, name, uc_program_get_constant_hash(prog, idx), + inc, uc_vm_is_strict(vm), &val)) uc_vm_stack_push(vm, val); ucv_put(name); diff --git a/vmcore.c b/vmcore.c index 1193b3ed..e5b77b9b 100644 --- a/vmcore.c +++ b/vmcore.c @@ -524,32 +524,6 @@ uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall ucv_put(res); } -/* The exports of the modules that a program links statically belong to the - * closure of its entry function that runs them, and live as long as that - * closure. An exported function holds the program, so exports that the program - * owned itself would keep the program alive. */ -static bool -uc_vm_closure_is_entry(uc_closure_t *closure) -{ - uc_function_t *function = closure->function; - - return function->program->functions.prev == &function->progref; -} - -void -uc_vm_run_begin(uc_closure_t *closure, bool keep) -{ - if (uc_vm_closure_is_entry(closure)) - uc_vector_push(&closure->function->program->runs, { .closure = closure, .keep = keep }); -} - -void -uc_vm_run_end(uc_closure_t *closure) -{ - if (uc_vm_closure_is_entry(closure)) - uc_vector_pop(&closure->function->program->runs); -} - /* Returns the expression invoked by the call that the instruction the VM is * about to execute belongs to, or NULL if it cannot be determined. The result * is meant for error messages describing a failed call, and must be @@ -2170,112 +2144,81 @@ uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from) vm->stack.count = base + count; } -uc_value_t * -uc_vm_global_get(uc_vm_t *vm, uc_value_t *name, bool strict) +/* Finds the scope in the chain of the globals that has the string `name` + * with the object key hash `hash`, or the last scope if none has it, which + * fails in strict mode */ +static bool +uc_vm_global_find(uc_vm_t *vm, uc_value_t *name, uint64_t hash, bool strict, + uc_value_t **scope, uc_value_t **val) { - uc_value_t *scope, *next, *val = NULL; + uc_value_t *next; bool found; - scope = vm->globals; - - while (ucv_type(name) == UC_STRING) { - val = ucv_object_get(scope, ucv_string_get(name), &found); + for (*scope = vm->globals; ; *scope = next) { + *val = ucv_object_lookup_hashed(*scope, name, hash, &found); if (found) - break; - - next = ucv_prototype_get(scope); + return true; - if (!next) { - if (strict) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "access to undeclared variable %s", - ucv_string_get(name)); - } + next = ucv_prototype_get(*scope); + if (!next) break; - } - - scope = next; } - return ucv_get(val); -} - -bool -uc_vm_global_set(uc_vm_t *vm, uc_value_t *name, uc_value_t *v, bool strict) -{ - uc_value_t *scope, *next; - bool found; + if (!strict) + return true; - scope = vm->globals; + uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, + "access to undeclared variable %s", + ucv_string_get(name)); - while (ucv_type(name) == UC_STRING) { - ucv_object_get(scope, ucv_string_get(name), &found); + return false; +} - if (found) - break; +uc_value_t * +uc_vm_global_get(uc_vm_t *vm, uc_value_t *name, uint64_t hash, bool strict) +{ + uc_value_t *scope, *val; - next = ucv_prototype_get(scope); + if (ucv_type(name) != UC_STRING || + !uc_vm_global_find(vm, name, hash, strict, &scope, &val)) + return NULL; - if (!next) { - if (strict) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "access to undeclared variable %s", - ucv_string_get(name)); + return ucv_get(val); +} - return false; - } +bool +uc_vm_global_set(uc_vm_t *vm, uc_value_t *name, uint64_t hash, uc_value_t *v, bool strict) +{ + uc_value_t *scope, *val; - break; - } + if (ucv_type(name) != UC_STRING) + return true; - scope = next; - } + if (!uc_vm_global_find(vm, name, hash, strict, &scope, &val)) + return false; - if (scope && ucv_type(name) == UC_STRING) - ucv_object_add(scope, ucv_string_get(name), ucv_get(v)); + if (scope) + ucv_object_insert_string(scope, name, ucv_get(v)); return true; } bool -uc_vm_global_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *name, +uc_vm_global_update(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *name, uint64_t hash, uc_value_t *inc, bool strict, uc_value_t **res) { - uc_value_t *scope, *next, *val = NULL; - bool found; - - scope = vm->globals; + uc_value_t *scope, *val; assert(ucv_type(name) == UC_STRING); - while (true) { - val = ucv_object_get(scope, ucv_string_get(name), &found); - - if (found) - break; - - next = ucv_prototype_get(scope); - - if (!next) { - if (strict) { - uc_vm_raise_exception(vm, EXCEPTION_REFERENCE, - "access to undeclared variable %s", - ucv_string_get(name)); - - return false; - } - - break; - } - - scope = next; - } + if (!uc_vm_global_find(vm, name, hash, strict, &scope, &val)) + return false; *res = uc_vm_value_arith(vm, operation, val, inc); - ucv_object_add(scope, ucv_string_get(name), ucv_get(*res)); + ucv_object_insert_string(scope, name, ucv_get(*res)); return true; } From bc746cc9699fbe75000d66585d5184b344fc3232 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 17:26:42 +0100 Subject: [PATCH 10/11] native: run scripts that a program does not contain in the interpreter A native program could not load a script that it does not contain, such as a plugin that it finds by a computed name, or code that loadstring() compiles. Such a script is now compiled by the ucode CLI, which the program runs as a child process with the settings of the load, and the program loads the bytecode that it writes. The runtime contains the interpreter and the bytecode loader for it, which add about 48 kB of code. The CLI is `ucode` from PATH, or the one that --ucode-compiler names. Native and interpreted code share the VM and call each other: a native call of a bytecode closure enters the interpreter, and the interpreter runs a native closure through the native runtime instead of pushing a bytecode frame for it. The custom tests of loadstring() and loadfile() now pass with native programs; only the cases that run the program as the ucode CLI or expect the instruction trace of the interpreter remain skipped. Signed-off-by: John Crispin --- CMakeLists.txt | 4 +- bytecode.c | 18 ++ compiler.c | 20 +- include/ucode/internal/program.h | 3 + include/ucode/internal/vmcore.h | 6 + native.c | 250 ++++++++++++++++++ native_insn.c | 29 +- tests/cram/native_build.t | 36 ++- .../02_runtime/11_runtime_compiled_calls | 71 +++++ tests/custom/native-ucode.sh | 4 + tests/custom/native-unsupported | 24 -- vm.c | 8 + vmcore.c | 2 +- 13 files changed, 401 insertions(+), 74 deletions(-) create mode 100644 tests/custom/02_runtime/11_runtime_compiled_calls diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d27fec1..48c37b1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -318,8 +318,10 @@ if(LLVM_SUPPORT AND NOT UCODE_WASM) set(native_bc_files) + # vm.c and bytecode.c run the scripts that a native program does not contain, + # which the ucode CLI compiles when the program loads them foreach(src lib.c vmcore.c chunk.c vallist.c source.c types.c json.c program.c platform.c - cli.c native.c native_insn.c lexer.c) + cli.c native.c native_insn.c lexer.c vm.c bytecode.c) get_filename_component(name ${src} NAME_WE) set(bc ${CMAKE_BINARY_DIR}/native-bc/${name}.bc) diff --git a/bytecode.c b/bytecode.c index ced6090a..f86b0e18 100644 --- a/bytecode.c +++ b/bytecode.c @@ -888,3 +888,21 @@ uc_program_load(uc_source_t *input, char **errp) return NULL; } + +uc_program_t * +uc_program_load_entry(uc_source_t *source, char **errp) +{ + uc_program_t *prog; + + prog = uc_program_load(source, errp); + + if (prog && !uc_program_entry(prog)) { + if (errp) + xasprintf(errp, "Program file contains no entry function\n"); + + ucv_put(&prog->header); + prog = NULL; + } + + return prog; +} diff --git a/compiler.c b/compiler.c index fa91dca9..295c9b16 100644 --- a/compiler.c +++ b/compiler.c @@ -5055,24 +5055,6 @@ uc_compile_from_source(uc_parse_config_t *config, uc_source_t *source, uc_progra #endif } -static uc_program_t * -uc_compile_from_bytecode(uc_parse_config_t *config, uc_source_t *source, char **errp) -{ - uc_program_t *prog; - - prog = uc_program_load(source, errp); - - if (prog && !uc_program_entry(prog)) { - if (errp) - xasprintf(errp, "Program file contains no entry function\n"); - - ucv_put(&prog->header); - prog = NULL; - } - - return prog; -} - uc_program_t * uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp) { @@ -5087,7 +5069,7 @@ uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp) break; case UC_SOURCE_TYPE_PRECOMPILED: - prog = uc_compile_from_bytecode(config, source, errp); + prog = uc_program_load_entry(source, errp); break; case UC_SOURCE_TYPE_UNAVAILABLE: diff --git a/include/ucode/internal/program.h b/include/ucode/internal/program.h index af31b4c8..ac2df0b0 100644 --- a/include/ucode/internal/program.h +++ b/include/ucode/internal/program.h @@ -54,6 +54,9 @@ uc_program_require_version(uc_program_t *prog, uint8_t version) __hidden uc_function_t *uc_program_entry(uc_program_t *); +/* uc_program_load() of a program, which fails if it has no entry function */ +__hidden uc_program_t *uc_program_load_entry(uc_source_t *source, char **errp); + /* prefix of the registry keys under which a VM holds the closures that * uc_vm_execute() runs; the address of the program follows */ #define UC_PROGRAM_MAIN_KEY "vm.main." diff --git a/include/ucode/internal/vmcore.h b/include/ucode/internal/vmcore.h index 1d0258d7..9ad9a365 100644 --- a/include/ucode/internal/vmcore.h +++ b/include/ucode/internal/vmcore.h @@ -173,4 +173,10 @@ __hidden bool uc_vm_call_depth_check(uc_vm_t *vm, bool tail); __hidden bool uc_vm_call_dispatch(uc_vm_t *vm, uc_value_t **ctx, uc_value_t **fno, bool mcall, size_t nargs); __hidden void uc_vm_call_args_adjust(uc_vm_t *vm, uc_function_t *function, size_t nargs); +/* Runs the closure that uc_vm_call_dispatch() returned, with its arguments + * on the stack above `stackoff`; takes the references to `ctx` and to the + * closure */ +__hidden void uc_vm_closure_call(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, + bool mcall, size_t nargs, size_t stackoff); + #endif /* UCODE_INTERNAL_VMCORE_H */ diff --git a/native.c b/native.c index 8c733904..f9ed11ca 100644 --- a/native.c +++ b/native.c @@ -21,6 +21,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include "ucode/internal/native.h" #include "ucode/internal/program.h" @@ -672,6 +678,240 @@ uc_native_bundle_files(uc_vm_t *vm, size_t nargs) return list; } +uc_declare_vector(uc_native_argv_t, char *); + +static const char *uc_native_compiler = "ucode"; + +/* Reads the output and the errors of the compiler until it closes both, as + * it can fill either pipe while the other one is read */ +static void +uc_native_compiler_read(int fds[2], uc_stringbuf_t *bufs[2]) +{ + struct pollfd p[2] = { { .fd = fds[0], .events = POLLIN }, { .fd = fds[1], .events = POLLIN } }; + char chunk[4096]; + ssize_t n; + + while (p[0].fd >= 0 || p[1].fd >= 0) { + n = poll(p, 2, -1); + + if (n < 0 && errno == EINTR) + continue; + + if (n < 0) + return; + + for (size_t i = 0; i < 2; i++) { + if (p[i].fd < 0 || !p[i].revents) + continue; + + n = read(p[i].fd, chunk, sizeof(chunk)); + + if (n > 0) + uc_stringbuf_add(bufs[i], chunk, n); + else if (n == 0 || errno != EINTR) + p[i].fd = -1; + } + } +} + +/* Runs the compiler with `argv` and `in`, if not -1, as its standard input, + * and returns its exit status, or -1 with errno set if it did not start */ +static int +uc_native_compiler_run(char **argv, int in, uc_stringbuf_t *out, uc_stringbuf_t *err) +{ + uc_stringbuf_t *bufs[2] = { out, err }; + posix_spawn_file_actions_t fa; + int po[2], pe[2], status = -1, rv; + pid_t pid; + + if (pipe2(po, O_CLOEXEC)) + return -1; + + if (pipe2(pe, O_CLOEXEC)) { + close(po[0]); + close(po[1]); + + return -1; + } + + posix_spawn_file_actions_init(&fa); + + if (in >= 0) + posix_spawn_file_actions_adddup2(&fa, in, STDIN_FILENO); + + posix_spawn_file_actions_adddup2(&fa, po[1], STDOUT_FILENO); + posix_spawn_file_actions_adddup2(&fa, pe[1], STDERR_FILENO); + rv = posix_spawnp(&pid, argv[0], &fa, NULL, argv, environ); + posix_spawn_file_actions_destroy(&fa); + close(po[1]); + close(pe[1]); + + if (rv == 0) { + uc_native_compiler_read((int[]){ po[0], pe[0] }, bufs); + + while (waitpid(pid, &status, 0) < 0 && errno == EINTR) + ; + } + + close(po[0]); + close(pe[0]); + + if (rv) { + errno = rv; + + return -1; + } + + return WIFEXITED(status) ? WEXITSTATUS(status) : -1; +} + +/* The ucode CLI arguments that compile `input` as uc_compile() would with + * the settings of `config` */ +static void +uc_native_compiler_args(uc_native_argv_t *args, uc_parse_config_t *config, const char *input) +{ + uc_stringbuf_t flags = { 0 }; + + uc_vector_push(args, xstrdup(uc_native_compiler)); + + uc_stringbuf_append(&flags, "-cno-interp"); + + if (config->compile_module) + uc_stringbuf_append(&flags, ",module"); + + uc_vector_foreach(&config->force_dynlink_list, name) + uc_stringbuf_printf(&flags, ",dynlink=%s", *name); + + uc_vector_push(args, xstrdup(flags.buf)); + uc_stringbuf_reset(&flags); + + if (config->raw_mode) { + uc_vector_push(args, xstrdup("-R")); + } + else { + uc_stringbuf_printf(&flags, "-T%s%s%s", config->lstrip_blocks ? "" : "no-lstrip", + (!config->lstrip_blocks && !config->trim_blocks) ? "," : "", + config->trim_blocks ? "" : "no-rtrim"); + uc_vector_push(args, xstrdup(flags.buf)); + } + + free(flags.buf); + + if (config->strict_declarations) + uc_vector_push(args, xstrdup("-S")); + + uc_vector_foreach(&config->module_search_path, pattern) { + uc_vector_push(args, xstrdup("-L")); + uc_vector_push(args, xstrdup(*pattern)); + } + + uc_vector_push(args, xstrdup("-o")); + uc_vector_push(args, xstrdup("-")); + uc_vector_push(args, xstrdup(input)); + uc_vector_push(args, NULL); +} + +/* A source string reaches the compiler as its standard input, which it names + * [stdin]; the program gets the name of the string back, so that its errors + * and traces read as in the interpreter. */ +static void +uc_native_compiled_rename(uc_program_t *prog, uc_source_t *source, size_t len) +{ + uc_source_t *old, *new; + + for (size_t i = 0; i < prog->sources.count; i++) { + old = prog->sources.entries[i]; + + if (strcmp(old->filename, "[stdin]")) + continue; + + new = uc_source_new_buffer(source->filename, xalloc(len), len); + memcpy(new->buffer, source->buffer, len); + uc_source_runpath_set(new, old->runpath); + new->lineinfo = old->lineinfo; + memset(&old->lineinfo, 0, sizeof(old->lineinfo)); + + uc_source_put(old); + prog->sources.entries[i] = new; + } +} + +/* Compiles a script that the program does not contain with the ucode CLI, + * which runs in its own process, and loads the bytecode that it writes, which + * the interpreter runs. */ +static uc_program_t * +uc_native_compile(uc_parse_config_t *config, uc_source_t *source, char **errp) +{ + uc_stringbuf_t out = { 0 }, err = { 0 }; + uc_native_argv_t args = { 0 }; + uc_program_t *prog = NULL; + uc_source_t *bytecode; + size_t len = 0; + int in = -1, rv; + + switch (uc_source_begin(source)) { + case UC_SOURCE_TYPE_PRECOMPILED: + return uc_program_load_entry(source, errp); + + case UC_SOURCE_TYPE_UNAVAILABLE: + xasprintf(errp, "Unable to open source file %s: %s\n", source->filename, strerror(errno)); + + return NULL; + + default: + break; + } + + if (source->buffer) { + fseek(source->fp, 0, SEEK_END); + len = ftell(source->fp); + in = memfd_create("ucode-source", MFD_CLOEXEC); + + if (in < 0 || write(in, source->buffer, len) != (ssize_t)len || lseek(in, 0, SEEK_SET)) { + xasprintf(errp, "Unable to pass the source to %s: %s\n", uc_native_compiler, + strerror(errno)); + + if (in >= 0) + close(in); + + return NULL; + } + } + + uc_native_compiler_args(&args, config, source->buffer ? "-" : source->filename); + rv = uc_native_compiler_run(args.entries, in, &out, &err); + + if (in >= 0) + close(in); + + for (size_t i = 0; i < args.count; i++) + free(args.entries[i]); + + uc_vector_clear(&args); + + if (rv < 0) { + xasprintf(errp, "Unable to run %s: %s\n", uc_native_compiler, strerror(errno)); + } + else if (rv > 0 || !out.buf) { + xasprintf(errp, "%s", err.buf ? err.buf : "Compilation failed\n"); + } + else { + bytecode = uc_source_new_buffer(source->filename, out.buf, uc_stringbuf_length(&out)); + out.buf = NULL; + uc_source_runpath_set(bytecode, source->runpath); + prog = uc_program_load_entry(bytecode, errp); + uc_source_put(bytecode); + } + + if (prog && source->buffer) + uc_native_compiled_rename(prog, source, len); + + free(out.buf); + free(err.buf); + + return prog; +} + /* Native program options, accepted before the first program argument. They * use long names, so that the short options of the program itself pass * through to ARGV unchanged. */ @@ -685,6 +925,7 @@ static const struct { { "--ucode-library", 'l' }, { "--ucode-search-path", 'L' }, { "--ucode-gc", 'g' }, + { "--ucode-compiler", 'C' }, }; static char @@ -731,6 +972,10 @@ uc_native_options_apply(uc_vm_t *vm, int argc, char **argv, uc_value_t *args) case 'g': vm->gc_interval = atoi(argv[i + 1]); break; + + case 'C': + uc_native_compiler = argv[i + 1]; + break; } } @@ -817,7 +1062,11 @@ uc_native_abi_load(void) int uc_native_main(int argc, char **argv, const uc_native_program_t *np) { + /* the settings of the ucode CLI, which apply to the scripts that the + * program compiles at runtime */ uc_parse_config_t config = { + .lstrip_blocks = true, + .trim_blocks = true, .raw_mode = !(np->units[0].flags & UC_NATIVE_UNIT_TEMPLATE), .setup_signal_handlers = true }; @@ -841,6 +1090,7 @@ uc_native_main(int argc, char **argv, const uc_native_program_t *np) uc_native_running = np; uc_vm_native_call_set(uc_native_vm_call); uc_loader_units_set(&uc_native_loader_units); + uc_loader_compile_set(uc_native_compile); /* the search path is part of the configuration the VM starts with; as in * the CLI, the given entries come before the default ones */ diff --git a/native_insn.c b/native_insn.c index fc277d23..9383c116 100644 --- a/native_insn.c +++ b/native_insn.c @@ -882,6 +882,7 @@ uc_native_call_stack(uc_vm_t *vm, uc_native_frame_t *f, uc_value_t *ctx, bool mc uc_value_t *fno = ucv_get(vm->stack.entries[stackoff]); uc_closure_t *closure; uc_value_t **args; + bool bytecode; *res = NULL; @@ -899,9 +900,16 @@ uc_native_call_stack(uc_vm_t *vm, uc_native_frame_t *f, uc_value_t *ctx, bool mc return false; } - /* a C function ran, and its frame pop left the result in place of the - * callee and the receiver */ - if (!fno) { + closure = fno ? ucv_as_closure(fno) : NULL; + bytecode = closure && !closure->function->native; + + /* the interpreter, if the program links it, runs a bytecode closure on + * the stack, and its frame pop leaves the result in place of the callee + * and the receiver, as a C function that already ran does */ + if (bytecode) + uc_vm_closure_call(vm, closure, ctx, mcall, nargs, stackoff); + + if (!closure || bytecode) { if (vm->stack.count > base) *res = uc_vm_stack_pop(vm); @@ -910,24 +918,9 @@ uc_native_call_stack(uc_vm_t *vm, uc_native_frame_t *f, uc_value_t *ctx, bool mc return uc_native_ok(vm); } - closure = ucv_as_closure(fno); args = uc_native_stack_args(vm, nargs); uc_native_stack_unwind(vm, base); - if (!closure->function->native) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, - "bytecode closures cannot execute in the native runtime"); - ucv_put(ctx); - ucv_put(fno); - - for (size_t i = 0; i < nargs; i++) - ucv_put(args[i]); - - free(args); - - return false; - } - if (tail) { f->tail.fn = fno; f->tail.ctx = ctx; diff --git a/tests/cram/native_build.t b/tests/cram/native_build.t index 0e781f28..d418cbc7 100644 --- a/tests/cram/native_build.t +++ b/tests/cram/native_build.t @@ -32,9 +32,10 @@ a native program behaves as the interpreter, also when it fails: 610 [ "-D", "x" ] 42 [254] -it contains neither the interpreter, the compiler nor the bytecode loader: +it contains the interpreter and the bytecode loader for the scripts that it +does not contain, but not the compiler: - $ nm hello | grep -E ' (uc_vm_execute|uc_vm_execute_chunk|uc_compile_from_source|uc_lexer_init|uc_program_load)$' + $ nm hello | grep -E ' (uc_compile_from_source|uc_lexer_init)$' [1] without the debug flag, a native program carries no debug information and @@ -111,7 +112,8 @@ the path the program loads them by: helper a script that fails to compile fails to load as in the interpreter, and -source code compiled at runtime is not supported: +source code that the program compiles at runtime is compiled by the ucode +CLI, which PATH finds, and runs in the interpreter: $ echo 'let = 1;' > broken.uc $ echo 'try { include("broken.uc"); } catch (e) { print(e.message, "\n"); }' > load.uc @@ -126,11 +128,23 @@ source code compiled at runtime is not supported: $ echo 'print(loadstring("return 6 * 7;")(), "\n");' > dynamic.uc $ native dynamic "" dynamic.uc && ./dynamic - Runtime error: Unable to compile source string: - - | Source code compilation not supported - - In function #1 at offset 19 of dynamic.uc - - - [254] + 42 + +a binding that depends on libucode gets the library in the native-abi +directory, which the program finds through its RUNPATH and which contains +no interpreter. Without it, the program warns, as the binding loads the +interpreter library: + + $ cat > binding.uc <<'EOF' + > require("debug"); + > let maps = split(require("fs").readfile("/proc/self/maps"), "\n"); + > let libs = uniq(map(filter(maps, l => index(l, "libucode.so") >= 0), l => match(l, /[^ ]*$/)[0])); + > print(length(libs), " ", index(libs[0], "/native-abi/") >= 0, "\n"); + > EOF + $ native binding "" binding.uc + $ ./binding --ucode-search-path "$BUILD_BIN_DIR/*.so" + 1 true + $ RUNPATH="$PWD/none" native nostub "" binding.uc + $ ./nostub --ucode-search-path "$BUILD_BIN_DIR/*.so" + libucode.so.[0-9]+ not found, bindings that depend on it load the interpreter library (re) + 1 false diff --git a/tests/custom/02_runtime/11_runtime_compiled_calls b/tests/custom/02_runtime/11_runtime_compiled_calls new file mode 100644 index 00000000..c3405185 --- /dev/null +++ b/tests/custom/02_runtime/11_runtime_compiled_calls @@ -0,0 +1,71 @@ +Code that is compiled at runtime calls the functions of the program and is +called by them, with arguments, `this`, closures and exceptions in both +directions. A native program runs such code in the interpreter. + +-- Testcase -- +{% + let counter = 0; + + function bump(n) { + counter += n; + + return counter; + } + + let plugin = loadstring(` + return { + name: "plugin", + map: function(list, fn) { + let out = []; + + for (let v in list) + push(out, fn(v)); + + return out; + }, + who: function() { + return this.name; + }, + adder: function(base) { + return (x) => base + x; + }, + fail: function(msg) { + die("plugin: " + msg); + }, + guard: function(fn) { + try { + fn(); + } + catch (e) { + return "caught " + e.message; + } + } + }; + `, { raw_mode: true })(); + + print(plugin.map([ 1, 2, 3 ], (v) => v * 10), "\n"); + print(plugin.map([ 1, 2 ], bump), " ", counter, "\n"); + print(plugin.who(), "\n"); + print(plugin.adder(40)(2), "\n"); + + try { + plugin.fail("boom"); + } + catch (e) { + print("caught ", e.message, "\n"); + } + + print(plugin.guard(() => die("program: bang")), "\n"); + print(sort([ 3, 1, 2 ], loadstring("return (a, b) => a - b;", { raw_mode: true })()), "\n"); +%} +-- End -- + +-- Expect stdout -- +[ 10, 20, 30 ] +[ 1, 3 ] 3 +plugin +42 +caught plugin: boom +caught program: bang +[ 1, 2, 3 ] +-- End -- diff --git a/tests/custom/native-ucode.sh b/tests/custom/native-ucode.sh index 064204ee..3028b0eb 100755 --- a/tests/custom/native-ucode.sh +++ b/tests/custom/native-ucode.sh @@ -32,6 +32,10 @@ quote() { add_compile() { compile_args="$compile_args $(quote "$1")"; } add_run() { run_args="$run_args $(quote "$1")"; } +# scripts that the program does not contain are compiled by the CLI of the +# build, not by one that PATH may find +add_run --ucode-compiler; add_run "$ucode" + # Modes that do not run a program, or that native programs do not provide, # are left to the interpreter. for arg in "$@"; do diff --git a/tests/custom/native-unsupported b/tests/custom/native-unsupported index a8b3c759..9bbb8945 100644 --- a/tests/custom/native-unsupported +++ b/tests/custom/native-unsupported @@ -1,33 +1,9 @@ # Test cases that native programs cannot pass, read through UCODE_TEST_SKIP # by the custom-native test. Format: category/test case-number reason -00_syntax/30_call_error_callee 3 compiles source at runtime with loadstring() -03_stdlib/36_render 6 compiles source at runtime with loadstring() 03_stdlib/39_trace 1 expects the instruction trace of the interpreter -03_stdlib/40_proto 8 loads the debug module, which needs vm.c -03_stdlib/40_proto 9 loads the debug module, which needs vm.c -03_stdlib/61_loadstring 1 compiles source at runtime with loadstring() -03_stdlib/61_loadstring 2 compiles source at runtime with loadstring() -03_stdlib/61_loadstring 3 compiles source at runtime with loadstring() -03_stdlib/61_loadstring 4 compiles source at runtime with loadstring() -03_stdlib/61_loadstring 5 compiles source at runtime with loadstring() -03_stdlib/61_loadstring 6 compiles source at runtime with loadstring() -03_stdlib/61_loadstring 7 compiles source at runtime with loadstring() -03_stdlib/62_loadfile 1 compiles source at runtime with loadfile() -03_stdlib/62_loadfile 2 compiles source at runtime with loadfile() -03_stdlib/62_loadfile 3 compiles source at runtime with loadfile() -03_stdlib/62_loadfile 4 compiles source at runtime with loadfile() -03_stdlib/62_loadfile 5 compiles source at runtime with loadfile() -03_stdlib/62_loadfile 6 compiles source at runtime with loadfile() 03_stdlib/62_loadfile 7 runs /proc/self/exe as the ucode CLI -04_modules/19_dynamic_import_module_exception 1 compiles source at runtime with loadstring() -04_modules/19_source_file_closed 1 compiles source at runtime with loadfile() -04_modules/20_dynamic_import_srcpos 1 compiles source at runtime with loadstring() 04_modules/20_repeated_source_compile 1 runs /proc/self/exe as the ucode CLI -04_modules/21_changed_source_compile 1 compiles source at runtime with loadfile() -04_modules/21_dynamic_import_callframe_realloc 1 compiles source at runtime with loadstring() 04_modules/22_unseekable_source_compile 1 runs /proc/self/exe as the ucode CLI -04_modules/23_dynamic_import_failed 1 compiles source at runtime with loadstring() -04_modules/24_load_in_module_body 1 compiles source at runtime with loadfile() 04_modules/26_export_lifetime 1 runs /proc/self/exe as the ucode CLI 04_modules/27_precompiled_module_exports 1 runs /proc/self/exe as the ucode CLI diff --git a/vm.c b/vm.c index b78eb04c..029efe09 100644 --- a/vm.c +++ b/vm.c @@ -260,6 +260,14 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, closure = ucv_as_closure(fno); + /* a native closure runs at once and leaves its result on the stack, as a + * C function does, also in a tail call, which a RETURN follows */ + if (closure->function->native) { + uc_vm_closure_call(vm, closure, ctx, mcall, nargs, stackoff); + + return true; + } + uc_vm_call_args_adjust(vm, closure->function, nargs); /* tail call: reuse the current call frame instead of pushing a new one. The diff --git a/vmcore.c b/vmcore.c index e5b77b9b..455a504d 100644 --- a/vmcore.c +++ b/vmcore.c @@ -2716,7 +2716,7 @@ uc_vm_call_args_adjust(uc_vm_t *vm, uc_function_t *function, size_t nargs) } } -static void +void uc_vm_closure_call(uc_vm_t *vm, uc_closure_t *closure, uc_value_t *ctx, bool mcall, size_t nargs, size_t stackoff) { From fc205fc30cb19774f2e5d3fb674b338b221d17ee Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 17:26:42 +0100 Subject: [PATCH 11/11] tests: compare native programs with the interpreter in more cases The 180 cases in tests/custom/19_native cover values, iteration, spread arguments, regular expressions, require(), include(), render(), stack traces and the command line. The interpreter output is the expectation, so the custom test runs them in the interpreter and the custom-native test runs them as native programs. The interpreter runs no source with the dynlink flag, so the 15 cases of deferred imports are in native_build.t. They compare the output of a native program with that of the interpreter, which runs bytecode compiled with the same flag, and run the native program without its source files. Signed-off-by: John Crispin --- tests/cram/native_build.t | 138 +++++ tests/custom/19_native/01_values | 767 ++++++++++++++++++++++++++++ tests/custom/19_native/02_iteration | 566 ++++++++++++++++++++ tests/custom/19_native/03_spread | 272 ++++++++++ tests/custom/19_native/04_regex | 410 +++++++++++++++ tests/custom/19_native/05_require | 378 ++++++++++++++ tests/custom/19_native/06_include | 588 +++++++++++++++++++++ tests/custom/19_native/07_render | 375 ++++++++++++++ tests/custom/19_native/08_traces | 239 +++++++++ tests/custom/19_native/09_cli | 152 ++++++ 10 files changed, 3885 insertions(+) create mode 100644 tests/custom/19_native/01_values create mode 100644 tests/custom/19_native/02_iteration create mode 100644 tests/custom/19_native/03_spread create mode 100644 tests/custom/19_native/04_regex create mode 100644 tests/custom/19_native/05_require create mode 100644 tests/custom/19_native/06_include create mode 100644 tests/custom/19_native/07_render create mode 100644 tests/custom/19_native/08_traces create mode 100644 tests/custom/19_native/09_cli diff --git a/tests/cram/native_build.t b/tests/cram/native_build.t index d418cbc7..0ab19cf4 100644 --- a/tests/cram/native_build.t +++ b/tests/cram/native_build.t @@ -148,3 +148,141 @@ interpreter library: $ ./nostub --ucode-search-path "$BUILD_BIN_DIR/*.so" libucode.so.[0-9]+ not found, bindings that depend on it load the interpreter library (re) 1 false + +the dynlink flag defers imports to the run time as in the interpreter; the +program embeds the modules and runs without their source files. dyn() +compares the output with that of the interpreter, which runs bytecode with +the same flag, as the interpreter runs no source with it: + + $ dyn() { + > flags=; for n in $1; do flags="$flags,dynlink=$n"; done + > native ./native "$flags" -L "$PWD/*.uc" main.uc && + > if [ -f loader.uc ]; then + > "$BUILD_BIN_DIR/ucode" -L "$PWD/*.uc" -c,no-interp$flags -o loader.ucb loader.uc && + > mv loader.ucb loader.uc + > fi && + > "$BUILD_BIN_DIR/ucode" -L "$PWD/*.uc" -c,no-interp$flags -o ref.ucb main.uc && + > "$BUILD_BIN_DIR/ucode" -L "$PWD/*.uc" ref.ucb > ref.out && + > mkdir hidden && mv *.uc hidden/ && + > ./native --ucode-search-path "$PWD/*.uc" > native.out && + > cmp ref.out native.out && cat native.out && echo + > } + $ loader='import * as ns from "dep";return ns;' + +named imports are live: + + $ mkdir dyn1 && cd dyn1 + $ printf '%s' 'import {n,inc} from "dep";print(n," ",inc()," ",n);' > main.uc + $ printf '%s' 'export let n=1;export function inc(){gc();return ++n;}' > dep.uc + $ dyn dep + 1 2 2 + $ cd .. + +a default, a namespace and a static import of the module: + + $ mkdir dyn2 && cd dyn2 + $ printf '%s' 'import d,* as ns from "dep";print(d(),ns.n," ",ns.inc()," ",ns.n);' > main.uc + $ printf '%s' 'import {x} from "./base.uc";export let n=x;export function inc(){gc();return ++n;}export default ()=>n;' > dep.uc + $ printf '%s' 'export let x=10;' > base.uc + $ dyn dep + 1010 11 11 + $ cd .. + +require() after and before an import of the same module: + + $ mkdir dyn3 && cd dyn3 + $ printf '%s' 'import * as x from "dep";print(require("dep").n," ",x.n);' > main.uc + $ printf '%s' 'export let n=42;' > dep.uc + $ dyn dep + 42 42 + $ cd .. + $ mkdir dyn4 && cd dyn4 + $ printf '%s' 'let x=require("dep");import {value} from "dep";print(value," ",x.value);' > main.uc + $ printf '%s' 'print("once ");return {value:7};' > dep.uc + $ dyn dep + once 7 7 + $ cd .. + $ mkdir dyn5 && cd dyn5 + $ printf '%s' 'import * as x from "dep";print(x," ",require("dep"));' > main.uc + $ printf '%s' 'print("once ");let value=7;' > dep.uc + $ dyn dep + once { } { } + $ cd .. + $ mkdir dyn6 && cd dyn6 + $ printf '%s' 'import * as x from "dep"; print(x);' > main.uc + $ printf '%s' 'let value=1;' > dep.uc + $ dyn dep + { } + $ cd .. + +a reload keeps the cells of the first load, and a failed load is not cached: + + $ mkdir dyn7 && cd dyn7 + $ printf '%s' 'let a=require("loader");delete modules.loader;delete modules.dep;let b=require("loader");gc();print(a.inc()," ",a.n," ",b.n," ",b.inc());' > main.uc + $ printf '%s' "$loader" > loader.uc + $ printf '%s' 'export let n=0;export function inc(){gc();return ++n;}' > dep.uc + $ dyn dep + 1 1 0 1 + $ cd .. + $ mkdir dyn8 && cd dyn8 + $ printf '%s' 'global.tries=0;try{require("loader");}catch(e){print(e.message," ");}print(exists(modules,"dep")," ",require("loader").n);' > main.uc + $ printf '%s' "$loader" > loader.uc + $ printf '%s' 'if(++tries==1)die("retry");export let n=tries;' > dep.uc + $ dyn dep + retry false 2 + $ cd .. + +a namespace is read only, and a module in the registry is not loaded: + + $ mkdir dyn9 && cd dyn9 + $ printf '%s' 'import * as ns from "dep";try{ns.n=4;}catch(e){print(e.type);}print(ns.n);' > main.uc + $ printf '%s' 'export let n=3;' > dep.uc + $ dyn dep + Type error3 + $ cd .. + $ mkdir dyn10 && cd dyn10 + $ printf '%s' 'modules.dep={n:8};import {n} from "dep";print(n);' > main.uc + $ printf '%s' 'die("must not execute");export let n=0;' > dep.uc + $ dyn dep + 8 + $ cd .. + +a deferred module can defer an import of its own: + + $ mkdir dyn11 && cd dyn11 + $ printf '%s' 'import {sum} from "dep";print(sum());' > main.uc + $ printf '%s' 'import {n,inc} from "other";export function sum(){return inc()+n;}' > dep.uc + $ printf '%s' 'export let n=10;export function inc(){gc();return ++n;}' > other.uc + $ dyn "dep other" + 22 + $ cd .. + +errors of deferred imports: + + $ mkdir dyn12 && cd dyn12 + $ printf '%s' 'try{require("loader");}catch(e){print(e.type);}print(exists(modules,"dep"));' > main.uc + $ printf '%s' "$loader" > loader.uc + $ printf '%s' 'return {value:7};' > dep.uc + $ dyn dep + Runtime errorfalse + $ cd .. + $ mkdir dyn13 && cd dyn13 + $ printf '%s' 'try{require("loader");}catch(e){print(e.type);}' > main.uc + $ printf '%s' 'import {missing} from "dep";return missing;' > loader.uc + $ printf '%s' 'export let n=1;' > dep.uc + $ dyn dep + Reference error + $ cd .. + $ mkdir dyn14 && cd dyn14 + $ printf '%s' 'try{require("dep");}catch(e){print(e.type);}import {n} from "dep";print(n);' > main.uc + $ printf '%s' 'export let n=42;' > dep.uc + $ dyn dep + Runtime error42 + $ cd .. + $ mkdir dyn15 && cd dyn15 + $ printf '%s' 'try{require("dep");}catch(e){print(e.type);}print(require("loader").n);' > main.uc + $ printf '%s' "$loader" > loader.uc + $ printf '%s' 'export let n=42;' > dep.uc + $ dyn dep + Runtime error42 + $ cd .. diff --git a/tests/custom/19_native/01_values b/tests/custom/19_native/01_values new file mode 100644 index 00000000..00b78bbb --- /dev/null +++ b/tests/custom/19_native/01_values @@ -0,0 +1,767 @@ +Numeric updates. + +-- Testcase -- +let x="12";global.y="12";let o={z:"12"};function f(){return ++x;}print(f()," ",++global.y," ",++o.z," ",x+=1," ",global.y+=1); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +13 13 13 14 14 +-- End (no-eol) -- + + +Object array spread. + +-- Testcase -- +print({...{a:1},...[2,3]});for(let x in [null,4,"bad"])try{let o={...x};}catch(e){print(e.type," ",e.message);}try{let a=[...4];}catch(e){print(e.type," ",e.message);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +{ "a": 1, "0": 2, "1": 3 }Type error Value (null) is not iterableType error Value (4) is not iterableType error Value (bad) is not iterableType error (4) is not iterable +-- End (no-eol) -- + + +Entry tail gc. + +-- Testcase -- +function f(){gc();print("alive");} return f(); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +alive +-- End (no-eol) -- + + +Closure identity. + +-- Testcase -- +function make(){return function(){return 1;};} print(make()===make()); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +false +-- End (no-eol) -- + + +Catch. + +-- Testcase -- +function f(){ let a={}; a.self=a; die("broken"); } try { f(); } catch(e) { print(e.type," ",e.message,"\n"); } try {null.x;} catch(e) {print(e.type," ",e.message,"\n");} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Error broken +Reference error left-hand side expression is null +-- End -- + + +Catch nested. + +-- Testcase -- +try {try {die("nested");} catch(e) {die(e);}} catch(e) {print(e,"\n");} print("alive\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +nested +alive +-- End -- + + +Gc callback. + +-- Testcase -- +let a={x:1}; let values=map([2,3],function(x){gc();a.x+=x;return a;}); gc(); print(values[0]===a," ",a.x,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +true 6 +-- End -- + + +Gc unwind. + +-- Testcase -- +let callbacks=[]; for(let i=0;i<100;i++) {let a={x:i};a.self=a;try {push(callbacks,()=>a.x);die("x");}catch(e){gc();}} print(callbacks[99](),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +99 +-- End -- + + +Loop gc. + +-- Testcase -- +gc("start",1000);for(let i=0;i<5000;i++){let a={};a.self=a;}print(gc("count")<1100,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +true +-- End -- + + +Prototype. + +-- Testcase -- +let a={}; proto(a,{__get__(key){gc();return "value:"+key;}}); print(a.x,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +value:x +-- End -- + + +Builtin alias. + +-- Testcase -- +let a=[]; let f=push; f(a,4); let g=pop; print(g(a),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +4 +-- End -- + + +Builtin join. + +-- Testcase -- +let a=[]; let f=true ? push : pop; f(a,4); print(a,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 4 ] +-- End -- + + +Builtin nested. + +-- Testcase -- +let a=[]; print(length(a)," ",push(a,pop([1,2]))," ",a,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +0 2 [ 2 ] +-- End -- + + +Bitwise coercion. + +-- Testcase -- +print(3.3<<4.1," ",120.3&54.3," ",~0," ",~10.4," ",~-1,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +48 48 18446744073709551615 18446744073709551605 0 +-- End -- + + +Array indices. + +-- Testcase -- +let a=[1,2,3]; a[-1]=9; print(a[-1]," ",a[1.0]," ",a[true]," ",a[null]," ",a["01"],"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +9 2 2 +-- End -- + + +Json numbers. + +-- Testcase -- +print([1.0,NaN,Infinity,-Infinity]," ","x"+1.0,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 1.0, "NaN", 1e309, -1e309 ] x1 +-- End -- + + +Arithmetic matrix. + +-- Testcase -- +let xs=[-9223372036854775808,-2,-1,0,1,2,9223372036854775807,9223372036854775808,18446744073709551615]; for(let i=0;i 10,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +4 4.5 0.25 false true +-- End -- + + +Coercion. + +-- Testcase -- +print("12"*2," ",null+1," ",true+3," ","abc"*2," ",+"0x20"," ",-"9223372036854775808","\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +24 1 4 NaN 32 -9223372036854775808 +-- End -- + + +Strings. + +-- Testcase -- +let a="hello"; print(a+" world"," ",length(a)," ","a"<"b"," ","x"+null,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +hello world 5 true xnull +-- End -- + + +Branches. + +-- Testcase -- +let a=0; for(let i=0;i<1000;i++){ if(i%3) a+=i; else a-=i; } print(a,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +165834 +-- End -- + + +Short circuit. + +-- Testcase -- +let n=0; print(false && ++n," ",true || ++n," ",null ?? ++n," ",n,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +false true 1 1 +-- End -- + + +Optional. + +-- Testcase -- +let f=null; print(f?.(1)," ",f?.x," ",({x:7})?.x,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- + 7 +-- End -- + + +Array. + +-- Testcase -- +let a=[1,2,3]; a[1]=9; a[5]=7; print(a," ",pop(a)," ",length(a)," ",push(a,4,5),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 1, 9, 3, null, null, 4, 5 ] 7 5 5 +-- End -- + + +Objects. + +-- Testcase -- +let o={x:1,y:2}; o.x+=4; o.z="ok"; print(o," ",delete o.y," ",keys(o)," ",o.missing,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +{ "x": 5, "z": "ok" } true [ "x", "z" ] +-- End -- + + +Spread. + +-- Testcase -- +let a=[1,2]; let b=[0,...a,3]; let o={x:1}; print(b," ",{...o,y:2},"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 0, 1, 2, 3 ] { "x": 1, "y": 2 } +-- End -- + + +Function. + +-- Testcase -- +function add(a,b) { return a+b; } print(add(3,4)," ",add(1)," ",add(1,2,3),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +7 1 3 +-- End -- + + +Varargs. + +-- Testcase -- +function count(x,...args) { return x+length(args); } print(count(4,1,2)," ",count(4),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +6 4 +-- End -- + + +Closure. + +-- Testcase -- +function make(x) { return function(y) { x+=y; return x; }; } let f=make(10); print(f(2)," ",f(5),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +12 17 +-- End -- + + +Capture forward. + +-- Testcase -- +function outer(x) { return function() { return () => ++x; }; } let f=outer(7)(); print(f()," ",f(),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +8 9 +-- End -- + + +Capture loop. + +-- Testcase -- +let a=[]; for(let i=0;i<3;i++){ let x=i; push(a,()=>x); } print(a[0]()," ",a[1]()," ",a[2](),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +0 1 2 +-- End -- + + +Method. + +-- Testcase -- +let o={x:3, inc(n) { this.x+=n; return this.x; }}; print(o.inc(4)," ",o.x,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +7 7 +-- End -- + + +Arrow this. + +-- Testcase -- +let f=()=>this.x; let o={x:9, g() { return f(); }}; print(o.g(),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +9 +-- End -- + + +Tail. + +-- Testcase -- +function count(n,acc) { if(!n) return acc; return count(n-1,acc+1); } print(count(100000,0),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +100000 +-- End -- + + +Mutual tail. + +-- Testcase -- +let even,odd; even=(n)=>{ if(!n) return true; return odd(n-1); }; odd=(n)=>{ if(!n) return false; return even(n-1); }; print(even(100000)," ",odd(100001),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +true true +-- End -- + + +Cycles. + +-- Testcase -- +let a={}; a.self=a; let b=[]; push(b,b); print(a.self===a," ",b[0]===b,"\n"); a=null; b=null; gc(); print("collected\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +true true +collected +-- End -- + + +Cycle live. + +-- Testcase -- +let a={}, b={}; a.b=b; b.a=a; b=null; gc(); print(a.b.a===a,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +true +-- End -- + + +Many objects. + +-- Testcase -- +let a=[]; for(let i=0;i<10000;i++) push(a,{}); gc(); print(length(a),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +10000 +-- End -- + + +Many records. + +-- Testcase -- +let a=[]; for(let i=0;i<10000;i++) push(a,{x:i,y:i+1}); print(length(a)," ",a[9999].y,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +10000 10000 +-- End -- + + +Runtime error cleanup call. + +-- Testcase -- +let a={}; a.self=a; a(); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect stderr -- +Type error: `a` is not a function, got { "self": null } instead +In line 1, byte 23: + + `let a={}; a.self=a; a();` + ~~^~ + + +-- End -- + +-- Expect exitcode -- +254 +-- End -- + + +Runtime error cleanup property. + +-- Testcase -- +let a=[{}]; a[0]=null; print(a[0].x); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect stderr -- +Reference error: left-hand side expression is null +In line 1, byte 35: + + `let a=[{}]; a[0]=null; print(a[0].x);` + ~~~~~~~~~~~^~~ + + +-- End -- + +-- Expect exitcode -- +254 +-- End -- diff --git a/tests/custom/19_native/02_iteration b/tests/custom/19_native/02_iteration new file mode 100644 index 00000000..719bde1c --- /dev/null +++ b/tests/custom/19_native/02_iteration @@ -0,0 +1,566 @@ +Append after last. + +-- Testcase -- +let o={a:1},seen=[]; + for(let k in o){push(seen,k);o.b=2;}print(seen," ",o); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "a", "b" ] { "a": 1, "b": 2 } +-- End (no-eol) -- + + +Array delete and splice. + +-- Testcase -- +let a=[1,2,3,4],seen=[]; + for(let i,v in a){push(seen,[i,v]);if(i==0)splice(a,1,1,8,9); + if(i==2)try{delete a[3];}catch(e){print(e.type," ");}} + print(seen," ",a); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Reference error [ [ 0, 1 ], [ 1, 8 ], [ 2, 9 ], [ 3, 3 ], [ 4, 4 ] ] [ 1, 8, 9, 3, 4 ] +-- End (no-eol) -- + + +Array mutations. + +-- Testcase -- +let a=[1,2],seen=[]; + for(let i,v in a){push(seen,[i,v]);if(i==0){push(a,3,4);shift(a);}gc();} + print(seen," ");a=[1,2,3];seen=[]; + for(let i,v in a){push(seen,[i,v]);if(i==0){pop(a);pop(a);}}print(seen); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ 0, 1 ], [ 1, 3 ], [ 2, 4 ] ] [ [ 0, 1 ] ] +-- End (no-eol) -- + + +Arrays. + +-- Testcase -- +let a=[10,null,false,"x"];a[6]=42; + for(let v in a) print(v,";");print("\n"); + for(let k,v in a) print(type(k),":",k,"=",v,";");print("\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +10;;false;x;;;42; +int:0=10;int:1=;int:2=false;int:3=x;int:4=;int:5=;int:6=42; +-- End -- + + +Automatic gc and cycles. + +-- Testcase -- +gc("start",1);let o={a:1,b:2},kept=[]; + for(let k,v in o){let item={k,v};item.self=item;push(kept,()=>item);} + o=null;gc();print(kept[0]().k,kept[1]().v); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +a2 +-- End (no-eol) -- + + +Break closes iterator. + +-- Testcase -- +let o={a:1,b:2}; + for(let i=0;i<100;i++){for(let k in o){gc();break;}o["x"+i]=i;} + for(let k in o)delete o[k];gc();print(o); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +{ } +-- End (no-eol) -- + + +Break continue nested. + +-- Testcase -- +let seen=[]; + for(let k,v in {a:1,b:2,c:3}) { + for(let x in [1,2,3]){if(x==1)continue;push(seen,k+x);break;} + if(v==2)continue;push(seen,v);if(v==3)break; + }print(seen); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "a2", 1, "b2", "c2", 3 ] +-- End (no-eol) -- + + +Captured loop variables. + +-- Testcase -- +let callbacks=[]; + for(let k,v in {a:1,b:2,c:3}){let snapshot=k;push(callbacks,()=>[k,v,snapshot]);} + gc();for(let f in callbacks)print(f(),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "c", 3, "a" ] +[ "c", 3, "b" ] +[ "c", 3, "c" ] +-- End -- + + +Catch inside loop. + +-- Testcase -- +let seen=[]; + for(let k,v in {a:1,b:2,c:3}){try{if(v==2)die(k);gc();push(seen,k);} + catch(e){push(seen,e.message);continue;}}print(seen); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "a", "b", "c" ] +-- End (no-eol) -- + + +Delete and reinsert. + +-- Testcase -- +let o={a:1,b:2,c:3};let seen=[]; + for(let k,v in o){push(seen,[k,v]);if(k=="a"){delete o.b;o.b=9;}} + print(seen); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "a", 1 ], [ "c", 3 ], [ "b", 9 ] ] +-- End (no-eol) -- + + +Delete pending and current. + +-- Testcase -- +let o={a:1,b:2,c:3,d:4}; + for(let k,v in o){print(k,v);if(k=="a")delete o.b;delete o[k];gc();}print(o); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +a1c3d4{ } +-- End (no-eol) -- + + +Empty and noniterable. + +-- Testcase -- +let n=0; + for(let item in [null,true,9,1.5,"abc",function(){},[],{}]) { + for(let k in item)n++;for(let k,v in item)n++; + }print(n); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +0 +-- End (no-eol) -- + + +Exception closes iterator. + +-- Testcase -- +let o={a:1,b:2}; + for(let i=0;i<50;i++){try{for(let k,v in o){let c={};c.self=c;gc();die("stop");}} + catch(e){o["x"+i]=i;}} + let count=0;for(let k in o)count++;gc();print(count); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +52 +-- End (no-eol) -- + + +Exit closes iterator. + +-- Testcase -- +for(let k,v in {a:1,b:2}){gc();print(k,v);exit(7);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +a1 +-- End (no-eol) -- + +-- Expect exitcode -- +7 +-- End -- + + +Global and upvalue targets. + +-- Testcase -- +for(k,v in {a:1,b:2})print(k,v);print(k,v," "); + function outer(){let key,value;function run(){for(key,value in [3,4])print(key,value);} + run();print(key,value);}outer(); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +a1b2b2 031414 +-- End (no-eol) -- + + +Growth and rehash. + +-- Testcase -- +let o={a:1,b:2},seen=[]; + for(let k,v in o){push(seen,[k,v]);if(k=="a")for(let i=0;i<80;i++)o["x"+i]=i;gc();} + print(length(seen)," ",seen[0]," ",seen[81]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +82 [ "a", 1 ] [ "x79", 79 ] +-- End (no-eol) -- + + +Iterable evaluated once. + +-- Testcase -- +let calls=0,o={a:{v:1},b:{v:2}}; + function get(){calls++;return o;} + for(let k,v in get()){o=null;gc();print(k,v.v);} + print(" ",calls); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +a1b2 1 +-- End (no-eol) -- + + +Iteration in bundled sources. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let run=require("worker");run(); + include("part.uc",{entries:{x:1,y:2}}); +-- End (no-eol) -- + +-- File worker.uc -- +return function(){for(let i,v in [3,4])print(i,v);}; +-- End (no-eol) -- + +-- File part.uc -- +for(let k,v in entries)print(k," ",k in entries," ",v); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +0314x true 1y true 2 +-- End (no-eol) -- + + +Membership binary strings. + +-- Testcase -- +let a="x\u0000a",b="x\u0000b"; + print([a in [a],b in [a],"x" in [a],a in {x:1}]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, false, false, false ] +-- End (no-eol) -- + + +Membership no key coercion. + +-- Testcase -- +let o={"1":true,"false":1,"[ ]":2}; + print(["1" in o,1 in o,false in o,[] in o,"x" in null,"x" in true,"a" in "abc"]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, false, false, false, false, false, false ] +-- End (no-eol) -- + + +Membership numbers. + +-- Testcase -- +print([NaN in [NaN],NaN in [1],Infinity in [Infinity], + -0.0 in [0.0],0 in [0.0],18446744073709551615 in [18446744073709551615], + -1 in [18446744073709551615],9223372036854775808 in [9223372036854775808]]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, false, true, true, false, true, false, true ] +-- End (no-eol) -- + + +Membership prototypes. + +-- Testcase -- +let calls=0,o={own:null}; + proto(o,{inherited:9,__get__(k){calls++;return 42;}});let a=[7];proto(a,{alias:1}); + print(["own" in o,"inherited" in o,"virtual" in o,7 in a,0 in a,"0" in a,"alias" in a]," ",calls); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, true, false, true, false, false, true ] 0 +-- End (no-eol) -- + + +Membership types and identity. + +-- Testcase -- +let o={},a=[],f=function(){}; + print([o in [o],{} in [o],a in [a],[] in [a],f in [f],function(){} in [f], + null in [null],false in [0],true in [1],1 in [1.0],"1" in [1],1 in [1]]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ true, false, true, false, true, false, true, false, false, false, false, true ] +-- End (no-eol) -- + + +Nested iterators same object. + +-- Testcase -- +let o={a:1,b:2},outer=[],inner=[]; + for(let k in o){push(outer,k);if(k=="a")for(let j in o){push(inner,j); + if(j=="a"){for(let i=0;i<40;i++)o["x"+i]=i;delete o.b;gc();}}} + print(outer,"\n",inner); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "a", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31", "x32", "x33", "x34", "x35", "x36", "x37", "x38", "x39" ] +[ "a", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31", "x32", "x33", "x34", "x35", "x36", "x37", "x38", "x39" ] +-- End (no-eol) -- + + +Objects. + +-- Testcase -- +let o={a:1,b:null,"":false,"9":9};proto(o,{inherited:7}); + for(let k in o)print(type(k),":",k,";");print("\n"); + for(let k,v in o)print(k,"=",v,";");print("\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +string:a;string:b;string:;string:9; +a=1;b=;=false;9=9; +-- End -- + + +Return closes iterator. + +-- Testcase -- +let o={a:1,b:2}; + function first(){for(let k,v in o){gc();return ()=>[k,v];}} + let f=first();for(let i=0;i<80;i++)o["x"+i]=i;gc();print(f()); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "a", 1 ] +-- End (no-eol) -- + + +Static export values. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import * as ns from './exports.uc'; + for(let k,v in ns){gc();print(k,":",type(v)," "); + if(k=="n"){ns.inc();print(v,"/",ns.n," ");} + if(k=="inc")print(v()," ");} + for(let v in values(ns)){gc();print(type(v)," ");} + for(let k,v in {...ns}){gc();print(k,":",type(v)," ");} + print("n" in ns); +-- End (no-eol) -- + +-- File exports.uc -- +export let n=1;export function inc(){gc();return ++n;} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +n:int 1/2 inc:function 3 int function n:int inc:function true +-- End (no-eol) -- + + +Tail return closes iterator. + +-- Testcase -- +let o={a:1,b:2}; + function last(){gc();for(let i=0;i<60;i++)o["x"+i]=i;return length(o);} + function first(){for(let k in o)return last();}print(first()); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +62 +-- End (no-eol) -- + + +Iterator results are no literals. + +-- Testcase -- +-- End -- + +-- File main.uc -- +for(let name,fn in {safe:x=>x})print(fn("trap")); +-- End (no-eol) -- + +-- File trap.uc -- +this is invalid source! +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +trap +-- End (no-eol) -- + + +Include glob style loop. + +-- Testcase -- +-- End -- + +-- File main.uc -- +for(let path in ["a.uc","b.uc"])include(path); +-- End (no-eol) -- + +-- File a.uc -- +print("A"); +-- End (no-eol) -- + +-- File b.uc -- +print("B"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +AB +-- End (no-eol) -- diff --git a/tests/custom/19_native/03_spread b/tests/custom/19_native/03_spread new file mode 100644 index 00000000..4b664a96 --- /dev/null +++ b/tests/custom/19_native/03_spread @@ -0,0 +1,272 @@ +Builtin. + +-- Testcase -- +let a=[];push(...[a,1],...[2,3]);let f=push;f(a,...[4]);print(...a);print(sprintf(...["%d:%s",7,"x"])); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +12347:x +-- End (no-eol) -- + + +Callable object. + +-- Testcase -- +let o={n:2};proto(o,{__call__(...v){gc();return this.n+v[0]+v[1];}});print(o(...[3,4])); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +9 +-- End (no-eol) -- + + +Captured identity. + +-- Testcase -- +let a={};function f(...v){gc();print(v[0]===a,v[0]===v[1]);return ()=>v;}let keep=f(...[a,a]);a=null;gc();print(keep()[0]===keep()[1]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +truetruetrue +-- End (no-eol) -- + + +Error conversion gc. + +-- Testcase -- +let o={};proto(o,{tostring(){gc();return "custom";}});try{print(...[{}],...o);}catch(e){print(e.type,e.message);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Type error(custom) is not iterable +-- End (no-eol) -- + + +Error order. + +-- Testcase -- +let n=0;try{print(...null,++n);}catch(e){print(n," ",e.type);}print(" ok"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +1 Type error ok +-- End (no-eol) -- + + +Evaluation order. + +-- Testcase -- +let a=[1],seen=[];function mutate(){push(a,2);push(seen,"mutate");return 3;}function f(...v){print(v,seen);}f(...a,mutate()); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 1, 2, 3 ][ "mutate" ] +-- End (no-eol) -- + + +Exception cleanup. + +-- Testcase -- +gc("start",1);for(let i=0;i<50;i++){let o={};o.self=o;try{((...v)=>die("x"))(...[o],...[o]);}catch(e){gc();}}print("ok"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +ok +-- End (no-eol) -- + + +Fixed varargs. + +-- Testcase -- +function f(a,b,...rest){print(a,b,rest);}f(...[]);f(...[1]);f(...[1,2,3,4]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ ]1[ ]12[ 3, 4 ] +-- End (no-eol) -- + + +Invalid. + +-- Testcase -- +for(let bad in [null,1,true,"ab",{},/x/])try{print(...bad);}catch(e){print(e.type,":",e.message,";");} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Type error:(null) is not iterable;Type error:(1) is not iterable;Type error:(true) is not iterable;Type error:(ab) is not iterable;Type error:({ }) is not iterable;Type error:(/x/) is not iterable; +-- End (no-eol) -- + + +Method. + +-- Testcase -- +let o={n:10,f(a,b){gc();return this.n+a+b;}};print(o.f(...[1,2])); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +13 +-- End (no-eol) -- + + +Mixed. + +-- Testcase -- +function f(...a){print(a);}f(0,...[1,2],3,...[],...[4,null],5);f(...[]);f(...[1],...[2]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 0, 1, 2, 3, 4, null, 5 ][ ][ 1, 2 ] +-- End (no-eol) -- + + +Mutual tail method. + +-- Testcase -- +let o={n:3,a(i,...v){if(i)return this.b(...[i-1],...v);return [this.n,v];},b(i,...v){return this.a(...[i],...v);}};print(o.a(...[2000,1,2])); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 3, [ 1, 2 ] ] +-- End (no-eol) -- + + +Optional. + +-- Testcase -- +let n=0,f=null,o={};f?.(...[++n]);o.f?.(...[++n]);print(n); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +0 +-- End (no-eol) -- + + +Return callback. + +-- Testcase -- +let f=(...a)=>map(a,x=>{gc();return x+1;});print(f(...[1,2,3])); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 2, 3, 4 ] +-- End (no-eol) -- + + +Sparse large. + +-- Testcase -- +let a=[1];a[20]=7;function f(...v){gc();print(length(v),v[1],v[20]);}f(...a); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +217 +-- End (no-eol) -- + + +Tail. + +-- Testcase -- +function f(n,...a){if(n)return f(...[n-1],...a);gc();return a;}print(f(...[10000,1,2,3])); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ 1, 2, 3 ] +-- End (no-eol) -- + + +Bundles and export cells. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import * as ns from './values.uc'; + print(...values(ns));let f=require("f");print(f(...[1,2]));include(...["part.uc",{a:[3,4]}]); +-- End (no-eol) -- + +-- File values.uc -- +export let a=1,b=2; +-- End (no-eol) -- + +-- File f.uc -- +return (...a)=>a; +-- End (no-eol) -- + +-- File part.uc -- +print(...a); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +12[ 1, 2 ]34 +-- End (no-eol) -- diff --git a/tests/custom/19_native/04_regex b/tests/custom/19_native/04_regex new file mode 100644 index 00000000..1f4209c4 --- /dev/null +++ b/tests/custom/19_native/04_regex @@ -0,0 +1,410 @@ +Builtin override. + +-- Testcase -- +regexp=function(){die("overridden");}; + print(match("hello",/HELLO/i)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "hello" ] +-- End (no-eol) -- + + +Bundled errors and retry. + +-- Testcase -- +-- End -- + +-- File main.uc -- +modules.count=0; + for(let i=0;i<2;i++)try{require("bad");}catch(e){print(e.type," ");} + try{include("bad-include.uc");}catch(e){print(e.type," ");} + print(modules.count," ",exists(modules,"bad")," ",match("x",/x/)); +-- End (no-eol) -- + +-- File bad.uc -- +modules.count++;return /(/; +-- End (no-eol) -- + +-- File bad-include.uc -- +let re=/(/; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +Syntax error Syntax error Syntax error 2 false [ "x" ] +-- End (no-eol) -- + + +Bundled sources. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import {re} from './static.uc'; + print(match("AAA",re)," ");let r=require("outer"); + print(match("bbb",r)," ");include("included.uc"); +-- End (no-eol) -- + +-- File static.uc -- +export let re=/a+/i; +-- End (no-eol) -- + +-- File outer.uc -- +let marker="separate constant table";return require("inner"); +-- End (no-eol) -- + +-- File inner.uc -- +return /b+/; +-- End (no-eol) -- + +-- File included.uc -- +print(match("c1 c2",/c[0-9]/g)); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +[ "AAA" ] [ "bbb" ] [ [ "c1" ], [ "c2" ] ] +-- End (no-eol) -- + + +Closure and tail lifetime. + +-- Testcase -- +function make(){let re=/a+/g;return ()=>re;} + function again(n){if(n)return again(n-1);return /b+/;} + let f=make();gc();print(match("aa ba",f())," ",match("bb",again(1000))); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "aa" ], [ "a" ] ] [ "bb" ] +-- End (no-eol) -- + + +Embedded nul. + +-- Testcase -- +print([/ab\u0000cd/,/\u0000/]); + print(match("abXX",/ab\u0000cd/)," ",match("",/\u0000/)," ", + match("ab\u0000cd",/cd/)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "/ab/", "//" ][ "ab" ] [ "" ] +-- End (no-eol) -- + + +Errors in callback. + +-- Testcase -- +try{replace("ab",/a/,function(){gc();return /(/;});} + catch(e){print(e.type,":",e.message," ");}print(match("ab",/ab/)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Syntax error:Unmatched ( or \( [ "ab" ] +-- End (no-eol) -- + + +Escaping and macros. + +-- Testcase -- +print([/test1 \/ test2/,/\x31\n\.\a\b\c\u2600\\/, + / \b \B [\b \B] /,/ \< \> [\< \>] /,/ \d \D [\d \D] /, + / \s \S [\s \S] /,/ \w \W [\w \W] /]); + print(match("x_12",/\w+/)," ",match("123",/\d+/)," ",match("☀",/\u2600/)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "/test1 / test2/", "/1\n\\.\u0007\\bc☀\\\\/", "/ \\b \\B [\b B] /", "/ \\< \\> [< >] /", "/ [[:digit:]] [^[:digit:]] [d D] /", "/ [[:space:]] [^[:space:]] [s S] /", "/ [[:alnum:]_] [^[:alnum:]_] [w W] /" ][ "x_12" ] [ "123" ] [ "☀" ] +-- End (no-eol) -- + + +Exit cleanup. + +-- Testcase -- +let re=/ok/;let o={re};o.self=o;gc();print(re);exit(7); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +/ok/ +-- End (no-eol) -- + +-- Expect exitcode -- +7 +-- End -- + + +Flags. + +-- Testcase -- +print(/a.b/," ", + match("A\nb aXb",/a.b/)," ", + match("x\na\ny",/^a$/)," ", + replace("a A a",/a/,"x"),"\n");print(/a.b/g," ", + match("A\nb aXb",/a.b/g)," ", + match("x\na\ny",/^a$/g)," ", + replace("a A a",/a/g,"x"),"\n");print(/a.b/i," ", + match("A\nb aXb",/a.b/i)," ", + match("x\na\ny",/^a$/i)," ", + replace("a A a",/a/i,"x"),"\n");print(/a.b/s," ", + match("A\nb aXb",/a.b/s)," ", + match("x\na\ny",/^a$/s)," ", + replace("a A a",/a/s,"x"),"\n");print(/a.b/gi," ", + match("A\nb aXb",/a.b/gi)," ", + match("x\na\ny",/^a$/gi)," ", + replace("a A a",/a/gi,"x"),"\n");print(/a.b/gs," ", + match("A\nb aXb",/a.b/gs)," ", + match("x\na\ny",/^a$/gs)," ", + replace("a A a",/a/gs,"x"),"\n");print(/a.b/is," ", + match("A\nb aXb",/a.b/is)," ", + match("x\na\ny",/^a$/is)," ", + replace("a A a",/a/is,"x"),"\n");print(/a.b/gis," ", + match("A\nb aXb",/a.b/gis)," ", + match("x\na\ny",/^a$/gis)," ", + replace("a A a",/a/gis,"x"),"\n");print(/a.b/ssggii," ", + match("A\nb aXb",/a.b/ssggii)," ", + match("x\na\ny",/^a$/ssggii)," ", + replace("a A a",/a/ssggii,"x"),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +/a.b/ [ "aXb" ] [ "a" ] x A a +/a.b/g [ [ "aXb" ] ] [ [ "a" ] ] x A x +/a.b/i [ "aXb" ] [ "a" ] x A a +/a.b/s [ "aXb" ] x A a +/a.b/gi [ [ "aXb" ] ] [ [ "a" ] ] x x x +/a.b/gs [ [ "aXb" ] ] x A x +/a.b/is [ "A\nb" ] x A a +/a.b/gis [ [ "A\nb" ], [ "aXb" ] ] x x x +/a.b/gis [ [ "A\nb" ], [ "aXb" ] ] x x x +-- End -- + + +Lazy evaluation. + +-- Testcase -- +function unused(){return /(/;} + let a=false && /(/,b=true || /(/,c=true ? /ok/ : /(/; + print(a," ",b," ",match("ok",c)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +false true [ "ok" ] +-- End (no-eol) -- + + +Loop cleanup. + +-- Testcase -- +gc("start",1);let kept=[]; + for(let i=0;i<100;i++){let re=/[a-z]+/i;let o={re};o.self=o; + if(i%3==0){push(kept,re);continue;}if(i==98)break;} + gc();print(length(kept)," ",match("ABC",kept[0])," ",kept[0]===kept[1]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +33 [ "ABC" ] false +-- End (no-eol) -- + + +Matching and captures. + +-- Testcase -- +print([ + match("abc123",/([a-z]+)([0-9]+)/),match("abc",/z/), + match("b",/(a)?(b)/),match("abc",/^/),match("a b c",/[a-z]+/g), + match("abc",/a*/g),match("",/^$/)]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "abc123", "abc", "123" ], null, [ "b", null, "b" ], [ "" ], [ [ "a" ], [ "b" ], [ "c" ] ], [ [ "a" ], [ "" ], [ "" ], [ "" ] ], [ "" ] ] +-- End (no-eol) -- + + +Replace callback gc. + +-- Testcase -- +gc("start",1);let held=[]; + print(replace("a1 b22",/([a-z])([0-9]+)/g,function(all,letter,digits){ + let re=/[0-9]+/;let cycle={re};cycle.self=cycle;push(held,()=>re); + gc();return letter+":"+match(digits,re)[0];})); + gc();print(" ",held[0]()===held[1]()," ",match("42",held[1]())); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +a:1 b:22 false [ "42" ] +-- End (no-eol) -- + + +Replace strings and limits. + +-- Testcase -- +print([ + replace("ab12 ab34",/([a-z]+)([0-9]+)/g,"$2:$1:$&:$$"), + replace("aaaa",/a/g,"x",2),replace("aaaa",/a/,"x"), + replace("abc",/^|$/g,"!"),replace("abc",/q/g,"x"), + replace("aaaa",/a/g,"x",0),replace("aaaa",/a/g,"x",-1)]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "12:ab:ab12:$ 34:ab:ab34:$", "xxaa", "xaaa", "!abc!bc!c!", "abc", "aaaa", "aaaa" ] +-- End (no-eol) -- + + +Runtime errors caught. + +-- Testcase -- +for(let i=0;i<5;i++){ + try{let cycle={re:/ok/};cycle.self=cycle;gc();let invalid=/foo(/;} + catch(e){print(e.type,":",e.message,";");}} + try{let invalid=/[z-a]/;}catch(e){print(e.type,":",e.message);} + gc();print(" ",match("ok",/ok/)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Syntax error:Unmatched ( or \(;Syntax error:Unmatched ( or \(;Syntax error:Unmatched ( or \(;Syntax error:Unmatched ( or \(;Syntax error:Unmatched ( or \(;Syntax error:Invalid range end [ "ok" ] +-- End (no-eol) -- + + +Slash character classes. + +-- Testcase -- +print([/[/]/,/[[./.]/]/,/[[:alpha:]/]/,/[[=/=]/]/]); + print(match("a/b",/[/]/)," ",match("/",/[[:alpha:]/]/)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "/[/]/", "/[[./.]/]/", "/[[:alpha:]/]/", "/[[=/=]/]/" ][ "/" ] [ "/" ] +-- End (no-eol) -- + + +Split and empty matches. + +-- Testcase -- +print([ + split("a,b; c",/[,; ]+/),split("a,b,c",/,/,2),split("abc",/x*/), + split("Axa",/a/i),split("a\nb",/./s),split("a,b",/,/,0)]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "a", "b", "c" ], [ "a", "b,c" ], [ "a", "b", "c" ], [ "", "x", "" ], [ "", "", "", "" ], [ ] ] +-- End (no-eol) -- + + +Types and identity. + +-- Testcase -- +function make(){return /abc/;} + let a=make(),b=make();print(type(a)," ",a," ",a===a," ",a===b," ", + a in [a]," ",b in [a]," ",[/abc/g,/abc/i,/abc/s]); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +regexp /abc/ true false true false [ "/abc/g", "/abc/i", "/abc/s" ] +-- End (no-eol) -- + + +Uncaught error cleanup. + +-- Testcase -- +let re=/ok/;let o={re};o.self=o;gc();let bad=/(/; +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect stderr -- +Syntax error: Unmatched ( or \( +In line 1, byte 46: + + `let re=/ok/;let o={re};o.self=o;gc();let bad=/(/;` + ~~~~~~~~^~~~ + + +-- End -- + +-- Expect exitcode -- +254 +-- End -- diff --git a/tests/custom/19_native/05_require b/tests/custom/19_native/05_require new file mode 100644 index 00000000..b7ba209f --- /dev/null +++ b/tests/custom/19_native/05_require @@ -0,0 +1,378 @@ +Cache mutation reload and closures. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let a=require("counter"), b=require("counter"); a.value=9; + print(a===b," ",b.value," ",a.next()," "); delete modules.counter; + let c=require("counter"); gc(); print(a===c," ",c.next()," ",a.next()," "); + let f=require("plain"); delete modules.plain; let g=require("plain"); + print(f===g," ",f()," ",g(),"\n"); +-- End (no-eol) -- + +-- File counter.uc -- +let n=0; return {value:1,next(){gc();return ++n;}}; +-- End (no-eol) -- + +-- File plain.uc -- +return function(){return 42;}; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +true 9 1 false 1 2 false 42 42 +-- End -- + + +Errors uncached and nested retry. + +-- Testcase -- +-- End -- + +-- File main.uc -- +modules.attempts=0; + try {require("outer");} catch(e){print(e.message," ");} + print(exists(modules,"outer")," ",exists(modules,"inner")," "); + let v=require("outer"); gc(); print(v.value," ",modules.attempts,"\n"); +-- End (no-eol) -- + +-- File outer.uc -- +let x=require("inner"); return {value:x}; +-- End (no-eol) -- + +-- File inner.uc -- +gc(); if (++modules.attempts==1) die("retry"); return 7; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +retry false false 7 2 +-- End -- + + +Exit unwinds. + +-- Testcase -- +-- End -- + +-- File main.uc -- +try {require("quit");} catch(e){print("caught");} print("unreachable"); +-- End (no-eol) -- + +-- File quit.uc -- +let o={};o.self=o;gc();print("bye\n");exit(7); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +bye +-- End -- + +-- Expect exitcode -- +7 +-- End -- + + +Falsey cache. + +-- Testcase -- +-- End -- + +-- File main.uc -- +modules.count=0; require("m0"); print(require("m0") === null," ");require("m1"); print(require("m1") === false," ");require("m2"); print(require("m2") === 0," ");require("m3"); print(require("m3") === ""," ");print(modules.count,"\n"); +-- End (no-eol) -- + +-- File m0.uc -- +modules.count++; return null; +-- End (no-eol) -- + +-- File m1.uc -- +modules.count++; return false; +-- End (no-eol) -- + +-- File m2.uc -- +modules.count++; return 0; +-- End (no-eol) -- + +-- File m3.uc -- +modules.count++; return ""; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +true true true true 4 +-- End -- + + +Import tables nested gc and reload. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import {n,inc} from './base.uc'; let a=require("outer"); + import * as after from './after.uc'; let old=a.inc; + delete modules.outer; let b=require("outer"); gc(); + print(n," ",inc()," ",a.value," ",old()," ",b.inc()," ",after.value,"\n"); +-- End (no-eol) -- + +-- File base.uc -- +export let n=10; export function inc(){gc();return ++n;} +-- End (no-eol) -- + +-- File after.uc -- +export let value=99; +-- End (no-eol) -- + +-- File outer.uc -- +import {n,inc} from './base.uc'; let nested=require("inner"); + gc(); import * as later from './after.uc'; + return {value:n+later.value+nested(),inc:inc}; +-- End (no-eol) -- + +-- File inner.uc -- +import {n,inc} from "./base.uc"; gc(); return inc; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +10 11 120 11 11 99 +-- End -- + + +Lazy transitive and dotted. + +-- Testcase -- +-- End -- + +-- File main.uc -- +function unused(){return require("unused");} print("main "); let a=require("pkg.outer"); print(a.value," ",a===require("pkg.outer"),"\n"); +-- End (no-eol) -- + +-- File unused.uc -- +die("must not run"); +-- End (no-eol) -- + +-- File pkg/outer.uc -- +print("outer "); return require("pkg.inner"); +-- End (no-eol) -- + +-- File pkg/inner.uc -- +print("inner "); return {value:42}; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +main outer inner 42 true +-- End -- + + +Local alias discovery and shadowing. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let r=require, name="dep";print(r(name)," "); + function f(require){return require("trap");}print(f(x=>x),"\n"); +-- End (no-eol) -- + +-- File dep.uc -- +return 5; +-- End (no-eol) -- + +-- File trap.uc -- +this is invalid source! +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +5 trap +-- End -- + + +Nonstring and nul. + +-- Testcase -- +print(require()," ",require(null)," ",require(17),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- + +-- End -- + + +Preseeded cycle and virtual modules. + +-- Testcase -- +-- End -- + +-- File main.uc -- +modules.virtual=null;print(require("virtual")," ");let a=require("a");gc();print(a===a.b.a,"\n"); +-- End (no-eol) -- + +-- File a.uc -- +let a={};modules.a=a;a.b=require("b");return a; +-- End (no-eol) -- + +-- File b.uc -- +return {a:require("a")}; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- + true +-- End -- + + +Cycle and retry. + +-- Testcase -- +-- End -- + +-- File main.uc -- +for(let i=0;i<2;i++) try {require("a");} catch(e){print(e.message,"\n");} + modules.b=9;print(require("a"),"\n"); +-- End (no-eol) -- + +-- File a.uc -- +return require("b"); +-- End (no-eol) -- + +-- File b.uc -- +return require("a"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +Too much recursion +Too much recursion +9 +-- End -- + +-- Expect stderr -- +-- End -- + + +Nul in name. + +-- Testcase -- +try {require("bad\u0000name");} catch(e){print(e.message,"\n");} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +No module named 'bad' could be found +-- End -- + + +Captured alias invalidation assignment. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let r=require;function change(){r=x=>x;}change();print(r("trap"),"\n"); +-- End (no-eol) -- + +-- File trap.uc -- +this is invalid source! +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +trap +-- End -- + + +Captured alias invalidation getter. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let r,o={};proto(o,{__get__(key){r=x=>x;return 0;}});r=require;o.x;print(r("trap"),"\n"); +-- End (no-eol) -- + +-- File trap.uc -- +this is invalid source! +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +trap +-- End -- diff --git a/tests/custom/19_native/06_include b/tests/custom/19_native/06_include new file mode 100644 index 00000000..c53547fe --- /dev/null +++ b/tests/custom/19_native/06_include @@ -0,0 +1,588 @@ +Alias and shadowed include. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let f=include;f("part.uc");function g(include){include("trap.uc");}g(x=>print(x)); +-- End (no-eol) -- + +-- File part.uc -- +print("part "); +-- End (no-eol) -- + +-- File trap.uc -- +syntax error! +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +part trap.uc +-- End (no-eol) -- + + +Argument errors. + +-- Testcase -- +try {include(1);}catch(e){print(e.type," ",e.message,"\n");} + try {include("missing.uc",false);}catch(e){print(e.type," ",e.message,"\n");} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Type error Passed filename is not a string +Type error Passed scope value is not an object +-- End -- + + +Dynamic global scope of escaped functions. + +-- Testcase -- +-- End -- + +-- File main.uc -- +value="outside";let kept=[];include("part.uc",{value:"inside",kept}); + print(kept[0]()," ",call(kept[0],null,{value:"call"}),"\n"); +-- End (no-eol) -- + +-- File part.uc -- +push(kept,function(){gc();return value;});print(kept[0]()," "); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +inside outside call +-- End -- + + +Error restores scope and retries. + +-- Testcase -- +-- End -- + +-- File main.uc -- +value="outer";let state={n:0};let s={value:"inner",state}; + for(let i=0;i<2;i++) try {include("part.uc",s);} catch(e){gc();print(e.message," ",value," ");} + print(state.n,"\n"); +-- End (no-eol) -- + +-- File part.uc -- +state.n++;let o={};o.self=o;gc();die(value); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +inner outer inner outer 2 +-- End -- + + +Global operations and scope restoration. + +-- Testcase -- +n=2;n*=3;print(n++," ",++n," ",n," "); + let saved=print;print=x=>"wrapped:"+x;saved(print(3)," ");global.print=saved; + call(function(){n+=10;gc();print(n," ");},null,{n:1});print(n,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +6 8 8 wrapped:3 11 8 +-- End -- + + +Isolated scope and builtin overrides. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let realprint=print, collect=push, sweep=gc, lines=[]; + let s=proto({print(...args){sweep();collect(lines,args);},NaN:7,Infinity:8},{}); + include("part.uc",s);gc();realprint(lines," ",s.value,"\n"); +-- End (no-eol) -- + +-- File part.uc -- +value=3;print(value,NaN,Infinity,require,modules,global,REQUIRE_SEARCH_PATH); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +[ [ 3, 7, 8, null, null, null, null ] ] 3 +-- End -- + + +Nested import require and callback origins. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import {run} from './lib/module.uc'; + run();let cb=require("lib.required");cb();include("inc/outer.uc"); +-- End (no-eol) -- + +-- File lib/module.uc -- +export function run(){return include("leaf.uc");} +-- End (no-eol) -- + +-- File lib/required.uc -- +return function(){include("leaf.uc");}; +-- End (no-eol) -- + +-- File lib/leaf.uc -- +print("lib\n"); +-- End (no-eol) -- + +-- File inc/outer.uc -- +include("nested/../leaf.uc"); +-- End (no-eol) -- + +-- File inc/leaf.uc -- +print("inc\n"); +-- End (no-eol) -- + +-- File inc/nested/empty.uc -- +-- End -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +lib +lib +inc +-- End -- + + +No lexical scope inheritance. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let local=7;include("part.uc");include("part.uc",{local}); +-- End (no-eol) -- + +-- File part.uc -- +print(local,"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- + +7 +-- End -- + + +Nonfunction builtin override. + +-- Testcase -- +-- End -- + +-- File main.uc -- +try {include("part.uc",{length:null});}catch(e){print(e.type,"\n");} + include("part.uc",{length(x){gc();return 99;}}); +-- End (no-eol) -- + +-- File part.uc -- +print(length([1,2]),"\n"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +Type error +99 +-- End -- + + +Prototype scope updated in place. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let parent={n:4,print:print},s=proto({own:1},parent); + include("part.uc",s);print(s.own," ",parent.n," ",parent.added,"\n"); +-- End (no-eol) -- + +-- File part.uc -- +own+=2;n++;added=9;print(own," ",n," "); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +3 5 3 5 9 +-- End -- + + +Recursive include is repeated execution. + +-- Testcase -- +-- End -- + +-- File main.uc -- +state={n:0};include("part.uc",{level:5});print(state.n); +-- End (no-eol) -- + +-- File part.uc -- +state.n++;gc();if(level)include("part.uc",{level:level-1}); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +6 +-- End (no-eol) -- + + +Repeated lazy and no return. + +-- Testcase -- +-- End -- + +-- File main.uc -- +function unused(){include("unused.uc");} + count=0;print(include("part.uc")," ");include("./part.uc");print(count,"\n"); +-- End (no-eol) -- + +-- File part.uc -- +print("part ");count++;return 42; +-- End (no-eol) -- + +-- File unused.uc -- +die("must not run"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +part part 0 +-- End -- + + +Repeated static imports and escaped closures. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import {value} from './base.uc';let callbacks=[]; + include("part.uc",{callbacks});include("part.uc",{callbacks});gc(); + print(value," ",callbacks[0]()," ",callbacks[0]()," ",callbacks[1](),"\n"); +-- End (no-eol) -- + +-- File base.uc -- +export let value=10;export function next(){gc();return ++value;} +-- End (no-eol) -- + +-- File part.uc -- +import {next} from "./base.uc";push(callbacks,next);include("nested.uc"); +-- End (no-eol) -- + +-- File nested.uc -- +import * as b from "./base.uc";gc();print(b.next()," "); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +11 11 10 11 12 11 +-- End -- + + +Replaced module cache. + +-- Testcase -- +-- End -- + +-- File main.uc -- +count=0;require("dep");modules={};require("dep");print(count); +-- End (no-eol) -- + +-- File dep.uc -- +count++;return null; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +2 +-- End (no-eol) -- + + +Require and include same file. + +-- Testcase -- +-- End -- + +-- File main.uc -- +count=0;require("part");include("part.uc");require("part");include("part.uc");print(count); +-- End (no-eol) -- + +-- File part.uc -- +count++;return count; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +1 +-- End (no-eol) -- + + +Scope copy and shared objects. + +-- Testcase -- +-- End -- + +-- File main.uc -- +count=0;let s={name:"original",box:{n:0}}; + include("part.uc",s);include("part.uc",s);gc();print(s.name," ",s.box.n," ",count," ",created,"\n"); +-- End (no-eol) -- + +-- File part.uc -- +print(name," ");name="changed";box.n++;count++;created=9;gc(); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +original original original 2 2 9 +-- End -- + + +Scoped require cache. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let s={modules:{},state:{n:0}}; + include("part.uc",s);include("part.uc",s);print(s.state.n," ",exists(modules,"dep"),"\n"); +-- End (no-eol) -- + +-- File part.uc -- +print(require("dep")," "); +-- End (no-eol) -- + +-- File dep.uc -- +state.n++;return state.n; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +1 1 1 false +-- End -- + + +Strict globals. + +-- Testcase -- +-- End -- + +-- File main.uc -- +import {read,write,update} from './strict.uc'; + try{read();}catch(e){print(e.type," ");}try{write();}catch(e){print(e.type," ");} + try{update();}catch(e){print(e.type," ");}print(call(read,null,{missing:42}),"\n"); +-- End (no-eol) -- + +-- File strict.uc -- +export function read(){return missing;}export function write(){missing=1;}export function update(){missing++;} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +Reference error Reference error Reference error 42 +-- End -- + + +Exit unwinds scope. + +-- Testcase -- +-- End -- + +-- File main.uc -- +try {include("part.uc",{value:7});} catch(e){print("caught");} +-- End (no-eol) -- + +-- File part.uc -- +let o={};o.self=o;gc();print(value);exit(value); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +7 +-- End (no-eol) -- + +-- Expect exitcode -- +7 +-- End -- + + +Nul in path. + +-- Testcase -- +try {include("bad\u0000name");}catch(e){print(e.message,"\n");} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Include file not found +-- End -- + + +Global loader override. + +-- Testcase -- +-- End -- + +-- File main.uc -- +include=x=>print(x);include("trap.uc"); +-- End (no-eol) -- + +-- File trap.uc -- +syntax error! +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +trap.uc +-- End (no-eol) -- + + +Computed directory and path normalization. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let name="one";include("plugins/./"+name+".uc"); +-- End (no-eol) -- + +-- File plugins/one.uc -- +print("one"); +-- End (no-eol) -- + +-- File plugins/unused.uc -- +die("unused"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +one +-- End (no-eol) -- diff --git a/tests/custom/19_native/07_render b/tests/custom/19_native/07_render new file mode 100644 index 00000000..8610e547 --- /dev/null +++ b/tests/custom/19_native/07_render @@ -0,0 +1,375 @@ +Binary output. + +-- Testcase -- +let s=render(function(){print("a\u0000b");});print(length(s)," ",hexenc(s)); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +3 610062 +-- End (no-eol) -- + + +Error restores output. + +-- Testcase -- +-- End -- + +-- File main.uc -- +try{render("fail.ut");}catch(e){print(e.message," ");} + try{render(function(){print("hidden");die("fn");});}catch(e){print(e.message," ");} + print(render(function(){print("ok");})); +-- End (no-eol) -- + +-- File fail.ut -- +hidden{% gc();die("template"); %} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +template fn ok +-- End (no-eol) -- + + +Invalid arguments. + +-- Testcase -- +for(let v in [null,42,{}])try{render(v);}catch(e){print(e.type,e.message);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Type errorPassed filename is not a stringType errorPassed filename is not a stringType errorPassed filename is not a string +-- End (no-eol) -- + + +Nested render. + +-- Testcase -- +-- End -- + +-- File main.uc -- +print(render("outer.uc",{n:3})); +-- End (no-eol) -- + +-- File outer.uc -- +A{{ render("inner.ut",{n:n+1}) }}B +-- End (no-eol) -- + +-- File inner.ut -- +{% gc(); %}{{ n }} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +A4B +-- End (no-eol) -- + + +Raw include inside render. + +-- Testcase -- +-- End -- + +-- File main.uc -- +print(render("page.ut")); +-- End (no-eol) -- + +-- File page.ut -- +A{% include("raw.uc"); %}Z +-- End (no-eol) -- + +-- File raw.uc -- +print("B"); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +Aprint("B");Z +-- End (no-eol) -- + + +Render callable. + +-- Testcase -- +let o={};proto(o,{__call__(x){gc();print(x);}});print(render(o,"hello")); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +hello +-- End (no-eol) -- + + +Render file. + +-- Testcase -- +-- End -- + +-- File main.uc -- +print(render("part.uc",{items:[1,2,3]})); +-- End (no-eol) -- + +-- File part.uc -- +{% for(let v in items) { %}[{{ v }}]{% gc(); } %} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +[1][2][3] +-- End (no-eol) -- + + +Render function. + +-- Testcase -- +let s=render(function(a,b){gc();print(a);printf("%s",b);return "ignored";},"a","b");print("[",s,"]"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ab] +-- End (no-eol) -- + + +Scoped globals. + +-- Testcase -- +-- End -- + +-- File main.uc -- +global.n=1;let s={n:9};print(render("part.ut",s)," ",n," ",s.n); +-- End (no-eol) -- + +-- File part.ut -- +{% n++; %}{{ n }} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +10 1 9 +-- End (no-eol) -- + + +Exit restores capture. + +-- Testcase -- +render(function(){print("hidden");exit(7);});print("wrong"); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +7 +-- End -- + + +Entry output. + +-- Testcase -- +Hello {{ "world" }}! +{% for(let v in [1,2]) { %}[{{ v }}]{% } %} +-- End -- + +-- Args -- +-T +-- End -- + +-- Expect stdout -- +Hello world! +[1][2] +-- End (no-eol) -- + + +Expression values. + +-- Testcase -- +[{{ null }}] {{ {a:1,b:null} }} {{ [1,2,null] }} {{ false }} +-- End (no-eol) -- + +-- Args -- +-T +-- End -- + +-- Expect stdout -- +[] { "a": 1, "b": null } [ 1, 2, null ] false +-- End (no-eol) -- + + +Whitespace default. + +-- Testcase -- +before + {% let x=1; %} +{{ x }} +after +-- End -- + +-- Args -- +-T +-- End -- + +-- Expect stdout -- +before +1 +after +-- End -- + + +Whitespace no lstrip. + +-- Testcase -- +before + {% let x=1; %} +{{ x }} +after +-- End -- + +-- Args -- +-Tno-lstrip +-- End -- + +-- Expect stdout -- +before + 1 +after +-- End -- + + +Whitespace no rtrim. + +-- Testcase -- +before + {% let x=1; %} +{{ x }} +after +-- End -- + +-- Args -- +-Tno-rtrim +-- End -- + +-- Expect stdout -- +before + +1 +after +-- End -- + + +Whitespace no lstrip no rtrim. + +-- Testcase -- +before + {% let x=1; %} +{{ x }} +after +-- End -- + +-- Args -- +-Tno-lstrip,no-rtrim +-- End -- + +-- Expect stdout -- +before + +1 +after +-- End -- + + +Include in template mode. + +-- Testcase -- +-- End -- + +-- File main.uc -- +A{% include("part.uc",{n:7}); %}Z +-- End (no-eol) -- + +-- File part.uc -- +P{{ n }}{% print("Q"); %} +-- End (no-eol) -- + +-- Args -- +-T +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +AP7QZ +-- End (no-eol) -- + + +Explicit computed template. + +-- Testcase -- +-- End -- + +-- File main.uc -- +let n="page";print(render(n+".ut")); +-- End (no-eol) -- + +-- File page.ut -- +{{ 42 }} +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +42 +-- End (no-eol) -- diff --git a/tests/custom/19_native/08_traces b/tests/custom/19_native/08_traces new file mode 100644 index 00000000..1fbd60d0 --- /dev/null +++ b/tests/custom/19_native/08_traces @@ -0,0 +1,239 @@ +Bundled locations. + +-- Testcase -- +-- End -- + +-- File main.uc -- +function show(e){print(map(e.stacktrace,x=>[x.filename&&replace(x.filename,TESTFILES_PATH,""),x.line,x.byte,x.tco]));} +try{require("worker")();}catch(e){show(e);} +try{include("part.uc");}catch(e){show(e);} +-- End (no-eol) -- + +-- File worker.uc -- +return function worker(){ + die("worker"); +}; +-- End (no-eol) -- + +-- File part.uc -- +let x=1; +null.property; +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +[ [ "files/worker.uc", 2, 14, null ], [ "files/main.uc", 2, 23, null ] ][ [ "/part.uc", 2, 6, null ], [ null, null, null, null ], [ "files/main.uc", 3, 22, null ] ] +-- End (no-eol) -- + + +Callback. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +try{map([1],function(){ + gc();die("callback"); +});}catch(e){show(e);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 3, 22, null ], [ null, null, null, null ], [ "[stdin]", 4, 2, null ] ] +-- End (no-eol) -- + + +Local catch includes callers. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +function inner(){try{null.x;}catch(e){show(e);}} +function outer(){inner();} +outer(); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 2, 27, null ], [ "[stdin]", 3, 24, 1 ] ] +-- End (no-eol) -- + + +Long lines and unicode. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + +let x="☀"; +try{ + null.x; +}catch(e){show(e);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 6, 158, null ] ] +-- End (no-eol) -- + + +Nested calls. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +function inner(){ + die("oops"); +} +function outer(){ + inner(); +} +try{outer();}catch(e){show(e);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 3, 13, null ], [ "[stdin]", 6, 9, null ], [ "[stdin]", 8, 11, null ] ] +-- End (no-eol) -- + + +Rethrow. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +function inner(){try{die("one");}catch(e){gc();die(e);}} +try{inner();}catch(e){show(e);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 2, 31, null ], [ "[stdin]", 3, 11, null ] ] +-- End (no-eol) -- + + +Sourcepath. + +-- Testcase -- +-- End -- + +-- File main.uc -- +global.rel=p=>p&&replace(p,TESTFILES_PATH,""); +function f(){print(rel(sourcepath())," ",rel(sourcepath(1,true))," ",rel(sourcepath(99)));} + f();include("part.uc"); +-- End (no-eol) -- + +-- File part.uc -- +print(" ",rel(sourcepath())," ",rel(sourcepath(1))); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +/main.uc /part.uc /main.uc +-- End (no-eol) -- + + +Spread tail calls. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +function inner(n){if(n)return inner(...[n-1]);return die(...["oops"]);} +try{inner(...[20]);}catch(e){show(e);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 2, 69, 20 ], [ "[stdin]", 3, 18, null ] ] +-- End (no-eol) -- + + +Tail calls. + +-- Testcase -- +function show(e){print(map(e.stacktrace,x=>[x.filename,x.line,x.byte,x.tco]));} +function inner(n){if(n)return inner(n-1);return die("oops");} +try{inner(50);}catch(e){show(e);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ [ "[stdin]", 2, 59, 50 ], [ "[stdin]", 3, 13, null ] ] +-- End (no-eol) -- + + +Uncaught trace. + +-- Testcase -- +function fail(){ +null.x; +} +fail(); +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect stderr -- +Reference error: left-hand side expression is null +In line 2, byte 6: + (1 tail call frames omitted) + + `null.x;` + ~~~~~^~ + + +-- End -- + +-- Expect exitcode -- +254 +-- End -- + + +Formatted context. + +-- Testcase -- +try{null.x;}catch(e){print(e.stacktrace[0].context);} +-- End (no-eol) -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +In line 1, byte 10: + + `try{null.x;}catch(e){print(e.stacktrace[0].context);}` + ~~~~~^~ +-- End -- diff --git a/tests/custom/19_native/09_cli b/tests/custom/19_native/09_cli new file mode 100644 index 00000000..94949a42 --- /dev/null +++ b/tests/custom/19_native/09_cli @@ -0,0 +1,152 @@ +Arguments. + +-- Testcase -- +-- End -- + +-- File main.uc -- +print(ARGV); +-- End (no-eol) -- + +-- Args -- +-R +-L +files/*.uc +files/main.uc +one +two +-D +☀ +-- End -- + +-- Expect stdout -- +[ "one", "two", "-D", "☀", "-" ] +-- End (no-eol) -- + + +Definitions. + +-- Testcase -- +print([n,word,obj,nil,flag,arr]); +-- End (no-eol) -- + +-- Args -- +-R +-D +n=42 +-D +word=unquoted +-D +obj={"x":7} +-D +nil=null +-D +flag=true +-D +arr=[1,2] +-- End -- + +-- Expect stdout -- +[ 42, "unquoted", { "x": 7 }, null, true, [ 1, 2 ] ] +-- End (no-eol) -- + + +Order and undefine. + +-- Testcase -- +print([a,b,exists(global,"gone"),ARGV]); +-- End (no-eol) -- + +-- Args -- +-R +-D +{"a":1,"b":2,"gone":3} +-D +a=9 +-U +gone +-D +ARGV=[4] +-- End -- + +-- Expect stdout -- +[ 9, 2, false, [ 4 ] ] +-- End (no-eol) -- + + +File and stdin. + +-- Testcase -- +{"y":9} +-- End (no-eol) -- + +-- File main.uc -- +print([x,cfg]); +-- End (no-eol) -- + +-- File globals.json -- +{"x":8} +-- End (no-eol) -- + +-- Args -- +-R +-F +files/globals.json +-F +cfg=- +-L +files/*.uc +files/main.uc +-- End -- + +-- Expect stdout -- +[ 8, { "y": 9 } ] +-- End (no-eol) -- + + +Builtin override. + +-- Testcase -- +print(length); +-- End (no-eol) -- + +-- Args -- +-R +-D +length=7 +-- End -- + +-- Expect stdout -- +7 +-- End (no-eol) -- + + +Globals in bundles. + +-- Testcase -- +-- End -- + +-- File main.uc -- +include("part.uc");print(require("worker")); +-- End (no-eol) -- + +-- File part.uc -- +print(ARGV[0]," "); +-- End (no-eol) -- + +-- File worker.uc -- +gc();return configured; +-- End (no-eol) -- + +-- Args -- +-R +-D +configured=42 +-L +files/*.uc +files/main.uc +arg +-- End -- + +-- Expect stdout -- +arg 42 +-- End (no-eol) --