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