Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
05bdff5
Give each V8 isolate an agent next to its env
syrusakbary Oct 23, 2026
2ff2331
Give each QuickJS runtime an agent and fix the callback realm
syrusakbary Oct 23, 2026
6e2ebd3
Split the bridge's env state into agent and env
syrusakbary Oct 23, 2026
f2eb4e1
Split host-JS env state into agent and env, drop the active context
syrusakbary Oct 23, 2026
8249a66
Cover the agent split in the shared and host-JS tests
syrusakbary Oct 23, 2026
1bae87c
Add the realm-bound napi_env design and Phase 0 notes
syrusakbary Oct 23, 2026
a3e13f2
Drop the branch name from the design doc's status line
syrusakbary Oct 23, 2026
c8289a1
Add realm handles for vm contexts
syrusakbary Oct 25, 2026
eed1fab
Test realm handles and realm collection
syrusakbary Oct 25, 2026
f2c0581
V8: realm handles with collectable realms
syrusakbary Oct 25, 2026
2d3e4c7
Say that two handles for one realm may differ
syrusakbary Oct 25, 2026
50c8d4f
Bridge: realm ids are value ids of realm globals
syrusakbary Oct 25, 2026
2d924e0
host-JS: realm handles and collectable realms
syrusakbary Oct 25, 2026
4266d87
Give QuickJS realm handles for vm contexts
syrusakbary Oct 25, 2026
4745825
Rewrite the design doc for realm handles
syrusakbary Oct 25, 2026
247d017
Test that a pending job outlives the realm that queued it
syrusakbary Oct 25, 2026
1f7a337
Pin QuickJS to 7ddf9e3 (a job holds its context)
syrusakbary Oct 25, 2026
eaeb1cd
Pin QuickJS to wasmerio/quickjs#10's current head
syrusakbary Oct 25, 2026
7f09bc7
Pin QuickJS to merged master
syrusakbary Oct 25, 2026
10dcc32
Merge main into refactor-agent-realm-phase0
syrusakbary Oct 27, 2026
697d1f1
Merge refactor-agent-realm-phase0 into refactor-realm-handle
syrusakbary Oct 27, 2026
fef1455
Add entering realms to the realm API
syrusakbary Oct 27, 2026
2bac018
V8: act in the current realm
syrusakbary Oct 27, 2026
851384c
QuickJS: act in the current realm
syrusakbary Oct 27, 2026
d020403
Bridge: enter and leave realms through the guest ABI
syrusakbary Oct 27, 2026
d496fa7
host-JS: act in the current realm
syrusakbary Oct 27, 2026
b101c35
Test entering realms
syrusakbary Oct 27, 2026
80f8219
host-JS: free a throwing callback's values before it throws
syrusakbary Oct 27, 2026
e526488
Merge main into refactor-agent-realm-phase0
syrusakbary Oct 28, 2026
e4812bc
Merge main into refactor-agent-realm-phase0
syrusakbary Oct 28, 2026
e1f3b58
Merge main into refactor-agent-realm-phase0
syrusakbary Oct 28, 2026
2af7006
Merge main into refactor-agent-realm-phase0
syrusakbary Oct 28, 2026
0a55728
Merge main into refactor-agent-realm-phase0
syrusakbary Oct 29, 2026
2c158cf
Merge main into refactor-agent-realm-phase0
syrusakbary Nov 5, 2026
7fc6918
Merge refactor-agent-realm-phase0 into refactor-realm-handle
syrusakbary Nov 5, 2026
dc1d526
Merge refactor-realm-handle into realm-entry
syrusakbary Nov 5, 2026
a6da125
Close realm scopes opened while the env is released
syrusakbary Nov 5, 2026
d76db7b
Make errors and DataViews from the current realm's intrinsics
syrusakbary Nov 5, 2026
b18cc85
V8: read Buffer from the env's realm
syrusakbary Nov 5, 2026
0a50296
V8: run module hooks in the env's realm
syrusakbary Nov 5, 2026
8f9d934
Realms: don't promise to refuse a stale realm handle
syrusakbary Nov 5, 2026
e37dbde
QuickJS: copy a sandbox realm's sandbox around napi_run_script
syrusakbary Nov 5, 2026
c7bb1d6
Realms: document which realm options each provider honors
syrusakbary Nov 5, 2026
adf3673
Test realm entry against errors, scopes, collection and teardown
syrusakbary Nov 5, 2026
a6d8781
host-JS: test the realm imports from a guest
syrusakbary Nov 5, 2026
883bfc9
V8: make typed arrays from the current realm's intrinsics
syrusakbary Nov 5, 2026
f928811
Test values made in a realm without relying on argument order
syrusakbary Nov 5, 2026
f2a1878
QuickJS: run finalizers at release while the engine is alive
syrusakbary Nov 5, 2026
816ee85
QuickJS: detach external buffers at release and finalize in linear time
syrusakbary Nov 5, 2026
ef9a5df
V8: run external ArrayBuffer finalizers on the env's thread
syrusakbary Nov 5, 2026
0e9412e
V8: free provider-owned external memory in the backing-store deleter
syrusakbary Nov 5, 2026
71d1c7d
V8: finalize instance data a finalizer sets during the env's release
syrusakbary Nov 5, 2026
253bdc0
QuickJS: finalize what the instance-data finalizer leaves at release
syrusakbary Nov 6, 2026
a84eeb3
V8: run the release in rounds and drop exceptions thrown at release
syrusakbary Nov 6, 2026
55ded9d
QuickJS: finalize an external buffer once when its finalizer detaches it
syrusakbary Nov 6, 2026
3a17cd6
QuickJS: report a detached ArrayBuffer's info as empty, as Node
syrusakbary Nov 6, 2026
815aec9
QuickJS: run an external buffer's finalizer once detached at release
syrusakbary Nov 6, 2026
695eb2f
V8: detach external buffers before their finalizers at release
syrusakbary Nov 6, 2026
c49e63d
V8: make node_api_create_buffer_from_arraybuffer view the ArrayBuffer
syrusakbary Nov 6, 2026
b41bc61
V8: finalize queued external buffers in their turn at release
syrusakbary Nov 6, 2026
14bf075
QuickJS: finalize buffers detached while the release runs in its pass
syrusakbary Nov 6, 2026
10fb03b
Document the release contract on unofficial_napi_release_env
syrusakbary Nov 6, 2026
83c5e19
QuickJS: free a dropped pending exception after taking it
syrusakbary Nov 6, 2026
2e0a067
QuickJS: keep the in-flight exception when a finalizer runs at release
syrusakbary Nov 6, 2026
95204fc
Drop a wrap from its object when the release finalizes it
syrusakbary Nov 6, 2026
4537eac
V8: leave queued external finalizers to the release pass in a checkpoint
syrusakbary Nov 6, 2026
93bf688
QuickJS: report views over a detached ArrayBuffer as empty, as Node
syrusakbary Nov 6, 2026
77d0f0d
QuickJS: call the instance-data finalizer for NULL data
syrusakbary Nov 6, 2026
78adfa6
Document the release contract's fixes and its transfer limitation
syrusakbary Nov 6, 2026
13c8c27
QuickJS: read a view's buffer through the intrinsic getters
syrusakbary Nov 6, 2026
d065cee
V8: forget an env's error formatting state after its release
syrusakbary Nov 6, 2026
eb19903
Bring back the contextify calls and version-1 option structs on realms
syrusakbary Nov 5, 2026
4643fcf
Restore the context-token hooks, as tokens on realms
syrusakbary Nov 5, 2026
34bdf3a
Test the deprecated API with main's contextify tests
syrusakbary Nov 5, 2026
b5cc2c7
Document the deprecated compatibility API
syrusakbary Nov 5, 2026
dd28ef1
Bring back the contextify guest imports, on realms
syrusakbary Nov 5, 2026
c352f38
Run main's run_script_test through the deprecated API
syrusakbary Nov 5, 2026
f90dcc1
Document the deprecated guest API
syrusakbary Nov 5, 2026
46b7521
Find a deprecated call's realm leaving no values in the caller's scope
syrusakbary Nov 6, 2026
870be92
Document the env's own global as a realm's sandbox in the deprecated API
syrusakbary Nov 6, 2026
5e65386
Document that host-JS contextify imports now refuse what main's accepted
syrusakbary Nov 6, 2026
2d7424a
Say the bytecode-open and module-create options must be zero-initialized
syrusakbary Nov 6, 2026
34a22d5
Say host-JS guests store at most one id for the current realm's global
syrusakbary Nov 6, 2026
fe78801
Point v1 bytecode and module options at run_script's QuickJS note
syrusakbary Nov 6, 2026
20a906f
Unassign the context token of every realm made during the env's release
syrusakbary Nov 6, 2026
7130535
Say a realm an unassign callback makes is unassigned in the same sweep
syrusakbary Nov 6, 2026
c711e50
Unassign a realm a cleanup hook makes at release before finalizers run
syrusakbary Nov 6, 2026
00309cd
Say which finalizers' realms keep tokens until the next release pass
syrusakbary Nov 6, 2026
1470845
QuickJS: read a view's length and offset from its slots
syrusakbary Nov 6, 2026
a23b155
QuickJS: keep the pending exception in napi_is_detached_arraybuffer
syrusakbary Nov 6, 2026
57d17eb
V8: return the serdes binding from outside its handle scope
syrusakbary Nov 6, 2026
8f98851
QuickJS: copy a view's own bytes in the serdes binding
syrusakbary Nov 6, 2026
9653cb2
QuickJS: read a view's data after the getters that can run script
syrusakbary Nov 6, 2026
4067980
V8: let a drain the hook runs after the release find no env
syrusakbary Nov 6, 2026
e2a9232
V8: forget the module loader's state at the end of an env's release
syrusakbary Nov 6, 2026
8f3621e
QuickJS: keep native code's pending exception from a callback it reaches
syrusakbary Nov 6, 2026
3079c0d
QuickJS: read a view's offset and length from one state of its buffer
syrusakbary Nov 6, 2026
7fd65a6
Reject an import() that nothing can load, on both engines
syrusakbary Nov 6, 2026
18f04e3
QuickJS: say what unofficial_napi_create_env_from_context trusts
syrusakbary Nov 6, 2026
faa05e8
Test a view's layout against its buffer as it is, in test_33
syrusakbary Nov 6, 2026
37ffb29
V8: reject an import() the module hooks have nothing to load with
syrusakbary Nov 6, 2026
3f474a7
QuickJS: read a view's offset and length again whenever a read threw
syrusakbary Nov 6, 2026
5d70b12
QuickJS: run native callbacks from the release's hooks and finalizers
syrusakbary Nov 6, 2026
6eebd49
Say an unassign callback's realm made outside a sweep waits for the next
syrusakbary Nov 6, 2026
ff53389
V8: evaluate a synthetic module whose steps are not a function
syrusakbary Nov 6, 2026
c5e1739
V8: throw what the import.meta callback throws from import.meta
syrusakbary Nov 6, 2026
9d4c25f
Refuse to release an env again while its release runs, on both engines
syrusakbary Nov 6, 2026
920dbd6
Pin each engine's outcome of an import() at release, in test_65
syrusakbary Nov 6, 2026
1f10832
V8: keep a termination the module hooks run into
syrusakbary Nov 6, 2026
5d4c943
V8: fail an import() whose default helper V8 terminated
syrusakbary Nov 6, 2026
65fe761
V8: refuse napi_fatal_exception while V8 terminates
syrusakbary Nov 6, 2026
ac8427a
V8: keep a termination the serializer's clone-error hook runs into
syrusakbary Nov 6, 2026
b1ac5ac
V8: keep a termination the promise reject callback runs into
syrusakbary Nov 6, 2026
5923973
Test that the promise and prepare-stack-trace hooks keep a termination
syrusakbary Nov 6, 2026
6159f5e
V8: keep a termination that formatting a module hook's error runs into
syrusakbary Nov 6, 2026
d922b70
V8: keep a termination an error's code setter runs into
syrusakbary Nov 6, 2026
ee09eb2
V8: keep a termination the serializer's shared-buffer getter runs into
syrusakbary Nov 6, 2026
d454d0d
Say memory pressure in a release leaves buffer finalizers to its pass
syrusakbary Nov 6, 2026
72eea76
V8: drop the termination's pending null when it is cancelled
syrusakbary Nov 6, 2026
2b83fec
V8: keep the embedder's termination when a script's timeout expires too
syrusakbary Nov 6, 2026
b3f0045
V8: keep a termination until it is cancelled
syrusakbary Nov 6, 2026
d58a614
Refuse N-API calls that run JS while the env terminates, on both engines
syrusakbary Nov 6, 2026
c1b2886
V8: keep a termination an interrupt callback's JS runs into
syrusakbary Nov 6, 2026
b74e789
V8: fail create_realm when an unassign hook it runs is terminated
syrusakbary Nov 6, 2026
a7f7d3e
Say what termination leaves pending, and QuickJS's prepareStackTrace gap
syrusakbary Nov 6, 2026
c741083
Test that cancelling drops the null a TryCatch records for a termination
syrusakbary Nov 6, 2026
21003ef
V8: leave the termination pending when it stops an unofficial call
syrusakbary Nov 6, 2026
455192b
V8: drop the termination's pending null when the release cancels it
syrusakbary Nov 6, 2026
d3286c4
V8: let a context-token hook's cancel drop the termination's null
syrusakbary Nov 6, 2026
8dff763
V8: fail the checkpoint when a task it runs is terminated
syrusakbary Nov 6, 2026
c0390de
Refuse error creators and V8's mark_promise_as_handled while terminating
syrusakbary Nov 6, 2026
f23ef70
QuickJS: keep native code's pending exception when a call is refused
syrusakbary Nov 6, 2026
1fe1ee7
V8: run no JS to stop a native function that no JS called
syrusakbary Nov 6, 2026
f9cea90
V8: ask V8 nothing when a native callback returns with nothing to do
syrusakbary Nov 6, 2026
483e7b0
Renumber the termination runners to test_75 and test_76
syrusakbary Nov 6, 2026
c3a6e57
V8: report a module that is not instantiated as settled in the TLA check
syrusakbary Nov 6, 2026
0aa4c40
V8: instantiate a module that is not instantiated before evaluating it
syrusakbary Nov 6, 2026
f623867
Refuse to evaluate a module while its evaluation runs, on both engines
syrusakbary Nov 6, 2026
128bd38
V8: start no module instantiation while one runs
syrusakbary Nov 6, 2026
bc10bce
Take only synthetic modules in set_export, on both engines
syrusakbary Nov 6, 2026
5433d1a
Throw a ReferenceError from set_export for a binding that does not exist
syrusakbary Nov 6, 2026
6022956
QuickJS: refuse the namespace of a module that is not instantiated
syrusakbary Nov 6, 2026
f410ebe
V8: instantiate the facade's module with its links, report its errors
syrusakbary Nov 6, 2026
0a6dac3
Undefine NAPI_MODULE before test_65 includes node_api.h
syrusakbary Nov 6, 2026
8c0b960
Undefine NAPI_MODULE before test_75 and test_76 include node_api.h
syrusakbary Nov 6, 2026
9c122a7
Document known limitations: views, token hooks, upgrading from next
syrusakbary Nov 6, 2026
307800c
Refuse to evaluate a module that imports an evaluating synthetic module
syrusakbary Nov 6, 2026
3fc90b6
QuickJS: start no module link while one runs
syrusakbary Nov 6, 2026
ee92f4a
V8: give the facade's instantiation the source-phase resolver
syrusakbary Nov 6, 2026
eedc878
V8: fail the require() facade of a module that throws or awaits
syrusakbary Nov 6, 2026
3ecd094
QuickJS: leave an evaluating module linked, and link none through one
syrusakbary Nov 6, 2026
ea0d15b
Say what an f928811 bytecode-open struct does now, not "with no error"
syrusakbary Nov 6, 2026
204fe49
Merge main into next
syrusakbary Nov 6, 2026
946cf55
Say what memory_pressure finalizes, and that it frees no realm record
syrusakbary Nov 6, 2026
1ea4aa1
Say a terminating env's checkpoint leaves pressure's finalizers queued
syrusakbary Nov 6, 2026
8a80a47
Keep test_71's token log alive until the env is released
syrusakbary Nov 6, 2026
80b591e
Say only V8's collect_garbage frees the records of collected realms
syrusakbary Nov 6, 2026
4a6e4b4
Renumber the termination-until-cancelled runner to test_78
syrusakbary Nov 6, 2026
08d7d90
Merge main into next
syrusakbary Nov 6, 2026
e45b116
Wake the host at a cancel for the tasks a termination left queued
syrusakbary Nov 6, 2026
ab41860
Fix module lifetime, realm and termination review findings
syrusakbary Nov 6, 2026
f564b7a
Document all PR 127 review findings, fixes and remaining work
syrusakbary Nov 6, 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
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn main() {
.unwrap(),
)
.file(napi_lib_src.join("napi_buffer_lease.cc").to_str().unwrap())
.file(napi_lib_src.join("napi_compat.cc").to_str().unwrap())
.file(napi_lib_src.join("napi_periodic_gate.cc").to_str().unwrap())
.file(napi_lib_src.join("napi_text.cc").to_str().unwrap())
.file(
Expand Down
558 changes: 558 additions & 0 deletions docs/pr127-review-followups.md

Large diffs are not rendered by default.

434 changes: 434 additions & 0 deletions docs/realms.md

Large diffs are not rendered by default.

535 changes: 509 additions & 26 deletions include/unofficial_napi.h

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions include/unofficial_napi_wasm32_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ typedef struct {
uint8_t reserved[2];
} unofficial_napi_wasm32_bytecode_open_options_v1;

// Version 2 appends the realm.
typedef struct {
unofficial_napi_wasm32_bytecode_open_options_v1 v1;
uint32_t realm_or_null;
} unofficial_napi_wasm32_bytecode_open_options_v2;

typedef struct {
uint32_t size;
uint32_t version;
Expand All @@ -70,21 +76,43 @@ typedef struct {
uint8_t payload[16];
} unofficial_napi_wasm32_module_create_options_v1;

// Version 2 appends the realm.
typedef struct {
unofficial_napi_wasm32_module_create_options_v1 v1;
uint32_t realm_or_null;
} unofficial_napi_wasm32_module_create_options_v2;

typedef struct {
uint32_t size;
uint32_t version;
uint32_t import_module_dynamically;
uint32_t initialize_import_meta_object;
} unofficial_napi_wasm32_module_hooks_v1;

typedef struct {
uint32_t size;
uint32_t version;
uint32_t sandbox_or_null;
uint32_t name;
uint32_t origin;
uint32_t host_defined_option_id;
uint8_t allow_code_gen_strings;
uint8_t allow_code_gen_wasm;
uint8_t own_microtask_queue;
uint8_t reserved;
} unofficial_napi_wasm32_realm_options_v1;

#if defined(__cplusplus)
static_assert(sizeof(unofficial_napi_wasm32_runtime_options_v1) == 16);
static_assert(sizeof(unofficial_napi_wasm32_env_create_options_v1) == 48);
static_assert(sizeof(unofficial_napi_wasm32_env_hooks_v1) == 32);
static_assert(sizeof(unofficial_napi_wasm32_js_source) == 12);
static_assert(sizeof(unofficial_napi_wasm32_bytecode_open_options_v1) == 48);
static_assert(sizeof(unofficial_napi_wasm32_bytecode_open_options_v2) == 52);
static_assert(sizeof(unofficial_napi_wasm32_module_create_options_v1) == 40);
static_assert(sizeof(unofficial_napi_wasm32_module_create_options_v2) == 44);
static_assert(sizeof(unofficial_napi_wasm32_module_hooks_v1) == 16);
static_assert(sizeof(unofficial_napi_wasm32_realm_options_v1) == 28);
#else
_Static_assert(sizeof(unofficial_napi_wasm32_runtime_options_v1) == 16,
"wasm32 runtime-options wire layout changed");
Expand All @@ -96,10 +124,16 @@ _Static_assert(sizeof(unofficial_napi_wasm32_js_source) == 12,
"wasm32 JS-source wire layout changed");
_Static_assert(sizeof(unofficial_napi_wasm32_bytecode_open_options_v1) == 48,
"wasm32 bytecode-open wire layout changed");
_Static_assert(sizeof(unofficial_napi_wasm32_bytecode_open_options_v2) == 52,
"wasm32 bytecode-open wire layout changed");
_Static_assert(sizeof(unofficial_napi_wasm32_module_create_options_v1) == 40,
"wasm32 module-create wire layout changed");
_Static_assert(sizeof(unofficial_napi_wasm32_module_create_options_v2) == 44,
"wasm32 module-create wire layout changed");
_Static_assert(sizeof(unofficial_napi_wasm32_module_hooks_v1) == 16,
"wasm32 module-hooks wire layout changed");
_Static_assert(sizeof(unofficial_napi_wasm32_realm_options_v1) == 28,
"wasm32 realm-options wire layout changed");
#endif

#endif // UNOFFICIAL_NAPI_WASM32_ABI_H_
226 changes: 226 additions & 0 deletions lib/src/napi_compat.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// This file implements the deprecated API.
#define UNOFFICIAL_NAPI_NO_DEPRECATION_WARNINGS
#include "napi_compat.h"

namespace napi::compat {
namespace {

bool is_nullish(napi_env env, napi_value value, napi_valuetype* type_out) {
*type_out = napi_undefined;
if (value == nullptr) return true;
if (napi_typeof(env, value, type_out) != napi_ok) {
*type_out = napi_undefined;
return false;
}
return *type_out == napi_undefined || *type_out == napi_null;
}

// Runs `lookup` in a handle scope of its own, so that the values it makes
// (globals, mostly) are not left in the caller's scope, which may be one that
// never closes. The realm it finds stays valid after the scope closes: on V8
// and QuickJS a realm handle is the provider's record of the realm, freed only
// by a later unofficial_napi_create_realm or unofficial_napi_collect_garbage
// once the realm is collected, and what named the realm keeps it alive (the
// caller's context value, or the entered scope or running callback of the
// current realm).
template <typename Lookup>
napi_status in_lookup_scope(napi_env env, Lookup lookup) {
napi_handle_scope scope = nullptr;
napi_status status = napi_open_handle_scope(env, &scope);
if (status != napi_ok) return status;
status = lookup();
const napi_status closed = napi_close_handle_scope(env, scope);
return status != napi_ok ? status : closed;
}

// The env's own global, whichever realm is current.
napi_status env_global(napi_env env, napi_value* global_out) {
unofficial_napi_realm_scope scope = 0;
napi_status status = unofficial_napi_enter_realm(env, nullptr, &scope);
if (status != napi_ok) return status;
status = napi_get_global(env, global_out);
const napi_status left = unofficial_napi_leave_realm(env, scope);
return status != napi_ok ? status : left;
}

napi_status is_env_global(napi_env env, napi_value value, bool* result_out) {
napi_value global = nullptr;
napi_status status = env_global(env, &global);
if (status != napi_ok) return status;
return napi_strict_equals(env, value, global, result_out);
}

// current_realm, making values in the caller's scope.
napi_status find_current_realm(napi_env env, unofficial_napi_realm* realm_out) {
napi_value global = nullptr;
napi_status status = napi_get_global(env, &global);
if (status != napi_ok) return status;
unofficial_napi_realm realm = nullptr;
status = unofficial_napi_get_realm(env, global, &realm);
if (status != napi_ok || realm == nullptr) return status;
bool env_own = false;
status = is_env_global(env, global, &env_own);
if (status != napi_ok) return status;
if (!env_own) *realm_out = realm;
return napi_ok;
}

} // namespace

napi_status current_realm(napi_env env, unofficial_napi_realm* realm_out) {
*realm_out = nullptr;
return in_lookup_scope(env, [&] { return find_current_realm(env, realm_out); });
}

napi_status realm_from_context(napi_env env,
napi_value context,
bool nullish_is_current,
unofficial_napi_realm* realm_out) {
*realm_out = nullptr;
napi_valuetype type = napi_undefined;
if (is_nullish(env, context, &type)) {
return nullish_is_current ? current_realm(env, realm_out) : napi_ok;
}
if (type != napi_object && type != napi_function) return napi_invalid_arg;
if (unofficial_napi_get_realm(env, context, realm_out) != napi_ok) return napi_invalid_arg;
if (*realm_out != nullptr) return napi_ok;
bool env_own = false;
const napi_status status =
in_lookup_scope(env, [&] { return is_env_global(env, context, &env_own); });
if (status != napi_ok) return status;
return env_own ? napi_ok : napi_invalid_arg;
}

napi_status bytecode_open_realm(napi_env env,
const unofficial_napi_bytecode_open_options* options,
unofficial_napi_realm* realm_out) {
*realm_out = nullptr;
if (options == nullptr) return napi_invalid_arg;
if (options->version == 1) {
if (options->size < UNOFFICIAL_NAPI_BYTECODE_OPEN_OPTIONS_V1_SIZE) return napi_invalid_arg;
return current_realm(env, realm_out);
}
if (options->version != UNOFFICIAL_NAPI_BYTECODE_OPEN_OPTIONS_VERSION ||
options->size < sizeof(*options)) {
return napi_invalid_arg;
}
*realm_out = options->realm_or_null;
return napi_ok;
}

napi_status module_create_realm(napi_env env,
const unofficial_napi_module_create_options* options,
unofficial_napi_realm* realm_out) {
*realm_out = nullptr;
if (options == nullptr) return napi_invalid_arg;
const bool v1 = options->version == 1;
if (v1 ? options->size < UNOFFICIAL_NAPI_MODULE_CREATE_OPTIONS_V1_SIZE
: options->version != UNOFFICIAL_NAPI_MODULE_CREATE_OPTIONS_VERSION ||
options->size < sizeof(*options)) {
return napi_invalid_arg;
}
if (!v1 && options->realm_or_null != nullptr) {
napi_valuetype type = napi_undefined;
if (!is_nullish(env, options->context_or_undefined, &type)) return napi_invalid_arg;
*realm_out = options->realm_or_null;
return napi_ok;
}
return realm_from_context(env, options->context_or_undefined, v1, realm_out);
}

} // namespace napi::compat

extern "C" {

napi_status NAPI_CDECL unofficial_napi_contextify_make_context(
napi_env env,
napi_value sandbox_or_symbol,
napi_value name,
napi_value origin_or_undefined,
bool allow_code_gen_strings,
bool allow_code_gen_wasm,
bool own_microtask_queue,
napi_value host_defined_option_id,
napi_value* result_out) {
if (env == nullptr || sandbox_or_symbol == nullptr || name == nullptr || result_out == nullptr) {
return napi_invalid_arg;
}
napi_valuetype type = napi_undefined;
if (napi_typeof(env, sandbox_or_symbol, &type) != napi_ok ||
(type != napi_symbol && type != napi_object && type != napi_function)) {
return napi_invalid_arg;
}
unofficial_napi_realm_options options;
unofficial_napi_realm_options_init(&options);
options.sandbox_or_null = type == napi_symbol ? nullptr : sandbox_or_symbol;
options.name = name;
options.origin = origin_or_undefined;
options.host_defined_option_id = host_defined_option_id;
options.allow_code_gen_strings = allow_code_gen_strings;
options.allow_code_gen_wasm = allow_code_gen_wasm;
options.own_microtask_queue = own_microtask_queue;
unofficial_napi_realm realm = nullptr;
return unofficial_napi_create_realm(env, &options, &realm, result_out);
}

napi_status NAPI_CDECL unofficial_napi_contextify_run_script(
napi_env env,
napi_value sandbox_or_null,
const unofficial_napi_js_source* source,
napi_value filename,
int32_t line_offset,
int32_t column_offset,
int64_t timeout,
bool display_errors,
bool break_on_sigint,
bool break_on_first_line,
napi_value host_defined_option_id,
napi_value* result_out) {
if (env == nullptr) return napi_invalid_arg;
unofficial_napi_realm realm = nullptr;
const napi_status status =
napi::compat::realm_from_context(env, sandbox_or_null, true, &realm);
if (status != napi_ok) return status;
return unofficial_napi_run_script(env,
realm,
source,
filename,
line_offset,
column_offset,
timeout,
display_errors,
break_on_sigint,
break_on_first_line,
host_defined_option_id,
result_out);
}

napi_status NAPI_CDECL unofficial_napi_contextify_compile_function(
napi_env env,
const unofficial_napi_js_source* source,
napi_value filename,
int32_t line_offset,
int32_t column_offset,
napi_value parsing_context_or_undefined,
napi_value context_extensions_or_undefined,
napi_value params_or_undefined,
napi_value host_defined_option_id,
napi_value* result_out) {
if (env == nullptr) return napi_invalid_arg;
unofficial_napi_realm realm = nullptr;
const napi_status status =
napi::compat::realm_from_context(env, parsing_context_or_undefined, true, &realm);
if (status != napi_ok) return status;
return unofficial_napi_compile_function(env,
realm,
source,
filename,
line_offset,
column_offset,
context_extensions_or_undefined,
params_or_undefined,
host_defined_option_id,
result_out);
}

} // extern "C"
45 changes: 45 additions & 0 deletions lib/src/napi_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef NAPI_COMPAT_H_
#define NAPI_COMPAT_H_

#include "unofficial_napi.h"

// The deprecated API (the contextify calls and version 1 of the option
// structs) on the public realm API, the same for every provider. Where it
// named a context it named a contextified object, the env's own global, or
// nothing for the current context, which is the current realm now. Finding
// the realm leaves no value in the caller's handle scope.
namespace napi::compat {

// The current realm: NULL for the env's own. It is the realm of the global
// napi_get_global returns, except that the env's own global means the env's
// realm even when it is also a realm's sandbox, as the env's context did
// before realms existed. (So on QuickJS, where napi_get_global returns a
// sandbox realm's sandbox, it is the env's realm also while the realm whose
// sandbox is the env's own global is current.)
napi_status current_realm(napi_env env, unofficial_napi_realm* realm_out);

// The realm `context` names: a contextified object's (a realm's global or
// sandbox, which the env's own global may be), the env's (NULL) for the env's
// own global otherwise, and for NULL, undefined or null the current realm, or
// the env's when `nullish_is_current` is false. Any other value fails with
// napi_invalid_arg.
napi_status realm_from_context(napi_env env,
napi_value context,
bool nullish_is_current,
unofficial_napi_realm* realm_out);

// Checks the size and version of bytecode-open options (version 1 or 2) and
// returns the realm to compile for: version 1 compiles for current_realm's.
napi_status bytecode_open_realm(napi_env env,
const unofficial_napi_bytecode_open_options* options,
unofficial_napi_realm* realm_out);

// Checks the size and version of module-create options (version 1 or 2) and
// returns the realm the module lives in, as unofficial_napi.h describes.
napi_status module_create_realm(napi_env env,
const unofficial_napi_module_create_options* options,
unofficial_napi_realm* realm_out);

} // namespace napi::compat

#endif // NAPI_COMPAT_H_
17 changes: 17 additions & 0 deletions lib/src/napi_testing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef NAPI_TESTING_H_
#define NAPI_TESTING_H_

#include <cstddef>

#include "js_native_api.h"

// For tests only: what the public API cannot observe. Each provider
// implements it.
namespace napi::testing {

// The number of napi_values the open handle scopes of `env` hold.
size_t open_value_count(napi_env env);

} // namespace napi::testing

#endif // NAPI_TESTING_H_
2 changes: 2 additions & 0 deletions quickjs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ file(GLOB_RECURSE NAPI_QUICKJS_SRC_FILES

add_library(napi_quickjs STATIC
../lib/src/napi_buffer_lease.cc
../lib/src/napi_compat.cc
../lib/src/napi_error_state.cc
../lib/src/napi_lifetime_tracker.cc
../lib/src/napi_periodic_gate.cc
../lib/src/napi_text.cc
../lib/src/napi_typedarray_metadata.cc
src/js_native_api_quickjs.cc
src/internal/napi_agent.cc
src/internal/napi_callback_info.cc
src/internal/napi_buffer.cc
src/internal/napi_callsite.cc
Expand Down
Loading
Loading