* Load a language from a WebAssembly module. * The module can be provided as a path to a file or as a buffer.
(input)
| 1480 | * The module can be provided as a path to a file or as a buffer. |
| 1481 | */ |
| 1482 | static async load(input) { |
| 1483 | let binary2; |
| 1484 | if (input instanceof Uint8Array) { |
| 1485 | binary2 = input; |
| 1486 | } else if (globalThis.process?.versions.node) { |
| 1487 | const fs2 = await import("fs/promises"); |
| 1488 | binary2 = await fs2.readFile(input); |
| 1489 | } else { |
| 1490 | const response = await fetch(input); |
| 1491 | if (!response.ok) { |
| 1492 | const body2 = await response.text(); |
| 1493 | throw new Error(`Language.load failed with status ${response.status}. |
| 1494 | |
| 1495 | ${body2}`); |
| 1496 | } |
| 1497 | const retryResp = response.clone(); |
| 1498 | try { |
| 1499 | binary2 = await WebAssembly.compileStreaming(response); |
| 1500 | } catch (reason) { |
| 1501 | console.error("wasm streaming compile failed:", reason); |
| 1502 | console.error("falling back to ArrayBuffer instantiation"); |
| 1503 | binary2 = new Uint8Array(await retryResp.arrayBuffer()); |
| 1504 | } |
| 1505 | } |
| 1506 | const mod = await C.loadWebAssemblyModule(binary2, { loadAsync: true }); |
| 1507 | const symbolNames = Object.keys(mod); |
| 1508 | const functionName = symbolNames.find((key) => LANGUAGE_FUNCTION_REGEX.test(key) && !key.includes("external_scanner_")); |
| 1509 | if (!functionName) { |
| 1510 | console.log(`Couldn't find language function in Wasm file. Symbols: |
| 1511 | ${JSON.stringify(symbolNames, null, 2)}`); |
| 1512 | throw new Error("Language.load failed: no language function found in Wasm file"); |
| 1513 | } |
| 1514 | const languageAddress = mod[functionName](); |
| 1515 | return new _Language(INTERNAL, languageAddress); |
| 1516 | } |
| 1517 | }; |
| 1518 | |
| 1519 | // lib/web-tree-sitter.mjs |
no test coverage detected