Skip to content

Commit c887945

Browse files
committed
fix: quickjs reports errors when promises are used
1 parent 9909d87 commit c887945

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

src/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe("evalCode", () => {
186186
expect(await promise).toBe("hoge!");
187187

188188
arena.dispose();
189-
// ctx.dispose(); // reports an error
189+
ctx.dispose();
190190
});
191191

192192
test("promise2", async () => {
@@ -198,15 +198,15 @@ describe("evalCode", () => {
198198
deferred.resolve = resolve;
199199
});
200200
const res = vi.fn();
201-
arena.evalCode(`(p, r) => { p.then(d => r(d + "!")); }`)(promise, res);
201+
arena.evalCode(`(p, r) => { p.then(d => { r(d + "!"); }); }`)(promise, res);
202202

203203
deferred.resolve?.("hoge");
204204
await promise;
205205
expect(arena.executePendingJobs()).toBe(1);
206206
expect(res).toBeCalledWith("hoge!");
207207

208208
arena.dispose();
209-
// ctx.dispose(); // reports an error
209+
ctx.dispose();
210210
});
211211
});
212212

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export class Arena {
305305

306306
let wrappedT = this._wrap(t);
307307
const [wrappedH] = this._wrapHandle(h);
308-
const isPromise = wrappedT instanceof Promise;
308+
const isPromise = t instanceof Promise;
309309
if (!wrappedH || (!wrappedT && !isPromise)) return; // t or h is not an object
310310
if (isPromise) wrappedT = t;
311311

src/wrapper.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ test("wrapHandle, unwrapHandle, isHandleWrapped", async () => {
240240
);
241241
expect(wrapped2 === wrapped).toBe(true);
242242

243+
// promise cannot be wrapped
244+
const deferred = ctx.newPromise();
245+
expect(isHandleWrapped(ctx, deferred.handle, proxyKeySymbolHandle)).toBe(
246+
true
247+
);
248+
249+
deferred.dispose();
243250
wrapped.dispose();
244251
handle.dispose();
245252
proxyKeySymbolHandle.dispose();

src/wrapper.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function wrap<T = any>(
1515
marshal: (target: any) => [QuickJSHandle, boolean],
1616
syncMode?: (target: T) => SyncMode | undefined
1717
): Wrapped<T> | undefined {
18+
// promise cannot be wrapped
1819
if (!isObject(target) || target instanceof Promise) return undefined;
1920
if (isWrapped(target, proxyKeySymbol)) return target;
2021

@@ -181,7 +182,8 @@ export function isHandleWrapped(
181182
return !!ctx.dump(
182183
call(
183184
ctx,
184-
`(a, s) => (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`,
185+
// promise cannot be wrapped
186+
`(a, s) => (a instanceof Promise) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`,
185187
undefined,
186188
handle,
187189
key

0 commit comments

Comments
 (0)