@componentor/quickjs-emscripten-core 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +341 -0
- package/README.template.md +70 -0
- package/dist/chunk-IBRLVSKW.mjs +7 -0
- package/dist/chunk-IBRLVSKW.mjs.map +1 -0
- package/dist/chunk-TKX7BUWH.mjs +2 -0
- package/dist/chunk-TKX7BUWH.mjs.map +1 -0
- package/dist/index.d.mts +2005 -0
- package/dist/index.d.ts +2005 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/module-4LU5GMK7.mjs +2 -0
- package/dist/module-4LU5GMK7.mjs.map +1 -0
- package/dist/module-asyncify-PHASWSB5.mjs +2 -0
- package/dist/module-asyncify-PHASWSB5.mjs.map +1 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2005 @@
|
|
|
1
|
+
import * as _componentor_quickjs_ffi_types from '@componentor/quickjs-ffi-types';
|
|
2
|
+
import { EitherModule, JSValueConstPointerPointer, OwnedHeapCharPointer, JSVoidPointer, EitherFFI, JSRuntimePointer, JSContextPointer, QuickJSAsyncEmscriptenModule, QuickJSAsyncFFI, JSValueConstPointer, JSValuePointer, JSBorrowedCharPointer, UInt32Pointer, EmscriptenModuleCallbacks, Asyncify, AsyncifySleepResult, QuickJSSyncVariant, QuickJSAsyncVariant, SourceMapData, EmscriptenModuleLoaderOptions, QuickJSVariant } from '@componentor/quickjs-ffi-types';
|
|
3
|
+
export * from '@componentor/quickjs-ffi-types';
|
|
4
|
+
export { QuickJSAsyncVariant, QuickJSSyncVariant, QuickJSVariant } from '@componentor/quickjs-ffi-types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Used as an optional.
|
|
8
|
+
* `{ value: S } | { error: E }`.
|
|
9
|
+
*/
|
|
10
|
+
type SuccessOrFail<S, F> = {
|
|
11
|
+
value: S;
|
|
12
|
+
error?: undefined;
|
|
13
|
+
} | {
|
|
14
|
+
error: F;
|
|
15
|
+
};
|
|
16
|
+
declare function isSuccess<S, F>(successOrFail: SuccessOrFail<S, F>): successOrFail is {
|
|
17
|
+
value: S;
|
|
18
|
+
};
|
|
19
|
+
declare function isFail<S, F>(successOrFail: SuccessOrFail<S, F>): successOrFail is {
|
|
20
|
+
error: F;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Used as an optional for results of a Vm call.
|
|
24
|
+
* `{ value: VmHandle } | { error: VmHandle }`.
|
|
25
|
+
*/
|
|
26
|
+
type VmCallResult<VmHandle> = SuccessOrFail<VmHandle, VmHandle>;
|
|
27
|
+
/**
|
|
28
|
+
* A VmFunctionImplementation takes handles as arguments.
|
|
29
|
+
* It should return a handle, or be void.
|
|
30
|
+
*
|
|
31
|
+
* To indicate an exception, a VMs can throw either a handle (transferred
|
|
32
|
+
* directly) or any other Javascript value (only the poperties `name` and
|
|
33
|
+
* `message` will be transferred). Or, the VmFunctionImplementation may return
|
|
34
|
+
* a VmCallResult's `{ error: handle }` error variant.
|
|
35
|
+
*
|
|
36
|
+
* VmFunctionImplementation should not free its arguments or its return value.
|
|
37
|
+
* It should not retain a reference to its return value or thrown error.
|
|
38
|
+
*/
|
|
39
|
+
type VmFunctionImplementation<VmHandle> = (this: VmHandle, ...args: VmHandle[]) => VmHandle | VmCallResult<VmHandle> | void;
|
|
40
|
+
/**
|
|
41
|
+
* A minimal interface to a Javascript execution environment.
|
|
42
|
+
*
|
|
43
|
+
* Higher-level tools should build over the LowLevelJavascriptVm interface to
|
|
44
|
+
* share as much as possible between executors.
|
|
45
|
+
*
|
|
46
|
+
* From https://www.figma.com/blog/how-we-built-the-figma-plugin-system/
|
|
47
|
+
*/
|
|
48
|
+
interface LowLevelJavascriptVm<VmHandle> {
|
|
49
|
+
global: VmHandle;
|
|
50
|
+
undefined: VmHandle;
|
|
51
|
+
typeof(handle: VmHandle): string;
|
|
52
|
+
getNumber(handle: VmHandle): number;
|
|
53
|
+
getString(handle: VmHandle): string;
|
|
54
|
+
newNumber(value: number): VmHandle;
|
|
55
|
+
newString(value: string): VmHandle;
|
|
56
|
+
newObject(prototype?: VmHandle): VmHandle;
|
|
57
|
+
newFunction(name: string, value: VmFunctionImplementation<VmHandle>): VmHandle;
|
|
58
|
+
getProp(handle: VmHandle, key: string | VmHandle): VmHandle;
|
|
59
|
+
setProp(handle: VmHandle, key: string | VmHandle, value: VmHandle): void;
|
|
60
|
+
defineProp(handle: VmHandle, key: string | VmHandle, descriptor: VmPropertyDescriptor<VmHandle>): void;
|
|
61
|
+
callFunction(func: VmHandle, thisVal: VmHandle, ...args: VmHandle[]): VmCallResult<VmHandle>;
|
|
62
|
+
evalCode(code: string, filename?: string): VmCallResult<VmHandle>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* From https://www.figma.com/blog/how-we-built-the-figma-plugin-system/
|
|
66
|
+
*/
|
|
67
|
+
interface VmPropertyDescriptor<VmHandle> {
|
|
68
|
+
value?: VmHandle;
|
|
69
|
+
configurable?: boolean;
|
|
70
|
+
enumerable?: boolean;
|
|
71
|
+
get?: (this: VmHandle) => VmHandle;
|
|
72
|
+
set?: (this: VmHandle, value: VmHandle) => void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
declare function awaitYield<T>(value: T | Promise<T>): Generator<T | Promise<T>, T, T>;
|
|
76
|
+
declare function awaitYieldOf<T, Yielded>(generator: Generator<Yielded | Promise<Yielded>, T, Yielded>): Generator<T | Promise<T>, T, T>;
|
|
77
|
+
type AwaitYield = typeof awaitYield & {
|
|
78
|
+
of: typeof awaitYieldOf;
|
|
79
|
+
};
|
|
80
|
+
type MaybeAsyncBlock<Return, This, Yielded, Args extends any[] = []> = (this: This, awaited: AwaitYield, ...args: Args) => Generator<Yielded | Promise<Yielded>, Return, Yielded>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
type HeapUint8Array = {
|
|
86
|
+
pointer: JSVoidPointer;
|
|
87
|
+
numBytes: number;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Add more types as needed.
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
type TypedArray = Int32Array<ArrayBuffer> | Uint32Array<ArrayBuffer>;
|
|
94
|
+
/** @private */
|
|
95
|
+
interface TypedArrayConstructor<T> {
|
|
96
|
+
new (length: number): T;
|
|
97
|
+
new (array: ArrayLike<number> | ArrayBufferLike): T;
|
|
98
|
+
new (buffer: ArrayBufferLike, byteOffset?: number, length?: number): T;
|
|
99
|
+
BYTES_PER_ELEMENT: number;
|
|
100
|
+
}
|
|
101
|
+
/** @private */
|
|
102
|
+
type HeapTypedArray<JS extends TypedArray, C extends number> = Lifetime<{
|
|
103
|
+
typedArray: JS;
|
|
104
|
+
ptr: C;
|
|
105
|
+
}>;
|
|
106
|
+
/**
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
declare class ModuleMemory {
|
|
110
|
+
module: EitherModule;
|
|
111
|
+
constructor(module: EitherModule);
|
|
112
|
+
toPointerArray(handleArray: QuickJSHandle[]): Lifetime<JSValueConstPointerPointer>;
|
|
113
|
+
newTypedArray<JS extends TypedArray, C extends number>(kind: TypedArrayConstructor<JS>, length: number): HeapTypedArray<JS, C>;
|
|
114
|
+
newMutablePointerArray<T extends number>(length: number): HeapTypedArray<Int32Array<ArrayBuffer>, T>;
|
|
115
|
+
newHeapCharPointer(string: string): Lifetime<{
|
|
116
|
+
ptr: OwnedHeapCharPointer;
|
|
117
|
+
strlen: number;
|
|
118
|
+
}>;
|
|
119
|
+
newHeapBufferPointer(buffer: Uint8Array): Lifetime<HeapUint8Array>;
|
|
120
|
+
consumeHeapCharPointer(ptr: OwnedHeapCharPointer): string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Callback called regularly while the VM executes code.
|
|
125
|
+
* Determines if a VM's execution should be interrupted.
|
|
126
|
+
*
|
|
127
|
+
* @returns `true` to interrupt JS execution inside the VM.
|
|
128
|
+
* @returns `false` or `undefined` to continue JS execution inside the VM.
|
|
129
|
+
*/
|
|
130
|
+
type InterruptHandler = (runtime: QuickJSRuntime) => boolean | undefined | void;
|
|
131
|
+
/**
|
|
132
|
+
* Used as an optional for the results of executing pendingJobs.
|
|
133
|
+
* On success, `value` contains the number of async jobs executed
|
|
134
|
+
* by the runtime.
|
|
135
|
+
* @source
|
|
136
|
+
*/
|
|
137
|
+
type ExecutePendingJobsResult = DisposableResult<
|
|
138
|
+
/** Number of jobs successfully executed. */
|
|
139
|
+
number,
|
|
140
|
+
/** The error that occurred. */
|
|
141
|
+
QuickJSHandle & {
|
|
142
|
+
/** The context where the error occurred. */
|
|
143
|
+
context: QuickJSContext;
|
|
144
|
+
}>;
|
|
145
|
+
/**
|
|
146
|
+
* A runtime represents a Javascript runtime corresponding to an object heap.
|
|
147
|
+
* Several runtimes can exist at the same time but they cannot exchange objects.
|
|
148
|
+
* Inside a given runtime, no multi-threading is supported.
|
|
149
|
+
*
|
|
150
|
+
* You can think of separate runtimes like different domains in a browser, and
|
|
151
|
+
* the contexts within a runtime like the different windows open to the same
|
|
152
|
+
* domain.
|
|
153
|
+
*
|
|
154
|
+
* Create a runtime via {@link QuickJSWASMModule.newRuntime}.
|
|
155
|
+
*
|
|
156
|
+
* You should create separate runtime instances for untrusted code from
|
|
157
|
+
* different sources for isolation. However, stronger isolation is also
|
|
158
|
+
* available (at the cost of memory usage), by creating separate WebAssembly
|
|
159
|
+
* modules to further isolate untrusted code.
|
|
160
|
+
* See {@link newQuickJSWASMModule}.
|
|
161
|
+
*
|
|
162
|
+
* Implement memory and CPU constraints with {@link setInterruptHandler}
|
|
163
|
+
* (called regularly while the interpreter runs), {@link setMemoryLimit}, and
|
|
164
|
+
* {@link setMaxStackSize}.
|
|
165
|
+
* Use {@link computeMemoryUsage} or {@link dumpMemoryUsage} to guide memory limit
|
|
166
|
+
* tuning.
|
|
167
|
+
*
|
|
168
|
+
* Configure ES module loading with {@link setModuleLoader}.
|
|
169
|
+
*/
|
|
170
|
+
declare class QuickJSRuntime extends UsingDisposable implements Disposable$1 {
|
|
171
|
+
/**
|
|
172
|
+
* If this runtime was created as as part of a context, points to the context
|
|
173
|
+
* associated with the runtime.
|
|
174
|
+
*
|
|
175
|
+
* If this runtime was created stand-alone, this may or may not contain a context.
|
|
176
|
+
* A context here may be allocated if one is needed by the runtime, eg for {@link computeMemoryUsage}.
|
|
177
|
+
*/
|
|
178
|
+
context: QuickJSContext | undefined;
|
|
179
|
+
/** @private */
|
|
180
|
+
protected module: EitherModule;
|
|
181
|
+
/** @private */
|
|
182
|
+
protected memory: ModuleMemory;
|
|
183
|
+
/** @private */
|
|
184
|
+
protected ffi: EitherFFI;
|
|
185
|
+
/** @private */
|
|
186
|
+
protected rt: Lifetime<JSRuntimePointer>;
|
|
187
|
+
/** @private */
|
|
188
|
+
protected callbacks: QuickJSModuleCallbacks;
|
|
189
|
+
/** @private */
|
|
190
|
+
protected scope: Scope;
|
|
191
|
+
/** @private */
|
|
192
|
+
protected contextMap: Map<JSContextPointer, QuickJSContext>;
|
|
193
|
+
/** @private */
|
|
194
|
+
protected moduleLoader: JSModuleLoader | undefined;
|
|
195
|
+
/** @private */
|
|
196
|
+
protected moduleNormalizer: JSModuleNormalizer | undefined;
|
|
197
|
+
/** @private */
|
|
198
|
+
constructor(args: {
|
|
199
|
+
module: EitherModule;
|
|
200
|
+
ffi: EitherFFI;
|
|
201
|
+
rt: Lifetime<JSRuntimePointer>;
|
|
202
|
+
callbacks: QuickJSModuleCallbacks;
|
|
203
|
+
ownedLifetimes?: Disposable$1[];
|
|
204
|
+
});
|
|
205
|
+
get alive(): boolean;
|
|
206
|
+
dispose(): void;
|
|
207
|
+
/**
|
|
208
|
+
* Create a new context within this runtime. Contexts have isolated globals,
|
|
209
|
+
* but you can explicitly share objects between contexts with the same
|
|
210
|
+
* runtime.
|
|
211
|
+
*
|
|
212
|
+
* You should dispose a created context before disposing this runtime.
|
|
213
|
+
*/
|
|
214
|
+
newContext(options?: ContextOptions): QuickJSContext;
|
|
215
|
+
/**
|
|
216
|
+
* Set the loader for EcmaScript modules requested by any context in this
|
|
217
|
+
* runtime.
|
|
218
|
+
*
|
|
219
|
+
* The loader can be removed with {@link removeModuleLoader}.
|
|
220
|
+
*/
|
|
221
|
+
setModuleLoader(moduleLoader: JSModuleLoader, moduleNormalizer?: JSModuleNormalizer): void;
|
|
222
|
+
/**
|
|
223
|
+
* Remove the the loader set by {@link setModuleLoader}. This disables module loading.
|
|
224
|
+
*/
|
|
225
|
+
removeModuleLoader(): void;
|
|
226
|
+
/**
|
|
227
|
+
* In QuickJS, promises and async functions create pendingJobs. These do not execute
|
|
228
|
+
* immediately and need to be run by calling {@link executePendingJobs}.
|
|
229
|
+
*
|
|
230
|
+
* @return true if there is at least one pendingJob queued up.
|
|
231
|
+
*/
|
|
232
|
+
hasPendingJob(): boolean;
|
|
233
|
+
private interruptHandler;
|
|
234
|
+
/**
|
|
235
|
+
* Set a callback which is regularly called by the QuickJS engine when it is
|
|
236
|
+
* executing code. This callback can be used to implement an execution
|
|
237
|
+
* timeout.
|
|
238
|
+
*
|
|
239
|
+
* The interrupt handler can be removed with {@link removeInterruptHandler}.
|
|
240
|
+
*/
|
|
241
|
+
setInterruptHandler(cb: InterruptHandler): void;
|
|
242
|
+
/**
|
|
243
|
+
* Remove the interrupt handler, if any.
|
|
244
|
+
* See {@link setInterruptHandler}.
|
|
245
|
+
*/
|
|
246
|
+
removeInterruptHandler(): void;
|
|
247
|
+
/**
|
|
248
|
+
* Execute pendingJobs on the runtime until `maxJobsToExecute` jobs are
|
|
249
|
+
* executed (default all pendingJobs), the queue is exhausted, or the runtime
|
|
250
|
+
* encounters an exception.
|
|
251
|
+
*
|
|
252
|
+
* In QuickJS, promises and async functions *inside the runtime* create
|
|
253
|
+
* pendingJobs. These do not execute immediately and need to triggered to run.
|
|
254
|
+
*
|
|
255
|
+
* @param maxJobsToExecute - When negative, run all pending jobs. Otherwise execute
|
|
256
|
+
* at most `maxJobsToExecute` before returning.
|
|
257
|
+
*
|
|
258
|
+
* @return On success, the number of executed jobs. On error, the exception
|
|
259
|
+
* that stopped execution, and the context it occurred in. Note that
|
|
260
|
+
* executePendingJobs will not normally return errors thrown inside async
|
|
261
|
+
* functions or rejected promises. Those errors are available by calling
|
|
262
|
+
* {@link QuickJSContext#resolvePromise} on the promise handle returned by the async function.
|
|
263
|
+
*/
|
|
264
|
+
executePendingJobs(maxJobsToExecute?: number | void): ExecutePendingJobsResult;
|
|
265
|
+
/**
|
|
266
|
+
* Set the max memory this runtime can allocate.
|
|
267
|
+
* To remove the limit, set to `-1`.
|
|
268
|
+
*/
|
|
269
|
+
setMemoryLimit(limitBytes: number): void;
|
|
270
|
+
/**
|
|
271
|
+
* Compute memory usage for this runtime. Returns the result as a handle to a
|
|
272
|
+
* JSValue object. Use {@link QuickJSContext#dump} to convert to a native object.
|
|
273
|
+
* Calling this method will allocate more memory inside the runtime. The information
|
|
274
|
+
* is accurate as of just before the call to `computeMemoryUsage`.
|
|
275
|
+
* For a human-digestible representation, see {@link dumpMemoryUsage}.
|
|
276
|
+
*/
|
|
277
|
+
computeMemoryUsage(): QuickJSHandle;
|
|
278
|
+
/**
|
|
279
|
+
* @returns a human-readable description of memory usage in this runtime.
|
|
280
|
+
* For programmatic access to this information, see {@link computeMemoryUsage}.
|
|
281
|
+
*/
|
|
282
|
+
dumpMemoryUsage(): string;
|
|
283
|
+
/**
|
|
284
|
+
* Set the max stack size for this runtime, in bytes.
|
|
285
|
+
* To remove the limit, set to `0`.
|
|
286
|
+
*/
|
|
287
|
+
setMaxStackSize(stackSize: number): void;
|
|
288
|
+
/**
|
|
289
|
+
* Assert that `handle` is owned by this runtime.
|
|
290
|
+
* @throws QuickJSWrongOwner if owned by a different runtime.
|
|
291
|
+
*/
|
|
292
|
+
assertOwned(handle: QuickJSHandle): void;
|
|
293
|
+
private _debugMode;
|
|
294
|
+
/**
|
|
295
|
+
* Enable or disable debug logging.
|
|
296
|
+
*
|
|
297
|
+
* If this module is a DEBUG variant, more logs will be printed from the C
|
|
298
|
+
* code.
|
|
299
|
+
*/
|
|
300
|
+
setDebugMode(enabled: boolean): void;
|
|
301
|
+
/**
|
|
302
|
+
* @returns true if debug logging is enabled
|
|
303
|
+
*/
|
|
304
|
+
isDebugMode(): boolean;
|
|
305
|
+
/**
|
|
306
|
+
* In debug mode, log the result of calling `msg()`.
|
|
307
|
+
*
|
|
308
|
+
* We take a function instead of a log message to avoid expensive string
|
|
309
|
+
* manipulation if debug logging is disabled.
|
|
310
|
+
*/
|
|
311
|
+
debugLog(...msg: unknown[]): void;
|
|
312
|
+
private getSystemContext;
|
|
313
|
+
private cToHostCallbacks;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
declare class QuickJSAsyncRuntime extends QuickJSRuntime {
|
|
317
|
+
context: QuickJSAsyncContext | undefined;
|
|
318
|
+
/** @private */
|
|
319
|
+
protected module: QuickJSAsyncEmscriptenModule;
|
|
320
|
+
/** @private */
|
|
321
|
+
protected ffi: QuickJSAsyncFFI;
|
|
322
|
+
/** @private */
|
|
323
|
+
protected rt: Lifetime<JSRuntimePointer>;
|
|
324
|
+
/** @private */
|
|
325
|
+
protected callbacks: QuickJSModuleCallbacks;
|
|
326
|
+
/** @private */
|
|
327
|
+
protected contextMap: Map<JSContextPointer, QuickJSAsyncContext>;
|
|
328
|
+
/** @private */
|
|
329
|
+
constructor(args: {
|
|
330
|
+
module: QuickJSAsyncEmscriptenModule;
|
|
331
|
+
ffi: QuickJSAsyncFFI;
|
|
332
|
+
rt: Lifetime<JSRuntimePointer>;
|
|
333
|
+
callbacks: QuickJSModuleCallbacks;
|
|
334
|
+
});
|
|
335
|
+
newContext(options?: ContextOptions): QuickJSAsyncContext;
|
|
336
|
+
setModuleLoader(moduleLoader: JSModuleLoaderAsync, moduleNormalizer?: JSModuleNormalizerAsync): void;
|
|
337
|
+
/**
|
|
338
|
+
* Set the max stack size for this runtime in bytes.
|
|
339
|
+
* To remove the limit, set to `0`.
|
|
340
|
+
*
|
|
341
|
+
* Setting this limit also adjusts the global `ASYNCIFY_STACK_SIZE` for the entire {@link QuickJSAsyncWASMModule}.
|
|
342
|
+
* See the [pull request](https://github.com/justjake/quickjs-emscripten/pull/114) for more details.
|
|
343
|
+
*/
|
|
344
|
+
setMaxStackSize(stackSize: number): void;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
type AsyncFunctionImplementation = (this: QuickJSHandle, ...args: QuickJSHandle[]) => Promise<QuickJSHandle | VmCallResult<QuickJSHandle> | void>;
|
|
348
|
+
/**
|
|
349
|
+
* Asyncified version of {@link QuickJSContext}.
|
|
350
|
+
*
|
|
351
|
+
* *Asyncify* allows normally synchronous code to wait for asynchronous Promises
|
|
352
|
+
* or callbacks. The asyncified version of QuickJSContext can wait for async
|
|
353
|
+
* host functions as though they were synchronous.
|
|
354
|
+
*/
|
|
355
|
+
declare class QuickJSAsyncContext extends QuickJSContext {
|
|
356
|
+
runtime: QuickJSAsyncRuntime;
|
|
357
|
+
/** @private */
|
|
358
|
+
protected module: QuickJSAsyncEmscriptenModule;
|
|
359
|
+
/** @private */
|
|
360
|
+
protected ffi: QuickJSAsyncFFI;
|
|
361
|
+
/** @private */
|
|
362
|
+
protected rt: Lifetime<JSRuntimePointer>;
|
|
363
|
+
/** @private */
|
|
364
|
+
protected callbacks: QuickJSModuleCallbacks;
|
|
365
|
+
/**
|
|
366
|
+
* Asyncified version of {@link evalCode}.
|
|
367
|
+
*/
|
|
368
|
+
evalCodeAsync(code: string, filename?: string,
|
|
369
|
+
/** See {@link EvalFlags} for number semantics */
|
|
370
|
+
options?: number | ContextEvalOptions): Promise<QuickJSContextResult<QuickJSHandle>>;
|
|
371
|
+
/**
|
|
372
|
+
* Similar to {@link newFunction}.
|
|
373
|
+
* Convert an async host Javascript function into a synchronous QuickJS function value.
|
|
374
|
+
*
|
|
375
|
+
* Whenever QuickJS calls this function, the VM's stack will be unwound while
|
|
376
|
+
* waiting the async function to complete, and then restored when the returned
|
|
377
|
+
* promise resolves.
|
|
378
|
+
*
|
|
379
|
+
* Asyncified functions must never call other asyncified functions or
|
|
380
|
+
* `import`, even indirectly, because the stack cannot be unwound twice.
|
|
381
|
+
*
|
|
382
|
+
* See [Emscripten's docs on Asyncify](https://emscripten.org/docs/porting/asyncify.html).
|
|
383
|
+
*/
|
|
384
|
+
newAsyncifiedFunction(name: string, fn: AsyncFunctionImplementation): QuickJSHandle;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* A QuickJSHandle to a constant that will never change, and does not need to
|
|
389
|
+
* be disposed.
|
|
390
|
+
*/
|
|
391
|
+
type StaticJSValue = Lifetime<JSValueConstPointer, JSValueConstPointer, QuickJSRuntime>;
|
|
392
|
+
/**
|
|
393
|
+
* A QuickJSHandle to a borrowed value that does not need to be disposed.
|
|
394
|
+
*
|
|
395
|
+
* In QuickJS, a JSValueConst is a "borrowed" reference that isn't owned by the
|
|
396
|
+
* current scope. That means that the current scope should not `JS_FreeValue`
|
|
397
|
+
* it, or retain a reference to it after the scope exits, because it may be
|
|
398
|
+
* freed by its owner.
|
|
399
|
+
*
|
|
400
|
+
* quickjs-emscripten takes care of disposing JSValueConst references.
|
|
401
|
+
*/
|
|
402
|
+
type JSValueConst = Lifetime<JSValueConstPointer, JSValuePointer, QuickJSRuntime>;
|
|
403
|
+
/**
|
|
404
|
+
* A owned QuickJSHandle that should be disposed or returned.
|
|
405
|
+
*
|
|
406
|
+
* The QuickJS interpreter passes Javascript values between functions as
|
|
407
|
+
* `JSValue` structs that references some internal data. Because passing
|
|
408
|
+
* structs cross the Empscripten FFI interfaces is bothersome, we use pointers
|
|
409
|
+
* to these structs instead.
|
|
410
|
+
*
|
|
411
|
+
* A JSValue reference is "owned" in its scope. before exiting the scope, it
|
|
412
|
+
* should be freed, by calling `JS_FreeValue(ctx, js_value)`) or returned from
|
|
413
|
+
* the scope. We extend that contract - a JSValuePointer (`JSValue*`) must also
|
|
414
|
+
* be `free`d.
|
|
415
|
+
*
|
|
416
|
+
* You can do so from Javascript by calling the .dispose() method.
|
|
417
|
+
*/
|
|
418
|
+
type JSValue = Lifetime<JSValuePointer, JSValuePointer, QuickJSRuntime>;
|
|
419
|
+
/**
|
|
420
|
+
* Wraps a C pointer to a QuickJS JSValue, which represents a Javascript value inside
|
|
421
|
+
* a QuickJS virtual machine.
|
|
422
|
+
*
|
|
423
|
+
* Values must not be shared between QuickJSContext instances.
|
|
424
|
+
* You must dispose of any handles you create by calling the `.dispose()` method.
|
|
425
|
+
*/
|
|
426
|
+
type QuickJSHandle = StaticJSValue | JSValue | JSValueConst;
|
|
427
|
+
type JSModuleLoadSuccess = string;
|
|
428
|
+
type JSModuleLoadFailure = Error | QuickJSHandle;
|
|
429
|
+
type JSModuleLoadResult = JSModuleLoadSuccess | SuccessOrFail<JSModuleLoadSuccess, JSModuleLoadFailure>;
|
|
430
|
+
interface JSModuleLoaderAsync {
|
|
431
|
+
/** Load module (async) */
|
|
432
|
+
(moduleName: string, context: QuickJSAsyncContext): JSModuleLoadResult | Promise<JSModuleLoadResult>;
|
|
433
|
+
}
|
|
434
|
+
interface JSModuleLoader {
|
|
435
|
+
/** Load module (sync) */
|
|
436
|
+
(moduleName: string, context: QuickJSContext): JSModuleLoadResult;
|
|
437
|
+
}
|
|
438
|
+
type JSModuleNormalizeSuccess = string;
|
|
439
|
+
type JSModuleNormalizeFailure = Error | QuickJSHandle;
|
|
440
|
+
type JSModuleNormalizeResult = JSModuleNormalizeSuccess | SuccessOrFail<JSModuleNormalizeSuccess, JSModuleNormalizeFailure>;
|
|
441
|
+
interface JSModuleNormalizerAsync {
|
|
442
|
+
(baseModuleName: string, requestedName: string, vm: QuickJSAsyncContext): JSModuleNormalizeResult | Promise<JSModuleNormalizeResult>;
|
|
443
|
+
}
|
|
444
|
+
interface JSModuleNormalizer extends JSModuleNormalizerAsync {
|
|
445
|
+
(baseModuleName: string, requestedName: string, vm: QuickJSContext): JSModuleNormalizeResult;
|
|
446
|
+
}
|
|
447
|
+
type TODO<hint extends string = "?", typeHint = unknown> = hint & typeHint & never;
|
|
448
|
+
interface RuntimeOptionsBase {
|
|
449
|
+
interruptHandler?: InterruptHandler;
|
|
450
|
+
maxStackSizeBytes?: number;
|
|
451
|
+
memoryLimitBytes?: number;
|
|
452
|
+
promiseRejectionHandler?: TODO<"JSHostPromiseRejectionTracker">;
|
|
453
|
+
runtimeInfo?: TODO<"JS_SetRuntimeInfo", string>;
|
|
454
|
+
gcThreshold?: TODO<"JS_SetGCThreshold", number>;
|
|
455
|
+
sharedArrayBufferFunctions?: TODO<"JS_SetJSSharedArrayBufferFunctions", {
|
|
456
|
+
sab_alloc: TODO;
|
|
457
|
+
sab_free: TODO;
|
|
458
|
+
sab_dup: TODO;
|
|
459
|
+
sab_opaque: TODO;
|
|
460
|
+
}>;
|
|
461
|
+
/**
|
|
462
|
+
* Extra lifetimes the runtime should dispose of after it is destroyed.
|
|
463
|
+
* @private
|
|
464
|
+
*/
|
|
465
|
+
ownedLifetimes?: Disposable$1[];
|
|
466
|
+
}
|
|
467
|
+
interface RuntimeOptions extends RuntimeOptionsBase {
|
|
468
|
+
moduleLoader?: JSModuleLoader;
|
|
469
|
+
}
|
|
470
|
+
interface AsyncRuntimeOptions extends RuntimeOptionsBase {
|
|
471
|
+
moduleLoader?: JSModuleLoaderAsync | JSModuleLoader;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Language features that can be enabled or disabled in a QuickJSContext.
|
|
475
|
+
* @see {@link ContextOptions}
|
|
476
|
+
*/
|
|
477
|
+
type Intrinsics = {
|
|
478
|
+
BaseObjects?: boolean;
|
|
479
|
+
Date?: boolean;
|
|
480
|
+
Eval?: boolean;
|
|
481
|
+
StringNormalize?: boolean;
|
|
482
|
+
RegExp?: boolean;
|
|
483
|
+
RegExpCompiler?: boolean;
|
|
484
|
+
JSON?: boolean;
|
|
485
|
+
Proxy?: boolean;
|
|
486
|
+
MapSet?: boolean;
|
|
487
|
+
TypedArrays?: boolean;
|
|
488
|
+
Promise?: boolean;
|
|
489
|
+
BigInt?: boolean;
|
|
490
|
+
BigFloat?: boolean;
|
|
491
|
+
BigDecimal?: boolean;
|
|
492
|
+
OperatorOverloading?: boolean;
|
|
493
|
+
BignumExt?: boolean;
|
|
494
|
+
};
|
|
495
|
+
/**
|
|
496
|
+
* The default {@link Intrinsics} language features enabled in a QuickJSContext.
|
|
497
|
+
* @see {@link ContextOptions}
|
|
498
|
+
*/
|
|
499
|
+
declare const DefaultIntrinsics: Readonly<{
|
|
500
|
+
readonly BaseObjects: true;
|
|
501
|
+
readonly Date: true;
|
|
502
|
+
readonly Eval: true;
|
|
503
|
+
readonly StringNormalize: true;
|
|
504
|
+
readonly RegExp: true;
|
|
505
|
+
readonly JSON: true;
|
|
506
|
+
readonly Proxy: true;
|
|
507
|
+
readonly MapSet: true;
|
|
508
|
+
readonly TypedArrays: true;
|
|
509
|
+
readonly Promise: true;
|
|
510
|
+
}>;
|
|
511
|
+
/**
|
|
512
|
+
* Options for creating a {@link QuickJSContext} or {@link QuickJSAsyncContext}
|
|
513
|
+
* Pass to {@link QuickJSRuntime#newContext}.
|
|
514
|
+
*/
|
|
515
|
+
interface ContextOptions {
|
|
516
|
+
/**
|
|
517
|
+
* What built-in objects and language features to enable?
|
|
518
|
+
* If unset, the default intrinsics will be used.
|
|
519
|
+
* To omit all intrinsics, pass an empty array.
|
|
520
|
+
*
|
|
521
|
+
* To remove a specific intrinsic, but retain the other defaults,
|
|
522
|
+
* override it from {@link DefaultIntrinsics}
|
|
523
|
+
* ```ts
|
|
524
|
+
* const contextWithoutDateOrEval = runtime.newContext({
|
|
525
|
+
* intrinsics: {
|
|
526
|
+
* ...DefaultIntrinsics,
|
|
527
|
+
* Date: false,
|
|
528
|
+
* }
|
|
529
|
+
* })
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
intrinsics?: Intrinsics;
|
|
533
|
+
/**
|
|
534
|
+
* Wrap the provided context instead of constructing a new one.
|
|
535
|
+
* @private
|
|
536
|
+
*/
|
|
537
|
+
contextPointer?: JSContextPointer;
|
|
538
|
+
/**
|
|
539
|
+
* Extra lifetimes the context should dispose of after it is destroyed.
|
|
540
|
+
* @private
|
|
541
|
+
*/
|
|
542
|
+
ownedLifetimes?: Disposable$1[];
|
|
543
|
+
}
|
|
544
|
+
interface ContextEvalOptions {
|
|
545
|
+
/**
|
|
546
|
+
* Global code (default), or "module" code?
|
|
547
|
+
*
|
|
548
|
+
* - When type is `"global"`, the code is evaluated in the global scope of the QuickJSContext, and the return value is the result of the last expression.
|
|
549
|
+
* - When type is `"module"`, the code is evaluated is a module scope, may use `import`, `export`, and top-level `await`. The return value is the module's exports, or a promise for the module's exports.
|
|
550
|
+
*/
|
|
551
|
+
type?: "global" | "module";
|
|
552
|
+
/** Force "strict" mode */
|
|
553
|
+
strict?: boolean;
|
|
554
|
+
/** Force "strip" mode */
|
|
555
|
+
strip?: boolean;
|
|
556
|
+
/**
|
|
557
|
+
* compile but do not run. The result is an object with a
|
|
558
|
+
* JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE tag. It can be executed
|
|
559
|
+
* with JS_EvalFunction().
|
|
560
|
+
*/
|
|
561
|
+
compileOnly?: boolean;
|
|
562
|
+
/** don't include the stack frames before this eval in the Error() backtraces */
|
|
563
|
+
backtraceBarrier?: boolean;
|
|
564
|
+
}
|
|
565
|
+
interface GetOwnPropertyNamesOptions {
|
|
566
|
+
/** Include number properties like array indexes *as numbers* in the result. This is not standards-compliant */
|
|
567
|
+
numbers?: boolean;
|
|
568
|
+
/** Enable standards-compliant number properties. When set, `includeNumbers` is ignored. */
|
|
569
|
+
numbersAsStrings?: boolean;
|
|
570
|
+
/** Include strings in the result */
|
|
571
|
+
strings?: boolean;
|
|
572
|
+
/** Include symbols in the result */
|
|
573
|
+
symbols?: boolean;
|
|
574
|
+
/** Include implementation-specific private properties in the result */
|
|
575
|
+
quickjsPrivate?: boolean;
|
|
576
|
+
/** Only include the enumerable properties */
|
|
577
|
+
onlyEnumerable?: boolean;
|
|
578
|
+
}
|
|
579
|
+
type PromiseExecutor<ResolveT, RejectT> = (resolve: (value: ResolveT | PromiseLike<ResolveT>) => void, reject: (reason: RejectT) => void) => void;
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* An object that can be disposed.
|
|
583
|
+
* {@link Lifetime} is the canonical implementation of Disposable.
|
|
584
|
+
* Use {@link Scope} to manage cleaning up multiple disposables.
|
|
585
|
+
*/
|
|
586
|
+
interface Disposable$1 {
|
|
587
|
+
/**
|
|
588
|
+
* Dispose of the underlying resources used by this object.
|
|
589
|
+
*/
|
|
590
|
+
dispose(): void;
|
|
591
|
+
/**
|
|
592
|
+
* @returns true if the object is alive
|
|
593
|
+
* @returns false after the object has been {@link dispose}d
|
|
594
|
+
*/
|
|
595
|
+
alive: boolean;
|
|
596
|
+
/**
|
|
597
|
+
* A method that is used to release resources held by an object. Called by the semantics of the `using` statement.
|
|
598
|
+
*/
|
|
599
|
+
[Symbol.dispose](): void;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Base abstract class that helps implement {@link Disposable} by providing a default implementation of {@link Symbol.dispose}.
|
|
603
|
+
*/
|
|
604
|
+
declare abstract class UsingDisposable implements Disposable$1 {
|
|
605
|
+
/**
|
|
606
|
+
* @returns true if the object is alive
|
|
607
|
+
* @returns false after the object has been {@link dispose}d
|
|
608
|
+
*/
|
|
609
|
+
abstract readonly alive: boolean;
|
|
610
|
+
/**
|
|
611
|
+
* Dispose of the underlying resources used by this object.
|
|
612
|
+
*/
|
|
613
|
+
abstract dispose(): void;
|
|
614
|
+
/**
|
|
615
|
+
* Just calls the standard .dispose() method of this class.
|
|
616
|
+
*/
|
|
617
|
+
[Symbol.dispose](): void;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* A lifetime prevents access to a value after the lifetime has been
|
|
621
|
+
* {@link dispose}ed.
|
|
622
|
+
*
|
|
623
|
+
* Typically, quickjs-emscripten uses Lifetimes to protect C memory pointers.
|
|
624
|
+
*/
|
|
625
|
+
declare class Lifetime<T, TCopy = never, Owner = never> extends UsingDisposable implements Disposable$1 {
|
|
626
|
+
protected readonly _value: T;
|
|
627
|
+
protected readonly copier?: ((value: T | TCopy) => TCopy) | undefined;
|
|
628
|
+
protected readonly disposer?: ((value: T | TCopy) => void) | undefined;
|
|
629
|
+
protected readonly _owner?: Owner | undefined;
|
|
630
|
+
protected _alive: boolean;
|
|
631
|
+
protected _constructorStack: string | undefined;
|
|
632
|
+
/**
|
|
633
|
+
* When the Lifetime is disposed, it will call `disposer(_value)`. Use the
|
|
634
|
+
* disposer function to implement whatever cleanup needs to happen at the end
|
|
635
|
+
* of `value`'s lifetime.
|
|
636
|
+
*
|
|
637
|
+
* `_owner` is not used or controlled by the lifetime. It's just metadata for
|
|
638
|
+
* the creator.
|
|
639
|
+
*/
|
|
640
|
+
constructor(_value: T, copier?: ((value: T | TCopy) => TCopy) | undefined, disposer?: ((value: T | TCopy) => void) | undefined, _owner?: Owner | undefined);
|
|
641
|
+
get alive(): boolean;
|
|
642
|
+
/**
|
|
643
|
+
* The value this Lifetime protects. You must never retain the value - it
|
|
644
|
+
* may become invalid, leading to memory errors.
|
|
645
|
+
*
|
|
646
|
+
* @throws If the lifetime has been {@link dispose}d already.
|
|
647
|
+
*/
|
|
648
|
+
get value(): T;
|
|
649
|
+
get owner(): Owner | undefined;
|
|
650
|
+
get dupable(): boolean;
|
|
651
|
+
/**
|
|
652
|
+
* Create a new handle pointing to the same {@link value}.
|
|
653
|
+
*/
|
|
654
|
+
dup(): Lifetime<TCopy, TCopy, Owner>;
|
|
655
|
+
/**
|
|
656
|
+
* Call `map` with this lifetime, then dispose the lifetime.
|
|
657
|
+
* @return the result of `map(this)`.
|
|
658
|
+
*/
|
|
659
|
+
consume<O>(map: (lifetime: this) => O): O;
|
|
660
|
+
consume<O>(map: (lifetime: QuickJSHandle) => O): O;
|
|
661
|
+
/**
|
|
662
|
+
* Call `map` with this lifetime, returning the result.
|
|
663
|
+
* Does not dispose the lifetime.
|
|
664
|
+
* @return the result of `map(this)`.
|
|
665
|
+
*/
|
|
666
|
+
map<O>(map: (lifetime: this) => O): O;
|
|
667
|
+
map<O>(map: (lifetime: QuickJSHandle) => O): O;
|
|
668
|
+
/**
|
|
669
|
+
* Call `fn` with this lifetime, then return `this`. Does not dispose the
|
|
670
|
+
* lifetime. Useful for imperative operations within an expression, like when
|
|
671
|
+
* you're building up objects, or to add logging in the middle of a call chain.
|
|
672
|
+
* @returns this
|
|
673
|
+
*/
|
|
674
|
+
tap(fn: (lifetime: this) => void): this;
|
|
675
|
+
tap(fn: (lifetime: QuickJSHandle) => void): QuickJSHandle;
|
|
676
|
+
/**
|
|
677
|
+
* Dispose of {@link value} and perform cleanup.
|
|
678
|
+
*/
|
|
679
|
+
dispose(): void;
|
|
680
|
+
private assertAlive;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* A Lifetime that lives forever. Used for constants.
|
|
684
|
+
*/
|
|
685
|
+
declare class StaticLifetime<T, Owner = never> extends Lifetime<T, T, Owner> {
|
|
686
|
+
constructor(value: T, owner?: Owner);
|
|
687
|
+
get dupable(): boolean;
|
|
688
|
+
dup(): this;
|
|
689
|
+
dispose(): void;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* A Lifetime that does not own its `value`. A WeakLifetime never calls its
|
|
693
|
+
* `disposer` function, but can be `dup`ed to produce regular lifetimes that
|
|
694
|
+
* do.
|
|
695
|
+
*
|
|
696
|
+
* Used for function arguments.
|
|
697
|
+
*/
|
|
698
|
+
declare class WeakLifetime<T, TCopy = never, Owner = never> extends Lifetime<T, TCopy, Owner> {
|
|
699
|
+
constructor(value: T, copier?: (value: T | TCopy) => TCopy, disposer?: (value: TCopy) => void, owner?: Owner);
|
|
700
|
+
dispose(): void;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Scope helps reduce the burden of manually tracking and disposing of
|
|
704
|
+
* Lifetimes. See {@link withScope}. and {@link withScopeAsync}.
|
|
705
|
+
*/
|
|
706
|
+
declare class Scope extends UsingDisposable implements Disposable$1 {
|
|
707
|
+
/**
|
|
708
|
+
* Run `block` with a new Scope instance that will be disposed after the block returns.
|
|
709
|
+
* Inside `block`, call `scope.manage` on each lifetime you create to have the lifetime
|
|
710
|
+
* automatically disposed after the block returns.
|
|
711
|
+
*
|
|
712
|
+
* @warning Do not use with async functions. Instead, use {@link withScopeAsync}.
|
|
713
|
+
*/
|
|
714
|
+
static withScope<R>(block: (scope: Scope) => R): R;
|
|
715
|
+
static withScopeMaybeAsync<Return, This, Yielded>(_this: This, block: MaybeAsyncBlock<Return, This, Yielded, [Scope]>): Return | Promise<Return>;
|
|
716
|
+
/**
|
|
717
|
+
* Run `block` with a new Scope instance that will be disposed after the
|
|
718
|
+
* block's returned promise settles. Inside `block`, call `scope.manage` on each
|
|
719
|
+
* lifetime you create to have the lifetime automatically disposed after the
|
|
720
|
+
* block returns.
|
|
721
|
+
*/
|
|
722
|
+
static withScopeAsync<R>(block: (scope: Scope) => Promise<R>): Promise<R>;
|
|
723
|
+
private _disposables;
|
|
724
|
+
/**
|
|
725
|
+
* Track `lifetime` so that it is disposed when this scope is disposed.
|
|
726
|
+
*/
|
|
727
|
+
manage: <T extends Disposable$1>(lifetime: T) => T;
|
|
728
|
+
get alive(): boolean;
|
|
729
|
+
dispose(): void;
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* An `Array` that also implements {@link Disposable}:
|
|
733
|
+
*
|
|
734
|
+
* - Considered {@link Disposable#alive} if any of its elements are `alive`.
|
|
735
|
+
* - When {@link Disposable#dispose}d, it will dispose of all its elements that are `alive`.
|
|
736
|
+
*/
|
|
737
|
+
type DisposableArray<T> = T[] & Disposable$1;
|
|
738
|
+
/**
|
|
739
|
+
* Create an array that also implements {@link Disposable}.
|
|
740
|
+
*/
|
|
741
|
+
declare function createDisposableArray<T extends Disposable$1>(items?: Iterable<T>): DisposableArray<T>;
|
|
742
|
+
declare abstract class AbstractDisposableResult extends UsingDisposable implements Disposable$1 {
|
|
743
|
+
static success<S, F>(value: S): DisposableSuccess<S>;
|
|
744
|
+
static fail<S, F>(error: F, onUnwrap: (status: SuccessOrFail<S, F>) => void): DisposableFail<F>;
|
|
745
|
+
static is<S, F>(result: SuccessOrFail<S, F>): result is DisposableResult<S, F>;
|
|
746
|
+
abstract get alive(): boolean;
|
|
747
|
+
abstract dispose(): void;
|
|
748
|
+
}
|
|
749
|
+
declare class DisposableSuccess<S> extends AbstractDisposableResult {
|
|
750
|
+
readonly value: S;
|
|
751
|
+
error?: undefined;
|
|
752
|
+
constructor(value: S);
|
|
753
|
+
get alive(): boolean;
|
|
754
|
+
dispose(): void;
|
|
755
|
+
unwrap(): S;
|
|
756
|
+
unwrapOr<T>(_fallback: T): S | T;
|
|
757
|
+
}
|
|
758
|
+
declare class DisposableFail<F> extends AbstractDisposableResult {
|
|
759
|
+
readonly error: F;
|
|
760
|
+
private readonly onUnwrap;
|
|
761
|
+
constructor(error: F, onUnwrap: (status: SuccessOrFail<never, F>) => void);
|
|
762
|
+
get alive(): boolean;
|
|
763
|
+
dispose(): void;
|
|
764
|
+
unwrap(): never;
|
|
765
|
+
unwrapOr<T>(fallback: T): T;
|
|
766
|
+
}
|
|
767
|
+
type DisposableResult<S, F> = DisposableSuccess<S> | DisposableFail<F>;
|
|
768
|
+
declare const DisposableResult: typeof AbstractDisposableResult;
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* A promise state inside QuickJS, which can be pending, fulfilled, or rejected.
|
|
772
|
+
* You can unwrap a JSPromiseState with {@link QuickJSContext#unwrapResult}.
|
|
773
|
+
*/
|
|
774
|
+
type JSPromiseState = JSPromiseStatePending | JSPromiseStateFulfilled | JSPromiseStateRejected;
|
|
775
|
+
/**
|
|
776
|
+
* Pending promise state.
|
|
777
|
+
* See {@link JSPromiseState}.
|
|
778
|
+
*/
|
|
779
|
+
interface JSPromiseStatePending {
|
|
780
|
+
type: "pending";
|
|
781
|
+
/**
|
|
782
|
+
* The error property here allows unwrapping a JSPromiseState with {@link QuickJSContext#unwrapResult}.
|
|
783
|
+
* Unwrapping a pending promise will throw a {@link QuickJSPromisePending} error.
|
|
784
|
+
*/
|
|
785
|
+
get error(): Error;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Fulfilled promise state.
|
|
789
|
+
* See {@link JSPromiseState}.
|
|
790
|
+
*/
|
|
791
|
+
interface JSPromiseStateFulfilled {
|
|
792
|
+
type: "fulfilled";
|
|
793
|
+
value: QuickJSHandle;
|
|
794
|
+
error?: undefined;
|
|
795
|
+
/** Trying to get the promise state of a non-Promise value returns a fulfilled state with the original value, and `notAPromise: true`. */
|
|
796
|
+
notAPromise?: boolean;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Rejected promise state.
|
|
800
|
+
* See {@link JSPromiseState}.
|
|
801
|
+
*/
|
|
802
|
+
interface JSPromiseStateRejected {
|
|
803
|
+
type: "rejected";
|
|
804
|
+
error: QuickJSHandle;
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* QuickJSDeferredPromise wraps a QuickJS promise {@link handle} and allows
|
|
808
|
+
* {@link resolve}ing or {@link reject}ing that promise. Use it to bridge asynchronous
|
|
809
|
+
* code on the host to APIs inside a QuickJSContext.
|
|
810
|
+
*
|
|
811
|
+
* Managing the lifetime of promises is tricky. There are three
|
|
812
|
+
* {@link QuickJSHandle}s inside of each deferred promise object: (1) the promise
|
|
813
|
+
* itself, (2) the `resolve` callback, and (3) the `reject` callback.
|
|
814
|
+
*
|
|
815
|
+
* - If the promise will be fulfilled before the end of it's {@link owner}'s lifetime,
|
|
816
|
+
* the only cleanup necessary is `deferred.handle.dispose()`, because
|
|
817
|
+
* calling {@link resolve} or {@link reject} will dispose of both callbacks automatically.
|
|
818
|
+
*
|
|
819
|
+
* - As the return value of a {@link VmFunctionImplementation}, return {@link handle},
|
|
820
|
+
* and ensure that either {@link resolve} or {@link reject} will be called. No other
|
|
821
|
+
* clean-up is necessary.
|
|
822
|
+
*
|
|
823
|
+
* - In other cases, call {@link dispose}, which will dispose {@link handle} as well as the
|
|
824
|
+
* QuickJS handles that back {@link resolve} and {@link reject}. For this object,
|
|
825
|
+
* {@link dispose} is idempotent.
|
|
826
|
+
*/
|
|
827
|
+
declare class QuickJSDeferredPromise extends UsingDisposable implements Disposable$1 {
|
|
828
|
+
owner: QuickJSRuntime;
|
|
829
|
+
context: QuickJSContext;
|
|
830
|
+
/**
|
|
831
|
+
* A handle of the Promise instance inside the QuickJSContext.
|
|
832
|
+
* You must dispose {@link handle} or the entire QuickJSDeferredPromise once you
|
|
833
|
+
* are finished with it.
|
|
834
|
+
*/
|
|
835
|
+
handle: QuickJSHandle;
|
|
836
|
+
/**
|
|
837
|
+
* A native promise that will resolve once this deferred is settled.
|
|
838
|
+
*/
|
|
839
|
+
settled: Promise<void>;
|
|
840
|
+
private resolveHandle;
|
|
841
|
+
private rejectHandle;
|
|
842
|
+
private onSettled;
|
|
843
|
+
/**
|
|
844
|
+
* Use {@link QuickJSContext#newPromise} to create a new promise instead of calling
|
|
845
|
+
* this constructor directly.
|
|
846
|
+
*/
|
|
847
|
+
constructor(args: {
|
|
848
|
+
context: QuickJSContext;
|
|
849
|
+
promiseHandle: QuickJSHandle;
|
|
850
|
+
resolveHandle: QuickJSHandle;
|
|
851
|
+
rejectHandle: QuickJSHandle;
|
|
852
|
+
});
|
|
853
|
+
/**
|
|
854
|
+
* Resolve {@link handle} with the given value, if any.
|
|
855
|
+
* Calling this method after calling {@link dispose} is a no-op.
|
|
856
|
+
*
|
|
857
|
+
* Note that after resolving a promise, you may need to call
|
|
858
|
+
* {@link QuickJSRuntime#executePendingJobs} to propagate the result to the promise's
|
|
859
|
+
* callbacks.
|
|
860
|
+
*/
|
|
861
|
+
resolve: (value?: QuickJSHandle) => void;
|
|
862
|
+
/**
|
|
863
|
+
* Reject {@link handle} with the given value, if any.
|
|
864
|
+
* Calling this method after calling {@link dispose} is a no-op.
|
|
865
|
+
*
|
|
866
|
+
* Note that after rejecting a promise, you may need to call
|
|
867
|
+
* {@link QuickJSRuntime#executePendingJobs} to propagate the result to the promise's
|
|
868
|
+
* callbacks.
|
|
869
|
+
*/
|
|
870
|
+
reject: (value?: QuickJSHandle) => void;
|
|
871
|
+
get alive(): boolean;
|
|
872
|
+
dispose: () => void;
|
|
873
|
+
private disposeResolvers;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* Proxies the iteration protocol from the host to a guest iterator.
|
|
878
|
+
* The guest iterator is a QuickJS object with a `next` method.
|
|
879
|
+
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols).
|
|
880
|
+
*
|
|
881
|
+
* If calling the `next` method or any other method of the iteration protocol throws an error,
|
|
882
|
+
* the iterator is disposed after returning the exception as the final value.
|
|
883
|
+
*
|
|
884
|
+
* When the iterator is done, the handle is disposed automatically.
|
|
885
|
+
* The caller is responsible for disposing each successive value.
|
|
886
|
+
*
|
|
887
|
+
* ```typescript
|
|
888
|
+
* for (const nextResult of context.unwrapResult(context.getIterator(arrayHandle)) {
|
|
889
|
+
* const nextHandle = context.unwrapResult(nextResult)
|
|
890
|
+
* try {
|
|
891
|
+
* // Do something with nextHandle
|
|
892
|
+
* console.log(context.dump(nextHandle))
|
|
893
|
+
* } finally {
|
|
894
|
+
* nextHandle.dispose()
|
|
895
|
+
* }
|
|
896
|
+
* }
|
|
897
|
+
* ```
|
|
898
|
+
*/
|
|
899
|
+
declare class QuickJSIterator extends UsingDisposable implements Disposable, IterableIterator<QuickJSContextResult<QuickJSHandle>> {
|
|
900
|
+
handle: QuickJSHandle;
|
|
901
|
+
context: QuickJSContext;
|
|
902
|
+
owner: QuickJSRuntime;
|
|
903
|
+
private _next;
|
|
904
|
+
private _isDone;
|
|
905
|
+
constructor(handle: QuickJSHandle, context: QuickJSContext);
|
|
906
|
+
[Symbol.iterator](): this;
|
|
907
|
+
next(value?: QuickJSHandle): IteratorResult<QuickJSContextResult<QuickJSHandle>, any>;
|
|
908
|
+
return(value?: QuickJSHandle): IteratorResult<QuickJSContextResult<QuickJSHandle>, any>;
|
|
909
|
+
throw(e?: any): IteratorResult<QuickJSContextResult<QuickJSHandle>, any>;
|
|
910
|
+
get alive(): boolean;
|
|
911
|
+
dispose(): void;
|
|
912
|
+
private callIteratorMethod;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
type QuickJSContextResult<S> = DisposableResult<S, QuickJSHandle>;
|
|
916
|
+
/**
|
|
917
|
+
* Property key for getting or setting a property on a handle with
|
|
918
|
+
* {@link QuickJSContext#getProp}, {@link QuickJSContext#setProp}, or {@link QuickJSContext#defineProp}.
|
|
919
|
+
*/
|
|
920
|
+
type QuickJSPropertyKey = number | string | QuickJSHandle;
|
|
921
|
+
/**
|
|
922
|
+
* @private
|
|
923
|
+
*/
|
|
924
|
+
declare class ContextMemory extends ModuleMemory implements Disposable$1 {
|
|
925
|
+
readonly owner: QuickJSRuntime;
|
|
926
|
+
readonly ctx: Lifetime<JSContextPointer>;
|
|
927
|
+
readonly rt: Lifetime<JSRuntimePointer>;
|
|
928
|
+
readonly module: EitherModule;
|
|
929
|
+
readonly ffi: EitherFFI;
|
|
930
|
+
readonly scope: Scope;
|
|
931
|
+
/** @private */
|
|
932
|
+
constructor(args: {
|
|
933
|
+
owner: QuickJSRuntime;
|
|
934
|
+
module: EitherModule;
|
|
935
|
+
ffi: EitherFFI;
|
|
936
|
+
ctx: Lifetime<JSContextPointer>;
|
|
937
|
+
rt: Lifetime<JSRuntimePointer>;
|
|
938
|
+
ownedLifetimes?: Disposable$1[];
|
|
939
|
+
});
|
|
940
|
+
get alive(): boolean;
|
|
941
|
+
dispose(): void;
|
|
942
|
+
[Symbol.dispose](): void;
|
|
943
|
+
/**
|
|
944
|
+
* Track `lifetime` so that it is disposed when this scope is disposed.
|
|
945
|
+
*/
|
|
946
|
+
manage<T extends Disposable$1>(lifetime: T): T;
|
|
947
|
+
copyJSValue: (ptr: JSValuePointer | JSValueConstPointer) => JSValuePointer;
|
|
948
|
+
freeJSValue: (ptr: JSValuePointer) => void;
|
|
949
|
+
consumeJSCharPointer(ptr: JSBorrowedCharPointer): string;
|
|
950
|
+
heapValueHandle(ptr: JSValuePointer): JSValue;
|
|
951
|
+
/** Manage a heap pointer with the lifetime of the context */
|
|
952
|
+
staticHeapValueHandle(ptr: JSValuePointer | JSValueConstPointer): StaticJSValue;
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* QuickJSContext wraps a QuickJS Javascript context (JSContext*) within a
|
|
956
|
+
* runtime. The contexts within the same runtime may exchange objects freely.
|
|
957
|
+
* You can think of separate runtimes like different domains in a browser, and
|
|
958
|
+
* the contexts within a runtime like the different windows open to the same
|
|
959
|
+
* domain. The {@link runtime} references the context's runtime.
|
|
960
|
+
*
|
|
961
|
+
* This class's methods return {@link QuickJSHandle}, which wrap C pointers (JSValue*).
|
|
962
|
+
* It's the caller's responsibility to call `.dispose()` on any
|
|
963
|
+
* handles you create to free memory once you're done with the handle.
|
|
964
|
+
*
|
|
965
|
+
* Use {@link QuickJSRuntime#newContext} or {@link QuickJSWASMModule#newContext}
|
|
966
|
+
* to create a new QuickJSContext.
|
|
967
|
+
*
|
|
968
|
+
* Create QuickJS values inside the interpreter with methods like
|
|
969
|
+
* {@link newNumber}, {@link newString}, {@link newArray}, {@link newObject},
|
|
970
|
+
* {@link newFunction}, and {@link newPromise}.
|
|
971
|
+
*
|
|
972
|
+
* Call {@link setProp} or {@link defineProp} to customize objects. Use those methods
|
|
973
|
+
* with {@link global} to expose the values you create to the interior of the
|
|
974
|
+
* interpreter, so they can be used in {@link evalCode}.
|
|
975
|
+
*
|
|
976
|
+
* Use {@link evalCode} or {@link callFunction} to execute Javascript inside the VM. If
|
|
977
|
+
* you're using asynchronous code inside the QuickJSContext, you may need to also
|
|
978
|
+
* call {@link QuickJSRuntime#executePendingJobs}. Executing code inside the runtime returns a
|
|
979
|
+
* result object representing successful execution or an error. You must dispose
|
|
980
|
+
* of any such results to avoid leaking memory inside the VM.
|
|
981
|
+
*
|
|
982
|
+
* Implement memory and CPU constraints at the runtime level, using {@link runtime}.
|
|
983
|
+
* See {@link QuickJSRuntime} for more information.
|
|
984
|
+
*
|
|
985
|
+
*/
|
|
986
|
+
declare class QuickJSContext extends UsingDisposable implements LowLevelJavascriptVm<QuickJSHandle>, Disposable$1 {
|
|
987
|
+
/**
|
|
988
|
+
* The runtime that created this context.
|
|
989
|
+
*/
|
|
990
|
+
readonly runtime: QuickJSRuntime;
|
|
991
|
+
/** @private */
|
|
992
|
+
protected readonly ctx: Lifetime<JSContextPointer>;
|
|
993
|
+
/** @private */
|
|
994
|
+
protected readonly rt: Lifetime<JSRuntimePointer>;
|
|
995
|
+
/** @private */
|
|
996
|
+
protected readonly module: EitherModule;
|
|
997
|
+
/** @private */
|
|
998
|
+
protected readonly ffi: EitherFFI;
|
|
999
|
+
/** @private */
|
|
1000
|
+
protected memory: ContextMemory;
|
|
1001
|
+
/** @private */
|
|
1002
|
+
protected _undefined: QuickJSHandle | undefined;
|
|
1003
|
+
/** @private */
|
|
1004
|
+
protected _null: QuickJSHandle | undefined;
|
|
1005
|
+
/** @private */
|
|
1006
|
+
protected _false: QuickJSHandle | undefined;
|
|
1007
|
+
/** @private */
|
|
1008
|
+
protected _true: QuickJSHandle | undefined;
|
|
1009
|
+
/** @private */
|
|
1010
|
+
protected _global: QuickJSHandle | undefined;
|
|
1011
|
+
/** @private */
|
|
1012
|
+
protected _BigInt: QuickJSHandle | undefined;
|
|
1013
|
+
/** @private */
|
|
1014
|
+
protected uint32Out: HeapTypedArray<Uint32Array<ArrayBuffer>, UInt32Pointer>;
|
|
1015
|
+
/** @private */
|
|
1016
|
+
protected _Symbol: QuickJSHandle | undefined;
|
|
1017
|
+
/** @private */
|
|
1018
|
+
protected _SymbolIterator: QuickJSHandle | undefined;
|
|
1019
|
+
/** @private */
|
|
1020
|
+
protected _SymbolAsyncIterator: QuickJSHandle | undefined;
|
|
1021
|
+
/**
|
|
1022
|
+
* Use {@link QuickJSRuntime#newContext} or {@link QuickJSWASMModule#newContext}
|
|
1023
|
+
* to create a new QuickJSContext.
|
|
1024
|
+
*/
|
|
1025
|
+
constructor(args: {
|
|
1026
|
+
module: EitherModule;
|
|
1027
|
+
ffi: EitherFFI;
|
|
1028
|
+
ctx: Lifetime<JSContextPointer>;
|
|
1029
|
+
rt: Lifetime<JSRuntimePointer>;
|
|
1030
|
+
runtime: QuickJSRuntime;
|
|
1031
|
+
ownedLifetimes?: Disposable$1[];
|
|
1032
|
+
callbacks: QuickJSModuleCallbacks;
|
|
1033
|
+
});
|
|
1034
|
+
get alive(): boolean;
|
|
1035
|
+
/**
|
|
1036
|
+
* Dispose of this VM's underlying resources.
|
|
1037
|
+
*
|
|
1038
|
+
* @throws Calling this method without disposing of all created handles
|
|
1039
|
+
* will result in an error.
|
|
1040
|
+
*/
|
|
1041
|
+
dispose(): void;
|
|
1042
|
+
/**
|
|
1043
|
+
* [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined).
|
|
1044
|
+
*/
|
|
1045
|
+
get undefined(): QuickJSHandle;
|
|
1046
|
+
/**
|
|
1047
|
+
* [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null).
|
|
1048
|
+
*/
|
|
1049
|
+
get null(): QuickJSHandle;
|
|
1050
|
+
/**
|
|
1051
|
+
* [`true`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/true).
|
|
1052
|
+
*/
|
|
1053
|
+
get true(): QuickJSHandle;
|
|
1054
|
+
/**
|
|
1055
|
+
* [`false`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/false).
|
|
1056
|
+
*/
|
|
1057
|
+
get false(): QuickJSHandle;
|
|
1058
|
+
/**
|
|
1059
|
+
* [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects).
|
|
1060
|
+
* A handle to the global object inside the interpreter.
|
|
1061
|
+
* You can set properties to create global variables.
|
|
1062
|
+
*/
|
|
1063
|
+
get global(): QuickJSHandle;
|
|
1064
|
+
/**
|
|
1065
|
+
* Converts a Javascript number into a QuickJS value.
|
|
1066
|
+
*/
|
|
1067
|
+
newNumber(num: number): QuickJSHandle;
|
|
1068
|
+
/**
|
|
1069
|
+
* Create a QuickJS [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) value.
|
|
1070
|
+
*/
|
|
1071
|
+
newString(str: string): QuickJSHandle;
|
|
1072
|
+
/**
|
|
1073
|
+
* Create a QuickJS [symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) value.
|
|
1074
|
+
* No two symbols created with this function will be the same value.
|
|
1075
|
+
*/
|
|
1076
|
+
newUniqueSymbol(description: string | symbol): QuickJSHandle;
|
|
1077
|
+
/**
|
|
1078
|
+
* Get a symbol from the [global registry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#shared_symbols_in_the_global_symbol_registry) for the given key.
|
|
1079
|
+
* All symbols created with the same key will be the same value.
|
|
1080
|
+
*/
|
|
1081
|
+
newSymbolFor(key: string | symbol): QuickJSHandle;
|
|
1082
|
+
/**
|
|
1083
|
+
* Access a well-known symbol that is a property of the global Symbol object, like `Symbol.iterator`.
|
|
1084
|
+
*/
|
|
1085
|
+
getWellKnownSymbol(name: string): QuickJSHandle;
|
|
1086
|
+
/**
|
|
1087
|
+
* Create a QuickJS [bigint](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) value.
|
|
1088
|
+
*/
|
|
1089
|
+
newBigInt(num: bigint): QuickJSHandle;
|
|
1090
|
+
/**
|
|
1091
|
+
* `{}`.
|
|
1092
|
+
* Create a new QuickJS [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer).
|
|
1093
|
+
*
|
|
1094
|
+
* @param prototype - Like [`Object.create`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create).
|
|
1095
|
+
*/
|
|
1096
|
+
newObject(prototype?: QuickJSHandle): QuickJSHandle;
|
|
1097
|
+
/**
|
|
1098
|
+
* `[]`.
|
|
1099
|
+
* Create a new QuickJS [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
|
|
1100
|
+
*/
|
|
1101
|
+
newArray(): QuickJSHandle;
|
|
1102
|
+
/**
|
|
1103
|
+
* Create a new QuickJS [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer).
|
|
1104
|
+
*/
|
|
1105
|
+
newArrayBuffer(buffer: ArrayBufferLike): QuickJSHandle;
|
|
1106
|
+
/**
|
|
1107
|
+
* Create a new {@link QuickJSDeferredPromise}. Use `deferred.resolve(handle)` and
|
|
1108
|
+
* `deferred.reject(handle)` to fulfill the promise handle available at `deferred.handle`.
|
|
1109
|
+
* Note that you are responsible for calling `deferred.dispose()` to free the underlying
|
|
1110
|
+
* resources; see the documentation on {@link QuickJSDeferredPromise} for details.
|
|
1111
|
+
*/
|
|
1112
|
+
newPromise(): QuickJSDeferredPromise;
|
|
1113
|
+
/**
|
|
1114
|
+
* Create a new {@link QuickJSDeferredPromise} that resolves when the
|
|
1115
|
+
* given native Promise<QuickJSHandle> resolves. Rejections will be coerced
|
|
1116
|
+
* to a QuickJS error.
|
|
1117
|
+
*
|
|
1118
|
+
* You can still resolve/reject the created promise "early" using its methods.
|
|
1119
|
+
*/
|
|
1120
|
+
newPromise(promise: Promise<QuickJSHandle>): QuickJSDeferredPromise;
|
|
1121
|
+
/**
|
|
1122
|
+
* Construct a new native Promise<QuickJSHandle>, and then convert it into a
|
|
1123
|
+
* {@link QuickJSDeferredPromise}.
|
|
1124
|
+
*
|
|
1125
|
+
* You can still resolve/reject the created promise "early" using its methods.
|
|
1126
|
+
*/
|
|
1127
|
+
newPromise(newPromiseFn: PromiseExecutor<QuickJSHandle, Error | QuickJSHandle>): QuickJSDeferredPromise;
|
|
1128
|
+
/**
|
|
1129
|
+
* Convert a Javascript function into a QuickJS function value.
|
|
1130
|
+
* See {@link VmFunctionImplementation} for more details.
|
|
1131
|
+
*
|
|
1132
|
+
* A {@link VmFunctionImplementation} should not free its arguments or its return
|
|
1133
|
+
* value. A VmFunctionImplementation should also not retain any references to
|
|
1134
|
+
* its return value.
|
|
1135
|
+
*
|
|
1136
|
+
* The function argument handles are automatically disposed when the function
|
|
1137
|
+
* returns. If you want to retain a handle beyond the end of the function, you
|
|
1138
|
+
* can call {@link Lifetime#dup} to create a copy of the handle that you own
|
|
1139
|
+
* and must dispose manually. For example, you need to use this API and do some
|
|
1140
|
+
* extra book keeping to implement `setInterval`:
|
|
1141
|
+
*
|
|
1142
|
+
* ```typescript
|
|
1143
|
+
* // This won't work because `callbackHandle` expires when the function returns,
|
|
1144
|
+
* // so when the interval fires, the callback handle is already disposed.
|
|
1145
|
+
* const WRONG_setIntervalHandle = context.newFunction("setInterval", (callbackHandle, delayHandle) => {
|
|
1146
|
+
* const delayMs = context.getNumber(delayHandle)
|
|
1147
|
+
* const intervalId = globalThis.setInterval(() => {
|
|
1148
|
+
* // ERROR: callbackHandle is already disposed here.
|
|
1149
|
+
* context.callFunction(callbackHandle)
|
|
1150
|
+
* }, intervalId)
|
|
1151
|
+
* return context.newNumber(intervalId)
|
|
1152
|
+
* })
|
|
1153
|
+
*
|
|
1154
|
+
* // This works since we dup the callbackHandle.
|
|
1155
|
+
* // We just need to make sure we clean it up manually when the interval is cleared --
|
|
1156
|
+
* // so we need to keep track of those interval IDs, and make sure we clean all
|
|
1157
|
+
* // of them up when we dispose the owning context.
|
|
1158
|
+
*
|
|
1159
|
+
* const setIntervalHandle = context.newFunction("setInterval", (callbackHandle, delayHandle) => {
|
|
1160
|
+
* // Ensure the guest can't overload us by scheduling too many intervals.
|
|
1161
|
+
* if (QuickJSInterval.INTERVALS.size > 100) {
|
|
1162
|
+
* throw new Error(`Too many intervals scheduled already`)
|
|
1163
|
+
* }
|
|
1164
|
+
*
|
|
1165
|
+
* const delayMs = context.getNumber(delayHandle)
|
|
1166
|
+
* const longLivedCallbackHandle = callbackHandle.dup()
|
|
1167
|
+
* const intervalId = globalThis.setInterval(() => {
|
|
1168
|
+
* context.callFunction(longLivedCallbackHandle)
|
|
1169
|
+
* }, intervalId)
|
|
1170
|
+
* const disposable = new QuickJSInterval(longLivedCallbackHandle, context, intervalId)
|
|
1171
|
+
* QuickJSInterval.INTERVALS.set(intervalId, disposable)
|
|
1172
|
+
* return context.newNumber(intervalId)
|
|
1173
|
+
* })
|
|
1174
|
+
*
|
|
1175
|
+
* const clearIntervalHandle = context.newFunction("clearInterval", (intervalIdHandle) => {
|
|
1176
|
+
* const intervalId = context.getNumber(intervalIdHandle)
|
|
1177
|
+
* const disposable = QuickJSInterval.INTERVALS.get(intervalId)
|
|
1178
|
+
* disposable?.dispose()
|
|
1179
|
+
* })
|
|
1180
|
+
*
|
|
1181
|
+
* class QuickJSInterval extends UsingDisposable {
|
|
1182
|
+
* static INTERVALS = new Map<number, QuickJSInterval>()
|
|
1183
|
+
*
|
|
1184
|
+
* static disposeContext(context: QuickJSContext) {
|
|
1185
|
+
* for (const interval of QuickJSInterval.INTERVALS.values()) {
|
|
1186
|
+
* if (interval.context === context) {
|
|
1187
|
+
* interval.dispose()
|
|
1188
|
+
* }
|
|
1189
|
+
* }
|
|
1190
|
+
* }
|
|
1191
|
+
*
|
|
1192
|
+
* constructor(
|
|
1193
|
+
* public fnHandle: QuickJSHandle,
|
|
1194
|
+
* public context: QuickJSContext,
|
|
1195
|
+
* public intervalId: number,
|
|
1196
|
+
* ) {
|
|
1197
|
+
* super()
|
|
1198
|
+
* }
|
|
1199
|
+
*
|
|
1200
|
+
* dispose() {
|
|
1201
|
+
* globalThis.clearInterval(this.intervalId)
|
|
1202
|
+
* this.fnHandle.dispose()
|
|
1203
|
+
* QuickJSInterval.INTERVALS.delete(this.fnHandle.value)
|
|
1204
|
+
* }
|
|
1205
|
+
*
|
|
1206
|
+
* get alive() {
|
|
1207
|
+
* return this.fnHandle.alive
|
|
1208
|
+
* }
|
|
1209
|
+
* }
|
|
1210
|
+
* ```
|
|
1211
|
+
*
|
|
1212
|
+
* To implement an async function, create a promise with {@link newPromise}, then
|
|
1213
|
+
* return the deferred promise handle from `deferred.handle` from your
|
|
1214
|
+
* function implementation:
|
|
1215
|
+
*
|
|
1216
|
+
* ```typescript
|
|
1217
|
+
* const deferred = vm.newPromise()
|
|
1218
|
+
* someNativeAsyncFunction().then(deferred.resolve)
|
|
1219
|
+
* return deferred.handle
|
|
1220
|
+
* ```
|
|
1221
|
+
*/
|
|
1222
|
+
newFunction(name: string, fn: VmFunctionImplementation<QuickJSHandle>): QuickJSHandle;
|
|
1223
|
+
newError(error: {
|
|
1224
|
+
name: string;
|
|
1225
|
+
message: string;
|
|
1226
|
+
}): QuickJSHandle;
|
|
1227
|
+
newError(message: string): QuickJSHandle;
|
|
1228
|
+
newError(): QuickJSHandle;
|
|
1229
|
+
/**
|
|
1230
|
+
* `typeof` operator. **Not** [standards compliant](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof).
|
|
1231
|
+
*
|
|
1232
|
+
* @remarks
|
|
1233
|
+
* Does not support BigInt values correctly.
|
|
1234
|
+
*/
|
|
1235
|
+
typeof(handle: QuickJSHandle): string;
|
|
1236
|
+
/**
|
|
1237
|
+
* Converts `handle` into a Javascript number.
|
|
1238
|
+
* @returns `NaN` on error, otherwise a `number`.
|
|
1239
|
+
*/
|
|
1240
|
+
getNumber(handle: QuickJSHandle): number;
|
|
1241
|
+
/**
|
|
1242
|
+
* Converts `handle` to a Javascript string.
|
|
1243
|
+
*/
|
|
1244
|
+
getString(handle: QuickJSHandle): string;
|
|
1245
|
+
/**
|
|
1246
|
+
* Converts `handle` into a Javascript symbol. If the symbol is in the global
|
|
1247
|
+
* registry in the guest, it will be created with Symbol.for on the host.
|
|
1248
|
+
*/
|
|
1249
|
+
getSymbol(handle: QuickJSHandle): symbol;
|
|
1250
|
+
/**
|
|
1251
|
+
* Converts `handle` to a Javascript bigint.
|
|
1252
|
+
*/
|
|
1253
|
+
getBigInt(handle: QuickJSHandle): bigint;
|
|
1254
|
+
/**
|
|
1255
|
+
* Coverts `handle` to a JavaScript ArrayBuffer
|
|
1256
|
+
*/
|
|
1257
|
+
getArrayBuffer(handle: QuickJSHandle): Lifetime<Uint8Array>;
|
|
1258
|
+
/**
|
|
1259
|
+
* Get the current state of a QuickJS promise, see {@link JSPromiseState} for the possible states.
|
|
1260
|
+
* This can be used to expect a promise to be fulfilled when combined with {@link unwrapResult}:
|
|
1261
|
+
*
|
|
1262
|
+
* ```typescript
|
|
1263
|
+
* const promiseHandle = context.evalCode(`Promise.resolve(42)`);
|
|
1264
|
+
* const resultHandle = context.unwrapResult(
|
|
1265
|
+
* context.getPromiseState(promiseHandle)
|
|
1266
|
+
* );
|
|
1267
|
+
* context.getNumber(resultHandle) === 42; // true
|
|
1268
|
+
* resultHandle.dispose();
|
|
1269
|
+
* ```
|
|
1270
|
+
*/
|
|
1271
|
+
getPromiseState(handle: QuickJSHandle): JSPromiseState;
|
|
1272
|
+
/**
|
|
1273
|
+
* `Promise.resolve(value)`.
|
|
1274
|
+
* Convert a handle containing a Promise-like value inside the VM into an
|
|
1275
|
+
* actual promise on the host.
|
|
1276
|
+
*
|
|
1277
|
+
* @remarks
|
|
1278
|
+
* You may need to call {@link runtime}.{@link QuickJSRuntime#executePendingJobs} to ensure that the promise is resolved.
|
|
1279
|
+
*
|
|
1280
|
+
* @param promiseLikeHandle - A handle to a Promise-like value with a `.then(onSuccess, onError)` method.
|
|
1281
|
+
*/
|
|
1282
|
+
resolvePromise(promiseLikeHandle: QuickJSHandle): Promise<QuickJSContextResult<QuickJSHandle>>;
|
|
1283
|
+
private isEqual;
|
|
1284
|
+
/**
|
|
1285
|
+
* `handle === other` - IsStrictlyEqual.
|
|
1286
|
+
* See [Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
|
|
1287
|
+
*/
|
|
1288
|
+
eq(handle: QuickJSHandle, other: QuickJSHandle): boolean;
|
|
1289
|
+
/**
|
|
1290
|
+
* `Object.is(a, b)`
|
|
1291
|
+
* See [Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
|
|
1292
|
+
*/
|
|
1293
|
+
sameValue(handle: QuickJSHandle, other: QuickJSHandle): boolean;
|
|
1294
|
+
/**
|
|
1295
|
+
* SameValueZero comparison.
|
|
1296
|
+
* See [Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
|
|
1297
|
+
*/
|
|
1298
|
+
sameValueZero(handle: QuickJSHandle, other: QuickJSHandle): boolean;
|
|
1299
|
+
/**
|
|
1300
|
+
* `handle[key]`.
|
|
1301
|
+
* Get a property from a JSValue.
|
|
1302
|
+
*
|
|
1303
|
+
* @param key - The property may be specified as a JSValue handle, or as a
|
|
1304
|
+
* Javascript string (which will be converted automatically).
|
|
1305
|
+
*/
|
|
1306
|
+
getProp(handle: QuickJSHandle, key: QuickJSPropertyKey): QuickJSHandle;
|
|
1307
|
+
/**
|
|
1308
|
+
* `handle.length` as a host number.
|
|
1309
|
+
*
|
|
1310
|
+
* Example use:
|
|
1311
|
+
* ```typescript
|
|
1312
|
+
* const length = context.getLength(arrayHandle) ?? 0
|
|
1313
|
+
* for (let i = 0; i < length; i++) {
|
|
1314
|
+
* using value = context.getProp(arrayHandle, i)
|
|
1315
|
+
* console.log(`array[${i}] =`, context.dump(value))
|
|
1316
|
+
* }
|
|
1317
|
+
* ```
|
|
1318
|
+
*
|
|
1319
|
+
* @returns a number if the handle has a numeric length property, otherwise `undefined`.
|
|
1320
|
+
*/
|
|
1321
|
+
getLength(handle: QuickJSHandle): number | undefined;
|
|
1322
|
+
/**
|
|
1323
|
+
* `Object.getOwnPropertyNames(handle)`.
|
|
1324
|
+
* Similar to the [standard semantics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames),
|
|
1325
|
+
* but with extra, non-standard options for:
|
|
1326
|
+
*
|
|
1327
|
+
* - fetching array indexes as numbers (`numbers: true`)
|
|
1328
|
+
* - including symbols (`symbols: true`)
|
|
1329
|
+
* - only iterating over enumerable properties (`onlyEnumerable: true`)
|
|
1330
|
+
*
|
|
1331
|
+
* The default behavior is to emulate the standard:
|
|
1332
|
+
* ```typescript
|
|
1333
|
+
* context.getOwnPropertyNames(handle, { strings: true, numbersAsStrings: true })
|
|
1334
|
+
* ```
|
|
1335
|
+
*
|
|
1336
|
+
* Note when passing an explicit options object, you must set at least one
|
|
1337
|
+
* option, and `strings` are not included unless specified.
|
|
1338
|
+
*
|
|
1339
|
+
* Example use:
|
|
1340
|
+
* ```typescript
|
|
1341
|
+
* for (using prop of context.getOwnPropertyNames(objectHandle).unwrap()) {
|
|
1342
|
+
* using value = context.getProp(handle, prop)
|
|
1343
|
+
* console.log(context.dump(prop), '->', context.dump(value))
|
|
1344
|
+
* }
|
|
1345
|
+
* ```
|
|
1346
|
+
*
|
|
1347
|
+
* @returns an an array of handles of the property names. The array itself is disposable for your convenience.
|
|
1348
|
+
* @throws QuickJSEmptyGetOwnPropertyNames if no options are set.
|
|
1349
|
+
*/
|
|
1350
|
+
getOwnPropertyNames(handle: QuickJSHandle, options?: GetOwnPropertyNamesOptions): QuickJSContextResult<DisposableArray<QuickJSHandle>>;
|
|
1351
|
+
/**
|
|
1352
|
+
* `handle[Symbol.iterator]()`. See {@link QuickJSIterator}.
|
|
1353
|
+
* Returns a host iterator that wraps and proxies calls to a guest iterator handle.
|
|
1354
|
+
* Each step of the iteration returns a result, either an error or a handle to the next value.
|
|
1355
|
+
* Once the iterator is done, the handle is automatically disposed, and the iterator
|
|
1356
|
+
* is considered done if the handle is disposed.
|
|
1357
|
+
*
|
|
1358
|
+
* ```typescript
|
|
1359
|
+
* for (using entriesHandle of context.getIterator(mapHandle).unwrap()) {
|
|
1360
|
+
* using keyHandle = context.getProp(entriesHandle, 0)
|
|
1361
|
+
* using valueHandle = context.getProp(entriesHandle, 1)
|
|
1362
|
+
* console.log(context.dump(keyHandle), '->', context.dump(valueHandle))
|
|
1363
|
+
* }
|
|
1364
|
+
* ```
|
|
1365
|
+
*/
|
|
1366
|
+
getIterator(iterableHandle: QuickJSHandle): QuickJSContextResult<QuickJSIterator>;
|
|
1367
|
+
/**
|
|
1368
|
+
* `handle[key] = value`.
|
|
1369
|
+
* Set a property on a JSValue.
|
|
1370
|
+
*
|
|
1371
|
+
* @remarks
|
|
1372
|
+
* Note that the QuickJS authors recommend using {@link defineProp} to define new
|
|
1373
|
+
* properties.
|
|
1374
|
+
*
|
|
1375
|
+
* @param key - The property may be specified as a JSValue handle, or as a
|
|
1376
|
+
* Javascript string or number (which will be converted automatically to a JSValue).
|
|
1377
|
+
*/
|
|
1378
|
+
setProp(handle: QuickJSHandle, key: QuickJSPropertyKey, value: QuickJSHandle): void;
|
|
1379
|
+
/**
|
|
1380
|
+
* [`Object.defineProperty(handle, key, descriptor)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).
|
|
1381
|
+
*
|
|
1382
|
+
* @param key - The property may be specified as a JSValue handle, or as a
|
|
1383
|
+
* Javascript string or number (which will be converted automatically to a JSValue).
|
|
1384
|
+
*/
|
|
1385
|
+
defineProp(handle: QuickJSHandle, key: QuickJSPropertyKey, descriptor: VmPropertyDescriptor<QuickJSHandle>): void;
|
|
1386
|
+
/**
|
|
1387
|
+
* [`func.call(thisVal, ...args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) or
|
|
1388
|
+
* [`func.apply(thisVal, args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply).
|
|
1389
|
+
* Call a JSValue as a function.
|
|
1390
|
+
*
|
|
1391
|
+
* See {@link unwrapResult}, which will throw if the function returned an error, or
|
|
1392
|
+
* return the result handle directly. If evaluation returned a handle containing
|
|
1393
|
+
* a promise, use {@link resolvePromise} to convert it to a native promise and
|
|
1394
|
+
* {@link runtime}.{@link QuickJSRuntime#executePendingJobs} to finish evaluating the promise.
|
|
1395
|
+
*
|
|
1396
|
+
* @returns A result. If the function threw synchronously, `result.error` be a
|
|
1397
|
+
* handle to the exception. Otherwise `result.value` will be a handle to the
|
|
1398
|
+
* value.
|
|
1399
|
+
*
|
|
1400
|
+
* Example:
|
|
1401
|
+
*
|
|
1402
|
+
* ```typescript
|
|
1403
|
+
* using parseIntHandle = context.getProp(global, "parseInt")
|
|
1404
|
+
* using stringHandle = context.newString("42")
|
|
1405
|
+
* using resultHandle = context.callFunction(parseIntHandle, context.undefined, stringHandle).unwrap()
|
|
1406
|
+
* console.log(context.dump(resultHandle)) // 42
|
|
1407
|
+
* ```
|
|
1408
|
+
*/
|
|
1409
|
+
callFunction(func: QuickJSHandle, thisVal: QuickJSHandle, args?: QuickJSHandle[]): QuickJSContextResult<QuickJSHandle>;
|
|
1410
|
+
callFunction(func: QuickJSHandle, thisVal: QuickJSHandle, ...args: QuickJSHandle[]): QuickJSContextResult<QuickJSHandle>;
|
|
1411
|
+
/**
|
|
1412
|
+
* `handle[key](...args)`
|
|
1413
|
+
*
|
|
1414
|
+
* Call a method on a JSValue. This is a convenience method that calls {@link getProp} and {@link callFunction}.
|
|
1415
|
+
*
|
|
1416
|
+
* @returns A result. If the function threw synchronously, `result.error` be a
|
|
1417
|
+
* handle to the exception. Otherwise `result.value` will be a handle to the
|
|
1418
|
+
* value.
|
|
1419
|
+
*/
|
|
1420
|
+
callMethod(thisHandle: QuickJSHandle, key: QuickJSPropertyKey, args?: QuickJSHandle[]): QuickJSContextResult<QuickJSHandle>;
|
|
1421
|
+
/**
|
|
1422
|
+
* Like [`eval(code)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Description).
|
|
1423
|
+
*
|
|
1424
|
+
* Evaluates `code`, as though it's in a file named `filename`, with options `options`.
|
|
1425
|
+
*
|
|
1426
|
+
* - When `options.type` is `"global"`, the code is evaluated in the global
|
|
1427
|
+
* scope of the QuickJSContext, and the return value is the result of the last
|
|
1428
|
+
* expression.
|
|
1429
|
+
* - When `options.type` is `"module"`, the code is evaluated is a module scope.
|
|
1430
|
+
* It may use `import` and `export` if {@link runtime}.{@link QuickJSRuntime#setModuleLoader} was called.
|
|
1431
|
+
* It may use top-level await if supported by the underlying QuickJS library.
|
|
1432
|
+
* The return value is the module's exports, or a promise for the module's exports.
|
|
1433
|
+
* - When `options.type` is unset, the code is evaluated as a module if it
|
|
1434
|
+
* contains an `import` or `export` statement, otherwise it is evaluated in
|
|
1435
|
+
* the global scope.
|
|
1436
|
+
*
|
|
1437
|
+
* When working with async code, you many need to call {@link runtime}.{@link QuickJSRuntime#executePendingJobs}
|
|
1438
|
+
* to execute callbacks pending after synchronous evaluation returns.
|
|
1439
|
+
*
|
|
1440
|
+
* See {@link unwrapResult}, which will throw if the function returned an error, or
|
|
1441
|
+
* return the result handle directly. If evaluation returned a handle containing
|
|
1442
|
+
* a promise, use {@link resolvePromise} to convert it to a native promise and
|
|
1443
|
+
* {@link QuickJSRuntime#executePendingJobs} to finish evaluating the promise.
|
|
1444
|
+
*
|
|
1445
|
+
* *Note*: to protect against infinite loops, provide an interrupt handler to
|
|
1446
|
+
* {@link QuickJSRuntime#setInterruptHandler}. You can use {@link shouldInterruptAfterDeadline} to
|
|
1447
|
+
* create a time-based deadline.
|
|
1448
|
+
*
|
|
1449
|
+
* @returns The last statement's value. If the code threw synchronously,
|
|
1450
|
+
* `result.error` will be a handle to the exception. If execution was
|
|
1451
|
+
* interrupted, the error will have name `InternalError` and message
|
|
1452
|
+
* `interrupted`.
|
|
1453
|
+
*/
|
|
1454
|
+
evalCode(code: string, filename?: string,
|
|
1455
|
+
/**
|
|
1456
|
+
* If no options are passed, a heuristic will be used to detect if `code` is
|
|
1457
|
+
* an ES module.
|
|
1458
|
+
*
|
|
1459
|
+
* See {@link EvalFlags} for number semantics.
|
|
1460
|
+
*/
|
|
1461
|
+
options?: number | ContextEvalOptions): QuickJSContextResult<QuickJSHandle>;
|
|
1462
|
+
/**
|
|
1463
|
+
* Throw an error in the VM, interrupted whatever current execution is in progress when execution resumes.
|
|
1464
|
+
* @experimental
|
|
1465
|
+
*/
|
|
1466
|
+
throw(error: Error | QuickJSHandle): JSValuePointer;
|
|
1467
|
+
/**
|
|
1468
|
+
* @private
|
|
1469
|
+
*/
|
|
1470
|
+
protected borrowPropertyKey(key: QuickJSPropertyKey): QuickJSHandle;
|
|
1471
|
+
/**
|
|
1472
|
+
* @private
|
|
1473
|
+
*/
|
|
1474
|
+
getMemory(rt: JSRuntimePointer): ContextMemory;
|
|
1475
|
+
/**
|
|
1476
|
+
* Dump a JSValue to Javascript in a best-effort fashion.
|
|
1477
|
+
* If the value is a promise, dumps the promise's state.
|
|
1478
|
+
* Returns `handle.toString()` if it cannot be serialized to JSON.
|
|
1479
|
+
*/
|
|
1480
|
+
dump(handle: QuickJSHandle): any;
|
|
1481
|
+
/**
|
|
1482
|
+
* Unwrap a SuccessOrFail result such as a {@link VmCallResult} or a
|
|
1483
|
+
* {@link ExecutePendingJobsResult}, where the fail branch contains a handle to a QuickJS error value.
|
|
1484
|
+
* If the result is a success, returns the value.
|
|
1485
|
+
* If the result is an error, converts the error to a native object and throws the error.
|
|
1486
|
+
*/
|
|
1487
|
+
unwrapResult<T>(result: SuccessOrFail<T, QuickJSHandle>): T;
|
|
1488
|
+
/** @private */
|
|
1489
|
+
protected fnNextId: number;
|
|
1490
|
+
/** @private */
|
|
1491
|
+
protected fnMaps: Map<number, Map<number, VmFunctionImplementation<QuickJSHandle>>>;
|
|
1492
|
+
/** @private */
|
|
1493
|
+
protected getFunction(fn_id: number): VmFunctionImplementation<QuickJSHandle> | undefined;
|
|
1494
|
+
/** @private */
|
|
1495
|
+
protected setFunction(fn_id: number, handle: VmFunctionImplementation<QuickJSHandle>): Map<number, VmFunctionImplementation<QuickJSHandle>>;
|
|
1496
|
+
/**
|
|
1497
|
+
* @hidden
|
|
1498
|
+
*/
|
|
1499
|
+
private cToHostCallbacks;
|
|
1500
|
+
private errorToHandle;
|
|
1501
|
+
/**
|
|
1502
|
+
* Outputs QuickJS Objects in binary form
|
|
1503
|
+
*
|
|
1504
|
+
* **WARNING**: QuickJS's binary JSON doesn't have a standard so expect it to change between version
|
|
1505
|
+
*
|
|
1506
|
+
* ```ts
|
|
1507
|
+
* // imagine sending data to another via IPC
|
|
1508
|
+
* let dataLifetime = context.newString("This is an example")
|
|
1509
|
+
* ?.consume(handle => context.encodeBinaryJSON(handle))
|
|
1510
|
+
* ?.consume(handle => context.getArrayBuffer(handle))
|
|
1511
|
+
* socket.write(dataLifetime?.value)
|
|
1512
|
+
* ```
|
|
1513
|
+
*/
|
|
1514
|
+
encodeBinaryJSON(handle: QuickJSHandle): QuickJSHandle;
|
|
1515
|
+
/**
|
|
1516
|
+
* Outputs Handle of the given QuickJS Object in binary form
|
|
1517
|
+
*
|
|
1518
|
+
* ```ts
|
|
1519
|
+
* // imagine receiving data from another via IPC
|
|
1520
|
+
* socket.on("data", chunk => {
|
|
1521
|
+
* context.newArrayBuffer(chunk)
|
|
1522
|
+
* ?.consume(handle => context.decodeBinaryJSON(handle))
|
|
1523
|
+
* ?.consume(handle => console.log(context.dump(handle)))
|
|
1524
|
+
* })
|
|
1525
|
+
* ```
|
|
1526
|
+
*/
|
|
1527
|
+
decodeBinaryJSON(handle: QuickJSHandle): QuickJSHandle;
|
|
1528
|
+
/**
|
|
1529
|
+
* Compile code to bytecode without executing it.
|
|
1530
|
+
* The bytecode can be serialized with {@link encodeBytecode} and later
|
|
1531
|
+
* restored with {@link decodeBytecode} and executed with {@link evalBytecode}.
|
|
1532
|
+
*
|
|
1533
|
+
* This enables bytecode caching for faster startup (~2x improvement).
|
|
1534
|
+
*
|
|
1535
|
+
* ```ts
|
|
1536
|
+
* // Compile code to bytecode
|
|
1537
|
+
* const bytecodeHandle = context.compileCode(`
|
|
1538
|
+
* function hello() { return "world"; }
|
|
1539
|
+
* hello();
|
|
1540
|
+
* `, "example.js").unwrap()
|
|
1541
|
+
*
|
|
1542
|
+
* // Serialize to ArrayBuffer for caching
|
|
1543
|
+
* const serialized = context.encodeBytecode(bytecodeHandle)
|
|
1544
|
+
* .consume(handle => context.getArrayBuffer(handle))
|
|
1545
|
+
*
|
|
1546
|
+
* // Later: restore and execute
|
|
1547
|
+
* const restored = context.newArrayBuffer(serialized.value)
|
|
1548
|
+
* .consume(handle => context.decodeBytecode(handle))
|
|
1549
|
+
* const result = context.evalBytecode(restored).unwrap()
|
|
1550
|
+
* ```
|
|
1551
|
+
*/
|
|
1552
|
+
compileCode(code: string, filename?: string, options?: Omit<ContextEvalOptions, "compileOnly">): QuickJSContextResult<QuickJSHandle>;
|
|
1553
|
+
/**
|
|
1554
|
+
* Execute a bytecode function that was previously compiled with
|
|
1555
|
+
* {@link compileCode} or `evalCode({ compileOnly: true })`, or restored
|
|
1556
|
+
* from serialized bytecode via {@link decodeBytecode}.
|
|
1557
|
+
*
|
|
1558
|
+
* @returns A result. If execution threw synchronously, `result.error` will be
|
|
1559
|
+
* a handle to the exception. Otherwise `result.value` will be a handle to the
|
|
1560
|
+
* return value.
|
|
1561
|
+
*/
|
|
1562
|
+
evalBytecode(handle: QuickJSHandle): QuickJSContextResult<QuickJSHandle>;
|
|
1563
|
+
/**
|
|
1564
|
+
* Serialize a bytecode function to binary format.
|
|
1565
|
+
* The bytecode can be stored/cached and later restored with {@link decodeBytecode}.
|
|
1566
|
+
*
|
|
1567
|
+
* This is more efficient than {@link encodeBinaryJSON} for bytecode because it
|
|
1568
|
+
* uses the `JS_WRITE_OBJ_BYTECODE` flag optimized for function serialization.
|
|
1569
|
+
*
|
|
1570
|
+
* **WARNING**: The bytecode format is not standardized and may change between
|
|
1571
|
+
* QuickJS versions.
|
|
1572
|
+
*
|
|
1573
|
+
* @param handle - A handle to a bytecode function (from {@link compileCode} or
|
|
1574
|
+
* `evalCode({ compileOnly: true })`)
|
|
1575
|
+
* @returns A handle to an ArrayBuffer containing the serialized bytecode
|
|
1576
|
+
*/
|
|
1577
|
+
encodeBytecode(handle: QuickJSHandle): QuickJSHandle;
|
|
1578
|
+
/**
|
|
1579
|
+
* Deserialize bytecode from binary format.
|
|
1580
|
+
* The bytecode must have been serialized with {@link encodeBytecode}.
|
|
1581
|
+
*
|
|
1582
|
+
* After decoding, use {@link evalBytecode} to execute the bytecode.
|
|
1583
|
+
*
|
|
1584
|
+
* **WARNING**: The bytecode format is not standardized and may change between
|
|
1585
|
+
* QuickJS versions.
|
|
1586
|
+
*
|
|
1587
|
+
* @param handle - A handle to an ArrayBuffer containing serialized bytecode
|
|
1588
|
+
* @returns A handle to the deserialized bytecode function
|
|
1589
|
+
*/
|
|
1590
|
+
decodeBytecode(handle: QuickJSHandle): QuickJSHandle;
|
|
1591
|
+
protected success<S>(value: S): DisposableSuccess<S>;
|
|
1592
|
+
protected fail(error: QuickJSHandle): DisposableFail<QuickJSHandle>;
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
type EmscriptenCallback<BaseArgs extends any[], Result> = (...args: [Asyncify | undefined, ...BaseArgs]) => Result | AsyncifySleepResult<Result>;
|
|
1596
|
+
type MaybeAsyncEmscriptenCallback<T extends EmscriptenCallback<any, any>> = T extends EmscriptenCallback<infer Args, infer Result> ? (...args: Args) => Result | Promise<Result> : never;
|
|
1597
|
+
type MaybeAsyncEmscriptenCallbacks = {
|
|
1598
|
+
[K in keyof EmscriptenModuleCallbacks]: MaybeAsyncEmscriptenCallback<EmscriptenModuleCallbacks[K]>;
|
|
1599
|
+
};
|
|
1600
|
+
/**
|
|
1601
|
+
* @private
|
|
1602
|
+
*/
|
|
1603
|
+
interface ContextCallbacks {
|
|
1604
|
+
callFunction: MaybeAsyncEmscriptenCallbacks["callFunction"];
|
|
1605
|
+
}
|
|
1606
|
+
/**
|
|
1607
|
+
* @private
|
|
1608
|
+
*/
|
|
1609
|
+
interface RuntimeCallbacks {
|
|
1610
|
+
shouldInterrupt: MaybeAsyncEmscriptenCallbacks["shouldInterrupt"];
|
|
1611
|
+
loadModuleSource: MaybeAsyncEmscriptenCallbacks["loadModuleSource"];
|
|
1612
|
+
normalizeModule: MaybeAsyncEmscriptenCallbacks["normalizeModule"];
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* Options for {@link QuickJSWASMModule#evalCode}.
|
|
1616
|
+
*/
|
|
1617
|
+
interface ModuleEvalOptions {
|
|
1618
|
+
/**
|
|
1619
|
+
* Interrupt evaluation if `shouldInterrupt` returns `true`.
|
|
1620
|
+
* See {@link shouldInterruptAfterDeadline}.
|
|
1621
|
+
*/
|
|
1622
|
+
shouldInterrupt?: InterruptHandler;
|
|
1623
|
+
/**
|
|
1624
|
+
* Memory limit, in bytes, of WebAssembly heap memory used by the QuickJS VM.
|
|
1625
|
+
*/
|
|
1626
|
+
memoryLimitBytes?: number;
|
|
1627
|
+
/**
|
|
1628
|
+
* Stack size limit for this vm, in bytes
|
|
1629
|
+
* To remove the limit, set to `0`.
|
|
1630
|
+
*/
|
|
1631
|
+
maxStackSizeBytes?: number;
|
|
1632
|
+
/**
|
|
1633
|
+
* Module loader for any `import` statements or expressions.
|
|
1634
|
+
*/
|
|
1635
|
+
moduleLoader?: JSModuleLoader;
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1638
|
+
* We use static functions per module to dispatch runtime or context calls from
|
|
1639
|
+
* C to the host. This class manages the indirection from a specific runtime or
|
|
1640
|
+
* context pointer to the appropriate callback handler.
|
|
1641
|
+
*
|
|
1642
|
+
* @private
|
|
1643
|
+
*/
|
|
1644
|
+
declare class QuickJSModuleCallbacks {
|
|
1645
|
+
private module;
|
|
1646
|
+
private contextCallbacks;
|
|
1647
|
+
private runtimeCallbacks;
|
|
1648
|
+
constructor(module: EitherModule);
|
|
1649
|
+
setRuntimeCallbacks(rt: JSRuntimePointer, callbacks: RuntimeCallbacks): void;
|
|
1650
|
+
deleteRuntime(rt: JSRuntimePointer): void;
|
|
1651
|
+
setContextCallbacks(ctx: JSContextPointer, callbacks: ContextCallbacks): void;
|
|
1652
|
+
deleteContext(ctx: JSContextPointer): void;
|
|
1653
|
+
private suspendedCount;
|
|
1654
|
+
private suspended;
|
|
1655
|
+
private handleAsyncify;
|
|
1656
|
+
private cToHostCallbacks;
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* This class presents a Javascript interface to QuickJS, a Javascript interpreter
|
|
1660
|
+
* that supports EcmaScript 2020 (ES2020).
|
|
1661
|
+
*
|
|
1662
|
+
* It wraps a single WebAssembly module containing the QuickJS library and
|
|
1663
|
+
* associated helper C code. WebAssembly modules are completely isolated from
|
|
1664
|
+
* each other by the host's WebAssembly runtime. Separate WebAssembly modules
|
|
1665
|
+
* have the most isolation guarantees possible with this library.
|
|
1666
|
+
*
|
|
1667
|
+
* The simplest way to start running code is {@link evalCode}. This shortcut
|
|
1668
|
+
* method will evaluate Javascript safely and return the result as a native
|
|
1669
|
+
* Javascript value.
|
|
1670
|
+
*
|
|
1671
|
+
* For more control over the execution environment, or to interact with values
|
|
1672
|
+
* inside QuickJS, create a context with {@link newContext} or a runtime with
|
|
1673
|
+
* {@link newRuntime}.
|
|
1674
|
+
*/
|
|
1675
|
+
declare class QuickJSWASMModule {
|
|
1676
|
+
/** @private */
|
|
1677
|
+
protected ffi: EitherFFI;
|
|
1678
|
+
/** @private */
|
|
1679
|
+
protected callbacks: QuickJSModuleCallbacks;
|
|
1680
|
+
/** @private */
|
|
1681
|
+
protected module: EitherModule;
|
|
1682
|
+
/** @private */
|
|
1683
|
+
constructor(module: EitherModule, ffi: EitherFFI);
|
|
1684
|
+
/**
|
|
1685
|
+
* Create a runtime.
|
|
1686
|
+
* Use the runtime to set limits on CPU and memory usage and configure module
|
|
1687
|
+
* loading for one or more {@link QuickJSContext}s inside the runtime.
|
|
1688
|
+
*/
|
|
1689
|
+
newRuntime(options?: RuntimeOptions): QuickJSRuntime;
|
|
1690
|
+
/**
|
|
1691
|
+
* A simplified API to create a new {@link QuickJSRuntime} and a
|
|
1692
|
+
* {@link QuickJSContext} inside that runtime at the same time. The runtime will
|
|
1693
|
+
* be disposed when the context is disposed.
|
|
1694
|
+
*/
|
|
1695
|
+
newContext(options?: ContextOptions): QuickJSContext;
|
|
1696
|
+
/**
|
|
1697
|
+
* One-off evaluate code without needing to create a {@link QuickJSRuntime} or
|
|
1698
|
+
* {@link QuickJSContext} explicitly.
|
|
1699
|
+
*
|
|
1700
|
+
* To protect against infinite loops, use the `shouldInterrupt` option. The
|
|
1701
|
+
* {@link shouldInterruptAfterDeadline} function will create a time-based deadline.
|
|
1702
|
+
*
|
|
1703
|
+
* If you need more control over how the code executes, create a
|
|
1704
|
+
* {@link QuickJSRuntime} (with {@link newRuntime}) or a {@link QuickJSContext} (with
|
|
1705
|
+
* {@link newContext} or {@link QuickJSRuntime#newContext}), and use its
|
|
1706
|
+
* {@link QuickJSContext#evalCode} method.
|
|
1707
|
+
*
|
|
1708
|
+
* Asynchronous callbacks may not run during the first call to `evalCode`. If
|
|
1709
|
+
* you need to work with async code inside QuickJS, create a runtime and use
|
|
1710
|
+
* {@link QuickJSRuntime#executePendingJobs}.
|
|
1711
|
+
*
|
|
1712
|
+
* @returns The result is coerced to a native Javascript value using JSON
|
|
1713
|
+
* serialization, so properties and values unsupported by JSON will be dropped.
|
|
1714
|
+
*
|
|
1715
|
+
* @throws If `code` throws during evaluation, the exception will be
|
|
1716
|
+
* converted into a native Javascript value and thrown.
|
|
1717
|
+
*
|
|
1718
|
+
* @throws if `options.shouldInterrupt` interrupted execution, will throw a Error
|
|
1719
|
+
* with name `"InternalError"` and message `"interrupted"`.
|
|
1720
|
+
*/
|
|
1721
|
+
evalCode(code: string, options?: ModuleEvalOptions): unknown;
|
|
1722
|
+
/**
|
|
1723
|
+
* Retrieve the WebAssembly memory used by this QuickJS module.
|
|
1724
|
+
* Use this access very carefully - you are responsible for safe interaction with the memory.
|
|
1725
|
+
*
|
|
1726
|
+
* To supply a custom, pre-initialized memory to QuickJS, create a new variant
|
|
1727
|
+
* and provide the {@link CustomizeVariantOptions#wasmMemory} option.
|
|
1728
|
+
*
|
|
1729
|
+
* @experimental
|
|
1730
|
+
*/
|
|
1731
|
+
getWasmMemory(): WebAssembly.Memory;
|
|
1732
|
+
/**
|
|
1733
|
+
* Get a low-level interface to the QuickJS functions in this WebAssembly
|
|
1734
|
+
* module.
|
|
1735
|
+
* @experimental
|
|
1736
|
+
* @unstable No warranty is provided with this API. It could change at any time.
|
|
1737
|
+
* @private
|
|
1738
|
+
*/
|
|
1739
|
+
getFFI(): EitherFFI;
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
/**
|
|
1743
|
+
* Asyncified version of {@link QuickJSWASMModule}.
|
|
1744
|
+
*
|
|
1745
|
+
* Due to limitations of Emscripten's ASYNCIFY process, only a single async
|
|
1746
|
+
* function call can happen at a time across the entire WebAssembly module.
|
|
1747
|
+
*
|
|
1748
|
+
* That means that all runtimes, contexts, functions, etc created inside this
|
|
1749
|
+
* WebAssembly are limited to a single concurrent async action.
|
|
1750
|
+
* **Multiple concurrent async actions is an error.**
|
|
1751
|
+
*
|
|
1752
|
+
* To allow for multiple concurrent async actions, you must create multiple WebAssembly
|
|
1753
|
+
* modules.
|
|
1754
|
+
*/
|
|
1755
|
+
declare class QuickJSAsyncWASMModule extends QuickJSWASMModule {
|
|
1756
|
+
/** @private */
|
|
1757
|
+
protected ffi: QuickJSAsyncFFI;
|
|
1758
|
+
/** @private */
|
|
1759
|
+
protected module: QuickJSAsyncEmscriptenModule;
|
|
1760
|
+
/** @private */
|
|
1761
|
+
constructor(module: QuickJSAsyncEmscriptenModule, ffi: QuickJSAsyncFFI);
|
|
1762
|
+
/**
|
|
1763
|
+
* Create a new async runtime inside this WebAssembly module. All runtimes inside a
|
|
1764
|
+
* module are limited to a single async call at a time. For multiple
|
|
1765
|
+
* concurrent async actions, create multiple WebAssembly modules.
|
|
1766
|
+
*/
|
|
1767
|
+
newRuntime(options?: AsyncRuntimeOptions): QuickJSAsyncRuntime;
|
|
1768
|
+
/**
|
|
1769
|
+
* A simplified API to create a new {@link QuickJSAsyncRuntime} and a
|
|
1770
|
+
* {@link QuickJSAsyncContext} inside that runtime at the same time. The runtime will
|
|
1771
|
+
* be disposed when the context is disposed.
|
|
1772
|
+
*/
|
|
1773
|
+
newContext(options?: ContextOptions): QuickJSAsyncContext;
|
|
1774
|
+
/** Synchronous evalCode is not supported. */
|
|
1775
|
+
evalCode(): never;
|
|
1776
|
+
/**
|
|
1777
|
+
* One-off evaluate code without needing to create a {@link QuickJSAsyncRuntime} or
|
|
1778
|
+
* {@link QuickJSAsyncContext} explicitly.
|
|
1779
|
+
*
|
|
1780
|
+
* This version allows for asynchronous Ecmascript module loading.
|
|
1781
|
+
*
|
|
1782
|
+
* Note that only a single async action can occur at a time inside the entire WebAssembly module.
|
|
1783
|
+
* **Multiple concurrent async actions is an error.**
|
|
1784
|
+
*
|
|
1785
|
+
* See the documentation for {@link QuickJSWASMModule#evalCode} for more details.
|
|
1786
|
+
*/
|
|
1787
|
+
evalCodeAsync(code: string, options: ModuleEvalOptions): Promise<unknown>;
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
type PromisedDefault<T> = T | Promise<T> | Promise<{
|
|
1791
|
+
default: T;
|
|
1792
|
+
}> | Promise<{
|
|
1793
|
+
default: {
|
|
1794
|
+
default: T;
|
|
1795
|
+
};
|
|
1796
|
+
}>;
|
|
1797
|
+
/**
|
|
1798
|
+
* Create a new, completely isolated WebAssembly module containing the QuickJS library.
|
|
1799
|
+
* See the documentation on {@link QuickJSWASMModule}.
|
|
1800
|
+
*
|
|
1801
|
+
* Note that there is a hard limit on the number of WebAssembly modules in older
|
|
1802
|
+
* versions of v8:
|
|
1803
|
+
* https://bugs.chromium.org/p/v8/issues/detail?id=12076
|
|
1804
|
+
*
|
|
1805
|
+
* @example
|
|
1806
|
+
* ```ts
|
|
1807
|
+
* const quickjs = new newQuickJSWASMModuleFromVariant(
|
|
1808
|
+
* import('@componentor/quickjs-browser-release-sync-wasm')
|
|
1809
|
+
* )
|
|
1810
|
+
* ```
|
|
1811
|
+
*/
|
|
1812
|
+
declare function newQuickJSWASMModuleFromVariant(
|
|
1813
|
+
/**
|
|
1814
|
+
* A {@link QuickJSSyncVariant} to construct the WebAssembly module.
|
|
1815
|
+
*/
|
|
1816
|
+
variantOrPromise: PromisedDefault<QuickJSSyncVariant>): Promise<QuickJSWASMModule>;
|
|
1817
|
+
/**
|
|
1818
|
+
* Create a new, completely isolated WebAssembly module containing a version of the QuickJS library
|
|
1819
|
+
* compiled with Emscripten's [ASYNCIFY](https://emscripten.org/docs/porting/asyncify.html) transform.
|
|
1820
|
+
*
|
|
1821
|
+
* This version of the library offers features that enable synchronous code
|
|
1822
|
+
* inside the VM to interact with asynchronous code in the host environment.
|
|
1823
|
+
* See the documentation on {@link QuickJSAsyncWASMModule}, {@link QuickJSAsyncRuntime},
|
|
1824
|
+
* and {@link QuickJSAsyncContext}.
|
|
1825
|
+
*
|
|
1826
|
+
* Note that there is a hard limit on the number of WebAssembly modules in older
|
|
1827
|
+
* versions of v8:
|
|
1828
|
+
* https://bugs.chromium.org/p/v8/issues/detail?id=12076
|
|
1829
|
+
*
|
|
1830
|
+
* @example
|
|
1831
|
+
* ```ts
|
|
1832
|
+
* const quickjs = new newQuickJSAsyncWASMModuleFromVariant(
|
|
1833
|
+
* import('@componentor/quickjs-browser-debug-asyncify-wasm')
|
|
1834
|
+
* )
|
|
1835
|
+
* ```
|
|
1836
|
+
*/
|
|
1837
|
+
declare function newQuickJSAsyncWASMModuleFromVariant(
|
|
1838
|
+
/**
|
|
1839
|
+
* A {@link QuickJSAsyncVariant} to construct the WebAssembly module.
|
|
1840
|
+
*/
|
|
1841
|
+
variantOrPromise: PromisedDefault<QuickJSAsyncVariant>): Promise<QuickJSAsyncWASMModule>;
|
|
1842
|
+
/**
|
|
1843
|
+
* Helper intended to memoize the creation of a WebAssembly module.
|
|
1844
|
+
* ```typescript
|
|
1845
|
+
* const getDebugModule = memoizePromiseFactory(() => newQuickJSWASMModule(DEBUG_SYNC))
|
|
1846
|
+
* ```
|
|
1847
|
+
*/
|
|
1848
|
+
declare function memoizePromiseFactory<T>(fn: () => Promise<T>): () => Promise<T>;
|
|
1849
|
+
type OrLoader<T> = T | (() => Promise<T>);
|
|
1850
|
+
interface CustomizeVariantOptions {
|
|
1851
|
+
/** If given, Emscripten will try to load the WebAssembly module data from this location (path or URI) as appropriate for the current platform. */
|
|
1852
|
+
wasmLocation?: string;
|
|
1853
|
+
/** If given, Emscripten will compile the WebAssembly.Module from these bytes. */
|
|
1854
|
+
wasmBinary?: OrLoader<ArrayBuffer>;
|
|
1855
|
+
/** If given, Emscripten will instantiate the WebAssembly.Instance from this existing WebAssembly.Module */
|
|
1856
|
+
wasmModule?: OrLoader<WebAssembly.Module>;
|
|
1857
|
+
/** If given, use the Memory when instantiating the WebAssembly.Instance. */
|
|
1858
|
+
wasmMemory?: OrLoader<WebAssembly.Memory>;
|
|
1859
|
+
/** If given, Emscripten will try to load the source map for the WebAssembly module from this location (path or URI) as appropriate for the current platform. */
|
|
1860
|
+
wasmSourceMapLocation?: string;
|
|
1861
|
+
/** If given, we will provide the source map to Emscripten directly. This may only be respected if wasmModule is also provided. */
|
|
1862
|
+
wasmSourceMapData?: OrLoader<string | SourceMapData>;
|
|
1863
|
+
/**
|
|
1864
|
+
* If set, this method will be called when the runtime needs to load a file,
|
|
1865
|
+
* such as a .wasm WebAssembly file, .mem memory init file, or a file
|
|
1866
|
+
* generated by the file packager.
|
|
1867
|
+
*
|
|
1868
|
+
* The function receives two parameters:
|
|
1869
|
+
*
|
|
1870
|
+
* - `fileName`, the relative path to the file as configured in build
|
|
1871
|
+
* process, eg `"emscripten-module.wasm"`.
|
|
1872
|
+
* - `prefix` (path to the main JavaScript file’s directory). This may be `''`
|
|
1873
|
+
* (empty string) in some cases if the Emscripten Javascript code can't locate
|
|
1874
|
+
* itself. Try logging it in your environment.
|
|
1875
|
+
*
|
|
1876
|
+
* It should return the actual URI or path to the requested file.
|
|
1877
|
+
*
|
|
1878
|
+
* This lets you host file packages on a different location than the directory
|
|
1879
|
+
* of the JavaScript file (which is the default expectation), for example if
|
|
1880
|
+
* you want to host them on a CDN.
|
|
1881
|
+
*/
|
|
1882
|
+
locateFile?: EmscriptenModuleLoaderOptions["locateFile"];
|
|
1883
|
+
/** The enumerable properties of this object will be passed verbatim, although they may be overwritten if you pass other options. */
|
|
1884
|
+
emscriptenModule?: EmscriptenModuleLoaderOptions;
|
|
1885
|
+
/** Debug logger */
|
|
1886
|
+
log?: typeof console.log;
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* Create a new variant by overriding how Emscripten obtains the WebAssembly module.
|
|
1890
|
+
* This may be necessary in Cloudflare Workers, which can't compile WebAssembly modules from binary data.
|
|
1891
|
+
*/
|
|
1892
|
+
declare function newVariant<T extends QuickJSVariant>(baseVariant: T, options: CustomizeVariantOptions): T;
|
|
1893
|
+
|
|
1894
|
+
/**
|
|
1895
|
+
* Returns an interrupt handler that interrupts Javascript execution after a deadline time.
|
|
1896
|
+
*
|
|
1897
|
+
* @param deadline - Interrupt execution if it's still running after this time.
|
|
1898
|
+
* Number values are compared against `Date.now()`
|
|
1899
|
+
*/
|
|
1900
|
+
declare function shouldInterruptAfterDeadline(deadline: Date | number): InterruptHandler;
|
|
1901
|
+
|
|
1902
|
+
/**
|
|
1903
|
+
* Error thrown if {@link QuickJSContext#unwrapResult} unwraps an error value that isn't an object.
|
|
1904
|
+
*/
|
|
1905
|
+
declare class QuickJSUnwrapError extends Error {
|
|
1906
|
+
cause: unknown;
|
|
1907
|
+
context?: QuickJSContext | undefined;
|
|
1908
|
+
name: string;
|
|
1909
|
+
constructor(cause: unknown, context?: QuickJSContext | undefined);
|
|
1910
|
+
}
|
|
1911
|
+
declare class QuickJSWrongOwner extends Error {
|
|
1912
|
+
name: string;
|
|
1913
|
+
}
|
|
1914
|
+
declare class QuickJSUseAfterFree extends Error {
|
|
1915
|
+
name: string;
|
|
1916
|
+
}
|
|
1917
|
+
declare class QuickJSNotImplemented extends Error {
|
|
1918
|
+
name: string;
|
|
1919
|
+
}
|
|
1920
|
+
declare class QuickJSAsyncifyError extends Error {
|
|
1921
|
+
name: string;
|
|
1922
|
+
}
|
|
1923
|
+
declare class QuickJSAsyncifySuspended extends Error {
|
|
1924
|
+
name: string;
|
|
1925
|
+
}
|
|
1926
|
+
declare class QuickJSMemoryLeakDetected extends Error {
|
|
1927
|
+
name: string;
|
|
1928
|
+
}
|
|
1929
|
+
declare class QuickJSEmscriptenModuleError extends Error {
|
|
1930
|
+
name: string;
|
|
1931
|
+
}
|
|
1932
|
+
declare class QuickJSUnknownIntrinsic extends TypeError {
|
|
1933
|
+
name: string;
|
|
1934
|
+
}
|
|
1935
|
+
declare class QuickJSPromisePending extends Error {
|
|
1936
|
+
name: string;
|
|
1937
|
+
}
|
|
1938
|
+
declare class QuickJSEmptyGetOwnPropertyNames extends Error {
|
|
1939
|
+
name: string;
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
type errors_QuickJSAsyncifyError = QuickJSAsyncifyError;
|
|
1943
|
+
declare const errors_QuickJSAsyncifyError: typeof QuickJSAsyncifyError;
|
|
1944
|
+
type errors_QuickJSAsyncifySuspended = QuickJSAsyncifySuspended;
|
|
1945
|
+
declare const errors_QuickJSAsyncifySuspended: typeof QuickJSAsyncifySuspended;
|
|
1946
|
+
type errors_QuickJSEmptyGetOwnPropertyNames = QuickJSEmptyGetOwnPropertyNames;
|
|
1947
|
+
declare const errors_QuickJSEmptyGetOwnPropertyNames: typeof QuickJSEmptyGetOwnPropertyNames;
|
|
1948
|
+
type errors_QuickJSEmscriptenModuleError = QuickJSEmscriptenModuleError;
|
|
1949
|
+
declare const errors_QuickJSEmscriptenModuleError: typeof QuickJSEmscriptenModuleError;
|
|
1950
|
+
type errors_QuickJSMemoryLeakDetected = QuickJSMemoryLeakDetected;
|
|
1951
|
+
declare const errors_QuickJSMemoryLeakDetected: typeof QuickJSMemoryLeakDetected;
|
|
1952
|
+
type errors_QuickJSNotImplemented = QuickJSNotImplemented;
|
|
1953
|
+
declare const errors_QuickJSNotImplemented: typeof QuickJSNotImplemented;
|
|
1954
|
+
type errors_QuickJSPromisePending = QuickJSPromisePending;
|
|
1955
|
+
declare const errors_QuickJSPromisePending: typeof QuickJSPromisePending;
|
|
1956
|
+
type errors_QuickJSUnknownIntrinsic = QuickJSUnknownIntrinsic;
|
|
1957
|
+
declare const errors_QuickJSUnknownIntrinsic: typeof QuickJSUnknownIntrinsic;
|
|
1958
|
+
type errors_QuickJSUnwrapError = QuickJSUnwrapError;
|
|
1959
|
+
declare const errors_QuickJSUnwrapError: typeof QuickJSUnwrapError;
|
|
1960
|
+
type errors_QuickJSUseAfterFree = QuickJSUseAfterFree;
|
|
1961
|
+
declare const errors_QuickJSUseAfterFree: typeof QuickJSUseAfterFree;
|
|
1962
|
+
type errors_QuickJSWrongOwner = QuickJSWrongOwner;
|
|
1963
|
+
declare const errors_QuickJSWrongOwner: typeof QuickJSWrongOwner;
|
|
1964
|
+
declare namespace errors {
|
|
1965
|
+
export { errors_QuickJSAsyncifyError as QuickJSAsyncifyError, errors_QuickJSAsyncifySuspended as QuickJSAsyncifySuspended, errors_QuickJSEmptyGetOwnPropertyNames as QuickJSEmptyGetOwnPropertyNames, errors_QuickJSEmscriptenModuleError as QuickJSEmscriptenModuleError, errors_QuickJSMemoryLeakDetected as QuickJSMemoryLeakDetected, errors_QuickJSNotImplemented as QuickJSNotImplemented, errors_QuickJSPromisePending as QuickJSPromisePending, errors_QuickJSUnknownIntrinsic as QuickJSUnknownIntrinsic, errors_QuickJSUnwrapError as QuickJSUnwrapError, errors_QuickJSUseAfterFree as QuickJSUseAfterFree, errors_QuickJSWrongOwner as QuickJSWrongOwner };
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
/**
|
|
1969
|
+
* A test wrapper of {@link QuickJSWASMModule} that keeps a reference to each
|
|
1970
|
+
* context or runtime created.
|
|
1971
|
+
*
|
|
1972
|
+
* Call {@link disposeAll} to reset these sets and calls `dispose` on any left alive
|
|
1973
|
+
* (which may throw an error).
|
|
1974
|
+
*
|
|
1975
|
+
* Call {@link assertNoMemoryAllocated} at the end of a test, when you expect that you've
|
|
1976
|
+
* freed all the memory you've ever allocated.
|
|
1977
|
+
*/
|
|
1978
|
+
declare class TestQuickJSWASMModule implements Pick<QuickJSWASMModule, keyof QuickJSWASMModule> {
|
|
1979
|
+
private parent;
|
|
1980
|
+
contexts: Set<QuickJSContext>;
|
|
1981
|
+
runtimes: Set<QuickJSRuntime>;
|
|
1982
|
+
constructor(parent: QuickJSWASMModule);
|
|
1983
|
+
newRuntime(options?: RuntimeOptions): QuickJSRuntime;
|
|
1984
|
+
newContext(options?: ContextOptions): QuickJSContext;
|
|
1985
|
+
evalCode(code: string, options?: ModuleEvalOptions): unknown;
|
|
1986
|
+
disposeAll(): void;
|
|
1987
|
+
assertNoMemoryAllocated(): void;
|
|
1988
|
+
getWasmMemory(): WebAssembly.Memory;
|
|
1989
|
+
/** @private */
|
|
1990
|
+
getFFI(): _componentor_quickjs_ffi_types.EitherFFI;
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
/**
|
|
1994
|
+
* Enable (or disable) debug logging and object creation tracking globally.
|
|
1995
|
+
* This setting is inherited by newly created QuickJSRuntime instances.
|
|
1996
|
+
* To get debug logging in the WebAssembly module, you need to use a debug build variant.
|
|
1997
|
+
* See [the quickjs-emscripten-core README](https://github.com/justjake/quickjs-emscripten/tree/main/doc/quickjs-emscripten-core) for more about build variants.
|
|
1998
|
+
*/
|
|
1999
|
+
declare function setDebugMode(enabled?: boolean): void;
|
|
2000
|
+
/**
|
|
2001
|
+
* @private
|
|
2002
|
+
*/
|
|
2003
|
+
declare function debugLog(...args: any[]): void;
|
|
2004
|
+
|
|
2005
|
+
export { type AsyncFunctionImplementation, type AsyncRuntimeOptions, type ContextEvalOptions, type ContextOptions, type CustomizeVariantOptions, DefaultIntrinsics, type Disposable$1 as Disposable, type DisposableArray, DisposableFail, DisposableResult, DisposableSuccess, type ExecutePendingJobsResult, type InterruptHandler, type Intrinsics, type JSModuleLoadFailure, type JSModuleLoadResult, type JSModuleLoadSuccess, type JSModuleLoader, type JSModuleLoaderAsync, type JSModuleNormalizeFailure, type JSModuleNormalizeResult, type JSModuleNormalizeSuccess, type JSModuleNormalizer, type JSModuleNormalizerAsync, type JSPromiseState, type JSPromiseStateFulfilled, type JSPromiseStatePending, type JSPromiseStateRejected, type JSValue, type JSValueConst, Lifetime, type LowLevelJavascriptVm, type ModuleEvalOptions, type OrLoader, type PromiseExecutor, type PromisedDefault, QuickJSAsyncContext, QuickJSAsyncRuntime, QuickJSAsyncWASMModule, QuickJSContext, QuickJSDeferredPromise, type QuickJSHandle, type QuickJSPropertyKey, QuickJSRuntime, QuickJSWASMModule, type RuntimeOptions, type RuntimeOptionsBase, Scope, type StaticJSValue, StaticLifetime, type SuccessOrFail, TestQuickJSWASMModule, UsingDisposable, type VmCallResult, type VmFunctionImplementation, type VmPropertyDescriptor, WeakLifetime, createDisposableArray, debugLog, errors, isFail, isSuccess, memoizePromiseFactory, newQuickJSAsyncWASMModuleFromVariant, newQuickJSWASMModuleFromVariant, newVariant, setDebugMode, shouldInterruptAfterDeadline };
|