11// @ts -ignore
2- import wasmModule from " ./tolkfiftlib.js"
2+ import wasmModule from ' ./tolkfiftlib.js'
33// @ts -ignore
4- import wasmBase64 from " ./tolkfiftlib.wasm.js"
4+ import wasmBase64 from ' ./tolkfiftlib.wasm.js'
55// @ts -ignore
6- import stdlibContents from " ./stdlib.tolk.js"
7- import { realpath } from " ./path-utils"
6+ import stdlibContents from ' ./stdlib.tolk.js'
7+ import { realpath } from ' ./path-utils'
88
99let wasmBinary : Uint8Array | undefined = undefined
1010
@@ -22,16 +22,16 @@ export type TolkCompilerConfig = {
2222}
2323
2424export type TolkResultSuccess = {
25- status : "ok"
25+ status : 'ok'
2626 fiftCode : string
2727 codeBoc64 : string
2828 codeHashHex : string
2929 stderr : string
30- sourcesSnapshot : { filename : string , contents : string } [ ]
30+ sourcesSnapshot : { filename : string ; contents : string } [ ]
3131}
3232
3333export type TolkResultError = {
34- status : " error"
34+ status : ' error'
3535 message : string
3636}
3737
@@ -54,10 +54,12 @@ function copyFromCString(mod: WasmModule, inPtr: any): string {
5454
5555async function instantiateWasmModule ( ) {
5656 if ( wasmBinary === undefined ) {
57- if ( typeof Buffer !== 'undefined' ) { // node.js
57+ if ( typeof Buffer !== 'undefined' ) {
58+ // node.js
5859 wasmBinary = new Uint8Array ( Buffer . from ( wasmBase64 , 'base64' ) )
59- } else if ( typeof window !== 'undefined' ) { // browser
60- const binaryString = atob ( wasmBase64 ) // window.atob() is fast and safe for valid base64 strings
60+ } else if ( typeof window !== 'undefined' ) {
61+ // browser
62+ const binaryString = atob ( wasmBase64 ) // window.atob() is fast and safe for valid base64 strings
6163 wasmBinary = new Uint8Array ( binaryString . length )
6264 for ( let i = 0 ; i < binaryString . length ; i ++ ) {
6365 wasmBinary [ i ] = binaryString . charCodeAt ( i )
@@ -77,33 +79,41 @@ export async function getTolkCompilerVersion(): Promise<string> {
7779 return result . tolkVersion
7880}
7981
80- export async function runTolkCompiler ( compilerConfig : TolkCompilerConfig ) : Promise < TolkResultSuccess | TolkResultError > {
82+ export async function runTolkCompiler (
83+ compilerConfig : TolkCompilerConfig ,
84+ ) : Promise < TolkResultSuccess | TolkResultError > {
8185 const mod = await instantiateWasmModule ( )
8286 const allocatedPointers = [ ]
8387 const sourcesSnapshot : TolkResultSuccess [ 'sourcesSnapshot' ] = [ ]
8488
8589 // see tolk-wasm.cpp: typedef void (*WasmFsReadCallback)(int, char const*, char**, char**)
86- const callbackPtr = mod . addFunction ( function ( kind : number , dataPtr : any , destContents : any , destError : any ) {
87- switch ( kind ) { // enum ReadCallback::Kind in C++
88- case 0 : // realpath
89- let relativeFilename = copyFromCString ( mod , dataPtr ) // from `import` statement, relative to cur file
90+ const callbackPtr = mod . addFunction ( function (
91+ kind : number ,
92+ dataPtr : any ,
93+ destContents : any ,
94+ destError : any ,
95+ ) {
96+ // enum ReadCallback::Kind in C++
97+ switch ( kind ) {
98+ case 0 : // realpath
99+ let relativeFilename = copyFromCString ( mod , dataPtr ) // from `import` statement, relative to cur file
90100 if ( ! relativeFilename . endsWith ( '.tolk' ) ) {
91101 relativeFilename += '.tolk'
92102 }
93103 allocatedPointers . push ( copyToCStringPtr ( mod , realpath ( relativeFilename ) , destContents ) )
94104 break
95- case 1 : // read file
105+ case 1 : // read file
96106 try {
97107 const filename = copyFromCString ( mod , dataPtr ) // already normalized (as returned above)
98108 if ( filename . startsWith ( '@stdlib/' ) ) {
99109 if ( filename in stdlibContents ) {
100110 allocatedPointers . push ( copyToCStringPtr ( mod , stdlibContents [ filename ] , destContents ) )
101111 } else {
102- allocatedPointers . push ( copyToCStringPtr ( mod , filename + " not found" , destError ) )
112+ allocatedPointers . push ( copyToCStringPtr ( mod , filename + ' not found' , destError ) )
103113 }
104114 } else {
105115 const contents = compilerConfig . fsReadCallback ( filename )
106- sourcesSnapshot . push ( { filename, contents } )
116+ sourcesSnapshot . push ( { filename, contents} )
107117 allocatedPointers . push ( copyToCStringPtr ( mod , contents , destContents ) )
108118 }
109119 } catch ( err : any ) {
@@ -116,7 +126,8 @@ export async function runTolkCompiler(compilerConfig: TolkCompilerConfig): Promi
116126 }
117127 } , 'viiii' )
118128
119- const configStr = JSON . stringify ( { // undefined fields won't be present, defaults will be used, see tolk-wasm.cpp
129+ // undefined fields won't be present, defaults will be used, see tolk-wasm.cpp
130+ const configStr = JSON . stringify ( {
120131 entrypointFileName : compilerConfig . entrypointFileName ,
121132 optimizationLevel : compilerConfig . optimizationLevel ,
122133 withStackComments : compilerConfig . withStackComments ,
0 commit comments