-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathmod.ts
79 lines (74 loc) · 2.74 KB
/
mod.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
import type {
AsyncRuntimeOptions,
ContextOptions,
QuickJSAsyncContext,
QuickJSAsyncRuntime,
QuickJSWASMModule,
} from "quickjs-emscripten-core"
import { newQuickJSAsyncWASMModule, newQuickJSWASMModule } from "#variants"
let singleton: QuickJSWASMModule | undefined = undefined
let singletonPromise: Promise<QuickJSWASMModule> | undefined = undefined
/**
* Get a shared singleton {@link QuickJSWASMModule}. Use this to evaluate code
* or create Javascript environments.
*
* This is the top-level entrypoint for the quickjs-emscripten library.
*
* If you need strictest possible isolation guarantees, you may create a
* separate {@link QuickJSWASMModule} via {@link newQuickJSWASMModule}.
*
* To work with the asyncified version of this library, see these functions:
*
* - {@link newAsyncRuntime}.
* - {@link newAsyncContext}.
* - {@link newQuickJSAsyncWASMModule}.
*/
export async function getQuickJS(): Promise<QuickJSWASMModule> {
singletonPromise ??= newQuickJSWASMModule().then((instance) => {
singleton = instance
return instance
})
return await singletonPromise
}
/**
* Provides synchronous access to the shared {@link QuickJSWASMModule} instance returned by {@link getQuickJS}, as long as
* least once.
* @throws If called before `getQuickJS` resolves.
*/
export function getQuickJSSync(): QuickJSWASMModule {
if (!singleton) {
throw new Error("QuickJS not initialized. Await getQuickJS() at least once.")
}
return singleton
}
/**
* Create a new {@link QuickJSAsyncRuntime} in a separate WebAssembly module.
*
* Each runtime is isolated in a separate WebAssembly module, so that errors in
* one runtime cannot contaminate another runtime, and each runtime can execute
* an asynchronous action without conflicts.
*
* Note that there is a hard limit on the number of WebAssembly modules in older
* versions of v8:
* https://bugs.chromium.org/p/v8/issues/detail?id=12076
*/
export async function newAsyncRuntime(options?: AsyncRuntimeOptions): Promise<QuickJSAsyncRuntime> {
const module = await newQuickJSAsyncWASMModule()
return module.newRuntime(options)
}
/**
* Create a new {@link QuickJSAsyncContext} (with an associated runtime) in an
* separate WebAssembly module.
*
* Each context is isolated in a separate WebAssembly module, so that errors in
* one runtime cannot contaminate another runtime, and each runtime can execute
* an asynchronous action without conflicts.
*
* Note that there is a hard limit on the number of WebAssembly modules in older
* versions of v8:
* https://bugs.chromium.org/p/v8/issues/detail?id=12076
*/
export async function newAsyncContext(options?: ContextOptions): Promise<QuickJSAsyncContext> {
const module = await newQuickJSAsyncWASMModule()
return module.newContext(options)
}