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,1470 @@
|
|
|
1
|
+
//#region ../node_modules/@emnapi/core/dist/plugins/async-work.js
|
|
2
|
+
function asyncWork(emnapiPluginCtx) {
|
|
3
|
+
const { emnapiCtx, emnapiNodeBinding, emnapiAsyncWorkPoolSize } = emnapiPluginCtx;
|
|
4
|
+
var mod = (function(exports, emnapi_shared, emscripten_runtime) {
|
|
5
|
+
/**
|
|
6
|
+
* @__deps $emnapiCtx
|
|
7
|
+
* @__deps $emnapiEnv
|
|
8
|
+
* @__deps $emnapiNodeBinding
|
|
9
|
+
* @__deps $emnapiAsyncWorkPoolSize
|
|
10
|
+
* @__postset
|
|
11
|
+
* ```
|
|
12
|
+
* emnapiAWST.init();
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
var emnapiAWST = {
|
|
16
|
+
idGen: {},
|
|
17
|
+
values: [void 0],
|
|
18
|
+
queued: /* @__PURE__ */ new Set(),
|
|
19
|
+
pending: [],
|
|
20
|
+
pendingHead: 0,
|
|
21
|
+
init: function() {
|
|
22
|
+
const idGen = {
|
|
23
|
+
nextId: 1,
|
|
24
|
+
list: [],
|
|
25
|
+
listHead: 0,
|
|
26
|
+
generate: function() {
|
|
27
|
+
let id;
|
|
28
|
+
if (idGen.listHead < idGen.list.length) {
|
|
29
|
+
id = idGen.list[idGen.listHead++];
|
|
30
|
+
if (idGen.listHead === idGen.list.length) {
|
|
31
|
+
idGen.list.length = 0;
|
|
32
|
+
idGen.listHead = 0;
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
id = idGen.nextId;
|
|
36
|
+
idGen.nextId++;
|
|
37
|
+
}
|
|
38
|
+
return id;
|
|
39
|
+
},
|
|
40
|
+
reuse: function(id) {
|
|
41
|
+
idGen.list.push(id);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
emnapiAWST.idGen = idGen;
|
|
45
|
+
emnapiAWST.values = [void 0];
|
|
46
|
+
emnapiAWST.queued = /* @__PURE__ */ new Set();
|
|
47
|
+
emnapiAWST.pending = [];
|
|
48
|
+
emnapiAWST.pendingHead = 0;
|
|
49
|
+
},
|
|
50
|
+
create: function(env, resource, resourceName, execute, complete, data) {
|
|
51
|
+
let asyncId = 0;
|
|
52
|
+
let triggerAsyncId = 0;
|
|
53
|
+
if (emnapiNodeBinding) {
|
|
54
|
+
const asyncContext = emnapiNodeBinding.node.emitAsyncInit(resource, resourceName, -1);
|
|
55
|
+
asyncId = asyncContext.asyncId;
|
|
56
|
+
triggerAsyncId = asyncContext.triggerAsyncId;
|
|
57
|
+
}
|
|
58
|
+
const id = emnapiAWST.idGen.generate();
|
|
59
|
+
emnapiAWST.values[id] = {
|
|
60
|
+
env,
|
|
61
|
+
id,
|
|
62
|
+
resource,
|
|
63
|
+
asyncId,
|
|
64
|
+
triggerAsyncId,
|
|
65
|
+
status: 0,
|
|
66
|
+
execute,
|
|
67
|
+
complete,
|
|
68
|
+
data
|
|
69
|
+
};
|
|
70
|
+
return id;
|
|
71
|
+
},
|
|
72
|
+
callComplete: function(work, status) {
|
|
73
|
+
const complete = work.complete;
|
|
74
|
+
const env = work.env;
|
|
75
|
+
const data = work.data;
|
|
76
|
+
const callback = () => {
|
|
77
|
+
if (!complete) return;
|
|
78
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
79
|
+
const scope = emnapiCtx.openScope(envObject);
|
|
80
|
+
try {
|
|
81
|
+
envObject.callbackIntoModule(true, () => {
|
|
82
|
+
emnapiPluginCtx.wasmTable.get(complete)(env, status, data);
|
|
83
|
+
});
|
|
84
|
+
} finally {
|
|
85
|
+
emnapiCtx.closeScope(envObject, scope);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
if (emnapiNodeBinding) emnapiNodeBinding.node.makeCallback(work.resource, callback, [], {
|
|
89
|
+
asyncId: work.asyncId,
|
|
90
|
+
triggerAsyncId: work.triggerAsyncId
|
|
91
|
+
});
|
|
92
|
+
else callback();
|
|
93
|
+
},
|
|
94
|
+
queue: function(id) {
|
|
95
|
+
const work = emnapiAWST.values[id];
|
|
96
|
+
if (!work) return;
|
|
97
|
+
if (work.status === 0) {
|
|
98
|
+
work.status = 1;
|
|
99
|
+
if (emnapiAWST.queued.size >= (Math.abs(emnapiAsyncWorkPoolSize) || 4)) {
|
|
100
|
+
if (emnapiAWST.pendingHead >= 1024 && emnapiAWST.pendingHead * 2 >= emnapiAWST.pending.length) {
|
|
101
|
+
emnapiAWST.pending.splice(0, emnapiAWST.pendingHead);
|
|
102
|
+
emnapiAWST.pendingHead = 0;
|
|
103
|
+
}
|
|
104
|
+
emnapiAWST.pending.push(id);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
emnapiAWST.queued.add(id);
|
|
108
|
+
const env = work.env;
|
|
109
|
+
const data = work.data;
|
|
110
|
+
const execute = work.execute;
|
|
111
|
+
work.status = 2;
|
|
112
|
+
emnapiCtx.features.setImmediate(() => {
|
|
113
|
+
emnapiPluginCtx.wasmTable.get(execute)(env, data);
|
|
114
|
+
emnapiAWST.queued.delete(id);
|
|
115
|
+
work.status = 3;
|
|
116
|
+
emnapiCtx.features.setImmediate(() => {
|
|
117
|
+
emnapiAWST.callComplete(work, 0);
|
|
118
|
+
});
|
|
119
|
+
if (emnapiAWST.pendingHead < emnapiAWST.pending.length) {
|
|
120
|
+
const nextWorkId = emnapiAWST.pending[emnapiAWST.pendingHead++];
|
|
121
|
+
if (emnapiAWST.pendingHead === emnapiAWST.pending.length) {
|
|
122
|
+
emnapiAWST.pending.length = 0;
|
|
123
|
+
emnapiAWST.pendingHead = 0;
|
|
124
|
+
}
|
|
125
|
+
emnapiAWST.values[nextWorkId].status = 0;
|
|
126
|
+
emnapiAWST.queue(nextWorkId);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
cancel: function(id) {
|
|
132
|
+
const index = emnapiAWST.pending.indexOf(id, emnapiAWST.pendingHead);
|
|
133
|
+
if (index !== -1) {
|
|
134
|
+
const work = emnapiAWST.values[id];
|
|
135
|
+
if (work && work.status === 1) {
|
|
136
|
+
work.status = 4;
|
|
137
|
+
emnapiAWST.pending.splice(index, 1);
|
|
138
|
+
if (emnapiAWST.pendingHead === emnapiAWST.pending.length) {
|
|
139
|
+
emnapiAWST.pending.length = 0;
|
|
140
|
+
emnapiAWST.pendingHead = 0;
|
|
141
|
+
}
|
|
142
|
+
emnapiCtx.features.setImmediate(() => {
|
|
143
|
+
emnapiAWST.callComplete(work, 11);
|
|
144
|
+
});
|
|
145
|
+
return 0;
|
|
146
|
+
} else return 9;
|
|
147
|
+
}
|
|
148
|
+
return 9;
|
|
149
|
+
},
|
|
150
|
+
remove: function(id) {
|
|
151
|
+
const work = emnapiAWST.values[id];
|
|
152
|
+
if (!work) return;
|
|
153
|
+
if (emnapiNodeBinding) emnapiNodeBinding.node.emitAsyncDestroy({
|
|
154
|
+
asyncId: work.asyncId,
|
|
155
|
+
triggerAsyncId: work.triggerAsyncId
|
|
156
|
+
});
|
|
157
|
+
emnapiAWST.values[id] = void 0;
|
|
158
|
+
emnapiAWST.idGen.reuse(id);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const { _emnapi_node_emit_async_init, _emnapi_node_emit_async_destroy, _emnapi_runtime_keepalive_pop, _emnapi_runtime_keepalive_push } = emnapiPluginCtx;
|
|
162
|
+
var emnapiAWMT = {
|
|
163
|
+
pool: [],
|
|
164
|
+
workerReady: null,
|
|
165
|
+
globalAddress: 0,
|
|
166
|
+
globalOffset: {
|
|
167
|
+
idle_threads: 0,
|
|
168
|
+
q: 4,
|
|
169
|
+
next: 4,
|
|
170
|
+
prev: 8,
|
|
171
|
+
mutex: 12,
|
|
172
|
+
cond: 16,
|
|
173
|
+
exit_message: 20,
|
|
174
|
+
end: 28
|
|
175
|
+
},
|
|
176
|
+
offset: {
|
|
177
|
+
resource: 0,
|
|
178
|
+
async_id: 8,
|
|
179
|
+
trigger_async_id: 16,
|
|
180
|
+
env: 24,
|
|
181
|
+
status: 28,
|
|
182
|
+
queue: 32,
|
|
183
|
+
queue_next: 32,
|
|
184
|
+
queue_prev: 36,
|
|
185
|
+
data: 40,
|
|
186
|
+
execute: 44,
|
|
187
|
+
complete: 48,
|
|
188
|
+
end: 52
|
|
189
|
+
},
|
|
190
|
+
/**
|
|
191
|
+
* When another thread grows the shared WebAssembly.Memory, this agent's
|
|
192
|
+
* cached `wasmMemory.buffer` may still have the old shorter length
|
|
193
|
+
* (V8 refreshes it lazily). If a pointer derived from shared memory lies
|
|
194
|
+
* beyond the cached length, `wasmMemory.grow(0)` forces the agent to
|
|
195
|
+
* observe the current memory size and refreshes the buffer.
|
|
196
|
+
*/
|
|
197
|
+
ensureBufferFor(end) {
|
|
198
|
+
let buffer = emscripten_runtime.wasmMemory.buffer;
|
|
199
|
+
if (end > buffer.byteLength) {
|
|
200
|
+
emscripten_runtime.wasmMemory.grow(0);
|
|
201
|
+
buffer = emscripten_runtime.wasmMemory.buffer;
|
|
202
|
+
}
|
|
203
|
+
return buffer;
|
|
204
|
+
},
|
|
205
|
+
init() {
|
|
206
|
+
emnapiAWMT.pool = [];
|
|
207
|
+
emnapiAWMT.workerReady = null;
|
|
208
|
+
if (typeof emscripten_runtime.PThread !== "undefined") {
|
|
209
|
+
emscripten_runtime.PThread.unusedWorkers.forEach(emnapiAWMT.addListener);
|
|
210
|
+
Object.values(emscripten_runtime.PThread.pthreads).forEach(emnapiAWMT.addListener);
|
|
211
|
+
const __original_getNewWorker = emscripten_runtime.PThread.getNewWorker;
|
|
212
|
+
emscripten_runtime.PThread.getNewWorker = function() {
|
|
213
|
+
const r = __original_getNewWorker.apply(this, arguments);
|
|
214
|
+
emnapiAWMT.addListener(r);
|
|
215
|
+
return r;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
addListener(worker) {
|
|
220
|
+
if (!worker) return false;
|
|
221
|
+
if (worker._emnapiAWMTListener) return true;
|
|
222
|
+
const handler = function(e) {
|
|
223
|
+
const __emnapi__ = (emscripten_runtime.ENVIRONMENT_IS_NODE ? e : e.data).__emnapi__;
|
|
224
|
+
if (__emnapi__) {
|
|
225
|
+
const type = __emnapi__.type;
|
|
226
|
+
const payload = __emnapi__.payload;
|
|
227
|
+
if (type === "async-work-complete") emnapiAWMT.callComplete(payload.work, 0);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
const dispose = function() {
|
|
231
|
+
if (emscripten_runtime.ENVIRONMENT_IS_NODE) worker.off("message", handler);
|
|
232
|
+
else worker.removeEventListener("message", handler, false);
|
|
233
|
+
delete worker._emnapiAWMTListener;
|
|
234
|
+
};
|
|
235
|
+
worker._emnapiAWMTListener = {
|
|
236
|
+
handler,
|
|
237
|
+
dispose
|
|
238
|
+
};
|
|
239
|
+
if (emscripten_runtime.ENVIRONMENT_IS_NODE) worker.on("message", handler);
|
|
240
|
+
else worker.addEventListener("message", handler, false);
|
|
241
|
+
return true;
|
|
242
|
+
},
|
|
243
|
+
initGlobal() {
|
|
244
|
+
if (!emnapiAWMT.globalAddress) {
|
|
245
|
+
emnapiAWMT.globalAddress = emscripten_runtime._malloc(emnapiAWMT.globalOffset.end);
|
|
246
|
+
emnapiAWMT.globalAddress >>>= 0;
|
|
247
|
+
const size = emnapiAWMT.globalOffset.end;
|
|
248
|
+
const addr = emnapiAWMT.globalAddress;
|
|
249
|
+
new Uint8Array(emnapiAWMT.ensureBufferFor(addr + size), addr, size).fill(0);
|
|
250
|
+
emnapiAWMT.queueInit(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.q);
|
|
251
|
+
emnapiAWMT.queueInit(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.exit_message);
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
terminateWorkers() {
|
|
255
|
+
emnapiAWMT.pool.forEach((w) => {
|
|
256
|
+
w._emnapiAWMTListener?.dispose();
|
|
257
|
+
w._emnapiTSFNListener?.dispose();
|
|
258
|
+
w.terminate();
|
|
259
|
+
});
|
|
260
|
+
emnapiAWMT.pool.length = 0;
|
|
261
|
+
},
|
|
262
|
+
initWorkers(n) {
|
|
263
|
+
if (emscripten_runtime.ENVIRONMENT_IS_PTHREAD) return emnapiAWMT.workerReady || (emnapiAWMT.workerReady = Promise.resolve());
|
|
264
|
+
if (emnapiAWMT.workerReady) return emnapiAWMT.workerReady;
|
|
265
|
+
if (!("emnapi_async_worker_create" in emscripten_runtime.wasmInstance.exports)) throw new TypeError("`emnapi_async_worker_create` is not exported, please try to add `--export=emnapi_async_worker_create` to linker flags");
|
|
266
|
+
const emnapi_async_worker_create = emscripten_runtime.wasmInstance.exports.emnapi_async_worker_create;
|
|
267
|
+
const args = [];
|
|
268
|
+
emnapiAWMT.initGlobal();
|
|
269
|
+
for (let i = 0; i < n; ++i) args.push(emnapi_async_worker_create(1, emnapiAWMT.globalAddress));
|
|
270
|
+
const promises = args.map((index) => {
|
|
271
|
+
if (index === 0) return Promise.reject(/* @__PURE__ */ new Error("Failed to create async worker"));
|
|
272
|
+
let worker;
|
|
273
|
+
if (index < 0) {
|
|
274
|
+
worker = emnapiAWMT.pool[-index - 1];
|
|
275
|
+
if (worker) return worker.whenLoaded;
|
|
276
|
+
}
|
|
277
|
+
index >>>= 0;
|
|
278
|
+
const tidOffset = 20;
|
|
279
|
+
const tid = new DataView(emnapiAWMT.ensureBufferFor(index + tidOffset + 4)).getInt32(index + tidOffset, true);
|
|
280
|
+
worker = emscripten_runtime.PThread.pthreads[tid];
|
|
281
|
+
return worker.whenLoaded;
|
|
282
|
+
});
|
|
283
|
+
emnapiAWMT.workerReady = Promise.all(promises);
|
|
284
|
+
return emnapiAWMT.workerReady;
|
|
285
|
+
},
|
|
286
|
+
getResource(work) {
|
|
287
|
+
emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.resource + 4);
|
|
288
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
289
|
+
return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.resource, true);
|
|
290
|
+
},
|
|
291
|
+
getExecute(work) {
|
|
292
|
+
emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.execute + 4);
|
|
293
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
294
|
+
return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.execute, true);
|
|
295
|
+
},
|
|
296
|
+
getComplete(work) {
|
|
297
|
+
emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.complete + 4);
|
|
298
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
299
|
+
return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.complete, true);
|
|
300
|
+
},
|
|
301
|
+
getEnv(work) {
|
|
302
|
+
emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.env + 4);
|
|
303
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
304
|
+
return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.env, true);
|
|
305
|
+
},
|
|
306
|
+
getData(work) {
|
|
307
|
+
emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.data + 4);
|
|
308
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
309
|
+
return GET_HEAP_DATA_VIEW().getUint32(work + emnapiAWMT.offset.data, true);
|
|
310
|
+
},
|
|
311
|
+
getMutex() {
|
|
312
|
+
const index = emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.mutex;
|
|
313
|
+
const mutex = {
|
|
314
|
+
lock() {
|
|
315
|
+
const isBrowserMain = typeof window !== "undefined" && typeof document !== "undefined" && !emscripten_runtime.ENVIRONMENT_IS_NODE;
|
|
316
|
+
const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
|
|
317
|
+
if (isBrowserMain) {
|
|
318
|
+
while (true) if (Atomics.compareExchange(i32a, 0, 0, 10) === 0) return;
|
|
319
|
+
} else while (true) {
|
|
320
|
+
if (Atomics.compareExchange(i32a, 0, 0, 10) === 0) return;
|
|
321
|
+
Atomics.wait(i32a, 0, 10);
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
unlock() {
|
|
325
|
+
const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
|
|
326
|
+
if (Atomics.compareExchange(i32a, 0, 10, 0) !== 10) throw new Error("Tried to unlock while not holding the mutex");
|
|
327
|
+
Atomics.notify(i32a, 0, 1);
|
|
328
|
+
},
|
|
329
|
+
execute(fn) {
|
|
330
|
+
mutex.lock();
|
|
331
|
+
try {
|
|
332
|
+
return fn();
|
|
333
|
+
} finally {
|
|
334
|
+
mutex.unlock();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
return mutex;
|
|
339
|
+
},
|
|
340
|
+
getCond() {
|
|
341
|
+
const index = emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.cond;
|
|
342
|
+
const mutex = emnapiAWMT.getMutex();
|
|
343
|
+
return {
|
|
344
|
+
wait() {
|
|
345
|
+
const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
|
|
346
|
+
const value = Atomics.load(i32a, 0);
|
|
347
|
+
mutex.unlock();
|
|
348
|
+
Atomics.wait(i32a, 0, value);
|
|
349
|
+
mutex.lock();
|
|
350
|
+
},
|
|
351
|
+
signal() {
|
|
352
|
+
const i32a = new Int32Array(emnapiAWMT.ensureBufferFor(index + 4), index, 1);
|
|
353
|
+
Atomics.add(i32a, 0, 1);
|
|
354
|
+
Atomics.notify(i32a, 0, 1);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
},
|
|
358
|
+
queueInit(q) {
|
|
359
|
+
emnapiAWMT.ensureBufferFor(q + 4 + 4);
|
|
360
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
361
|
+
GET_HEAP_DATA_VIEW().setUint32(q, q, true);
|
|
362
|
+
GET_HEAP_DATA_VIEW().setUint32(q + 4, q, true);
|
|
363
|
+
},
|
|
364
|
+
queueInsertTail(h, q) {
|
|
365
|
+
emnapiAWMT.ensureBufferFor(h + 4 + 4);
|
|
366
|
+
emnapiAWMT.ensureBufferFor(q + 4 + 4);
|
|
367
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
368
|
+
GET_HEAP_DATA_VIEW().setUint32(q, h, true);
|
|
369
|
+
const tempValue = GET_HEAP_DATA_VIEW().getUint32(h + 4, true);
|
|
370
|
+
GET_HEAP_DATA_VIEW().setUint32(q + 4, tempValue, true);
|
|
371
|
+
const qprev = GET_HEAP_DATA_VIEW().getUint32(q + 4, true);
|
|
372
|
+
GET_HEAP_DATA_VIEW().setUint32(qprev, q, true);
|
|
373
|
+
GET_HEAP_DATA_VIEW().setUint32(h + 4, q, true);
|
|
374
|
+
},
|
|
375
|
+
queueRemove(q) {
|
|
376
|
+
emnapiAWMT.ensureBufferFor(q + 4 + 4);
|
|
377
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
378
|
+
const qprev = GET_HEAP_DATA_VIEW().getUint32(q + 4, true);
|
|
379
|
+
const qnext = GET_HEAP_DATA_VIEW().getUint32(q, true);
|
|
380
|
+
GET_HEAP_DATA_VIEW().setUint32(qprev, qnext, true);
|
|
381
|
+
GET_HEAP_DATA_VIEW().setUint32(qnext + 4, qprev, true);
|
|
382
|
+
},
|
|
383
|
+
queueEmpty(q) {
|
|
384
|
+
emnapiAWMT.ensureBufferFor(q + 4);
|
|
385
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
386
|
+
return q == GET_HEAP_DATA_VIEW().getUint32(q, true);
|
|
387
|
+
},
|
|
388
|
+
scheduleWork: function(work) {
|
|
389
|
+
if (!emnapiAWMT.workerReady?.ready) emnapiAWMT.initWorkers(emnapi_shared._emnapi_async_work_pool_size()).then(() => {
|
|
390
|
+
emnapiAWMT.workerReady.ready = true;
|
|
391
|
+
}).catch((err) => {
|
|
392
|
+
emnapiAWMT.workerReady = null;
|
|
393
|
+
throw err;
|
|
394
|
+
});
|
|
395
|
+
_emnapi_runtime_keepalive_push();
|
|
396
|
+
emnapiCtx.increaseWaitingRequestCounter();
|
|
397
|
+
const statusBuffer = new Int32Array(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4), work + emnapiAWMT.offset.status, 1);
|
|
398
|
+
Atomics.store(statusBuffer, 0, 0);
|
|
399
|
+
const mutex = emnapiAWMT.getMutex();
|
|
400
|
+
const cond = emnapiAWMT.getCond();
|
|
401
|
+
mutex.lock();
|
|
402
|
+
try {
|
|
403
|
+
emnapiAWMT.queueInsertTail(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.q, work + emnapiAWMT.offset.queue);
|
|
404
|
+
} catch (err) {
|
|
405
|
+
_emnapi_runtime_keepalive_pop();
|
|
406
|
+
emnapiCtx.decreaseWaitingRequestCounter();
|
|
407
|
+
mutex.unlock();
|
|
408
|
+
throw err;
|
|
409
|
+
}
|
|
410
|
+
emnapiAWMT.ensureBufferFor(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.idle_threads + 4);
|
|
411
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
412
|
+
if (GET_HEAP_DATA_VIEW().getUint32(emnapiAWMT.globalAddress + emnapiAWMT.globalOffset.idle_threads, true) > 0) cond.signal();
|
|
413
|
+
mutex.unlock();
|
|
414
|
+
},
|
|
415
|
+
cancelWork(work) {
|
|
416
|
+
let cancelled = false;
|
|
417
|
+
emnapiAWMT.getMutex().execute(() => {
|
|
418
|
+
emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4);
|
|
419
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
420
|
+
cancelled = !emnapiAWMT.queueEmpty(work + emnapiAWMT.offset.queue) && GET_HEAP_DATA_VIEW().getInt32(work + emnapiAWMT.offset.status, true) !== 2;
|
|
421
|
+
if (cancelled) emnapiAWMT.queueRemove(work + emnapiAWMT.offset.queue);
|
|
422
|
+
});
|
|
423
|
+
if (!cancelled) return 9;
|
|
424
|
+
if (Atomics.compareExchange(new Int32Array(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4), work + emnapiAWMT.offset.status, 1), 0, 0, 1) !== 0) return 9;
|
|
425
|
+
emnapiCtx.features.setImmediate(() => {
|
|
426
|
+
emnapiAWMT.callComplete(work, 11);
|
|
427
|
+
});
|
|
428
|
+
return 0;
|
|
429
|
+
},
|
|
430
|
+
callComplete: function(work, status) {
|
|
431
|
+
_emnapi_runtime_keepalive_pop();
|
|
432
|
+
emnapiCtx.decreaseWaitingRequestCounter();
|
|
433
|
+
const complete = emnapiAWMT.getComplete(work);
|
|
434
|
+
const env = emnapiAWMT.getEnv(work);
|
|
435
|
+
const data = emnapiAWMT.getData(work);
|
|
436
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
437
|
+
const scope = emnapiCtx.openScope(envObject);
|
|
438
|
+
const callback = () => {
|
|
439
|
+
if (!complete) return;
|
|
440
|
+
envObject.callbackIntoModule(true, () => {
|
|
441
|
+
emnapiPluginCtx.wasmTable.get(complete)(env, status, data);
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
try {
|
|
445
|
+
if (emnapiNodeBinding) {
|
|
446
|
+
const resource = emnapiAWMT.getResource(work);
|
|
447
|
+
const resource_value = emnapiCtx.getRef(resource).get();
|
|
448
|
+
const resourceObject = emnapiCtx.jsValueFromNapiValue(resource_value);
|
|
449
|
+
const view = new DataView(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.trigger_async_id + 8));
|
|
450
|
+
const asyncId = view.getFloat64(work + emnapiAWMT.offset.async_id, true);
|
|
451
|
+
const triggerAsyncId = view.getFloat64(work + emnapiAWMT.offset.trigger_async_id, true);
|
|
452
|
+
emnapiNodeBinding.node.makeCallback(resourceObject, callback, [], {
|
|
453
|
+
asyncId,
|
|
454
|
+
triggerAsyncId
|
|
455
|
+
});
|
|
456
|
+
} else callback();
|
|
457
|
+
} finally {
|
|
458
|
+
emnapiCtx.closeScope(envObject, scope);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
emnapiAWST.init();
|
|
463
|
+
emnapiAWMT.init();
|
|
464
|
+
/** @__sig ippppppp */
|
|
465
|
+
var napi_create_async_work = emnapi_shared.singleThreadAsyncWork ? function(env, resource, resource_name, execute, complete, data, result) {
|
|
466
|
+
if (!env) return 1;
|
|
467
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
468
|
+
envObject.checkGCAccess();
|
|
469
|
+
if (!execute) return envObject.setLastError(1);
|
|
470
|
+
if (!result) return envObject.setLastError(1);
|
|
471
|
+
let resourceObject;
|
|
472
|
+
if (resource) resourceObject = Object(emnapiCtx.jsValueFromNapiValue(resource));
|
|
473
|
+
else resourceObject = {};
|
|
474
|
+
if (!resource_name) return envObject.setLastError(1);
|
|
475
|
+
const resourceName = String(emnapiCtx.jsValueFromNapiValue(resource_name));
|
|
476
|
+
const id = emnapiAWST.create(env, resourceObject, resourceName, execute, complete, data);
|
|
477
|
+
result >>>= 0;
|
|
478
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
479
|
+
GET_HEAP_DATA_VIEW().setUint32(result, id, true);
|
|
480
|
+
return envObject.clearLastError();
|
|
481
|
+
} : function(env, resource, resource_name, execute, complete, data, result) {
|
|
482
|
+
if (!env) return 1;
|
|
483
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
484
|
+
envObject.checkGCAccess();
|
|
485
|
+
if (!execute) return envObject.setLastError(1);
|
|
486
|
+
if (!result) return envObject.setLastError(1);
|
|
487
|
+
let resourceObject;
|
|
488
|
+
if (resource) resourceObject = Object(emnapiCtx.jsValueFromNapiValue(resource));
|
|
489
|
+
else resourceObject = {};
|
|
490
|
+
if (!resource_name) return envObject.setLastError(1);
|
|
491
|
+
const sizeofAW = emnapiAWMT.offset.end;
|
|
492
|
+
let aw = emscripten_runtime._malloc(sizeofAW);
|
|
493
|
+
if (!aw) return envObject.setLastError(9);
|
|
494
|
+
aw >>>= 0;
|
|
495
|
+
new Uint8Array(emnapiAWMT.ensureBufferFor(aw + sizeofAW)).subarray(aw, aw + sizeofAW).fill(0);
|
|
496
|
+
const s = emnapiCtx.napiValueFromJsValue(resourceObject);
|
|
497
|
+
const resource_ = emnapiCtx.createReference(envObject, s, 1, 1).id;
|
|
498
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
499
|
+
GET_HEAP_DATA_VIEW().setUint32(aw, resource_, true);
|
|
500
|
+
_emnapi_node_emit_async_init(s, resource_name, -1, aw + emnapiAWMT.offset.async_id);
|
|
501
|
+
GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.env, env, true);
|
|
502
|
+
GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.execute, execute, true);
|
|
503
|
+
GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.complete, complete, true);
|
|
504
|
+
GET_HEAP_DATA_VIEW().setUint32(aw + emnapiAWMT.offset.data, data, true);
|
|
505
|
+
emnapiAWMT.queueInit(aw + emnapiAWMT.offset.queue);
|
|
506
|
+
result >>>= 0;
|
|
507
|
+
GET_HEAP_DATA_VIEW().setUint32(result, aw, true);
|
|
508
|
+
return envObject.clearLastError();
|
|
509
|
+
};
|
|
510
|
+
/** @__sig ipp */
|
|
511
|
+
var napi_delete_async_work = emnapi_shared.singleThreadAsyncWork ? function(env, work) {
|
|
512
|
+
if (!env) return 1;
|
|
513
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
514
|
+
envObject.checkGCAccess();
|
|
515
|
+
if (!work) return envObject.setLastError(1);
|
|
516
|
+
work >>>= 0;
|
|
517
|
+
emnapiAWST.remove(work);
|
|
518
|
+
return envObject.clearLastError();
|
|
519
|
+
} : function(env, work) {
|
|
520
|
+
if (!env) return 1;
|
|
521
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
522
|
+
envObject.checkGCAccess();
|
|
523
|
+
if (!work) return envObject.setLastError(1);
|
|
524
|
+
work >>>= 0;
|
|
525
|
+
const resource = emnapiAWMT.getResource(work);
|
|
526
|
+
emnapiCtx.getRef(resource).dispose();
|
|
527
|
+
if (emnapiNodeBinding) {
|
|
528
|
+
const view = new DataView(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.trigger_async_id + 8));
|
|
529
|
+
const asyncId = view.getFloat64(work + emnapiAWMT.offset.async_id, true);
|
|
530
|
+
const triggerAsyncId = view.getFloat64(work + emnapiAWMT.offset.trigger_async_id, true);
|
|
531
|
+
_emnapi_node_emit_async_destroy(asyncId, triggerAsyncId);
|
|
532
|
+
}
|
|
533
|
+
emscripten_runtime._free(work);
|
|
534
|
+
return envObject.clearLastError();
|
|
535
|
+
};
|
|
536
|
+
/** @__sig ipp */
|
|
537
|
+
var napi_queue_async_work = emnapi_shared.singleThreadAsyncWork ? function(env, work) {
|
|
538
|
+
if (!env) return 1;
|
|
539
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
540
|
+
if (!work) return envObject.setLastError(1);
|
|
541
|
+
work >>>= 0;
|
|
542
|
+
emnapiAWST.queue(work);
|
|
543
|
+
return envObject.clearLastError();
|
|
544
|
+
} : function(env, work) {
|
|
545
|
+
if (!env) return 1;
|
|
546
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
547
|
+
if (!work) return envObject.setLastError(1);
|
|
548
|
+
work >>>= 0;
|
|
549
|
+
emnapiAWMT.scheduleWork(work);
|
|
550
|
+
return envObject.clearLastError();
|
|
551
|
+
};
|
|
552
|
+
/** @__sig ipp */
|
|
553
|
+
var napi_cancel_async_work = emnapi_shared.singleThreadAsyncWork ? function(env, work) {
|
|
554
|
+
if (!env) return 1;
|
|
555
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
556
|
+
if (!work) return envObject.setLastError(1);
|
|
557
|
+
work >>>= 0;
|
|
558
|
+
const status = emnapiAWST.cancel(work);
|
|
559
|
+
if (status === 0) return envObject.clearLastError();
|
|
560
|
+
return envObject.setLastError(status);
|
|
561
|
+
} : function(env, work) {
|
|
562
|
+
if (!env) return 1;
|
|
563
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
564
|
+
if (!work) return envObject.setLastError(1);
|
|
565
|
+
work >>>= 0;
|
|
566
|
+
const status = emnapiAWMT.cancelWork(work);
|
|
567
|
+
if (status === 0) return envObject.clearLastError();
|
|
568
|
+
return envObject.setLastError(status);
|
|
569
|
+
};
|
|
570
|
+
/** @__sig pp */
|
|
571
|
+
function _emnapi_async_worker(globalAddress) {
|
|
572
|
+
globalAddress >>>= 0;
|
|
573
|
+
emnapiAWMT.globalAddress = globalAddress;
|
|
574
|
+
const mutex = emnapiAWMT.getMutex();
|
|
575
|
+
const cond = emnapiAWMT.getCond();
|
|
576
|
+
mutex.lock();
|
|
577
|
+
const exitMessageAddr = globalAddress + emnapiAWMT.globalOffset.exit_message;
|
|
578
|
+
const idleThreadsAddr = globalAddress + emnapiAWMT.globalOffset.idle_threads;
|
|
579
|
+
const workerQueueAddr = globalAddress + emnapiAWMT.globalOffset.q;
|
|
580
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
581
|
+
for (;;) {
|
|
582
|
+
emnapiAWMT.ensureBufferFor(workerQueueAddr + 4);
|
|
583
|
+
while (emnapiAWMT.queueEmpty(workerQueueAddr)) {
|
|
584
|
+
Atomics.add(new Int32Array(emnapiAWMT.ensureBufferFor(idleThreadsAddr + 4), idleThreadsAddr, 1), 0, 1);
|
|
585
|
+
cond.wait();
|
|
586
|
+
Atomics.sub(new Int32Array(emnapiAWMT.ensureBufferFor(idleThreadsAddr + 4), idleThreadsAddr, 1), 0, 1);
|
|
587
|
+
}
|
|
588
|
+
const q = GET_HEAP_DATA_VIEW().getUint32(workerQueueAddr, true);
|
|
589
|
+
if (q === exitMessageAddr) {
|
|
590
|
+
cond.signal();
|
|
591
|
+
mutex.unlock();
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
const work = q - emnapiAWMT.offset.queue;
|
|
595
|
+
emnapiAWMT.queueRemove(q);
|
|
596
|
+
emnapiAWMT.queueInit(q);
|
|
597
|
+
mutex.unlock();
|
|
598
|
+
const statusBuffer = new Int32Array(emnapiAWMT.ensureBufferFor(work + emnapiAWMT.offset.status + 4), work + emnapiAWMT.offset.status, 1);
|
|
599
|
+
if (Atomics.load(statusBuffer, 0) === 1) emscripten_runtime.abort("unreachable");
|
|
600
|
+
const execute = emnapiAWMT.getExecute(work);
|
|
601
|
+
const env = emnapiAWMT.getEnv(work);
|
|
602
|
+
const data = emnapiAWMT.getData(work);
|
|
603
|
+
emnapiPluginCtx.wasmTable.get(execute)(env, data);
|
|
604
|
+
Atomics.store(statusBuffer, 0, 2);
|
|
605
|
+
const postMessage = emnapi_shared.napiModule.postMessage;
|
|
606
|
+
postMessage({ __emnapi__: {
|
|
607
|
+
type: "async-work-complete",
|
|
608
|
+
payload: { work }
|
|
609
|
+
} });
|
|
610
|
+
mutex.lock();
|
|
611
|
+
}
|
|
612
|
+
return 0;
|
|
613
|
+
}
|
|
614
|
+
/** @__sig ipp */
|
|
615
|
+
function _emnapi_spawn_worker(f, globalAddress) {
|
|
616
|
+
if (typeof emnapi_shared.onCreateWorker !== "function") throw new TypeError("`options.onCreateWorker` is not a function");
|
|
617
|
+
const promises = [];
|
|
618
|
+
const args = [];
|
|
619
|
+
if (!("emnapi_async_worker_create" in emscripten_runtime.wasmInstance.exports)) throw new TypeError("`emnapi_async_worker_create` is not exported, please try to add `--export=emnapi_async_worker_create` to linker flags");
|
|
620
|
+
args.push(emscripten_runtime.wasmInstance.exports.emnapi_async_worker_create(0, 0));
|
|
621
|
+
let ret;
|
|
622
|
+
try {
|
|
623
|
+
const worker = emnapi_shared.onCreateWorker({
|
|
624
|
+
type: "async-work",
|
|
625
|
+
name: "emnapi-async-worker"
|
|
626
|
+
});
|
|
627
|
+
const handleError = () => {
|
|
628
|
+
if (!worker.loaded) return;
|
|
629
|
+
Promise.resolve().then(() => {
|
|
630
|
+
emnapiAWMT.terminateWorkers();
|
|
631
|
+
emscripten_runtime.PThread.terminateAllThreads();
|
|
632
|
+
});
|
|
633
|
+
};
|
|
634
|
+
if (emscripten_runtime.ENVIRONMENT_IS_NODE) worker.on("error", handleError);
|
|
635
|
+
else worker.addEventListener("error", handleError, false);
|
|
636
|
+
const p = emscripten_runtime.PThread.loadWasmModuleToWorker(worker);
|
|
637
|
+
emnapiAWMT.addListener(worker);
|
|
638
|
+
if (typeof emnapiPluginCtx.emnapiTSFN !== "undefined") emnapiPluginCtx.emnapiTSFN.addListener(worker);
|
|
639
|
+
promises.push(p.then(() => {
|
|
640
|
+
if (typeof worker.unref === "function") worker.unref();
|
|
641
|
+
}));
|
|
642
|
+
ret = emnapiAWMT.pool.push(worker) - 1;
|
|
643
|
+
const arg = args[0];
|
|
644
|
+
worker.threadBlockBase = arg;
|
|
645
|
+
worker.postMessage({ __emnapi__: {
|
|
646
|
+
type: "async-worker-init",
|
|
647
|
+
payload: {
|
|
648
|
+
arg,
|
|
649
|
+
func: [f, globalAddress]
|
|
650
|
+
}
|
|
651
|
+
} });
|
|
652
|
+
} catch (err) {
|
|
653
|
+
const arg = args[0];
|
|
654
|
+
emscripten_runtime._free(arg);
|
|
655
|
+
throw err;
|
|
656
|
+
}
|
|
657
|
+
return ret;
|
|
658
|
+
}
|
|
659
|
+
function initWorker(startArg, func) {
|
|
660
|
+
if (emnapi_shared.napiModule.childThread) {
|
|
661
|
+
if (typeof emscripten_runtime.wasmInstance.exports.emnapi_async_worker_init !== "function") throw new TypeError("`emnapi_async_worker_init` is not exported, please try to add `--export=emnapi_async_worker_init` to linker flags");
|
|
662
|
+
emscripten_runtime.wasmInstance.exports.emnapi_async_worker_init(startArg);
|
|
663
|
+
emnapiPluginCtx.wasmTable.get(func[0])(func[1]);
|
|
664
|
+
} else throw new Error("startThread is only available in child threads");
|
|
665
|
+
}
|
|
666
|
+
emnapi_shared.napiModule.initWorker = initWorker;
|
|
667
|
+
exports._emnapi_async_worker = _emnapi_async_worker;
|
|
668
|
+
exports._emnapi_spawn_worker = _emnapi_spawn_worker;
|
|
669
|
+
exports.napi_cancel_async_work = napi_cancel_async_work;
|
|
670
|
+
exports.napi_create_async_work = napi_create_async_work;
|
|
671
|
+
exports.napi_delete_async_work = napi_delete_async_work;
|
|
672
|
+
exports.napi_queue_async_work = napi_queue_async_work;
|
|
673
|
+
return exports;
|
|
674
|
+
})({}, emnapiPluginCtx, emnapiPluginCtx);
|
|
675
|
+
return { importObject: (original) => {
|
|
676
|
+
Object.keys(mod).forEach((key) => {
|
|
677
|
+
if (key.startsWith("napi_") || key.startsWith("node_api_")) original.napi[key] = mod[key];
|
|
678
|
+
else original.env[key] = mod[key];
|
|
679
|
+
});
|
|
680
|
+
} };
|
|
681
|
+
}
|
|
682
|
+
//#endregion
|
|
683
|
+
//#region ../node_modules/@emnapi/core/dist/plugins/threadsafe-function.js
|
|
684
|
+
function threadsafeFunction(emnapiPluginCtx) {
|
|
685
|
+
const { emnapiCtx, emnapiNodeBinding, _emnapi_node_emit_async_destroy: __emnapi_node_emit_async_destroy, _emnapi_node_emit_async_init: __emnapi_node_emit_async_init, _emnapi_runtime_keepalive_pop: __emnapi_runtime_keepalive_pop, _emnapi_runtime_keepalive_push: __emnapi_runtime_keepalive_push } = emnapiPluginCtx;
|
|
686
|
+
var mod = (function(exports, emnapi_shared, emscripten_runtime) {
|
|
687
|
+
/**
|
|
688
|
+
* @__deps malloc
|
|
689
|
+
* @__deps free
|
|
690
|
+
* @__deps $emnapiCtx
|
|
691
|
+
* @__deps $emnapiEnv
|
|
692
|
+
* @__deps $emnapiNodeBinding
|
|
693
|
+
* @__deps _emnapi_node_emit_async_destroy
|
|
694
|
+
* @__deps _emnapi_runtime_keepalive_pop
|
|
695
|
+
* @__postset
|
|
696
|
+
* ```
|
|
697
|
+
* emnapiTSFN.init();
|
|
698
|
+
* ```
|
|
699
|
+
*/
|
|
700
|
+
const emnapiTSFN = {
|
|
701
|
+
_liveSet: {},
|
|
702
|
+
offset: {
|
|
703
|
+
__size__: 0,
|
|
704
|
+
resource: 0,
|
|
705
|
+
async_id: 0,
|
|
706
|
+
trigger_async_id: 0,
|
|
707
|
+
queue_size: 0,
|
|
708
|
+
is_some: 0,
|
|
709
|
+
queue: 0,
|
|
710
|
+
async_pending: 0,
|
|
711
|
+
async_u_fd: 0,
|
|
712
|
+
thread_count: 0,
|
|
713
|
+
state: 0,
|
|
714
|
+
dispatch_state: 0,
|
|
715
|
+
context: 0,
|
|
716
|
+
max_queue_size: 0,
|
|
717
|
+
ref: 0,
|
|
718
|
+
env: 0,
|
|
719
|
+
finalize_data: 0,
|
|
720
|
+
finalize_cb: 0,
|
|
721
|
+
call_js_cb: 0,
|
|
722
|
+
handles_closing: 0,
|
|
723
|
+
async_ref: 0,
|
|
724
|
+
mutex: 0,
|
|
725
|
+
cond: 0
|
|
726
|
+
},
|
|
727
|
+
init() {
|
|
728
|
+
emnapiTSFN._liveSet = /* @__PURE__ */ new Set();
|
|
729
|
+
emnapiTSFN.offset.__size__ = 184;
|
|
730
|
+
emnapiTSFN.offset.resource = 0;
|
|
731
|
+
emnapiTSFN.offset.async_id = 8;
|
|
732
|
+
emnapiTSFN.offset.trigger_async_id = 16;
|
|
733
|
+
emnapiTSFN.offset.queue_size = 60;
|
|
734
|
+
emnapiTSFN.offset.is_some = 24;
|
|
735
|
+
emnapiTSFN.offset.queue = 64;
|
|
736
|
+
emnapiTSFN.offset.async_pending = 132;
|
|
737
|
+
emnapiTSFN.offset.async_u_fd = 96;
|
|
738
|
+
emnapiTSFN.offset.thread_count = 136;
|
|
739
|
+
emnapiTSFN.offset.state = 140;
|
|
740
|
+
emnapiTSFN.offset.dispatch_state = 144;
|
|
741
|
+
emnapiTSFN.offset.context = 148;
|
|
742
|
+
emnapiTSFN.offset.max_queue_size = 152;
|
|
743
|
+
emnapiTSFN.offset.ref = 156;
|
|
744
|
+
emnapiTSFN.offset.env = 160;
|
|
745
|
+
emnapiTSFN.offset.finalize_data = 164;
|
|
746
|
+
emnapiTSFN.offset.finalize_cb = 168;
|
|
747
|
+
emnapiTSFN.offset.call_js_cb = 172;
|
|
748
|
+
emnapiTSFN.offset.handles_closing = 176;
|
|
749
|
+
emnapiTSFN.offset.async_ref = 180;
|
|
750
|
+
emnapiTSFN.offset.mutex = 32;
|
|
751
|
+
emnapiTSFN.offset.cond = 56;
|
|
752
|
+
emnapiTSFN.offset.mutex = emnapiTSFN.offset.mutex + 4;
|
|
753
|
+
if (typeof emscripten_runtime.PThread !== "undefined") {
|
|
754
|
+
emscripten_runtime.PThread.unusedWorkers.forEach(emnapiTSFN.addListener);
|
|
755
|
+
Object.values(emscripten_runtime.PThread.pthreads).forEach(emnapiTSFN.addListener);
|
|
756
|
+
const __original_getNewWorker = emscripten_runtime.PThread.getNewWorker;
|
|
757
|
+
emscripten_runtime.PThread.getNewWorker = function() {
|
|
758
|
+
const r = __original_getNewWorker.apply(this, arguments);
|
|
759
|
+
emnapiTSFN.addListener(r);
|
|
760
|
+
return r;
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
},
|
|
764
|
+
addListener(worker) {
|
|
765
|
+
if (!worker) return false;
|
|
766
|
+
if (worker._emnapiTSFNListener) return true;
|
|
767
|
+
const handler = function(e) {
|
|
768
|
+
const __emnapi__ = (emscripten_runtime.ENVIRONMENT_IS_NODE ? e : e.data).__emnapi__;
|
|
769
|
+
if (__emnapi__) {
|
|
770
|
+
const type = __emnapi__.type;
|
|
771
|
+
const payload = __emnapi__.payload;
|
|
772
|
+
if (type === "tsfn-send") {
|
|
773
|
+
const pendng = payload.tsfn + emnapiTSFN.offset.async_pending;
|
|
774
|
+
if (Atomics.load(new Int32Array(emnapiTSFN.ensureBufferFor(pendng + 4)), pendng >>> 2) !== 0) emnapiTSFN.enqueue(payload.tsfn);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
const dispose = function() {
|
|
779
|
+
if (emscripten_runtime.ENVIRONMENT_IS_NODE) worker.off("message", handler);
|
|
780
|
+
else worker.removeEventListener("message", handler, false);
|
|
781
|
+
delete worker._emnapiTSFNListener;
|
|
782
|
+
};
|
|
783
|
+
worker._emnapiTSFNListener = {
|
|
784
|
+
handler,
|
|
785
|
+
dispose
|
|
786
|
+
};
|
|
787
|
+
if (emscripten_runtime.ENVIRONMENT_IS_NODE) worker.on("message", handler);
|
|
788
|
+
else worker.addEventListener("message", handler, false);
|
|
789
|
+
return true;
|
|
790
|
+
},
|
|
791
|
+
/**
|
|
792
|
+
* When another thread grows the shared WebAssembly.Memory, this agent's
|
|
793
|
+
* cached `wasmMemory.buffer` may still have the old shorter length
|
|
794
|
+
* (V8 refreshes it lazily). If a pointer derived from shared memory lies
|
|
795
|
+
* beyond the cached length, `wasmMemory.grow(0)` forces the agent to
|
|
796
|
+
* observe the current memory size and refreshes the buffer.
|
|
797
|
+
*/
|
|
798
|
+
ensureBufferFor(end) {
|
|
799
|
+
let buffer = emscripten_runtime.wasmMemory.buffer;
|
|
800
|
+
if (end > buffer.byteLength) {
|
|
801
|
+
emscripten_runtime.wasmMemory.grow(0);
|
|
802
|
+
buffer = emscripten_runtime.wasmMemory.buffer;
|
|
803
|
+
}
|
|
804
|
+
return buffer;
|
|
805
|
+
},
|
|
806
|
+
initQueue(func) {
|
|
807
|
+
const size = 8;
|
|
808
|
+
let queue = emscripten_runtime._malloc(size);
|
|
809
|
+
if (!queue) return false;
|
|
810
|
+
queue >>>= 0;
|
|
811
|
+
new Uint8Array(emnapiTSFN.ensureBufferFor(queue + size), queue, size).fill(0);
|
|
812
|
+
emnapiTSFN.storeSizeTypeValue(func + emnapiTSFN.offset.queue, queue, false);
|
|
813
|
+
return true;
|
|
814
|
+
},
|
|
815
|
+
destroyQueue(func) {
|
|
816
|
+
const queue = emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue, false);
|
|
817
|
+
if (queue) {
|
|
818
|
+
let node = emnapiTSFN.loadSizeTypeValue(queue, false);
|
|
819
|
+
while (node !== 0) {
|
|
820
|
+
const next = emnapiTSFN.loadSizeTypeValue(node + 4, false);
|
|
821
|
+
emscripten_runtime._free(node);
|
|
822
|
+
node = next;
|
|
823
|
+
}
|
|
824
|
+
emscripten_runtime._free(queue);
|
|
825
|
+
}
|
|
826
|
+
},
|
|
827
|
+
pushQueue(func, data) {
|
|
828
|
+
const queue = emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue, false);
|
|
829
|
+
const head = emnapiTSFN.loadSizeTypeValue(queue, false);
|
|
830
|
+
const tail = emnapiTSFN.loadSizeTypeValue(queue + 4, false);
|
|
831
|
+
let node = emscripten_runtime._malloc(8);
|
|
832
|
+
if (!node) throw new Error("OOM");
|
|
833
|
+
node >>>= 0;
|
|
834
|
+
emnapiTSFN.storeSizeTypeValue(node, data, false);
|
|
835
|
+
emnapiTSFN.storeSizeTypeValue(node + 4, 0, false);
|
|
836
|
+
if (head === 0 && tail === 0) {
|
|
837
|
+
emnapiTSFN.storeSizeTypeValue(queue, node, false);
|
|
838
|
+
emnapiTSFN.storeSizeTypeValue(queue + 4, node, false);
|
|
839
|
+
} else {
|
|
840
|
+
emnapiTSFN.storeSizeTypeValue(tail + 4, node, false);
|
|
841
|
+
emnapiTSFN.storeSizeTypeValue(queue + 4, node, false);
|
|
842
|
+
}
|
|
843
|
+
emnapiTSFN.addQueueSize(func);
|
|
844
|
+
},
|
|
845
|
+
shiftQueue(func) {
|
|
846
|
+
const queue = emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue, false);
|
|
847
|
+
const head = emnapiTSFN.loadSizeTypeValue(queue, false);
|
|
848
|
+
if (head === 0) return 0;
|
|
849
|
+
const node = head;
|
|
850
|
+
const next = emnapiTSFN.loadSizeTypeValue(head + 4, false);
|
|
851
|
+
emnapiTSFN.storeSizeTypeValue(queue, next, false);
|
|
852
|
+
if (next === 0) emnapiTSFN.storeSizeTypeValue(queue + 4, 0, false);
|
|
853
|
+
emnapiTSFN.storeSizeTypeValue(node + 4, 0, false);
|
|
854
|
+
const value = emnapiTSFN.loadSizeTypeValue(node, false);
|
|
855
|
+
emscripten_runtime._free(node);
|
|
856
|
+
emnapiTSFN.subQueueSize(func);
|
|
857
|
+
return value;
|
|
858
|
+
},
|
|
859
|
+
push(func, data, mode) {
|
|
860
|
+
const mutex = emnapiTSFN.getMutex(func);
|
|
861
|
+
const cond = emnapiTSFN.getCond(func);
|
|
862
|
+
const waitCondition = () => {
|
|
863
|
+
const queueSize = emnapiTSFN.getQueueSize(func);
|
|
864
|
+
const maxSize = emnapiTSFN.getMaxQueueSize(func);
|
|
865
|
+
return queueSize >= maxSize && maxSize > 0 && emnapiTSFN.getState(func) === 0;
|
|
866
|
+
};
|
|
867
|
+
const isBrowserMain = typeof window !== "undefined" && typeof document !== "undefined" && !emscripten_runtime.ENVIRONMENT_IS_NODE;
|
|
868
|
+
let shouldDelete = false;
|
|
869
|
+
const ret = mutex.execute(() => {
|
|
870
|
+
while (waitCondition()) {
|
|
871
|
+
if (mode === 0) return 15;
|
|
872
|
+
/**
|
|
873
|
+
* Browser JS main thread can not use `Atomics.wait`
|
|
874
|
+
*
|
|
875
|
+
* Related:
|
|
876
|
+
* https://github.com/nodejs/node/pull/32689
|
|
877
|
+
* https://github.com/nodejs/node/pull/33453
|
|
878
|
+
*/
|
|
879
|
+
if (isBrowserMain) return 21;
|
|
880
|
+
cond.wait();
|
|
881
|
+
}
|
|
882
|
+
if (emnapiTSFN.getState(func) === 0) {
|
|
883
|
+
emnapiTSFN.pushQueue(func, data);
|
|
884
|
+
emnapiTSFN.send(func);
|
|
885
|
+
return 0;
|
|
886
|
+
}
|
|
887
|
+
if (emnapiTSFN.getThreadCount(func) === 0) return 1;
|
|
888
|
+
emnapiTSFN.subThreadCount(func);
|
|
889
|
+
if (!(emnapiTSFN.getState(func) === 2 && emnapiTSFN.getThreadCount(func) === 0)) return 16;
|
|
890
|
+
shouldDelete = true;
|
|
891
|
+
return 16;
|
|
892
|
+
});
|
|
893
|
+
if (shouldDelete) emnapiTSFN.destroy(func);
|
|
894
|
+
return ret;
|
|
895
|
+
},
|
|
896
|
+
getMutex(func) {
|
|
897
|
+
const index = func + emnapiTSFN.offset.mutex;
|
|
898
|
+
const mutex = {
|
|
899
|
+
lock() {
|
|
900
|
+
const isBrowserMain = typeof window !== "undefined" && typeof document !== "undefined" && !emscripten_runtime.ENVIRONMENT_IS_NODE;
|
|
901
|
+
const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
|
|
902
|
+
if (isBrowserMain) {
|
|
903
|
+
while (true) if (Atomics.compareExchange(i32a, 0, 0, 10) === 0) return;
|
|
904
|
+
} else while (true) {
|
|
905
|
+
if (Atomics.compareExchange(i32a, 0, 0, 10) === 0) return;
|
|
906
|
+
Atomics.wait(i32a, 0, 10);
|
|
907
|
+
}
|
|
908
|
+
},
|
|
909
|
+
unlock() {
|
|
910
|
+
const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
|
|
911
|
+
if (Atomics.compareExchange(i32a, 0, 10, 0) !== 10) throw new Error("Tried to unlock while not holding the mutex");
|
|
912
|
+
Atomics.notify(i32a, 0, 1);
|
|
913
|
+
},
|
|
914
|
+
execute(fn) {
|
|
915
|
+
mutex.lock();
|
|
916
|
+
try {
|
|
917
|
+
return fn();
|
|
918
|
+
} finally {
|
|
919
|
+
mutex.unlock();
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
return mutex;
|
|
924
|
+
},
|
|
925
|
+
getCond(func) {
|
|
926
|
+
const index = func + emnapiTSFN.offset.cond;
|
|
927
|
+
const mutex = emnapiTSFN.getMutex(func);
|
|
928
|
+
return {
|
|
929
|
+
wait() {
|
|
930
|
+
const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
|
|
931
|
+
const value = Atomics.load(i32a, 0);
|
|
932
|
+
mutex.unlock();
|
|
933
|
+
Atomics.wait(i32a, 0, value);
|
|
934
|
+
mutex.lock();
|
|
935
|
+
},
|
|
936
|
+
signal() {
|
|
937
|
+
const i32a = new Int32Array(emnapiTSFN.ensureBufferFor(index + 4), index, 1);
|
|
938
|
+
Atomics.add(i32a, 0, 1);
|
|
939
|
+
Atomics.notify(i32a, 0, 1);
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
},
|
|
943
|
+
getQueueSize(func) {
|
|
944
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.queue_size, true);
|
|
945
|
+
},
|
|
946
|
+
addQueueSize(func) {
|
|
947
|
+
const offset = emnapiTSFN.offset.queue_size;
|
|
948
|
+
let arr, index;
|
|
949
|
+
arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
|
|
950
|
+
index = func + offset >>> 2;
|
|
951
|
+
Atomics.add(arr, index, 1);
|
|
952
|
+
},
|
|
953
|
+
subQueueSize(func) {
|
|
954
|
+
const offset = emnapiTSFN.offset.queue_size;
|
|
955
|
+
let arr, index;
|
|
956
|
+
arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
|
|
957
|
+
index = func + offset >>> 2;
|
|
958
|
+
Atomics.sub(arr, index, 1);
|
|
959
|
+
},
|
|
960
|
+
getThreadCount(func) {
|
|
961
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.thread_count, true);
|
|
962
|
+
},
|
|
963
|
+
addThreadCount(func) {
|
|
964
|
+
const offset = emnapiTSFN.offset.thread_count;
|
|
965
|
+
let arr, index;
|
|
966
|
+
arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
|
|
967
|
+
index = func + offset >>> 2;
|
|
968
|
+
Atomics.add(arr, index, 1);
|
|
969
|
+
},
|
|
970
|
+
subThreadCount(func) {
|
|
971
|
+
const offset = emnapiTSFN.offset.thread_count;
|
|
972
|
+
let arr, index;
|
|
973
|
+
arr = new Uint32Array(emnapiTSFN.ensureBufferFor(func + offset + 4));
|
|
974
|
+
index = func + offset >>> 2;
|
|
975
|
+
Atomics.sub(arr, index, 1);
|
|
976
|
+
},
|
|
977
|
+
getState(func) {
|
|
978
|
+
return Atomics.load(new Int32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.state + 4)), func + emnapiTSFN.offset.state >>> 2);
|
|
979
|
+
},
|
|
980
|
+
setState(func, value) {
|
|
981
|
+
Atomics.store(new Int32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.state + 4)), func + emnapiTSFN.offset.state >>> 2, value);
|
|
982
|
+
},
|
|
983
|
+
getHandlesClosing(func) {
|
|
984
|
+
return Atomics.load(new Int8Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.handles_closing + 1)), func + emnapiTSFN.offset.handles_closing);
|
|
985
|
+
},
|
|
986
|
+
setHandlesClosing(func, value) {
|
|
987
|
+
Atomics.store(new Int8Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.handles_closing + 1)), func + emnapiTSFN.offset.handles_closing, value);
|
|
988
|
+
},
|
|
989
|
+
getDispatchState(func) {
|
|
990
|
+
return Atomics.load(new Uint32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.dispatch_state + 4)), func + emnapiTSFN.offset.dispatch_state >>> 2);
|
|
991
|
+
},
|
|
992
|
+
getContext(func) {
|
|
993
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.context, false);
|
|
994
|
+
},
|
|
995
|
+
getMaxQueueSize(func) {
|
|
996
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.max_queue_size, true);
|
|
997
|
+
},
|
|
998
|
+
getEnv(func) {
|
|
999
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.env, false);
|
|
1000
|
+
},
|
|
1001
|
+
getCallJSCb(func) {
|
|
1002
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.call_js_cb, false);
|
|
1003
|
+
},
|
|
1004
|
+
getRef(func) {
|
|
1005
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.ref, false);
|
|
1006
|
+
},
|
|
1007
|
+
getResource(func) {
|
|
1008
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.resource, false);
|
|
1009
|
+
},
|
|
1010
|
+
getFinalizeCb(func) {
|
|
1011
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.finalize_cb, false);
|
|
1012
|
+
},
|
|
1013
|
+
getFinalizeData(func) {
|
|
1014
|
+
return emnapiTSFN.loadSizeTypeValue(func + emnapiTSFN.offset.finalize_data, false);
|
|
1015
|
+
},
|
|
1016
|
+
loadSizeTypeValue(offset, unsigned) {
|
|
1017
|
+
let ret;
|
|
1018
|
+
let arr;
|
|
1019
|
+
if (unsigned) {
|
|
1020
|
+
arr = new Uint32Array(emnapiTSFN.ensureBufferFor(offset + 4));
|
|
1021
|
+
ret = Atomics.load(arr, offset >>> 2);
|
|
1022
|
+
return ret;
|
|
1023
|
+
} else {
|
|
1024
|
+
arr = new Int32Array(emnapiTSFN.ensureBufferFor(offset + 4));
|
|
1025
|
+
ret = Atomics.load(arr, offset >>> 2);
|
|
1026
|
+
return ret;
|
|
1027
|
+
}
|
|
1028
|
+
},
|
|
1029
|
+
storeSizeTypeValue(offset, value, unsigned) {
|
|
1030
|
+
let arr;
|
|
1031
|
+
if (unsigned) {
|
|
1032
|
+
arr = new Uint32Array(emnapiTSFN.ensureBufferFor(offset + 4));
|
|
1033
|
+
Atomics.store(arr, offset >>> 2, value);
|
|
1034
|
+
return;
|
|
1035
|
+
} else {
|
|
1036
|
+
arr = new Int32Array(emnapiTSFN.ensureBufferFor(offset + 4));
|
|
1037
|
+
Atomics.store(arr, offset >>> 2, value >>> 0);
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
},
|
|
1041
|
+
releaseResources(func) {
|
|
1042
|
+
if (emnapiTSFN.getState(func) !== 2) {
|
|
1043
|
+
emnapiTSFN.setState(func, 2);
|
|
1044
|
+
emnapiTSFN.getEnv(func);
|
|
1045
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
1046
|
+
const ref = emnapiTSFN.getRef(func);
|
|
1047
|
+
if (ref) emnapiCtx.getRef(ref).dispose();
|
|
1048
|
+
const resource = emnapiTSFN.getResource(func);
|
|
1049
|
+
emnapiCtx.getRef(resource).dispose();
|
|
1050
|
+
emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.is_some + 1);
|
|
1051
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
1052
|
+
GET_HEAP_DATA_VIEW().setInt8(func + emnapiTSFN.offset.is_some, 0, true);
|
|
1053
|
+
emnapiCtx.removeCleanupHook(envObject, emnapiTSFN.cleanup, func);
|
|
1054
|
+
envObject.unref();
|
|
1055
|
+
const asyncRefAddress = func + emnapiTSFN.offset.async_ref;
|
|
1056
|
+
const asyncRefOffset = asyncRefAddress >>> 2;
|
|
1057
|
+
const arr = new Uint32Array(emnapiTSFN.ensureBufferFor(asyncRefAddress + 4));
|
|
1058
|
+
if (Atomics.load(arr, asyncRefOffset) > 0) {
|
|
1059
|
+
Atomics.store(arr, asyncRefOffset, 0);
|
|
1060
|
+
__emnapi_runtime_keepalive_pop();
|
|
1061
|
+
emnapiCtx.decreaseWaitingRequestCounter();
|
|
1062
|
+
}
|
|
1063
|
+
if (emnapiNodeBinding) {
|
|
1064
|
+
const view = new DataView(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.trigger_async_id + 8));
|
|
1065
|
+
const asyncId = view.getFloat64(func + emnapiTSFN.offset.async_id, true);
|
|
1066
|
+
const triggerAsyncId = view.getFloat64(func + emnapiTSFN.offset.trigger_async_id, true);
|
|
1067
|
+
__emnapi_node_emit_async_destroy(asyncId, triggerAsyncId);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
},
|
|
1071
|
+
destroy(func) {
|
|
1072
|
+
emnapiTSFN._liveSet.delete(func);
|
|
1073
|
+
emnapiTSFN.destroyQueue(func);
|
|
1074
|
+
emnapiTSFN.releaseResources(func);
|
|
1075
|
+
emscripten_runtime._free(func);
|
|
1076
|
+
},
|
|
1077
|
+
emptyQueue(func) {
|
|
1078
|
+
const drainQueue = [];
|
|
1079
|
+
emnapiTSFN.getMutex(func).execute(() => {
|
|
1080
|
+
while (emnapiTSFN.getQueueSize(func) > 0) drainQueue.push(emnapiTSFN.shiftQueue(func));
|
|
1081
|
+
});
|
|
1082
|
+
const callJsCb = emnapiTSFN.getCallJSCb(func);
|
|
1083
|
+
const context = emnapiTSFN.getContext(func);
|
|
1084
|
+
let data;
|
|
1085
|
+
for (let i = 0; i < drainQueue.length; i++) {
|
|
1086
|
+
data = drainQueue[i];
|
|
1087
|
+
if (callJsCb) emnapiPluginCtx.wasmTable.get(callJsCb)(0, 0, context, data);
|
|
1088
|
+
}
|
|
1089
|
+
},
|
|
1090
|
+
maybeDelete(func) {
|
|
1091
|
+
let shouldDelete = false;
|
|
1092
|
+
emnapiTSFN.getMutex(func).execute(() => {
|
|
1093
|
+
if (emnapiTSFN.getThreadCount(func) > 0) emnapiTSFN.releaseResources(func);
|
|
1094
|
+
else shouldDelete = true;
|
|
1095
|
+
});
|
|
1096
|
+
if (shouldDelete) emnapiTSFN.destroy(func);
|
|
1097
|
+
},
|
|
1098
|
+
finalize(func) {
|
|
1099
|
+
emnapiTSFN.getEnv(func);
|
|
1100
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
1101
|
+
emnapiCtx.openScope(envObject);
|
|
1102
|
+
const finalize = emnapiTSFN.getFinalizeCb(func);
|
|
1103
|
+
const data = emnapiTSFN.getFinalizeData(func);
|
|
1104
|
+
const context = emnapiTSFN.getContext(func);
|
|
1105
|
+
const f = () => {
|
|
1106
|
+
envObject.callFinalizerInternal(0, finalize, data, context);
|
|
1107
|
+
};
|
|
1108
|
+
try {
|
|
1109
|
+
emnapiTSFN.emptyQueue(func);
|
|
1110
|
+
if (finalize) {
|
|
1111
|
+
if (emnapiNodeBinding) {
|
|
1112
|
+
const resource = emnapiTSFN.getResource(func);
|
|
1113
|
+
const resource_value = emnapiCtx.getRef(resource).get();
|
|
1114
|
+
const resourceObject = emnapiCtx.jsValueFromNapiValue(resource_value);
|
|
1115
|
+
const view = new DataView(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.trigger_async_id + 8));
|
|
1116
|
+
const asyncId = view.getFloat64(func + emnapiTSFN.offset.async_id, true);
|
|
1117
|
+
const triggerAsyncId = view.getFloat64(func + emnapiTSFN.offset.trigger_async_id, true);
|
|
1118
|
+
emnapiNodeBinding.node.makeCallback(resourceObject, f, [], {
|
|
1119
|
+
asyncId,
|
|
1120
|
+
triggerAsyncId
|
|
1121
|
+
});
|
|
1122
|
+
} else f();
|
|
1123
|
+
}
|
|
1124
|
+
emnapiTSFN.maybeDelete(func);
|
|
1125
|
+
} finally {
|
|
1126
|
+
emnapiCtx.closeScope(envObject);
|
|
1127
|
+
}
|
|
1128
|
+
},
|
|
1129
|
+
cleanup(func) {
|
|
1130
|
+
emnapiTSFN.closeHandlesAndMaybeDelete(func, 1);
|
|
1131
|
+
},
|
|
1132
|
+
closeHandlesAndMaybeDelete(func, set_closing) {
|
|
1133
|
+
emnapiTSFN.getEnv(func);
|
|
1134
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
1135
|
+
emnapiCtx.openScope(envObject);
|
|
1136
|
+
try {
|
|
1137
|
+
if (set_closing) emnapiTSFN.getMutex(func).execute(() => {
|
|
1138
|
+
emnapiTSFN.setState(func, 1);
|
|
1139
|
+
if (emnapiTSFN.getMaxQueueSize(func) > 0) emnapiTSFN.getCond(func).signal();
|
|
1140
|
+
});
|
|
1141
|
+
if (emnapiTSFN.getHandlesClosing(func)) return;
|
|
1142
|
+
emnapiTSFN.setHandlesClosing(func, 1);
|
|
1143
|
+
Atomics.store(new Int32Array(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.async_pending + 4)), func + emnapiTSFN.offset.async_pending >>> 2, 1);
|
|
1144
|
+
emnapiCtx.features.setImmediate(() => {
|
|
1145
|
+
emnapiTSFN.finalize(func);
|
|
1146
|
+
});
|
|
1147
|
+
} finally {
|
|
1148
|
+
emnapiCtx.closeScope(envObject);
|
|
1149
|
+
}
|
|
1150
|
+
},
|
|
1151
|
+
dispatchOne(func) {
|
|
1152
|
+
let data = 0;
|
|
1153
|
+
let popped_value = false;
|
|
1154
|
+
let has_more = false;
|
|
1155
|
+
const mutex = emnapiTSFN.getMutex(func);
|
|
1156
|
+
const cond = emnapiTSFN.getCond(func);
|
|
1157
|
+
mutex.execute(() => {
|
|
1158
|
+
if (emnapiTSFN.getState(func) === 0) {
|
|
1159
|
+
let size = emnapiTSFN.getQueueSize(func);
|
|
1160
|
+
if (size > 0) {
|
|
1161
|
+
data = emnapiTSFN.shiftQueue(func);
|
|
1162
|
+
popped_value = true;
|
|
1163
|
+
const maxQueueSize = emnapiTSFN.getMaxQueueSize(func);
|
|
1164
|
+
if (size === maxQueueSize && maxQueueSize > 0) cond.signal();
|
|
1165
|
+
size--;
|
|
1166
|
+
}
|
|
1167
|
+
if (size === 0) {
|
|
1168
|
+
if (emnapiTSFN.getThreadCount(func) === 0) {
|
|
1169
|
+
emnapiTSFN.setState(func, 1);
|
|
1170
|
+
if (emnapiTSFN.getMaxQueueSize(func) > 0) cond.signal();
|
|
1171
|
+
emnapiTSFN.closeHandlesAndMaybeDelete(func, 0);
|
|
1172
|
+
}
|
|
1173
|
+
} else has_more = true;
|
|
1174
|
+
} else emnapiTSFN.closeHandlesAndMaybeDelete(func, 0);
|
|
1175
|
+
});
|
|
1176
|
+
if (popped_value) {
|
|
1177
|
+
const env = emnapiTSFN.getEnv(func);
|
|
1178
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
1179
|
+
emnapiCtx.openScope(envObject);
|
|
1180
|
+
const f = () => {
|
|
1181
|
+
envObject.callbackIntoModule(false, () => {
|
|
1182
|
+
const callJsCb = emnapiTSFN.getCallJSCb(func);
|
|
1183
|
+
const ref = emnapiTSFN.getRef(func);
|
|
1184
|
+
const js_callback = ref ? emnapiCtx.getRef(ref).get() : 0;
|
|
1185
|
+
if (callJsCb) {
|
|
1186
|
+
const context = emnapiTSFN.getContext(func);
|
|
1187
|
+
emnapiPluginCtx.wasmTable.get(callJsCb)(env, js_callback, context, data);
|
|
1188
|
+
} else {
|
|
1189
|
+
const jsCallback = js_callback ? emnapiCtx.jsValueFromNapiValue(js_callback) : null;
|
|
1190
|
+
if (typeof jsCallback === "function") jsCallback();
|
|
1191
|
+
}
|
|
1192
|
+
});
|
|
1193
|
+
};
|
|
1194
|
+
try {
|
|
1195
|
+
if (emnapiNodeBinding) {
|
|
1196
|
+
const resource = emnapiTSFN.getResource(func);
|
|
1197
|
+
const resource_value = emnapiCtx.getRef(resource).get();
|
|
1198
|
+
const resourceObject = emnapiCtx.jsValueFromNapiValue(resource_value);
|
|
1199
|
+
const view = new DataView(emnapiTSFN.ensureBufferFor(func + emnapiTSFN.offset.trigger_async_id + 8));
|
|
1200
|
+
emnapiNodeBinding.node.makeCallback(resourceObject, f, [], {
|
|
1201
|
+
asyncId: view.getFloat64(func + emnapiTSFN.offset.async_id, true),
|
|
1202
|
+
triggerAsyncId: view.getFloat64(func + emnapiTSFN.offset.trigger_async_id, true)
|
|
1203
|
+
});
|
|
1204
|
+
} else f();
|
|
1205
|
+
} finally {
|
|
1206
|
+
emnapiCtx.closeScope(envObject);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
return has_more;
|
|
1210
|
+
},
|
|
1211
|
+
dispatch(func) {
|
|
1212
|
+
let has_more = true;
|
|
1213
|
+
let iterations_left = 1e3;
|
|
1214
|
+
const dispatchStateAddress = func + emnapiTSFN.offset.dispatch_state;
|
|
1215
|
+
const index = dispatchStateAddress >>> 2;
|
|
1216
|
+
while (has_more && --iterations_left !== 0) {
|
|
1217
|
+
Atomics.store(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), index, 1);
|
|
1218
|
+
try {
|
|
1219
|
+
has_more = emnapiTSFN.dispatchOne(func);
|
|
1220
|
+
} catch (err) {
|
|
1221
|
+
if (emnapiTSFN._liveSet.has(func)) {
|
|
1222
|
+
Atomics.exchange(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), index, 0);
|
|
1223
|
+
emnapiTSFN.send(func);
|
|
1224
|
+
}
|
|
1225
|
+
throw err;
|
|
1226
|
+
}
|
|
1227
|
+
if (Atomics.exchange(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), index, 0) !== 1) has_more = true;
|
|
1228
|
+
}
|
|
1229
|
+
if (has_more) emnapiTSFN.send(func);
|
|
1230
|
+
},
|
|
1231
|
+
enqueue(func) {
|
|
1232
|
+
const pending = func + emnapiTSFN.offset.async_pending;
|
|
1233
|
+
const scheduled = func + emnapiTSFN.offset.async_u_fd;
|
|
1234
|
+
const end = Math.max(pending, scheduled) + 4;
|
|
1235
|
+
const state = () => new Int32Array(emnapiTSFN.ensureBufferFor(end));
|
|
1236
|
+
if (Atomics.exchange(state(), scheduled >>> 2, 1) !== 0) return;
|
|
1237
|
+
emnapiCtx.features.setImmediate(() => {
|
|
1238
|
+
if (!emnapiTSFN._liveSet.has(func)) return;
|
|
1239
|
+
if (Atomics.load(state(), pending >>> 2) === 0) {
|
|
1240
|
+
Atomics.store(state(), scheduled >>> 2, 0);
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
emnapiCtx.features.setImmediate(() => {
|
|
1244
|
+
if (!emnapiTSFN._liveSet.has(func)) return;
|
|
1245
|
+
try {
|
|
1246
|
+
if (Atomics.exchange(state(), pending >>> 2, 0) === 0) return;
|
|
1247
|
+
emnapiTSFN.dispatch(func);
|
|
1248
|
+
} finally {
|
|
1249
|
+
if (emnapiTSFN._liveSet.has(func)) {
|
|
1250
|
+
Atomics.store(state(), scheduled >>> 2, 0);
|
|
1251
|
+
if (Atomics.load(state(), pending >>> 2) !== 0) emnapiTSFN.enqueue(func);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
});
|
|
1255
|
+
});
|
|
1256
|
+
},
|
|
1257
|
+
send(func) {
|
|
1258
|
+
const dispatchStateAddress = func + emnapiTSFN.offset.dispatch_state;
|
|
1259
|
+
if ((Atomics.or(new Uint32Array(emnapiTSFN.ensureBufferFor(dispatchStateAddress + 4)), dispatchStateAddress >>> 2, 2) & 1) === 1) return;
|
|
1260
|
+
const pendng = func + emnapiTSFN.offset.async_pending;
|
|
1261
|
+
if (Atomics.load(new Int32Array(emnapiTSFN.ensureBufferFor(pendng + 4)), pendng >>> 2) !== 0) return;
|
|
1262
|
+
if (Atomics.exchange(new Int32Array(emnapiTSFN.ensureBufferFor(pendng + 4)), pendng >>> 2, 1) === 0) {
|
|
1263
|
+
if (typeof emscripten_runtime.ENVIRONMENT_IS_PTHREAD !== "undefined" && emscripten_runtime.ENVIRONMENT_IS_PTHREAD) postMessage({ __emnapi__: {
|
|
1264
|
+
type: "tsfn-send",
|
|
1265
|
+
payload: { tsfn: func }
|
|
1266
|
+
} });
|
|
1267
|
+
else emnapiTSFN.enqueue(func);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
emnapiTSFN.init();
|
|
1272
|
+
emnapiPluginCtx.emnapiTSFN = emnapiTSFN;
|
|
1273
|
+
/**
|
|
1274
|
+
* @__deps _emnapi_node_emit_async_init
|
|
1275
|
+
* @__deps _emnapi_runtime_keepalive_push
|
|
1276
|
+
* @__sig ippppppppppp
|
|
1277
|
+
*/
|
|
1278
|
+
function napi_create_threadsafe_function(env, func, async_resource, async_resource_name, max_queue_size, initial_thread_count, thread_finalize_data, thread_finalize_cb, context, call_js_cb, result) {
|
|
1279
|
+
if (!env) return 1;
|
|
1280
|
+
const envObject = emnapi_shared.emnapiEnv;
|
|
1281
|
+
envObject.checkGCAccess();
|
|
1282
|
+
if (!async_resource_name) return envObject.setLastError(1);
|
|
1283
|
+
max_queue_size >>>= 0;
|
|
1284
|
+
initial_thread_count >>>= 0;
|
|
1285
|
+
env >>>= 0;
|
|
1286
|
+
thread_finalize_data >>>= 0;
|
|
1287
|
+
thread_finalize_cb >>>= 0;
|
|
1288
|
+
context >>>= 0;
|
|
1289
|
+
call_js_cb >>>= 0;
|
|
1290
|
+
max_queue_size = max_queue_size >>> 0;
|
|
1291
|
+
initial_thread_count = initial_thread_count >>> 0;
|
|
1292
|
+
if (initial_thread_count === 0) return envObject.setLastError(1);
|
|
1293
|
+
if (!result) return envObject.setLastError(1);
|
|
1294
|
+
let ref = 0;
|
|
1295
|
+
func >>>= 0;
|
|
1296
|
+
if (!func) {
|
|
1297
|
+
if (!call_js_cb) return envObject.setLastError(1);
|
|
1298
|
+
} else {
|
|
1299
|
+
if (typeof emnapiCtx.jsValueFromNapiValue(func) !== "function") return envObject.setLastError(1);
|
|
1300
|
+
ref = emnapiCtx.createReference(envObject, func, 1, 1).id;
|
|
1301
|
+
}
|
|
1302
|
+
let asyncResourceObject;
|
|
1303
|
+
if (async_resource) {
|
|
1304
|
+
asyncResourceObject = emnapiCtx.jsValueFromNapiValue(async_resource);
|
|
1305
|
+
if (asyncResourceObject == null) return envObject.setLastError(2);
|
|
1306
|
+
asyncResourceObject = Object(asyncResourceObject);
|
|
1307
|
+
} else asyncResourceObject = {};
|
|
1308
|
+
const resource = emnapiCtx.napiValueFromJsValue(asyncResourceObject);
|
|
1309
|
+
let asyncResourceName = emnapiCtx.jsValueFromNapiValue(async_resource_name);
|
|
1310
|
+
if (typeof asyncResourceName === "symbol") return envObject.setLastError(3);
|
|
1311
|
+
asyncResourceName = String(asyncResourceName);
|
|
1312
|
+
const resource_name = emnapiCtx.napiValueFromJsValue(asyncResourceName);
|
|
1313
|
+
const sizeofTSFN = emnapiTSFN.offset.__size__;
|
|
1314
|
+
let tsfn = emscripten_runtime._malloc(sizeofTSFN);
|
|
1315
|
+
if (!tsfn) return envObject.setLastError(9);
|
|
1316
|
+
tsfn >>>= 0;
|
|
1317
|
+
new Uint8Array(emnapiTSFN.ensureBufferFor(tsfn + sizeofTSFN)).subarray(tsfn, tsfn + sizeofTSFN).fill(0);
|
|
1318
|
+
const resourceRef = emnapiCtx.createReference(envObject, resource, 1, 1);
|
|
1319
|
+
const resource_ = resourceRef.id;
|
|
1320
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
1321
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.resource, resource_, true);
|
|
1322
|
+
if (!emnapiTSFN.initQueue(tsfn)) {
|
|
1323
|
+
emscripten_runtime._free(tsfn);
|
|
1324
|
+
resourceRef.dispose();
|
|
1325
|
+
return envObject.setLastError(9);
|
|
1326
|
+
}
|
|
1327
|
+
__emnapi_node_emit_async_init(resource, resource_name, -1, tsfn + emnapiTSFN.offset.async_id);
|
|
1328
|
+
GET_HEAP_DATA_VIEW().setInt8(tsfn + emnapiTSFN.offset.is_some, 1, true);
|
|
1329
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.thread_count, initial_thread_count, true);
|
|
1330
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.context, context, true);
|
|
1331
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.max_queue_size, max_queue_size, true);
|
|
1332
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.ref, ref, true);
|
|
1333
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.env, env, true);
|
|
1334
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.finalize_data, thread_finalize_data, true);
|
|
1335
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.finalize_cb, thread_finalize_cb, true);
|
|
1336
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.call_js_cb, call_js_cb, true);
|
|
1337
|
+
emnapiCtx.addCleanupHook(envObject, emnapiTSFN.cleanup, tsfn);
|
|
1338
|
+
emnapiTSFN._liveSet.add(tsfn);
|
|
1339
|
+
envObject.ref();
|
|
1340
|
+
__emnapi_runtime_keepalive_push();
|
|
1341
|
+
emnapiCtx.increaseWaitingRequestCounter();
|
|
1342
|
+
GET_HEAP_DATA_VIEW().setUint32(tsfn + emnapiTSFN.offset.async_ref, 1, true);
|
|
1343
|
+
result >>>= 0;
|
|
1344
|
+
GET_HEAP_DATA_VIEW().setUint32(result, tsfn, true);
|
|
1345
|
+
return envObject.clearLastError();
|
|
1346
|
+
}
|
|
1347
|
+
/** @__sig ipp */
|
|
1348
|
+
function napi_get_threadsafe_function_context(func, result) {
|
|
1349
|
+
if (!func || !result) {
|
|
1350
|
+
emscripten_runtime.abort();
|
|
1351
|
+
return 1;
|
|
1352
|
+
}
|
|
1353
|
+
func >>>= 0;
|
|
1354
|
+
result >>>= 0;
|
|
1355
|
+
const context = emnapiTSFN.getContext(func);
|
|
1356
|
+
var HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer), GET_HEAP_DATA_VIEW = () => HEAP_DATA_VIEW.buffer === emnapiPluginCtx.wasmMemory.buffer ? HEAP_DATA_VIEW : HEAP_DATA_VIEW = new DataView(emnapiPluginCtx.wasmMemory.buffer);
|
|
1357
|
+
GET_HEAP_DATA_VIEW().setUint32(result, context, true);
|
|
1358
|
+
return 0;
|
|
1359
|
+
}
|
|
1360
|
+
/** @__sig ippi */
|
|
1361
|
+
function napi_call_threadsafe_function(func, data, mode) {
|
|
1362
|
+
if (!func) {
|
|
1363
|
+
emscripten_runtime.abort();
|
|
1364
|
+
return 1;
|
|
1365
|
+
}
|
|
1366
|
+
func >>>= 0;
|
|
1367
|
+
data >>>= 0;
|
|
1368
|
+
return emnapiTSFN.push(func, data, mode);
|
|
1369
|
+
}
|
|
1370
|
+
/** @__sig ip */
|
|
1371
|
+
function napi_acquire_threadsafe_function(func) {
|
|
1372
|
+
if (!func) {
|
|
1373
|
+
emscripten_runtime.abort();
|
|
1374
|
+
return 1;
|
|
1375
|
+
}
|
|
1376
|
+
func >>>= 0;
|
|
1377
|
+
return emnapiTSFN.getMutex(func).execute(() => {
|
|
1378
|
+
if (emnapiTSFN.getState(func) === 0) {
|
|
1379
|
+
emnapiTSFN.addThreadCount(func);
|
|
1380
|
+
return 0;
|
|
1381
|
+
}
|
|
1382
|
+
return 16;
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
/** @__sig ipi */
|
|
1386
|
+
function napi_release_threadsafe_function(func, mode) {
|
|
1387
|
+
if (!func) {
|
|
1388
|
+
emscripten_runtime.abort();
|
|
1389
|
+
return 1;
|
|
1390
|
+
}
|
|
1391
|
+
func >>>= 0;
|
|
1392
|
+
const mutex = emnapiTSFN.getMutex(func);
|
|
1393
|
+
const cond = emnapiTSFN.getCond(func);
|
|
1394
|
+
let shouldDelete = false;
|
|
1395
|
+
const ret = mutex.execute(() => {
|
|
1396
|
+
if (emnapiTSFN.getThreadCount(func) === 0) return 1;
|
|
1397
|
+
emnapiTSFN.subThreadCount(func);
|
|
1398
|
+
if (emnapiTSFN.getThreadCount(func) === 0 || mode === 1) {
|
|
1399
|
+
if (emnapiTSFN.getState(func) === 0) {
|
|
1400
|
+
if (mode === 1) emnapiTSFN.setState(func, 1);
|
|
1401
|
+
if (emnapiTSFN.getState(func) === 1 && emnapiTSFN.getMaxQueueSize(func) > 0) cond.signal();
|
|
1402
|
+
emnapiTSFN.send(func);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
if (!(emnapiTSFN.getState(func) === 2 && emnapiTSFN.getThreadCount(func) === 0)) return 0;
|
|
1406
|
+
shouldDelete = true;
|
|
1407
|
+
return 0;
|
|
1408
|
+
});
|
|
1409
|
+
if (shouldDelete) emnapiTSFN.destroy(func);
|
|
1410
|
+
return ret;
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* @__deps _emnapi_runtime_keepalive_pop
|
|
1414
|
+
* @__sig ipp
|
|
1415
|
+
*/
|
|
1416
|
+
function napi_unref_threadsafe_function(env, func) {
|
|
1417
|
+
if (!func) {
|
|
1418
|
+
emscripten_runtime.abort();
|
|
1419
|
+
return 1;
|
|
1420
|
+
}
|
|
1421
|
+
func >>>= 0;
|
|
1422
|
+
const asyncRefAddress = func + emnapiTSFN.offset.async_ref;
|
|
1423
|
+
const asyncRefOffset = asyncRefAddress >>> 2;
|
|
1424
|
+
const arr = new Uint32Array(emnapiTSFN.ensureBufferFor(asyncRefAddress + 4));
|
|
1425
|
+
const currentValue = Atomics.load(arr, asyncRefOffset);
|
|
1426
|
+
if (currentValue > 0) {
|
|
1427
|
+
Atomics.store(arr, asyncRefOffset, currentValue - 1);
|
|
1428
|
+
if (currentValue === 1) {
|
|
1429
|
+
__emnapi_runtime_keepalive_pop();
|
|
1430
|
+
emnapiCtx.decreaseWaitingRequestCounter();
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
return 0;
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* @__deps _emnapi_runtime_keepalive_push
|
|
1437
|
+
* @__sig ipp
|
|
1438
|
+
*/
|
|
1439
|
+
function napi_ref_threadsafe_function(env, func) {
|
|
1440
|
+
if (!func) {
|
|
1441
|
+
emscripten_runtime.abort();
|
|
1442
|
+
return 1;
|
|
1443
|
+
}
|
|
1444
|
+
func >>>= 0;
|
|
1445
|
+
const asyncRefAddress = func + emnapiTSFN.offset.async_ref;
|
|
1446
|
+
const asyncRefOffset = asyncRefAddress >>> 2;
|
|
1447
|
+
const arr = new Uint32Array(emnapiTSFN.ensureBufferFor(asyncRefAddress + 4));
|
|
1448
|
+
const currentValue = Atomics.load(arr, asyncRefOffset);
|
|
1449
|
+
if (!currentValue) {
|
|
1450
|
+
__emnapi_runtime_keepalive_push();
|
|
1451
|
+
emnapiCtx.increaseWaitingRequestCounter();
|
|
1452
|
+
}
|
|
1453
|
+
Atomics.store(arr, asyncRefOffset, currentValue + 1);
|
|
1454
|
+
return 0;
|
|
1455
|
+
}
|
|
1456
|
+
exports.napi_acquire_threadsafe_function = napi_acquire_threadsafe_function;
|
|
1457
|
+
exports.napi_call_threadsafe_function = napi_call_threadsafe_function;
|
|
1458
|
+
exports.napi_create_threadsafe_function = napi_create_threadsafe_function;
|
|
1459
|
+
exports.napi_get_threadsafe_function_context = napi_get_threadsafe_function_context;
|
|
1460
|
+
exports.napi_ref_threadsafe_function = napi_ref_threadsafe_function;
|
|
1461
|
+
exports.napi_release_threadsafe_function = napi_release_threadsafe_function;
|
|
1462
|
+
exports.napi_unref_threadsafe_function = napi_unref_threadsafe_function;
|
|
1463
|
+
return exports;
|
|
1464
|
+
})({}, emnapiPluginCtx, emnapiPluginCtx);
|
|
1465
|
+
return { importObject: (original) => {
|
|
1466
|
+
Object.assign(original.napi, mod);
|
|
1467
|
+
} };
|
|
1468
|
+
}
|
|
1469
|
+
//#endregion
|
|
1470
|
+
export { asyncWork as emnapiAsyncWorkPlugin, threadsafeFunction as emnapiTSFNPlugin };
|