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.
- package/LICENSE +2 -2
- package/README.md +59 -49
- package/dist/index.d.ts +121 -26
- package/dist/quickjs-emscripten-sync.es.js +962 -0
- package/dist/quickjs-emscripten-sync.umd.js +64 -0
- package/package.json +28 -40
- package/src/default.ts +2 -2
- package/src/index.test.ts +371 -69
- package/src/index.ts +135 -72
- package/src/marshal/function.test.ts +70 -61
- package/src/marshal/function.ts +10 -9
- package/src/marshal/index.test.ts +135 -62
- package/src/marshal/index.ts +33 -16
- package/src/marshal/json.test.ts +94 -0
- package/src/marshal/json.ts +16 -0
- package/src/marshal/object.test.ts +65 -58
- package/src/marshal/object.ts +6 -5
- package/src/marshal/primitive.test.ts +23 -17
- package/src/marshal/primitive.ts +10 -9
- package/src/marshal/promise.test.ts +86 -0
- package/src/marshal/promise.ts +26 -0
- package/src/marshal/properties.test.ts +23 -19
- package/src/marshal/properties.ts +11 -10
- package/src/marshal/symbol.test.ts +9 -7
- package/src/marshal/symbol.ts +5 -4
- package/src/unmarshal/function.test.ts +70 -61
- package/src/unmarshal/function.ts +39 -30
- package/src/unmarshal/index.test.ts +101 -100
- package/src/unmarshal/index.ts +13 -9
- package/src/unmarshal/object.test.ts +45 -41
- package/src/unmarshal/object.ts +14 -13
- package/src/unmarshal/primitive.test.ts +20 -15
- package/src/unmarshal/primitive.ts +10 -10
- package/src/unmarshal/promise.test.ts +44 -0
- package/src/unmarshal/promise.ts +38 -0
- package/src/unmarshal/properties.test.ts +10 -8
- package/src/unmarshal/properties.ts +47 -42
- package/src/unmarshal/symbol.test.ts +9 -7
- package/src/unmarshal/symbol.ts +4 -4
- package/src/util.test.ts +23 -5
- package/src/util.ts +15 -0
- package/src/vmmap.test.ts +88 -82
- package/src/vmmap.ts +21 -17
- package/src/vmutil.test.ts +159 -53
- package/src/vmutil.ts +91 -19
- package/src/wrapper.test.ts +151 -115
- package/src/wrapper.ts +69 -48
- package/dist/default.d.ts +0 -4
- package/dist/index.js +0 -8
- package/dist/marshal/function.d.ts +0 -2
- package/dist/marshal/index.d.ts +0 -11
- package/dist/marshal/object.d.ts +0 -2
- package/dist/marshal/primitive.d.ts +0 -2
- package/dist/marshal/properties.d.ts +0 -2
- package/dist/marshal/symbol.d.ts +0 -2
- package/dist/quickjs-emscripten-sync.cjs.development.js +0 -1289
- package/dist/quickjs-emscripten-sync.cjs.development.js.map +0 -1
- package/dist/quickjs-emscripten-sync.cjs.production.min.js +0 -2
- package/dist/quickjs-emscripten-sync.cjs.production.min.js.map +0 -1
- package/dist/quickjs-emscripten-sync.esm.js +0 -1272
- package/dist/quickjs-emscripten-sync.esm.js.map +0 -1
- package/dist/unmarshal/function.d.ts +0 -2
- package/dist/unmarshal/index.d.ts +0 -9
- package/dist/unmarshal/object.d.ts +0 -2
- package/dist/unmarshal/primitive.d.ts +0 -2
- package/dist/unmarshal/properties.d.ts +0 -2
- package/dist/unmarshal/symbol.d.ts +0 -2
- package/dist/util.d.ts +0 -7
- package/dist/vmmap.d.ts +0 -35
- package/dist/vmutil.d.ts +0 -7
- package/dist/wrapper.d.ts +0 -11
package/src/wrapper.ts
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
56
|
-
|
|
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
|
-
|
|
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(
|
|
74
|
-
if (isHandleWrapped(
|
|
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
|
-
|
|
98
|
+
ctx.newFunction("getSyncMode", (h) => {
|
|
79
99
|
const res = syncMode?.(unmarshal(h));
|
|
80
|
-
if (typeof res === "string") return
|
|
81
|
-
return
|
|
100
|
+
if (typeof res === "string") return ctx.newString(res);
|
|
101
|
+
return ctx.undefined;
|
|
82
102
|
}),
|
|
83
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
165
|
+
ctx: QuickJSContext,
|
|
146
166
|
handle: QuickJSHandle,
|
|
147
167
|
key: QuickJSHandle
|
|
148
168
|
): [QuickJSHandle, boolean] {
|
|
149
|
-
if (!isHandleWrapped(
|
|
150
|
-
return [
|
|
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
|
-
|
|
178
|
+
ctx: QuickJSContext,
|
|
159
179
|
handle: QuickJSHandle,
|
|
160
180
|
key: QuickJSHandle
|
|
161
181
|
): handle is Wrapped<QuickJSHandle> {
|
|
162
|
-
return !!
|
|
182
|
+
return !!ctx.dump(
|
|
163
183
|
call(
|
|
164
|
-
|
|
165
|
-
|
|
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
package/dist/index.js
DELETED
|
@@ -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;
|
package/dist/marshal/index.d.ts
DELETED
|
@@ -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;
|
package/dist/marshal/object.d.ts
DELETED
|
@@ -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;
|
package/dist/marshal/symbol.d.ts
DELETED