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.
Files changed (44) hide show
  1. package/README.md +36 -34
  2. package/dist/index.d.ts +17 -15
  3. package/dist/quickjs-emscripten-sync.es.js +146 -135
  4. package/dist/quickjs-emscripten-sync.umd.js +6 -6
  5. package/package.json +3 -3
  6. package/src/index.test.ts +146 -60
  7. package/src/index.ts +40 -26
  8. package/src/marshal/function.test.ts +61 -53
  9. package/src/marshal/function.ts +7 -6
  10. package/src/marshal/index.test.ts +73 -65
  11. package/src/marshal/index.ts +12 -11
  12. package/src/marshal/json.test.ts +30 -30
  13. package/src/marshal/json.ts +4 -3
  14. package/src/marshal/object.test.ts +55 -50
  15. package/src/marshal/object.ts +6 -5
  16. package/src/marshal/primitive.test.ts +21 -17
  17. package/src/marshal/primitive.ts +10 -9
  18. package/src/marshal/promise.test.ts +14 -14
  19. package/src/marshal/promise.ts +3 -3
  20. package/src/marshal/properties.test.ts +17 -15
  21. package/src/marshal/properties.ts +10 -9
  22. package/src/marshal/symbol.test.ts +6 -6
  23. package/src/marshal/symbol.ts +5 -4
  24. package/src/unmarshal/function.test.ts +45 -43
  25. package/src/unmarshal/function.ts +9 -8
  26. package/src/unmarshal/index.test.ts +41 -40
  27. package/src/unmarshal/index.ts +9 -8
  28. package/src/unmarshal/object.test.ts +33 -31
  29. package/src/unmarshal/object.ts +12 -11
  30. package/src/unmarshal/primitive.test.ts +18 -15
  31. package/src/unmarshal/primitive.ts +9 -9
  32. package/src/unmarshal/promise.test.ts +11 -11
  33. package/src/unmarshal/promise.ts +9 -11
  34. package/src/unmarshal/properties.test.ts +6 -6
  35. package/src/unmarshal/properties.ts +47 -44
  36. package/src/unmarshal/symbol.test.ts +6 -6
  37. package/src/unmarshal/symbol.ts +4 -4
  38. package/src/util.test.ts +1 -0
  39. package/src/vmmap.test.ts +72 -88
  40. package/src/vmmap.ts +16 -16
  41. package/src/vmutil.test.ts +71 -73
  42. package/src/vmutil.ts +22 -18
  43. package/src/wrapper.test.ts +111 -88
  44. package/src/wrapper.ts +31 -27
@@ -15,16 +15,17 @@ import {
15
15
  } from "./vmutil";
16
16
 
17
17
  test("fn", async () => {
18
- const quickjs = await getQuickJS();
19
- const vm = quickjs.createVm();
18
+ const ctx = (await getQuickJS()).newContext();
20
19
 
21
- const f = fn(vm, "(a, b) => a + b");
22
- expect(vm.getNumber(f(undefined, vm.newNumber(1), vm.newNumber(2)))).toBe(3);
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 = vm.newObject();
25
- vm.setProp(obj, "a", vm.newNumber(2));
26
- const f2 = fn(vm, "(function() { return this.a + 1; })");
27
- expect(vm.getNumber(f2(obj))).toBe(3);
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
- vm.dispose();
37
+ ctx.dispose();
37
38
  });
38
39
 
