nuxt-ai-ready 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/agent-skills.mjs +1 -1
- package/dist/chunks/prerender.mjs +1 -1
- package/dist/cli.mjs +2 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/server/db/drizzle/queries.js +0 -1
- package/dist/runtime/server/db/queries.d.ts +2 -0
- package/dist/runtime/server/db/queries.js +11 -5
- package/dist/runtime/server/db/seed.d.ts +2 -2
- package/dist/runtime/server/routes/__ai-ready/reindex.post.js +2 -0
- package/dist/runtime/server/utils/batchIndex.js +4 -2
- package/dist/runtime/server/utils/checkStale.d.ts +1 -1
- package/dist/runtime/server/utils/checkStale.js +1 -1
- package/dist/runtime/server/utils/indexPage.d.ts +8 -1
- package/dist/runtime/server/utils/indexPage.js +13 -5
- package/dist/shared/{nuxt-ai-ready.DsokNmXc.mjs → nuxt-ai-ready.CLSWou20.mjs} +2 -0
- package/node_modules/@mdream/js/LICENSE.md +9 -0
- package/node_modules/@mdream/js/README.md +792 -0
- package/node_modules/@mdream/js/bin/mdream.mjs +2 -0
- package/node_modules/@mdream/js/dist/_chunks/const.mjs +134 -0
- package/node_modules/@mdream/js/dist/_chunks/index.d.mts +9 -0
- package/node_modules/@mdream/js/dist/_chunks/markdown-processor.mjs +1968 -0
- package/node_modules/@mdream/js/dist/_chunks/minimal.d.mts +6 -0
- package/node_modules/@mdream/js/dist/_chunks/parse.mjs +2756 -0
- package/node_modules/@mdream/js/dist/_chunks/plugins.mjs +725 -0
- package/node_modules/@mdream/js/dist/_chunks/resolve-plugins.mjs +44 -0
- package/node_modules/@mdream/js/dist/_chunks/stream.mjs +37 -0
- package/node_modules/@mdream/js/dist/_chunks/types.d.mts +473 -0
- package/node_modules/@mdream/js/dist/cli.d.mts +1 -0
- package/node_modules/@mdream/js/dist/cli.mjs +32 -0
- package/node_modules/@mdream/js/dist/core.d.mts +15 -0
- package/node_modules/@mdream/js/dist/core.mjs +28 -0
- package/node_modules/@mdream/js/dist/index.d.mts +4 -0
- package/node_modules/@mdream/js/dist/index.mjs +58 -0
- package/node_modules/@mdream/js/dist/llms-txt.d.mts +86 -0
- package/node_modules/@mdream/js/dist/llms-txt.mjs +308 -0
- package/node_modules/@mdream/js/dist/negotiate.d.mts +35 -0
- package/node_modules/@mdream/js/dist/negotiate.mjs +121 -0
- package/node_modules/@mdream/js/dist/parse.d.mts +90 -0
- package/node_modules/@mdream/js/dist/parse.mjs +2 -0
- package/node_modules/@mdream/js/dist/plugins.d.mts +85 -0
- package/node_modules/@mdream/js/dist/plugins.mjs +2 -0
- package/node_modules/@mdream/js/dist/preset/minimal.d.mts +2 -0
- package/node_modules/@mdream/js/dist/preset/minimal.mjs +33 -0
- package/node_modules/@mdream/js/dist/splitter.d.mts +18 -0
- package/node_modules/@mdream/js/dist/splitter.mjs +194 -0
- package/node_modules/@mdream/js/package.json +103 -0
- package/node_modules/cac/LICENSE +21 -0
- package/node_modules/cac/README.md +492 -0
- package/node_modules/cac/dist/index.d.ts +196 -0
- package/node_modules/cac/dist/index.js +638 -0
- package/node_modules/cac/package.json +72 -0
- package/node_modules/mdream/LICENSE.md +9 -0
- package/node_modules/mdream/README.md +1091 -0
- package/node_modules/mdream/bin/mdream.mjs +46 -0
- package/node_modules/mdream/dist/browser.d.mts +105 -0
- package/node_modules/mdream/dist/browser.mjs +262 -0
- package/node_modules/mdream/dist/edge.d.mts +100 -0
- package/node_modules/mdream/dist/edge.mjs +250 -0
- package/node_modules/mdream/dist/iife.js +605 -0
- package/node_modules/mdream/dist/index.d.mts +89 -0
- package/node_modules/mdream/dist/index.mjs +184 -0
- package/node_modules/mdream/dist/wasm.d.mts +100 -0
- package/node_modules/mdream/dist/wasm.mjs +140 -0
- package/node_modules/mdream/dist/worker.d.mts +101 -0
- package/node_modules/mdream/dist/worker.mjs +217 -0
- package/node_modules/mdream/napi/index.d.mts +127 -0
- package/node_modules/mdream/napi/index.d.ts +127 -0
- package/node_modules/mdream/napi/index.mjs +725 -0
- package/node_modules/mdream/napi/rust.android-arm-eabi.node +0 -0
- package/node_modules/mdream/napi/rust.android-arm64.node +0 -0
- package/node_modules/mdream/napi/rust.darwin-arm64.node +0 -0
- package/node_modules/mdream/napi/rust.darwin-x64.node +0 -0
- package/node_modules/mdream/napi/rust.freebsd-x64.node +0 -0
- package/node_modules/mdream/napi/rust.linux-arm-gnueabihf.node +0 -0
- package/node_modules/mdream/napi/rust.linux-arm64-gnu.node +0 -0
- package/node_modules/mdream/napi/rust.linux-arm64-musl.node +0 -0
- package/node_modules/mdream/napi/rust.linux-x64-gnu.node +0 -0
- package/node_modules/mdream/napi/rust.linux-x64-musl.node +0 -0
- package/node_modules/mdream/napi/rust.wasi.cjs +2144 -0
- package/node_modules/mdream/napi/rust.wasm32-wasi.wasm +0 -0
- package/node_modules/mdream/napi/rust.win32-arm64-msvc.node +0 -0
- package/node_modules/mdream/napi/rust.win32-x64-msvc.node +0 -0
- package/node_modules/mdream/node_modules/@emnapi/core/LICENSE +21 -0
- package/node_modules/mdream/node_modules/@emnapi/core/README.md +1 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.d.ts +392 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.full.d.ts +392 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.full.js +8351 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/emnapi-core.js +7268 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/async-work.d.ts +5 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/async-work.js +843 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/index.d.ts +4 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/index.js +3 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/threadsafe-function.d.ts +5 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/threadsafe-function.js +1010 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/v8.d.ts +5 -0
- package/node_modules/mdream/node_modules/@emnapi/core/dist/plugins/v8.js +1446 -0
- package/node_modules/mdream/node_modules/@emnapi/core/package.json +39 -0
- package/node_modules/mdream/node_modules/@emnapi/core/rollup.config.js +60 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/LICENSE +21 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/README.md +1 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/dist/emnapi.d.ts +833 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/dist/emnapi.js +2306 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/dist/emnapi.js.map +1 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/index.cjs +63 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/package.json +39 -0
- package/node_modules/mdream/node_modules/@emnapi/runtime/rollup.config.js +25 -0
- package/node_modules/mdream/node_modules/@emnapi/wasi-threads/LICENSE +21 -0
- package/node_modules/mdream/node_modules/@emnapi/wasi-threads/README.md +203 -0
- package/node_modules/mdream/node_modules/@emnapi/wasi-threads/dist/wasi-threads.d.ts +236 -0
- package/node_modules/mdream/node_modules/@emnapi/wasi-threads/dist/wasi-threads.js +1086 -0
- package/node_modules/mdream/node_modules/@emnapi/wasi-threads/package.json +35 -0
- package/node_modules/mdream/node_modules/@emnapi/wasi-threads/rollup.config.js +6 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.cjs +1472 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/emnapi-plugins.js +1470 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/fs-proxy.cjs +237 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/dist/fs.js +18260 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/fs-proxy.js +317 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/package.json +67 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/runtime.cjs +39 -0
- package/node_modules/mdream/node_modules/@napi-rs/wasm-runtime/runtime.js +36 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/LICENSE +21 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/README.md +193 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/tsdoc-metadata.json +11 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.d.ts +423 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js +2932 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.esm.js +2932 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.esm.min.js +1 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.js +2954 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/dist/wasm-util.min.js +1 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/asyncify.js +158 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/index.js +13 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/jspi.js +46 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/load.js +97 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/memory.js +35 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/error.js +103 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/fd.js +268 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/fs.js +3 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/index.js +192 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/path.js +316 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/preview1.js +1599 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/rights.js +141 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/types.js +220 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/wasi/util.js +128 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/cjs/webassembly.js +13 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/asyncify.mjs +153 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/index.mjs +9 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/jspi.mjs +39 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/load.mjs +89 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/memory.mjs +28 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/error.mjs +97 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/fd.mjs +256 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/fs.mjs +1 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/index.mjs +186 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/path.mjs +310 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/preview1.mjs +1593 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/rights.mjs +135 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/types.mjs +216 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/wasi/util.mjs +113 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/lib/mjs/webassembly.mjs +9 -0
- package/node_modules/mdream/node_modules/@tybys/wasm-util/package.json +58 -0
- package/node_modules/mdream/node_modules/tslib/CopyrightNotice.txt +15 -0
- package/node_modules/mdream/node_modules/tslib/LICENSE.txt +12 -0
- package/node_modules/mdream/node_modules/tslib/README.md +164 -0
- package/node_modules/mdream/node_modules/tslib/SECURITY.md +41 -0
- package/node_modules/mdream/node_modules/tslib/modules/index.d.ts +38 -0
- package/node_modules/mdream/node_modules/tslib/modules/index.js +70 -0
- package/node_modules/mdream/node_modules/tslib/modules/package.json +3 -0
- package/node_modules/mdream/node_modules/tslib/package.json +47 -0
- package/node_modules/mdream/node_modules/tslib/tslib.d.ts +460 -0
- package/node_modules/mdream/node_modules/tslib/tslib.es6.html +1 -0
- package/node_modules/mdream/node_modules/tslib/tslib.es6.js +402 -0
- package/node_modules/mdream/node_modules/tslib/tslib.es6.mjs +401 -0
- package/node_modules/mdream/node_modules/tslib/tslib.html +1 -0
- package/node_modules/mdream/node_modules/tslib/tslib.js +484 -0
- package/node_modules/mdream/package.json +110 -0
- package/node_modules/mdream/skills/mdream/SKILL.md +124 -0
- package/node_modules/mdream/skills/mdream/references/runtimes.md +49 -0
- package/node_modules/mdream/wasm/mdream_edge.d.ts +90 -0
- package/node_modules/mdream/wasm/mdream_edge.js +517 -0
- package/node_modules/mdream/wasm/mdream_edge_bg.wasm +0 -0
- package/node_modules/mdream/wasm/mdream_edge_bg.wasm.d.ts +20 -0
- package/node_modules/mdream/wasm/package.json +17 -0
- package/node_modules/mdream/wasm-bundler/mdream_edge.d.ts +45 -0
- package/node_modules/mdream/wasm-bundler/mdream_edge.js +13 -0
- package/node_modules/mdream/wasm-bundler/mdream_edge_bg.js +413 -0
- package/node_modules/mdream/wasm-bundler/mdream_edge_bg.wasm +0 -0
- package/node_modules/mdream/wasm-bundler/mdream_edge_bg.wasm.d.ts +20 -0
- package/node_modules/mdream/wasm-bundler/package.json +19 -0
- package/node_modules/pathe/LICENSE +70 -0
- package/node_modules/pathe/README.md +73 -0
- package/node_modules/pathe/dist/index.cjs +39 -0
- package/node_modules/pathe/dist/index.d.cts +47 -0
- package/node_modules/pathe/dist/index.d.mts +47 -0
- package/node_modules/pathe/dist/index.d.ts +47 -0
- package/node_modules/pathe/dist/index.mjs +19 -0
- package/node_modules/pathe/dist/shared/pathe.BSlhyZSM.cjs +266 -0
- package/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs +249 -0
- package/node_modules/pathe/dist/utils.cjs +82 -0
- package/node_modules/pathe/dist/utils.d.cts +32 -0
- package/node_modules/pathe/dist/utils.d.mts +32 -0
- package/node_modules/pathe/dist/utils.d.ts +32 -0
- package/node_modules/pathe/dist/utils.mjs +77 -0
- package/node_modules/pathe/package.json +61 -0
- package/node_modules/pathe/utils.d.ts +1 -0
- package/package.json +8 -64
|
@@ -0,0 +1,2306 @@
|
|
|
1
|
+
//#region src/util.ts
|
|
2
|
+
function detectFinalizer() {
|
|
3
|
+
return (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined');
|
|
4
|
+
}
|
|
5
|
+
const supportFinalizer = detectFinalizer();
|
|
6
|
+
function detectFeatures(features) {
|
|
7
|
+
const supportNewFunction = (function () {
|
|
8
|
+
let f;
|
|
9
|
+
try {
|
|
10
|
+
f = new Function();
|
|
11
|
+
}
|
|
12
|
+
catch (_) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return typeof f === 'function';
|
|
16
|
+
})();
|
|
17
|
+
const canSetFunctionName = (function () {
|
|
18
|
+
try {
|
|
19
|
+
return Boolean(Object.getOwnPropertyDescriptor(Function.prototype, 'name')?.configurable);
|
|
20
|
+
}
|
|
21
|
+
catch (_) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
})();
|
|
25
|
+
const weakSymbol = (function () {
|
|
26
|
+
try {
|
|
27
|
+
const sym = Symbol();
|
|
28
|
+
new WeakRef(sym);
|
|
29
|
+
new WeakMap().set(sym, undefined);
|
|
30
|
+
}
|
|
31
|
+
catch (_) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
})();
|
|
36
|
+
const _require = (function () {
|
|
37
|
+
let nativeRequire;
|
|
38
|
+
if (typeof __webpack_public_path__ !== 'undefined') {
|
|
39
|
+
nativeRequire = (function () {
|
|
40
|
+
return typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined;
|
|
41
|
+
})();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
nativeRequire = (function () {
|
|
45
|
+
return typeof __webpack_public_path__ !== 'undefined' ? (typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined) : (typeof require !== 'undefined' ? require : undefined);
|
|
46
|
+
})();
|
|
47
|
+
}
|
|
48
|
+
return nativeRequire;
|
|
49
|
+
})();
|
|
50
|
+
function defaultGetGlobalThis() {
|
|
51
|
+
if (typeof globalThis !== 'undefined')
|
|
52
|
+
return globalThis;
|
|
53
|
+
let g = (function () { return this; })();
|
|
54
|
+
if (!g && supportNewFunction) {
|
|
55
|
+
try {
|
|
56
|
+
g = new Function('return this')();
|
|
57
|
+
}
|
|
58
|
+
catch (_) { }
|
|
59
|
+
}
|
|
60
|
+
if (!g) {
|
|
61
|
+
if (typeof __webpack_public_path__ === 'undefined') {
|
|
62
|
+
if (typeof global !== 'undefined')
|
|
63
|
+
return global;
|
|
64
|
+
}
|
|
65
|
+
if (typeof window !== 'undefined')
|
|
66
|
+
return window;
|
|
67
|
+
if (typeof self !== 'undefined')
|
|
68
|
+
return self;
|
|
69
|
+
}
|
|
70
|
+
throw new Error('Unable to get globalThis');
|
|
71
|
+
}
|
|
72
|
+
function defaultWithResolvers() {
|
|
73
|
+
let ok;
|
|
74
|
+
let err;
|
|
75
|
+
const promise = new this((resolve, reject) => {
|
|
76
|
+
ok = resolve;
|
|
77
|
+
err = reject;
|
|
78
|
+
});
|
|
79
|
+
return { promise, resolve: ok, reject: err };
|
|
80
|
+
}
|
|
81
|
+
const getGlobalThis = features && 'getGlobalThis' in features
|
|
82
|
+
? (features.getGlobalThis ?? defaultGetGlobalThis)
|
|
83
|
+
: defaultGetGlobalThis;
|
|
84
|
+
const withResolvers = (features && 'withResolvers' in features)
|
|
85
|
+
? (features.withResolvers ?? defaultWithResolvers)
|
|
86
|
+
: typeof Promise.withResolvers === 'function'
|
|
87
|
+
? Promise.withResolvers
|
|
88
|
+
: defaultWithResolvers;
|
|
89
|
+
const getBuiltinModule = getGlobalThis?.().process?.getBuiltinModule ?? _require;
|
|
90
|
+
const ret = {
|
|
91
|
+
makeDynamicFunction: (supportNewFunction
|
|
92
|
+
? (...args) => (new Function(...args))
|
|
93
|
+
: undefined),
|
|
94
|
+
setFunctionName: (canSetFunctionName
|
|
95
|
+
? (f, name) => { Object.defineProperty(f, 'name', { value: name }); }
|
|
96
|
+
: undefined),
|
|
97
|
+
Reflect: typeof Reflect === 'object' ? Reflect : undefined,
|
|
98
|
+
finalizer: detectFinalizer(),
|
|
99
|
+
weakSymbol,
|
|
100
|
+
BigInt: typeof BigInt !== 'undefined' ? BigInt : undefined,
|
|
101
|
+
MessageChannel: typeof MessageChannel === 'function'
|
|
102
|
+
? MessageChannel
|
|
103
|
+
: (function () {
|
|
104
|
+
try {
|
|
105
|
+
return getBuiltinModule('worker_threads').MessageChannel;
|
|
106
|
+
}
|
|
107
|
+
catch (_) { }
|
|
108
|
+
return undefined;
|
|
109
|
+
})(),
|
|
110
|
+
setImmediate: typeof setImmediate === 'function'
|
|
111
|
+
? setImmediate.bind(getGlobalThis())
|
|
112
|
+
: function (callback) {
|
|
113
|
+
if (typeof callback !== 'function') {
|
|
114
|
+
throw new TypeError('The "callback" argument must be of type function');
|
|
115
|
+
}
|
|
116
|
+
if (ret.MessageChannel) {
|
|
117
|
+
let channel = new ret.MessageChannel();
|
|
118
|
+
channel.port1.onmessage = function () {
|
|
119
|
+
channel.port1.onmessage = null;
|
|
120
|
+
channel = undefined;
|
|
121
|
+
callback();
|
|
122
|
+
};
|
|
123
|
+
channel.port2.postMessage(null);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
setTimeout(callback, 0);
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
Buffer: typeof Buffer === 'function'
|
|
130
|
+
? Buffer
|
|
131
|
+
: (function () {
|
|
132
|
+
try {
|
|
133
|
+
return getBuiltinModule('buffer').Buffer;
|
|
134
|
+
}
|
|
135
|
+
catch (_) { }
|
|
136
|
+
return undefined;
|
|
137
|
+
})(),
|
|
138
|
+
...features,
|
|
139
|
+
getGlobalThis,
|
|
140
|
+
withResolvers
|
|
141
|
+
};
|
|
142
|
+
return ret;
|
|
143
|
+
}
|
|
144
|
+
function isReferenceType(v) {
|
|
145
|
+
return (typeof v === 'object' && v !== null) || typeof v === 'function';
|
|
146
|
+
}
|
|
147
|
+
function getDylinkMetadata(binary) {
|
|
148
|
+
let offset = 0;
|
|
149
|
+
let end = 0;
|
|
150
|
+
const decoder = new TextDecoder();
|
|
151
|
+
function getU8() {
|
|
152
|
+
return binary[offset++];
|
|
153
|
+
}
|
|
154
|
+
function getLEB() {
|
|
155
|
+
let ret = 0;
|
|
156
|
+
let mul = 1;
|
|
157
|
+
while (1) {
|
|
158
|
+
let byte = binary[offset++];
|
|
159
|
+
ret += ((byte & 0x7f) * mul);
|
|
160
|
+
mul *= 0x80;
|
|
161
|
+
if (!(byte & 0x80))
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
return ret;
|
|
165
|
+
}
|
|
166
|
+
function getString() {
|
|
167
|
+
let len = getLEB();
|
|
168
|
+
offset += len;
|
|
169
|
+
return decoder.decode(binary.subarray(offset - len, offset));
|
|
170
|
+
}
|
|
171
|
+
function getStringList() {
|
|
172
|
+
let count = getLEB();
|
|
173
|
+
let rtn = [];
|
|
174
|
+
while (count--)
|
|
175
|
+
rtn.push(getString());
|
|
176
|
+
return rtn;
|
|
177
|
+
}
|
|
178
|
+
function failIf(condition, message) {
|
|
179
|
+
if (condition)
|
|
180
|
+
throw new Error(message);
|
|
181
|
+
}
|
|
182
|
+
if (binary instanceof WebAssembly.Module) {
|
|
183
|
+
const dylinkSection = WebAssembly.Module.customSections(binary, 'dylink.0');
|
|
184
|
+
failIf(dylinkSection.length === 0, 'need dylink section');
|
|
185
|
+
binary = new Uint8Array(dylinkSection[0]);
|
|
186
|
+
end = binary.length;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
var int32View = new Uint32Array(new Uint8Array(binary.subarray(0, 24)).buffer);
|
|
190
|
+
var magicNumberFound = int32View[0] === 0x6d736100 || int32View[0] === 0x0061736d;
|
|
191
|
+
failIf(!magicNumberFound, 'need to see wasm magic number');
|
|
192
|
+
failIf(binary[8] !== 0, 'need the dylink section to be first');
|
|
193
|
+
offset = 9;
|
|
194
|
+
var section_size = getLEB();
|
|
195
|
+
end = offset + section_size;
|
|
196
|
+
var name = getString();
|
|
197
|
+
failIf(name !== 'dylink.0');
|
|
198
|
+
}
|
|
199
|
+
var customSection = {
|
|
200
|
+
neededDynlibs: [],
|
|
201
|
+
tlsExports: new Set(),
|
|
202
|
+
weakImports: new Set(),
|
|
203
|
+
runtimePaths: [],
|
|
204
|
+
memorySize: 0,
|
|
205
|
+
memoryAlign: 0,
|
|
206
|
+
tableSize: 0,
|
|
207
|
+
tableAlign: 0
|
|
208
|
+
};
|
|
209
|
+
var WASM_DYLINK_MEM_INFO = 0x1;
|
|
210
|
+
var WASM_DYLINK_NEEDED = 0x2;
|
|
211
|
+
var WASM_DYLINK_EXPORT_INFO = 0x3;
|
|
212
|
+
var WASM_DYLINK_IMPORT_INFO = 0x4;
|
|
213
|
+
var WASM_DYLINK_RUNTIME_PATH = 0x5;
|
|
214
|
+
var WASM_SYMBOL_TLS = 0x100;
|
|
215
|
+
var WASM_SYMBOL_BINDING_MASK = 0x3;
|
|
216
|
+
var WASM_SYMBOL_BINDING_WEAK = 0x1;
|
|
217
|
+
while (offset < end) {
|
|
218
|
+
var subsectionType = getU8();
|
|
219
|
+
var subsectionSize = getLEB();
|
|
220
|
+
if (subsectionType === WASM_DYLINK_MEM_INFO) {
|
|
221
|
+
customSection.memorySize = getLEB();
|
|
222
|
+
customSection.memoryAlign = getLEB();
|
|
223
|
+
customSection.tableSize = getLEB();
|
|
224
|
+
customSection.tableAlign = getLEB();
|
|
225
|
+
}
|
|
226
|
+
else if (subsectionType === WASM_DYLINK_NEEDED) {
|
|
227
|
+
customSection.neededDynlibs = getStringList();
|
|
228
|
+
}
|
|
229
|
+
else if (subsectionType === WASM_DYLINK_EXPORT_INFO) {
|
|
230
|
+
let count = getLEB();
|
|
231
|
+
while (count--) {
|
|
232
|
+
let symname = getString();
|
|
233
|
+
let flags = getLEB();
|
|
234
|
+
if (flags & WASM_SYMBOL_TLS) {
|
|
235
|
+
customSection.tlsExports.add(symname);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
else if (subsectionType === WASM_DYLINK_IMPORT_INFO) {
|
|
240
|
+
let count = getLEB();
|
|
241
|
+
while (count--) {
|
|
242
|
+
getString();
|
|
243
|
+
let symname = getString();
|
|
244
|
+
let flags = getLEB();
|
|
245
|
+
if ((flags & WASM_SYMBOL_BINDING_MASK) === WASM_SYMBOL_BINDING_WEAK) {
|
|
246
|
+
customSection.weakImports.add(symname);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (subsectionType === WASM_DYLINK_RUNTIME_PATH) {
|
|
251
|
+
customSection.runtimePaths = getStringList();
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
console.error('unknown dylink.0 subsection:', subsectionType);
|
|
255
|
+
offset += subsectionSize;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
var tableAlign = Math.pow(2, customSection.tableAlign);
|
|
259
|
+
failIf(tableAlign !== 1, `invalid tableAlign ${tableAlign}`);
|
|
260
|
+
failIf(offset !== end);
|
|
261
|
+
return customSection;
|
|
262
|
+
}
|
|
263
|
+
const version = "2.0.0-alpha.5";
|
|
264
|
+
const NODE_API_SUPPORTED_VERSION_MIN = 1;
|
|
265
|
+
const NODE_API_SUPPORTED_VERSION_MAX = 10;
|
|
266
|
+
const NAPI_VERSION_EXPERIMENTAL = 2147483647;
|
|
267
|
+
const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
|
|
268
|
+
const NODE_MODULE_VERSION = 127;
|
|
269
|
+
|
|
270
|
+
//#endregion src/util.ts
|
|
271
|
+
|
|
272
|
+
//#region src/Disaposable.ts
|
|
273
|
+
class Disposable {
|
|
274
|
+
}
|
|
275
|
+
if (typeof Symbol.dispose === 'symbol') {
|
|
276
|
+
Object.defineProperty(Disposable.prototype, Symbol.dispose, {
|
|
277
|
+
value() {
|
|
278
|
+
this.dispose();
|
|
279
|
+
},
|
|
280
|
+
writable: true,
|
|
281
|
+
enumerable: false,
|
|
282
|
+
configurable: true
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
//#endregion src/Disaposable.ts
|
|
287
|
+
|
|
288
|
+
//#region src/RefTracker.ts
|
|
289
|
+
class RefTracker extends Disposable {
|
|
290
|
+
constructor() {
|
|
291
|
+
super(...arguments);
|
|
292
|
+
this._next = null;
|
|
293
|
+
this._prev = null;
|
|
294
|
+
}
|
|
295
|
+
dispose() { }
|
|
296
|
+
finalize() { }
|
|
297
|
+
link(list) {
|
|
298
|
+
this._prev = list;
|
|
299
|
+
this._next = list._next;
|
|
300
|
+
if (this._next !== null) {
|
|
301
|
+
this._next._prev = this;
|
|
302
|
+
}
|
|
303
|
+
list._next = this;
|
|
304
|
+
}
|
|
305
|
+
unlink() {
|
|
306
|
+
if (this._prev !== null) {
|
|
307
|
+
this._prev._next = this._next;
|
|
308
|
+
}
|
|
309
|
+
if (this._next !== null) {
|
|
310
|
+
this._next._prev = this._prev;
|
|
311
|
+
}
|
|
312
|
+
this._prev = null;
|
|
313
|
+
this._next = null;
|
|
314
|
+
}
|
|
315
|
+
static finalizeAll(list) {
|
|
316
|
+
while (list._next !== null) {
|
|
317
|
+
list._next.finalize();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
//#endregion src/RefTracker.ts
|
|
323
|
+
|
|
324
|
+
//#region src/Finalizer.ts
|
|
325
|
+
class Finalizer {
|
|
326
|
+
constructor(envObject, _finalizeCallback = 0, _finalizeData = 0, _finalizeHint = 0) {
|
|
327
|
+
this.envObject = envObject;
|
|
328
|
+
this._finalizeCallback = _finalizeCallback;
|
|
329
|
+
this._finalizeData = _finalizeData;
|
|
330
|
+
this._finalizeHint = _finalizeHint;
|
|
331
|
+
this._makeDynCall_vppp = envObject.bridge.makeDynCall_vppp;
|
|
332
|
+
}
|
|
333
|
+
copy() {
|
|
334
|
+
const newFina = Object.create(Finalizer.prototype);
|
|
335
|
+
newFina.envObject = this.envObject;
|
|
336
|
+
newFina._makeDynCall_vppp = this._makeDynCall_vppp;
|
|
337
|
+
newFina._finalizeCallback = this._finalizeCallback;
|
|
338
|
+
newFina._finalizeData = this._finalizeData;
|
|
339
|
+
newFina._finalizeHint = this._finalizeHint;
|
|
340
|
+
return newFina;
|
|
341
|
+
}
|
|
342
|
+
move(target) {
|
|
343
|
+
target._finalizeCallback = this._finalizeCallback;
|
|
344
|
+
target._finalizeData = this._finalizeData;
|
|
345
|
+
target._finalizeHint = this._finalizeHint;
|
|
346
|
+
target.envObject = this.envObject;
|
|
347
|
+
target._makeDynCall_vppp = this._makeDynCall_vppp;
|
|
348
|
+
this._finalizeCallback = 0;
|
|
349
|
+
this._finalizeData = 0;
|
|
350
|
+
this._finalizeHint = 0;
|
|
351
|
+
this.dispose();
|
|
352
|
+
}
|
|
353
|
+
callback() { return this._finalizeCallback; }
|
|
354
|
+
data() { return this._finalizeData; }
|
|
355
|
+
hint() { return this._finalizeHint; }
|
|
356
|
+
resetEnv() {
|
|
357
|
+
this.envObject = undefined;
|
|
358
|
+
}
|
|
359
|
+
resetFinalizer() {
|
|
360
|
+
this._finalizeCallback = 0;
|
|
361
|
+
this._finalizeData = 0;
|
|
362
|
+
this._finalizeHint = 0;
|
|
363
|
+
}
|
|
364
|
+
callFinalizer() {
|
|
365
|
+
const finalize_callback = this._finalizeCallback;
|
|
366
|
+
const finalize_data = this._finalizeData;
|
|
367
|
+
const finalize_hint = this._finalizeHint;
|
|
368
|
+
this.resetFinalizer();
|
|
369
|
+
if (!finalize_callback)
|
|
370
|
+
return;
|
|
371
|
+
const fini = Number(finalize_callback);
|
|
372
|
+
if (!this.envObject) {
|
|
373
|
+
this._makeDynCall_vppp(fini)(0, finalize_data, finalize_hint);
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
dispose() {
|
|
380
|
+
this.envObject = undefined;
|
|
381
|
+
this._makeDynCall_vppp = undefined;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
//#endregion src/Finalizer.ts
|
|
386
|
+
|
|
387
|
+
//#region src/TrackedFinalizer.ts
|
|
388
|
+
class TrackedFinalizer extends RefTracker {
|
|
389
|
+
static create(envObject, finalize_callback, finalize_data, finalize_hint) {
|
|
390
|
+
const finalizer = new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
|
|
391
|
+
finalizer.link(envObject.finalizing_reflist);
|
|
392
|
+
return finalizer;
|
|
393
|
+
}
|
|
394
|
+
constructor(envObject, finalize_callback, finalize_data, finalize_hint) {
|
|
395
|
+
super();
|
|
396
|
+
this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
|
|
397
|
+
}
|
|
398
|
+
data() {
|
|
399
|
+
return this._finalizer.data();
|
|
400
|
+
}
|
|
401
|
+
dispose() {
|
|
402
|
+
if (!this._finalizer)
|
|
403
|
+
return;
|
|
404
|
+
this.unlink();
|
|
405
|
+
this._finalizer.envObject.dequeueFinalizer(this);
|
|
406
|
+
this._finalizer.dispose();
|
|
407
|
+
this._finalizer = undefined;
|
|
408
|
+
super.dispose();
|
|
409
|
+
}
|
|
410
|
+
finalize() {
|
|
411
|
+
this.unlink();
|
|
412
|
+
let error;
|
|
413
|
+
let caught = false;
|
|
414
|
+
try {
|
|
415
|
+
this._finalizer.callFinalizer();
|
|
416
|
+
}
|
|
417
|
+
catch (err) {
|
|
418
|
+
caught = true;
|
|
419
|
+
error = err;
|
|
420
|
+
}
|
|
421
|
+
this.dispose();
|
|
422
|
+
if (caught) {
|
|
423
|
+
throw error;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
//#endregion src/TrackedFinalizer.ts
|
|
429
|
+
|
|
430
|
+
//#region src/Store.ts
|
|
431
|
+
class CountIdAllocator extends Disposable {
|
|
432
|
+
constructor(initialNext = 1) {
|
|
433
|
+
super();
|
|
434
|
+
this.next = initialNext;
|
|
435
|
+
}
|
|
436
|
+
aquire() {
|
|
437
|
+
return this.next++;
|
|
438
|
+
}
|
|
439
|
+
release(_) { }
|
|
440
|
+
dispose() { }
|
|
441
|
+
}
|
|
442
|
+
class CountIdReuseAllocator extends CountIdAllocator {
|
|
443
|
+
constructor() {
|
|
444
|
+
super(...arguments);
|
|
445
|
+
this._freeList = [];
|
|
446
|
+
this._freeListHead = 0;
|
|
447
|
+
}
|
|
448
|
+
aquire() {
|
|
449
|
+
if (this._freeListHead < this._freeList.length) {
|
|
450
|
+
const id = this._freeList[this._freeListHead++];
|
|
451
|
+
if (this._freeListHead === this._freeList.length) {
|
|
452
|
+
this._freeList.length = 0;
|
|
453
|
+
this._freeListHead = 0;
|
|
454
|
+
}
|
|
455
|
+
return id;
|
|
456
|
+
}
|
|
457
|
+
return super.aquire();
|
|
458
|
+
}
|
|
459
|
+
release(id) {
|
|
460
|
+
this._freeList.push(id);
|
|
461
|
+
}
|
|
462
|
+
dispose() {
|
|
463
|
+
this._freeList.length = 0;
|
|
464
|
+
this._freeListHead = 0;
|
|
465
|
+
super.dispose();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
class BaseArrayStore extends Disposable {
|
|
469
|
+
constructor(initialCapacity = 1) {
|
|
470
|
+
super();
|
|
471
|
+
this._values = [undefined];
|
|
472
|
+
for (let i = 1; i < initialCapacity; i++) {
|
|
473
|
+
this._values.push(undefined);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
assign(id, value) {
|
|
477
|
+
this._values[id] = value;
|
|
478
|
+
return value;
|
|
479
|
+
}
|
|
480
|
+
deref(id) {
|
|
481
|
+
return this._values[id];
|
|
482
|
+
}
|
|
483
|
+
alloc(factory, ...args) {
|
|
484
|
+
return factory(...args);
|
|
485
|
+
}
|
|
486
|
+
dealloc(id) {
|
|
487
|
+
this._values[id] = undefined;
|
|
488
|
+
}
|
|
489
|
+
dispose() {
|
|
490
|
+
this._values.forEach((_, i) => this.dealloc(i));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
class ArrayStore extends BaseArrayStore {
|
|
494
|
+
constructor(initialCapacity = 4) {
|
|
495
|
+
super(initialCapacity);
|
|
496
|
+
this._allocator = new CountIdReuseAllocator(1);
|
|
497
|
+
}
|
|
498
|
+
insert(value) {
|
|
499
|
+
const id = this._allocator.aquire();
|
|
500
|
+
value.id = id;
|
|
501
|
+
this._values[id] = value;
|
|
502
|
+
}
|
|
503
|
+
alloc(factory, ...args) {
|
|
504
|
+
const value = factory(...args);
|
|
505
|
+
this.insert(value);
|
|
506
|
+
return value;
|
|
507
|
+
}
|
|
508
|
+
dealloc(id) {
|
|
509
|
+
this._allocator.release(id);
|
|
510
|
+
super.dealloc(id);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
//#endregion src/Store.ts
|
|
515
|
+
|
|
516
|
+
//#region src/Persistent.ts
|
|
517
|
+
class PersistentStore extends ArrayStore {
|
|
518
|
+
constructor(initialCapacity = 4) {
|
|
519
|
+
super(initialCapacity);
|
|
520
|
+
this._recyleList = [];
|
|
521
|
+
this._recyleListHead = 0;
|
|
522
|
+
}
|
|
523
|
+
markUnused(id) {
|
|
524
|
+
this._recyleList.push(id);
|
|
525
|
+
}
|
|
526
|
+
recycle() {
|
|
527
|
+
while (this._recyleListHead < this._recyleList.length) {
|
|
528
|
+
const id = this._recyleList[this._recyleListHead++];
|
|
529
|
+
if (this._recyleListHead === this._recyleList.length) {
|
|
530
|
+
this._recyleList.length = 0;
|
|
531
|
+
this._recyleListHead = 0;
|
|
532
|
+
}
|
|
533
|
+
const persistent = this.deref(id);
|
|
534
|
+
persistent.dispose();
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
class StrongRef extends Disposable {
|
|
539
|
+
constructor(value) {
|
|
540
|
+
super();
|
|
541
|
+
this._value = value;
|
|
542
|
+
}
|
|
543
|
+
deref() {
|
|
544
|
+
return this._value;
|
|
545
|
+
}
|
|
546
|
+
dispose() {
|
|
547
|
+
this._value = undefined;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
class Persistent extends Disposable {
|
|
551
|
+
constructor(isolate, ...args) {
|
|
552
|
+
super();
|
|
553
|
+
this.id = 0;
|
|
554
|
+
this._isolate = isolate;
|
|
555
|
+
isolate.insertRef(this);
|
|
556
|
+
this._slotId = 0x80000000 + this.id;
|
|
557
|
+
this.setSlot(args.length === 0 ? undefined : new StrongRef(args[0]));
|
|
558
|
+
}
|
|
559
|
+
dispose() {
|
|
560
|
+
this.reset();
|
|
561
|
+
this.deleteSlot();
|
|
562
|
+
this._isolate.globalHandleStore.dealloc(this.id);
|
|
563
|
+
this._isolate = undefined;
|
|
564
|
+
this.id = 0;
|
|
565
|
+
}
|
|
566
|
+
copy() {
|
|
567
|
+
const target = new Persistent(this._isolate);
|
|
568
|
+
target._param = this._param;
|
|
569
|
+
target._callback = this._callback;
|
|
570
|
+
const ref = this.getSlot();
|
|
571
|
+
if (ref instanceof StrongRef) {
|
|
572
|
+
target.setSlot(new StrongRef(ref.deref()));
|
|
573
|
+
}
|
|
574
|
+
else if (ref instanceof WeakRef) {
|
|
575
|
+
const value = ref.deref();
|
|
576
|
+
if (value === undefined) {
|
|
577
|
+
target.setSlot(undefined);
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
target.setSlot(new StrongRef(value));
|
|
581
|
+
target.setWeak(this._param, this._callback);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
target.setSlot(undefined);
|
|
586
|
+
}
|
|
587
|
+
return target;
|
|
588
|
+
}
|
|
589
|
+
move(to) {
|
|
590
|
+
if (this === to)
|
|
591
|
+
return;
|
|
592
|
+
to.reset();
|
|
593
|
+
to._param = this._param;
|
|
594
|
+
to._callback = this._callback;
|
|
595
|
+
to._isolate = this._isolate;
|
|
596
|
+
to.setSlot(this.getSlot());
|
|
597
|
+
this.reset();
|
|
598
|
+
this.setSlot(undefined);
|
|
599
|
+
}
|
|
600
|
+
slot() {
|
|
601
|
+
return this._slotId;
|
|
602
|
+
}
|
|
603
|
+
getSlot() {
|
|
604
|
+
return this._isolate.getRefSlotValue(this.slot());
|
|
605
|
+
}
|
|
606
|
+
setSlot(ref) {
|
|
607
|
+
this._isolate.setRefSlotValue(this.slot(), ref);
|
|
608
|
+
}
|
|
609
|
+
deleteSlot() {
|
|
610
|
+
this._isolate.deleteRefSlotValue(this.slot());
|
|
611
|
+
}
|
|
612
|
+
setWeak(param, callback) {
|
|
613
|
+
const ref = this.getSlot();
|
|
614
|
+
if (!supportFinalizer || ref === undefined || ref instanceof WeakRef)
|
|
615
|
+
return;
|
|
616
|
+
const value = ref.deref();
|
|
617
|
+
try {
|
|
618
|
+
Persistent._registry.register(value, this, this);
|
|
619
|
+
const weakRef = new WeakRef(value);
|
|
620
|
+
ref.dispose();
|
|
621
|
+
this.setSlot(weakRef);
|
|
622
|
+
this._param = param;
|
|
623
|
+
this._callback = callback;
|
|
624
|
+
}
|
|
625
|
+
catch (err) {
|
|
626
|
+
if (typeof value === 'symbol') ;
|
|
627
|
+
else {
|
|
628
|
+
throw err;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
clearWeak() {
|
|
633
|
+
const ref = this.getSlot();
|
|
634
|
+
if (!supportFinalizer || ref === undefined)
|
|
635
|
+
return;
|
|
636
|
+
if (ref instanceof WeakRef) {
|
|
637
|
+
try {
|
|
638
|
+
Persistent._registry.unregister(this);
|
|
639
|
+
}
|
|
640
|
+
catch (_) { }
|
|
641
|
+
this._param = undefined;
|
|
642
|
+
this._callback = undefined;
|
|
643
|
+
const value = ref.deref();
|
|
644
|
+
if (value === undefined) {
|
|
645
|
+
this.setSlot(value);
|
|
646
|
+
}
|
|
647
|
+
else {
|
|
648
|
+
this.setSlot(new StrongRef(value));
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
reset() {
|
|
653
|
+
if (supportFinalizer) {
|
|
654
|
+
try {
|
|
655
|
+
Persistent._registry.unregister(this);
|
|
656
|
+
}
|
|
657
|
+
catch (_) { }
|
|
658
|
+
}
|
|
659
|
+
this._param = undefined;
|
|
660
|
+
this._callback = undefined;
|
|
661
|
+
const ref = this.getSlot();
|
|
662
|
+
if (ref instanceof StrongRef) {
|
|
663
|
+
ref.dispose();
|
|
664
|
+
}
|
|
665
|
+
this.setSlot(undefined);
|
|
666
|
+
}
|
|
667
|
+
resetTo(value) {
|
|
668
|
+
this.reset();
|
|
669
|
+
this.setSlot(new StrongRef(value));
|
|
670
|
+
}
|
|
671
|
+
isEmpty() {
|
|
672
|
+
return this.getSlot() === undefined;
|
|
673
|
+
}
|
|
674
|
+
deref() {
|
|
675
|
+
const ref = this.getSlot();
|
|
676
|
+
if (ref === undefined)
|
|
677
|
+
return undefined;
|
|
678
|
+
return ref.deref();
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
Persistent._registry = supportFinalizer
|
|
682
|
+
? new FinalizationRegistry((value) => {
|
|
683
|
+
value.setSlot(undefined);
|
|
684
|
+
const callback = value._callback;
|
|
685
|
+
const param = value._param;
|
|
686
|
+
value._callback = undefined;
|
|
687
|
+
value._param = undefined;
|
|
688
|
+
if (typeof callback === 'function') {
|
|
689
|
+
callback(param);
|
|
690
|
+
}
|
|
691
|
+
})
|
|
692
|
+
: undefined;
|
|
693
|
+
|
|
694
|
+
//#endregion src/Persistent.ts
|
|
695
|
+
|
|
696
|
+
//#region src/env.ts
|
|
697
|
+
function handleThrow(envObject, value) {
|
|
698
|
+
if (envObject.terminatedOrTerminating()) {
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
throw value;
|
|
702
|
+
}
|
|
703
|
+
class Env extends Disposable {
|
|
704
|
+
constructor(ctx, store, bridge) {
|
|
705
|
+
super();
|
|
706
|
+
this.openHandleScopes = 0;
|
|
707
|
+
this.instanceData = null;
|
|
708
|
+
this.refs = 1;
|
|
709
|
+
this.reflist = new RefTracker();
|
|
710
|
+
this.finalizing_reflist = new RefTracker();
|
|
711
|
+
this.pendingFinalizers = new Set();
|
|
712
|
+
this.inGcFinalizer = false;
|
|
713
|
+
this._bindingMap = new WeakMap();
|
|
714
|
+
this.ctx = ctx;
|
|
715
|
+
this.store = store;
|
|
716
|
+
this.id = 0;
|
|
717
|
+
this.store.insert(this);
|
|
718
|
+
this.lastException = new Persistent(ctx.isolate);
|
|
719
|
+
this.bridge = bridge;
|
|
720
|
+
}
|
|
721
|
+
canCallIntoJs() {
|
|
722
|
+
return true;
|
|
723
|
+
}
|
|
724
|
+
terminatedOrTerminating() {
|
|
725
|
+
return !this.canCallIntoJs();
|
|
726
|
+
}
|
|
727
|
+
ref() {
|
|
728
|
+
this.refs++;
|
|
729
|
+
}
|
|
730
|
+
unref() {
|
|
731
|
+
this.refs--;
|
|
732
|
+
if (this.refs === 0) {
|
|
733
|
+
this.dispose();
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
clearLastError() {
|
|
737
|
+
this.bridge.setLastError(this.bridge.address, 0, 0, 0);
|
|
738
|
+
return 0;
|
|
739
|
+
}
|
|
740
|
+
setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
|
|
741
|
+
this.bridge.setLastError(this.bridge.address, error_code, engine_error_code, engine_reserved);
|
|
742
|
+
return error_code;
|
|
743
|
+
}
|
|
744
|
+
callIntoModule(fn, handleException = handleThrow) {
|
|
745
|
+
const openHandleScopesBefore = this.openHandleScopes;
|
|
746
|
+
this.clearLastError();
|
|
747
|
+
const r = fn(this);
|
|
748
|
+
if (openHandleScopesBefore !== this.openHandleScopes) {
|
|
749
|
+
this.bridge.abort('open_handle_scopes != open_handle_scopes_before');
|
|
750
|
+
}
|
|
751
|
+
if (!this.lastException.isEmpty()) {
|
|
752
|
+
const err = this.lastException.deref();
|
|
753
|
+
this.lastException.reset();
|
|
754
|
+
handleException(this, err);
|
|
755
|
+
}
|
|
756
|
+
return r;
|
|
757
|
+
}
|
|
758
|
+
invokeFinalizerFromGC(finalizer) {
|
|
759
|
+
if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
|
|
760
|
+
this.enqueueFinalizer(finalizer);
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
const saved = this.inGcFinalizer;
|
|
764
|
+
this.inGcFinalizer = true;
|
|
765
|
+
try {
|
|
766
|
+
finalizer.finalize();
|
|
767
|
+
}
|
|
768
|
+
finally {
|
|
769
|
+
this.inGcFinalizer = saved;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
checkGCAccess() {
|
|
774
|
+
if (this.moduleApiVersion === NAPI_VERSION_EXPERIMENTAL && this.inGcFinalizer) {
|
|
775
|
+
this.bridge.abort('Finalizer is calling a function that may affect GC state.\n' +
|
|
776
|
+
'The finalizers are run directly from GC and must not affect GC ' +
|
|
777
|
+
'state.\n' +
|
|
778
|
+
'Use `node_api_post_finalizer` from inside of the finalizer to work ' +
|
|
779
|
+
'around this issue.\n' +
|
|
780
|
+
'It schedules the call as a new task in the event loop.');
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
enqueueFinalizer(finalizer) {
|
|
784
|
+
this.pendingFinalizers.add(finalizer);
|
|
785
|
+
}
|
|
786
|
+
dequeueFinalizer(finalizer) {
|
|
787
|
+
this.pendingFinalizers.delete(finalizer);
|
|
788
|
+
}
|
|
789
|
+
deleteMe() {
|
|
790
|
+
RefTracker.finalizeAll(this.finalizing_reflist);
|
|
791
|
+
RefTracker.finalizeAll(this.reflist);
|
|
792
|
+
this.lastException.reset();
|
|
793
|
+
this.store.dealloc(this.id);
|
|
794
|
+
this.bridge.deleteEnv(this.bridge.address);
|
|
795
|
+
}
|
|
796
|
+
dispose() {
|
|
797
|
+
if (this.id === 0)
|
|
798
|
+
return;
|
|
799
|
+
this.deleteMe();
|
|
800
|
+
this.finalizing_reflist.dispose();
|
|
801
|
+
this.reflist.dispose();
|
|
802
|
+
this.id = 0;
|
|
803
|
+
}
|
|
804
|
+
initObjectBinding(value) {
|
|
805
|
+
const binding = {
|
|
806
|
+
wrapped: 0,
|
|
807
|
+
tag: null
|
|
808
|
+
};
|
|
809
|
+
this._bindingMap.set(value, binding);
|
|
810
|
+
return binding;
|
|
811
|
+
}
|
|
812
|
+
getObjectBinding(value) {
|
|
813
|
+
if (this._bindingMap.has(value)) {
|
|
814
|
+
return this._bindingMap.get(value);
|
|
815
|
+
}
|
|
816
|
+
return this.initObjectBinding(value);
|
|
817
|
+
}
|
|
818
|
+
setInstanceData(data, finalize_cb, finalize_hint) {
|
|
819
|
+
if (this.instanceData) {
|
|
820
|
+
this.instanceData.dispose();
|
|
821
|
+
}
|
|
822
|
+
this.instanceData = TrackedFinalizer.create(this, finalize_cb, data, finalize_hint);
|
|
823
|
+
}
|
|
824
|
+
getInstanceData() {
|
|
825
|
+
return this.instanceData ? this.instanceData.data() : 0;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
class NodeEnv extends Env {
|
|
829
|
+
constructor(ctx, store, bridge) {
|
|
830
|
+
super(ctx, store, bridge);
|
|
831
|
+
this.destructing = false;
|
|
832
|
+
this.finalizationScheduled = false;
|
|
833
|
+
this.bridge = bridge;
|
|
834
|
+
}
|
|
835
|
+
deleteMe() {
|
|
836
|
+
this.destructing = true;
|
|
837
|
+
this.drainFinalizerQueue();
|
|
838
|
+
super.deleteMe();
|
|
839
|
+
}
|
|
840
|
+
canCallIntoJs() {
|
|
841
|
+
return super.canCallIntoJs() && this.ctx.canCallIntoJs();
|
|
842
|
+
}
|
|
843
|
+
triggerFatalException(err) {
|
|
844
|
+
if (this.nodeBinding) {
|
|
845
|
+
this.nodeBinding.napi.fatalException(err);
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
if (typeof process === 'object' && process !== null && typeof process._fatalException === 'function') {
|
|
849
|
+
const handled = process._fatalException(err);
|
|
850
|
+
if (!handled) {
|
|
851
|
+
console.error(err);
|
|
852
|
+
process.exit(1);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
856
|
+
throw err;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
callbackIntoModule(enforceUncaughtExceptionPolicy, fn) {
|
|
861
|
+
return this.callIntoModule(fn, (envObject, err) => {
|
|
862
|
+
if (envObject.terminatedOrTerminating()) {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
const hasProcess = typeof process === 'object' && process !== null;
|
|
866
|
+
const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
|
|
867
|
+
if (envObject.moduleApiVersion < 10 && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
|
|
868
|
+
const warn = hasProcess && typeof process.emitWarning === 'function'
|
|
869
|
+
? process.emitWarning
|
|
870
|
+
: function (warning, type, code) {
|
|
871
|
+
if (warning instanceof Error) {
|
|
872
|
+
console.warn(warning.toString());
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
const prefix = code ? `[${code}] ` : '';
|
|
876
|
+
console.warn(`${prefix}${type || 'Warning'}: ${warning}`);
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
warn('Uncaught Node-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.', 'DeprecationWarning', 'DEP0168');
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
envObject.triggerFatalException(err);
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
callFinalizer(cb, data, hint) {
|
|
886
|
+
this.callFinalizerInternal(1, cb, data, hint);
|
|
887
|
+
}
|
|
888
|
+
callFinalizerInternal(forceUncaught, cb, data, hint) {
|
|
889
|
+
const f = this.bridge.makeDynCall_vppp(cb);
|
|
890
|
+
const env = this.bridge.address;
|
|
891
|
+
const scope = this.ctx.openScope(this);
|
|
892
|
+
try {
|
|
893
|
+
this.callbackIntoModule(Boolean(forceUncaught), () => { f(env, data, hint); });
|
|
894
|
+
}
|
|
895
|
+
finally {
|
|
896
|
+
this.ctx.closeScope(this, scope);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
enqueueFinalizer(finalizer) {
|
|
900
|
+
super.enqueueFinalizer(finalizer);
|
|
901
|
+
if (!this.finalizationScheduled && !this.destructing) {
|
|
902
|
+
this.finalizationScheduled = true;
|
|
903
|
+
this.ref();
|
|
904
|
+
this.ctx.features.setImmediate(() => {
|
|
905
|
+
this.finalizationScheduled = false;
|
|
906
|
+
this.unref();
|
|
907
|
+
this.drainFinalizerQueue();
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
drainFinalizerQueue() {
|
|
912
|
+
for (const refTracker of this.pendingFinalizers) {
|
|
913
|
+
this.pendingFinalizers.delete(refTracker);
|
|
914
|
+
refTracker.finalize();
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
//#endregion src/env.ts
|
|
920
|
+
|
|
921
|
+
//#region src/errors.ts
|
|
922
|
+
class EmnapiError extends Error {
|
|
923
|
+
constructor(message) {
|
|
924
|
+
super(message);
|
|
925
|
+
const ErrorConstructor = new.target;
|
|
926
|
+
const proto = ErrorConstructor.prototype;
|
|
927
|
+
if (!(this instanceof EmnapiError)) {
|
|
928
|
+
const setPrototypeOf = Object.setPrototypeOf;
|
|
929
|
+
if (typeof setPrototypeOf === 'function') {
|
|
930
|
+
setPrototypeOf.call(Object, this, proto);
|
|
931
|
+
}
|
|
932
|
+
else {
|
|
933
|
+
this.__proto__ = proto;
|
|
934
|
+
}
|
|
935
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
936
|
+
Error.captureStackTrace(this, ErrorConstructor);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
Object.defineProperty(EmnapiError.prototype, 'name', {
|
|
942
|
+
configurable: true,
|
|
943
|
+
writable: true,
|
|
944
|
+
value: 'EmnapiError'
|
|
945
|
+
});
|
|
946
|
+
class NotSupportWeakRefError extends EmnapiError {
|
|
947
|
+
constructor(api, message) {
|
|
948
|
+
super(`${api}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${message ? ` ${message}` : ''}`);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
Object.defineProperty(NotSupportWeakRefError.prototype, 'name', {
|
|
952
|
+
configurable: true,
|
|
953
|
+
writable: true,
|
|
954
|
+
value: 'NotSupportWeakRefError'
|
|
955
|
+
});
|
|
956
|
+
class NotSupportBufferError extends EmnapiError {
|
|
957
|
+
constructor(api, message) {
|
|
958
|
+
super(`${api}: The current runtime does not support "Buffer". Consider using buffer polyfill to make sure \`globalThis.Buffer\` is defined.${message ? ` ${message}` : ''}`);
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
Object.defineProperty(NotSupportBufferError.prototype, 'name', {
|
|
962
|
+
configurable: true,
|
|
963
|
+
writable: true,
|
|
964
|
+
value: 'NotSupportBufferError'
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
//#endregion src/errors.ts
|
|
968
|
+
|
|
969
|
+
//#region src/Reference.ts
|
|
970
|
+
var ReferenceOwnership;
|
|
971
|
+
(function (ReferenceOwnership) {
|
|
972
|
+
ReferenceOwnership[ReferenceOwnership["kRuntime"] = 0] = "kRuntime";
|
|
973
|
+
ReferenceOwnership[ReferenceOwnership["kUserland"] = 1] = "kUserland";
|
|
974
|
+
})(ReferenceOwnership || (ReferenceOwnership = {}));
|
|
975
|
+
function canBeHeldWeakly(value) {
|
|
976
|
+
if (!value)
|
|
977
|
+
return false;
|
|
978
|
+
const type = typeof value;
|
|
979
|
+
return type === 'object' || type === 'function' || type === 'symbol';
|
|
980
|
+
}
|
|
981
|
+
class Reference extends RefTracker {
|
|
982
|
+
static weakCallback(ref) {
|
|
983
|
+
const persistent = ref.getPersistent();
|
|
984
|
+
persistent.reset();
|
|
985
|
+
ref.invokeFinalizerFromGC();
|
|
986
|
+
}
|
|
987
|
+
static create(ctx, envObject, handle_id, initialRefcount, ownership, _unused1, _unused2, _unused3) {
|
|
988
|
+
const ref = new Reference(ctx, envObject, handle_id, initialRefcount, ownership);
|
|
989
|
+
if (envObject) {
|
|
990
|
+
ref.link(envObject.reflist);
|
|
991
|
+
}
|
|
992
|
+
return ref;
|
|
993
|
+
}
|
|
994
|
+
constructor(ctx, envObject, handle_id, initialRefcount, ownership) {
|
|
995
|
+
super();
|
|
996
|
+
this.envObject = envObject;
|
|
997
|
+
this._refcount = initialRefcount;
|
|
998
|
+
this._ownership = ownership;
|
|
999
|
+
const value = envObject ? envObject.ctx.jsValueFromNapiValue(handle_id) : handle_id;
|
|
1000
|
+
this.canBeWeak = canBeHeldWeakly(value);
|
|
1001
|
+
this.ctx = ctx;
|
|
1002
|
+
this.id = new Persistent(ctx.isolate, value).id;
|
|
1003
|
+
ctx.refStore.set(this.id, this);
|
|
1004
|
+
if (initialRefcount === 0) {
|
|
1005
|
+
this._setWeak();
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
getPersistent() {
|
|
1009
|
+
return this.ctx.isolate.getRef(this.id);
|
|
1010
|
+
}
|
|
1011
|
+
ref() {
|
|
1012
|
+
const persistent = this.getPersistent();
|
|
1013
|
+
if (persistent.isEmpty()) {
|
|
1014
|
+
return 0;
|
|
1015
|
+
}
|
|
1016
|
+
if (++this._refcount === 1 && this.canBeWeak) {
|
|
1017
|
+
persistent.clearWeak();
|
|
1018
|
+
}
|
|
1019
|
+
return this._refcount;
|
|
1020
|
+
}
|
|
1021
|
+
unref() {
|
|
1022
|
+
const persistent = this.getPersistent();
|
|
1023
|
+
if (persistent.isEmpty() || this._refcount === 0) {
|
|
1024
|
+
return 0;
|
|
1025
|
+
}
|
|
1026
|
+
if (--this._refcount === 0) {
|
|
1027
|
+
this._setWeak();
|
|
1028
|
+
}
|
|
1029
|
+
return this._refcount;
|
|
1030
|
+
}
|
|
1031
|
+
deref() {
|
|
1032
|
+
const persistent = this.getPersistent();
|
|
1033
|
+
return persistent.deref();
|
|
1034
|
+
}
|
|
1035
|
+
get() {
|
|
1036
|
+
const persistent = this.getPersistent();
|
|
1037
|
+
if (persistent.isEmpty()) {
|
|
1038
|
+
return 0;
|
|
1039
|
+
}
|
|
1040
|
+
return persistent.slot();
|
|
1041
|
+
}
|
|
1042
|
+
resetFinalizer() { }
|
|
1043
|
+
data() { return 0; }
|
|
1044
|
+
refcount() { return this._refcount; }
|
|
1045
|
+
ownership() { return this._ownership; }
|
|
1046
|
+
callUserFinalizer() { }
|
|
1047
|
+
invokeFinalizerFromGC() {
|
|
1048
|
+
this.finalize();
|
|
1049
|
+
}
|
|
1050
|
+
_setWeak() {
|
|
1051
|
+
this.setWeakWithData(this, Reference.weakCallback);
|
|
1052
|
+
}
|
|
1053
|
+
setWeakWithData(data, callback) {
|
|
1054
|
+
const persistent = this.getPersistent();
|
|
1055
|
+
if (this.canBeWeak) {
|
|
1056
|
+
persistent.setWeak(data, callback);
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
persistent.reset();
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
clearWeak() {
|
|
1063
|
+
const persistent = this.getPersistent();
|
|
1064
|
+
persistent.clearWeak();
|
|
1065
|
+
}
|
|
1066
|
+
finalize() {
|
|
1067
|
+
const persistent = this.getPersistent();
|
|
1068
|
+
persistent.reset();
|
|
1069
|
+
const deleteMe = this._ownership === ReferenceOwnership.kRuntime;
|
|
1070
|
+
this.unlink();
|
|
1071
|
+
this.callUserFinalizer();
|
|
1072
|
+
if (deleteMe) {
|
|
1073
|
+
this.dispose();
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
dispose() {
|
|
1077
|
+
if (this.id === 0)
|
|
1078
|
+
return;
|
|
1079
|
+
this.unlink();
|
|
1080
|
+
this.ctx.isolate.removeRef(this.id);
|
|
1081
|
+
this.ctx.refStore.delete(this.id);
|
|
1082
|
+
super.dispose();
|
|
1083
|
+
this.ctx = undefined;
|
|
1084
|
+
this.envObject = undefined;
|
|
1085
|
+
this.id = 0;
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
class ReferenceWithData extends Reference {
|
|
1089
|
+
static create(ctx, envObject, value, initialRefcount, ownership, data) {
|
|
1090
|
+
const reference = new ReferenceWithData(ctx, envObject, value, initialRefcount, ownership, data);
|
|
1091
|
+
if (envObject) {
|
|
1092
|
+
reference.link(envObject.reflist);
|
|
1093
|
+
}
|
|
1094
|
+
return reference;
|
|
1095
|
+
}
|
|
1096
|
+
constructor(ctx, envObject, value, initialRefcount, ownership, data) {
|
|
1097
|
+
super(ctx, envObject, value, initialRefcount, ownership);
|
|
1098
|
+
this._data = data;
|
|
1099
|
+
}
|
|
1100
|
+
data() {
|
|
1101
|
+
return this._data;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
class ReferenceWithFinalizer extends Reference {
|
|
1105
|
+
static create(ctx, envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
|
|
1106
|
+
if (!envObject) {
|
|
1107
|
+
throw new TypeError('envObject is required for ReferenceWithFinalizer');
|
|
1108
|
+
}
|
|
1109
|
+
const reference = new ReferenceWithFinalizer(ctx, envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
1110
|
+
reference.link(envObject.finalizing_reflist);
|
|
1111
|
+
return reference;
|
|
1112
|
+
}
|
|
1113
|
+
constructor(ctx, envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
|
|
1114
|
+
super(ctx, envObject, value, initialRefcount, ownership);
|
|
1115
|
+
this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
|
|
1116
|
+
}
|
|
1117
|
+
resetFinalizer() {
|
|
1118
|
+
this._finalizer.resetFinalizer();
|
|
1119
|
+
}
|
|
1120
|
+
data() {
|
|
1121
|
+
return this._finalizer.data();
|
|
1122
|
+
}
|
|
1123
|
+
callUserFinalizer() {
|
|
1124
|
+
this._finalizer.callFinalizer();
|
|
1125
|
+
}
|
|
1126
|
+
invokeFinalizerFromGC() {
|
|
1127
|
+
this._finalizer.envObject.invokeFinalizerFromGC(this);
|
|
1128
|
+
}
|
|
1129
|
+
dispose() {
|
|
1130
|
+
if (!this._finalizer)
|
|
1131
|
+
return;
|
|
1132
|
+
this._finalizer.envObject.dequeueFinalizer(this);
|
|
1133
|
+
this._finalizer.dispose();
|
|
1134
|
+
super.dispose();
|
|
1135
|
+
this._finalizer = undefined;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
//#endregion src/Reference.ts
|
|
1140
|
+
|
|
1141
|
+
//#region src/Handle.ts
|
|
1142
|
+
class HandleStore extends BaseArrayStore {
|
|
1143
|
+
constructor(features) {
|
|
1144
|
+
super(HandleStore.MIN_ID);
|
|
1145
|
+
this._features = features;
|
|
1146
|
+
this._allocator = new CountIdAllocator(HandleStore.MIN_ID);
|
|
1147
|
+
this.refValues = new Map();
|
|
1148
|
+
this._values[2] = undefined;
|
|
1149
|
+
this._values[3] = null;
|
|
1150
|
+
this._values[4] = false;
|
|
1151
|
+
this._values[5] = true;
|
|
1152
|
+
this._values[6] = features.getGlobalThis();
|
|
1153
|
+
this._values[7] = '';
|
|
1154
|
+
const _erase = (start, end) => {
|
|
1155
|
+
for (let i = start; i < end; ++i) {
|
|
1156
|
+
this.dealloc(i);
|
|
1157
|
+
}
|
|
1158
|
+
};
|
|
1159
|
+
this._erase = this._features.finalizer
|
|
1160
|
+
? this._features.weakSymbol
|
|
1161
|
+
? (start, end, weak) => {
|
|
1162
|
+
if (!weak) {
|
|
1163
|
+
_erase(start, end);
|
|
1164
|
+
}
|
|
1165
|
+
else {
|
|
1166
|
+
for (let i = start; i < end; ++i) {
|
|
1167
|
+
const value = this._values[i];
|
|
1168
|
+
const type = typeof value;
|
|
1169
|
+
if ((type === 'object' && value !== null) || (type === 'function') || (type === 'symbol' && Symbol.keyFor(value) === undefined)) {
|
|
1170
|
+
this._values[i] = new WeakRef(value);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
: (start, end, weak) => {
|
|
1176
|
+
if (!weak) {
|
|
1177
|
+
_erase(start, end);
|
|
1178
|
+
}
|
|
1179
|
+
else {
|
|
1180
|
+
for (let i = start; i < end; ++i) {
|
|
1181
|
+
const value = this._values[i];
|
|
1182
|
+
const type = typeof value;
|
|
1183
|
+
if ((type === 'object' && value !== null) || (type === 'function')) {
|
|
1184
|
+
this._values[i] = new WeakRef(value);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
: _erase;
|
|
1190
|
+
this._deref = this._features.finalizer
|
|
1191
|
+
? (id) => {
|
|
1192
|
+
id = Number(id);
|
|
1193
|
+
if (id < 0 || id > 0x7fffffff) {
|
|
1194
|
+
const ref = this.refValues.get(id >>> 0);
|
|
1195
|
+
if (ref === undefined)
|
|
1196
|
+
return undefined;
|
|
1197
|
+
return ref.deref();
|
|
1198
|
+
}
|
|
1199
|
+
const value = this._values[id];
|
|
1200
|
+
if (value instanceof WeakRef && this.isOutOfScope(id)) {
|
|
1201
|
+
return value.deref();
|
|
1202
|
+
}
|
|
1203
|
+
return value;
|
|
1204
|
+
}
|
|
1205
|
+
: (id) => {
|
|
1206
|
+
id = Number(id);
|
|
1207
|
+
if (id < 0 || id > 0x7fffffff) {
|
|
1208
|
+
const ref = this.refValues.get(id >>> 0);
|
|
1209
|
+
if (ref === undefined)
|
|
1210
|
+
return undefined;
|
|
1211
|
+
return ref.deref();
|
|
1212
|
+
}
|
|
1213
|
+
const value = this._values[id];
|
|
1214
|
+
return value;
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
isOutOfScope(id) {
|
|
1218
|
+
return id >= this._allocator.next;
|
|
1219
|
+
}
|
|
1220
|
+
deepDeref(id) {
|
|
1221
|
+
return this._deref(id);
|
|
1222
|
+
}
|
|
1223
|
+
push(value) {
|
|
1224
|
+
const next = this._allocator.aquire();
|
|
1225
|
+
this._values[next] = value;
|
|
1226
|
+
return next;
|
|
1227
|
+
}
|
|
1228
|
+
erase(start, end, weak) {
|
|
1229
|
+
this._allocator.next = start;
|
|
1230
|
+
this._erase(start, end, weak);
|
|
1231
|
+
}
|
|
1232
|
+
swap(a, b) {
|
|
1233
|
+
const values = this._values;
|
|
1234
|
+
const h = values[a];
|
|
1235
|
+
values[a] = values[b];
|
|
1236
|
+
values[b] = h;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
HandleStore.MIN_ID = 8;
|
|
1240
|
+
|
|
1241
|
+
//#endregion src/Handle.ts
|
|
1242
|
+
|
|
1243
|
+
//#region src/External.ts
|
|
1244
|
+
const externalValue = new WeakMap();
|
|
1245
|
+
function isExternal(object) {
|
|
1246
|
+
return externalValue.has(object);
|
|
1247
|
+
}
|
|
1248
|
+
const External = (() => {
|
|
1249
|
+
function External(value) {
|
|
1250
|
+
Object.setPrototypeOf(this, null);
|
|
1251
|
+
externalValue.set(this, value);
|
|
1252
|
+
}
|
|
1253
|
+
External.prototype = null;
|
|
1254
|
+
return External;
|
|
1255
|
+
})();
|
|
1256
|
+
function getExternalValue(external) {
|
|
1257
|
+
if (!isExternal(external)) {
|
|
1258
|
+
throw new TypeError('not external');
|
|
1259
|
+
}
|
|
1260
|
+
return externalValue.get(external);
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
//#endregion src/External.ts
|
|
1264
|
+
|
|
1265
|
+
//#region src/HandleScope.ts
|
|
1266
|
+
class HandleScope extends Disposable {
|
|
1267
|
+
static create(parentScope, handleStore, start = parentScope?.end ?? 1, end = start) {
|
|
1268
|
+
return new HandleScope(parentScope, handleStore, start, end);
|
|
1269
|
+
}
|
|
1270
|
+
constructor(parentScope, handleStore, start = parentScope?.end ?? 1, end = start) {
|
|
1271
|
+
super();
|
|
1272
|
+
this._escapeCalled = false;
|
|
1273
|
+
this.handleStore = handleStore;
|
|
1274
|
+
this.id = 0;
|
|
1275
|
+
this.parent = parentScope;
|
|
1276
|
+
this.child = null;
|
|
1277
|
+
if (parentScope !== null)
|
|
1278
|
+
parentScope.child = this;
|
|
1279
|
+
this.start = start;
|
|
1280
|
+
this.end = end;
|
|
1281
|
+
this.callbackInfo = {
|
|
1282
|
+
thiz: undefined,
|
|
1283
|
+
holder: undefined,
|
|
1284
|
+
data: 0,
|
|
1285
|
+
args: undefined,
|
|
1286
|
+
fn: undefined
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
reuse(parentScope) {
|
|
1290
|
+
this.start = this.end = parentScope.end;
|
|
1291
|
+
this._escapeCalled = false;
|
|
1292
|
+
}
|
|
1293
|
+
add(value) {
|
|
1294
|
+
const h = this.handleStore.push(value);
|
|
1295
|
+
this.end = h + 1;
|
|
1296
|
+
return h;
|
|
1297
|
+
}
|
|
1298
|
+
addExternal(data) {
|
|
1299
|
+
return this.add(new External(data));
|
|
1300
|
+
}
|
|
1301
|
+
dispose() {
|
|
1302
|
+
const weak = this.callbackInfo.fn === undefined;
|
|
1303
|
+
if (!weak) {
|
|
1304
|
+
this.callbackInfo.data = 0;
|
|
1305
|
+
this.callbackInfo.args = undefined;
|
|
1306
|
+
this.callbackInfo.thiz = undefined;
|
|
1307
|
+
this.callbackInfo.holder = undefined;
|
|
1308
|
+
this.callbackInfo.fn = undefined;
|
|
1309
|
+
}
|
|
1310
|
+
if (this.start === this.end)
|
|
1311
|
+
return;
|
|
1312
|
+
this.handleStore.erase(this.start, this.end, weak);
|
|
1313
|
+
}
|
|
1314
|
+
escape(handle) {
|
|
1315
|
+
if (handle < this.start || handle >= this.end)
|
|
1316
|
+
return handle;
|
|
1317
|
+
if (this._escapeCalled)
|
|
1318
|
+
return 0;
|
|
1319
|
+
this._escapeCalled = true;
|
|
1320
|
+
const id = this.start;
|
|
1321
|
+
this.handleStore.swap(handle, id);
|
|
1322
|
+
this.start++;
|
|
1323
|
+
this.parent.end++;
|
|
1324
|
+
return id;
|
|
1325
|
+
}
|
|
1326
|
+
escapeCalled() {
|
|
1327
|
+
return this._escapeCalled;
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
//#endregion src/HandleScope.ts
|
|
1332
|
+
|
|
1333
|
+
//#region src/ScopeStore.ts
|
|
1334
|
+
class ScopeStore extends BaseArrayStore {
|
|
1335
|
+
constructor() {
|
|
1336
|
+
super();
|
|
1337
|
+
this._rootScope = new HandleScope(null, null, 1, HandleStore.MIN_ID);
|
|
1338
|
+
this.currentScope = this._rootScope;
|
|
1339
|
+
}
|
|
1340
|
+
openScope(handleStore) {
|
|
1341
|
+
const currentScope = this.currentScope;
|
|
1342
|
+
let scope = currentScope.child;
|
|
1343
|
+
if (scope) {
|
|
1344
|
+
scope.reuse(currentScope);
|
|
1345
|
+
}
|
|
1346
|
+
else {
|
|
1347
|
+
scope = new HandleScope(currentScope, handleStore);
|
|
1348
|
+
const id = currentScope.id + 1;
|
|
1349
|
+
scope.id = id;
|
|
1350
|
+
this._values[id] = scope;
|
|
1351
|
+
}
|
|
1352
|
+
this.currentScope = scope;
|
|
1353
|
+
return scope;
|
|
1354
|
+
}
|
|
1355
|
+
closeScope() {
|
|
1356
|
+
const scope = this.currentScope;
|
|
1357
|
+
this.currentScope = scope.parent;
|
|
1358
|
+
scope.dispose();
|
|
1359
|
+
}
|
|
1360
|
+
isEmpty() {
|
|
1361
|
+
return this.currentScope === this._rootScope;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
//#endregion src/ScopeStore.ts
|
|
1366
|
+
|
|
1367
|
+
//#region src/TryCatch.ts
|
|
1368
|
+
class TryCatch {
|
|
1369
|
+
constructor(id) {
|
|
1370
|
+
this.id = 0;
|
|
1371
|
+
this._exception = undefined;
|
|
1372
|
+
this._caught = false;
|
|
1373
|
+
this._next = null;
|
|
1374
|
+
this.id = id;
|
|
1375
|
+
this._next = TryCatch.top;
|
|
1376
|
+
TryCatch.top = this;
|
|
1377
|
+
TryCatch._map.set(id, this);
|
|
1378
|
+
}
|
|
1379
|
+
static deref(id) {
|
|
1380
|
+
return this._map.get(id);
|
|
1381
|
+
}
|
|
1382
|
+
static pop() {
|
|
1383
|
+
const top = TryCatch.top;
|
|
1384
|
+
if (top) {
|
|
1385
|
+
TryCatch._map.delete(top.id);
|
|
1386
|
+
TryCatch.top = top._next;
|
|
1387
|
+
top._next = null;
|
|
1388
|
+
top.id = 0;
|
|
1389
|
+
top._exception = undefined;
|
|
1390
|
+
top._caught = false;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
isEmpty() {
|
|
1394
|
+
return !this._caught;
|
|
1395
|
+
}
|
|
1396
|
+
hasCaught() {
|
|
1397
|
+
return this._caught;
|
|
1398
|
+
}
|
|
1399
|
+
exception() {
|
|
1400
|
+
return this._exception;
|
|
1401
|
+
}
|
|
1402
|
+
rethrow(ctx) {
|
|
1403
|
+
if (this._caught) {
|
|
1404
|
+
const e = this.extractException();
|
|
1405
|
+
ctx.setLastException(e);
|
|
1406
|
+
return e;
|
|
1407
|
+
}
|
|
1408
|
+
return undefined;
|
|
1409
|
+
}
|
|
1410
|
+
setError(err) {
|
|
1411
|
+
this._caught = true;
|
|
1412
|
+
this._exception = err;
|
|
1413
|
+
}
|
|
1414
|
+
reset() {
|
|
1415
|
+
this._caught = false;
|
|
1416
|
+
this._exception = undefined;
|
|
1417
|
+
}
|
|
1418
|
+
extractException() {
|
|
1419
|
+
const e = this._exception;
|
|
1420
|
+
this.reset();
|
|
1421
|
+
return e;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
TryCatch.top = null;
|
|
1425
|
+
TryCatch._map = new Map();
|
|
1426
|
+
|
|
1427
|
+
//#endregion src/TryCatch.ts
|
|
1428
|
+
|
|
1429
|
+
//#region src/Template.ts
|
|
1430
|
+
var PropertyAttribute;
|
|
1431
|
+
(function (PropertyAttribute) {
|
|
1432
|
+
PropertyAttribute[PropertyAttribute["None"] = 0] = "None";
|
|
1433
|
+
PropertyAttribute[PropertyAttribute["ReadOnly"] = 1] = "ReadOnly";
|
|
1434
|
+
PropertyAttribute[PropertyAttribute["DontEnum"] = 2] = "DontEnum";
|
|
1435
|
+
PropertyAttribute[PropertyAttribute["DontDelete"] = 4] = "DontDelete";
|
|
1436
|
+
})(PropertyAttribute || (PropertyAttribute = {}));
|
|
1437
|
+
class Template {
|
|
1438
|
+
constructor(ctx) {
|
|
1439
|
+
this._properties = new Map();
|
|
1440
|
+
this.ctx = ctx;
|
|
1441
|
+
}
|
|
1442
|
+
set(name, value, attr) {
|
|
1443
|
+
this._properties.set(name, [value, attr]);
|
|
1444
|
+
}
|
|
1445
|
+
_addPropertiesToInstance(instance) {
|
|
1446
|
+
this._properties.forEach((property, name) => {
|
|
1447
|
+
const [v, attr] = property;
|
|
1448
|
+
if (v instanceof Template) {
|
|
1449
|
+
if ('newInstance' in v) {
|
|
1450
|
+
Object.defineProperty(instance, name, {
|
|
1451
|
+
value: v.newInstance(6),
|
|
1452
|
+
writable: !(attr & 1),
|
|
1453
|
+
enumerable: !(attr & 2),
|
|
1454
|
+
configurable: !(attr & 4)
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
else if ('getFunction' in v) {
|
|
1458
|
+
Object.defineProperty(instance, name, {
|
|
1459
|
+
value: v.getFunction(),
|
|
1460
|
+
writable: !(attr & 1),
|
|
1461
|
+
enumerable: !(attr & 2),
|
|
1462
|
+
configurable: !(attr & 4)
|
|
1463
|
+
});
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
else {
|
|
1467
|
+
Object.defineProperty(instance, name, {
|
|
1468
|
+
value: v,
|
|
1469
|
+
writable: !(attr & 1),
|
|
1470
|
+
enumerable: !(attr & 2),
|
|
1471
|
+
configurable: !(attr & 4)
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
});
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
//#endregion src/Template.ts
|
|
1479
|
+
|
|
1480
|
+
//#region src/ObjectTemplate.ts
|
|
1481
|
+
function findHolder(obj, _target) {
|
|
1482
|
+
return obj;
|
|
1483
|
+
}
|
|
1484
|
+
const internalField = new WeakMap();
|
|
1485
|
+
function getInternalFieldCount(instance) {
|
|
1486
|
+
return internalField.get(instance)?.length ?? 0;
|
|
1487
|
+
}
|
|
1488
|
+
function getInternalField(instance, index) {
|
|
1489
|
+
return internalField.get(instance)?.[index];
|
|
1490
|
+
}
|
|
1491
|
+
function setInternalField(instance, index, value) {
|
|
1492
|
+
let fields = internalField.get(instance);
|
|
1493
|
+
if (fields) {
|
|
1494
|
+
fields[index] = value;
|
|
1495
|
+
}
|
|
1496
|
+
else {
|
|
1497
|
+
fields = [];
|
|
1498
|
+
fields[index] = value;
|
|
1499
|
+
internalField.set(instance, fields);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
class ObjectTemplate extends Template {
|
|
1503
|
+
constructor(ctx, Ctor) {
|
|
1504
|
+
super(ctx);
|
|
1505
|
+
this.internalFieldCount = 0;
|
|
1506
|
+
this._accessors = new Map();
|
|
1507
|
+
this._instances = new WeakSet();
|
|
1508
|
+
this.Ctor = Ctor ?? Object;
|
|
1509
|
+
}
|
|
1510
|
+
setAccessor(name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType) {
|
|
1511
|
+
const config = {
|
|
1512
|
+
name,
|
|
1513
|
+
getterWrap,
|
|
1514
|
+
setterWrap,
|
|
1515
|
+
getter,
|
|
1516
|
+
setter,
|
|
1517
|
+
data,
|
|
1518
|
+
attribute,
|
|
1519
|
+
getterSideEffectType,
|
|
1520
|
+
setterSideEffectType,
|
|
1521
|
+
getterFunction: undefined,
|
|
1522
|
+
setterFunction: undefined
|
|
1523
|
+
};
|
|
1524
|
+
config.getterFunction = getter
|
|
1525
|
+
? this._createAccessorWrapper('getter', config)
|
|
1526
|
+
: undefined;
|
|
1527
|
+
config.setterFunction = setter
|
|
1528
|
+
? this._createAccessorWrapper('setter', config)
|
|
1529
|
+
: undefined;
|
|
1530
|
+
this._accessors.set(name, config);
|
|
1531
|
+
}
|
|
1532
|
+
setInternalFieldCount(value) {
|
|
1533
|
+
this.internalFieldCount = value;
|
|
1534
|
+
}
|
|
1535
|
+
_createAccessorWrapper(type, config) {
|
|
1536
|
+
const { ctx } = this;
|
|
1537
|
+
const instances = this._instances;
|
|
1538
|
+
const resolveHolder = (receiver) => {
|
|
1539
|
+
let holder = receiver;
|
|
1540
|
+
while (holder != null && !instances.has(holder)) {
|
|
1541
|
+
holder = Object.getPrototypeOf(holder);
|
|
1542
|
+
}
|
|
1543
|
+
return holder || receiver;
|
|
1544
|
+
};
|
|
1545
|
+
function accessor(value) {
|
|
1546
|
+
const scope = ctx.openScope();
|
|
1547
|
+
const callbackInfo = scope.callbackInfo;
|
|
1548
|
+
let returnValue;
|
|
1549
|
+
try {
|
|
1550
|
+
callbackInfo.data = config.data;
|
|
1551
|
+
callbackInfo.args = type === 'getter' ? [] : [value];
|
|
1552
|
+
callbackInfo.thiz = this;
|
|
1553
|
+
callbackInfo.holder = resolveHolder(this);
|
|
1554
|
+
callbackInfo.fn = accessor;
|
|
1555
|
+
const ret = type === 'getter'
|
|
1556
|
+
? config.getterWrap(ctx.napiValueFromJsValue(config.name), ctx.getCurrentScope().id, config.getter)
|
|
1557
|
+
: config.setterWrap(ctx.napiValueFromJsValue(config.name), ctx.napiValueFromJsValue(value), ctx.getCurrentScope().id, config.setter);
|
|
1558
|
+
returnValue = ret ? ctx.jsValueFromNapiValue(ret) : undefined;
|
|
1559
|
+
}
|
|
1560
|
+
catch (err) {
|
|
1561
|
+
ctx.throwException(err);
|
|
1562
|
+
}
|
|
1563
|
+
ctx.closeScope(scope);
|
|
1564
|
+
if (ctx.hasPendingException()) {
|
|
1565
|
+
if (TryCatch.top) {
|
|
1566
|
+
TryCatch.top.setError(ctx.getAndClearLastException());
|
|
1567
|
+
}
|
|
1568
|
+
else {
|
|
1569
|
+
throw ctx.getAndClearLastException();
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
return returnValue;
|
|
1573
|
+
}
|
|
1574
|
+
return accessor;
|
|
1575
|
+
}
|
|
1576
|
+
applyToInstance(instance) {
|
|
1577
|
+
this._instances.add(instance);
|
|
1578
|
+
internalField.set(instance, Array(this.internalFieldCount));
|
|
1579
|
+
this._addPropertiesToInstance(instance);
|
|
1580
|
+
this._accessors.forEach((config, name) => {
|
|
1581
|
+
Object.defineProperty(instance, name, {
|
|
1582
|
+
get: config.getterFunction,
|
|
1583
|
+
set: config.setterFunction,
|
|
1584
|
+
enumerable: !(config.attribute & 2),
|
|
1585
|
+
configurable: !(config.attribute & 4)
|
|
1586
|
+
});
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
newInstance(_context) {
|
|
1590
|
+
const { ctx, Ctor } = this;
|
|
1591
|
+
let instance;
|
|
1592
|
+
try {
|
|
1593
|
+
instance = new Ctor();
|
|
1594
|
+
}
|
|
1595
|
+
catch (err) {
|
|
1596
|
+
ctx.throwException(err);
|
|
1597
|
+
}
|
|
1598
|
+
this.applyToInstance(instance);
|
|
1599
|
+
return instance;
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
//#endregion src/ObjectTemplate.ts
|
|
1604
|
+
|
|
1605
|
+
//#region src/FunctionTemplate.ts
|
|
1606
|
+
class Signature {
|
|
1607
|
+
constructor(receiver) {
|
|
1608
|
+
this.receiver = receiver;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
class FunctionTemplate extends Template {
|
|
1612
|
+
constructor(ctx, callback, v8FunctionCallback, data, signature) {
|
|
1613
|
+
super(ctx);
|
|
1614
|
+
this.ctx = ctx;
|
|
1615
|
+
this.callback = callback;
|
|
1616
|
+
this.v8FunctionCallback = v8FunctionCallback;
|
|
1617
|
+
this.data = data;
|
|
1618
|
+
this.className = undefined;
|
|
1619
|
+
this.signature = signature;
|
|
1620
|
+
this._cached = [undefined];
|
|
1621
|
+
}
|
|
1622
|
+
setClassName(name) {
|
|
1623
|
+
this.className = name;
|
|
1624
|
+
}
|
|
1625
|
+
instanceTemplate() {
|
|
1626
|
+
if (!this._instanceTemplate) {
|
|
1627
|
+
this._instanceTemplate = new ObjectTemplate(this.ctx);
|
|
1628
|
+
}
|
|
1629
|
+
return this._instanceTemplate;
|
|
1630
|
+
}
|
|
1631
|
+
prototypeTemplate() {
|
|
1632
|
+
if (!this._prototypeTemplate) {
|
|
1633
|
+
this._prototypeTemplate = new ObjectTemplate(this.ctx);
|
|
1634
|
+
}
|
|
1635
|
+
return this._prototypeTemplate;
|
|
1636
|
+
}
|
|
1637
|
+
getFunction() {
|
|
1638
|
+
if (this._cached[0]) {
|
|
1639
|
+
return this._cached[0];
|
|
1640
|
+
}
|
|
1641
|
+
const { ctx, callback, v8FunctionCallback, data, signature, _instanceTemplate } = this;
|
|
1642
|
+
function _() {
|
|
1643
|
+
if (signature && signature.receiver) {
|
|
1644
|
+
const f = signature.receiver.getFunction();
|
|
1645
|
+
if (!(this instanceof f)) {
|
|
1646
|
+
throw new TypeError('Illegal invocation');
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
const scope = ctx.openScope();
|
|
1650
|
+
const callbackInfo = scope.callbackInfo;
|
|
1651
|
+
let returnValue;
|
|
1652
|
+
try {
|
|
1653
|
+
callbackInfo.data = data;
|
|
1654
|
+
callbackInfo.args = arguments;
|
|
1655
|
+
callbackInfo.thiz = this;
|
|
1656
|
+
callbackInfo.holder = findHolder(this, _) || this;
|
|
1657
|
+
callbackInfo.fn = _;
|
|
1658
|
+
if (_instanceTemplate && this instanceof _) {
|
|
1659
|
+
_instanceTemplate.applyToInstance(this);
|
|
1660
|
+
}
|
|
1661
|
+
const ret = callback(ctx.getCurrentScope().id, v8FunctionCallback);
|
|
1662
|
+
returnValue = ret ? ctx.jsValueFromNapiValue(ret) : undefined;
|
|
1663
|
+
}
|
|
1664
|
+
catch (err) {
|
|
1665
|
+
if (err !== 'unwind') {
|
|
1666
|
+
ctx.throwException(err);
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
finally {
|
|
1670
|
+
ctx.closeScope(scope);
|
|
1671
|
+
}
|
|
1672
|
+
if (ctx.hasPendingException()) {
|
|
1673
|
+
if (TryCatch.top) {
|
|
1674
|
+
TryCatch.top.setError(ctx.getAndClearLastException());
|
|
1675
|
+
}
|
|
1676
|
+
else {
|
|
1677
|
+
throw ctx.getAndClearLastException();
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
return returnValue;
|
|
1681
|
+
}
|
|
1682
|
+
if (typeof this.className === 'string') {
|
|
1683
|
+
this.ctx.features.setFunctionName?.(_, this.className);
|
|
1684
|
+
}
|
|
1685
|
+
if (this._prototypeTemplate) {
|
|
1686
|
+
this._prototypeTemplate.applyToInstance(_.prototype);
|
|
1687
|
+
}
|
|
1688
|
+
this._addPropertiesToInstance(_);
|
|
1689
|
+
this._cached[0] = _;
|
|
1690
|
+
return _;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
//#endregion src/FunctionTemplate.ts
|
|
1695
|
+
|
|
1696
|
+
//#region src/Private.ts
|
|
1697
|
+
const globalPrivateRegistry = new Map();
|
|
1698
|
+
const map = new WeakMap();
|
|
1699
|
+
class Private {
|
|
1700
|
+
constructor(name) {
|
|
1701
|
+
this._name = name;
|
|
1702
|
+
}
|
|
1703
|
+
name() {
|
|
1704
|
+
return this._name;
|
|
1705
|
+
}
|
|
1706
|
+
static forApi(name) {
|
|
1707
|
+
let privateInstance = globalPrivateRegistry.get(name);
|
|
1708
|
+
if (!privateInstance) {
|
|
1709
|
+
privateInstance = new Private(name);
|
|
1710
|
+
globalPrivateRegistry.set(name, privateInstance);
|
|
1711
|
+
}
|
|
1712
|
+
return privateInstance;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
function setPrivate(obj, key, value) {
|
|
1716
|
+
if (!(key instanceof Private)) {
|
|
1717
|
+
throw new TypeError('Expected key to be an instance of Private');
|
|
1718
|
+
}
|
|
1719
|
+
let m = map.get(key);
|
|
1720
|
+
if (!m) {
|
|
1721
|
+
m = new WeakMap();
|
|
1722
|
+
map.set(key, m);
|
|
1723
|
+
}
|
|
1724
|
+
m.set(obj, value);
|
|
1725
|
+
}
|
|
1726
|
+
function getPrivate(obj, key) {
|
|
1727
|
+
if (!(key instanceof Private)) {
|
|
1728
|
+
throw new TypeError('Expected key to be an instance of Private');
|
|
1729
|
+
}
|
|
1730
|
+
const m = map.get(key);
|
|
1731
|
+
if (!m) {
|
|
1732
|
+
return undefined;
|
|
1733
|
+
}
|
|
1734
|
+
return m.get(obj);
|
|
1735
|
+
}
|
|
1736
|
+
function hasPrivate(obj, key) {
|
|
1737
|
+
if (!(key instanceof Private)) {
|
|
1738
|
+
throw new TypeError('Expected key to be an instance of Private');
|
|
1739
|
+
}
|
|
1740
|
+
const m = map.get(key);
|
|
1741
|
+
if (!m) {
|
|
1742
|
+
return false;
|
|
1743
|
+
}
|
|
1744
|
+
return m.has(obj);
|
|
1745
|
+
}
|
|
1746
|
+
function deletePrivate(obj, key) {
|
|
1747
|
+
if (!(key instanceof Private)) {
|
|
1748
|
+
throw new TypeError('Expected key to be an instance of Private');
|
|
1749
|
+
}
|
|
1750
|
+
const m = map.get(key);
|
|
1751
|
+
if (!m || !m.has(obj)) {
|
|
1752
|
+
return true;
|
|
1753
|
+
}
|
|
1754
|
+
return m.delete(obj);
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
//#endregion src/Private.ts
|
|
1758
|
+
|
|
1759
|
+
//#region src/ExternalMemory.ts
|
|
1760
|
+
const kMaxReasonableBytes = BigInt(1) << BigInt(60);
|
|
1761
|
+
const kMinReasonableBytes = -kMaxReasonableBytes;
|
|
1762
|
+
class ExternalMemory {
|
|
1763
|
+
constructor(onChange) {
|
|
1764
|
+
this.total = BigInt(0);
|
|
1765
|
+
this.onChange = onChange ?? null;
|
|
1766
|
+
}
|
|
1767
|
+
adjust(changeInBytes) {
|
|
1768
|
+
changeInBytes = BigInt(changeInBytes);
|
|
1769
|
+
if (!(kMinReasonableBytes <= changeInBytes && changeInBytes < kMaxReasonableBytes)) {
|
|
1770
|
+
throw new RangeError(`changeInBytes ${changeInBytes} is out of reasonable range`);
|
|
1771
|
+
}
|
|
1772
|
+
const old = this.total;
|
|
1773
|
+
this.total += changeInBytes;
|
|
1774
|
+
const amount = this.total;
|
|
1775
|
+
const onChange = this.onChange;
|
|
1776
|
+
if (changeInBytes) {
|
|
1777
|
+
onChange?.(amount, old, changeInBytes);
|
|
1778
|
+
}
|
|
1779
|
+
return amount;
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
//#endregion src/ExternalMemory.ts
|
|
1784
|
+
|
|
1785
|
+
//#region src/Isolate.ts
|
|
1786
|
+
class Isolate {
|
|
1787
|
+
constructor(options) {
|
|
1788
|
+
this.features = detectFeatures(options?.features);
|
|
1789
|
+
this._globalThis = this.features.getGlobalThis();
|
|
1790
|
+
this._scopeStore = new ScopeStore();
|
|
1791
|
+
this._handleStore = new HandleStore(this.features);
|
|
1792
|
+
this.globalHandleStore = new PersistentStore();
|
|
1793
|
+
this._lastException = new Persistent(this);
|
|
1794
|
+
this._externalMemory = new ExternalMemory(options?.onExternalMemoryChange);
|
|
1795
|
+
}
|
|
1796
|
+
napiValueFromJsValue(value) {
|
|
1797
|
+
switch (value) {
|
|
1798
|
+
case undefined: return 2;
|
|
1799
|
+
case null: return 3;
|
|
1800
|
+
case false: return 4;
|
|
1801
|
+
case true: return 5;
|
|
1802
|
+
case '': return 7;
|
|
1803
|
+
case this._globalThis: return 6;
|
|
1804
|
+
default: return this._scopeStore.currentScope.add(value);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
jsValueFromNapiValue(napiValue) {
|
|
1808
|
+
return this._handleStore.deepDeref(napiValue);
|
|
1809
|
+
}
|
|
1810
|
+
deleteRefSlotValue(id) {
|
|
1811
|
+
this._handleStore.refValues.delete(Number(id));
|
|
1812
|
+
}
|
|
1813
|
+
getRefSlotValue(id) {
|
|
1814
|
+
return this._handleStore.refValues.get(Number(id));
|
|
1815
|
+
}
|
|
1816
|
+
setRefSlotValue(id, ref) {
|
|
1817
|
+
this._handleStore.refValues.set(Number(id), ref);
|
|
1818
|
+
}
|
|
1819
|
+
createReference(...args) {
|
|
1820
|
+
return new Persistent(this, ...args);
|
|
1821
|
+
}
|
|
1822
|
+
insertRef(persistent) {
|
|
1823
|
+
this.globalHandleStore.insert(persistent);
|
|
1824
|
+
}
|
|
1825
|
+
getRef(ref) {
|
|
1826
|
+
return this.globalHandleStore.deref(ref);
|
|
1827
|
+
}
|
|
1828
|
+
removeRef(ref, force = false) {
|
|
1829
|
+
if (force || this._scopeStore.isEmpty()) {
|
|
1830
|
+
const persistent = this.globalHandleStore.deref(ref);
|
|
1831
|
+
persistent?.dispose();
|
|
1832
|
+
}
|
|
1833
|
+
else {
|
|
1834
|
+
this.globalHandleStore.markUnused(ref);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
getTryCatch(address) {
|
|
1838
|
+
return TryCatch.deref(address);
|
|
1839
|
+
}
|
|
1840
|
+
pushTryCatch(address) {
|
|
1841
|
+
const tryCatch = new TryCatch(address);
|
|
1842
|
+
return tryCatch;
|
|
1843
|
+
}
|
|
1844
|
+
popTryCatch(address) {
|
|
1845
|
+
if (address !== TryCatch.top?.id) {
|
|
1846
|
+
throw new Error('TryCatch mismatch');
|
|
1847
|
+
}
|
|
1848
|
+
return TryCatch.pop();
|
|
1849
|
+
}
|
|
1850
|
+
setLastException(err) {
|
|
1851
|
+
this._lastException.resetTo(err);
|
|
1852
|
+
}
|
|
1853
|
+
throwException(err) {
|
|
1854
|
+
if (TryCatch.top) {
|
|
1855
|
+
TryCatch.top.setError(err);
|
|
1856
|
+
}
|
|
1857
|
+
else {
|
|
1858
|
+
this.setLastException(err);
|
|
1859
|
+
}
|
|
1860
|
+
return err;
|
|
1861
|
+
}
|
|
1862
|
+
hasPendingException() {
|
|
1863
|
+
return !this._lastException.isEmpty();
|
|
1864
|
+
}
|
|
1865
|
+
getAndClearLastException() {
|
|
1866
|
+
const err = this._lastException.deref();
|
|
1867
|
+
this._lastException.reset();
|
|
1868
|
+
return err;
|
|
1869
|
+
}
|
|
1870
|
+
adjustAmountOfExternalAllocatedMemory(changeInBytes) {
|
|
1871
|
+
return this._externalMemory.adjust(changeInBytes);
|
|
1872
|
+
}
|
|
1873
|
+
createSignature(template) {
|
|
1874
|
+
return new Signature(template);
|
|
1875
|
+
}
|
|
1876
|
+
createObjectTemplate(constructor) {
|
|
1877
|
+
return new ObjectTemplate(this, constructor);
|
|
1878
|
+
}
|
|
1879
|
+
setInternalField(obj, index, value) {
|
|
1880
|
+
setInternalField(obj, index, value);
|
|
1881
|
+
}
|
|
1882
|
+
getInternalField(obj, index) {
|
|
1883
|
+
return getInternalField(obj, index);
|
|
1884
|
+
}
|
|
1885
|
+
getInternalFieldCount(obj) {
|
|
1886
|
+
return getInternalFieldCount(obj);
|
|
1887
|
+
}
|
|
1888
|
+
createFunctionTemplate(callback, v8FunctionCallback, data, signature) {
|
|
1889
|
+
const functionTemplate = new FunctionTemplate(this, callback, v8FunctionCallback, data, signature);
|
|
1890
|
+
return functionTemplate;
|
|
1891
|
+
}
|
|
1892
|
+
isScopeEmpty() {
|
|
1893
|
+
return this._scopeStore.isEmpty();
|
|
1894
|
+
}
|
|
1895
|
+
getCurrentScope() {
|
|
1896
|
+
return this._scopeStore.currentScope;
|
|
1897
|
+
}
|
|
1898
|
+
openScope() {
|
|
1899
|
+
return this._scopeStore.openScope(this._handleStore);
|
|
1900
|
+
}
|
|
1901
|
+
closeScope(_scope) {
|
|
1902
|
+
this._scopeStore.closeScope();
|
|
1903
|
+
if (this.isScopeEmpty()) {
|
|
1904
|
+
this.globalHandleStore.recycle();
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
getHandleScope(scope) {
|
|
1908
|
+
return this._scopeStore.deref(scope);
|
|
1909
|
+
}
|
|
1910
|
+
getCallbackInfo(info) {
|
|
1911
|
+
return this._scopeStore.deref(info).callbackInfo;
|
|
1912
|
+
}
|
|
1913
|
+
createExternal(data) {
|
|
1914
|
+
return new External(data);
|
|
1915
|
+
}
|
|
1916
|
+
getExternalValue(external) {
|
|
1917
|
+
return getExternalValue(external);
|
|
1918
|
+
}
|
|
1919
|
+
isExternal(value) {
|
|
1920
|
+
return isExternal(value);
|
|
1921
|
+
}
|
|
1922
|
+
createResolver() {
|
|
1923
|
+
return this.features.withResolvers.call(Promise);
|
|
1924
|
+
}
|
|
1925
|
+
createPrivate(name) {
|
|
1926
|
+
return new Private(name);
|
|
1927
|
+
}
|
|
1928
|
+
getOrCreateGlobalPrivate(name) {
|
|
1929
|
+
return Private.forApi(name);
|
|
1930
|
+
}
|
|
1931
|
+
setPrivate(obj, key, value) {
|
|
1932
|
+
return setPrivate(obj, key, value);
|
|
1933
|
+
}
|
|
1934
|
+
getPrivate(obj, key) {
|
|
1935
|
+
return getPrivate(obj, key);
|
|
1936
|
+
}
|
|
1937
|
+
hasPrivate(obj, key) {
|
|
1938
|
+
return hasPrivate(obj, key);
|
|
1939
|
+
}
|
|
1940
|
+
deletePrivate(obj, key) {
|
|
1941
|
+
return deletePrivate(obj, key);
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
//#endregion src/Isolate.ts
|
|
1946
|
+
|
|
1947
|
+
//#region src/Context.ts
|
|
1948
|
+
const callbackWrapper = (envObject, callback) => {
|
|
1949
|
+
const napiValue = envObject.callIntoModule(callback);
|
|
1950
|
+
return (!napiValue) ? undefined : envObject.ctx.jsValueFromNapiValue(napiValue);
|
|
1951
|
+
};
|
|
1952
|
+
const withScope = (envObject, thiz, args, data, getFunction, wrapper, callback) => {
|
|
1953
|
+
const scope = envObject.ctx.openScope(envObject);
|
|
1954
|
+
const callbackInfo = scope.callbackInfo;
|
|
1955
|
+
callbackInfo.data = data;
|
|
1956
|
+
callbackInfo.args = args;
|
|
1957
|
+
callbackInfo.thiz = thiz;
|
|
1958
|
+
callbackInfo.fn = getFunction;
|
|
1959
|
+
try {
|
|
1960
|
+
return wrapper(envObject, callback);
|
|
1961
|
+
}
|
|
1962
|
+
catch (e) {
|
|
1963
|
+
if (e !== 'unwind') {
|
|
1964
|
+
throw e;
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
finally {
|
|
1968
|
+
envObject.ctx.closeScope(envObject, scope);
|
|
1969
|
+
}
|
|
1970
|
+
};
|
|
1971
|
+
class CleanupHookCallback {
|
|
1972
|
+
constructor(envObject, fn, arg) {
|
|
1973
|
+
this.envObject = envObject;
|
|
1974
|
+
this.fn = fn;
|
|
1975
|
+
this.arg = arg;
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
class CleanupQueue {
|
|
1979
|
+
constructor() {
|
|
1980
|
+
this._cleanupHooks = [];
|
|
1981
|
+
}
|
|
1982
|
+
empty() {
|
|
1983
|
+
return this._cleanupHooks.length === 0;
|
|
1984
|
+
}
|
|
1985
|
+
add(envObject, fn, arg) {
|
|
1986
|
+
if (this._cleanupHookIndex === undefined) {
|
|
1987
|
+
for (let i = 0; i < this._cleanupHooks.length; ++i) {
|
|
1988
|
+
const hook = this._cleanupHooks[i];
|
|
1989
|
+
if (hook.envObject === envObject && hook.fn === fn && hook.arg === arg) {
|
|
1990
|
+
throw new Error('Can not add same fn and arg twice');
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
this._cleanupHooks.push(new CleanupHookCallback(envObject, fn, arg));
|
|
1994
|
+
if (this._cleanupHooks.length > CleanupQueue.INDEX_THRESHOLD) {
|
|
1995
|
+
this._createIndex();
|
|
1996
|
+
}
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
let envHooks = this._cleanupHookIndex.get(envObject);
|
|
2000
|
+
if (envHooks === undefined) {
|
|
2001
|
+
envHooks = new Map();
|
|
2002
|
+
this._cleanupHookIndex.set(envObject, envHooks);
|
|
2003
|
+
}
|
|
2004
|
+
let args = envHooks.get(fn);
|
|
2005
|
+
if (args === undefined) {
|
|
2006
|
+
args = new Set();
|
|
2007
|
+
envHooks.set(fn, args);
|
|
2008
|
+
}
|
|
2009
|
+
else if (args.has(arg)) {
|
|
2010
|
+
throw new Error('Can not add same fn and arg twice');
|
|
2011
|
+
}
|
|
2012
|
+
args.add(arg);
|
|
2013
|
+
this._cleanupHooks.push(new CleanupHookCallback(envObject, fn, arg));
|
|
2014
|
+
}
|
|
2015
|
+
remove(envObject, fn, arg) {
|
|
2016
|
+
for (let i = 0; i < this._cleanupHooks.length; ++i) {
|
|
2017
|
+
const hook = this._cleanupHooks[i];
|
|
2018
|
+
if (hook.envObject === envObject && hook.fn === fn && hook.arg === arg) {
|
|
2019
|
+
this._cleanupHooks.splice(i, 1);
|
|
2020
|
+
this._deleteIndex(hook);
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
drain() {
|
|
2026
|
+
const hooks = this._cleanupHooks.slice();
|
|
2027
|
+
for (let i = hooks.length - 1; i >= 0; --i) {
|
|
2028
|
+
const cb = hooks[i];
|
|
2029
|
+
if (typeof cb.fn === 'number') {
|
|
2030
|
+
cb.envObject.bridge.makeDynCall_vp(cb.fn)(cb.arg);
|
|
2031
|
+
}
|
|
2032
|
+
else {
|
|
2033
|
+
cb.fn(cb.arg);
|
|
2034
|
+
}
|
|
2035
|
+
const lastIndex = this._cleanupHooks.length - 1;
|
|
2036
|
+
if (this._cleanupHooks[lastIndex] === cb) {
|
|
2037
|
+
this._cleanupHooks.pop();
|
|
2038
|
+
this._deleteIndex(cb);
|
|
2039
|
+
}
|
|
2040
|
+
else {
|
|
2041
|
+
const index = this._cleanupHooks.lastIndexOf(cb);
|
|
2042
|
+
if (index !== -1) {
|
|
2043
|
+
this._cleanupHooks.splice(index, 1);
|
|
2044
|
+
this._deleteIndex(cb);
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
if (this._cleanupHooks.length === 0) {
|
|
2049
|
+
this._cleanupHookIndex = undefined;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
_createIndex() {
|
|
2053
|
+
const index = new WeakMap();
|
|
2054
|
+
for (let i = 0; i < this._cleanupHooks.length; ++i) {
|
|
2055
|
+
const hook = this._cleanupHooks[i];
|
|
2056
|
+
let envHooks = index.get(hook.envObject);
|
|
2057
|
+
if (envHooks === undefined) {
|
|
2058
|
+
envHooks = new Map();
|
|
2059
|
+
index.set(hook.envObject, envHooks);
|
|
2060
|
+
}
|
|
2061
|
+
let args = envHooks.get(hook.fn);
|
|
2062
|
+
if (args === undefined) {
|
|
2063
|
+
args = new Set();
|
|
2064
|
+
envHooks.set(hook.fn, args);
|
|
2065
|
+
}
|
|
2066
|
+
args.add(hook.arg);
|
|
2067
|
+
}
|
|
2068
|
+
this._cleanupHookIndex = index;
|
|
2069
|
+
}
|
|
2070
|
+
_deleteIndex(hook) {
|
|
2071
|
+
if (this._cleanupHookIndex === undefined)
|
|
2072
|
+
return;
|
|
2073
|
+
const envHooks = this._cleanupHookIndex.get(hook.envObject);
|
|
2074
|
+
const args = envHooks.get(hook.fn);
|
|
2075
|
+
args.delete(hook.arg);
|
|
2076
|
+
if (args.size === 0)
|
|
2077
|
+
envHooks.delete(hook.fn);
|
|
2078
|
+
if (envHooks.size === 0)
|
|
2079
|
+
this._cleanupHookIndex.delete(hook.envObject);
|
|
2080
|
+
}
|
|
2081
|
+
dispose() {
|
|
2082
|
+
this._cleanupHooks.length = 0;
|
|
2083
|
+
this._cleanupHookIndex = undefined;
|
|
2084
|
+
}
|
|
2085
|
+
}
|
|
2086
|
+
CleanupQueue.INDEX_THRESHOLD = 16;
|
|
2087
|
+
class NodejsWaitingRequestCounter {
|
|
2088
|
+
constructor(MessageChannel) {
|
|
2089
|
+
this.refHandle = new MessageChannel().port1;
|
|
2090
|
+
this.count = 0;
|
|
2091
|
+
}
|
|
2092
|
+
increase() {
|
|
2093
|
+
if (this.count === 0) {
|
|
2094
|
+
if (this.refHandle.ref) {
|
|
2095
|
+
this.refHandle.ref();
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
this.count++;
|
|
2099
|
+
}
|
|
2100
|
+
decrease() {
|
|
2101
|
+
if (this.count === 0)
|
|
2102
|
+
return;
|
|
2103
|
+
if (this.count === 1) {
|
|
2104
|
+
if (this.refHandle.unref) {
|
|
2105
|
+
this.refHandle.unref();
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
this.count--;
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
class Context {
|
|
2112
|
+
constructor(options) {
|
|
2113
|
+
this._isStopping = false;
|
|
2114
|
+
this._canCallIntoJs = true;
|
|
2115
|
+
this._suppressDestroy = false;
|
|
2116
|
+
this.envStore = new ArrayStore();
|
|
2117
|
+
this.refStore = new Map();
|
|
2118
|
+
this.isolate = new Isolate(options);
|
|
2119
|
+
this.features = this.isolate.features;
|
|
2120
|
+
this.cleanupQueue = new CleanupQueue();
|
|
2121
|
+
if (typeof process === 'object' && process !== null && typeof process.once === 'function' && this.features.MessageChannel) {
|
|
2122
|
+
this.refCounter = new NodejsWaitingRequestCounter(this.features.MessageChannel);
|
|
2123
|
+
process.once('beforeExit', () => {
|
|
2124
|
+
if (!this._suppressDestroy) {
|
|
2125
|
+
this.destroy();
|
|
2126
|
+
}
|
|
2127
|
+
});
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
getIsolate() {
|
|
2131
|
+
return this.isolate;
|
|
2132
|
+
}
|
|
2133
|
+
suppressDestroy() {
|
|
2134
|
+
this._suppressDestroy = true;
|
|
2135
|
+
}
|
|
2136
|
+
getRuntimeVersions() {
|
|
2137
|
+
return {
|
|
2138
|
+
version,
|
|
2139
|
+
NODE_API_SUPPORTED_VERSION_MAX,
|
|
2140
|
+
NAPI_VERSION_EXPERIMENTAL,
|
|
2141
|
+
NODE_API_DEFAULT_MODULE_API_VERSION,
|
|
2142
|
+
NODE_MODULE_VERSION
|
|
2143
|
+
};
|
|
2144
|
+
}
|
|
2145
|
+
createNotSupportWeakRefError(api, message) {
|
|
2146
|
+
return new NotSupportWeakRefError(api, message);
|
|
2147
|
+
}
|
|
2148
|
+
createNotSupportBufferError(api, message) {
|
|
2149
|
+
return new NotSupportBufferError(api, message);
|
|
2150
|
+
}
|
|
2151
|
+
createReference(envObject, handle_id, initialRefcount, ownership) {
|
|
2152
|
+
return Reference.create(this, envObject, handle_id, initialRefcount, ownership);
|
|
2153
|
+
}
|
|
2154
|
+
createReferenceWithData(envObject, handle_id, initialRefcount, ownership, data) {
|
|
2155
|
+
return ReferenceWithData.create(this, envObject, handle_id, initialRefcount, ownership, data);
|
|
2156
|
+
}
|
|
2157
|
+
createReferenceWithFinalizer(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
|
|
2158
|
+
return ReferenceWithFinalizer.create(this, envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
|
|
2159
|
+
}
|
|
2160
|
+
createResolver() {
|
|
2161
|
+
return this.isolate.createResolver();
|
|
2162
|
+
}
|
|
2163
|
+
adjustAmountOfExternalAllocatedMemory(changeInBytes) {
|
|
2164
|
+
return this.isolate.adjustAmountOfExternalAllocatedMemory(changeInBytes);
|
|
2165
|
+
}
|
|
2166
|
+
createEnv(filename, moduleApiVersion, bridge, nodeBinding) {
|
|
2167
|
+
moduleApiVersion = typeof moduleApiVersion !== 'number' ? NODE_API_DEFAULT_MODULE_API_VERSION : moduleApiVersion;
|
|
2168
|
+
if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
|
|
2169
|
+
moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
|
|
2170
|
+
}
|
|
2171
|
+
else if (moduleApiVersion > NODE_API_SUPPORTED_VERSION_MAX && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
|
|
2172
|
+
const errorMessage = `${filename} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NODE_API_SUPPORTED_VERSION_MAX} add-ons.`;
|
|
2173
|
+
throw new Error(errorMessage);
|
|
2174
|
+
}
|
|
2175
|
+
const env = new NodeEnv(this, this.envStore, { ...bridge });
|
|
2176
|
+
env.filename = filename;
|
|
2177
|
+
env.moduleApiVersion = moduleApiVersion;
|
|
2178
|
+
env.nodeBinding = nodeBinding;
|
|
2179
|
+
this.addCleanupHook(env, () => { env.unref(); }, 0);
|
|
2180
|
+
return env;
|
|
2181
|
+
}
|
|
2182
|
+
createFunction(envObject, napiCallback, data, name, dynamicExecution) {
|
|
2183
|
+
if (envObject.ctx !== this) {
|
|
2184
|
+
throw new Error(`The napi_env (${envObject.bridge.address}) is not created by this context`);
|
|
2185
|
+
}
|
|
2186
|
+
const callback = (envObject) => {
|
|
2187
|
+
return napiCallback(envObject.bridge.address, envObject.ctx.getCurrentScope().id);
|
|
2188
|
+
};
|
|
2189
|
+
let _;
|
|
2190
|
+
const staticFunctionWrapper = (withScope, envObject, data, callbackWrapper, callback) => {
|
|
2191
|
+
return function () {
|
|
2192
|
+
return withScope(envObject, this, arguments, data, _, callbackWrapper, callback);
|
|
2193
|
+
};
|
|
2194
|
+
};
|
|
2195
|
+
let functionWrapper;
|
|
2196
|
+
if (name && dynamicExecution && this.features.makeDynamicFunction) {
|
|
2197
|
+
try {
|
|
2198
|
+
functionWrapper = this.features.makeDynamicFunction('withScope', 'envObject', 'data', 'callbackWrapper', 'callback', 'return function ' + name + '(){' +
|
|
2199
|
+
'return withScope(envObject,this,arguments,data,' + name + ',callbackWrapper,callback)' +
|
|
2200
|
+
'};');
|
|
2201
|
+
}
|
|
2202
|
+
catch (_) {
|
|
2203
|
+
functionWrapper = staticFunctionWrapper;
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
else {
|
|
2207
|
+
functionWrapper = staticFunctionWrapper;
|
|
2208
|
+
}
|
|
2209
|
+
_ = functionWrapper(withScope, envObject, data, callbackWrapper, callback);
|
|
2210
|
+
if (functionWrapper === staticFunctionWrapper && this.features.setFunctionName && name) {
|
|
2211
|
+
this.features.setFunctionName(_, name);
|
|
2212
|
+
}
|
|
2213
|
+
return _;
|
|
2214
|
+
}
|
|
2215
|
+
createTrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
|
|
2216
|
+
return TrackedFinalizer.create(envObject, finalize_callback, finalize_data, finalize_hint);
|
|
2217
|
+
}
|
|
2218
|
+
getCurrentScope() {
|
|
2219
|
+
return this.isolate.getCurrentScope();
|
|
2220
|
+
}
|
|
2221
|
+
openScope(envObject) {
|
|
2222
|
+
const scope = this.isolate.openScope();
|
|
2223
|
+
envObject.openHandleScopes++;
|
|
2224
|
+
return scope;
|
|
2225
|
+
}
|
|
2226
|
+
closeScope(envObject, scope) {
|
|
2227
|
+
this.isolate.closeScope(scope);
|
|
2228
|
+
envObject.openHandleScopes--;
|
|
2229
|
+
}
|
|
2230
|
+
getEnv(env) {
|
|
2231
|
+
return this.envStore.deref(env);
|
|
2232
|
+
}
|
|
2233
|
+
getRef(ref) {
|
|
2234
|
+
return this.refStore.get(Number(ref));
|
|
2235
|
+
}
|
|
2236
|
+
getHandleScope(scope) {
|
|
2237
|
+
return this.isolate.getHandleScope(scope);
|
|
2238
|
+
}
|
|
2239
|
+
getCallbackInfo(info) {
|
|
2240
|
+
return this.isolate.getCallbackInfo(info);
|
|
2241
|
+
}
|
|
2242
|
+
napiValueFromJsValue(value) {
|
|
2243
|
+
return this.isolate.napiValueFromJsValue(value);
|
|
2244
|
+
}
|
|
2245
|
+
jsValueFromNapiValue(napiValue) {
|
|
2246
|
+
return this.isolate.jsValueFromNapiValue(napiValue);
|
|
2247
|
+
}
|
|
2248
|
+
createExternal(data) {
|
|
2249
|
+
return this.isolate.createExternal(data);
|
|
2250
|
+
}
|
|
2251
|
+
getExternalValue(external) {
|
|
2252
|
+
return this.isolate.getExternalValue(external);
|
|
2253
|
+
}
|
|
2254
|
+
isExternal(value) {
|
|
2255
|
+
return this.isolate.isExternal(value);
|
|
2256
|
+
}
|
|
2257
|
+
addCleanupHook(envObject, fn, arg) {
|
|
2258
|
+
this.cleanupQueue.add(envObject, fn, arg);
|
|
2259
|
+
}
|
|
2260
|
+
removeCleanupHook(envObject, fn, arg) {
|
|
2261
|
+
this.cleanupQueue.remove(envObject, fn, arg);
|
|
2262
|
+
}
|
|
2263
|
+
runCleanup() {
|
|
2264
|
+
while (!this.cleanupQueue.empty()) {
|
|
2265
|
+
this.cleanupQueue.drain();
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
increaseWaitingRequestCounter() {
|
|
2269
|
+
this.refCounter?.increase();
|
|
2270
|
+
}
|
|
2271
|
+
decreaseWaitingRequestCounter() {
|
|
2272
|
+
this.refCounter?.decrease();
|
|
2273
|
+
}
|
|
2274
|
+
setCanCallIntoJs(value) {
|
|
2275
|
+
this._canCallIntoJs = value;
|
|
2276
|
+
}
|
|
2277
|
+
setStopping(value) {
|
|
2278
|
+
this._isStopping = value;
|
|
2279
|
+
}
|
|
2280
|
+
canCallIntoJs() {
|
|
2281
|
+
return this._canCallIntoJs && !this._isStopping;
|
|
2282
|
+
}
|
|
2283
|
+
getDylinkMetadata(binary) {
|
|
2284
|
+
return getDylinkMetadata(binary);
|
|
2285
|
+
}
|
|
2286
|
+
destroy() {
|
|
2287
|
+
this.setStopping(true);
|
|
2288
|
+
this.setCanCallIntoJs(false);
|
|
2289
|
+
this.runCleanup();
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
let defaultContext;
|
|
2293
|
+
function createContext(options) {
|
|
2294
|
+
return new Context(options);
|
|
2295
|
+
}
|
|
2296
|
+
function getDefaultContext() {
|
|
2297
|
+
if (!defaultContext) {
|
|
2298
|
+
defaultContext = createContext();
|
|
2299
|
+
}
|
|
2300
|
+
return defaultContext;
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
//#endregion src/Context.ts
|
|
2304
|
+
|
|
2305
|
+
export { ArrayStore, BaseArrayStore, Context, CountIdAllocator, CountIdReuseAllocator, Disposable, EmnapiError, Env, External, Finalizer, FunctionTemplate, HandleScope, HandleStore, Isolate, NAPI_VERSION_EXPERIMENTAL, NODE_API_DEFAULT_MODULE_API_VERSION, NODE_API_SUPPORTED_VERSION_MAX, NODE_API_SUPPORTED_VERSION_MIN, NodeEnv, NotSupportBufferError, NotSupportWeakRefError, ObjectTemplate, Persistent, PersistentStore, Private, RefTracker, Reference, ReferenceOwnership, ReferenceWithData, ReferenceWithFinalizer, ScopeStore, Signature, StrongRef, Template, TrackedFinalizer, TryCatch, createContext, deletePrivate, getDefaultContext, getDylinkMetadata, getExternalValue, getInternalField, getPrivate, hasPrivate, isExternal, isReferenceType, setInternalField, setPrivate, version };
|
|
2306
|
+
//# sourceMappingURL=emnapi.js.map
|