@yuku-parser/wasm 0.5.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,60 @@
1
+ import { decode } from "./decode.js";
2
+
3
+ const wasmUrl = new URL("./yuku-parser.wasm", import.meta.url);
4
+
5
+ async function instantiate() {
6
+ if (wasmUrl.protocol !== "file:") {
7
+ try {
8
+ return (await WebAssembly.instantiateStreaming(fetch(wasmUrl))).instance;
9
+ } catch {
10
+ const bytes = await (await fetch(wasmUrl)).arrayBuffer();
11
+ return (await WebAssembly.instantiate(bytes)).instance;
12
+ }
13
+ }
14
+ const { readFile } = await import("node:fs/promises");
15
+ return (await WebAssembly.instantiate(await readFile(wasmUrl))).instance;
16
+ }
17
+
18
+ const { memory, alloc, free, parse: wasmParse } = (await instantiate()).exports;
19
+ const encoder = new TextEncoder();
20
+
21
+ const LANGS = { js: 0, ts: 1, jsx: 2, tsx: 3, dts: 4 };
22
+
23
+ function packFlags(o = {}) {
24
+ let f = (LANGS[o.lang] ?? 0) << 1;
25
+ if (o.sourceType === "script") f |= 1 << 0;
26
+ if (o.preserveParens !== false) f |= 1 << 4;
27
+ if (o.allowReturnOutsideFunction) f |= 1 << 5;
28
+ if (o.semanticErrors) f |= 1 << 6;
29
+ if (o.attachComments) f |= 1 << 7;
30
+ return f;
31
+ }
32
+
33
+ export function parse(source, options) {
34
+ const bytes = encoder.encode(source);
35
+ const srcPtr = alloc(bytes.length);
36
+ // Growing wasm memory detaches memory.buffer, so re-view after every call.
37
+ new Uint8Array(memory.buffer, srcPtr, bytes.length).set(bytes);
38
+
39
+ const ptr = wasmParse(srcPtr, bytes.length, packFlags(options));
40
+ free(srcPtr, bytes.length);
41
+ if (ptr === 0) throw new Error("yuku-parser-wasm: failed to parse source");
42
+
43
+ const len = new DataView(memory.buffer).getUint32(ptr, true);
44
+ const buffer = memory.buffer.slice(ptr + 4, ptr + 4 + len);
45
+ free(ptr, 4 + len);
46
+
47
+ return decode(buffer, source);
48
+ }
49
+
50
+ export function langFromPath(path) {
51
+ if (path.endsWith(".d.ts") || path.endsWith(".d.mts") || path.endsWith(".d.cts")) return "dts";
52
+ if (path.endsWith(".tsx")) return "tsx";
53
+ if (path.endsWith(".ts") || path.endsWith(".mts") || path.endsWith(".cts")) return "ts";
54
+ if (path.endsWith(".jsx")) return "jsx";
55
+ return "js";
56
+ }
57
+
58
+ export function sourceTypeFromPath(path) {
59
+ return path.endsWith(".cjs") || path.endsWith(".cts") ? "script" : "module";
60
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@yuku-parser/wasm",
3
+ "version": "0.5.21",
4
+ "description": "High-performance JavaScript/TypeScript parser, compiled to WebAssembly",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/yuku-toolchain/yuku"
9
+ },
10
+ "type": "module",
11
+ "main": "index.js",
12
+ "types": "index.d.ts",
13
+ "files": [
14
+ "index.js",
15
+ "index.d.ts",
16
+ "decode.js",
17
+ "yuku-parser.wasm"
18
+ ],
19
+ "keywords": [
20
+ "acorn",
21
+ "ast",
22
+ "babel",
23
+ "browser",
24
+ "compiler",
25
+ "ecmascript",
26
+ "espree",
27
+ "fast",
28
+ "javascript",
29
+ "js",
30
+ "jsx",
31
+ "lexer",
32
+ "oxc",
33
+ "parser",
34
+ "swc",
35
+ "tokenizer",
36
+ "ts",
37
+ "tsx",
38
+ "typescript",
39
+ "wasm",
40
+ "webassembly",
41
+ "zig"
42
+ ]
43
+ }
Binary file