yuku 0.1.0 → 0.2.0
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/dist/browser.d.ts +69 -0
- package/dist/browser.js +122 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/yuku.wasm +0 -0
- package/package.json +40 -8
- package/index.js +0 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
declare enum SourceType {
|
|
2
|
+
Script = 0,
|
|
3
|
+
Module = 1
|
|
4
|
+
}
|
|
5
|
+
declare enum Lang {
|
|
6
|
+
JS = 0,
|
|
7
|
+
TS = 1,
|
|
8
|
+
JSX = 2,
|
|
9
|
+
TSX = 3,
|
|
10
|
+
DTS = 4
|
|
11
|
+
}
|
|
12
|
+
interface ParseOptions {
|
|
13
|
+
sourceType?: "script" | "module";
|
|
14
|
+
lang?: "js" | "ts" | "jsx" | "tsx" | "dts";
|
|
15
|
+
}
|
|
16
|
+
interface YukuNode {
|
|
17
|
+
type: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
type YukuAST = YukuNode;
|
|
21
|
+
/**
|
|
22
|
+
* Parse JavaScript/TypeScript source code into a Yuku AST.
|
|
23
|
+
*
|
|
24
|
+
* @param source - Source code to parse
|
|
25
|
+
* @param options - Parse options
|
|
26
|
+
* @returns Yuku AST
|
|
27
|
+
* @throws Error if parsing fails
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const ast = await parse('const x = 5;', {
|
|
32
|
+
* sourceType: 'module',
|
|
33
|
+
* lang: 'js'
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare function parse(source: string, options?: ParseOptions): Promise<YukuAST>;
|
|
38
|
+
/**
|
|
39
|
+
* Synchronous parse function (requires WASM to be preloaded).
|
|
40
|
+
* Use `parse()` for most cases.
|
|
41
|
+
*
|
|
42
|
+
* @param source - Source code to parse
|
|
43
|
+
* @param options - Parse options
|
|
44
|
+
* @returns Yuku AST
|
|
45
|
+
* @throws Error if WASM not loaded or parsing fails
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* await preload(); // Load WASM first
|
|
50
|
+
* const ast = parseSync('const x = 5;', {
|
|
51
|
+
* sourceType: 'module',
|
|
52
|
+
* lang: 'js'
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function parseSync(source: string, options?: ParseOptions): YukuAST;
|
|
57
|
+
/**
|
|
58
|
+
* Preload WASM module (optional).
|
|
59
|
+
* Useful if you want to load WASM before first parse.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* await preload();
|
|
64
|
+
* // Now you can use parseSync()
|
|
65
|
+
* const ast = parseSync('const x = 5;');
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function preload(): Promise<void>;
|
|
69
|
+
export { preload, parseSync, parse, YukuNode, YukuAST, SourceType, ParseOptions, Lang };
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// src/browser.ts
|
|
2
|
+
var SourceType;
|
|
3
|
+
((SourceType2) => {
|
|
4
|
+
SourceType2[SourceType2["Script"] = 0] = "Script";
|
|
5
|
+
SourceType2[SourceType2["Module"] = 1] = "Module";
|
|
6
|
+
})(SourceType ||= {});
|
|
7
|
+
var Lang;
|
|
8
|
+
((Lang2) => {
|
|
9
|
+
Lang2[Lang2["JS"] = 0] = "JS";
|
|
10
|
+
Lang2[Lang2["TS"] = 1] = "TS";
|
|
11
|
+
Lang2[Lang2["JSX"] = 2] = "JSX";
|
|
12
|
+
Lang2[Lang2["TSX"] = 3] = "TSX";
|
|
13
|
+
Lang2[Lang2["DTS"] = 4] = "DTS";
|
|
14
|
+
})(Lang ||= {});
|
|
15
|
+
var wasmInstance = null;
|
|
16
|
+
async function getWasmInstance() {
|
|
17
|
+
if (wasmInstance)
|
|
18
|
+
return wasmInstance;
|
|
19
|
+
const wasmUrl = new URL("./yuku.wasm", import.meta.url);
|
|
20
|
+
const wasmBuffer = await fetch(wasmUrl).then((r) => r.arrayBuffer());
|
|
21
|
+
const wasmModule = await WebAssembly.instantiate(wasmBuffer);
|
|
22
|
+
wasmInstance = wasmModule.instance.exports;
|
|
23
|
+
return wasmInstance;
|
|
24
|
+
}
|
|
25
|
+
async function parse(source, options = {}) {
|
|
26
|
+
const wasm = await getWasmInstance();
|
|
27
|
+
const encoder = new TextEncoder;
|
|
28
|
+
const sourceBytes = encoder.encode(source);
|
|
29
|
+
const sourceLen = sourceBytes.length;
|
|
30
|
+
const sourcePtr = wasm.alloc(sourceLen);
|
|
31
|
+
if (!sourcePtr) {
|
|
32
|
+
throw new Error("Failed to allocate memory for source code");
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const wasmMemory = new Uint8Array(wasm.memory.buffer, sourcePtr, sourceLen);
|
|
36
|
+
wasmMemory.set(sourceBytes);
|
|
37
|
+
const sourceType = normalizeSourceType(options.sourceType);
|
|
38
|
+
const lang = normalizeLang(options.lang);
|
|
39
|
+
const resultPtr = wasm.parse(sourcePtr, sourceLen, sourceType, lang);
|
|
40
|
+
if (resultPtr === 0) {
|
|
41
|
+
throw new Error("Failed to parse source code");
|
|
42
|
+
}
|
|
43
|
+
const { length: jsonLen, dataPtr: jsonDataPtr } = readLengthPrefixedData(wasm.memory, resultPtr);
|
|
44
|
+
try {
|
|
45
|
+
const decoder = new TextDecoder;
|
|
46
|
+
const jsonBytes = new Uint8Array(wasm.memory.buffer, jsonDataPtr, jsonLen);
|
|
47
|
+
const jsonStr = decoder.decode(jsonBytes);
|
|
48
|
+
return JSON.parse(jsonStr);
|
|
49
|
+
} finally {
|
|
50
|
+
wasm.free(resultPtr, jsonLen + 4);
|
|
51
|
+
}
|
|
52
|
+
} finally {
|
|
53
|
+
wasm.free(sourcePtr, sourceLen);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function parseSync(source, options = {}) {
|
|
57
|
+
if (!wasmInstance) {
|
|
58
|
+
throw new Error("WASM not loaded. Call parse() first or manually call preload()");
|
|
59
|
+
}
|
|
60
|
+
const wasm = wasmInstance;
|
|
61
|
+
const encoder = new TextEncoder;
|
|
62
|
+
const sourceBytes = encoder.encode(source);
|
|
63
|
+
const sourceLen = sourceBytes.length;
|
|
64
|
+
const sourcePtr = wasm.alloc(sourceLen);
|
|
65
|
+
if (!sourcePtr) {
|
|
66
|
+
throw new Error("Failed to allocate memory for source code");
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const wasmMemory = new Uint8Array(wasm.memory.buffer, sourcePtr, sourceLen);
|
|
70
|
+
wasmMemory.set(sourceBytes);
|
|
71
|
+
const sourceType = normalizeSourceType(options.sourceType);
|
|
72
|
+
const lang = normalizeLang(options.lang);
|
|
73
|
+
const resultPtr = wasm.parse(sourcePtr, sourceLen, sourceType, lang);
|
|
74
|
+
if (resultPtr === 0) {
|
|
75
|
+
throw new Error("Failed to parse source code");
|
|
76
|
+
}
|
|
77
|
+
const { length: jsonLen, dataPtr: jsonDataPtr } = readLengthPrefixedData(wasm.memory, resultPtr);
|
|
78
|
+
try {
|
|
79
|
+
const decoder = new TextDecoder;
|
|
80
|
+
const jsonBytes = new Uint8Array(wasm.memory.buffer, jsonDataPtr, jsonLen);
|
|
81
|
+
const jsonStr = decoder.decode(jsonBytes);
|
|
82
|
+
return JSON.parse(jsonStr);
|
|
83
|
+
} finally {
|
|
84
|
+
wasm.free(resultPtr, jsonLen + 4);
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
wasm.free(sourcePtr, sourceLen);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function normalizeSourceType(sourceType) {
|
|
91
|
+
return sourceType === "script" ? 0 /* Script */ : 1 /* Module */;
|
|
92
|
+
}
|
|
93
|
+
function normalizeLang(lang) {
|
|
94
|
+
switch (lang) {
|
|
95
|
+
case "ts":
|
|
96
|
+
return 1 /* TS */;
|
|
97
|
+
case "jsx":
|
|
98
|
+
return 2 /* JSX */;
|
|
99
|
+
case "tsx":
|
|
100
|
+
return 3 /* TSX */;
|
|
101
|
+
case "dts":
|
|
102
|
+
return 4 /* DTS */;
|
|
103
|
+
default:
|
|
104
|
+
return 0 /* JS */;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function readLengthPrefixedData(memory, ptr) {
|
|
108
|
+
const view = new DataView(memory.buffer);
|
|
109
|
+
const length = view.getUint32(ptr, true);
|
|
110
|
+
const dataPtr = ptr + 4;
|
|
111
|
+
return { length, dataPtr };
|
|
112
|
+
}
|
|
113
|
+
async function preload() {
|
|
114
|
+
await getWasmInstance();
|
|
115
|
+
}
|
|
116
|
+
export {
|
|
117
|
+
preload,
|
|
118
|
+
parseSync,
|
|
119
|
+
parse,
|
|
120
|
+
SourceType,
|
|
121
|
+
Lang
|
|
122
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/yuku.wasm
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,13 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuku",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "A very fast JavaScript/TypeScript parser written in Zig",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"browser": "./dist/browser.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"browser": {
|
|
15
|
+
"types": "./dist/browser.d.ts",
|
|
16
|
+
"import": "./dist/browser.js",
|
|
17
|
+
"default": "./dist/browser.js"
|
|
18
|
+
},
|
|
19
|
+
"import": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
6
27
|
"scripts": {
|
|
7
|
-
"
|
|
28
|
+
"build": "bunup",
|
|
29
|
+
"dev": "bunup --watch",
|
|
30
|
+
"type-check": "tsc --noEmit"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/bun": "^1.3.5",
|
|
34
|
+
"bunup": "^0.16.11",
|
|
35
|
+
"typescript": "^5.9.3"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"typescript": ">=4.5.0"
|
|
8
39
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"typescript": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
}
|
|
13
45
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const yuku = "Very fast parser for the web, written in Zig."
|