|
| 1 | +/* @ts-self-types="./fratm_wasm.d.ts" */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Compile FratmScript source to JavaScript |
| 5 | + * |
| 6 | + * Returns a JSON object with: |
| 7 | + * - `success`: boolean |
| 8 | + * - `code`: string (if success) |
| 9 | + * - `sourceMap`: string (if success and requested) |
| 10 | + * - `error`: string (if failure) |
| 11 | + * - `line`: number (if failure) |
| 12 | + * - `column`: number (if failure) |
| 13 | + * @param {string} source |
| 14 | + * @param {boolean} generate_source_map |
| 15 | + * @returns {any} |
| 16 | + */ |
| 17 | +export function compile(source, generate_source_map) { |
| 18 | + const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); |
| 19 | + const len0 = WASM_VECTOR_LEN; |
| 20 | + const ret = wasm.compile(ptr0, len0, generate_source_map); |
| 21 | + return ret; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Initialize panic hook for better error messages in browser console |
| 26 | + */ |
| 27 | +export function init() { |
| 28 | + wasm.init(); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Tokenize source code (for syntax highlighting) |
| 33 | + * @param {string} source |
| 34 | + * @returns {any} |
| 35 | + */ |
| 36 | +export function tokenize(source) { |
| 37 | + const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); |
| 38 | + const len0 = WASM_VECTOR_LEN; |
| 39 | + const ret = wasm.tokenize(ptr0, len0); |
| 40 | + return ret; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Get the compiler version |
| 45 | + * @returns {string} |
| 46 | + */ |
| 47 | +export function version() { |
| 48 | + let deferred1_0; |
| 49 | + let deferred1_1; |
| 50 | + try { |
| 51 | + const ret = wasm.version(); |
| 52 | + deferred1_0 = ret[0]; |
| 53 | + deferred1_1 = ret[1]; |
| 54 | + return getStringFromWasm0(ret[0], ret[1]); |
| 55 | + } finally { |
| 56 | + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function __wbg_get_imports() { |
| 61 | + const import0 = { |
| 62 | + __proto__: null, |
| 63 | + __wbg_Error_8c4e43fe74559d73: function(arg0, arg1) { |
| 64 | + const ret = Error(getStringFromWasm0(arg0, arg1)); |
| 65 | + return ret; |
| 66 | + }, |
| 67 | + __wbg_Number_04624de7d0e8332d: function(arg0) { |
| 68 | + const ret = Number(arg0); |
| 69 | + return ret; |
| 70 | + }, |
| 71 | + __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) { |
| 72 | + throw new Error(getStringFromWasm0(arg0, arg1)); |
| 73 | + }, |
| 74 | + __wbg_error_7534b8e9a36f1ab4: function(arg0, arg1) { |
| 75 | + let deferred0_0; |
| 76 | + let deferred0_1; |
| 77 | + try { |
| 78 | + deferred0_0 = arg0; |
| 79 | + deferred0_1 = arg1; |
| 80 | + console.error(getStringFromWasm0(arg0, arg1)); |
| 81 | + } finally { |
| 82 | + wasm.__wbindgen_free(deferred0_0, deferred0_1, 1); |
| 83 | + } |
| 84 | + }, |
| 85 | + __wbg_new_361308b2356cecd0: function() { |
| 86 | + const ret = new Object(); |
| 87 | + return ret; |
| 88 | + }, |
| 89 | + __wbg_new_3eb36ae241fe6f44: function() { |
| 90 | + const ret = new Array(); |
| 91 | + return ret; |
| 92 | + }, |
| 93 | + __wbg_new_8a6f238a6ece86ea: function() { |
| 94 | + const ret = new Error(); |
| 95 | + return ret; |
| 96 | + }, |
| 97 | + __wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) { |
| 98 | + arg0[arg1] = arg2; |
| 99 | + }, |
| 100 | + __wbg_set_f43e577aea94465b: function(arg0, arg1, arg2) { |
| 101 | + arg0[arg1 >>> 0] = arg2; |
| 102 | + }, |
| 103 | + __wbg_stack_0ed75d68575b0f3c: function(arg0, arg1) { |
| 104 | + const ret = arg1.stack; |
| 105 | + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); |
| 106 | + const len1 = WASM_VECTOR_LEN; |
| 107 | + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); |
| 108 | + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); |
| 109 | + }, |
| 110 | + __wbindgen_cast_0000000000000001: function(arg0) { |
| 111 | + // Cast intrinsic for `F64 -> Externref`. |
| 112 | + const ret = arg0; |
| 113 | + return ret; |
| 114 | + }, |
| 115 | + __wbindgen_cast_0000000000000002: function(arg0, arg1) { |
| 116 | + // Cast intrinsic for `Ref(String) -> Externref`. |
| 117 | + const ret = getStringFromWasm0(arg0, arg1); |
| 118 | + return ret; |
| 119 | + }, |
| 120 | + __wbindgen_cast_0000000000000003: function(arg0) { |
| 121 | + // Cast intrinsic for `U64 -> Externref`. |
| 122 | + const ret = BigInt.asUintN(64, arg0); |
| 123 | + return ret; |
| 124 | + }, |
| 125 | + __wbindgen_init_externref_table: function() { |
| 126 | + const table = wasm.__wbindgen_externrefs; |
| 127 | + const offset = table.grow(4); |
| 128 | + table.set(0, undefined); |
| 129 | + table.set(offset + 0, undefined); |
| 130 | + table.set(offset + 1, null); |
| 131 | + table.set(offset + 2, true); |
| 132 | + table.set(offset + 3, false); |
| 133 | + }, |
| 134 | + }; |
| 135 | + return { |
| 136 | + __proto__: null, |
| 137 | + "./fratm_wasm_bg.js": import0, |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +let cachedDataViewMemory0 = null; |
| 142 | +function getDataViewMemory0() { |
| 143 | + if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { |
| 144 | + cachedDataViewMemory0 = new DataView(wasm.memory.buffer); |
| 145 | + } |
| 146 | + return cachedDataViewMemory0; |
| 147 | +} |
| 148 | + |
| 149 | +function getStringFromWasm0(ptr, len) { |
| 150 | + ptr = ptr >>> 0; |
| 151 | + return decodeText(ptr, len); |
| 152 | +} |
| 153 | + |
| 154 | +let cachedUint8ArrayMemory0 = null; |
| 155 | +function getUint8ArrayMemory0() { |
| 156 | + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { |
| 157 | + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); |
| 158 | + } |
| 159 | + return cachedUint8ArrayMemory0; |
| 160 | +} |
| 161 | + |
| 162 | +function passStringToWasm0(arg, malloc, realloc) { |
| 163 | + if (realloc === undefined) { |
| 164 | + const buf = cachedTextEncoder.encode(arg); |
| 165 | + const ptr = malloc(buf.length, 1) >>> 0; |
| 166 | + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); |
| 167 | + WASM_VECTOR_LEN = buf.length; |
| 168 | + return ptr; |
| 169 | + } |
| 170 | + |
| 171 | + let len = arg.length; |
| 172 | + let ptr = malloc(len, 1) >>> 0; |
| 173 | + |
| 174 | + const mem = getUint8ArrayMemory0(); |
| 175 | + |
| 176 | + let offset = 0; |
| 177 | + |
| 178 | + for (; offset < len; offset++) { |
| 179 | + const code = arg.charCodeAt(offset); |
| 180 | + if (code > 0x7F) break; |
| 181 | + mem[ptr + offset] = code; |
| 182 | + } |
| 183 | + if (offset !== len) { |
| 184 | + if (offset !== 0) { |
| 185 | + arg = arg.slice(offset); |
| 186 | + } |
| 187 | + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; |
| 188 | + const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); |
| 189 | + const ret = cachedTextEncoder.encodeInto(arg, view); |
| 190 | + |
| 191 | + offset += ret.written; |
| 192 | + ptr = realloc(ptr, len, offset, 1) >>> 0; |
| 193 | + } |
| 194 | + |
| 195 | + WASM_VECTOR_LEN = offset; |
| 196 | + return ptr; |
| 197 | +} |
| 198 | + |
| 199 | +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); |
| 200 | +cachedTextDecoder.decode(); |
| 201 | +const MAX_SAFARI_DECODE_BYTES = 2146435072; |
| 202 | +let numBytesDecoded = 0; |
| 203 | +function decodeText(ptr, len) { |
| 204 | + numBytesDecoded += len; |
| 205 | + if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) { |
| 206 | + cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); |
| 207 | + cachedTextDecoder.decode(); |
| 208 | + numBytesDecoded = len; |
| 209 | + } |
| 210 | + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); |
| 211 | +} |
| 212 | + |
| 213 | +const cachedTextEncoder = new TextEncoder(); |
| 214 | + |
| 215 | +if (!('encodeInto' in cachedTextEncoder)) { |
| 216 | + cachedTextEncoder.encodeInto = function (arg, view) { |
| 217 | + const buf = cachedTextEncoder.encode(arg); |
| 218 | + view.set(buf); |
| 219 | + return { |
| 220 | + read: arg.length, |
| 221 | + written: buf.length |
| 222 | + }; |
| 223 | + }; |
| 224 | +} |
| 225 | + |
| 226 | +let WASM_VECTOR_LEN = 0; |
| 227 | + |
| 228 | +let wasmModule, wasm; |
| 229 | +function __wbg_finalize_init(instance, module) { |
| 230 | + wasm = instance.exports; |
| 231 | + wasmModule = module; |
| 232 | + cachedDataViewMemory0 = null; |
| 233 | + cachedUint8ArrayMemory0 = null; |
| 234 | + wasm.__wbindgen_start(); |
| 235 | + return wasm; |
| 236 | +} |
| 237 | + |
| 238 | +async function __wbg_load(module, imports) { |
| 239 | + if (typeof Response === 'function' && module instanceof Response) { |
| 240 | + if (typeof WebAssembly.instantiateStreaming === 'function') { |
| 241 | + try { |
| 242 | + return await WebAssembly.instantiateStreaming(module, imports); |
| 243 | + } catch (e) { |
| 244 | + const validResponse = module.ok && expectedResponseType(module.type); |
| 245 | + |
| 246 | + if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') { |
| 247 | + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); |
| 248 | + |
| 249 | + } else { throw e; } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + const bytes = await module.arrayBuffer(); |
| 254 | + return await WebAssembly.instantiate(bytes, imports); |
| 255 | + } else { |
| 256 | + const instance = await WebAssembly.instantiate(module, imports); |
| 257 | + |
| 258 | + if (instance instanceof WebAssembly.Instance) { |
| 259 | + return { instance, module }; |
| 260 | + } else { |
| 261 | + return instance; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + function expectedResponseType(type) { |
| 266 | + switch (type) { |
| 267 | + case 'basic': case 'cors': case 'default': return true; |
| 268 | + } |
| 269 | + return false; |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +function initSync(module) { |
| 274 | + if (wasm !== undefined) return wasm; |
| 275 | + |
| 276 | + |
| 277 | + if (module !== undefined) { |
| 278 | + if (Object.getPrototypeOf(module) === Object.prototype) { |
| 279 | + ({module} = module) |
| 280 | + } else { |
| 281 | + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + const imports = __wbg_get_imports(); |
| 286 | + if (!(module instanceof WebAssembly.Module)) { |
| 287 | + module = new WebAssembly.Module(module); |
| 288 | + } |
| 289 | + const instance = new WebAssembly.Instance(module, imports); |
| 290 | + return __wbg_finalize_init(instance, module); |
| 291 | +} |
| 292 | + |
| 293 | +async function __wbg_init(module_or_path) { |
| 294 | + if (wasm !== undefined) return wasm; |
| 295 | + |
| 296 | + |
| 297 | + if (module_or_path !== undefined) { |
| 298 | + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { |
| 299 | + ({module_or_path} = module_or_path) |
| 300 | + } else { |
| 301 | + console.warn('using deprecated parameters for the initialization function; pass a single object instead') |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + if (module_or_path === undefined) { |
| 306 | + module_or_path = new URL('fratm_wasm_bg.wasm', import.meta.url); |
| 307 | + } |
| 308 | + const imports = __wbg_get_imports(); |
| 309 | + |
| 310 | + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { |
| 311 | + module_or_path = fetch(module_or_path); |
| 312 | + } |
| 313 | + |
| 314 | + const { instance, module } = await __wbg_load(await module_or_path, imports); |
| 315 | + |
| 316 | + return __wbg_finalize_init(instance, module); |
| 317 | +} |
| 318 | + |
| 319 | +export { initSync, __wbg_init as default }; |
0 commit comments