quickjs-emscripten-sync 1.5.0 → 1.5.2
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/dist/index.d.ts +8 -2
- package/dist/quickjs-emscripten-sync.mjs +590 -450
- package/dist/quickjs-emscripten-sync.umd.js +17 -10
- package/package.json +8 -8
- package/src/contextex.test.ts +43 -0
- package/src/contextex.ts +65 -0
- package/src/edge.test.ts +70 -0
- package/src/index.test.ts +36 -39
- package/src/index.ts +6 -2
- package/src/marshal/index.test.ts +2 -2
- package/src/vmmap.ts +1 -1
- package/src/wrapper.ts +44 -32
package/src/wrapper.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { QuickJSHandle, QuickJSContext } from "quickjs-emscripten";
|
|
2
2
|
|
|
3
3
|
import { isObject } from "./util";
|
|
4
|
-
import { call, isHandleObject,
|
|
4
|
+
import { call, isHandleObject, mayConsumeAll } from "./vmutil";
|
|
5
5
|
|
|
6
6
|
export type SyncMode = "both" | "vm" | "host";
|
|
7
7
|
|
|
@@ -86,32 +86,45 @@ export function wrapHandle(
|
|
|
86
86
|
|
|
87
87
|
if (isHandleWrapped(ctx, handle, proxyKeySymbolHandle)) return [handle, false];
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
89
|
+
const getSyncMode = (h: QuickJSHandle) => {
|
|
90
|
+
const res = syncMode?.(unmarshal(h));
|
|
91
|
+
if (typeof res === "string") return ctx.newString(res);
|
|
92
|
+
return ctx.undefined;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const setter = (h: QuickJSHandle, keyHandle: QuickJSHandle, valueHandle: QuickJSHandle) => {
|
|
96
|
+
const target = unmarshal(h);
|
|
97
|
+
if (!target) return;
|
|
98
|
+
const key = unmarshal(keyHandle);
|
|
99
|
+
if (key === "__proto__") return; // for security
|
|
100
|
+
const value = unmarshal(valueHandle);
|
|
101
|
+
unwrap(target, proxyKeySymbol)[key] = value;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const deleter = (h: QuickJSHandle, keyHandle: QuickJSHandle) => {
|
|
105
|
+
const target = unmarshal(h);
|
|
106
|
+
if (!target) return;
|
|
107
|
+
const key = unmarshal(keyHandle);
|
|
108
|
+
delete unwrap(target, proxyKeySymbol)[key];
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return ctx
|
|
112
|
+
.newFunction("proxyFuncs", (t, ...args) => {
|
|
113
|
+
const name = ctx.getNumber(t);
|
|
114
|
+
switch (name) {
|
|
115
|
+
case 1:
|
|
116
|
+
return getSyncMode(args[0]);
|
|
117
|
+
case 2:
|
|
118
|
+
return setter(args[0], args[1], args[2]);
|
|
119
|
+
case 3:
|
|
120
|
+
return deleter(args[0], args[1]);
|
|
121
|
+
}
|
|
122
|
+
return ctx.undefined;
|
|
123
|
+
})
|
|
124
|
+
.consume(proxyFuncs => [
|
|
112
125
|
call(
|
|
113
126
|
ctx,
|
|
114
|
-
`(target, sym,
|
|
127
|
+
`(target, sym, proxyFuncs) => {
|
|
115
128
|
const rec = new Proxy(target, {
|
|
116
129
|
get(obj, key, receiver) {
|
|
117
130
|
return key === sym ? obj : Reflect.get(obj, key, receiver)
|
|
@@ -120,19 +133,19 @@ export function wrapHandle(
|
|
|
120
133
|
const v = typeof value === "object" && value !== null || typeof value === "function"
|
|
121
134
|
? value[sym] ?? value
|
|
122
135
|
: value;
|
|
123
|
-
const sync =
|
|
136
|
+
const sync = proxyFuncs(1, receiver) ?? "vm";
|
|
124
137
|
if (sync === "host" || Reflect.set(obj, key, v, receiver)) {
|
|
125
138
|
if (sync !== "vm") {
|
|
126
|
-
|
|
139
|
+
proxyFuncs(2, receiver, key, v);
|
|
127
140
|
}
|
|
128
141
|
}
|
|
129
142
|
return true;
|
|
130
143
|
},
|
|
131
144
|
deleteProperty(obj, key) {
|
|
132
|
-
const sync =
|
|
145
|
+
const sync = proxyFuncs(1, rec) ?? "vm";
|
|
133
146
|
if (sync === "host" || Reflect.deleteProperty(obj, key)) {
|
|
134
147
|
if (sync !== "vm") {
|
|
135
|
-
|
|
148
|
+
proxyFuncs(3, rec, key);
|
|
136
149
|
}
|
|
137
150
|
}
|
|
138
151
|
return true;
|
|
@@ -143,11 +156,10 @@ export function wrapHandle(
|
|
|
143
156
|
undefined,
|
|
144
157
|
handle,
|
|
145
158
|
proxyKeySymbolHandle,
|
|
146
|
-
|
|
159
|
+
proxyFuncs,
|
|
147
160
|
) as Wrapped<QuickJSHandle>,
|
|
148
161
|
true,
|
|
149
|
-
]
|
|
150
|
-
);
|
|
162
|
+
]);
|
|
151
163
|
}
|
|
152
164
|
|
|
153
165
|
export function unwrap<T>(obj: T, key: string | symbol): T {
|