-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Date and custom marshaller options
- Loading branch information
Showing
16 changed files
with
320 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { getQuickJS } from "quickjs-emscripten"; | ||
import { expect, test, vi } from "vitest"; | ||
import { call } from "../vmutil"; | ||
|
||
import marshalCustom, { defaultCustom } from "./custom"; | ||
|
||
test("symbol", async () => { | ||
const ctx = (await getQuickJS()).newContext(); | ||
const pre = vi.fn(); | ||
const sym = Symbol("foobar"); | ||
|
||
const marshal = (t: unknown) => marshalCustom(ctx, t, pre, defaultCustom); | ||
|
||
expect(marshal({})).toBe(undefined); | ||
expect(pre).toBeCalledTimes(0); | ||
|
||
const handle = marshal(sym); | ||
if (!handle) throw new Error("handle is undefined"); | ||
expect(ctx.typeof(handle)).toBe("symbol"); | ||
expect(ctx.getString(ctx.getProp(handle, "description"))).toBe("foobar"); | ||
expect(pre).toReturnTimes(1); | ||
expect(pre.mock.calls[0][0]).toBe(sym); | ||
expect(pre.mock.calls[0][1] === handle).toBe(true); | ||
|
||
handle.dispose(); | ||
ctx.dispose(); | ||
}); | ||
|
||
test("date", async () => { | ||
const ctx = (await getQuickJS()).newContext(); | ||
const pre = vi.fn(); | ||
const date = new Date(2022, 7, 26); | ||
|
||
const marshal = (t: unknown) => marshalCustom(ctx, t, pre, defaultCustom); | ||
|
||
expect(marshal({})).toBe(undefined); | ||
expect(pre).toBeCalledTimes(0); | ||
|
||
const handle = marshal(date); | ||
if (!handle) throw new Error("handle is undefined"); | ||
expect(ctx.dump(call(ctx, "d => d instanceof Date", undefined, handle))).toBe( | ||
true | ||
); | ||
expect(ctx.dump(call(ctx, "d => d.getTime()", undefined, handle))).toBe( | ||
date.getTime() | ||
); | ||
expect(pre).toReturnTimes(1); | ||
expect(pre.mock.calls[0][0]).toBe(date); | ||
expect(pre.mock.calls[0][1] === handle).toBe(true); | ||
|
||
handle.dispose(); | ||
ctx.dispose(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten"; | ||
|
||
import { call } from "../vmutil"; | ||
|
||
export default function marshalCustom( | ||
ctx: QuickJSContext, | ||
target: unknown, | ||
preMarshal: ( | ||
target: unknown, | ||
handle: QuickJSHandle | ||
) => QuickJSHandle | undefined, | ||
custom: Iterable< | ||
(target: unknown, ctx: QuickJSContext) => QuickJSHandle | undefined | ||
> | ||
): QuickJSHandle | undefined { | ||
let handle: QuickJSHandle | undefined; | ||
for (const c of custom) { | ||
handle = c(target, ctx); | ||
if (handle) break; | ||
} | ||
return handle ? preMarshal(target, handle) ?? handle : undefined; | ||
} | ||
|
||
export function symbol( | ||
target: unknown, | ||
ctx: QuickJSContext | ||
): QuickJSHandle | undefined { | ||
if (typeof target !== "symbol") return; | ||
const handle = call( | ||
ctx, | ||
"d => Symbol(d)", | ||
undefined, | ||
target.description ? ctx.newString(target.description) : ctx.undefined | ||
); | ||
return handle; | ||
} | ||
|
||
export function date( | ||
target: unknown, | ||
ctx: QuickJSContext | ||
): QuickJSHandle | undefined { | ||
if (!(target instanceof Date)) return; | ||
const handle = call( | ||
ctx, | ||
"d => new Date(d)", | ||
undefined, | ||
ctx.newNumber(target.getTime()) | ||
); | ||
return handle; | ||
} | ||
|
||
export const defaultCustom = [symbol, date]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { getQuickJS, QuickJSHandle } from "quickjs-emscripten"; | ||
import { expect, test, vi } from "vitest"; | ||
import unmarshalCustom, { defaultCustom } from "./custom"; | ||
|
||
test("symbol", async () => { | ||
const ctx = (await getQuickJS()).newContext(); | ||
const pre = vi.fn(); | ||
const obj = ctx.newObject(); | ||
const handle = ctx.unwrapResult(ctx.evalCode(`Symbol("foobar")`)); | ||
|
||
const unmarshal = (h: QuickJSHandle): any => | ||
unmarshalCustom(ctx, h, pre, defaultCustom); | ||
|
||
expect(unmarshal(obj)).toBe(undefined); | ||
expect(pre).toBeCalledTimes(0); | ||
|
||
const sym = unmarshal(handle); | ||
expect(typeof sym).toBe("symbol"); | ||
expect((sym as any).description).toBe("foobar"); | ||
expect(pre).toReturnTimes(1); | ||
expect(pre.mock.calls[0][0]).toBe(sym); | ||
expect(pre.mock.calls[0][1] === handle).toBe(true); | ||
|
||
handle.dispose(); | ||
obj.dispose(); | ||
ctx.dispose(); | ||
}); | ||
|
||
test("date", async () => { | ||
const ctx = (await getQuickJS()).newContext(); | ||
const pre = vi.fn(); | ||
const obj = ctx.newObject(); | ||
const handle = ctx.unwrapResult(ctx.evalCode(`new Date(2022, 7, 26)`)); | ||
|
||
const unmarshal = (h: QuickJSHandle): any => | ||
unmarshalCustom(ctx, h, pre, defaultCustom); | ||
|
||
expect(unmarshal(obj)).toBe(undefined); | ||
expect(pre).toBeCalledTimes(0); | ||
|
||
const date = unmarshal(handle); | ||
expect(date).toBeInstanceOf(Date); | ||
expect(date.getTime()).toBe(new Date(2022, 7, 26).getTime()); | ||
expect(pre).toReturnTimes(1); | ||
expect(pre.mock.calls[0][0]).toBe(date); | ||
expect(pre.mock.calls[0][1] === handle).toBe(true); | ||
|
||
handle.dispose(); | ||
obj.dispose(); | ||
ctx.dispose(); | ||
}); |
Oops, something went wrong.