Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
dbd9a6a
vm: release only the call frames of the current invocation on exit
blogic Oct 29, 2026
3d7653c
vm: drop the callee of a call that fails before its frame exists
nbd168 Nov 19, 2026
35b40c2
vm: run no interpreter after a C function that __call__ resolves to
blogic Oct 31, 2026
0c25918
vm: release the value that an object spread rejects
blogic Oct 31, 2026
2fa9c51
vm: keep the arguments of a spread call on the stack
blogic Oct 31, 2026
92a7e6b
vm: select the exception handler by the instruction that raised
nbd168 Nov 11, 2026
f9b7a44
vm: close the upvalues of the slots that an exception unwind drops
nbd168 Nov 12, 2026
8031af0
vm: run no signal handler after the last RETURN of an invocation
nbd168 Nov 12, 2026
da3d93c
vm: keep the operands of a property operation on the stack
nbd168 Nov 11, 2026
fc787e9
vm: read the operand of SOBJ before the conversion of a key
nbd168 Nov 19, 2026
28011e5
vm: do not use the call frame pointer after a tostring call
nbd168 Nov 19, 2026
44ae2ad
vm: release the condition of a jump that is out of range
nbd168 Nov 11, 2026
fdbee95
vm: hold the scope during the update of a global variable
nbd168 Nov 11, 2026
e1d412f
vm, lib: install the signal handlers without SA_ONSTACK
nbd168 Nov 12, 2026
215ec41
vm: run the handlers of pending signals in the order of their numbers
nbd168 Nov 12, 2026
f26ddd3
vm: take the count of a shift modulo 64 for signed operands
nbd168 Nov 12, 2026
e1b42d2
vm: keep the 64 bits of each operand of a bit operation
nbd168 Nov 19, 2026
7bc1911
vm: resolve an upvalue reference in the update of a global variable
nbd168 Nov 12, 2026
35b8ceb
vm: raise an exception if an import gets no module
nbd168 Nov 12, 2026
3e44e78
vm: raise a type error for the store of a global in a constant scope
nbd168 Nov 12, 2026
3d5123e
vm: keep the 64 bits of the operand of the complement
nbd168 Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/ucode/internal/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define UCODE_INTERNAL_VM_H

#include "ucode/vm.h"
#include "ucode/internal/util.h"

#define __insns \
__insn(NOOP) \
Expand Down Expand Up @@ -109,6 +110,9 @@ extern const int8_t uc_vm_insn_format[__I_MAX];

int8_t uc_vm_insn_to_argtype(uc_vm_insn_t insn);

__hidden void uc_vm_raise_exception_trace(uc_vm_t *vm, uc_exception_type_t type,
const char *message, uc_value_t *stacktrace);

