automaton.js (14615B)
1 let wasm; 2 3 const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); 4 5 if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; 6 7 let cachedUint8Memory0 = null; 8 9 function getUint8Memory0() { 10 if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { 11 cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); 12 } 13 return cachedUint8Memory0; 14 } 15 16 function getStringFromWasm0(ptr, len) { 17 ptr = ptr >>> 0; 18 return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); 19 } 20 21 const heap = new Array(128).fill(undefined); 22 23 heap.push(undefined, null, true, false); 24 25 let heap_next = heap.length; 26 27 function addHeapObject(obj) { 28 if (heap_next === heap.length) heap.push(heap.length + 1); 29 const idx = heap_next; 30 heap_next = heap[idx]; 31 32 heap[idx] = obj; 33 return idx; 34 } 35 36 let cachedInt32Memory0 = null; 37 38 function getInt32Memory0() { 39 if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { 40 cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); 41 } 42 return cachedInt32Memory0; 43 } 44 45 let WASM_VECTOR_LEN = 0; 46 47 const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); 48 49 const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' 50 ? function (arg, view) { 51 return cachedTextEncoder.encodeInto(arg, view); 52 } 53 : function (arg, view) { 54 const buf = cachedTextEncoder.encode(arg); 55 view.set(buf); 56 return { 57 read: arg.length, 58 written: buf.length 59 }; 60 }); 61 62 function passStringToWasm0(arg, malloc, realloc) { 63 64 if (realloc === undefined) { 65 const buf = cachedTextEncoder.encode(arg); 66 const ptr = malloc(buf.length, 1) >>> 0; 67 getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); 68 WASM_VECTOR_LEN = buf.length; 69 return ptr; 70 } 71 72 let len = arg.length; 73 let ptr = malloc(len, 1) >>> 0; 74 75 const mem = getUint8Memory0(); 76 77 let offset = 0; 78 79 for (; offset < len; offset++) { 80 const code = arg.charCodeAt(offset); 81 if (code > 0x7F) break; 82 mem[ptr + offset] = code; 83 } 84 85 if (offset !== len) { 86 if (offset !== 0) { 87 arg = arg.slice(offset); 88 } 89 ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; 90 const view = getUint8Memory0().subarray(ptr + offset, ptr + len); 91 const ret = encodeString(arg, view); 92 93 offset += ret.written; 94 ptr = realloc(ptr, len, offset, 1) >>> 0; 95 } 96 97 WASM_VECTOR_LEN = offset; 98 return ptr; 99 } 100 101 function getObject(idx) { return heap[idx]; } 102 103 function dropObject(idx) { 104 if (idx < 132) return; 105 heap[idx] = heap_next; 106 heap_next = idx; 107 } 108 109 function takeObject(idx) { 110 const ret = getObject(idx); 111 dropObject(idx); 112 return ret; 113 } 114 115 const WebAutomatonErrorFinalization = (typeof FinalizationRegistry === 'undefined') 116 ? { register: () => {}, unregister: () => {} } 117 : new FinalizationRegistry(ptr => wasm.__wbg_webautomatonerror_free(ptr >>> 0)); 118 /** 119 */ 120 export class WebAutomatonError { 121 122 static __wrap(ptr) { 123 ptr = ptr >>> 0; 124 const obj = Object.create(WebAutomatonError.prototype); 125 obj.__wbg_ptr = ptr; 126 WebAutomatonErrorFinalization.register(obj, obj.__wbg_ptr, obj); 127 return obj; 128 } 129 130 __destroy_into_raw() { 131 const ptr = this.__wbg_ptr; 132 this.__wbg_ptr = 0; 133 WebAutomatonErrorFinalization.unregister(this); 134 return ptr; 135 } 136 137 free() { 138 const ptr = this.__destroy_into_raw(); 139 wasm.__wbg_webautomatonerror_free(ptr); 140 } 141 /** 142 * @returns {string} 143 */ 144 to_string() { 145 let deferred1_0; 146 let deferred1_1; 147 try { 148 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 149 wasm.webautomatonerror_to_string(retptr, this.__wbg_ptr); 150 var r0 = getInt32Memory0()[retptr / 4 + 0]; 151 var r1 = getInt32Memory0()[retptr / 4 + 1]; 152 deferred1_0 = r0; 153 deferred1_1 = r1; 154 return getStringFromWasm0(r0, r1); 155 } finally { 156 wasm.__wbindgen_add_to_stack_pointer(16); 157 wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); 158 } 159 } 160 } 161 162 const WebRegexFinalization = (typeof FinalizationRegistry === 'undefined') 163 ? { register: () => {}, unregister: () => {} } 164 : new FinalizationRegistry(ptr => wasm.__wbg_webregex_free(ptr >>> 0)); 165 /** 166 */ 167 export class WebRegex { 168 169 __destroy_into_raw() { 170 const ptr = this.__wbg_ptr; 171 this.__wbg_ptr = 0; 172 WebRegexFinalization.unregister(this); 173 return ptr; 174 } 175 176 free() { 177 const ptr = this.__destroy_into_raw(); 178 wasm.__wbg_webregex_free(ptr); 179 } 180 /** 181 * @param {string} regex 182 */ 183 constructor(regex) { 184 try { 185 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 186 const ptr0 = passStringToWasm0(regex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); 187 const len0 = WASM_VECTOR_LEN; 188 wasm.webregex_new(retptr, ptr0, len0); 189 var r0 = getInt32Memory0()[retptr / 4 + 0]; 190 var r1 = getInt32Memory0()[retptr / 4 + 1]; 191 var r2 = getInt32Memory0()[retptr / 4 + 2]; 192 if (r2) { 193 throw takeObject(r1); 194 } 195 this.__wbg_ptr = r0 >>> 0; 196 return this; 197 } finally { 198 wasm.__wbindgen_add_to_stack_pointer(16); 199 } 200 } 201 /** 202 * @param {string} input 203 * @returns {boolean} 204 */ 205 accepts(input) { 206 const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); 207 const len0 = WASM_VECTOR_LEN; 208 const ret = wasm.webregex_accepts(this.__wbg_ptr, ptr0, len0); 209 return ret !== 0; 210 } 211 /** 212 * @returns {string} 213 */ 214 regex_string() { 215 let deferred1_0; 216 let deferred1_1; 217 try { 218 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 219 wasm.webregex_regex_string(retptr, this.__wbg_ptr); 220 var r0 = getInt32Memory0()[retptr / 4 + 0]; 221 var r1 = getInt32Memory0()[retptr / 4 + 1]; 222 deferred1_0 = r0; 223 deferred1_1 = r1; 224 return getStringFromWasm0(r0, r1); 225 } finally { 226 wasm.__wbindgen_add_to_stack_pointer(16); 227 wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); 228 } 229 } 230 /** 231 * @returns {string} 232 */ 233 regex_to_string() { 234 let deferred1_0; 235 let deferred1_1; 236 try { 237 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 238 wasm.webregex_regex_to_string(retptr, this.__wbg_ptr); 239 var r0 = getInt32Memory0()[retptr / 4 + 0]; 240 var r1 = getInt32Memory0()[retptr / 4 + 1]; 241 deferred1_0 = r0; 242 deferred1_1 = r1; 243 return getStringFromWasm0(r0, r1); 244 } finally { 245 wasm.__wbindgen_add_to_stack_pointer(16); 246 wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); 247 } 248 } 249 /** 250 * @returns {string} 251 */ 252 enfa_to_string() { 253 let deferred1_0; 254 let deferred1_1; 255 try { 256 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 257 wasm.webregex_enfa_to_string(retptr, this.__wbg_ptr); 258 var r0 = getInt32Memory0()[retptr / 4 + 0]; 259 var r1 = getInt32Memory0()[retptr / 4 + 1]; 260 deferred1_0 = r0; 261 deferred1_1 = r1; 262 return getStringFromWasm0(r0, r1); 263 } finally { 264 wasm.__wbindgen_add_to_stack_pointer(16); 265 wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); 266 } 267 } 268 /** 269 * @returns {string} 270 */ 271 nfa_to_string() { 272 let deferred1_0; 273 let deferred1_1; 274 try { 275 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 276 wasm.webregex_nfa_to_string(retptr, this.__wbg_ptr); 277 var r0 = getInt32Memory0()[retptr / 4 + 0]; 278 var r1 = getInt32Memory0()[retptr / 4 + 1]; 279 deferred1_0 = r0; 280 deferred1_1 = r1; 281 return getStringFromWasm0(r0, r1); 282 } finally { 283 wasm.__wbindgen_add_to_stack_pointer(16); 284 wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); 285 } 286 } 287 /** 288 * @returns {string} 289 */ 290 dfa_to_string() { 291 let deferred1_0; 292 let deferred1_1; 293 try { 294 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 295 wasm.webregex_dfa_to_string(retptr, this.__wbg_ptr); 296 var r0 = getInt32Memory0()[retptr / 4 + 0]; 297 var r1 = getInt32Memory0()[retptr / 4 + 1]; 298 deferred1_0 = r0; 299 deferred1_1 = r1; 300 return getStringFromWasm0(r0, r1); 301 } finally { 302 wasm.__wbindgen_add_to_stack_pointer(16); 303 wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); 304 } 305 } 306 /** 307 * @returns {string} 308 */ 309 enfa_dot() { 310 let deferred2_0; 311 let deferred2_1; 312 try { 313 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 314 wasm.webregex_enfa_dot(retptr, this.__wbg_ptr); 315 var r0 = getInt32Memory0()[retptr / 4 + 0]; 316 var r1 = getInt32Memory0()[retptr / 4 + 1]; 317 var r2 = getInt32Memory0()[retptr / 4 + 2]; 318 var r3 = getInt32Memory0()[retptr / 4 + 3]; 319 var ptr1 = r0; 320 var len1 = r1; 321 if (r3) { 322 ptr1 = 0; len1 = 0; 323 throw takeObject(r2); 324 } 325 deferred2_0 = ptr1; 326 deferred2_1 = len1; 327 return getStringFromWasm0(ptr1, len1); 328 } finally { 329 wasm.__wbindgen_add_to_stack_pointer(16); 330 wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); 331 } 332 } 333 /** 334 * @returns {string} 335 */ 336 nfa_dot() { 337 let deferred2_0; 338 let deferred2_1; 339 try { 340 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 341 wasm.webregex_nfa_dot(retptr, this.__wbg_ptr); 342 var r0 = getInt32Memory0()[retptr / 4 + 0]; 343 var r1 = getInt32Memory0()[retptr / 4 + 1]; 344 var r2 = getInt32Memory0()[retptr / 4 + 2]; 345 var r3 = getInt32Memory0()[retptr / 4 + 3]; 346 var ptr1 = r0; 347 var len1 = r1; 348 if (r3) { 349 ptr1 = 0; len1 = 0; 350 throw takeObject(r2); 351 } 352 deferred2_0 = ptr1; 353 deferred2_1 = len1; 354 return getStringFromWasm0(ptr1, len1); 355 } finally { 356 wasm.__wbindgen_add_to_stack_pointer(16); 357 wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); 358 } 359 } 360 /** 361 * @returns {string} 362 */ 363 dfa_dot() { 364 let deferred2_0; 365 let deferred2_1; 366 try { 367 const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); 368 wasm.webregex_dfa_dot(retptr, this.__wbg_ptr); 369 var r0 = getInt32Memory0()[retptr / 4 + 0]; 370 var r1 = getInt32Memory0()[retptr / 4 + 1]; 371 var r2 = getInt32Memory0()[retptr / 4 + 2]; 372 var r3 = getInt32Memory0()[retptr / 4 + 3]; 373 var ptr1 = r0; 374 var len1 = r1; 375 if (r3) { 376 ptr1 = 0; len1 = 0; 377 throw takeObject(r2); 378 } 379 deferred2_0 = ptr1; 380 deferred2_1 = len1; 381 return getStringFromWasm0(ptr1, len1); 382 } finally { 383 wasm.__wbindgen_add_to_stack_pointer(16); 384 wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); 385 } 386 } 387 } 388 389 async function __wbg_load(module, imports) { 390 if (typeof Response === 'function' && module instanceof Response) { 391 if (typeof WebAssembly.instantiateStreaming === 'function') { 392 try { 393 return await WebAssembly.instantiateStreaming(module, imports); 394 395 } catch (e) { 396 if (module.headers.get('Content-Type') != 'application/wasm') { 397 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); 398 399 } else { 400 throw e; 401 } 402 } 403 } 404 405 const bytes = await module.arrayBuffer(); 406 return await WebAssembly.instantiate(bytes, imports); 407 408 } else { 409 const instance = await WebAssembly.instantiate(module, imports); 410 411 if (instance instanceof WebAssembly.Instance) { 412 return { instance, module }; 413 414 } else { 415 return instance; 416 } 417 } 418 } 419 420 function __wbg_get_imports() { 421 const imports = {}; 422 imports.wbg = {}; 423 imports.wbg.__wbg_webautomatonerror_new = function(arg0) { 424 const ret = WebAutomatonError.__wrap(arg0); 425 return addHeapObject(ret); 426 }; 427 imports.wbg.__wbindgen_throw = function(arg0, arg1) { 428 throw new Error(getStringFromWasm0(arg0, arg1)); 429 }; 430 431 return imports; 432 } 433 434 function __wbg_init_memory(imports, maybe_memory) { 435 436 } 437 438 function __wbg_finalize_init(instance, module) { 439 wasm = instance.exports; 440 __wbg_init.__wbindgen_wasm_module = module; 441 cachedInt32Memory0 = null; 442 cachedUint8Memory0 = null; 443 444 445 return wasm; 446 } 447 448 function initSync(module) { 449 if (wasm !== undefined) return wasm; 450 451 const imports = __wbg_get_imports(); 452 453 __wbg_init_memory(imports); 454 455 if (!(module instanceof WebAssembly.Module)) { 456 module = new WebAssembly.Module(module); 457 } 458 459 const instance = new WebAssembly.Instance(module, imports); 460 461 return __wbg_finalize_init(instance, module); 462 } 463 464 async function __wbg_init(input) { 465 if (wasm !== undefined) return wasm; 466 467 if (typeof input === 'undefined') { 468 input = new URL('automaton_bg.wasm', import.meta.url); 469 } 470 const imports = __wbg_get_imports(); 471 472 if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { 473 input = fetch(input); 474 } 475 476 __wbg_init_memory(imports); 477 478 const { instance, module } = await __wbg_load(await input, imports); 479 480 return __wbg_finalize_init(instance, module); 481 } 482 483 export { initSync } 484 export default __wbg_init;