|
| 1 | +// @ts-ignore |
| 2 | +import wasmModule from "./tolkfiftlib.js" |
| 3 | +// @ts-ignore |
| 4 | +import wasmBase64 from "./tolkfiftlib.wasm.js" |
| 5 | +// @ts-ignore |
| 6 | +import stdlibContents from "./stdlib.tolk.js" |
| 7 | +import {realpath} from "./path-utils" |
| 8 | + |
| 9 | +let wasmBinary: Uint8Array | undefined = undefined |
| 10 | + |
| 11 | +type WasmModule = any |
| 12 | + |
| 13 | +export type FsReadCallback = (path: string) => string |
| 14 | + |
| 15 | +export type TolkCompilerConfig = { |
| 16 | + entrypointFileName: string |
| 17 | + fsReadCallback: FsReadCallback |
| 18 | + optimizationLevel?: number |
| 19 | + withStackComments?: boolean |
| 20 | + experimentalOptions?: string |
| 21 | +} |
| 22 | + |
| 23 | +export type TolkResultSuccess = { |
| 24 | + status: "ok" |
| 25 | + fiftCode: string |
| 26 | + codeBoc64: string |
| 27 | + codeHashHex: string |
| 28 | + stderr: string |
| 29 | + sourcesSnapshot: { filename: string, contents: string }[] |
| 30 | +} |
| 31 | + |
| 32 | +export type TolkResultError = { |
| 33 | + status: "error" |
| 34 | + message: string |
| 35 | +} |
| 36 | + |
| 37 | +function copyToCStringAllocating(mod: WasmModule, inStr: string): any { |
| 38 | + const len = mod.lengthBytesUTF8(inStr) + 1 |
| 39 | + const ptr = mod._malloc(len) |
| 40 | + mod.stringToUTF8(inStr, ptr, len) |
| 41 | + return ptr |
| 42 | +} |
| 43 | + |
| 44 | +function copyToCStringPtr(mod: WasmModule, inStr: string, destPtr: any): any { |
| 45 | + const allocated = copyToCStringAllocating(mod, inStr) |
| 46 | + mod.setValue(destPtr, allocated, '*') |
| 47 | + return allocated |
| 48 | +} |
| 49 | + |
| 50 | +function copyFromCString(mod: WasmModule, inPtr: any): string { |
| 51 | + return mod.UTF8ToString(inPtr) |
| 52 | +} |
| 53 | + |
| 54 | +async function instantiateWasmModule() { |
| 55 | + if (wasmBinary === undefined) { |
| 56 | + if (typeof Buffer !== 'undefined') { // node.js |
| 57 | + wasmBinary = new Uint8Array(Buffer.from(wasmBase64, 'base64')) |
| 58 | + } else if (typeof window !== 'undefined') { // browser |
| 59 | + const binaryString = atob(wasmBase64) // window.atob() is fast and safe for valid base64 strings |
| 60 | + wasmBinary = new Uint8Array(binaryString.length) |
| 61 | + for (let i = 0; i < binaryString.length; i++) { |
| 62 | + wasmBinary[i] = binaryString.charCodeAt(i) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return await wasmModule({wasmBinary}) |
| 67 | +} |
| 68 | + |
| 69 | +export async function getTolkCompilerVersion(): Promise<string> { |
| 70 | + const mod = await instantiateWasmModule() |
| 71 | + |
| 72 | + const versionJsonPtr = mod._version() |
| 73 | + const result = JSON.parse(copyFromCString(mod, versionJsonPtr)) |
| 74 | + mod._free(versionJsonPtr) |
| 75 | + |
| 76 | + return result.tolkVersion |
| 77 | +} |
| 78 | + |
| 79 | +export async function runTolkCompiler(compilerConfig: TolkCompilerConfig): Promise<TolkResultSuccess | TolkResultError> { |
| 80 | + const mod = await instantiateWasmModule() |
| 81 | + const allocatedPointers = [] |
| 82 | + const sourcesSnapshot: TolkResultSuccess['sourcesSnapshot'] = [] |
| 83 | + |
| 84 | + // see tolk-wasm.cpp: typedef void (*WasmFsReadCallback)(int, char const*, char**, char**) |
| 85 | + const callbackPtr = mod.addFunction(function (kind: number, dataPtr: any, destContents: any, destError: any) { |
| 86 | + switch (kind) { // enum ReadCallback::Kind in C++ |
| 87 | + case 0: // realpath |
| 88 | + const relativeFilename = copyFromCString(mod, dataPtr) |
| 89 | + allocatedPointers.push(copyToCStringPtr(mod, realpath(relativeFilename), destContents)) |
| 90 | + break |
| 91 | + case 1: // read file |
| 92 | + try { |
| 93 | + const filename = copyFromCString(mod, dataPtr) // already normalized (as returned above) |
| 94 | + if (filename.startsWith('@stdlib/')) { |
| 95 | + const stdlibKey = filename.endsWith('.tolk') ? filename : filename + '.tolk' |
| 96 | + if (!(stdlibKey in stdlibContents)) { |
| 97 | + allocatedPointers.push(copyToCStringPtr(mod, stdlibKey + " not found", destError)) |
| 98 | + } else { |
| 99 | + allocatedPointers.push(copyToCStringPtr(mod, stdlibContents[stdlibKey], destContents)) |
| 100 | + } |
| 101 | + } else { |
| 102 | + const contents = compilerConfig.fsReadCallback(filename) |
| 103 | + sourcesSnapshot.push({ filename, contents }) |
| 104 | + allocatedPointers.push(copyToCStringPtr(mod, contents, destContents)) |
| 105 | + } |
| 106 | + } catch (err: any) { |
| 107 | + allocatedPointers.push(copyToCStringPtr(mod, err.message || err.toString(), destError)) |
| 108 | + } |
| 109 | + break |
| 110 | + default: |
| 111 | + allocatedPointers.push(copyToCStringPtr(mod, 'Unknown callback kind=' + kind, destError)) |
| 112 | + break |
| 113 | + } |
| 114 | + }, 'viiii') |
| 115 | + |
| 116 | + const configStr = JSON.stringify({ // undefined fields won't be present, defaults will be used, see tolk-wasm.cpp |
| 117 | + entrypointFileName: compilerConfig.entrypointFileName, |
| 118 | + optimizationLevel: compilerConfig.optimizationLevel, |
| 119 | + withStackComments: compilerConfig.withStackComments, |
| 120 | + experimentalOptions: compilerConfig.experimentalOptions, |
| 121 | + }) |
| 122 | + |
| 123 | + const configStrPtr = copyToCStringAllocating(mod, configStr) |
| 124 | + allocatedPointers.push(configStrPtr) |
| 125 | + |
| 126 | + const resultPtr = mod._tolk_compile(configStrPtr, callbackPtr) |
| 127 | + allocatedPointers.push(resultPtr) |
| 128 | + const result: TolkResultSuccess | TolkResultError = JSON.parse(copyFromCString(mod, resultPtr)) |
| 129 | + |
| 130 | + allocatedPointers.forEach(ptr => mod._free(ptr)) |
| 131 | + mod.removeFunction(callbackPtr) |
| 132 | + |
| 133 | + return result.status === 'error' ? result : {...result, sourcesSnapshot} |
| 134 | +} |
0 commit comments