static inline bool
uc_vm_is_strict(uc_vm_t *vm)
{
Expand Down
30 changes: 11 additions & 19 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -941,12 +941,8 @@ uc_die(uc_vm_t *vm, size_t nargs)

s = msg ? uc_cast_string(vm, &msg, &freeable) : "Died";

vm->exception.type = etype;
free(vm->exception.message);
vm->exception.message = strdup(s);

ucv_put(vm->exception.stacktrace);
vm->exception.stacktrace = ucv_get(ucv_object_get(arg, "stacktrace", NULL));
uc_vm_raise_exception_trace(vm, etype, s,
ucv_object_get(arg, "stacktrace", NULL));
}
else
{
Expand Down Expand Up @@ -3044,19 +3040,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;
Expand Down Expand Up @@ -6258,7 +6250,7 @@ uc_signal(uc_vm_t *vm, size_t nargs)
if (ucv_type(sighandler) == UC_STRING) {
sigstr = ucv_string_get(sighandler);

sa.sa_flags = SA_ONSTACK | SA_RESTART;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);

if (!strcmp(sigstr, "ignore"))
Expand Down
47 changes: 47 additions & 0 deletions tests/custom/01_arithmetic/03_bitwise
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,50 @@ Complement:
~0 = {{ ~0 }}
~10.4 = {{ ~10.4 }}
-- End --

An operand keeps its 64 bits. If one of the operands is negative, an
integer of 2^63 or more thus counts as the negative number with the same
bits. A double out of the 64 bit range wraps modulo 2^64, and an infinite
double counts as 0.

-- Expect stdout --
-1 & 0x8000000000000000 = -9223372036854775808
-1 | 0x8000000000000000 = -1
-1 ^ 0xffffffffffffffff = 0
-2 & 0xffffffffffffffff = -2
0x8000000000000000 & 0x8000000000000001 = 9223372036854775808
-1 & 1e19 = -8446744073709551616
-1e19 | 0 = 8446744073709551616
1e20 & 0xffffffff = 1661992960
Infinity | 0 = 0
-- End --

-- Testcase --
-1 & 0x8000000000000000 = {{ -1 & 0x8000000000000000 }}
-1 | 0x8000000000000000 = {{ -1 | 0x8000000000000000 }}
-1 ^ 0xffffffffffffffff = {{ -1 ^ 0xffffffffffffffff }}
-2 & 0xffffffffffffffff = {{ -2 & 0xffffffffffffffff }}
0x8000000000000000 & 0x8000000000000001 = {{ 0x8000000000000000 & 0x8000000000000001 }}
-1 & 1e19 = {{ -1 & 1e19 }}
-1e19 | 0 = {{ -1e19 | 0 }}
1e20 & 0xffffffff = {{ 1e20 & 0xffffffff }}
Infinity | 0 = {{ Infinity | 0 }}
-- End --

The complement works on the same 64 bits as the other bit operators.

-- Expect stdout --
~0x8000000000000000 = 9223372036854775807
~1e20 = 10680464442257309695
~-1e20 = 7766279631452241919
~Infinity = 18446744073709551615
~-Infinity = 18446744073709551615
-- End --

-- Testcase --
~0x8000000000000000 = {{ ~0x8000000000000000 }}
~1e20 = {{ ~1e20 }}
~-1e20 = {{ ~-1e20 }}
~Infinity = {{ ~Infinity }}
~-Infinity = {{ ~-Infinity }}
-- End --
58 changes: 58 additions & 0 deletions tests/custom/01_arithmetic/06_shift_count
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
The shift operators use the low six bits of the count. A negative count
and a count of 64 or more give the result of a shift by the count modulo
64. If one of the operands is negative, the result is a signed number and
the right shift keeps the sign. An operand of 2^63 or more then counts as
the negative number with the same 64 bits.

-- Testcase --
{%
function shift(value, count) {
return [ value << count, value >> count ];
}

let cases = [
[ 1, 63 ], [ 1, 64 ], [ 1, 65 ], [ 1, 0xffffffffffffffff ],
[ 1, -1 ], [ 1, -63 ], [ 1, -64 ], [ 5, -62 ],
[ -8, 1 ], [ -8, 64 ], [ -8, 65 ], [ -8, -63 ],
[ -1, 63 ], [ -1, -1 ], [ -1, 0xffffffffffffffff ],
[ -1, 0x8000000000000000 ],
[ 0xffffffffffffffff, 1 ], [ 0xffffffffffffffff, 64 ],
[ 0xffffffffffffffff, -1 ]
];

for (let c in cases)
print(c, " ", shift(c[0], c[1]), "\n");

let n = 1;
n <<= -62;
print(n, "\n");

n = -16;
n >>= 66;
print(n, "\n");
%}
-- End --

-- Expect stdout --
[ 1, 63 ] [ 9223372036854775808, 0 ]
[ 1, 64 ] [ 1, 1 ]
[ 1, 65 ] [ 2, 0 ]
[ 1, 18446744073709551615 ] [ 9223372036854775808, 0 ]
[ 1, -1 ] [ -9223372036854775808, 0 ]
[ 1, -63 ] [ 2, 0 ]
[ 1, -64 ] [ 1, 1 ]
[ 5, -62 ] [ 20, 1 ]
[ -8, 1 ] [ -16, -4 ]
[ -8, 64 ] [ -8, -8 ]
[ -8, 65 ] [ -16, -4 ]
[ -8, -63 ] [ -16, -4 ]
[ -1, 63 ] [ -9223372036854775808, -1 ]
[ -1, -1 ] [ -9223372036854775808, -1 ]
[ -1, 18446744073709551615 ] [ -9223372036854775808, -1 ]
[ -1, 9223372036854775808 ] [ -1, -1 ]
[ 18446744073709551615, 1 ] [ 18446744073709551614, 9223372036854775807 ]
[ 18446744073709551615, 64 ] [ 18446744073709551615, 18446744073709551615 ]
[ 18446744073709551615, -1 ] [ -9223372036854775808, -1 ]
4
-4
-- End --
50 changes: 50 additions & 0 deletions tests/custom/04_modules/28_export_in_call_scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
call() copies the entries of a scope object without a prototype into the
global scope of the called function. The entries of a module namespace
are references to the exported variables. A compound assignment, an
increment and a decrement of such a global variable read the value of
the exported variable. Like a plain assignment they store the result in
the scope of the function, and the variable of the module keeps its value.

-- Testcase --
import * as mod from "./files/counter.uc";

function assign() { counter = counter + 2; return counter; }
function add() { counter += 2; return counter; }
function append() { label += "b"; return label; }
function shift() { counter <<= 3; return counter; }
function increment() { return [ counter++, ++counter, counter ]; }
function decrement() { return [ counter--, --counter, counter ]; }
function copy() { let scope = { ...mod }; scope.counter += 2; scope.counter++; return scope.counter; }

for (let fn in [ assign, add, append, shift, increment, decrement, copy ])
print(call(fn, null, mod), " ", mod.read(), "\n");

mod.step();

for (let fn in [ add, increment ])
print(call(fn, null, mod), " ", mod.read(), "\n");
-- End --

-- File counter.uc --
export let counter = 5;
export let label = "a";

export function read() { return [ counter, label ]; };
export function step() { counter++; };
-- End --

-- Args --
-R
-- End --

-- Expect stdout --
7 [ 5, "a" ]
7 [ 5, "a" ]
ab [ 5, "a" ]
40 [ 5, "a" ]
[ 5, 7, 7 ] [ 5, "a" ]
[ 5, 3, 3 ] [ 5, "a" ]
8 [ 5, "a" ]
8 [ 6, "a" ]
[ 6, 8, 8 ] [ 6, "a" ]
-- End --
86 changes: 86 additions & 0 deletions tests/custom/04_modules/29_dynamic_import_no_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import() raises an exception if its argument is not a string and if the
module table holds null for the name. The values of the function that
called import() stay in place.

-- Testcase --
function load(name) {
let before = "before", module, after = "after";

try {
module = import(name);
}
catch (e) {
module = `${e.type}: ${e.message}`;
}

return [ before, module, after ];
}

modules.absent = null;

print(load(5), "\n");
print(load(null), "\n");
print(load([ "fs" ]), "\n");
print(load("absent"), "\n");
print([ 1, type(import("fs")), 3 ], "\n");
-- End --

-- Args --
-R
-- End --

-- Expect stdout --
[ "before", "Type error: Passed module name is not a string", "after" ]
[ "before", "Type error: Passed module name is not a string", "after" ]
[ "before", "Type error: Passed module name is not a string", "after" ]
[ "before", "Runtime error: Module 'absent' is null", "after" ]
[ 1, "object", 3 ]
-- End --

The static import of a native module loads it at run time as well. If the
module table holds null for the name, the import raises the same
exception, for a list of names and for a wildcard import.

-- Testcase --
modules.fs = null;

import { readfile } from 'fs';

print(type(readfile), "\n");
-- End --

-- Args --
-R
-- End --

-- Expect stderr --
Runtime error: Module 'fs' is null
In line 3, byte 26:

`import { readfile } from 'fs';`
~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~


-- End --

-- Testcase --
modules.fs = null;

import * as fs from 'fs';

print(type(fs), "\n");
-- End --

-- Args --
-R
-- End --

-- Expect stderr --
Runtime error: Module 'fs' is null
In line 3, byte 21:

`import * as fs from 'fs';`
~~~~~~~~~~~~~~~~~~~~^~~~~


-- End --
Loading
Loading