Skip to content

Commit 10be501

Browse files
committed
fix prettier
1 parent bbcd4c8 commit 10be501

File tree

10 files changed

+118
-75
lines changed

10 files changed

+118
-75
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
ts/ffi.ts
2+
ts/ffi-asyncify.ts
3+
ts/quickjs-emscripten-module-asyncify.js
4+
ts/quickjs-emscripten-module.js
25
dist/*

.prettierrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"semi": false,
2+
"semi": false,
33
"singleQuote": true,
44
"printWidth": 100,
5-
"trailingComma": "es5"
5+
"trailingComma": "es5",
6+
"arrowParens": "avoid"
67
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@types/mocha": "^5.2.7",
4545
"@types/node": "^13.1.4",
4646
"mocha": "7.2.0",
47-
"prettier": "^1.19.1",
47+
"prettier": "2.5.1",
4848
"source-map-support": "^0.5.16",
4949
"ts-node": "^8.5.4",
5050
"typedoc": "^0.22.0",

ts/deferred-promise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Disposable } from "./lifetime"
2-
import type { QuickJSHandle, QuickJSVm } from "./quickjs"
1+
import type { Disposable } from './lifetime'
2+
import type { QuickJSHandle, QuickJSVm } from './quickjs'
33

44
/**
55
* QuickJSDeferredPromise wraps a QuickJS promise [[handle]] and allows

ts/quickjs.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe('QuickJSVm', async () => {
374374
typeofTestExample({ cow: true })
375375
typeofTestExample([1, 2, 3])
376376
typeofTestExample(
377-
function() {},
377+
function () {},
378378
(val: any) => val.toString()
379379
)
380380
})

ts/quickjs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class QuickJS {
154154
export function shouldInterruptAfterDeadline(deadline: Date | number): InterruptHandler {
155155
const deadlineAsNumber = typeof deadline === 'number' ? deadline : deadline.getTime()
156156

157-
return function() {
157+
return function () {
158158
return Date.now() > deadlineAsNumber
159159
}
160160
}

ts/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QuickJSFFI } from "./ffi";
2-
import type { QuickJSAsyncFFI } from "./ffi-asyncify";
1+
import type { QuickJSFFI } from './ffi'
2+
import type { QuickJSAsyncFFI } from './ffi-asyncify'
33

4-
export type EitherFFI = QuickJSFFI | QuickJSAsyncFFI
4+
export type EitherFFI = QuickJSFFI | QuickJSAsyncFFI

ts/vm-asyncify.ts

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import { QuickJSAsyncEmscriptenModule } from './emscripten-types'
22
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'
410
import { Lifetime, WeakLifetime } from './lifetime'
511
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'
718
import { PureQuickJSVm, QuickJSHandle, QuickJSPropertyKey, QuickJSVm } from './vm'
819
import { SuccessOrFail, VmFunctionImplementation } from './vm-interface'
920

@@ -38,22 +49,22 @@ function assertSync<T>(value: T | Promise<T>): T {
3849
return value
3950
}
4051

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 }
4459

4560
export interface ModuleDefinition {
4661
name: string
4762
exports: ModuleExport[]
4863
}
4964

50-
export type ModuleLoadSuccess =
51-
| string
52-
| /** TODO */ ModuleDefinition
65+
export type ModuleLoadSuccess = string | /** TODO */ ModuleDefinition
5366

54-
export type ModuleLoadFailure =
55-
| Error
56-
| QuickJSHandle
67+
export type ModuleLoadFailure = Error | QuickJSHandle
5768

5869
export type ModuleLoadResult = SuccessOrFail<ModuleLoadSuccess, ModuleLoadFailure>
5970

@@ -71,7 +82,10 @@ export interface RuntimeOptions {
7182
memoryLimit?: TODO<'JS_SetMemoryLimit', number>
7283
gcThreshold?: TODO<'JS_SetGCThreshold', number>
7384
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+
>
7589
}
7690

7791
export type Intrinsic =
@@ -106,7 +120,7 @@ const DefaultIntrinsicsList = [
106120
'Promise',
107121
] as const
108122

109-
export const DefaultIntrinsics = Symbol('DefaultIntrinsics')
123+
export const DefaultIntrinsics = Symbol('DefaultIntrinsics')
110124

