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
@@ -5,16 +5,16 @@ import { json } from "../vmutil";
5
5
  import unmarshalFunction from "./function";
6
6
 
7
7
  test("arrow function", async () => {
8
- const vm = (await getQuickJS()).createVm();
9
- const marshal = vi.fn((v): [QuickJSHandle, boolean] => [json(vm, v), false]);
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
- vm.dump(v),
11
+ ctx.dump(v),
12
12
  false,
13
13
  ]);
14
14
  const preUnmarshal = vi.fn((a) => a);
15
15
 
16
- const handle = vm.unwrapResult(vm.evalCode(`(a, b) => a + b`));
17
- const func = unmarshalFunction(vm, handle, marshal, unmarshal, preUnmarshal);
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
- vm.dispose();
37
+ ctx.dispose();
38
38
  });
39
39
 
40
40
  test("function", async () => {
41
- const vm = (await getQuickJS()).createVm();
41
+ const ctx = (await getQuickJS()).newContext();
42
42
  const that = { a: 1 };
43
- const thatHandle = vm.unwrapResult(vm.evalCode(`({ a: 1 })`));
43
+ const thatHandle = ctx.unwrapResult(ctx.evalCode(`({ a: 1 })`));
44
44
  const marshal = vi.fn((v): [QuickJSHandle, boolean] => [
45
- v === that ? thatHandle : json(vm, v),
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 = vm.typeof(v);
50
+ const ty = ctx.typeof(v);
51
51
  if (ty === "object" || ty === "function") disposables.push(v);
52
- return [vm.dump(v), false];
52
+ return [ctx.dump(v), false];
53
53
  });
54
54
  const preUnmarshal = vi.fn((a) => a);
55
55
 
56
- const handle = vm.unwrapResult(
57
- vm.evalCode(`(function (a) { return this.a + a; })`)
56
+ const handle = ctx.unwrapResult(
57
+ ctx.evalCode(`(function (a) { return this.a + a; })`)
58
58
  );
59
59
 
60
- const func = unmarshalFunction(vm, handle, marshal, unmarshal, preUnmarshal);
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
- vm.dispose();
81
+ ctx.dispose();
82
82
  });
83
83
 
84
84
  test("constructor", async () => {
85
- const vm = (await getQuickJS()).createVm();
85
+ const ctx = (await getQuickJS()).newContext();
86
86
  const disposables: QuickJSHandle[] = [];
87
87
  const marshal = vi.fn((v): [QuickJSHandle, boolean] => [
88
- typeof v === "object" ? vm.undefined : json(vm, v),
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 = vm.typeof(v);
92
+ const ty = ctx.typeof(v);
93
93
  if (ty === "object" || ty === "function") disposables.push(v);
94
- return [vm.dump(v), false];
94
+ return [ctx.dump(v), false];
95
95
  });
96
96
  const preUnmarshal = vi.fn((a) => a);
97
97
 
98
- const handle = vm.unwrapResult(
99
- vm.evalCode(`(function (b) { this.a = b + 2; })`)
98
+ const handle = ctx.unwrapResult(
99
+ ctx.evalCode(`(function (b) { this.a = b + 2; })`)
100
100
  );
101
101
 
102
102
  const Cls = unmarshalFunction(
103
- vm,
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
- vm.dispose();
130
+ ctx.dispose();
131
131
  });
132
132
 
133
133
  test("class", async () => {
134
- const vm = (await getQuickJS()).createVm();
134
+ const ctx = (await getQuickJS()).newContext();
135
135
  const marshal = vi.fn((v): [QuickJSHandle, boolean] => [
136
- typeof v === "object" ? vm.undefined : json(vm, v),
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 = vm.typeof(v);
141
+ const ty = ctx.typeof(v);
142
142
  if (ty === "object" || ty === "function") disposables.push(v);
143
- return [vm.dump(v), false];
143
+ return [ctx.dump(v), false];
144
144
  });
145
145
  const preUnmarshal = vi.fn((a) => a);
146
146
 
147
- const handle = vm.unwrapResult(
148
- vm.evalCode(`(class A { constructor(a) { this.a = a + 1; } })`)
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
- vm,
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
- vm.dispose();
179
+ ctx.dispose();
180
180
  });
181
181
 
182
182
  test("undefined", async () => {
183
- const vm = (await getQuickJS()).createVm();
183
+ const ctx = (await getQuickJS()).newContext();
184
184
  const f = vi.fn();
185
185
 
186
- expect(unmarshalFunction(vm, vm.undefined, f, f, f)).toEqual(undefined);
187
- expect(unmarshalFunction(vm, vm.true, f, f, f)).toEqual(undefined);
188
- expect(unmarshalFunction(vm, vm.false, f, f, f)).toEqual(undefined);
189
- expect(unmarshalFunction(vm, vm.null, f, f, f)).toEqual(undefined);
190
- expect(unmarshalFunction(vm, vm.newString("hoge"), f, f, f)).toEqual(
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 = vm.newObject();
196
- expect(unmarshalFunction(vm, obj, f, f, f)).toEqual(undefined);
197
- const array = vm.newArray();
198
- expect(unmarshalFunction(vm, array, f, f, f)).toEqual(undefined);
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
- vm.dispose();
206
+ ctx.dispose();
205
207
  });
@@ -1,16 +1,17 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
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
- vm: QuickJSVm,
7
+ ctx: QuickJSContext,
7
8
  handle: QuickJSHandle,
8
- // marshal returns handle and boolean indicates that the handle should be disposed after use
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 (vm.typeof(handle) !== "function") return;
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
- vm,
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 = vm.unwrapResult(
37
- vm.callFunction(handle, thisHandle, ...argHandles)
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(vm, handle, raw, unmarshal);
50
+ unmarshalProperties(ctx, handle, raw, unmarshal);
50
51
 
51
52
  return func;
52
53
  }
@@ -1,4 +1,4 @@
1
- import { Disposable, getQuickJS, QuickJSHandle } from "quickjs-emscripten";
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 { vm, unmarshal, marshal, map, dispose } = await setup();
9
+ const { ctx, unmarshal, marshal, map, dispose } = await setup();
10
10
 
11
- const handle = vm.unwrapResult(
12
- vm.evalCode(`({
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
- vm.getProp(handle, "aaa").consume((h) =>
32
- expect(map.getByHandle(h)).toBe(target.aaa)
33
- );
34
- vm.getProp(handle, "aaa")
35
- .consume((h) => vm.getProp(h, 2))
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
- vm.getProp(handle, "nested").consume((h) =>
38
- expect(map.getByHandle(h)).toBe(target.nested)
39
- );
40
- vm.getProp(handle, "bbb").consume((h) =>
41
- expect(map.getByHandle(h)).toBe(target.bbb)
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 { vm, unmarshal, dispose } = await setup();
54
+ const { ctx, unmarshal, dispose } = await setup();
54
55
 
55
- const handle = vm.unwrapResult(
56
- vm.evalCode(`({
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 { vm, unmarshal, marshal, map, dispose } = await setup();
71
+ const { ctx, unmarshal, marshal, map, dispose } = await setup();
71
72
 
72
- const handle = vm.unwrapResult(
73
- vm.evalCode(`(function(a) { return a.a + "!"; })`)
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 { vm, unmarshal, dispose } = await setup();
92
+ const { ctx, unmarshal, dispose } = await setup();
92
93
 
93
- const deferred = vm.newPromise();
94
+ const deferred = ctx.newPromise();
94
95
  const promise = unmarshal(deferred.handle);
95
- deferred.resolve(vm.newString("resolved!"));
96
- vm.executePendingJobs();
96
+ deferred.resolve(ctx.newString("resolved!"));
97
+ ctx.runtime.executePendingJobs();
97
98
  await expect(promise).resolves.toBe("resolved!");
98
99
 
99
- const deferred2 = vm.newPromise();
100
+ const deferred2 = ctx.newPromise();
100
101
  const promise2 = unmarshal(deferred2.handle);
101
- deferred2.reject(vm.newString("rejected!"));
102
- vm.executePendingJobs();
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 { vm, unmarshal, dispose } = await setup();
112
+ const { ctx, unmarshal, dispose } = await setup();
112
113
 
113
- const handle = vm.unwrapResult(
114
- vm.evalCode(`{
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 vm = (await getQuickJS()).createVm();
142
- const map = new VMMap(vm);
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
- ? vm.newFunction(target.name, (...handles) => {
151
- target(...handles.map((h) => vm.dump(h)));
151
+ ? ctx.newFunction(target.name, (...handles) => {
152
+ target(...handles.map((h) => ctx.dump(h)));
152
153
  })
153
- : json(vm, target);
154
- const ty = vm.typeof(handle2);
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
- vm,
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
- vm,
171
+ ctx: ctx,
171
172
  }),
172
173
  marshal,
173
174
  dispose: () => {
174
175
  disposables.forEach((d) => d.dispose());
175
176
  map.dispose();
176
- vm.dispose();
177
+ ctx.dispose();
177
178
  },
178
179
  };
179
180
  };
@@ -1,4 +1,5 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
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
- vm: QuickJSVm;
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 { vm, marshal, find, pre } = options;
26
+ const { ctx, marshal, find, pre } = options;
26
27
 
27
28
  {
28
- const [target, ok] = unmarshalPrimitive(vm, handle);
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(vm, handle, pre) ??
43
- unmarshalPromise(vm, handle, marshal, pre) ??
44
- unmarshalFunction(vm, handle, marshal, unmarshal2, pre) ??
45
- unmarshalObject(vm, handle, unmarshal2, pre);
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 vm = (await getQuickJS()).createVm();
7
+ const ctx = (await getQuickJS()).newContext();
8
8
  const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
9
- vm.dump(v),
9
+ ctx.dump(v),
10
10
  false,
11
11
  ]);
12
12
  const preUnmarshal = vi.fn((a) => a);
13
13
 
14
- const handle = vm.unwrapResult(vm.evalCode(`({ a: 1, b: true })`));
15
- const obj = unmarshalObject(vm, handle, unmarshal, preUnmarshal);
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
- vm.dispose();
27
+ ctx.dispose();
28
28
  });
29
29
 
30
30
  test("properties", async () => {
31
- const vm = (await getQuickJS()).createVm();
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 [vm.typeof(v) === "function" ? () => {} : vm.dump(v), false];
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 = vm.unwrapResult(
40
- vm.evalCode(`{
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(vm, handle, unmarshal, preUnmarshal);
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
- vm.dispose();
77
+ ctx.dispose();
78
78
  });
79
79
 
80
80
  test("array", async () => {
81
- const vm = (await getQuickJS()).createVm();
81
+ const ctx = (await getQuickJS()).newContext();
82
82
  const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
83
- vm.dump(v),
83
+ ctx.dump(v),
84
84
  false,
85
85
  ]);
86
86
  const preUnmarshal = vi.fn((a) => a);
87
87
 
88
- const handle = vm.unwrapResult(vm.evalCode(`[1, true, "a"]`));
89
- const array = unmarshalObject(vm, handle, unmarshal, preUnmarshal);
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
- vm.dispose();
103
+ ctx.dispose();
104
104
  });
105
105
 
106
106
  test("prototype", async () => {
107
- const vm = (await getQuickJS()).createVm();
107
+ const ctx = (await getQuickJS()).newContext();
108
108
  const unmarshal = vi.fn((v: QuickJSHandle): [unknown, boolean] => [
109
- vm.typeof(v) === "object" ? { a: () => 1 } : vm.dump(v),
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 = vm.unwrapResult(vm.evalCode(`Object.create({ a: () => 1 })`));
115
- const obj = unmarshalObject(vm, handle, unmarshal, preUnmarshal) as any;
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
- vm.dispose();
127
+ ctx.dispose();
126
128
  });
127
129
 
128
130
  test("undefined", async () => {
129
- const vm = (await getQuickJS()).createVm();
131
+ const ctx = (await getQuickJS()).newContext();
130
132
  const f = vi.fn();
131
133
 
132
- expect(unmarshalObject(vm, vm.undefined, f, f)).toEqual(undefined);
133
- expect(unmarshalObject(vm, vm.true, f, f)).toEqual(undefined);
134
- expect(unmarshalObject(vm, vm.false, f, f)).toEqual(undefined);
135
- expect(unmarshalObject(vm, vm.null, f, f)).toEqual(undefined);
136
- expect(unmarshalObject(vm, vm.newString("hoge"), f, f)).toEqual(undefined);
137
- expect(unmarshalObject(vm, vm.newNumber(-10), f, f)).toEqual(undefined);
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 = vm.newFunction("", () => {});
140
- expect(unmarshalObject(vm, func, f, f)).toEqual(undefined);
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
- vm.dispose();
147
+ ctx.dispose();
146
148
  });
@@ -1,33 +1,34 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
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
- vm: QuickJSVm,
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
- vm.typeof(handle) !== "object" ||
13
+ ctx.typeof(handle) !== "object" ||
13
14
  // null check
14
- vm
15
- .unwrapResult(vm.evalCode("o => o === null"))
15
+ ctx
16
+ .unwrapResult(ctx.evalCode("o => o === null"))
16
17
  .consume((n) =>
17
- vm.dump(vm.unwrapResult(vm.callFunction(n, vm.undefined, handle)))
18
+ ctx.dump(ctx.unwrapResult(ctx.callFunction(n, ctx.undefined, handle)))
18
19
  )
19
20
  )
20
21
  return;
21
22
 
22
- const raw = call(vm, "Array.isArray", undefined, handle).consume((r) =>
23
- vm.dump(r)
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
- vm,
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 (vm.typeof(prototype) === "undefined") return;
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(vm, handle, raw, unmarshal);
47
+ unmarshalProperties(ctx, handle, raw, unmarshal);
47
48
 
48
49
  return obj;
49
50
  }