quickjs-emscripten-sync 1.3.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/README.md +36 -34
- package/dist/index.d.ts +17 -15
- package/dist/quickjs-emscripten-sync.es.js +146 -135
- package/dist/quickjs-emscripten-sync.umd.js +6 -6
- package/package.json +3 -3
- package/src/index.test.ts +146 -60
- package/src/index.ts +40 -26
- package/src/marshal/function.test.ts +61 -53
- package/src/marshal/function.ts +7 -6
- package/src/marshal/index.test.ts +73 -65
- package/src/marshal/index.ts +12 -11
- package/src/marshal/json.test.ts +30 -30
- package/src/marshal/json.ts +4 -3
- package/src/marshal/object.test.ts +55 -50
- package/src/marshal/object.ts +6 -5
- package/src/marshal/primitive.test.ts +21 -17
- package/src/marshal/primitive.ts +10 -9
- package/src/marshal/promise.test.ts +14 -14
- package/src/marshal/promise.ts +3 -3
- package/src/marshal/properties.test.ts +17 -15
- package/src/marshal/properties.ts +10 -9
- package/src/marshal/symbol.test.ts +6 -6
- package/src/marshal/symbol.ts +5 -4
- package/src/unmarshal/function.test.ts +45 -43
- package/src/unmarshal/function.ts +9 -8
- package/src/unmarshal/index.test.ts +41 -40
- package/src/unmarshal/index.ts +9 -8
- package/src/unmarshal/object.test.ts +33 -31
- package/src/unmarshal/object.ts +12 -11
- package/src/unmarshal/primitive.test.ts +18 -15
- package/src/unmarshal/primitive.ts +9 -9
- package/src/unmarshal/promise.test.ts +11 -11
- package/src/unmarshal/promise.ts +9 -11
- package/src/unmarshal/properties.test.ts +6 -6
- package/src/unmarshal/properties.ts +47 -44
- package/src/unmarshal/symbol.test.ts +6 -6
- package/src/unmarshal/symbol.ts +4 -4
- package/src/util.test.ts +1 -0
- package/src/vmmap.test.ts +72 -88
- package/src/vmmap.ts +16 -16
- package/src/vmutil.test.ts +71 -73
- package/src/vmutil.ts +22 -18
- package/src/wrapper.test.ts +111 -88
- package/src/wrapper.ts +31 -27
|
@@ -5,16 +5,16 @@ import { json } from "../vmutil";
|
|
|
5
5
|
import unmarshalFunction from "./function";
|
|
6
6
|
|
|
7
7
|
test("arrow function", async () => {
|
|
8
|
-
const
|
|
9
|
-
const marshal = vi.fn((v): [QuickJSHandle, boolean] => [json(
|
|
8
|
+
const ctx = (await getQuickJS()).newContext();
|
|
9
|
+
const marshal = vi.fn((v): [QuickJSHandle, boolean] => [json(ctx, v), false]);
|
|
10
10
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
|
|
11
|
-
|
|
11
|
+
ctx.dump(v),
|
|
12
12
|
false,
|
|
13
13
|
]);
|
|
14
14
|
const preUnmarshal = vi.fn((a) => a);
|
|
15
15
|
|
|
16
|
-
const handle =
|
|
17
|
-
const func = unmarshalFunction(
|
|
16
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`(a, b) => a + b`));
|
|
17
|
+
const func = unmarshalFunction(ctx, handle, marshal, unmarshal, preUnmarshal);
|
|
18
18
|
if (!func) throw new Error("func is undefined");
|
|
19
19
|
|
|
20
20
|
expect(func(1, 2)).toBe(3);
|
|
@@ -34,30 +34,30 @@ test("arrow function", async () => {
|
|
|
34
34
|
handle.dispose();
|
|
35
35
|
expect(() => func(1, 2)).toThrow("Lifetime not alive");
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
ctx.dispose();
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
test("function", async () => {
|
|
41
|
-
const
|
|
41
|
+
const ctx = (await getQuickJS()).newContext();
|
|
42
42
|
const that = { a: 1 };
|
|
43
|
-
const thatHandle =
|
|
43
|
+
const thatHandle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
|
|
44
44
|
const marshal = vi.fn((v): [QuickJSHandle, boolean] => [
|
|
45
|
-
v === that ? thatHandle : json(
|
|
45
|
+
v === that ? thatHandle : json(ctx, v),
|
|
46
46
|
false,
|
|
47
47
|
]);
|
|
48
48
|
const disposables: QuickJSHandle[] = [];
|
|
49
49
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => {
|
|
50
|
-
const ty =
|
|
50
|
+
const ty = ctx.typeof(v);
|
|
51
51
|
if (ty === "object" || ty === "function") disposables.push(v);
|
|
52
|
-
return [
|
|
52
|
+
return [ctx.dump(v), false];
|
|
53
53
|
});
|
|
54
54
|
const preUnmarshal = vi.fn((a) => a);
|
|
55
55
|
|
|
56
|
-
const handle =
|
|
57
|
-
|
|
56
|
+
const handle = ctx.unwrapResult(
|
|
57
|
+
ctx.evalCode(`(function (a) { return this.a + a; })`)
|
|
58
58
|
);
|
|
59
59
|
|
|
60
|
-
const func = unmarshalFunction(
|
|
60
|
+
const func = unmarshalFunction(ctx, handle, marshal, unmarshal, preUnmarshal);
|
|
61
61
|
if (!func) throw new Error("func is undefined");
|
|
62
62
|
|
|
63
63
|
expect(func.call(that, 2)).toBe(3);
|
|
@@ -78,29 +78,29 @@ test("function", async () => {
|
|
|
78
78
|
disposables.forEach((d) => d.dispose());
|
|
79
79
|
thatHandle.dispose();
|
|
80
80
|
handle.dispose();
|
|
81
|
-
|
|
81
|
+
ctx.dispose();
|
|
82
82
|
});
|
|
83
83
|
|
|
84
84
|
test("constructor", async () => {
|
|
85
|
-
const
|
|
85
|
+
const ctx = (await getQuickJS()).newContext();
|
|
86
86
|
const disposables: QuickJSHandle[] = [];
|
|
87
87
|
const marshal = vi.fn((v): [QuickJSHandle, boolean] => [
|
|
88
|
-
typeof v === "object" ?
|
|
88
|
+
typeof v === "object" ? ctx.undefined : json(ctx, v),
|
|
89
89
|
false,
|
|
90
90
|
]);
|
|
91
91
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => {
|
|
92
|
-
const ty =
|
|
92
|
+
const ty = ctx.typeof(v);
|
|
93
93
|
if (ty === "object" || ty === "function") disposables.push(v);
|
|
94
|
-
return [
|
|
94
|
+
return [ctx.dump(v), false];
|
|
95
95
|
});
|
|
96
96
|
const preUnmarshal = vi.fn((a) => a);
|
|
97
97
|
|
|
98
|
-
const handle =
|
|
99
|
-
|
|
98
|
+
const handle = ctx.unwrapResult(
|
|
99
|
+
ctx.evalCode(`(function (b) { this.a = b + 2; })`)
|
|
100
100
|
);
|
|
101
101
|
|
|
102
102
|
const Cls = unmarshalFunction(
|
|
103
|
-
|
|
103
|
+
ctx,
|
|
104
104
|
handle,
|
|
105
105
|
marshal,
|
|
106
106
|
unmarshal,
|
|
@@ -127,29 +127,29 @@ test("constructor", async () => {
|
|
|
127
127
|
|
|
128
128
|
disposables.forEach((d) => d.dispose());
|
|
129
129
|
handle.dispose();
|
|
130
|
-
|
|
130
|
+
ctx.dispose();
|
|
131
131
|
});
|
|
132
132
|
|
|
133
133
|
test("class", async () => {
|
|
134
|
-
const
|
|
134
|
+
const ctx = (await getQuickJS()).newContext();
|
|
135
135
|
const marshal = vi.fn((v): [QuickJSHandle, boolean] => [
|
|
136
|
-
typeof v === "object" ?
|
|
136
|
+
typeof v === "object" ? ctx.undefined : json(ctx, v),
|
|
137
137
|
false,
|
|
138
138
|
]);
|
|
139
139
|
const disposables: QuickJSHandle[] = [];
|
|
140
140
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => {
|
|
141
|
-
const ty =
|
|
141
|
+
const ty = ctx.typeof(v);
|
|
142
142
|
if (ty === "object" || ty === "function") disposables.push(v);
|
|
143
|
-
return [
|
|
143
|
+
return [ctx.dump(v), false];
|
|
144
144
|
});
|
|
145
145
|
const preUnmarshal = vi.fn((a) => a);
|
|
146
146
|
|
|
147
|
-
const handle =
|
|
148
|
-
|
|
147
|
+
const handle = ctx.unwrapResult(
|
|
148
|
+
ctx.evalCode(`(class A { constructor(a) { this.a = a + 1; } })`)
|
|
149
149
|
);
|
|
150
150
|
|
|
151
151
|
const Cls = unmarshalFunction(
|
|
152
|
-
|
|
152
|
+
ctx,
|
|
153
153
|
handle,
|
|
154
154
|
marshal,
|
|
155
155
|
unmarshal,
|
|
@@ -176,30 +176,32 @@ test("class", async () => {
|
|
|
176
176
|
|
|
177
177
|
disposables.forEach((d) => d.dispose());
|
|
178
178
|
handle.dispose();
|
|
179
|
-
|
|
179
|
+
ctx.dispose();
|
|
180
180
|
});
|
|
181
181
|
|
|
182
182
|
test("undefined", async () => {
|
|
183
|
-
const
|
|
183
|
+
const ctx = (await getQuickJS()).newContext();
|
|
184
184
|
const f = vi.fn();
|
|
185
185
|
|
|
186
|
-
expect(unmarshalFunction(
|
|
187
|
-
expect(unmarshalFunction(
|
|
188
|
-
expect(unmarshalFunction(
|
|
189
|
-
expect(unmarshalFunction(
|
|
190
|
-
expect(unmarshalFunction(
|
|
186
|
+
expect(unmarshalFunction(ctx, ctx.undefined, f, f, f)).toEqual(undefined);
|
|
187
|
+
expect(unmarshalFunction(ctx, ctx.true, f, f, f)).toEqual(undefined);
|
|
188
|
+
expect(unmarshalFunction(ctx, ctx.false, f, f, f)).toEqual(undefined);
|
|
189
|
+
expect(unmarshalFunction(ctx, ctx.null, f, f, f)).toEqual(undefined);
|
|
190
|
+
expect(unmarshalFunction(ctx, ctx.newString("hoge"), f, f, f)).toEqual(
|
|
191
|
+
undefined
|
|
192
|
+
);
|
|
193
|
+
expect(unmarshalFunction(ctx, ctx.newNumber(-10), f, f, f)).toEqual(
|
|
191
194
|
undefined
|
|
192
195
|
);
|
|
193
|
-
expect(unmarshalFunction(vm, vm.newNumber(-10), f, f, f)).toEqual(undefined);
|
|
194
196
|
|
|
195
|
-
const obj =
|
|
196
|
-
expect(unmarshalFunction(
|
|
197
|
-
const array =
|
|
198
|
-
expect(unmarshalFunction(
|
|
197
|
+
const obj = ctx.newObject();
|
|
198
|
+
expect(unmarshalFunction(ctx, obj, f, f, f)).toEqual(undefined);
|
|
199
|
+
const array = ctx.newArray();
|
|
200
|
+
expect(unmarshalFunction(ctx, array, f, f, f)).toEqual(undefined);
|
|
199
201
|
|
|
200
202
|
expect(f).toBeCalledTimes(0);
|
|
201
203
|
|
|
202
204
|
obj.dispose();
|
|
203
205
|
array.dispose();
|
|
204
|
-
|
|
206
|
+
ctx.dispose();
|
|
205
207
|
});
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
|
|
2
|
+
|
|
2
3
|
import { call, mayConsumeAll } from "../vmutil";
|
|
3
4
|
import unmarshalProperties from "./properties";
|
|
4
5
|
|
|
5
6
|
export default function unmarshalFunction(
|
|
6
|
-
|
|
7
|
+
ctx: QuickJSContext,
|
|
7
8
|
handle: QuickJSHandle,
|
|
8
|
-
|
|
9
|
+
/** marshal returns handle and boolean indicates that the handle should be disposed after use */
|
|
9
10
|
marshal: (value: unknown) => [QuickJSHandle, boolean],
|
|
10
11
|
unmarshal: (handle: QuickJSHandle) => [unknown, boolean],
|
|
11
12
|
preUnmarshal: <T>(target: T, handle: QuickJSHandle) => T | undefined
|
|
12
13
|
): Function | undefined {
|
|
13
|
-
if (
|
|
14
|
+
if (ctx.typeof(handle) !== "function") return;
|
|
14
15
|
|
|
15
16
|
const raw = function (this: any, ...args: any[]) {
|
|
16
17
|
return mayConsumeAll(
|
|
@@ -19,7 +20,7 @@ export default function unmarshalFunction(
|
|
|
19
20
|
if (new.target) {
|
|
20
21
|
const [instance] = unmarshal(
|
|
21
22
|
call(
|
|
22
|
-
|
|
23
|
+
ctx,
|
|
23
24
|
`(Cls, ...args) => new Cls(...args)`,
|
|
24
25
|
thisHandle,
|
|
25
26
|
handle,
|
|
@@ -33,8 +34,8 @@ export default function unmarshalFunction(
|
|
|
33
34
|
return this;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
const resultHandle =
|
|
37
|
-
|
|
37
|
+
const resultHandle = ctx.unwrapResult(
|
|
38
|
+
ctx.callFunction(handle, thisHandle, ...argHandles)
|
|
38
39
|
);
|
|
39
40
|
|
|
40
41
|
const [result, alreadyExists] = unmarshal(resultHandle);
|
|
@@ -46,7 +47,7 @@ export default function unmarshalFunction(
|
|
|
46
47
|
};
|
|
47
48
|
|
|
48
49
|
const func = preUnmarshal(raw, handle) ?? raw;
|
|
49
|
-
unmarshalProperties(
|
|
50
|
+
unmarshalProperties(ctx, handle, raw, unmarshal);
|
|
50
51
|
|
|
51
52
|
return func;
|
|
52
53
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getQuickJS, QuickJSHandle } from "quickjs-emscripten";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
3
|
|
|
4
4
|
import VMMap from "../vmmap";
|
|
@@ -6,10 +6,10 @@ import unmarshal from ".";
|
|
|
6
6
|
import { json } from "../vmutil";
|
|
7
7
|
|
|
8
8
|
test("primitive, array, object", async () => {
|
|
9
|
-
const {
|
|
9
|
+
const { ctx, unmarshal, marshal, map, dispose } = await setup();
|
|
10
10
|
|
|
11
|
-
const handle =
|
|
12
|
-
|
|
11
|
+
const handle = ctx.unwrapResult(
|
|
12
|
+
ctx.evalCode(`({
|
|
13
13
|
hoge: "foo",
|
|
14
14
|
foo: 1,
|
|
15
15
|
aaa: [1, true, {}],
|
|
@@ -28,18 +28,19 @@ test("primitive, array, object", async () => {
|
|
|
28
28
|
});
|
|
29
29
|
expect(map.size).toBe(5);
|
|
30
30
|
expect(map.getByHandle(handle)).toBe(target);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
.
|
|
31
|
+
ctx
|
|
32
|
+
.getProp(handle, "aaa")
|
|
33
|
+
.consume((h) => expect(map.getByHandle(h)).toBe(target.aaa));
|
|
34
|
+
ctx
|
|
35
|
+
.getProp(handle, "aaa")
|
|
36
|
+
.consume((h) => ctx.getProp(h, 2))
|
|
36
37
|
.consume((h) => expect(map.getByHandle(h)).toBe(target.aaa[2]));
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
ctx
|
|
39
|
+
.getProp(handle, "nested")
|
|
40
|
+
.consume((h) => expect(map.getByHandle(h)).toBe(target.nested));
|
|
41
|
+
ctx
|
|
42
|
+
.getProp(handle, "bbb")
|
|
43
|
+
.consume((h) => expect(map.getByHandle(h)).toBe(target.bbb));
|
|
43
44
|
|
|
44
45
|
expect(marshal).toBeCalledTimes(0);
|
|
45
46
|
expect(target.bbb()).toBe("bar");
|
|
@@ -50,10 +51,10 @@ test("primitive, array, object", async () => {
|
|
|
50
51
|
});
|
|
51
52
|
|
|
52
53
|
test("object with symbol key", async () => {
|
|
53
|
-
const {
|
|
54
|
+
const { ctx, unmarshal, dispose } = await setup();
|
|
54
55
|
|
|
55
|
-
const handle =
|
|
56
|
-
|
|
56
|
+
const handle = ctx.unwrapResult(
|
|
57
|
+
ctx.evalCode(`({
|
|
57
58
|
hoge: "foo",
|
|
58
59
|
[Symbol("a")]: "bar"
|
|
59
60
|
})`)
|
|
@@ -67,10 +68,10 @@ test("object with symbol key", async () => {
|
|
|
67
68
|
});
|
|
68
69
|
|
|
69
70
|
test("function", async () => {
|
|
70
|
-
const {
|
|
71
|
+
const { ctx, unmarshal, marshal, map, dispose } = await setup();
|
|
71
72
|
|
|
72
|
-
const handle =
|
|
73
|
-
|
|
73
|
+
const handle = ctx.unwrapResult(
|
|
74
|
+
ctx.evalCode(`(function(a) { return a.a + "!"; })`)
|
|
74
75
|
);
|
|
75
76
|
const func = unmarshal(handle);
|
|
76
77
|
const arg = { a: "hoge" };
|
|
@@ -88,18 +89,18 @@ test("function", async () => {
|
|
|
88
89
|
});
|
|
89
90
|
|
|
90
91
|
test("promise", async () => {
|
|
91
|
-
const {
|
|
92
|
+
const { ctx, unmarshal, dispose } = await setup();
|
|
92
93
|
|
|
93
|
-
const deferred =
|
|
94
|
+
const deferred = ctx.newPromise();
|
|
94
95
|
const promise = unmarshal(deferred.handle);
|
|
95
|
-
deferred.resolve(
|
|
96
|
-
|
|
96
|
+
deferred.resolve(ctx.newString("resolved!"));
|
|
97
|
+
ctx.runtime.executePendingJobs();
|
|
97
98
|
await expect(promise).resolves.toBe("resolved!");
|
|
98
99
|
|
|
99
|
-
const deferred2 =
|
|
100
|
+
const deferred2 = ctx.newPromise();
|
|
100
101
|
const promise2 = unmarshal(deferred2.handle);
|
|
101
|
-
deferred2.reject(
|
|
102
|
-
|
|
102
|
+
deferred2.reject(ctx.newString("rejected!"));
|
|
103
|
+
ctx.runtime.executePendingJobs();
|
|
103
104
|
await expect(promise2).rejects.toBe("rejected!");
|
|
104
105
|
|
|
105
106
|
deferred.dispose();
|
|
@@ -108,10 +109,10 @@ test("promise", async () => {
|
|
|
108
109
|
});
|
|
109
110
|
|
|
110
111
|
test("class", async () => {
|
|
111
|
-
const {
|
|
112
|
+
const { ctx, unmarshal, dispose } = await setup();
|
|
112
113
|
|
|
113
|
-
const handle =
|
|
114
|
-
|
|
114
|
+
const handle = ctx.unwrapResult(
|
|
115
|
+
ctx.evalCode(`{
|
|
115
116
|
class Cls {
|
|
116
117
|
static hoge = "foo";
|
|
117
118
|
|
|
@@ -138,8 +139,8 @@ test("class", async () => {
|
|
|
138
139
|
});
|
|
139
140
|
|
|
140
141
|
const setup = async () => {
|
|
141
|
-
const
|
|
142
|
-
const map = new VMMap(
|
|
142
|
+
const ctx = (await getQuickJS()).newContext();
|
|
143
|
+
const map = new VMMap(ctx);
|
|
143
144
|
const disposables: QuickJSHandle[] = [];
|
|
144
145
|
const marshal = vi.fn((target: unknown): [QuickJSHandle, boolean] => {
|
|
145
146
|
const handle = map.get(target);
|
|
@@ -147,17 +148,17 @@ const setup = async () => {
|
|
|
147
148
|
|
|
148
149
|
const handle2 =
|
|
149
150
|
typeof target === "function"
|
|
150
|
-
?
|
|
151
|
-
target(...handles.map((h) =>
|
|
151
|
+
? ctx.newFunction(target.name, (...handles) => {
|
|
152
|
+
target(...handles.map((h) => ctx.dump(h)));
|
|
152
153
|
})
|
|
153
|
-
: json(
|
|
154
|
-
const ty =
|
|
154
|
+
: json(ctx, target);
|
|
155
|
+
const ty = ctx.typeof(handle2);
|
|
155
156
|
if (ty === "object" || ty === "function") map.set(target, handle2);
|
|
156
157
|
return [handle2, false];
|
|
157
158
|
});
|
|
158
159
|
|
|
159
160
|
return {
|
|
160
|
-
|
|
161
|
+
ctx,
|
|
161
162
|
map,
|
|
162
163
|
unmarshal: (handle: QuickJSHandle) =>
|
|
163
164
|
unmarshal(handle, {
|
|
@@ -167,13 +168,13 @@ const setup = async () => {
|
|
|
167
168
|
map.set(t, h);
|
|
168
169
|
return t;
|
|
169
170
|
},
|
|
170
|
-
|
|
171
|
+
ctx: ctx,
|
|
171
172
|
}),
|
|
172
173
|
marshal,
|
|
173
174
|
dispose: () => {
|
|
174
175
|
disposables.forEach((d) => d.dispose());
|
|
175
176
|
map.dispose();
|
|
176
|
-
|
|
177
|
+
ctx.dispose();
|
|
177
178
|
},
|
|
178
179
|
};
|
|
179
180
|
};
|
package/src/unmarshal/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
|
|
2
|
+
|
|
2
3
|
import unmarshalFunction from "./function";
|
|
3
4
|
import unmarshalObject from "./object";
|
|
4
5
|
import unmarshalPrimitive from "./primitive";
|
|
@@ -6,7 +7,7 @@ import unmarshalPromise from "./promise";
|
|
|
6
7
|
import unmarshalSymbol from "./symbol";
|
|
7
8
|
|
|
8
9
|
export type Options = {
|
|
9
|
-
|
|
10
|
+
ctx: QuickJSContext;
|
|
10
11
|
/** marshal returns handle and boolean indicates that the handle should be disposed after use */
|
|
11
12
|
marshal: (target: unknown) => [QuickJSHandle, boolean];
|
|
12
13
|
find: (handle: QuickJSHandle) => unknown | undefined;
|
|
@@ -22,10 +23,10 @@ function unmarshalInner(
|
|
|
22
23
|
handle: QuickJSHandle,
|
|
23
24
|
options: Options
|
|
24
25
|
): [any, boolean] {
|
|
25
|
-
const {
|
|
26
|
+
const { ctx, marshal, find, pre } = options;
|
|
26
27
|
|
|
27
28
|
{
|
|
28
|
-
const [target, ok] = unmarshalPrimitive(
|
|
29
|
+
const [target, ok] = unmarshalPrimitive(ctx, handle);
|
|
29
30
|
if (ok) return [target, false];
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -39,10 +40,10 @@ function unmarshalInner(
|
|
|
39
40
|
const unmarshal2 = (h: QuickJSHandle) => unmarshalInner(h, options);
|
|
40
41
|
|
|
41
42
|
const result =
|
|
42
|
-
unmarshalSymbol(
|
|
43
|
-
unmarshalPromise(
|
|
44
|
-
unmarshalFunction(
|
|
45
|
-
unmarshalObject(
|
|
43
|
+
unmarshalSymbol(ctx, handle, pre) ??
|
|
44
|
+
unmarshalPromise(ctx, handle, marshal, pre) ??
|
|
45
|
+
unmarshalFunction(ctx, handle, marshal, unmarshal2, pre) ??
|
|
46
|
+
unmarshalObject(ctx, handle, unmarshal2, pre);
|
|
46
47
|
|
|
47
48
|
return [result, false];
|
|
48
49
|
}
|
|
@@ -4,15 +4,15 @@ import { expect, test, vi } from "vitest";
|
|
|
4
4
|
import unmarshalObject from "./object";
|
|
5
5
|
|
|
6
6
|
test("normal object", async () => {
|
|
7
|
-
const
|
|
7
|
+
const ctx = (await getQuickJS()).newContext();
|
|
8
8
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
|
|
9
|
-
|
|
9
|
+
ctx.dump(v),
|
|
10
10
|
false,
|
|
11
11
|
]);
|
|
12
12
|
const preUnmarshal = vi.fn((a) => a);
|
|
13
13
|
|
|
14
|
-
const handle =
|
|
15
|
-
const obj = unmarshalObject(
|
|
14
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`({ a: 1, b: true })`));
|
|
15
|
+
const obj = unmarshalObject(ctx, handle, unmarshal, preUnmarshal);
|
|
16
16
|
if (!obj) throw new Error("obj is undefined");
|
|
17
17
|
expect(obj).toEqual({ a: 1, b: true });
|
|
18
18
|
expect(unmarshal).toReturnTimes(4);
|
|
@@ -24,20 +24,20 @@ test("normal object", async () => {
|
|
|
24
24
|
expect(preUnmarshal).toBeCalledWith(obj, handle);
|
|
25
25
|
|
|
26
26
|
handle.dispose();
|
|
27
|
-
|
|
27
|
+
ctx.dispose();
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
test("properties", async () => {
|
|
31
|
-
const
|
|
31
|
+
const ctx = (await getQuickJS()).newContext();
|
|
32
32
|
const disposables: QuickJSHandle[] = [];
|
|
33
33
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => {
|
|
34
34
|
disposables.push(v);
|
|
35
|
-
return [
|
|
35
|
+
return [ctx.typeof(v) === "function" ? () => {} : ctx.dump(v), false];
|
|
36
36
|
});
|
|
37
37
|
const preUnmarshal = vi.fn((a) => a);
|
|
38
38
|
|
|
39
|
-
const handle =
|
|
40
|
-
|
|
39
|
+
const handle = ctx.unwrapResult(
|
|
40
|
+
ctx.evalCode(`{
|
|
41
41
|
const obj = {};
|
|
42
42
|
Object.defineProperties(obj, {
|
|
43
43
|
a: { value: 1, writable: true, configurable: true, enumerable: true },
|
|
@@ -47,7 +47,7 @@ test("properties", async () => {
|
|
|
47
47
|
obj
|
|
48
48
|
}`)
|
|
49
49
|
);
|
|
50
|
-
const obj = unmarshalObject(
|
|
50
|
+
const obj = unmarshalObject(ctx, handle, unmarshal, preUnmarshal);
|
|
51
51
|
if (!obj) throw new Error("obj is undefined");
|
|
52
52
|
expect(obj).toEqual({
|
|
53
53
|
a: 1,
|
|
@@ -74,19 +74,19 @@ test("properties", async () => {
|
|
|
74
74
|
|
|
75
75
|
disposables.forEach((d) => d.dispose());
|
|
76
76
|
handle.dispose();
|
|
77
|
-
|
|
77
|
+
ctx.dispose();
|
|
78
78
|
});
|
|
79
79
|
|
|
80
80
|
test("array", async () => {
|
|
81
|
-
const
|
|
81
|
+
const ctx = (await getQuickJS()).newContext();
|
|
82
82
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
|
|
83
|
-
|
|
83
|
+
ctx.dump(v),
|
|
84
84
|
false,
|
|
85
85
|
]);
|
|
86
86
|
const preUnmarshal = vi.fn((a) => a);
|
|
87
87
|
|
|
88
|
-
const handle =
|
|
89
|
-
const array = unmarshalObject(
|
|
88
|
+
const handle = ctx.unwrapResult(ctx.evalCode(`[1, true, "a"]`));
|
|
89
|
+
const array = unmarshalObject(ctx, handle, unmarshal, preUnmarshal);
|
|
90
90
|
expect((array as any)[0]).toEqual(1);
|
|
91
91
|
expect(Array.isArray(array)).toBe(true);
|
|
92
92
|
expect(unmarshal.mock.results[0].value).toEqual(["0", false]);
|
|
@@ -100,19 +100,21 @@ test("array", async () => {
|
|
|
100
100
|
expect(preUnmarshal).toBeCalledWith(array, handle);
|
|
101
101
|
|
|
102
102
|
handle.dispose();
|
|
103
|
-
|
|
103
|
+
ctx.dispose();
|
|
104
104
|
});
|
|
105
105
|
|
|
106
106
|
test("prototype", async () => {
|
|
107
|
-
const
|
|
107
|
+
const ctx = (await getQuickJS()).newContext();
|
|
108
108
|
const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
|
|
109
|
-
|
|
109
|
+
ctx.typeof(v) === "object" ? { a: () => 1 } : ctx.dump(v),
|
|
110
110
|
false,
|
|
111
111
|
]);
|
|
112
112
|
const preUnmarshal = vi.fn((a) => a);
|
|
113
113
|
|
|
114
|
-
const handle =
|
|
115
|
-
|
|
114
|
+
const handle = ctx.unwrapResult(
|
|
115
|
+
ctx.evalCode(`Object.create({ a: () => 1 })`)
|
|
116
|
+
);
|
|
117
|
+
const obj = unmarshalObject(ctx, handle, unmarshal, preUnmarshal) as any;
|
|
116
118
|
if (!obj) throw new Error("obj is undefined");
|
|
117
119
|
expect(Object.getPrototypeOf(obj)).toEqual({ a: expect.any(Function) });
|
|
118
120
|
expect(obj.a()).toBe(1);
|
|
@@ -122,25 +124,25 @@ test("prototype", async () => {
|
|
|
122
124
|
expect(preUnmarshal).toBeCalledWith(obj, handle);
|
|
123
125
|
|
|
124
126
|
handle.dispose();
|
|
125
|
-
|
|
127
|
+
ctx.dispose();
|
|
126
128
|
});
|
|
127
129
|
|
|
128
130
|
test("undefined", async () => {
|
|
129
|
-
const
|
|
131
|
+
const ctx = (await getQuickJS()).newContext();
|
|
130
132
|
const f = vi.fn();
|
|
131
133
|
|
|
132
|
-
expect(unmarshalObject(
|
|
133
|
-
expect(unmarshalObject(
|
|
134
|
-
expect(unmarshalObject(
|
|
135
|
-
expect(unmarshalObject(
|
|
136
|
-
expect(unmarshalObject(
|
|
137
|
-
expect(unmarshalObject(
|
|
134
|
+
expect(unmarshalObject(ctx, ctx.undefined, f, f)).toEqual(undefined);
|
|
135
|
+
expect(unmarshalObject(ctx, ctx.true, f, f)).toEqual(undefined);
|
|
136
|
+
expect(unmarshalObject(ctx, ctx.false, f, f)).toEqual(undefined);
|
|
137
|
+
expect(unmarshalObject(ctx, ctx.null, f, f)).toEqual(undefined);
|
|
138
|
+
expect(unmarshalObject(ctx, ctx.newString("hoge"), f, f)).toEqual(undefined);
|
|
139
|
+
expect(unmarshalObject(ctx, ctx.newNumber(-10), f, f)).toEqual(undefined);
|
|
138
140
|
|
|
139
|
-
const func =
|
|
140
|
-
expect(unmarshalObject(
|
|
141
|
+
const func = ctx.newFunction("", () => {});
|
|
142
|
+
expect(unmarshalObject(ctx, func, f, f)).toEqual(undefined);
|
|
141
143
|
|
|
142
144
|
expect(f).toBeCalledTimes(0);
|
|
143
145
|
|
|
144
146
|
func.dispose();
|
|
145
|
-
|
|
147
|
+
ctx.dispose();
|
|
146
148
|
});
|
package/src/unmarshal/object.ts
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
|
|
2
|
+
|
|
2
3
|
import unmarshalProperties from "./properties";
|
|
3
4
|
import { call } from "../vmutil";
|
|
4
5
|
|
|
5
6
|
export default function unmarshalObject(
|
|
6
|
-
|
|
7
|
+
ctx: QuickJSContext,
|
|
7
8
|
handle: QuickJSHandle,
|
|
8
9
|
unmarshal: (handle: QuickJSHandle) => [unknown, boolean],
|
|
9
10
|
preUnmarshal: <T>(target: T, handle: QuickJSHandle) => T | undefined
|
|
10
11
|
): object | undefined {
|
|
11
12
|
if (
|
|
12
|
-
|
|
13
|
+
ctx.typeof(handle) !== "object" ||
|
|
13
14
|
// null check
|
|
14
|
-
|
|
15
|
-
.unwrapResult(
|
|
15
|
+
ctx
|
|
16
|
+
.unwrapResult(ctx.evalCode("o => o === null"))
|
|
16
17
|
.consume((n) =>
|
|
17
|
-
|
|
18
|
+
ctx.dump(ctx.unwrapResult(ctx.callFunction(n, ctx.undefined, handle)))
|
|
18
19
|
)
|
|
19
20
|
)
|
|
20
21
|
return;
|
|
21
22
|
|
|
22
|
-
const raw = call(
|
|
23
|
-
|
|
23
|
+
const raw = call(ctx, "Array.isArray", undefined, handle).consume((r) =>
|
|
24
|
+
ctx.dump(r)
|
|
24
25
|
)
|
|
25
26
|
? []
|
|
26
27
|
: {};
|
|
27
28
|
const obj = preUnmarshal(raw, handle) ?? raw;
|
|
28
29
|
|
|
29
30
|
const prototype = call(
|
|
30
|
-
|
|
31
|
+
ctx,
|
|
31
32
|
`o => {
|
|
32
33
|
const p = Object.getPrototypeOf(o);
|
|
33
34
|
return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
|
|
@@ -35,7 +36,7 @@ export default function unmarshalObject(
|
|
|
35
36
|
undefined,
|
|
36
37
|
handle
|
|
37
38
|
).consume((prototype) => {
|
|
38
|
-
if (
|
|
39
|
+
if (ctx.typeof(prototype) === "undefined") return;
|
|
39
40
|
const [proto] = unmarshal(prototype);
|
|
40
41
|
return proto;
|
|
41
42
|
});
|
|
@@ -43,7 +44,7 @@ export default function unmarshalObject(
|
|
|
43
44
|
Object.setPrototypeOf(obj, prototype);
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
unmarshalProperties(
|
|
47
|
+
unmarshalProperties(ctx, handle, raw, unmarshal);
|
|
47
48
|
|
|
48
49
|
return obj;
|
|
49
50
|
}
|