111125
export interface ContextOptions {
112126
/**
@@ -119,7 +133,7 @@ export interface ContextOptions {
119133

120134
/**
121135
* Create a new [QuickJSAsyncRuntime].
122-
*
136+
*
123137
* Each runtime is isolated in a separate WebAssembly module, so that errors in
124138
* one runtime cannot contaminate another runtime.
125139
*/
@@ -128,22 +142,22 @@ export async function newRuntime(options: RuntimeOptions = {}): Promise<QuickJSA
128142
const module = await newModule()
129143
const ffi = new QuickJSAsyncFFI(module)
130144
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+
}
145159

146-
return runtime
160+
return runtime
147161
}
148162

149163
/**
@@ -272,12 +286,17 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
272286
this.ffi = ffi
273287
}
274288

275-
compileModule(moduleName: string, source: string): Lifetime<JSModuleDefPointer, never, QuickJSAsyncContext> {
289+
compileModule(
290+
moduleName: string,
291+
source: string
292+
): Lifetime<JSModuleDefPointer, never, QuickJSAsyncContext> {
276293
return Scope.withScope(scope => {
277294
const sourcePtr = scope.manage(this.memory.newHeapCharPointer(source))
278295
const moduleDefPtr = this.ffi.QTS_CompileModule(this.ctx.value, moduleName, sourcePtr.value)
279296
// 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+
)
281300
})
282301
}
283302

@@ -289,7 +308,9 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
289308
}
290309

291310
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+
)
293314
}
294315

295316
/** @private */
@@ -302,12 +323,16 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
302323

303324
if (error.name !== undefined) {
304325
// 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+
)
306329
}
307330

308331
if (error.message !== undefined) {
309332
// 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+
)
311336
}
312337

313338
// Disabled due to security leak concerns
@@ -343,7 +368,12 @@ export class QuickJSAsyncContext extends PureQuickJSVm implements ContextCallbac
343368