39
40
  test("call", async () => {
40
- const quickjs = await getQuickJS();
41
- const vm = quickjs.createVm();
41
+ const ctx = (await getQuickJS()).newContext();
42
42
 
43
43
  expect(
44
- vm.getNumber(
45
- call(vm, "(a, b) => a + b", undefined, vm.newNumber(1), vm.newNumber(2))
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 = vm.newObject();
50
- vm.setProp(obj, "a", vm.newNumber(2));
55
+ const obj = ctx.newObject();
56
+ ctx.setProp(obj, "a", ctx.newNumber(2));
51
57
  expect(
52
- vm.getNumber(call(vm, "(function() { return this.a + 1; })", obj))
58
+ ctx.getNumber(call(ctx, "(function() { return this.a + 1; })", obj))
53
59
  ).toBe(3);
54
60
 
55
61
  obj.dispose();
56
- vm.dispose();
62
+ ctx.dispose();
57
63
  });
58
64
 
59
65
  test("eq", async () => {
60
- const quickjs = await getQuickJS();
61
- const vm = quickjs.createVm();
66
+ const ctx = (await getQuickJS()).newContext();
62
67
 
63
- const math1 = vm.unwrapResult(vm.evalCode("Math"));
64
- const math2 = vm.unwrapResult(vm.evalCode("Math"));
65
- const obj = vm.newObject();
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(vm, math1, math2)).toBe(true);
68
- expect(eq(vm, math1, obj)).toBe(false);
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
- vm.dispose();
78
+ ctx.dispose();
74
79
  });
75
80
 
76
81
  test("instanceOf", async () => {
77
- const quickjs = await getQuickJS();
78
- const vm = quickjs.createVm();
82
+ const ctx = (await getQuickJS()).newContext();
79
83
 
80
- const pr = vm.unwrapResult(vm.evalCode("Promise"));
81
- const func = vm.unwrapResult(vm.evalCode("(function() {})"));
82
- const p = vm.unwrapResult(vm.evalCode("Promise.resolve()"));
83
- expect(instanceOf(vm, p, pr)).toBe(true);
84
- expect(instanceOf(vm, p, func)).toBe(false);
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
- vm.dispose();
93
+ ctx.dispose();
90
94
  });
91
95
 
92
96
  test("isHandleObject", async () => {
93
- const quickjs = await getQuickJS();
94
- const vm = quickjs.createVm();
95
-
96
- const obj = vm.newObject();
97
- expect(isHandleObject(vm, obj)).toBe(true);
98
- const func = vm.newFunction("", () => {});
99
- expect(isHandleObject(vm, func)).toBe(true);
100
- const array = vm.newArray();
101
- expect(isHandleObject(vm, array)).toBe(true);
102
- const num = vm.newNumber(NaN);
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
- vm.dispose();
111
+ ctx.dispose();
109
112
  });
110
113
 
111
114
  test("json", async () => {
112
- const quickjs = await getQuickJS();
113
- const vm = quickjs.createVm();
115
+ const ctx = (await getQuickJS()).newContext();
114
116
 
115
- const handle = json(vm, {
117
+ const handle = json(ctx, {
116
118
  hoge: { foo: ["bar"] },
117
119
  });
118
120
  expect(
119
- vm.dump(call(vm, `a => a.hoge.foo[0] === "bar"`, undefined, handle))
121
+ ctx.dump(call(ctx, `a => a.hoge.foo[0] === "bar"`, undefined, handle))
120
122
  ).toBe(true);
121
- expect(vm.typeof(json(vm, undefined))).toBe("undefined");
123
+ expect(ctx.typeof(json(ctx, undefined))).toBe("undefined");
122
124
 
123
125
  handle.dispose();
124
- vm.dispose();
126
+ ctx.dispose();
125
127
  });
126
128
 
127
129
  test("consumeAll", async () => {
128
- const quickjs = await getQuickJS();
129
- const vm = quickjs.createVm();
130
+ const ctx = (await getQuickJS()).newContext();
130
131
 
131
132
  const o = {};
132
133
 
133
- const handles = [vm.newObject(), vm.newObject()];
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 = [vm.newObject(), vm.newObject()];
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
- vm.dispose();
151
+ ctx.dispose();
151
152
  });
152
153
 
153
154
  test("mayConsume", async () => {
154
- const quickjs = await getQuickJS();
155
- const vm = quickjs.createVm();
155
+ const ctx = (await getQuickJS()).newContext();
156
156
 
157
157
  const o = {};
158
158
 
159
- const handle = vm.newArray();
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 = vm.newArray();
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
- vm.dispose();
179
+ ctx.dispose();
180
180
  });
181
181
 
182
182
  test("mayConsumeAll", async () => {
183
- const quickjs = await getQuickJS();
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
- [vm.newObject(), false],
190
- [vm.newObject(), true],
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
- [vm.newObject(), false],
203
- [vm.newObject(), true],
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
- vm.dispose();
214
+ ctx.dispose();
216
215
  });
217
216
 
218
217
  test("handleFrom", async () => {
219
- const quickjs = await getQuickJS();
220
- const vm = quickjs.createVm();
218
+ const ctx = (await getQuickJS()).newContext();
221
219
 
222
- const handle = vm.newObject();
223
- const promise = vm.newPromise();
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
- vm.dispose();
228
+ ctx.dispose();
231
229
  });
package/src/vmutil.ts CHANGED
@@ -1,25 +1,25 @@
1
1
  import type {
2
2
  Disposable,
3
- QuickJSVm,
3
+ QuickJSContext,
4
4
  QuickJSHandle,
5
5
  QuickJSDeferredPromise,
6
6
  } from "quickjs-emscripten";
7
7
 
8
8
  export function fn(
9
- vm: QuickJSVm,
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 = vm.unwrapResult(vm.evalCode(code));
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 vm.unwrapResult(
22
- vm.callFunction(handle, thisArg ?? vm.undefined, ...args)
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
- vm: QuickJSVm,
34
+ ctx: QuickJSContext,
35
35
  code: string,
36
36
  thisArg?: QuickJSHandle,
37
37
  ...args: QuickJSHandle[]
38
38
  ): QuickJSHandle {
39
- const f = fn(vm, code);
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(vm: QuickJSVm, a: QuickJSHandle, b: QuickJSHandle): boolean {
48
- return vm.dump(call(vm, "Object.is", undefined, a, b));
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
- vm: QuickJSVm,
56
+ ctx: QuickJSContext,
53
57
  a: QuickJSHandle,
54
58
  b: QuickJSHandle
55
59
  ): boolean {
56
- return vm.dump(call(vm, "(a, b) => a instanceof b", undefined, a, b));
60
+ return ctx.dump(call(ctx, "(a, b) => a instanceof b", undefined, a, b));
57
61
  }
58
62
 
59
- export function isHandleObject(vm: QuickJSVm, a: QuickJSHandle): boolean {
60
- return vm.dump(
63
+ export function isHandleObject(ctx: QuickJSContext, h: QuickJSHandle): boolean {
64
+ return ctx.dump(
61
65
  call(
62
- vm,
66
+ ctx,
63
67
  `a => typeof a === "object" && a !== null || typeof a === "function"`,
64
68
  undefined,
65
- a
69
+ h
66
70
  )
67
71
  );
68
72
  }
69
73
 
70
- export function json(vm: QuickJSVm, target: any): QuickJSHandle {
74
+ export function json(ctx: QuickJSContext, target: any): QuickJSHandle {
71
75
  const json = JSON.stringify(target);
72
- if (!json) return vm.undefined;
73
- return call(vm, `JSON.parse`, undefined, vm.newString(json));
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>(