-
Notifications
You must be signed in to change notification settings - Fork 7
/
custom.ts
31 lines (26 loc) · 1.04 KB
/
custom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
import { call } from "../vmutil";
export default function unmarshalCustom(
ctx: QuickJSContext,
handle: QuickJSHandle,
preUnmarshal: <T>(target: T, handle: QuickJSHandle) => T | undefined,
custom: Iterable<(handle: QuickJSHandle, ctx: QuickJSContext) => any>,
): symbol | undefined {
let obj: any;
for (const c of custom) {
obj = c(handle, ctx);
if (obj) break;
}
return obj ? preUnmarshal(obj, handle) ?? obj : undefined;
}
export function symbol(handle: QuickJSHandle, ctx: QuickJSContext): symbol | undefined {
if (ctx.typeof(handle) !== "symbol") return;
const desc = ctx.getString(ctx.getProp(handle, "description"));
return Symbol(desc);
}
export function date(handle: QuickJSHandle, ctx: QuickJSContext): Date | undefined {
if (!ctx.dump(call(ctx, "a => a instanceof Date", undefined, handle))) return;
const t = ctx.getNumber(call(ctx, "a => a.getTime()", undefined, handle));
return new Date(t);
}
export const defaultCustom = [symbol, date];