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
package/src/vmutil.test.ts
CHANGED
|
@@ -15,16 +15,17 @@ import {
|
|
|
15
15
|
} from "./vmutil";
|
|
16
16
|
|
|
17
17
|
test("fn", async () => {
|
|
18
|
-
const
|
|
19
|
-
const vm = quickjs.createVm();
|
|
18
|
+
const ctx = (await getQuickJS()).newContext();
|
|
20
19
|
|
|
21
|
-
const f = fn(
|
|
22
|
-
expect(
|
|
20
|
+
const f = fn(ctx, "(a, b) => a + b");
|
|
21
|
+
expect(ctx.getNumber(f(undefined, ctx.newNumber(1), ctx.newNumber(2)))).toBe(
|
|
22
|
+
3
|
|
23
|
+
);
|
|
23
24
|
|
|
24
|
-
const obj =
|
|
25
|
-
|
|
26
|
-
const f2 = fn(
|
|
27
|
-
expect(
|
|
25
|
+
const obj = ctx.newObject();
|
|
26
|
+
ctx.setProp(obj, "a", ctx.newNumber(2));
|
|
27
|
+
const f2 = fn(ctx, "(function() { return this.a + 1; })");
|
|
28
|
+
expect(ctx.getNumber(f2(obj))).toBe(3);
|
|
28
29
|
|
|
29
30
|
obj.dispose();
|
|
30
31
|
expect(f.alive).toBe(true);
|
|
@@ -33,104 +34,104 @@ test("fn", async () => {
|
|
|
33
34
|
f.dispose();
|
|
34
35
|
expect(f.alive).toBe(false);
|
|
35
36
|
expect(f2.alive).toBe(false);
|
|
36
|
-
|
|
37
|
+
ctx.dispose();
|
|
37
38
|
});
|
|
38
39
|
|
|
39
40
|
test("call", async () => {
|
|
40
|
-
const
|
|
41
|
-
const vm = quickjs.createVm();
|
|
41
|
+
const ctx = (await getQuickJS()).newContext();
|
|
42
42
|
|
|
43
43
|
expect(
|
|
44
|
-
|
|
45
|
-
call(
|
|
44
|
+
ctx.getNumber(
|
|
45
|
+
call(
|
|
46
|
+
ctx,
|
|
47
|
+
"(a, b) => a + b",
|
|
48
|
+
undefined,
|
|
49
|
+
ctx.newNumber(1),
|
|
50
|
+
ctx.newNumber(2)
|
|
51
|
+
)
|
|
46
52
|
)
|
|
47
53
|
).toBe(3);
|
|
48
54
|
|
|
49
|
-
const obj =
|
|
50
|
-
|
|
55
|
+
const obj = ctx.newObject();
|
|
56
|
+
ctx.setProp(obj, "a", ctx.newNumber(2));
|
|
51
57
|
expect(
|
|
52
|
-
|
|
58
|
+
ctx.getNumber(call(ctx, "(function() { return this.a + 1; })", obj))
|
|
53
59
|
).toBe(3);
|
|
54
60
|
|
|
55
61
|
obj.dispose();
|
|
56
|
-
|
|
62
|
+
ctx.dispose();
|
|
57
63
|
});
|
|
58
64
|
|
|
59
65
|
test("eq", async () => {
|
|
60
|
-
const
|
|
61
|
-
const vm = quickjs.createVm();
|
|
66
|
+
const ctx = (await getQuickJS()).newContext();
|
|
62
67
|
|
|
63
|
-
const math1 =
|
|
64
|
-
const math2 =
|
|
65
|
-
const obj =
|
|
68
|
+
const math1 = ctx.unwrapResult(ctx.evalCode("Math"));
|
|
69
|
+
const math2 = ctx.unwrapResult(ctx.evalCode("Math"));
|
|
70
|
+
const obj = ctx.newObject();
|
|
66
71
|
expect(math1 === math2).toBe(false);
|
|
67
|
-
expect(eq(
|
|
68
|
-
expect(eq(
|
|
72
|
+
expect(eq(ctx, math1, math2)).toBe(true);
|
|
73
|
+
expect(eq(ctx, math1, obj)).toBe(false);
|
|
69
74
|
|
|
70
75
|
math1.dispose();
|
|
71
76
|
math2.dispose();
|
|
72
77
|
obj.dispose();
|
|
73
|
-
|
|
78
|
+
ctx.dispose();
|
|
74
79
|
});
|
|
75
80
|
|
|
76
81
|
test("instanceOf", async () => {
|
|
77
|
-
const
|
|
78
|
-
const vm = quickjs.createVm();
|
|
82
|
+
const ctx = (await getQuickJS()).newContext();
|
|
79
83
|
|
|
80
|
-
const pr =
|
|
81
|
-
const func =
|
|
82
|
-
const p =
|
|
83
|
-
expect(instanceOf(
|
|
84
|
-
expect(instanceOf(
|
|
84
|
+
const pr = ctx.unwrapResult(ctx.evalCode("Promise"));
|
|
85
|
+
const func = ctx.unwrapResult(ctx.evalCode("(function() {})"));
|
|
86
|
+
const p = ctx.unwrapResult(ctx.evalCode("Promise.resolve()"));
|
|
87
|
+
expect(instanceOf(ctx, p, pr)).toBe(true);
|
|
88
|
+
expect(instanceOf(ctx, p, func)).toBe(false);
|
|
85
89
|
|
|
86
90
|
p.dispose();
|
|
87
91
|
pr.dispose();
|
|
88
92
|
func.dispose();
|
|
89
|
-
|
|
93
|
+
ctx.dispose();
|
|
90
94
|
});
|
|
91
95
|
|
|
92
96
|
test("isHandleObject", async () => {
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
expect(isHandleObject(vm, num)).toBe(false);
|
|
97
|
+
const ctx = (await getQuickJS()).newContext();
|
|
98
|
+
|
|
99
|
+
const obj = ctx.newObject();
|
|
100
|
+
expect(isHandleObject(ctx, obj)).toBe(true);
|
|
101
|
+
const func = ctx.newFunction("", () => {});
|
|
102
|
+
expect(isHandleObject(ctx, func)).toBe(true);
|
|
103
|
+
const array = ctx.newArray();
|
|
104
|
+
expect(isHandleObject(ctx, array)).toBe(true);
|
|
105
|
+
const num = ctx.newNumber(NaN);
|
|
106
|
+
expect(isHandleObject(ctx, num)).toBe(false);
|
|
104
107
|
|
|
105
108
|
obj.dispose();
|
|
106
109
|
func.dispose();
|
|
107
110
|
array.dispose();
|
|
108
|
-
|
|
111
|
+
ctx.dispose();
|
|
109
112
|
});
|
|
110
113
|
|
|
111
114
|
test("json", async () => {
|
|
112
|
-
const
|
|
113
|
-
const vm = quickjs.createVm();
|
|
115
|
+
const ctx = (await getQuickJS()).newContext();
|
|
114
116
|
|
|
115
|
-
const handle = json(
|
|
117
|
+
const handle = json(ctx, {
|
|
116
118
|
hoge: { foo: ["bar"] },
|
|
117
119
|
});
|
|
118
120
|
expect(
|
|
119
|
-
|
|
121
|
+
ctx.dump(call(ctx, `a => a.hoge.foo[0] === "bar"`, undefined, handle))
|
|
120
122
|
).toBe(true);
|
|
121
|
-
expect(
|
|
123
|
+
expect(ctx.typeof(json(ctx, undefined))).toBe("undefined");
|
|
122
124
|
|
|
123
125
|
handle.dispose();
|
|
124
|
-
|
|
126
|
+
ctx.dispose();
|
|
125
127
|
});
|
|
126
128
|
|
|
127
129
|
test("consumeAll", async () => {
|
|
128
|
-
const
|
|
129
|
-
const vm = quickjs.createVm();
|
|
130
|
+
const ctx = (await getQuickJS()).newContext();
|
|
130
131
|
|
|
131
132
|
const o = {};
|
|
132
133
|
|
|
133
|
-
const handles = [
|
|
134
|
+
const handles = [ctx.newObject(), ctx.newObject()];
|
|
134
135
|
expect(
|
|
135
136
|
consumeAll(
|
|
136
137
|
handles,
|
|
@@ -139,7 +140,7 @@ test("consumeAll", async () => {
|
|
|
139
140
|
).toBe(o);
|
|
140
141
|
expect(handles.every((h) => !h.alive)).toBe(true);
|
|
141
142
|
|
|
142
|
-
const handles2 = [
|
|
143
|
+
const handles2 = [ctx.newObject(), ctx.newObject()];
|
|
143
144
|
expect(() =>
|
|
144
145
|
consumeAll(handles2, () => {
|
|
145
146
|
throw new Error("qes error");
|
|
@@ -147,16 +148,15 @@ test("consumeAll", async () => {
|
|
|
147
148
|
).toThrow("qes error");
|
|
148
149
|
expect(handles2.every((h) => !h.alive)).toBe(true);
|
|
149
150
|
|
|
150
|
-
|
|
151
|
+
ctx.dispose();
|
|
151
152
|
});
|
|
152
153
|
|
|
153
154
|
test("mayConsume", async () => {
|
|
154
|
-
const
|
|
155
|
-
const vm = quickjs.createVm();
|
|
155
|
+
const ctx = (await getQuickJS()).newContext();
|
|
156
156
|
|
|
157
157
|
const o = {};
|
|
158
158
|
|
|
159
|
-
const handle =
|
|
159
|
+
const handle = ctx.newArray();
|
|
160
160
|
expect(
|
|
161
161
|
mayConsume(
|
|
162
162
|
[handle, false],
|
|
@@ -168,7 +168,7 @@ test("mayConsume", async () => {
|
|
|
168
168
|
mayConsume([handle, true], () => {});
|
|
169
169
|
expect(handle.alive).toBe(false);
|
|
170
170
|
|
|
171
|
-
const handle2 =
|
|
171
|
+
const handle2 = ctx.newArray();
|
|
172
172
|
expect(() =>
|
|
173
173
|
mayConsume([handle2, true], () => {
|
|
174
174
|
throw new Error("qes error");
|
|
@@ -176,18 +176,17 @@ test("mayConsume", async () => {
|
|
|
176
176
|
).toThrow("qes error");
|
|
177
177
|
expect(handle.alive).toBe(false);
|
|
178
178
|
|
|
179
|
-
|
|
179
|
+
ctx.dispose();
|
|
180
180
|
});
|
|
181
181
|
|
|
182
182
|
test("mayConsumeAll", async () => {
|
|
183
|
-
const
|
|
184
|
-
const vm = quickjs.createVm();
|
|
183
|
+
const ctx = (await getQuickJS()).newContext();
|
|
185
184
|
|
|
186
185
|
const o = {};
|
|
187
186
|
|
|
188
187
|
const handles: [QuickJSHandle, boolean][] = [
|
|
189
|
-
[
|
|
190
|
-
[
|
|
188
|
+
[ctx.newObject(), false],
|
|
189
|
+
[ctx.newObject(), true],
|
|
191
190
|
];
|
|
192
191
|
expect(
|
|
193
192
|
mayConsumeAll(
|
|
@@ -199,8 +198,8 @@ test("mayConsumeAll", async () => {
|
|
|
199
198
|
expect(handles[1][0].alive).toBe(false);
|
|
200
199
|
|
|
201
200
|
const handles2: [QuickJSHandle, boolean][] = [
|
|
202
|
-
[
|
|
203
|
-
[
|
|
201
|
+
[ctx.newObject(), false],
|
|
202
|
+
[ctx.newObject(), true],
|
|
204
203
|
];
|
|
205
204
|
expect(() =>
|
|
206
205
|
mayConsumeAll(handles2, (..._args) => {
|
|
@@ -212,20 +211,19 @@ test("mayConsumeAll", async () => {
|
|
|
212
211
|
|
|
213
212
|
handles[0][0].dispose();
|
|
214
213
|
handles2[0][0].dispose();
|
|
215
|
-
|
|
214
|
+
ctx.dispose();
|
|
216
215
|
});
|
|
217
216
|
|
|
218
217
|
test("handleFrom", async () => {
|
|
219
|
-
const
|
|
220
|
-
const vm = quickjs.createVm();
|
|
218
|
+
const ctx = (await getQuickJS()).newContext();
|
|
221
219
|
|
|
222
|
-
const handle =
|
|
223
|
-
const promise =
|
|
220
|
+
const handle = ctx.newObject();
|
|
221
|
+
const promise = ctx.newPromise();
|
|
224
222
|
|
|
225
223
|
expect(handleFrom(handle) === handle).toBe(true);
|
|
226
224
|
expect(handleFrom(promise) === promise.handle).toBe(true);
|
|
227
225
|
|
|
228
226
|
handle.dispose();
|
|
229
227
|
promise.dispose();
|
|
230
|
-
|
|
228
|
+
ctx.dispose();
|
|
231
229
|
});
|
package/src/vmutil.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
Disposable,
|
|
3
|
-
|
|
3
|
+
QuickJSContext,
|
|
4
4
|
QuickJSHandle,
|
|
5
5
|
QuickJSDeferredPromise,
|
|
6
6
|
} from "quickjs-emscripten";
|
|
7
7
|
|
|
8
8
|
export function fn(
|
|
9
|
-
|
|
9
|
+
ctx: QuickJSContext,
|
|
10
10
|
code: string
|
|
11
11
|
): ((
|
|
12
12
|
thisArg: QuickJSHandle | undefined,
|
|
13
13
|
...args: QuickJSHandle[]
|
|
14
14
|
) => QuickJSHandle) &
|
|
15
15
|
Disposable {
|
|
16
|
-
const handle =
|
|
16
|
+
const handle = ctx.unwrapResult(ctx.evalCode(code));
|
|
17
17
|
const f = (
|
|
18
18
|
thisArg: QuickJSHandle | undefined,
|
|
19
19
|
...args: QuickJSHandle[]
|
|
20
20
|
): any => {
|
|
21
|
-
return
|
|
22
|
-
|
|
21
|
+
return ctx.unwrapResult(
|
|
22
|
+
ctx.callFunction(handle, thisArg ?? ctx.undefined, ...args)
|
|
23
23
|
);
|
|
24
24
|
};
|
|
25
25
|
f.dispose = () => handle.dispose();
|
|
@@ -31,12 +31,12 @@ export function fn(
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export function call(
|
|
34
|
-
|
|
34
|
+
ctx: QuickJSContext,
|
|
35
35
|
code: string,
|
|
36
36
|
thisArg?: QuickJSHandle,
|
|
37
37
|
...args: QuickJSHandle[]
|
|
38
38
|
): QuickJSHandle {
|
|
39
|
-
const f = fn(
|
|
39
|
+
const f = fn(ctx, code);
|
|
40
40
|
try {
|
|
41
41
|
return f(thisArg, ...args);
|
|
42
42
|
} finally {
|
|
@@ -44,33 +44,37 @@ export function call(
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export function eq(
|
|
48
|
-
|
|
47
|
+
export function eq(
|
|
48
|
+
ctx: QuickJSContext,
|
|
49
|
+
a: QuickJSHandle,
|
|
50
|
+
b: QuickJSHandle
|
|
51
|
+
): boolean {
|
|
52
|
+
return ctx.dump(call(ctx, "Object.is", undefined, a, b));
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
export function instanceOf(
|
|
52
|
-
|
|
56
|
+
ctx: QuickJSContext,
|
|
53
57
|
a: QuickJSHandle,
|
|
54
58
|
b: QuickJSHandle
|
|
55
59
|
): boolean {
|
|
56
|
-
return
|
|
60
|
+
return ctx.dump(call(ctx, "(a, b) => a instanceof b", undefined, a, b));
|
|
57
61
|
}
|
|
58
62
|
|
|
59
|
-
export function isHandleObject(
|
|
60
|
-
return
|
|
63
|
+
export function isHandleObject(ctx: QuickJSContext, h: QuickJSHandle): boolean {
|
|
64
|
+
return ctx.dump(
|
|
61
65
|
call(
|
|
62
|
-
|
|
66
|
+
ctx,
|
|
63
67
|
`a => typeof a === "object" && a !== null || typeof a === "function"`,
|
|
64
68
|
undefined,
|
|
65
|
-
|
|
69
|
+
h
|
|
66
70
|
)
|
|
67
71
|
);
|
|
68
72
|
}
|
|
69
73
|
|
|
70
|
-
export function json(
|
|
74
|
+
export function json(ctx: QuickJSContext, target: any): QuickJSHandle {
|
|
71
75
|
const json = JSON.stringify(target);
|
|
72
|
-
if (!json) return
|
|
73
|
-
return call(
|
|
76
|
+
if (!json) return ctx.undefined;
|
|
77
|
+
return call(ctx, `JSON.parse`, undefined, ctx.newString(json));
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
export function consumeAll<T extends QuickJSHandle[], K>(
|