1
1
import { QuickJSAsyncEmscriptenModule } from './emscripten-types'
2
2
import { QuickJSAsyncFFI } from './ffi-asyncify'
3
- import { JSContextPointer , JSModuleDefPointer , JSRuntimePointer , JSValuePointer , JSVoidPointer } from './ffi-types'
3
+ import {
4
+ JSContextPointer ,
5
+ JSModuleDefPointer ,
6
+ JSRuntimePointer ,
7
+ JSValuePointer ,
8
+ JSVoidPointer ,
9
+ } from './ffi-types'
4
10
import { Lifetime , WeakLifetime } from './lifetime'
5
11
import { Disposable , Scope } from './quickjs'
6
- import { ContextCallbacks , CToHostCallbackFunctionImplementation , CToHostModuleLoaderImplementation , QuickJSModuleCallbacks } from './quickjs-module'
12
+ import {
13
+ ContextCallbacks ,
14
+ CToHostCallbackFunctionImplementation ,
15
+ CToHostModuleLoaderImplementation ,
16
+ QuickJSModuleCallbacks ,
17
+ } from './quickjs-module'
7
18
import { PureQuickJSVm , QuickJSHandle , QuickJSPropertyKey , QuickJSVm } from './vm'
8
19
import { SuccessOrFail , VmFunctionImplementation } from './vm-interface'
9
20
@@ -38,22 +49,22 @@ function assertSync<T>(value: T | Promise<T>): T {
38
49
return value
39
50
}
40
51
41
- export type ModuleExport =
42
- | { type : 'function' , name : string , implementation : ( vm : QuickJSVm ) => VmFunctionImplementation < QuickJSHandle > }
43
- | { type : 'value' , name : string , value : ( vm : QuickJSVm ) => QuickJSHandle }
52
+ export type ModuleExport =
53
+ | {
54
+ type : 'function'
55
+ name : string
56
+ implementation : ( vm : QuickJSVm ) => VmFunctionImplementation < QuickJSHandle >
57
+ }
58
+ | { type : 'value' ; name : string ; value : ( vm : QuickJSVm ) => QuickJSHandle }
44
59
45
60
export interface ModuleDefinition {
46
61
name : string
47
62
exports : ModuleExport [ ]
48
63
}
49
64
50
- export type ModuleLoadSuccess =
51
- | string
52
- | /** TODO */ ModuleDefinition
65
+ export type ModuleLoadSuccess = string | /** TODO */ ModuleDefinition
53
66
54
- export type ModuleLoadFailure =
55
- | Error
56
- | QuickJSHandle
67
+ export type ModuleLoadFailure = Error | QuickJSHandle
57
68
58
69
export type ModuleLoadResult = SuccessOrFail < ModuleLoadSuccess , ModuleLoadFailure >
59
70
@@ -71,7 +82,10 @@ export interface RuntimeOptions {
71
82
memoryLimit ?: TODO < 'JS_SetMemoryLimit' , number >
72
83
gcThreshold ?: TODO < 'JS_SetGCThreshold' , number >
73
84
maxStackSize ?: TODO < 'JS_SetMaxStackSize' , number >
74
- sharedArrayBufferFunctions ?: TODO < 'JS_SetJSSharedArrayBufferFunctions' , { sab_alloc : TODO , sab_free : TODO , sab_dup : TODO , sab_opaque : TODO } >
85
+ sharedArrayBufferFunctions ?: TODO <
86
+ 'JS_SetJSSharedArrayBufferFunctions' ,
87
+ { sab_alloc : TODO ; sab_free : TODO ; sab_dup : TODO ; sab_opaque : TODO }
88
+ >
75
89
}
76
90
77
91
export type Intrinsic =
@@ -106,7 +120,7 @@ const DefaultIntrinsicsList = [
106
120
'Promise' ,
107
121
] as const
108
122
109
- export const DefaultIntrinsics = Symbol ( 'DefaultIntrinsics' )
123
+ export const DefaultIntrinsics = Symbol ( 'DefaultIntrinsics' )
110
124
111
125
export interface ContextOptions {
112
126
/**
@@ -119,7 +133,7 @@ export interface ContextOptions {
119
133
120
134
/**
121
135
* Create a new [QuickJSAsyncRuntime].
122
- *
136
+ *
123
137
* Each runtime is isolated in a separate WebAssembly module, so that errors in
124
138
* one runtime cannot contaminate another runtime.
125
139
*/
@@ -128,22 +142,22 @@ export async function newRuntime(options: RuntimeOptions = {}): Promise<QuickJSA
128
142
const module = await newModule ( )
129
143
const ffi = new QuickJSAsyncFFI ( module )
130
144
const callbacks = new QuickJSModuleCallbacks ( module , ffi )
131
- const rt = new Lifetime ( ffi . QTS_NewRuntime ( ) , undefined , rt_ptr => {
132
- callbacks . deleteRuntime ( rt_ptr )
133
- ffi . QTS_FreeRuntime ( rt_ptr )
134
- } )
135
- const runtime = new QuickJSAsyncRuntime ( {
136
- module,
137
- ffi,
138
- rt,
139
- callbacks,
140
- } )
141
-
142
- if ( options . moduleLoader ) {
143
- runtime . setModuleLoader ( options . moduleLoader )
144
- }
145
+ const rt = new Lifetime ( ffi . QTS_NewRuntime ( ) , undefined , rt_ptr => {
146
+ callbacks . deleteRuntime ( rt_ptr )
147
+ ffi . QTS_FreeRuntime ( rt_ptr )
148
+ } )
149
+ const runtime = new QuickJSAsyncRuntime ( {
150
+ module,
151
+ ffi,
152
+ rt,
153
+ callbacks,
154
+ } )
155
+
156
+ if ( options . moduleLoader ) {
157
+ runtime . setModuleLoader ( options . moduleLoader )
158
+ }
145
159
146
- return runtime
160
+ return runtime
147
161
}
148
162
149
163
/**
@@ -272,12 +286,17 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
272
286
this . ffi = ffi
273
287
}
274
288
275
- compileModule ( moduleName : string , source : string ) : Lifetime < JSModuleDefPointer , never , QuickJSAsyncContext > {
289
+ compileModule (
290
+ moduleName : string ,
291
+ source : string
292
+ ) : Lifetime < JSModuleDefPointer , never , QuickJSAsyncContext > {
276
293
return Scope . withScope ( scope => {
277
294
const sourcePtr = scope . manage ( this . memory . newHeapCharPointer ( source ) )
278
295
const moduleDefPtr = this . ffi . QTS_CompileModule ( this . ctx . value , moduleName , sourcePtr . value )
279
296
// uh... how do we free this?
280
- return new Lifetime ( moduleDefPtr , undefined , ptr => this . ffi . QTS_FreeVoidPointer ( this . ctx . value , ptr as JSVoidPointer ) )
297
+ return new Lifetime ( moduleDefPtr , undefined , ptr =>
298
+ this . ffi . QTS_FreeVoidPointer ( this . ctx . value , ptr as JSVoidPointer )
299
+ )
281
300
} )
282
301
}
283
302
@@ -289,7 +308,9 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
289
308
}
290
309
291
310
throw ( error : Error | QuickJSHandle ) {
292
- return this . errorToHandle ( error ) . consume ( handle => this . ffi . QTS_Throw ( this . ctx . value , handle . value ) )
311
+ return this . errorToHandle ( error ) . consume ( handle =>
312
+ this . ffi . QTS_Throw ( this . ctx . value , handle . value )
313
+ )
293
314
}
294
315
295
316
/** @private */
@@ -302,12 +323,16 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
302
323
303
324
if ( error . name !== undefined ) {
304
325
// TODO: assertSync won't work, because in DEBUG, setProp's FFI will always return `Promise`.
305
- this . newString ( error . name ) . consume ( handle => assertSync ( this . setProp ( errorHandle , 'name' , handle ) ) )
326
+ this . newString ( error . name ) . consume ( handle =>
327
+ assertSync ( this . setProp ( errorHandle , 'name' , handle ) )
328
+ )
306
329
}
307
330
308
331
if ( error . message !== undefined ) {
309
332
// TODO: assertSync won't work, because in DEBUG, setProp's FFI will always return `Promise`.
310
- this . newString ( error . message ) . consume ( handle => assertSync ( this . setProp ( errorHandle , 'message' , handle ) ) )
333
+ this . newString ( error . message ) . consume ( handle =>
334
+ assertSync ( this . setProp ( errorHandle , 'message' , handle ) )
335
+ )
311
336
}
312
337
313
338
// Disabled due to security leak concerns
@@ -343,7 +368,12 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
343
368
344
369
return Scope . withScope ( scope => {
345
370
const thisHandle = scope . manage (
346
- new WeakLifetime ( this_ptr , this . memory . copyJSValue , this . memory . freeJSValue , this . memory . owner )
371
+ new WeakLifetime (
372
+ this_ptr ,
373
+ this . memory . copyJSValue ,
374
+ this . memory . freeJSValue ,
375
+ this . memory . owner
376
+ )
347
377
)
348
378
const argHandles = new Array < QuickJSHandle > ( argc )
349
379
for ( let i = 0 ; i < argc ; i ++ ) {
0 commit comments