quickjs-emscripten-sync 1.1.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 (71) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +59 -49
  3. package/dist/index.d.ts +121 -26
  4. package/dist/quickjs-emscripten-sync.es.js +962 -0
  5. package/dist/quickjs-emscripten-sync.umd.js +64 -0
  6. package/package.json +28 -40
  7. package/src/default.ts +2 -2
  8. package/src/index.test.ts +371 -69
  9. package/src/index.ts +135 -72
  10. package/src/marshal/function.test.ts +70 -61
  11. package/src/marshal/function.ts +10 -9
  12. package/src/marshal/index.test.ts +135 -62
  13. package/src/marshal/index.ts +33 -16
  14. package/src/marshal/json.test.ts +94 -0
  15. package/src/marshal/json.ts +16 -0
  16. package/src/marshal/object.test.ts +65 -58
  17. package/src/marshal/object.ts +6 -5
  18. package/src/marshal/primitive.test.ts +23 -17
  19. package/src/marshal/primitive.ts +10 -9
  20. package/src/marshal/promise.test.ts +86 -0
  21. package/src/marshal/promise.ts +26 -0
  22. package/src/marshal/properties.test.ts +23 -19
  23. package/src/marshal/properties.ts +11 -10
  24. package/src/marshal/symbol.test.ts +9 -7
  25. package/src/marshal/symbol.ts +5 -4
  26. package/src/unmarshal/function.test.ts +70 -61
  27. package/src/unmarshal/function.ts +39 -30
  28. package/src/unmarshal/index.test.ts +101 -100
  29. package/src/unmarshal/index.ts +13 -9
  30. package/src/unmarshal/object.test.ts +45 -41
  31. package/src/unmarshal/object.ts +14 -13
  32. package/src/unmarshal/primitive.test.ts +20 -15
  33. package/src/unmarshal/primitive.ts +10 -10
  34. package/src/unmarshal/promise.test.ts +44 -0
  35. package/src/unmarshal/promise.ts +38 -0
  36. package/src/unmarshal/properties.test.ts +10 -8
  37. package/src/unmarshal/properties.ts +47 -42
  38. package/src/unmarshal/symbol.test.ts +9 -7
  39. package/src/unmarshal/symbol.ts +4 -4
  40. package/src/util.test.ts +23 -5
  41. package/src/util.ts +15 -0
  42. package/src/vmmap.test.ts +88 -82
  43. package/src/vmmap.ts +21 -17
  44. package/src/vmutil.test.ts +159 -53
  45. package/src/vmutil.ts +91 -19
  46. package/src/wrapper.test.ts +151 -115
  47. package/src/wrapper.ts +69 -48
  48. package/dist/default.d.ts +0 -4
  49. package/dist/index.js +0 -8
  50. package/dist/marshal/function.d.ts +0 -2
  51. package/dist/marshal/index.d.ts +0 -11
  52. package/dist/marshal/object.d.ts +0 -2
  53. package/dist/marshal/primitive.d.ts +0 -2
  54. package/dist/marshal/properties.d.ts +0 -2
  55. package/dist/marshal/symbol.d.ts +0 -2
  56. package/dist/quickjs-emscripten-sync.cjs.development.js +0 -1289
  57. package/dist/quickjs-emscripten-sync.cjs.development.js.map +0 -1
  58. package/dist/quickjs-emscripten-sync.cjs.production.min.js +0 -2
  59. package/dist/quickjs-emscripten-sync.cjs.production.min.js.map +0 -1
  60. package/dist/quickjs-emscripten-sync.esm.js +0 -1272
  61. package/dist/quickjs-emscripten-sync.esm.js.map +0 -1
  62. package/dist/unmarshal/function.d.ts +0 -2
  63. package/dist/unmarshal/index.d.ts +0 -9
  64. package/dist/unmarshal/object.d.ts +0 -2
  65. package/dist/unmarshal/primitive.d.ts +0 -2
  66. package/dist/unmarshal/properties.d.ts +0 -2
  67. package/dist/unmarshal/symbol.d.ts +0 -2
  68. package/dist/util.d.ts +0 -7
  69. package/dist/vmmap.d.ts +0 -35
  70. package/dist/vmutil.d.ts +0 -7
  71. package/dist/wrapper.d.ts +0 -11
