-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathruntime.ts
473 lines (420 loc) · 15.1 KB
/
runtime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import type {
BorrowedHeapCharPointer,
JSContextPointer,
JSContextPointerPointer,
JSRuntimePointer,
EitherFFI,
EitherModule,
} from "@jitl/quickjs-ffi-types"
import { maybeAsyncFn } from "./asyncify-helpers"
import { QuickJSContext } from "./context"
import { QTS_DEBUG } from "./debug"
import { QuickJSWrongOwner } from "./errors"
import type { Disposable } from "./lifetime"
import { DisposableResult, Lifetime, Scope, UsingDisposable } from "./lifetime"
import { ModuleMemory } from "./memory"
import type { QuickJSModuleCallbacks, RuntimeCallbacks } from "./module"
import type { ContextOptions, JSModuleLoader, JSModuleNormalizer, QuickJSHandle } from "./types"
import { intrinsicsToFlags } from "./types"
/**
* Callback called regularly while the VM executes code.
* Determines if a VM's execution should be interrupted.
*
* @returns `true` to interrupt JS execution inside the VM.
* @returns `false` or `undefined` to continue JS execution inside the VM.
*/
export type InterruptHandler = (runtime: QuickJSRuntime) => boolean | undefined | void
/**
* Used as an optional for the results of executing pendingJobs.
* On success, `value` contains the number of async jobs executed
* by the runtime.
* @source
*/
export type ExecutePendingJobsResult = DisposableResult<
/** Number of jobs successfully executed. */
number,
/** The error that occurred. */
QuickJSHandle & {
/** The context where the error occurred. */
context: QuickJSContext
}
>
/**
* A runtime represents a Javascript runtime corresponding to an object heap.
* Several runtimes can exist at the same time but they cannot exchange objects.
* Inside a given runtime, no multi-threading is supported.
*
* You can think of separate runtimes like different domains in a browser, and
* the contexts within a runtime like the different windows open to the same
* domain.
*
* Create a runtime via {@link QuickJSWASMModule.newRuntime}.
*
* You should create separate runtime instances for untrusted code from
* different sources for isolation. However, stronger isolation is also
* available (at the cost of memory usage), by creating separate WebAssembly
* modules to further isolate untrusted code.
* See {@link newQuickJSWASMModule}.
*
* Implement memory and CPU constraints with {@link setInterruptHandler}
* (called regularly while the interpreter runs), {@link setMemoryLimit}, and
* {@link setMaxStackSize}.
* Use {@link computeMemoryUsage} or {@link dumpMemoryUsage} to guide memory limit
* tuning.
*
* Configure ES module loading with {@link setModuleLoader}.
*/
export class QuickJSRuntime extends UsingDisposable implements Disposable {
/**
* If this runtime was created as as part of a context, points to the context
* associated with the runtime.
*
* If this runtime was created stand-alone, this may or may not contain a context.
* A context here may be allocated if one is needed by the runtime, eg for {@link computeMemoryUsage}.
*/
public context: QuickJSContext | undefined
/** @private */
protected module: EitherModule
/** @private */
protected memory: ModuleMemory
/** @private */
protected ffi: EitherFFI
/** @private */
protected rt: Lifetime<JSRuntimePointer>
/** @private */
protected callbacks: QuickJSModuleCallbacks
/** @private */
protected scope = new Scope()
/** @private */
protected contextMap = new Map<JSContextPointer, QuickJSContext>()
/** @private */
protected moduleLoader: JSModuleLoader | undefined
/** @private */
protected moduleNormalizer: JSModuleNormalizer | undefined
/** @private */
constructor(args: {
module: EitherModule
ffi: EitherFFI
rt: Lifetime<JSRuntimePointer>
callbacks: QuickJSModuleCallbacks
ownedLifetimes?: Disposable[]
}) {
super()
args.ownedLifetimes?.forEach((lifetime) => this.scope.manage(lifetime))
this.module = args.module
this.memory = new ModuleMemory(this.module)
this.ffi = args.ffi
this.rt = args.rt
this.callbacks = args.callbacks
this.scope.manage(this.rt)
this.callbacks.setRuntimeCallbacks(this.rt.value, this.cToHostCallbacks)
this.executePendingJobs = this.executePendingJobs.bind(this)
if (QTS_DEBUG) {
this.setDebugMode(true)
}
}
get alive() {
return this.scope.alive
}
dispose() {
return this.scope.dispose()
}
/**
* Create a new context within this runtime. Contexts have isolated globals,
* but you can explicitly share objects between contexts with the same
* runtime.
*
* You should dispose a created context before disposing this runtime.
*/
newContext(options: ContextOptions = {}): QuickJSContext {
const intrinsics = intrinsicsToFlags(options.intrinsics)
const ctx = new Lifetime(
options.contextPointer || this.ffi.QTS_NewContext(this.rt.value, intrinsics),
undefined,
(ctx_ptr) => {
this.contextMap.delete(ctx_ptr)
this.callbacks.deleteContext(ctx_ptr)
this.ffi.QTS_FreeContext(ctx_ptr)
},
)
const context = new QuickJSContext({
module: this.module,
ctx,
ffi: this.ffi,
rt: this.rt,
ownedLifetimes: options.ownedLifetimes,
runtime: this,
callbacks: this.callbacks,
})
this.contextMap.set(ctx.value, context)
return context
}
/**
* Set the loader for EcmaScript modules requested by any context in this
* runtime.
*
* The loader can be removed with {@link removeModuleLoader}.
*/
setModuleLoader(moduleLoader: JSModuleLoader, moduleNormalizer?: JSModuleNormalizer): void {
this.moduleLoader = moduleLoader
this.moduleNormalizer = moduleNormalizer
this.ffi.QTS_RuntimeEnableModuleLoader(this.rt.value, this.moduleNormalizer ? 1 : 0)
}
/**
* Remove the the loader set by {@link setModuleLoader}. This disables module loading.
*/
removeModuleLoader(): void {
this.moduleLoader = undefined
this.ffi.QTS_RuntimeDisableModuleLoader(this.rt.value)
}
// Runtime management -------------------------------------------------------
/**
* In QuickJS, promises and async functions create pendingJobs. These do not execute
* immediately and need to be run by calling {@link executePendingJobs}.
*
* @return true if there is at least one pendingJob queued up.
*/
hasPendingJob(): boolean {
return Boolean(this.ffi.QTS_IsJobPending(this.rt.value))
}
private interruptHandler: InterruptHandler | undefined
/**
* Set a callback which is regularly called by the QuickJS engine when it is
* executing code. This callback can be used to implement an execution
* timeout.
*
* The interrupt handler can be removed with {@link removeInterruptHandler}.
*/
setInterruptHandler(cb: InterruptHandler) {
const prevInterruptHandler = this.interruptHandler
this.interruptHandler = cb
if (!prevInterruptHandler) {
this.ffi.QTS_RuntimeEnableInterruptHandler(this.rt.value)
}
}
/**
* Remove the interrupt handler, if any.
* See {@link setInterruptHandler}.
*/
removeInterruptHandler() {
if (this.interruptHandler) {
this.ffi.QTS_RuntimeDisableInterruptHandler(this.rt.value)
this.interruptHandler = undefined
}
}
/**
* Execute pendingJobs on the runtime until `maxJobsToExecute` jobs are
* executed (default all pendingJobs), the queue is exhausted, or the runtime
* encounters an exception.
*
* In QuickJS, promises and async functions *inside the runtime* create
* pendingJobs. These do not execute immediately and need to triggered to run.
*
* @param maxJobsToExecute - When negative, run all pending jobs. Otherwise execute
* at most `maxJobsToExecute` before returning.
*
* @return On success, the number of executed jobs. On error, the exception
* that stopped execution, and the context it occurred in. Note that
* executePendingJobs will not normally return errors thrown inside async
* functions or rejected promises. Those errors are available by calling
* {@link QuickJSContext#resolvePromise} on the promise handle returned by the async function.
*/
executePendingJobs(maxJobsToExecute: number | void = -1): ExecutePendingJobsResult {
const ctxPtrOut = this.memory.newMutablePointerArray<JSContextPointerPointer>(1)
const valuePtr = this.ffi.QTS_ExecutePendingJob(
this.rt.value,
maxJobsToExecute ?? -1,
ctxPtrOut.value.ptr,
)
const ctxPtr = ctxPtrOut.value.typedArray[0] as JSContextPointer
ctxPtrOut.dispose()
if (ctxPtr === 0) {
// No jobs executed.
this.ffi.QTS_FreeValuePointerRuntime(this.rt.value, valuePtr)
return DisposableResult.success(0)
}
const context =
this.contextMap.get(ctxPtr) ??
this.newContext({
contextPointer: ctxPtr,
})
const resultValue = context.getMemory(this.rt.value).heapValueHandle(valuePtr)
const typeOfRet = context.typeof(resultValue)
if (typeOfRet === "number") {
const executedJobs = context.getNumber(resultValue)
resultValue.dispose()
return DisposableResult.success(executedJobs)
} else {
const error = Object.assign(resultValue as QuickJSHandle, { context })
return DisposableResult.fail(error, (error) => context.unwrapResult(error))
}
}
/**
* Set the max memory this runtime can allocate.
* To remove the limit, set to `-1`.
*/
setMemoryLimit(limitBytes: number) {
if (limitBytes < 0 && limitBytes !== -1) {
throw new Error("Cannot set memory limit to negative number. To unset, pass -1")
}
this.ffi.QTS_RuntimeSetMemoryLimit(this.rt.value, limitBytes)
}
/**
* Compute memory usage for this runtime. Returns the result as a handle to a
* JSValue object. Use {@link QuickJSContext#dump} to convert to a native object.
* Calling this method will allocate more memory inside the runtime. The information
* is accurate as of just before the call to `computeMemoryUsage`.
* For a human-digestible representation, see {@link dumpMemoryUsage}.
*/
computeMemoryUsage(): QuickJSHandle {
const serviceContextMemory = this.getSystemContext().getMemory(this.rt.value)
return serviceContextMemory.heapValueHandle(
this.ffi.QTS_RuntimeComputeMemoryUsage(this.rt.value, serviceContextMemory.ctx.value),
)
}
/**
* @returns a human-readable description of memory usage in this runtime.
* For programmatic access to this information, see {@link computeMemoryUsage}.
*/
dumpMemoryUsage(): string {
return this.memory.consumeHeapCharPointer(this.ffi.QTS_RuntimeDumpMemoryUsage(this.rt.value))
}
/**
* Set the max stack size for this runtime, in bytes.
* To remove the limit, set to `0`.
*/
setMaxStackSize(stackSize: number) {
if (stackSize < 0) {
throw new Error("Cannot set memory limit to negative number. To unset, pass 0.")
}
this.ffi.QTS_RuntimeSetMaxStackSize(this.rt.value, stackSize)
}
/**
* Assert that `handle` is owned by this runtime.
* @throws QuickJSWrongOwner if owned by a different runtime.
*/
assertOwned(handle: QuickJSHandle) {
if (handle.owner && handle.owner.rt !== this.rt) {
throw new QuickJSWrongOwner(
`Handle is not owned by this runtime: ${handle.owner.rt.value} != ${this.rt.value}`,
)
}
}
private _debugMode = false
/**
* Enable or disable debug logging.
*
* If this module is a DEBUG variant, more logs will be printed from the C
* code.
*/
setDebugMode(enabled: boolean) {
this._debugMode = enabled
if (this.ffi.DEBUG && this.rt.alive) {
this.ffi.QTS_SetDebugLogEnabled(this.rt.value, enabled ? 1 : 0)
}
}
/**
* @returns true if debug logging is enabled
*/
isDebugMode(): boolean {
return this._debugMode
}
/**
* In debug mode, log the result of calling `msg()`.
*
* We take a function instead of a log message to avoid expensive string
* manipulation if debug logging is disabled.
*/
debugLog(...msg: unknown[]) {
if (this._debugMode) {
console.log("quickjs-emscripten:", ...msg)
}
}
/** @private */
[Symbol.for("nodejs.util.inspect.custom")]() {
if (!this.alive) {
return `${this.constructor.name} { disposed }`
}
return `${this.constructor.name} { rt: ${this.rt.value} }`
}
private getSystemContext() {
if (!this.context) {
// We own this context and should dispose of it.
this.context = this.scope.manage(this.newContext())
}
return this.context
}
private cToHostCallbacks: RuntimeCallbacks = {
shouldInterrupt: (rt) => {
if (rt !== this.rt.value) {
throw new Error("QuickJSContext instance received C -> JS interrupt with mismatched rt")
}
const fn = this.interruptHandler
if (!fn) {
throw new Error("QuickJSContext had no interrupt handler")
}
return fn(this) ? 1 : 0
},
loadModuleSource: maybeAsyncFn(this, function* (awaited, rt, ctx, moduleName) {
const moduleLoader = this.moduleLoader
if (!moduleLoader) {
throw new Error("Runtime has no module loader")
}
if (rt !== this.rt.value) {
throw new Error("Runtime pointer mismatch")
}
const context =
this.contextMap.get(ctx) ??
this.newContext({
contextPointer: ctx,
})
try {
const result = yield* awaited(moduleLoader(moduleName, context))
if (typeof result === "object" && "error" in result && result.error) {
this.debugLog("cToHostLoadModule: loader returned error", result.error)
throw result.error
}
const moduleSource =
typeof result === "string" ? result : "value" in result ? result.value : result
return this.memory.newHeapCharPointer(moduleSource).value.ptr
} catch (error) {
this.debugLog("cToHostLoadModule: caught error", error)
context.throw(error as any)
return 0 as BorrowedHeapCharPointer
}
}),
normalizeModule: maybeAsyncFn(
this,
function* (awaited, rt, ctx, baseModuleName, moduleNameRequest) {
const moduleNormalizer = this.moduleNormalizer
if (!moduleNormalizer) {
throw new Error("Runtime has no module normalizer")
}
if (rt !== this.rt.value) {
throw new Error("Runtime pointer mismatch")
}
const context: QuickJSContext =
this.contextMap.get(ctx) ??
this.newContext({
/* TODO: Does this happen? Are we responsible for disposing? I don't think so */
contextPointer: ctx,
})
try {
const result = yield* awaited(
moduleNormalizer(baseModuleName, moduleNameRequest, context),
)
if (typeof result === "object" && "error" in result && result.error) {
this.debugLog("cToHostNormalizeModule: normalizer returned error", result.error)
throw result.error
}
const name = typeof result === "string" ? result : result.value
return context.getMemory(this.rt.value).newHeapCharPointer(name).value.ptr
} catch (error) {
this.debugLog("normalizeModule: caught error", error)
context.throw(error as any)
return 0 as BorrowedHeapCharPointer
}
},
),
}
}