-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcontext-asyncify.ts
94 lines (90 loc) · 3.24 KB
/
context-asyncify.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import type {
QuickJSAsyncEmscriptenModule,
QuickJSAsyncFFI,
EvalDetectModule,
EvalFlags,
JSRuntimePointer,
JSValuePointer,
} from "@jitl/quickjs-ffi-types"
import type { QuickJSContextResult } from "./context"
import { QuickJSContext } from "./context"
import type { Lifetime } from "./lifetime"
import type { QuickJSModuleCallbacks } from "./module"
import type { QuickJSAsyncRuntime } from "./runtime-asyncify"
import type { ContextEvalOptions, QuickJSHandle } from "./types"
import { evalOptionsToFlags } from "./types"
import type { VmCallResult } from "./vm-interface"
export type AsyncFunctionImplementation = (
this: QuickJSHandle,
...args: QuickJSHandle[]
) => Promise<QuickJSHandle | VmCallResult<QuickJSHandle> | void>
/**
* Asyncified version of {@link QuickJSContext}.
*
* *Asyncify* allows normally synchronous code to wait for asynchronous Promises
* or callbacks. The asyncified version of QuickJSContext can wait for async
* host functions as though they were synchronous.
*/
export class QuickJSAsyncContext extends QuickJSContext {
public declare runtime: QuickJSAsyncRuntime
/** @private */
protected declare module: QuickJSAsyncEmscriptenModule
/** @private */
protected declare ffi: QuickJSAsyncFFI
/** @private */
protected declare rt: Lifetime<JSRuntimePointer>
/** @private */
protected declare callbacks: QuickJSModuleCallbacks
/**
* Asyncified version of {@link evalCode}.
*/
async evalCodeAsync(
code: string,
filename: string = "eval.js",
/** See {@link EvalFlags} for number semantics */
options?: number | ContextEvalOptions,
): Promise<QuickJSContextResult<QuickJSHandle>> {
const detectModule = (options === undefined ? 1 : 0) as EvalDetectModule
const flags = evalOptionsToFlags(options) as EvalFlags
let resultPtr = 0 as JSValuePointer
try {
resultPtr = await this.memory
.newHeapCharPointer(code)
.consume((charHandle) =>
this.ffi.QTS_Eval_MaybeAsync(
this.ctx.value,
charHandle.value.ptr,
charHandle.value.strlen,
filename,
detectModule,
flags,
),
)
} catch (error) {
this.runtime.debugLog("QTS_Eval_MaybeAsync threw", error)
throw error
}
const errorPtr = this.ffi.QTS_ResolveException(this.ctx.value, resultPtr)
if (errorPtr) {
this.ffi.QTS_FreeValuePointer(this.ctx.value, resultPtr)
return this.fail(this.memory.heapValueHandle(errorPtr))
}
return this.success(this.memory.heapValueHandle(resultPtr))
}
/**
* Similar to {@link newFunction}.
* Convert an async host Javascript function into a synchronous QuickJS function value.
*
* Whenever QuickJS calls this function, the VM's stack will be unwound while
* waiting the async function to complete, and then restored when the returned
* promise resolves.
*
* Asyncified functions must never call other asyncified functions or
* `import`, even indirectly, because the stack cannot be unwound twice.
*
* See [Emscripten's docs on Asyncify](https://emscripten.org/docs/porting/asyncify.html).
*/
newAsyncifiedFunction(name: string, fn: AsyncFunctionImplementation): QuickJSHandle {
return this.newFunction(name, fn as any)
}
}