344369
return Scope.withScope(scope => {
345370
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+
)
347377
)
348378
const argHandles = new Array<QuickJSHandle>(argc)
349379
for (let i = 0; i < argc; i++) {

ts/vm.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
HeapCharPointer,
1313
} from './ffi-types'
1414
import { Disposable, Lifetime, Scope, StaticLifetime, WeakLifetime } from './lifetime'
15-
import { CToHostCallbackFunctionImplementation, CToHostInterruptImplementation } from './quickjs-module'
15+
import {
16+
CToHostCallbackFunctionImplementation,
17+
CToHostInterruptImplementation,
18+
} from './quickjs-module'
1619
import {
1720
SuccessOrFail,
1821
LowLevelJavascriptVm,
@@ -65,8 +68,6 @@ export type JSValue = Lifetime<JSValuePointer, JSValuePointer, QuickJSVm>
6568
*/
6669
export type QuickJSHandle = StaticJSValue | JSValue | JSValueConst
6770

68-
69-
7071
/**
7172
* Callback called regularly while the VM executes code.
7273
* Determines if a VM's execution should be interrupted.
@@ -195,10 +196,10 @@ class QuickJSVmMemory implements Disposable {
195196
}
196197
}
197198

198-
/**
199+
/**
199200
* Implements methods that are always synchronous - such methods should never
200201
* call into a function that suspends when built with ASYNCIFY.
201-
*
202+
*
202203
* @private
203204
*/
204205
export class PureQuickJSVm implements Disposable {
@@ -337,7 +338,7 @@ export class PureQuickJSVm implements Disposable {
337338
// disposed, no other functions will accept the value.
338339
this._global = new StaticLifetime(ptr, this.owner)
339340
return this._global
340-
}
341+
}
341342

342343
// New values ---------------------------------------------------------------
343344

@@ -352,9 +353,9 @@ export class PureQuickJSVm implements Disposable {
352353
* Create a QuickJS [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) value.
353354
*/
354355
newString(str: string): QuickJSHandle {
355-
const ptr = this.memory.newHeapCharPointer(str).consume(charHandle =>
356-
this.ffi.QTS_NewString(this.ctx.value, charHandle.value)
357-
)
356+
const ptr = this.memory
357+
.newHeapCharPointer(str)
358+
.consume(charHandle => this.ffi.QTS_NewString(this.ctx.value, charHandle.value))
358359
return this.memory.heapValueHandle(ptr)
359360
}
360361

@@ -397,9 +398,9 @@ export class PureQuickJSVm implements Disposable {
397398
mutablePointerArray.value.ptr
398399
)
399400
const promiseHandle = this.memory.heapValueHandle(promisePtr)
400-
const [resolveHandle, rejectHandle] = Array.from(
401-
mutablePointerArray.value.typedArray
402-
).map(jsvaluePtr => this.memory.heapValueHandle(jsvaluePtr as any))
401+
const [resolveHandle, rejectHandle] = Array.from(mutablePointerArray.value.typedArray).map(
402+
jsvaluePtr => this.memory.heapValueHandle(jsvaluePtr as any)
403+
)
403404
return new QuickJSDeferredPromise({
404405
owner: this.owner,
405406
promiseHandle,
@@ -421,7 +422,6 @@ export class PureQuickJSVm implements Disposable {
421422
return Boolean(this.ffi.QTS_IsJobPending(this.rt.value))
422423
}
423424

424-
425425
private interruptHandler: InterruptHandler | undefined
426426

427427
/**
@@ -548,9 +548,12 @@ export class PureQuickJSVm implements Disposable {
548548
* Use [[computeMemoryUsage]] or [[dumpMemoryUsage]] to guide memory limit
549549
* tuning.
550550
*/
551-
export class QuickJSVm extends PureQuickJSVm implements LowLevelJavascriptVm<QuickJSHandle>, Disposable {
551+
export class QuickJSVm
552+
extends PureQuickJSVm
553+
implements LowLevelJavascriptVm<QuickJSHandle>, Disposable
554+
{
552555
/** @private */
553-
readonly module: QuickJSEmscriptenModule
556+
readonly module: QuickJSEmscriptenModule
554557
/** @private */
555558
protected readonly ffi: QuickJSFFI
556559

@@ -566,7 +569,7 @@ export class QuickJSVm extends PureQuickJSVm implements LowLevelJavascriptVm<Qui
566569
super(args)
567570
this.module = args.module
568571
this.ffi = args.ffi
569-
}
572+
}
570573

571574
// New values ---------------------------------------------------------------
572575

@@ -604,7 +607,6 @@ export class QuickJSVm extends PureQuickJSVm implements LowLevelJavascriptVm<Qui
604607
return funcHandle
605608
}
606609

607-
608610
// Read values --------------------------------------------------------------
609611

610612
/**
@@ -618,7 +620,6 @@ export class QuickJSVm extends PureQuickJSVm implements LowLevelJavascriptVm<Qui
618620
return this.ffi.QTS_Typeof(this.ctx.value, handle.value)
619621
}
620622

621-
622623
/**
623624
* Converts `handle` into a Javascript number.
624625
* @returns `NaN` on error, otherwise a `number`.
@@ -780,9 +781,17 @@ export class QuickJSVm extends PureQuickJSVm implements LowLevelJavascriptVm<Qui
780781
...args: QuickJSHandle[]
781782
): VmCallResult<QuickJSHandle> {
782783
this.memory.assertOwned(func)
783-
const resultPtr = this.memory.toPointerArray(args).consume(argsArrayPtr =>
784-
this.ffi.QTS_Call(this.ctx.value, func.value, thisVal.value, args.length, argsArrayPtr.value)
785-
)
784+
const resultPtr = this.memory
785+
.toPointerArray(args)
786+
.consume(argsArrayPtr =>
787+
this.ffi.QTS_Call(
788+
this.ctx.value,
789+
func.value,
790+
thisVal.value,
791+
args.length,
792+
argsArrayPtr.value
793+
)
794+
)
786795

787796
const errorPtr = this.ffi.QTS_ResolveException(this.ctx.value, resultPtr)
788797
if (errorPtr) {
@@ -814,9 +823,9 @@ export class QuickJSVm extends PureQuickJSVm implements LowLevelJavascriptVm<Qui
814823
* `interrupted`.
815824
*/
816825
evalCode(code: string, filename: string = 'eval.js'): VmCallResult<QuickJSHandle> {
817-
const resultPtr = this.memory.newHeapCharPointer(code).consume(charHandle =>
818-
this.ffi.QTS_Eval(this.ctx.value, charHandle.value, filename)
819-
)
826+
const resultPtr = this.memory
827+
.newHeapCharPointer(code)
828+
.consume(charHandle => this.ffi.QTS_Eval(this.ctx.value, charHandle.value, filename))
820829
const errorPtr = this.ffi.QTS_ResolveException(this.ctx.value, resultPtr)
821830
if (errorPtr) {
822831
this.ffi.QTS_FreeValuePointer(this.ctx.value, resultPtr)

0 commit comments

Comments
 (0)