Skip to content

Commit

Permalink
fix: quickjs reports errors when promises are used
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jul 13, 2022
1 parent 9909d87 commit c887945
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("evalCode", () => {
expect(await promise).toBe("hoge!");

arena.dispose();
// ctx.dispose(); // reports an error
ctx.dispose();
});

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

deferred.resolve?.("hoge");
await promise;
expect(arena.executePendingJobs()).toBe(1);
expect(res).toBeCalledWith("hoge!");

arena.dispose();
// ctx.dispose(); // reports an error
ctx.dispose();
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class Arena {

let wrappedT = this._wrap(t);
const [wrappedH] = this._wrapHandle(h);
const isPromise = wrappedT instanceof Promise;
const isPromise = t instanceof Promise;
if (!wrappedH || (!wrappedT && !isPromise)) return; // t or h is not an object
if (isPromise) wrappedT = t;

Expand Down
7 changes: 7 additions & 0 deletions src/wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ test("wrapHandle, unwrapHandle, isHandleWrapped", async () => {
);
expect(wrapped2 === wrapped).toBe(true);

// promise cannot be wrapped
const deferred = ctx.newPromise();
expect(isHandleWrapped(ctx, deferred.handle, proxyKeySymbolHandle)).toBe(
true
);

deferred.dispose();
wrapped.dispose();
handle.dispose();
proxyKeySymbolHandle.dispose();
Expand Down
4 changes: 3 additions & 1 deletion src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function wrap<T = any>(
marshal: (target: any) => [QuickJSHandle, boolean],
syncMode?: (target: T) => SyncMode | undefined
): Wrapped<T> | undefined {
// promise cannot be wrapped
if (!isObject(target) || target instanceof Promise) return undefined;
if (isWrapped(target, proxyKeySymbol)) return target;

Expand Down Expand Up @@ -181,7 +182,8 @@ export function isHandleWrapped(
return !!ctx.dump(
call(
ctx,
`(a, s) => (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`,
// promise cannot be wrapped
`(a, s) => (a instanceof Promise) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`,
undefined,
handle,
key
Expand Down

0 comments on commit c887945

Please sign in to comment.