package/src/wrapper.ts CHANGED
@@ -1,20 +1,22 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
1
+ import type { QuickJSHandle, QuickJSContext } from "quickjs-emscripten";
2
+
2
3
  import { isObject } from "./util";
3
- import { call, isHandleObject, consumeAll } from "./vmutil";
4
+ import { call, isHandleObject, consumeAll, mayConsumeAll } from "./vmutil";
4
5
 
5
6
  export type SyncMode = "both" | "vm" | "host";
6
7
 
7
8
  export type Wrapped<T> = T & { __qes_wrapped: never };
8
9
 
9
10
  export function wrap<T = any>(
10
- vm: QuickJSVm,
11
+ ctx: QuickJSContext,
11
12
  target: T,
12
13
  proxyKeySymbol: symbol,
13
14
  proxyKeySymbolHandle: QuickJSHandle,
14
- marshal: (target: any) => QuickJSHandle,
15
+ marshal: (target: any) => [QuickJSHandle, boolean],
15
16
  syncMode?: (target: T) => SyncMode | undefined
16
17
  ): Wrapped<T> | undefined {
17
- if (!isObject(target)) return undefined;
18
+ // promise cannot be wrapped
19
+ if (!isObject(target) || target instanceof Promise) return undefined;
18
20
  if (isWrapped(target, proxyKeySymbol)) return target;
19
21
 
20
22
  const rec = new Proxy(target as any, {
@@ -24,63 +26,81 @@ export function wrap<T = any>(
24
26
  set(obj, key, value, receiver) {
25
27
  const v = unwrap(value, proxyKeySymbol);
26
28
  const sync = syncMode?.(receiver) ?? "host";
27
- if (sync === "vm" || Reflect.set(obj, key, v, receiver)) {
28
- if (sync === "host") return true;
29
- const [handle2, unwrapped] = unwrapHandle(
30
- vm,
31
- marshal(receiver),
32
- proxyKeySymbolHandle
33
- );
34
- if (unwrapped) {
35
- handle2.consume(h => vm.setProp(h, marshal(key), marshal(v)));
36
- } else {
37
- vm.setProp(handle2, marshal(key), marshal(v));
29
+ if (
30
+ (sync !== "vm" && !Reflect.set(obj, key, v, receiver)) ||
31
+ sync === "host" ||
32
+ !ctx.alive
33
+ )
34
+ return true;
35
+
36
+ mayConsumeAll(
37
+ [marshal(receiver), marshal(key), marshal(v)],
38
+ (receiverHandle, keyHandle, valueHandle) => {
39
+ const [handle2, unwrapped] = unwrapHandle(
40
+ ctx,
41
+ receiverHandle,
42
+ proxyKeySymbolHandle
43
+ );
44
+ if (unwrapped) {
45
+ handle2.consume((h) => ctx.setProp(h, keyHandle, valueHandle));
46
+ } else {
47
+ ctx.setProp(handle2, keyHandle, valueHandle);
48
+ }
38
49
  }
39
- }
50
+ );
51
+
40
52
  return true;
41
53
  },
42
54
  deleteProperty(obj, key) {
43
55
  const sync = syncMode?.(rec) ?? "host";
44
- const [handle2, unwrapped] = unwrapHandle(
45
- vm,
46
- marshal(rec),
47
- proxyKeySymbolHandle
48
- );
49
- if (sync === "vm" || Reflect.deleteProperty(obj, key)) {
50
- if (sync === "host") return true;
51
- if (unwrapped) {
52
- handle2.consume(h =>
53
- call(vm, `(a, b) => delete a[b]`, undefined, h, marshal(key))
56
+ return mayConsumeAll(
57
+ [marshal(rec), marshal(key)],
58
+ (recHandle, keyHandle) => {
59
+ const [handle2, unwrapped] = unwrapHandle(
60
+ ctx,
61
+ recHandle,
62
+ proxyKeySymbolHandle
54
63
  );
55
- } else {
56
- call(vm, `(a, b) => delete a[b]`, undefined, handle2, marshal(key));
64
+
65
+ if (sync === "vm" || Reflect.deleteProperty(obj, key)) {
66
+ if (sync === "host" || !ctx.alive) return true;
67
+
68
+ if (unwrapped) {
69
+ handle2.consume((h) =>
70
+ call(ctx, `(a, b) => delete a[b]`, undefined, h, keyHandle)
71
+ );
72
+ } else {
73
+ call(ctx, `(a, b) => delete a[b]`, undefined, handle2, keyHandle);
74
+ }
75
+ }
76
+ return true;
57
77
  }
58
- }
59
- return true;
78
+ );
60
79
  },
61
80
  }) as Wrapped<T>;
62
81
  return rec;
63
82
  }
64
83
 
65
84
  export function wrapHandle(
66
- vm: QuickJSVm,
85
+ ctx: QuickJSContext,
67
86
  handle: QuickJSHandle,
68
87
  proxyKeySymbol: symbol,
69
88
  proxyKeySymbolHandle: QuickJSHandle,
70
89
  unmarshal: (handle: QuickJSHandle) => any,
71
90
  syncMode?: (target: QuickJSHandle) => SyncMode | undefined
72
91
  ): [Wrapped<QuickJSHandle> | undefined, boolean] {
73
- if (!isHandleObject(vm, handle)) return [undefined, false];
74
- if (isHandleWrapped(vm, handle, proxyKeySymbolHandle)) return [handle, false];
92
+ if (!isHandleObject(ctx, handle)) return [undefined, false];
93
+ if (isHandleWrapped(ctx, handle, proxyKeySymbolHandle))
94
+ return [handle, false];
75
95
 
76
96
  return consumeAll(
77
97
  [
78
- vm.newFunction("getSyncMode", h => {
98
+ ctx.newFunction("getSyncMode", (h) => {
79
99
  const res = syncMode?.(unmarshal(h));
80
- if (typeof res === "string") return vm.newString(res);
81
- return vm.undefined;
100
+ if (typeof res === "string") return ctx.newString(res);
101
+ return ctx.undefined;
82
102
  }),
83
- vm.newFunction("setter", (h, keyHandle, valueHandle) => {
103
+ ctx.newFunction("setter", (h, keyHandle, valueHandle) => {
84
104
  const target = unmarshal(h);
85
105
  if (!target) return;
86
106
  const key = unmarshal(keyHandle);
@@ -88,16 +108,16 @@ export function wrapHandle(
88
108
  const value = unmarshal(valueHandle);
89
109
  unwrap(target, proxyKeySymbol)[key] = value;
90
110
  }),
91
- vm.newFunction("deleter", (h, keyHandle) => {
111
+ ctx.newFunction("deleter", (h, keyHandle) => {
92
112
  const target = unmarshal(h);
93
113
  if (!target) return;
94
114
  const key = unmarshal(keyHandle);
95
115
  delete unwrap(target, proxyKeySymbol)[key];
96
116
  }),
97
117
  ],
98
- args => [
118
+ (args) => [
99
119
  call(
100
- vm,
120
+ ctx,
101
121
  `(target, sym, getSyncMode, setter, deleter) => {
102
122
  const rec = new Proxy(target, {
103
123
  get(obj, key, receiver) {
@@ -142,12 +162,12 @@ export function unwrap<T>(obj: T, key: string | symbol): T {
142
162
  }
143
163
 
144
164
  export function unwrapHandle(
145
- vm: QuickJSVm,
165
+ ctx: QuickJSContext,
146
166
  handle: QuickJSHandle,
147
167
  key: QuickJSHandle
148
168
  ): [QuickJSHandle, boolean] {
149
- if (!isHandleWrapped(vm, handle, key)) return [handle, false];
150
- return [vm.getProp(handle, key), true];
169
+ if (!isHandleWrapped(ctx, handle, key)) return [handle, false];
170
+ return [ctx.getProp(handle, key), true];
151
171
  }
152
172
 
153
173
  export function isWrapped<T>(obj: T, key: string | symbol): obj is Wrapped<T> {
@@ -155,14 +175,15 @@ export function isWrapped<T>(obj: T, key: string | symbol): obj is Wrapped<T> {
155
175
  }
156
176
 
157
177
  export function isHandleWrapped(
158
- vm: QuickJSVm,
178
+ ctx: QuickJSContext,
159
179
  handle: QuickJSHandle,
160
180
  key: QuickJSHandle
161
181
  ): handle is Wrapped<QuickJSHandle> {
162
- return !!vm.dump(
182
+ return !!ctx.dump(
163
183
  call(
164
- vm,
165
- `(a, s) => (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`,
184
+ ctx,
185
+ // promise cannot be wrapped
186
+ `(a, s) => (a instanceof Promise) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`,
166
187
  undefined,
167
188
  handle,
168
189
  key
package/dist/default.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * Default value of registeredObjects option of the Arena class constructor.
3
- */
4
- export declare const defaultRegisteredObjects: [any, string][];
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
-
2
- 'use strict'
3
-
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./quickjs-emscripten-sync.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./quickjs-emscripten-sync.cjs.development.js')
8
- }
@@ -1,2 +0,0 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
2
- export default function marshalFunction(vm: QuickJSVm, target: unknown, marshal: (target: unknown) => QuickJSHandle, unmarshal: (handle: QuickJSHandle) => unknown, preMarshal: (target: unknown, handle: QuickJSHandle) => QuickJSHandle | undefined, preApply?: (target: Function, thisArg: unknown, args: unknown[]) => any): QuickJSHandle | undefined;
@@ -1,11 +0,0 @@
1
- import { QuickJSHandle, QuickJSVm } from "quickjs-emscripten";
2
- export declare type Options = {
3
- vm: QuickJSVm;
4
- unmarshal: (handle: QuickJSHandle) => unknown;
5
- isMarshalable?: (target: unknown) => boolean;
6
- find: (target: unknown) => QuickJSHandle | undefined;
7
- pre: (target: unknown, handle: QuickJSHandle) => QuickJSHandle | undefined;
8
- preApply?: (target: Function, thisArg: unknown, args: unknown[]) => any;
9
- };
10
- export declare function marshal(target: unknown, options: Options): QuickJSHandle;
11
- export default marshal;
@@ -1,2 +0,0 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
2
- export default function marshalObject(vm: QuickJSVm, target: unknown, marshal: (target: unknown) => QuickJSHandle, preMarshal: (target: unknown, handle: QuickJSHandle) => QuickJSHandle | undefined): QuickJSHandle | undefined;
@@ -1,2 +0,0 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
2
- export default function marshalPrimitive(vm: QuickJSVm, target: unknown): QuickJSHandle | undefined;
@@ -1,2 +0,0 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
2
- export default function marshalProperties(vm: QuickJSVm, target: object | Function, handle: QuickJSHandle, marshal: (target: unknown) => QuickJSHandle): void;
@@ -1,2 +0,0 @@
1
- import { QuickJSVm, QuickJSHandle } from "quickjs-emscripten";
2
- export default function marshalSymbol(vm: QuickJSVm, target: unknown, preMarshal: (target: unknown, handle: QuickJSHandle) => QuickJSHandle | undefined): QuickJSHandle | undefined;