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/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, consumeAll, mayConsumeAll } from "./vmutil";
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
- return consumeAll(
90
- [
91
- ctx.newFunction("getSyncMode", h => {
92
- const res = syncMode?.(unmarshal(h));
93
- if (typeof res === "string") return ctx.newString(res);
94
- return ctx.undefined;
95
- }),
96
- ctx.newFunction("setter", (h, keyHandle, valueHandle) => {
97
- const target = unmarshal(h);
98
- if (!target) return;
99
- const key = unmarshal(keyHandle);
100
- if (key === "__proto__") return; // for security
101
- const value = unmarshal(valueHandle);
102
- unwrap(target, proxyKeySymbol)[key] = value;
103
- }),
104
- ctx.newFunction("deleter", (h, keyHandle) => {
105
- const target = unmarshal(h);
106
- if (!target) return;
107
- const key = unmarshal(keyHandle);
108
- delete unwrap(target, proxyKeySymbol)[key];
109
- }),
110
- ],
111
- args => [
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, getSyncMode, setter, deleter) => {
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 = getSyncMode(receiver) ?? "vm";
136
+ const sync = proxyFuncs(1, receiver) ?? "vm";
124
137
  if (sync === "host" || Reflect.set(obj, key, v, receiver)) {
125
138
  if (sync !== "vm") {
126
- setter(receiver, key, v);
139
+ proxyFuncs(2, receiver, key, v);
127
140
  }
128
141
  }
129
142
  return true;
130
143
  },
131
144
  deleteProperty(obj, key) {
132
- const sync = getSyncMode(rec) ?? "vm";
145
+ const sync = proxyFuncs(1, rec) ?? "vm";
133
146
  if (sync === "host" || Reflect.deleteProperty(obj, key)) {
134
147
  if (sync !== "vm") {
135
- deleter(rec, key);
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
- ...args,
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 {