quickjs-emscripten-sync 1.4.0 → 1.5.0
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/README.md +28 -7
- package/dist/index.d.ts +12 -7
- package/dist/quickjs-emscripten-sync.mjs +842 -0
- package/dist/quickjs-emscripten-sync.umd.js +15 -15
- package/package.json +9 -18
- package/src/default.ts +2 -2
- package/src/index.test.ts +21 -13
- package/src/index.ts +30 -43
- package/src/marshal/custom.test.ts +50 -0
- package/src/marshal/custom.ts +36 -0
- package/src/marshal/function.test.ts +17 -37
- package/src/marshal/function.ts +8 -12
- package/src/marshal/index.test.ts +35 -39
- package/src/marshal/index.ts +6 -9
- package/src/marshal/json.test.ts +9 -15
- package/src/marshal/json.ts +1 -4
- package/src/marshal/object.test.ts +19 -51
- package/src/marshal/object.ts +3 -11
- package/src/marshal/primitive.test.ts +4 -13
- package/src/marshal/primitive.ts +1 -1
- package/src/marshal/promise.test.ts +9 -10
- package/src/marshal/promise.ts +4 -11
- package/src/marshal/properties.test.ts +7 -14
- package/src/marshal/properties.ts +6 -9
- package/src/unmarshal/custom.test.ts +50 -0
- package/src/unmarshal/custom.ts +31 -0
- package/src/unmarshal/function.test.ts +20 -44
- package/src/unmarshal/function.ts +7 -17
- package/src/unmarshal/index.test.ts +30 -23
- package/src/unmarshal/index.ts +4 -6
- package/src/unmarshal/object.test.ts +9 -17
- package/src/unmarshal/object.ts +7 -12
- package/src/unmarshal/primitive.test.ts +1 -4
- package/src/unmarshal/primitive.ts +3 -10
- package/src/unmarshal/promise.test.ts +3 -3
- package/src/unmarshal/promise.ts +3 -10
- package/src/unmarshal/properties.test.ts +2 -2
- package/src/unmarshal/properties.ts +4 -8
- package/src/util.test.ts +1 -7
- package/src/util.ts +3 -8
- package/src/vmmap.ts +13 -31
- package/src/vmutil.test.ts +15 -29
- package/src/vmutil.ts +12 -39
- package/src/wrapper.test.ts +27 -90
- package/src/wrapper.ts +43 -50
- package/dist/quickjs-emscripten-sync.es.js +0 -962
- package/src/marshal/symbol.test.ts +0 -24
- package/src/marshal/symbol.ts +0 -21
- package/src/unmarshal/symbol.test.ts +0 -25
- package/src/unmarshal/symbol.ts +0 -12
|
@@ -1,962 +0,0 @@
|
|
|
1
|
-
class VMMap {
|
|
2
|
-
constructor(ctx) {
|
|
3
|
-
this._map1 = /* @__PURE__ */ new Map();
|
|
4
|
-
this._map2 = /* @__PURE__ */ new Map();
|
|
5
|
-
this._map3 = /* @__PURE__ */ new Map();
|
|
6
|
-
this._map4 = /* @__PURE__ */ new Map();
|
|
7
|
-
this._counterMap = /* @__PURE__ */ new Map();
|
|
8
|
-
this._disposables = /* @__PURE__ */ new Set();
|
|
9
|
-
this._counter = 0;
|
|
10
|
-
this.ctx = ctx;
|
|
11
|
-
const result = ctx.unwrapResult(ctx.evalCode(`() => {
|
|
12
|
-
const mapSym = new Map();
|
|
13
|
-
let map = new WeakMap();
|
|
14
|
-
let map2 = new WeakMap();
|
|
15
|
-
const isObj = o => typeof o === "object" && o !== null || typeof o === "function";
|
|
16
|
-
return {
|
|
17
|
-
get: key => mapSym.get(key) ?? map.get(key) ?? map2.get(key) ?? -1,
|
|
18
|
-
set: (key, value, key2) => {
|
|
19
|
-
if (typeof key === "symbol") mapSym.set(key, value);
|
|
20
|
-
if (isObj(key)) map.set(key, value);
|
|
21
|
-
if (isObj(key2)) map2.set(key2, value);
|
|
22
|
-
},
|
|
23
|
-
delete: (key, key2) => {
|
|
24
|
-
mapSym.delete(key);
|
|
25
|
-
map.delete(key);
|
|
26
|
-
map2.delete(key2);
|
|
27
|
-
},
|
|
28
|
-
clear: () => {
|
|
29
|
-
mapSym.clear();
|
|
30
|
-
map = new WeakMap();
|
|
31
|
-
map2 = new WeakMap();
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}`)).consume((fn2) => this._call(fn2, void 0));
|
|
35
|
-
this._mapGet = ctx.getProp(result, "get");
|
|
36
|
-
this._mapSet = ctx.getProp(result, "set");
|
|
37
|
-
this._mapDelete = ctx.getProp(result, "delete");
|
|
38
|
-
this._mapClear = ctx.getProp(result, "clear");
|
|
39
|
-
result.dispose();
|
|
40
|
-
this._disposables.add(this._mapGet);
|
|
41
|
-
this._disposables.add(this._mapSet);
|
|
42
|
-
this._disposables.add(this._mapDelete);
|
|
43
|
-
this._disposables.add(this._mapClear);
|
|
44
|
-
}
|
|
45
|
-
set(key, handle, key2, handle2) {
|
|
46
|
-
var _a;
|
|
47
|
-
if (!handle.alive || handle2 && !handle2.alive)
|
|
48
|
-
return false;
|
|
49
|
-
const v = (_a = this.get(key)) != null ? _a : this.get(key2);
|
|
50
|
-
if (v) {
|
|
51
|
-
return v === handle || v === handle2;
|
|
52
|
-
}
|
|
53
|
-
const counter = this._counter++;
|
|
54
|
-
this._map1.set(key, counter);
|
|
55
|
-
this._map3.set(counter, handle);
|
|
56
|
-
this._counterMap.set(counter, key);
|
|
57
|
-
if (key2) {
|
|
58
|
-
this._map2.set(key2, counter);
|
|
59
|
-
if (handle2) {
|
|
60
|
-
this._map4.set(counter, handle2);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
this.ctx.newNumber(counter).consume((c) => {
|
|
64
|
-
this._call(this._mapSet, void 0, handle, c, handle2 != null ? handle2 : this.ctx.undefined);
|
|
65
|
-
});
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
merge(iteratable) {
|
|
69
|
-
if (!iteratable)
|
|
70
|
-
return;
|
|
71
|
-
for (const iter of iteratable) {
|
|
72
|
-
if (!iter)
|
|
73
|
-
continue;
|
|
74
|
-
if (iter[1]) {
|
|
75
|
-
this.set(iter[0], iter[1], iter[2], iter[3]);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
get(key) {
|
|
80
|
-
var _a;
|
|
81
|
-
const num = (_a = this._map1.get(key)) != null ? _a : this._map2.get(key);
|
|
82
|
-
const handle = typeof num === "number" ? this._map3.get(num) : void 0;
|
|
83
|
-
if (!handle)
|
|
84
|
-
return;
|
|
85
|
-
if (!handle.alive) {
|
|
86
|
-
this.delete(key);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
return handle;
|
|
90
|
-
}
|
|
91
|
-
getByHandle(handle) {
|
|
92
|
-
if (!handle.alive) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
return this._counterMap.get(this.ctx.getNumber(this._call(this._mapGet, void 0, handle)));
|
|
96
|
-
}
|
|
97
|
-
has(key) {
|
|
98
|
-
return !!this.get(key);
|
|
99
|
-
}
|
|
100
|
-
hasHandle(handle) {
|
|
101
|
-
return typeof this.getByHandle(handle) !== "undefined";
|
|
102
|
-
}
|
|
103
|
-
keys() {
|
|
104
|
-
return this._map1.keys();
|
|
105
|
-
}
|
|
106
|
-
delete(key, dispose) {
|
|
107
|
-
var _a;
|
|
108
|
-
const num = (_a = this._map1.get(key)) != null ? _a : this._map2.get(key);
|
|
109
|
-
if (typeof num === "undefined")
|
|
110
|
-
return;
|
|
111
|
-
const handle = this._map3.get(num);
|
|
112
|
-
const handle2 = this._map4.get(num);
|
|
113
|
-
this._call(this._mapDelete, void 0, ...[handle, handle2].filter((h) => !!(h == null ? void 0 : h.alive)));
|
|
114
|
-
this._map1.delete(key);
|
|
115
|
-
this._map2.delete(key);
|
|
116
|
-
this._map3.delete(num);
|
|
117
|
-
this._map4.delete(num);
|
|
118
|
-
for (const [k, v] of this._map1) {
|
|
119
|
-
if (v === num) {
|
|
120
|
-
this._map1.delete(k);
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
for (const [k, v] of this._map2) {
|
|
125
|
-
if (v === num) {
|
|
126
|
-
this._map2.delete(k);
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
for (const [k, v] of this._counterMap) {
|
|
131
|
-
if (v === key) {
|
|
132
|
-
this._counterMap.delete(k);
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (dispose) {
|
|
137
|
-
if (handle == null ? void 0 : handle.alive)
|
|
138
|
-
handle.dispose();
|
|
139
|
-
if (handle2 == null ? void 0 : handle2.alive)
|
|
140
|
-
handle2.dispose();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
deleteByHandle(handle, dispose) {
|
|
144
|
-
const key = this.getByHandle(handle);
|
|
145
|
-
if (typeof key !== "undefined") {
|
|
146
|
-
this.delete(key, dispose);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
clear() {
|
|
150
|
-
this._counter = 0;
|
|
151
|
-
this._map1.clear();
|
|
152
|
-
this._map2.clear();
|
|
153
|
-
this._map3.clear();
|
|
154
|
-
this._map4.clear();
|
|
155
|
-
this._counterMap.clear();
|
|
156
|
-
if (this._mapClear.alive) {
|
|
157
|
-
this._call(this._mapClear, void 0);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
dispose() {
|
|
161
|
-
for (const v of this._disposables.values()) {
|
|
162
|
-
if (v.alive) {
|
|
163
|
-
v.dispose();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
for (const v of this._map3.values()) {
|
|
167
|
-
if (v.alive) {
|
|
168
|
-
v.dispose();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
for (const v of this._map4.values()) {
|
|
172
|
-
if (v.alive) {
|
|
173
|
-
v.dispose();
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
this._disposables.clear();
|
|
177
|
-
this.clear();
|
|
178
|
-
}
|
|
179
|
-
get size() {
|
|
180
|
-
return this._map1.size;
|
|
181
|
-
}
|
|
182
|
-
[Symbol.iterator]() {
|
|
183
|
-
const keys = this._map1.keys();
|
|
184
|
-
return {
|
|
185
|
-
next: () => {
|
|
186
|
-
while (true) {
|
|
187
|
-
const k1 = keys.next();
|
|
188
|
-
if (k1.done)
|
|
189
|
-
return { value: void 0, done: true };
|
|
190
|
-
const n = this._map1.get(k1.value);
|
|
191
|
-
if (typeof n === "undefined")
|
|
192
|
-
continue;
|
|
193
|
-
const v1 = this._map3.get(n);
|
|
194
|
-
const v2 = this._map4.get(n);
|
|
195
|
-
if (!v1)
|
|
196
|
-
continue;
|
|
197
|
-
const k2 = this._get2(n);
|
|
198
|
-
return { value: [k1.value, v1, k2, v2], done: false };
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
_get2(num) {
|
|
204
|
-
for (const [k, v] of this._map2) {
|
|
205
|
-
if (v === num)
|
|
206
|
-
return k;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
_call(fn2, thisArg, ...args) {
|
|
210
|
-
return this.ctx.unwrapResult(this.ctx.callFunction(fn2, typeof thisArg === "undefined" ? this.ctx.undefined : thisArg, ...args));
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
function isES2015Class(cls) {
|
|
214
|
-
return typeof cls === "function" && /^class\s/.test(Function.prototype.toString.call(cls));
|
|
215
|
-
}
|
|
216
|
-
function isObject(value) {
|
|
217
|
-
return typeof value === "function" || typeof value === "object" && value !== null;
|
|
218
|
-
}
|
|
219
|
-
function walkObject(value, callback) {
|
|
220
|
-
const set = /* @__PURE__ */ new Set();
|
|
221
|
-
const walk = (v) => {
|
|
222
|
-
if (!isObject(v) || set.has(v) || (callback == null ? void 0 : callback(v, set)) === false)
|
|
223
|
-
return;
|
|
224
|
-
set.add(v);
|
|
225
|
-
if (Array.isArray(v)) {
|
|
226
|
-
for (const e of v) {
|
|
227
|
-
walk(e);
|
|
228
|
-
}
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (typeof v === "object") {
|
|
232
|
-
const proto = Object.getPrototypeOf(v);
|
|
233
|
-
if (proto && proto !== Object.prototype) {
|
|
234
|
-
walk(proto);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
for (const d of Object.values(Object.getOwnPropertyDescriptors(v))) {
|
|
238
|
-
if ("value" in d)
|
|
239
|
-
walk(d.value);
|
|
240
|
-
if ("get" in d)
|
|
241
|
-
walk(d.get);
|
|
242
|
-
if ("set" in d)
|
|
243
|
-
walk(d.set);
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
walk(value);
|
|
247
|
-
return set;
|
|
248
|
-
}
|
|
249
|
-
function complexity(value, max) {
|
|
250
|
-
return walkObject(value, max ? (_, set) => set.size < max : void 0).size;
|
|
251
|
-
}
|
|
252
|
-
function newDeferred() {
|
|
253
|
-
let res = () => {
|
|
254
|
-
};
|
|
255
|
-
let rej = () => {
|
|
256
|
-
};
|
|
257
|
-
const promise = new Promise((resolve, reject) => {
|
|
258
|
-
res = resolve;
|
|
259
|
-
rej = reject;
|
|
260
|
-
});
|
|
261
|
-
return {
|
|
262
|
-
promise,
|
|
263
|
-
resolve: res,
|
|
264
|
-
reject: rej
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
function fn(ctx, code) {
|
|
268
|
-
const handle = ctx.unwrapResult(ctx.evalCode(code));
|
|
269
|
-
const f = (thisArg, ...args) => {
|
|
270
|
-
return ctx.unwrapResult(ctx.callFunction(handle, thisArg != null ? thisArg : ctx.undefined, ...args));
|
|
271
|
-
};
|
|
272
|
-
f.dispose = () => handle.dispose();
|
|
273
|
-
f.alive = true;
|
|
274
|
-
Object.defineProperty(f, "alive", {
|
|
275
|
-
get: () => handle.alive
|
|
276
|
-
});
|
|
277
|
-
return f;
|
|
278
|
-
}
|
|
279
|
-
function call(ctx, code, thisArg, ...args) {
|
|
280
|
-
const f = fn(ctx, code);
|
|
281
|
-
try {
|
|
282
|
-
return f(thisArg, ...args);
|
|
283
|
-
} finally {
|
|
284
|
-
f.dispose();
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
function eq(ctx, a, b) {
|
|
288
|
-
return ctx.dump(call(ctx, "Object.is", void 0, a, b));
|
|
289
|
-
}
|
|
290
|
-
function instanceOf(ctx, a, b) {
|
|
291
|
-
return ctx.dump(call(ctx, "(a, b) => a instanceof b", void 0, a, b));
|
|
292
|
-
}
|
|
293
|
-
function isHandleObject(ctx, h) {
|
|
294
|
-
return ctx.dump(call(ctx, `a => typeof a === "object" && a !== null || typeof a === "function"`, void 0, h));
|
|
295
|
-
}
|
|
296
|
-
function json(ctx, target) {
|
|
297
|
-
const json2 = JSON.stringify(target);
|
|
298
|
-
if (!json2)
|
|
299
|
-
return ctx.undefined;
|
|
300
|
-
return call(ctx, `JSON.parse`, void 0, ctx.newString(json2));
|
|
301
|
-
}
|
|
302
|
-
function consumeAll(handles, cb) {
|
|
303
|
-
try {
|
|
304
|
-
return cb(handles);
|
|
305
|
-
} finally {
|
|
306
|
-
for (const h of handles) {
|
|
307
|
-
if (h.alive)
|
|
308
|
-
h.dispose();
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
function mayConsume([handle, shouldBeDisposed], fn2) {
|
|
313
|
-
try {
|
|
314
|
-
return fn2(handle);
|
|
315
|
-
} finally {
|
|
316
|
-
if (shouldBeDisposed) {
|
|
317
|
-
handle.dispose();
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
function mayConsumeAll(handles, fn2) {
|
|
322
|
-
try {
|
|
323
|
-
return fn2(...handles.map((h) => h[0]));
|
|
324
|
-
} finally {
|
|
325
|
-
for (const [handle, shouldBeDisposed] of handles) {
|
|
326
|
-
if (shouldBeDisposed) {
|
|
327
|
-
handle.dispose();
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
function isQuickJSDeferredPromise(d) {
|
|
333
|
-
return "handle" in d;
|
|
334
|
-
}
|
|
335
|
-
function handleFrom(d) {
|
|
336
|
-
return isQuickJSDeferredPromise(d) ? d.handle : d;
|
|
337
|
-
}
|
|
338
|
-
function marshalProperties(ctx, target, handle, marshal2) {
|
|
339
|
-
const descs = ctx.newObject();
|
|
340
|
-
const cb = (key, desc2) => {
|
|
341
|
-
const keyHandle = marshal2(key);
|
|
342
|
-
const valueHandle = typeof desc2.value === "undefined" ? void 0 : marshal2(desc2.value);
|
|
343
|
-
const getHandle = typeof desc2.get === "undefined" ? void 0 : marshal2(desc2.get);
|
|
344
|
-
const setHandle = typeof desc2.set === "undefined" ? void 0 : marshal2(desc2.set);
|
|
345
|
-
ctx.newObject().consume((descObj) => {
|
|
346
|
-
Object.entries(desc2).forEach(([k, v]) => {
|
|
347
|
-
const v2 = k === "value" ? valueHandle : k === "get" ? getHandle : k === "set" ? setHandle : v ? ctx.true : ctx.false;
|
|
348
|
-
if (v2) {
|
|
349
|
-
ctx.setProp(descObj, k, v2);
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
ctx.setProp(descs, keyHandle, descObj);
|
|
353
|
-
});
|
|
354
|
-
};
|
|
355
|
-
const desc = Object.getOwnPropertyDescriptors(target);
|
|
356
|
-
Object.entries(desc).forEach(([k, v]) => cb(k, v));
|
|
357
|
-
Object.getOwnPropertySymbols(desc).forEach((k) => cb(k, desc[k]));
|
|
358
|
-
call(ctx, `Object.defineProperties`, void 0, handle, descs).dispose();
|
|
359
|
-
descs.dispose();
|
|
360
|
-
}
|
|
361
|
-
function marshalFunction(ctx, target, marshal2, unmarshal2, preMarshal, preApply) {
|
|
362
|
-
var _a;
|
|
363
|
-
if (typeof target !== "function")
|
|
364
|
-
return;
|
|
365
|
-
const raw = ctx.newFunction(target.name, function(...argHandles) {
|
|
366
|
-
const that = unmarshal2(this);
|
|
367
|
-
const args = argHandles.map((a) => unmarshal2(a));
|
|
368
|
-
if (isES2015Class(target) && isObject(that)) {
|
|
369
|
-
const result = new target(...args);
|
|
370
|
-
Object.entries(result).forEach(([key, value]) => {
|
|
371
|
-
ctx.setProp(this, key, marshal2(value));
|
|
372
|
-
});
|
|
373
|
-
return this;
|
|
374
|
-
}
|
|
375
|
-
return marshal2(preApply ? preApply(target, that, args) : target.apply(that, args));
|
|
376
|
-
}).consume((handle2) => call(ctx, `Cls => {
|
|
377
|
-
const fn = function(...args) { return Cls.apply(this, args); };
|
|
378
|
-
fn.name = Cls.name;
|
|
379
|
-
fn.length = Cls.length;
|
|
380
|
-
return fn;
|
|
381
|
-
}`, void 0, handle2));
|
|
382
|
-
const handle = (_a = preMarshal(target, raw)) != null ? _a : raw;
|
|
383
|
-
marshalProperties(ctx, target, raw, marshal2);
|
|
384
|
-
return handle;
|
|
385
|
-
}
|
|
386
|
-
function marshalObject(ctx, target, marshal2, preMarshal) {
|
|
387
|
-
var _a;
|
|
388
|
-
if (typeof target !== "object" || target === null)
|
|
389
|
-
return;
|
|
390
|
-
const raw = Array.isArray(target) ? ctx.newArray() : ctx.newObject();
|
|
391
|
-
const handle = (_a = preMarshal(target, raw)) != null ? _a : raw;
|
|
392
|
-
const prototype = Object.getPrototypeOf(target);
|
|
393
|
-
const prototypeHandle = prototype && prototype !== Object.prototype && prototype !== Array.prototype ? marshal2(prototype) : void 0;
|
|
394
|
-
if (prototypeHandle) {
|
|
395
|
-
call(ctx, "Object.setPrototypeOf", void 0, handle, prototypeHandle).dispose();
|
|
396
|
-
}
|
|
397
|
-
marshalProperties(ctx, target, raw, marshal2);
|
|
398
|
-
return handle;
|
|
399
|
-
}
|
|
400
|
-
function marshalPrimitive(ctx, target) {
|
|
401
|
-
switch (typeof target) {
|
|
402
|
-
case "undefined":
|
|
403
|
-
return ctx.undefined;
|
|
404
|
-
case "number":
|
|
405
|
-
return ctx.newNumber(target);
|
|
406
|
-
case "string":
|
|
407
|
-
return ctx.newString(target);
|
|
408
|
-
case "boolean":
|
|
409
|
-
return target ? ctx.true : ctx.false;
|
|
410
|
-
case "object":
|
|
411
|
-
return target === null ? ctx.null : void 0;
|
|
412
|
-
}
|
|
413
|
-
return void 0;
|
|
414
|
-
}
|
|
415
|
-
function marshalSymbol(ctx, target, preMarshal) {
|
|
416
|
-
var _a;
|
|
417
|
-
if (typeof target !== "symbol")
|
|
418
|
-
return;
|
|
419
|
-
const handle = call(ctx, "d => Symbol(d)", void 0, target.description ? ctx.newString(target.description) : ctx.undefined);
|
|
420
|
-
return (_a = preMarshal(target, handle)) != null ? _a : handle;
|
|
421
|
-
}
|
|
422
|
-
function marshalJSON(ctx, target, preMarshal) {
|
|
423
|
-
var _a;
|
|
424
|
-
const raw = json(ctx, target);
|
|
425
|
-
const handle = (_a = preMarshal(target, raw)) != null ? _a : raw;
|
|
426
|
-
return handle;
|
|
427
|
-
}
|
|
428
|
-
function marshalPromise(ctx, target, marshal2, preMarshal) {
|
|
429
|
-
var _a;
|
|
430
|
-
if (!(target instanceof Promise))
|
|
431
|
-
return;
|
|
432
|
-
const promise = ctx.newPromise();
|
|
433
|
-
target.then((d) => promise.resolve(marshal2(d)), (d) => promise.reject(marshal2(d)));
|
|
434
|
-
return (_a = preMarshal(target, promise)) != null ? _a : promise.handle;
|
|
435
|
-
}
|
|
436
|
-
function marshal(target, options) {
|
|
437
|
-
var _a, _b, _c, _d;
|
|
438
|
-
const { ctx, unmarshal: unmarshal2, isMarshalable, find, pre } = options;
|
|
439
|
-
{
|
|
440
|
-
const primitive = marshalPrimitive(ctx, target);
|
|
441
|
-
if (primitive) {
|
|
442
|
-
return primitive;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
{
|
|
446
|
-
const handle = find(target);
|
|
447
|
-
if (handle)
|
|
448
|
-
return handle;
|
|
449
|
-
}
|
|
450
|
-
const marshalable = isMarshalable == null ? void 0 : isMarshalable(target);
|
|
451
|
-
if (marshalable === false) {
|
|
452
|
-
return ctx.undefined;
|
|
453
|
-
}
|
|
454
|
-
const pre2 = (target2, handle) => pre(target2, handle, marshalable);
|
|
455
|
-
if (marshalable === "json") {
|
|
456
|
-
return marshalJSON(ctx, target, pre2);
|
|
457
|
-
}
|
|
458
|
-
const marshal2 = (t) => marshal(t, options);
|
|
459
|
-
return (_d = (_c = (_b = (_a = marshalSymbol(ctx, target, pre2)) != null ? _a : marshalPromise(ctx, target, marshal2, pre2)) != null ? _b : marshalFunction(ctx, target, marshal2, unmarshal2, pre2, options.preApply)) != null ? _c : marshalObject(ctx, target, marshal2, pre2)) != null ? _d : ctx.undefined;
|
|
460
|
-
}
|
|
461
|
-
function unmarshalProperties(ctx, handle, target, unmarshal2) {
|
|
462
|
-
ctx.newFunction("", (key, value) => {
|
|
463
|
-
const [keyName] = unmarshal2(key);
|
|
464
|
-
if (typeof keyName !== "string" && typeof keyName !== "number" && typeof keyName !== "symbol")
|
|
465
|
-
return;
|
|
466
|
-
const desc = [
|
|
467
|
-
["value", true],
|
|
468
|
-
["get", true],
|
|
469
|
-
["set", true],
|
|
470
|
-
["configurable", false],
|
|
471
|
-
["enumerable", false],
|
|
472
|
-
["writable", false]
|
|
473
|
-
].reduce((desc2, [key2, unmarshable]) => {
|
|
474
|
-
const h = ctx.getProp(value, key2);
|
|
475
|
-
const ty = ctx.typeof(h);
|
|
476
|
-
if (ty === "undefined")
|
|
477
|
-
return desc2;
|
|
478
|
-
if (!unmarshable && ty === "boolean") {
|
|
479
|
-
desc2[key2] = ctx.dump(ctx.getProp(value, key2));
|
|
480
|
-
return desc2;
|
|
481
|
-
}
|
|
482
|
-
const [v, alreadyExists] = unmarshal2(h);
|
|
483
|
-
if (alreadyExists) {
|
|
484
|
-
h.dispose();
|
|
485
|
-
}
|
|
486
|
-
desc2[key2] = v;
|
|
487
|
-
return desc2;
|
|
488
|
-
}, {});
|
|
489
|
-
Object.defineProperty(target, keyName, desc);
|
|
490
|
-
}).consume((fn2) => {
|
|
491
|
-
call(ctx, `(o, fn) => {
|
|
492
|
-
const descs = Object.getOwnPropertyDescriptors(o);
|
|
493
|
-
Object.entries(descs).forEach(([k, v]) => fn(k, v));
|
|
494
|
-
Object.getOwnPropertySymbols(descs).forEach(k => fn(k, descs[k]));
|
|
495
|
-
}`, void 0, handle, fn2).dispose();
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
function unmarshalFunction(ctx, handle, marshal2, unmarshal2, preUnmarshal) {
|
|
499
|
-
var _a;
|
|
500
|
-
if (ctx.typeof(handle) !== "function")
|
|
501
|
-
return;
|
|
502
|
-
const raw = function(...args) {
|
|
503
|
-
return mayConsumeAll([marshal2(this), ...args.map((a) => marshal2(a))], (thisHandle, ...argHandles) => {
|
|
504
|
-
if (new.target) {
|
|
505
|
-
const [instance] = unmarshal2(call(ctx, `(Cls, ...args) => new Cls(...args)`, thisHandle, handle, ...argHandles));
|
|
506
|
-
Object.defineProperties(this, Object.getOwnPropertyDescriptors(instance));
|
|
507
|
-
return this;
|
|
508
|
-
}
|
|
509
|
-
const resultHandle = ctx.unwrapResult(ctx.callFunction(handle, thisHandle, ...argHandles));
|
|
510
|
-
const [result, alreadyExists] = unmarshal2(resultHandle);
|
|
511
|
-
if (alreadyExists)
|
|
512
|
-
resultHandle.dispose();
|
|
513
|
-
return result;
|
|
514
|
-
});
|
|
515
|
-
};
|
|
516
|
-
const func = (_a = preUnmarshal(raw, handle)) != null ? _a : raw;
|
|
517
|
-
unmarshalProperties(ctx, handle, raw, unmarshal2);
|
|
518
|
-
return func;
|
|
519
|
-
}
|
|
520
|
-
function unmarshalObject(ctx, handle, unmarshal2, preUnmarshal) {
|
|
521
|
-
var _a;
|
|
522
|
-
if (ctx.typeof(handle) !== "object" || ctx.unwrapResult(ctx.evalCode("o => o === null")).consume((n) => ctx.dump(ctx.unwrapResult(ctx.callFunction(n, ctx.undefined, handle)))))
|
|
523
|
-
return;
|
|
524
|
-
const raw = call(ctx, "Array.isArray", void 0, handle).consume((r) => ctx.dump(r)) ? [] : {};
|
|
525
|
-
const obj = (_a = preUnmarshal(raw, handle)) != null ? _a : raw;
|
|
526
|
-
const prototype = call(ctx, `o => {
|
|
527
|
-
const p = Object.getPrototypeOf(o);
|
|
528
|
-
return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
|
|
529
|
-
}`, void 0, handle).consume((prototype2) => {
|
|
530
|
-
if (ctx.typeof(prototype2) === "undefined")
|
|
531
|
-
return;
|
|
532
|
-
const [proto] = unmarshal2(prototype2);
|
|
533
|
-
return proto;
|
|
534
|
-
});
|
|
535
|
-
if (typeof prototype === "object") {
|
|
536
|
-
Object.setPrototypeOf(obj, prototype);
|
|
537
|
-
}
|
|
538
|
-
unmarshalProperties(ctx, handle, raw, unmarshal2);
|
|
539
|
-
return obj;
|
|
540
|
-
}
|
|
541
|
-
function unmarshalPrimitive(ctx, handle) {
|
|
542
|
-
const ty = ctx.typeof(handle);
|
|
543
|
-
if (ty === "undefined" || ty === "number" || ty === "string" || ty === "boolean") {
|
|
544
|
-
return [ctx.dump(handle), true];
|
|
545
|
-
} else if (ty === "object") {
|
|
546
|
-
const isNull = ctx.unwrapResult(ctx.evalCode("a => a === null")).consume((n) => ctx.dump(ctx.unwrapResult(ctx.callFunction(n, ctx.undefined, handle))));
|
|
547
|
-
if (isNull) {
|
|
548
|
-
return [null, true];
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
return [void 0, false];
|
|
552
|
-
}
|
|
553
|
-
function unmarshalPromise(ctx, handle, marshal2, preUnmarshal) {
|
|
554
|
-
var _a;
|
|
555
|
-
if (!isPromiseHandle(ctx, handle))
|
|
556
|
-
return;
|
|
557
|
-
const deferred = newDeferred();
|
|
558
|
-
const [resHandle, resShouldBeDisposed] = marshal2(deferred.resolve);
|
|
559
|
-
const [rejHandle, rejShouldBeDisposed] = marshal2(deferred.reject);
|
|
560
|
-
call(ctx, "(p, res, rej) => { p.then(res, rej); }", void 0, handle, resHandle, rejHandle);
|
|
561
|
-
if (resShouldBeDisposed)
|
|
562
|
-
resHandle.dispose();
|
|
563
|
-
if (rejShouldBeDisposed)
|
|
564
|
-
rejHandle.dispose();
|
|
565
|
-
return (_a = preUnmarshal(deferred.promise, handle)) != null ? _a : deferred.promise;
|
|
566
|
-
}
|
|
567
|
-
function isPromiseHandle(ctx, handle) {
|
|
568
|
-
if (!handle.owner)
|
|
569
|
-
return false;
|
|
570
|
-
return ctx.unwrapResult(ctx.evalCode("Promise")).consume((promise) => {
|
|
571
|
-
if (!handle.owner)
|
|
572
|
-
return false;
|
|
573
|
-
return instanceOf(ctx, handle, promise);
|
|
574
|
-
});
|
|
575
|
-
}
|
|
576
|
-
function unmarshalSymbol(ctx, handle, preUnmarshal) {
|
|
577
|
-
var _a;
|
|
578
|
-
if (ctx.typeof(handle) !== "symbol")
|
|
579
|
-
return;
|
|
580
|
-
const desc = ctx.getString(ctx.getProp(handle, "description"));
|
|
581
|
-
const sym = Symbol(desc);
|
|
582
|
-
return (_a = preUnmarshal(sym, handle)) != null ? _a : sym;
|
|
583
|
-
}
|
|
584
|
-
function unmarshal(handle, options) {
|
|
585
|
-
const [result] = unmarshalInner(handle, options);
|
|
586
|
-
return result;
|
|
587
|
-
}
|
|
588
|
-
function unmarshalInner(handle, options) {
|
|
589
|
-
var _a, _b, _c;
|
|
590
|
-
const { ctx, marshal: marshal2, find, pre } = options;
|
|
591
|
-
{
|
|
592
|
-
const [target, ok] = unmarshalPrimitive(ctx, handle);
|
|
593
|
-
if (ok)
|
|
594
|
-
return [target, false];
|
|
595
|
-
}
|
|
596
|
-
{
|
|
597
|
-
const target = find(handle);
|
|
598
|
-
if (target) {
|
|
599
|
-
return [target, true];
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
const unmarshal2 = (h) => unmarshalInner(h, options);
|
|
603
|
-
const result = (_c = (_b = (_a = unmarshalSymbol(ctx, handle, pre)) != null ? _a : unmarshalPromise(ctx, handle, marshal2, pre)) != null ? _b : unmarshalFunction(ctx, handle, marshal2, unmarshal2, pre)) != null ? _c : unmarshalObject(ctx, handle, unmarshal2, pre);
|
|
604
|
-
return [result, false];
|
|
605
|
-
}
|
|
606
|
-
function wrap(ctx, target, proxyKeySymbol, proxyKeySymbolHandle, marshal2, syncMode) {
|
|
607
|
-
if (!isObject(target) || target instanceof Promise)
|
|
608
|
-
return void 0;
|
|
609
|
-
if (isWrapped(target, proxyKeySymbol))
|
|
610
|
-
return target;
|
|
611
|
-
const rec = new Proxy(target, {
|
|
612
|
-
get(obj, key) {
|
|
613
|
-
return key === proxyKeySymbol ? obj : Reflect.get(obj, key);
|
|
614
|
-
},
|
|
615
|
-
set(obj, key, value, receiver) {
|
|
616
|
-
var _a;
|
|
617
|
-
const v = unwrap(value, proxyKeySymbol);
|
|
618
|
-
const sync = (_a = syncMode == null ? void 0 : syncMode(receiver)) != null ? _a : "host";
|
|
619
|
-
if (sync !== "vm" && !Reflect.set(obj, key, v, receiver) || sync === "host" || !ctx.alive)
|
|
620
|
-
return true;
|
|
621
|
-
mayConsumeAll([marshal2(receiver), marshal2(key), marshal2(v)], (receiverHandle, keyHandle, valueHandle) => {
|
|
622
|
-
const [handle2, unwrapped] = unwrapHandle(ctx, receiverHandle, proxyKeySymbolHandle);
|
|
623
|
-
if (unwrapped) {
|
|
624
|
-
handle2.consume((h) => ctx.setProp(h, keyHandle, valueHandle));
|
|
625
|
-
} else {
|
|
626
|
-
ctx.setProp(handle2, keyHandle, valueHandle);
|
|
627
|
-
}
|
|
628
|
-
});
|
|
629
|
-
return true;
|
|
630
|
-
},
|
|
631
|
-
deleteProperty(obj, key) {
|
|
632
|
-
var _a;
|
|
633
|
-
const sync = (_a = syncMode == null ? void 0 : syncMode(rec)) != null ? _a : "host";
|
|
634
|
-
return mayConsumeAll([marshal2(rec), marshal2(key)], (recHandle, keyHandle) => {
|
|
635
|
-
const [handle2, unwrapped] = unwrapHandle(ctx, recHandle, proxyKeySymbolHandle);
|
|
636
|
-
if (sync === "vm" || Reflect.deleteProperty(obj, key)) {
|
|
637
|
-
if (sync === "host" || !ctx.alive)
|
|
638
|
-
return true;
|
|
639
|
-
if (unwrapped) {
|
|
640
|
-
handle2.consume((h) => call(ctx, `(a, b) => delete a[b]`, void 0, h, keyHandle));
|
|
641
|
-
} else {
|
|
642
|
-
call(ctx, `(a, b) => delete a[b]`, void 0, handle2, keyHandle);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
return true;
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
});
|
|
649
|
-
return rec;
|
|
650
|
-
}
|
|
651
|
-
function wrapHandle(ctx, handle, proxyKeySymbol, proxyKeySymbolHandle, unmarshal2, syncMode) {
|
|
652
|
-
if (!isHandleObject(ctx, handle))
|
|
653
|
-
return [void 0, false];
|
|
654
|
-
if (isHandleWrapped(ctx, handle, proxyKeySymbolHandle))
|
|
655
|
-
return [handle, false];
|
|
656
|
-
return consumeAll([
|
|
657
|
-
ctx.newFunction("getSyncMode", (h) => {
|
|
658
|
-
const res = syncMode == null ? void 0 : syncMode(unmarshal2(h));
|
|
659
|
-
if (typeof res === "string")
|
|
660
|
-
return ctx.newString(res);
|
|
661
|
-
return ctx.undefined;
|
|
662
|
-
}),
|
|
663
|
-
ctx.newFunction("setter", (h, keyHandle, valueHandle) => {
|
|
664
|
-
const target = unmarshal2(h);
|
|
665
|
-
if (!target)
|
|
666
|
-
return;
|
|
667
|
-
const key = unmarshal2(keyHandle);
|
|
668
|
-
if (key === "__proto__")
|
|
669
|
-
return;
|
|
670
|
-
const value = unmarshal2(valueHandle);
|
|
671
|
-
unwrap(target, proxyKeySymbol)[key] = value;
|
|
672
|
-
}),
|
|
673
|
-
ctx.newFunction("deleter", (h, keyHandle) => {
|
|
674
|
-
const target = unmarshal2(h);
|
|
675
|
-
if (!target)
|
|
676
|
-
return;
|
|
677
|
-
const key = unmarshal2(keyHandle);
|
|
678
|
-
delete unwrap(target, proxyKeySymbol)[key];
|
|
679
|
-
})
|
|
680
|
-
], (args) => [
|
|
681
|
-
call(ctx, `(target, sym, getSyncMode, setter, deleter) => {
|
|
682
|
-
const rec = new Proxy(target, {
|
|
683
|
-
get(obj, key, receiver) {
|
|
684
|
-
return key === sym ? obj : Reflect.get(obj, key, receiver)
|
|
685
|
-
},
|
|
686
|
-
set(obj, key, value, receiver) {
|
|
687
|
-
const v = typeof value === "object" && value !== null || typeof value === "function"
|
|
688
|
-
? value[sym] ?? value
|
|
689
|
-
: value;
|
|
690
|
-
const sync = getSyncMode(receiver) ?? "vm";
|
|
691
|
-
if (sync === "host" || Reflect.set(obj, key, v, receiver)) {
|
|
692
|
-
if (sync !== "vm") {
|
|
693
|
-
setter(receiver, key, v);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
return true;
|
|
697
|
-
},
|
|
698
|
-
deleteProperty(obj, key) {
|
|
699
|
-
const sync = getSyncMode(rec) ?? "vm";
|
|
700
|
-
if (sync === "host" || Reflect.deleteProperty(obj, key)) {
|
|
701
|
-
if (sync !== "vm") {
|
|
702
|
-
deleter(rec, key);
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
return true;
|
|
706
|
-
},
|
|
707
|
-
});
|
|
708
|
-
return rec;
|
|
709
|
-
}`, void 0, handle, proxyKeySymbolHandle, ...args),
|
|
710
|
-
true
|
|
711
|
-
]);
|
|
712
|
-
}
|
|
713
|
-
function unwrap(obj, key) {
|
|
714
|
-
var _a;
|
|
715
|
-
return isObject(obj) ? (_a = obj[key]) != null ? _a : obj : obj;
|
|
716
|
-
}
|
|
717
|
-
function unwrapHandle(ctx, handle, key) {
|
|
718
|
-
if (!isHandleWrapped(ctx, handle, key))
|
|
719
|
-
return [handle, false];
|
|
720
|
-
return [ctx.getProp(handle, key), true];
|
|
721
|
-
}
|
|
722
|
-
function isWrapped(obj, key) {
|
|
723
|
-
return isObject(obj) && !!obj[key];
|
|
724
|
-
}
|
|
725
|
-
function isHandleWrapped(ctx, handle, key) {
|
|
726
|
-
return !!ctx.dump(call(ctx, `(a, s) => (a instanceof Promise) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`, void 0, handle, key));
|
|
727
|
-
}
|
|
728
|
-
const defaultRegisteredObjects = [
|
|
729
|
-
[Symbol, "Symbol"],
|
|
730
|
-
[Symbol.prototype, "Symbol.prototype"],
|
|
731
|
-
[Object, "Object"],
|
|
732
|
-
[Object.prototype, "Object.prototype"],
|
|
733
|
-
[Function, "Function"],
|
|
734
|
-
[Function.prototype, "Function.prototype"],
|
|
735
|
-
[Boolean, "Boolean"],
|
|
736
|
-
[Boolean.prototype, "Boolean.prototype"],
|
|
737
|
-
[Array, "Array"],
|
|
738
|
-
[Array.prototype, "Array.prototype"],
|
|
739
|
-
[Error, "Error"],
|
|
740
|
-
[Error.prototype, "Error.prototype"],
|
|
741
|
-
[EvalError, "EvalError"],
|
|
742
|
-
[EvalError.prototype, "EvalError.prototype"],
|
|
743
|
-
[RangeError, "RangeError"],
|
|
744
|
-
[RangeError.prototype, "RangeError.prototype"],
|
|
745
|
-
[ReferenceError, "ReferenceError"],
|
|
746
|
-
[ReferenceError.prototype, "ReferenceError.prototype"],
|
|
747
|
-
[SyntaxError, "SyntaxError"],
|
|
748
|
-
[SyntaxError.prototype, "SyntaxError.prototype"],
|
|
749
|
-
[TypeError, "TypeError"],
|
|
750
|
-
[TypeError.prototype, "TypeError.prototype"],
|
|
751
|
-
[URIError, "URIError"],
|
|
752
|
-
[URIError.prototype, "URIError.prototype"],
|
|
753
|
-
...Object.getOwnPropertyNames(Symbol).filter((k) => typeof Symbol[k] === "symbol").map((k) => [Symbol[k], `Symbol.${k}`])
|
|
754
|
-
];
|
|
755
|
-
class Arena {
|
|
756
|
-
constructor(ctx, options) {
|
|
757
|
-
var _a;
|
|
758
|
-
this._registeredMapDispose = /* @__PURE__ */ new Set();
|
|
759
|
-
this._sync = /* @__PURE__ */ new Set();
|
|
760
|
-
this._temporalSync = /* @__PURE__ */ new Set();
|
|
761
|
-
this._symbol = Symbol();
|
|
762
|
-
this._isMarshalable = (t) => {
|
|
763
|
-
var _a2, _b;
|
|
764
|
-
const im = (_a2 = this._options) == null ? void 0 : _a2.isMarshalable;
|
|
765
|
-
return (_b = typeof im === "function" ? im(this._unwrap(t)) : im) != null ? _b : "json";
|
|
766
|
-
};
|
|
767
|
-
this._marshalFind = (t) => {
|
|
768
|
-
var _a2, _b, _c;
|
|
769
|
-
const unwrappedT = this._unwrap(t);
|
|
770
|
-
const handle = (_c = (_b = (_a2 = this._registeredMap.get(t)) != null ? _a2 : unwrappedT !== t ? this._registeredMap.get(unwrappedT) : void 0) != null ? _b : this._map.get(t)) != null ? _c : unwrappedT !== t ? this._map.get(unwrappedT) : void 0;
|
|
771
|
-
return handle;
|
|
772
|
-
};
|
|
773
|
-
this._marshalPre = (t, h, mode) => {
|
|
774
|
-
var _a2;
|
|
775
|
-
if (mode === "json")
|
|
776
|
-
return;
|
|
777
|
-
return (_a2 = this._register(t, handleFrom(h), this._map)) == null ? void 0 : _a2[1];
|
|
778
|
-
};
|
|
779
|
-
this._marshalPreApply = (target, that, args) => {
|
|
780
|
-
const unwrapped = isObject(that) ? this._unwrap(that) : void 0;
|
|
781
|
-
if (unwrapped)
|
|
782
|
-
this._temporalSync.add(unwrapped);
|
|
783
|
-
try {
|
|
784
|
-
return target.apply(that, args);
|
|
785
|
-
} finally {
|
|
786
|
-
if (unwrapped)
|
|
787
|
-
this._temporalSync.delete(unwrapped);
|
|
788
|
-
}
|
|
789
|
-
};
|
|
790
|
-
this._marshal = (target) => {
|
|
791
|
-
var _a2;
|
|
792
|
-
const registered = this._registeredMap.get(target);
|
|
793
|
-
if (registered) {
|
|
794
|
-
return [registered, false];
|
|
795
|
-
}
|
|
796
|
-
const handle = marshal((_a2 = this._wrap(target)) != null ? _a2 : target, {
|
|
797
|
-
ctx: this.context,
|
|
798
|
-
unmarshal: this._unmarshal,
|
|
799
|
-
isMarshalable: this._isMarshalable,
|
|
800
|
-
find: this._marshalFind,
|
|
801
|
-
pre: this._marshalPre,
|
|
802
|
-
preApply: this._marshalPreApply
|
|
803
|
-
});
|
|
804
|
-
return [handle, !this._map.hasHandle(handle)];
|
|
805
|
-
};
|
|
806
|
-
this._preUnmarshal = (t, h) => {
|
|
807
|
-
var _a2;
|
|
808
|
-
return (_a2 = this._register(t, h, void 0, true)) == null ? void 0 : _a2[0];
|
|
809
|
-
};
|
|
810
|
-
this._unmarshalFind = (h) => {
|
|
811
|
-
var _a2;
|
|
812
|
-
return (_a2 = this._registeredMap.getByHandle(h)) != null ? _a2 : this._map.getByHandle(h);
|
|
813
|
-
};
|
|
814
|
-
this._unmarshal = (handle) => {
|
|
815
|
-
const registered = this._registeredMap.getByHandle(handle);
|
|
816
|
-
if (typeof registered !== "undefined") {
|
|
817
|
-
return registered;
|
|
818
|
-
}
|
|
819
|
-
const [wrappedHandle] = this._wrapHandle(handle);
|
|
820
|
-
return unmarshal(wrappedHandle != null ? wrappedHandle : handle, {
|
|
821
|
-
ctx: this.context,
|
|
822
|
-
marshal: this._marshal,
|
|
823
|
-
find: this._unmarshalFind,
|
|
824
|
-
pre: this._preUnmarshal
|
|
825
|
-
});
|
|
826
|
-
};
|
|
827
|
-
this._syncMode = (obj) => {
|
|
828
|
-
const obj2 = this._unwrap(obj);
|
|
829
|
-
return this._sync.has(obj2) || this._temporalSync.has(obj2) ? "both" : void 0;
|
|
830
|
-
};
|
|
831
|
-
this._unwrapIfNotSynced = (target) => {
|
|
832
|
-
const unwrapped = this._unwrap(target);
|
|
833
|
-
return unwrapped instanceof Promise || !this._sync.has(unwrapped) ? unwrapped : target;
|
|
834
|
-
};
|
|
835
|
-
if ((options == null ? void 0 : options.compat) && !("runtime" in ctx)) {
|
|
836
|
-
ctx.runtime = {
|
|
837
|
-
hasPendingJob: () => ctx.hasPendingJob(),
|
|
838
|
-
executePendingJobs: (maxJobsToExecute) => ctx.executePendingJobs(maxJobsToExecute)
|
|
839
|
-
};
|
|
840
|
-
}
|
|
841
|
-
this.context = ctx;
|
|
842
|
-
this._options = options;
|
|
843
|
-
this._symbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
844
|
-
this._map = new VMMap(ctx);
|
|
845
|
-
this._registeredMap = new VMMap(ctx);
|
|
846
|
-
this.registerAll((_a = options == null ? void 0 : options.registeredObjects) != null ? _a : defaultRegisteredObjects);
|
|
847
|
-
}
|
|
848
|
-
dispose() {
|
|
849
|
-
this._map.dispose();
|
|
850
|
-
this._registeredMap.dispose();
|
|
851
|
-
this._symbolHandle.dispose();
|
|
852
|
-
}
|
|
853
|
-
evalCode(code) {
|
|
854
|
-
const handle = this.context.evalCode(code);
|
|
855
|
-
return this._unwrapResultAndUnmarshal(handle);
|
|
856
|
-
}
|
|
857
|
-
executePendingJobs(maxJobsToExecute) {
|
|
858
|
-
const result = this.context.runtime.executePendingJobs(maxJobsToExecute);
|
|
859
|
-
if ("value" in result) {
|
|
860
|
-
return result.value;
|
|
861
|
-
}
|
|
862
|
-
throw this._unwrapIfNotSynced(result.error.consume(this._unmarshal));
|
|
863
|
-
}
|
|
864
|
-
expose(obj) {
|
|
865
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
866
|
-
mayConsume(this._marshal(value), (handle) => {
|
|
867
|
-
this.context.setProp(this.context.global, key, handle);
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
sync(target) {
|
|
872
|
-
const wrapped = this._wrap(target);
|
|
873
|
-
if (typeof wrapped === "undefined")
|
|
874
|
-
return target;
|
|
875
|
-
walkObject(wrapped, (v) => {
|
|
876
|
-
const u = this._unwrap(v);
|
|
877
|
-
this._sync.add(u);
|
|
878
|
-
});
|
|
879
|
-
return wrapped;
|
|
880
|
-
}
|
|
881
|
-
register(target, handleOrCode) {
|
|
882
|
-
if (this._registeredMap.has(target))
|
|
883
|
-
return;
|
|
884
|
-
const handle = typeof handleOrCode === "string" ? this._unwrapResult(this.context.evalCode(handleOrCode)) : handleOrCode;
|
|
885
|
-
if (eq(this.context, handle, this.context.undefined))
|
|
886
|
-
return;
|
|
887
|
-
if (typeof handleOrCode === "string") {
|
|
888
|
-
this._registeredMapDispose.add(target);
|
|
889
|
-
}
|
|
890
|
-
this._registeredMap.set(target, handle);
|
|
891
|
-
}
|
|
892
|
-
registerAll(map) {
|
|
893
|
-
for (const [k, v] of map) {
|
|
894
|
-
this.register(k, v);
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
unregister(target, dispose) {
|
|
898
|
-
this._registeredMap.delete(target, this._registeredMapDispose.has(target) || dispose);
|
|
899
|
-
this._registeredMapDispose.delete(target);
|
|
900
|
-
}
|
|
901
|
-
unregisterAll(targets, dispose) {
|
|
902
|
-
for (const t of targets) {
|
|
903
|
-
this.unregister(t, dispose);
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
startSync(target) {
|
|
907
|
-
if (!isObject(target))
|
|
908
|
-
return;
|
|
909
|
-
const u = this._unwrap(target);
|
|
910
|
-
this._sync.add(u);
|
|
911
|
-
}
|
|
912
|
-
endSync(target) {
|
|
913
|
-
this._sync.delete(this._unwrap(target));
|
|
914
|
-
}
|
|
915
|
-
_unwrapResult(result) {
|
|
916
|
-
if ("value" in result) {
|
|
917
|
-
return result.value;
|
|
918
|
-
}
|
|
919
|
-
throw this._unwrapIfNotSynced(result.error.consume(this._unmarshal));
|
|
920
|
-
}
|
|
921
|
-
_unwrapResultAndUnmarshal(result) {
|
|
922
|
-
if (!result)
|
|
923
|
-
return;
|
|
924
|
-
return this._unwrapIfNotSynced(this._unwrapResult(result).consume(this._unmarshal));
|
|
925
|
-
}
|
|
926
|
-
_register(t, h, map = this._map, sync) {
|
|
927
|
-
if (this._registeredMap.has(t) || this._registeredMap.hasHandle(h)) {
|
|
928
|
-
return;
|
|
929
|
-
}
|
|
930
|
-
let wrappedT = this._wrap(t);
|
|
931
|
-
const [wrappedH] = this._wrapHandle(h);
|
|
932
|
-
const isPromise = t instanceof Promise;
|
|
933
|
-
if (!wrappedH || !wrappedT && !isPromise)
|
|
934
|
-
return;
|
|
935
|
-
if (isPromise)
|
|
936
|
-
wrappedT = t;
|
|
937
|
-
const unwrappedT = this._unwrap(t);
|
|
938
|
-
const [unwrappedH, unwrapped] = this._unwrapHandle(h);
|
|
939
|
-
const res = map.set(wrappedT, wrappedH, unwrappedT, unwrappedH);
|
|
940
|
-
if (!res) {
|
|
941
|
-
if (unwrapped)
|
|
942
|
-
unwrappedH.dispose();
|
|
943
|
-
throw new Error("already registered");
|
|
944
|
-
} else if (sync) {
|
|
945
|
-
this._sync.add(unwrappedT);
|
|
946
|
-
}
|
|
947
|
-
return [wrappedT, wrappedH];
|
|
948
|
-
}
|
|
949
|
-
_wrap(target) {
|
|
950
|
-
return wrap(this.context, target, this._symbol, this._symbolHandle, this._marshal, this._syncMode);
|
|
951
|
-
}
|
|
952
|
-
_unwrap(target) {
|
|
953
|
-
return unwrap(target, this._symbol);
|
|
954
|
-
}
|
|
955
|
-
_wrapHandle(handle) {
|
|
956
|
-
return wrapHandle(this.context, handle, this._symbol, this._symbolHandle, this._unmarshal, this._syncMode);
|
|
957
|
-
}
|
|
958
|
-
_unwrapHandle(target) {
|
|
959
|
-
return unwrapHandle(this.context, target, this._symbolHandle);
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
export { Arena, VMMap, call, complexity, consumeAll, defaultRegisteredObjects, eq, isES2015Class, isHandleObject, isObject, json, marshal, unmarshal, walkObject };
|