quickjs-emscripten-sync 1.1.0 → 1.4.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/LICENSE +2 -2
- package/README.md +59 -49
- package/dist/index.d.ts +121 -26
- package/dist/quickjs-emscripten-sync.es.js +962 -0
- package/dist/quickjs-emscripten-sync.umd.js +64 -0
- package/package.json +28 -40
- package/src/default.ts +2 -2
- package/src/index.test.ts +371 -69
- package/src/index.ts +135 -72
- package/src/marshal/function.test.ts +70 -61
- package/src/marshal/function.ts +10 -9
- package/src/marshal/index.test.ts +135 -62
- package/src/marshal/index.ts +33 -16
- package/src/marshal/json.test.ts +94 -0
- package/src/marshal/json.ts +16 -0
- package/src/marshal/object.test.ts +65 -58
- package/src/marshal/object.ts +6 -5
- package/src/marshal/primitive.test.ts +23 -17
- package/src/marshal/primitive.ts +10 -9
- package/src/marshal/promise.test.ts +86 -0
- package/src/marshal/promise.ts +26 -0
- package/src/marshal/properties.test.ts +23 -19
- package/src/marshal/properties.ts +11 -10
- package/src/marshal/symbol.test.ts +9 -7
- package/src/marshal/symbol.ts +5 -4
- package/src/unmarshal/function.test.ts +70 -61
- package/src/unmarshal/function.ts +39 -30
- package/src/unmarshal/index.test.ts +101 -100
- package/src/unmarshal/index.ts +13 -9
- package/src/unmarshal/object.test.ts +45 -41
- package/src/unmarshal/object.ts +14 -13
- package/src/unmarshal/primitive.test.ts +20 -15
- package/src/unmarshal/primitive.ts +10 -10
- package/src/unmarshal/promise.test.ts +44 -0
- package/src/unmarshal/promise.ts +38 -0
- package/src/unmarshal/properties.test.ts +10 -8
- package/src/unmarshal/properties.ts +47 -42
- package/src/unmarshal/symbol.test.ts +9 -7
- package/src/unmarshal/symbol.ts +4 -4
- package/src/util.test.ts +23 -5
- package/src/util.ts +15 -0
- package/src/vmmap.test.ts +88 -82
- package/src/vmmap.ts +21 -17
- package/src/vmutil.test.ts +159 -53
- package/src/vmutil.ts +91 -19
- package/src/wrapper.test.ts +151 -115
- package/src/wrapper.ts +69 -48
- package/dist/default.d.ts +0 -4
- package/dist/index.js +0 -8
- package/dist/marshal/function.d.ts +0 -2
- package/dist/marshal/index.d.ts +0 -11
- package/dist/marshal/object.d.ts +0 -2
- package/dist/marshal/primitive.d.ts +0 -2
- package/dist/marshal/properties.d.ts +0 -2
- package/dist/marshal/symbol.d.ts +0 -2
- package/dist/quickjs-emscripten-sync.cjs.development.js +0 -1289
- package/dist/quickjs-emscripten-sync.cjs.development.js.map +0 -1
- package/dist/quickjs-emscripten-sync.cjs.production.min.js +0 -2
- package/dist/quickjs-emscripten-sync.cjs.production.min.js.map +0 -1
- package/dist/quickjs-emscripten-sync.esm.js +0 -1272
- package/dist/quickjs-emscripten-sync.esm.js.map +0 -1
- package/dist/unmarshal/function.d.ts +0 -2
- package/dist/unmarshal/index.d.ts +0 -9
- package/dist/unmarshal/object.d.ts +0 -2
- package/dist/unmarshal/primitive.d.ts +0 -2
- package/dist/unmarshal/properties.d.ts +0 -2
- package/dist/unmarshal/symbol.d.ts +0 -2
- package/dist/util.d.ts +0 -7
- package/dist/vmmap.d.ts +0 -35
- package/dist/vmutil.d.ts +0 -7
- package/dist/wrapper.d.ts +0 -11
package/src/wrapper.test.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { QuickJSHandle, getQuickJS } from "quickjs-emscripten";
|
|
2
|
+
import { expect, test, vi } from "vitest";
|
|
3
|
+
|
|
2
4
|
import {
|
|
3
5
|
wrap,
|
|
4
6
|
unwrap,
|
|
@@ -8,22 +10,22 @@ import {
|
|
|
8
10
|
isHandleWrapped,
|
|
9
11
|
SyncMode,
|
|
10
12
|
} from "./wrapper";
|
|
11
|
-
import { call, eq,
|
|
13
|
+
import { call, eq, json } from "./vmutil";
|
|
12
14
|
|
|
13
15
|
test("wrap, unwrap, isWrapped", async () => {
|
|
14
|
-
const
|
|
16
|
+
const ctx = (await getQuickJS()).newContext();
|
|
15
17
|
const target = { a: 1 };
|
|
16
|
-
const handle =
|
|
18
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
17
19
|
const proxyKeySymbol = Symbol();
|
|
18
|
-
const proxyKeySymbolHandle =
|
|
19
|
-
const marshal =
|
|
20
|
-
const syncMode =
|
|
20
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
21
|
+
const marshal = vi.fn();
|
|
22
|
+
const syncMode = vi.fn();
|
|
21
23
|
|
|
22
24
|
expect(isWrapped(target, proxyKeySymbol)).toBe(false);
|
|
23
25
|
expect(unwrap(target, proxyKeySymbol)).toBe(target);
|
|
24
26
|
|
|
25
27
|
const wrapped = wrap(
|
|
26
|
-
|
|
28
|
+
ctx,
|
|
27
29
|
target,
|
|
28
30
|
proxyKeySymbol,
|
|
29
31
|
proxyKeySymbolHandle,
|
|
@@ -37,25 +39,37 @@ test("wrap, unwrap, isWrapped", async () => {
|
|
|
37
39
|
expect(unwrap(wrapped, proxyKeySymbol)).toBe(target);
|
|
38
40
|
|
|
39
41
|
expect(
|
|
40
|
-
wrap(
|
|
42
|
+
wrap(ctx, wrapped, proxyKeySymbol, proxyKeySymbolHandle, marshal, syncMode)
|
|
41
43
|
).toBe(wrapped);
|
|
42
44
|
|
|
45
|
+
// promise cannot be wrapped
|
|
46
|
+
expect(
|
|
47
|
+
wrap(
|
|
48
|
+
ctx,
|
|
49
|
+
Promise.resolve(1),
|
|
50
|
+
proxyKeySymbol,
|
|
51
|
+
proxyKeySymbolHandle,
|
|
52
|
+
marshal,
|
|
53
|
+
syncMode
|
|
54
|
+
)
|
|
55
|
+
).toBeUndefined();
|
|
56
|
+
|
|
43
57
|
proxyKeySymbolHandle.dispose();
|
|
44
58
|
handle.dispose();
|
|
45
|
-
|
|
59
|
+
ctx.dispose();
|
|
46
60
|
});
|
|
47
61
|
|
|
48
62
|
test("wrap without sync", async () => {
|
|
49
|
-
const
|
|
63
|
+
const ctx = (await getQuickJS()).newContext();
|
|
50
64
|
const target = { a: 1 };
|
|
51
|
-
const handle =
|
|
65
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
52
66
|
const proxyKeySymbol = Symbol();
|
|
53
|
-
const proxyKeySymbolHandle =
|
|
54
|
-
const marshal =
|
|
55
|
-
const syncMode =
|
|
67
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
68
|
+
const marshal = vi.fn();
|
|
69
|
+
const syncMode = vi.fn();
|
|
56
70
|
|
|
57
71
|
const wrapped = wrap(
|
|
58
|
-
|
|
72
|
+
ctx,
|
|
59
73
|
target,
|
|
60
74
|
proxyKeySymbol,
|
|
61
75
|
proxyKeySymbolHandle,
|
|
@@ -70,29 +84,33 @@ test("wrap without sync", async () => {
|
|
|
70
84
|
wrapped.a = 2;
|
|
71
85
|
|
|
72
86
|
expect(target.a).toBe(2);
|
|
73
|
-
expect(
|
|
87
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(1); // not synced
|
|
74
88
|
expect(marshal).toBeCalledTimes(0);
|
|
75
89
|
expect(syncMode).toBeCalledTimes(1);
|
|
76
90
|
expect(syncMode).toBeCalledWith(wrapped);
|
|
77
91
|
|
|
78
92
|
proxyKeySymbolHandle.dispose();
|
|
79
93
|
handle.dispose();
|
|
80
|
-
|
|
94
|
+
ctx.dispose();
|
|
95
|
+
|
|
96
|
+
wrapped.a = 3; // no error even if vm is disposed
|
|
97
|
+
expect(wrapped.a).toBe(3);
|
|
81
98
|
});
|
|
82
99
|
|
|
83
100
|
test("wrap with both sync", async () => {
|
|
84
|
-
const
|
|
101
|
+
const ctx = (await getQuickJS()).newContext();
|
|
85
102
|
const target = { a: 1 } as { a?: number };
|
|
86
|
-
const handle =
|
|
103
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
87
104
|
const proxyKeySymbol = Symbol();
|
|
88
|
-
const proxyKeySymbolHandle =
|
|
89
|
-
const marshal =
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
106
|
+
const marshal = vi.fn((t: any): [QuickJSHandle, boolean] => [
|
|
107
|
+
t === wrapped ? handle : json(ctx, t),
|
|
108
|
+
false,
|
|
109
|
+
]);
|
|
110
|
+
const syncMode = vi.fn((): SyncMode => "both");
|
|
93
111
|
|
|
94
112
|
const wrapped = wrap(
|
|
95
|
-
|
|
113
|
+
ctx,
|
|
96
114
|
target,
|
|
97
115
|
proxyKeySymbol,
|
|
98
116
|
proxyKeySymbolHandle,
|
|
@@ -107,7 +125,7 @@ test("wrap with both sync", async () => {
|
|
|
107
125
|
wrapped.a = 2;
|
|
108
126
|
|
|
109
127
|
expect(target.a).toBe(2);
|
|
110
|
-
expect(
|
|
128
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(2); // synced
|
|
111
129
|
expect(marshal).toBeCalledTimes(3);
|
|
112
130
|
expect(marshal).toBeCalledWith(2);
|
|
113
131
|
expect(marshal).toBeCalledWith("a");
|
|
@@ -116,26 +134,30 @@ test("wrap with both sync", async () => {
|
|
|
116
134
|
|
|
117
135
|
delete wrapped.a;
|
|
118
136
|
expect(target.a).toBe(undefined);
|
|
119
|
-
expect(
|
|
137
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(undefined); // synced
|
|
120
138
|
|
|
121
139
|
proxyKeySymbolHandle.dispose();
|
|
122
140
|
handle.dispose();
|
|
123
|
-
|
|
141
|
+
ctx.dispose();
|
|
142
|
+
|
|
143
|
+
wrapped.a = 3; // no error even if vm is disposed
|
|
144
|
+
expect(wrapped.a).toBe(3);
|
|
124
145
|
});
|
|
125
146
|
|
|
126
147
|
test("wrap with vm sync", async () => {
|
|
127
|
-
const
|
|
148
|
+
const ctx = (await getQuickJS()).newContext();
|
|
128
149
|
const target = { a: 1 } as { a?: number };
|
|
129
|
-
const handle =
|
|
150
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
130
151
|
const proxyKeySymbol = Symbol();
|
|
131
|
-
const proxyKeySymbolHandle =
|
|
132
|
-
const marshal =
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
152
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
153
|
+
const marshal = vi.fn((t: any): [QuickJSHandle, boolean] => [
|
|
154
|
+
t === wrapped ? handle : json(ctx, t),
|
|
155
|
+
false,
|
|
156
|
+
]);
|
|
157
|
+
const syncMode = vi.fn((): SyncMode => "vm");
|
|
136
158
|
|
|
137
159
|
const wrapped = wrap(
|
|
138
|
-
|
|
160
|
+
ctx,
|
|
139
161
|
target,
|
|
140
162
|
proxyKeySymbol,
|
|
141
163
|
proxyKeySymbolHandle,
|
|
@@ -150,7 +172,7 @@ test("wrap with vm sync", async () => {
|
|
|
150
172
|
wrapped.a = 2;
|
|
151
173
|
|
|
152
174
|
expect(target.a).toBe(1); // not set
|
|
153
|
-
expect(
|
|
175
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(2); // synced
|
|
154
176
|
expect(marshal).toBeCalledTimes(3);
|
|
155
177
|
expect(marshal).toBeCalledWith(2);
|
|
156
178
|
expect(marshal).toBeCalledWith("a");
|
|
@@ -159,30 +181,33 @@ test("wrap with vm sync", async () => {
|
|
|
159
181
|
|
|
160
182
|
delete wrapped.a;
|
|
161
183
|
expect(target.a).toBe(1); // not deleted
|
|
162
|
-
expect(
|
|
184
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(undefined); // synced
|
|
163
185
|
|
|
164
186
|
proxyKeySymbolHandle.dispose();
|
|
165
187
|
handle.dispose();
|
|
166
|
-
|
|
188
|
+
ctx.dispose();
|
|
189
|
+
|
|
190
|
+
wrapped.a = 3; // no error even after vm is disposed
|
|
191
|
+
expect(wrapped.a).toBe(1); // vm mode cannot modify obj in host
|
|
167
192
|
});
|
|
168
193
|
|
|
169
194
|
test("wrapHandle, unwrapHandle, isHandleWrapped", async () => {
|
|
170
|
-
const
|
|
195
|
+
const ctx = (await getQuickJS()).newContext();
|
|
171
196
|
const target = { a: 1 };
|
|
172
|
-
const handle =
|
|
197
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
173
198
|
const proxyKeySymbol = Symbol();
|
|
174
|
-
const proxyKeySymbolHandle =
|
|
175
|
-
const unmarshal =
|
|
176
|
-
const syncMode =
|
|
199
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
200
|
+
const unmarshal = vi.fn();
|
|
201
|
+
const syncMode = vi.fn();
|
|
177
202
|
|
|
178
|
-
expect(isHandleWrapped(
|
|
179
|
-
expect(unwrapHandle(
|
|
203
|
+
expect(isHandleWrapped(ctx, handle, proxyKeySymbolHandle)).toBe(false);
|
|
204
|
+
expect(unwrapHandle(ctx, handle, proxyKeySymbolHandle)).toEqual([
|
|
180
205
|
handle,
|
|
181
206
|
false,
|
|
182
207
|
]);
|
|
183
208
|
|
|
184
209
|
const [wrapped, w] = wrapHandle(
|
|
185
|
-
|
|
210
|
+
ctx,
|
|
186
211
|
handle,
|
|
187
212
|
proxyKeySymbol,
|
|
188
213
|
proxyKeySymbolHandle,
|
|
@@ -191,18 +216,22 @@ test("wrapHandle, unwrapHandle, isHandleWrapped", async () => {
|
|
|
191
216
|
);
|
|
192
217
|
if (!wrapped || !w) throw new Error("wrapped is undefined");
|
|
193
218
|
|
|
194
|
-
expect(
|
|
195
|
-
expect(
|
|
196
|
-
expect(isHandleWrapped(
|
|
219
|
+
expect(ctx.dump(wrapped)).toEqual(target); // vm.dump does not support proxies
|
|
220
|
+
expect(ctx.dump(ctx.getProp(wrapped, "a"))).toBe(1);
|
|
221
|
+
expect(isHandleWrapped(ctx, wrapped, proxyKeySymbolHandle)).toBe(true);
|
|
197
222
|
|
|
198
|
-
const [handle2, unwrapped2] = unwrapHandle(
|
|
223
|
+
const [handle2, unwrapped2] = unwrapHandle(
|
|
224
|
+
ctx,
|
|
225
|
+
wrapped,
|
|
226
|
+
proxyKeySymbolHandle
|
|
227
|
+
);
|
|
199
228
|
expect(unwrapped2).toBe(true);
|
|
200
|
-
handle2.consume(h => {
|
|
201
|
-
expect(eq(
|
|
229
|
+
handle2.consume((h) => {
|
|
230
|
+
expect(eq(ctx, handle, h)).toBe(true);
|
|
202
231
|
});
|
|
203
232
|
|
|
204
233
|
const [wrapped2] = wrapHandle(
|
|
205
|
-
|
|
234
|
+
ctx,
|
|
206
235
|
wrapped,
|
|
207
236
|
proxyKeySymbol,
|
|
208
237
|
proxyKeySymbolHandle,
|
|
@@ -211,25 +240,32 @@ test("wrapHandle, unwrapHandle, isHandleWrapped", async () => {
|
|
|
211
240
|
);
|
|
212
241
|
expect(wrapped2 === wrapped).toBe(true);
|
|
213
242
|
|
|
243
|
+
// promise cannot be wrapped
|
|
244
|
+
const deferred = ctx.newPromise();
|
|
245
|
+
expect(isHandleWrapped(ctx, deferred.handle, proxyKeySymbolHandle)).toBe(
|
|
246
|
+
true
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
deferred.dispose();
|
|
214
250
|
wrapped.dispose();
|
|
215
251
|
handle.dispose();
|
|
216
252
|
proxyKeySymbolHandle.dispose();
|
|
217
|
-
|
|
253
|
+
ctx.dispose();
|
|
218
254
|
});
|
|
219
255
|
|
|
220
256
|
test("wrapHandle without sync", async () => {
|
|
221
|
-
const
|
|
257
|
+
const ctx = (await getQuickJS()).newContext();
|
|
222
258
|
const target = { a: 1 };
|
|
223
|
-
const handle =
|
|
259
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
224
260
|
const proxyKeySymbol = Symbol();
|
|
225
|
-
const proxyKeySymbolHandle =
|
|
226
|
-
const unmarshal =
|
|
227
|
-
wrapped && eq(
|
|
261
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
262
|
+
const unmarshal = vi.fn((h: QuickJSHandle) =>
|
|
263
|
+
wrapped && eq(ctx, h, wrapped) ? target : ctx.dump(h)
|
|
228
264
|
);
|
|
229
|
-
const syncMode =
|
|
265
|
+
const syncMode = vi.fn();
|
|
230
266
|
|
|
231
267
|
const [wrapped, w] = wrapHandle(
|
|
232
|
-
|
|
268
|
+
ctx,
|
|
233
269
|
handle,
|
|
234
270
|
proxyKeySymbol,
|
|
235
271
|
proxyKeySymbolHandle,
|
|
@@ -241,9 +277,9 @@ test("wrapHandle without sync", async () => {
|
|
|
241
277
|
expect(unmarshal).toBeCalledTimes(0);
|
|
242
278
|
expect(syncMode).toBeCalledTimes(0);
|
|
243
279
|
|
|
244
|
-
call(
|
|
280
|
+
call(ctx, `a => a.a = 2`, undefined, wrapped);
|
|
245
281
|
|
|
246
|
-
expect(
|
|
282
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(2);
|
|
247
283
|
expect(target.a).toBe(1); // not synced
|
|
248
284
|
expect(unmarshal).toBeCalledTimes(1);
|
|
249
285
|
expect(unmarshal).toReturnWith(target);
|
|
@@ -253,22 +289,22 @@ test("wrapHandle without sync", async () => {
|
|
|
253
289
|
wrapped.dispose();
|
|
254
290
|
handle.dispose();
|
|
255
291
|
proxyKeySymbolHandle.dispose();
|
|
256
|
-
|
|
292
|
+
ctx.dispose();
|
|
257
293
|
});
|
|
258
294
|
|
|
259
295
|
test("wrapHandle with both sync", async () => {
|
|
260
|
-
const
|
|
296
|
+
const ctx = (await getQuickJS()).newContext();
|
|
261
297
|
const target = { a: 1 };
|
|
262
|
-
const handle =
|
|
298
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
263
299
|
const proxyKeySymbol = Symbol();
|
|
264
|
-
const proxyKeySymbolHandle =
|
|
265
|
-
const unmarshal =
|
|
266
|
-
return wrapped && eq(
|
|
300
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
301
|
+
const unmarshal = vi.fn((h: QuickJSHandle) => {
|
|
302
|
+
return wrapped && eq(ctx, h, wrapped) ? target : ctx.dump(h);
|
|
267
303
|
});
|
|
268
|
-
const syncMode =
|
|
304
|
+
const syncMode = vi.fn((): SyncMode => "both");
|
|
269
305
|
|
|
270
306
|
const [wrapped, w] = wrapHandle(
|
|
271
|
-
|
|
307
|
+
ctx,
|
|
272
308
|
handle,
|
|
273
309
|
proxyKeySymbol,
|
|
274
310
|
proxyKeySymbolHandle,
|
|
@@ -280,9 +316,9 @@ test("wrapHandle with both sync", async () => {
|
|
|
280
316
|
expect(unmarshal).toBeCalledTimes(0);
|
|
281
317
|
expect(syncMode).toBeCalledTimes(0);
|
|
282
318
|
|
|
283
|
-
call(
|
|
319
|
+
call(ctx, `a => a.a = 2`, undefined, wrapped);
|
|
284
320
|
|
|
285
|
-
expect(
|
|
321
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(2);
|
|
286
322
|
expect(target.a).toBe(2); // synced
|
|
287
323
|
expect(unmarshal).toBeCalledTimes(4);
|
|
288
324
|
expect(unmarshal).toReturnWith(target); // twice
|
|
@@ -291,13 +327,13 @@ test("wrapHandle with both sync", async () => {
|
|
|
291
327
|
expect(syncMode).toBeCalledTimes(1);
|
|
292
328
|
expect(syncMode).toBeCalledWith(target);
|
|
293
329
|
|
|
294
|
-
call(
|
|
295
|
-
expect(
|
|
330
|
+
call(ctx, `a => { delete a.a; }`, undefined, wrapped);
|
|
331
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(undefined);
|
|
296
332
|
expect(target.a).toBe(undefined); // synced
|
|
297
333
|
|
|
298
334
|
// changing __proto__ will be blocked
|
|
299
|
-
call(
|
|
300
|
-
expect(
|
|
335
|
+
call(ctx, `a => { a.__proto__ = null; }`, undefined, wrapped);
|
|
336
|
+
expect(ctx.dump(call(ctx, `Object.getPrototypeOf`, undefined, handle))).toBe(
|
|
301
337
|
null
|
|
302
338
|
);
|
|
303
339
|
expect(Object.getPrototypeOf(target)).toBe(Object.prototype); // not changed
|
|
@@ -305,22 +341,22 @@ test("wrapHandle with both sync", async () => {
|
|
|
305
341
|
wrapped.dispose();
|
|
306
342
|
handle.dispose();
|
|
307
343
|
proxyKeySymbolHandle.dispose();
|
|
308
|
-
|
|
344
|
+
ctx.dispose();
|
|
309
345
|
});
|
|
310
346
|
|
|
311
347
|
test("wrapHandle with host sync", async () => {
|
|
312
|
-
const
|
|
348
|
+
const ctx = (await getQuickJS()).newContext();
|
|
313
349
|
const target = { a: 1 };
|
|
314
|
-
const handle =
|
|
350
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
315
351
|
const proxyKeySymbol = Symbol();
|
|
316
|
-
const proxyKeySymbolHandle =
|
|
317
|
-
const unmarshal =
|
|
318
|
-
wrapped && eq(
|
|
352
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
353
|
+
const unmarshal = vi.fn((handle: QuickJSHandle) =>
|
|
354
|
+
wrapped && eq(ctx, handle, wrapped) ? target : ctx.dump(handle)
|
|
319
355
|
);
|
|
320
|
-
const syncMode =
|
|
356
|
+
const syncMode = vi.fn((): SyncMode => "host");
|
|
321
357
|
|
|
322
358
|
const [wrapped, w] = wrapHandle(
|
|
323
|
-
|
|
359
|
+
ctx,
|
|
324
360
|
handle,
|
|
325
361
|
proxyKeySymbol,
|
|
326
362
|
proxyKeySymbolHandle,
|
|
@@ -332,9 +368,9 @@ test("wrapHandle with host sync", async () => {
|
|
|
332
368
|
expect(unmarshal).toBeCalledTimes(0);
|
|
333
369
|
expect(syncMode).toBeCalledTimes(0);
|
|
334
370
|
|
|
335
|
-
call(
|
|
371
|
+
call(ctx, `a => a.a = 2`, undefined, wrapped);
|
|
336
372
|
|
|
337
|
-
expect(
|
|
373
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(1); // not set
|
|
338
374
|
expect(target.a).toBe(2); // synced
|
|
339
375
|
expect(unmarshal).toBeCalledTimes(4);
|
|
340
376
|
expect(unmarshal).toReturnWith(target); // twice
|
|
@@ -343,33 +379,33 @@ test("wrapHandle with host sync", async () => {
|
|
|
343
379
|
expect(syncMode).toBeCalledTimes(1);
|
|
344
380
|
expect(syncMode).toBeCalledWith(target);
|
|
345
381
|
|
|
346
|
-
call(
|
|
347
|
-
expect(
|
|
382
|
+
call(ctx, `a => delete a.a`, undefined, handle);
|
|
383
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(undefined);
|
|
348
384
|
expect(target.a).toBe(2); // not synced
|
|
349
385
|
|
|
350
386
|
wrapped.dispose();
|
|
351
387
|
handle.dispose();
|
|
352
388
|
proxyKeySymbolHandle.dispose();
|
|
353
|
-
|
|
389
|
+
ctx.dispose();
|
|
354
390
|
});
|
|
355
391
|
|
|
356
392
|
test("wrap and wrapHandle", async () => {
|
|
357
|
-
const
|
|
393
|
+
const ctx = (await getQuickJS()).newContext();
|
|
358
394
|
const target = { a: 1 };
|
|
359
|
-
const handle =
|
|
395
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
360
396
|
const proxyKeySymbol = Symbol();
|
|
361
|
-
const proxyKeySymbolHandle =
|
|
362
|
-
const marshal =
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
);
|
|
366
|
-
const unmarshal =
|
|
367
|
-
wrappedHandle && eq(
|
|
397
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
398
|
+
const marshal = vi.fn((t: any): [QuickJSHandle, boolean] => [
|
|
399
|
+
wrappedHandle && t === wrapped ? wrappedHandle : json(ctx, t),
|
|
400
|
+
false,
|
|
401
|
+
]);
|
|
402
|
+
const unmarshal = vi.fn((handle: QuickJSHandle) =>
|
|
403
|
+
wrappedHandle && eq(ctx, handle, wrappedHandle) ? wrapped : ctx.dump(handle)
|
|
368
404
|
);
|
|
369
|
-
const syncMode =
|
|
405
|
+
const syncMode = vi.fn((): SyncMode => "both");
|
|
370
406
|
|
|
371
407
|
const wrapped = wrap(
|
|
372
|
-
|
|
408
|
+
ctx,
|
|
373
409
|
target,
|
|
374
410
|
proxyKeySymbol,
|
|
375
411
|
proxyKeySymbolHandle,
|
|
@@ -378,7 +414,7 @@ test("wrap and wrapHandle", async () => {
|
|
|
378
414
|
);
|
|
379
415
|
if (!wrapped) throw new Error("wrapped is undefined");
|
|
380
416
|
const [wrappedHandle, w] = wrapHandle(
|
|
381
|
-
|
|
417
|
+
ctx,
|
|
382
418
|
handle,
|
|
383
419
|
proxyKeySymbol,
|
|
384
420
|
proxyKeySymbolHandle,
|
|
@@ -387,9 +423,9 @@ test("wrap and wrapHandle", async () => {
|
|
|
387
423
|
);
|
|
388
424
|
if (!wrappedHandle || !w) throw new Error("wrappedHandle is undefined");
|
|
389
425
|
|
|
390
|
-
call(
|
|
426
|
+
call(ctx, `a => a.a = 2`, undefined, wrappedHandle);
|
|
391
427
|
|
|
392
|
-
expect(
|
|
428
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(2);
|
|
393
429
|
expect(target.a).toBe(2);
|
|
394
430
|
expect(marshal).toBeCalledTimes(0);
|
|
395
431
|
expect(unmarshal).toBeCalledTimes(4);
|
|
@@ -402,7 +438,7 @@ test("wrap and wrapHandle", async () => {
|
|
|
402
438
|
|
|
403
439
|
wrapped.a = 3;
|
|
404
440
|
|
|
405
|
-
expect(
|
|
441
|
+
expect(ctx.dump(ctx.getProp(handle, "a"))).toBe(3);
|
|
406
442
|
expect(target.a).toBe(3);
|
|
407
443
|
expect(marshal).toBeCalledTimes(3);
|
|
408
444
|
expect(marshal).toBeCalledWith(wrapped);
|
|
@@ -413,31 +449,31 @@ test("wrap and wrapHandle", async () => {
|
|
|
413
449
|
wrappedHandle.dispose();
|
|
414
450
|
handle.dispose();
|
|
415
451
|
proxyKeySymbolHandle.dispose();
|
|
416
|
-
|
|
452
|
+
ctx.dispose();
|
|
417
453
|
});
|
|
418
454
|
|
|
419
455
|
test("non object", async () => {
|
|
420
|
-
const
|
|
456
|
+
const ctx = (await getQuickJS()).newContext();
|
|
421
457
|
const target = 1;
|
|
422
|
-
const handle =
|
|
458
|
+
const handle = ctx.newNumber(1);
|
|
423
459
|
const proxyKeySymbol = Symbol();
|
|
424
|
-
const proxyKeySymbolHandle =
|
|
460
|
+
const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
|
|
425
461
|
|
|
426
462
|
expect(
|
|
427
|
-
wrap(
|
|
463
|
+
wrap(ctx, target, proxyKeySymbol, proxyKeySymbolHandle, vi.fn(), vi.fn())
|
|
428
464
|
).toBe(undefined);
|
|
429
465
|
|
|
430
466
|
expect(
|
|
431
467
|
wrapHandle(
|
|
432
|
-
|
|
468
|
+
ctx,
|
|
433
469
|
handle,
|
|
434
470
|
proxyKeySymbol,
|
|
435
471
|
proxyKeySymbolHandle,
|
|
436
|
-
|
|
437
|
-
|
|
472
|
+
vi.fn(),
|
|
473
|
+
vi.fn()
|
|
438
474
|
)
|
|
439
475
|
).toEqual([undefined, false]);
|
|
440
476
|
|
|
441
477
|
proxyKeySymbolHandle.dispose();
|
|
442
|
-
|
|
478
|
+
ctx.dispose();
|
|
443
479
|
});
|