yuku 0.2.5 → 0.2.9
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 +63 -63
- package/dist/browser.js +123 -79
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -6
- package/dist/yuku.wasm +0 -0
- package/package.json +29 -20
package/dist/browser.d.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
declare enum SourceType {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export declare enum SourceType {
|
|
2
|
+
Script = 0,
|
|
3
|
+
Module = 1
|
|
4
4
|
}
|
|
5
|
-
declare enum Lang {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
export declare enum Lang {
|
|
6
|
+
JS = 0,
|
|
7
|
+
TS = 1,
|
|
8
|
+
JSX = 2,
|
|
9
|
+
TSX = 3,
|
|
10
|
+
DTS = 4
|
|
11
11
|
}
|
|
12
|
-
interface ParseOptions {
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
export interface ParseOptions {
|
|
13
|
+
sourceType?: 'script' | 'module';
|
|
14
|
+
lang?: 'js' | 'ts' | 'jsx' | 'tsx' | 'dts';
|
|
15
15
|
}
|
|
16
16
|
interface YukuNode {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
type: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
19
|
}
|
|
20
20
|
type YukuAST = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
program: YukuNode[];
|
|
22
|
+
errors: Record<string, unknown>[];
|
|
23
|
+
comments: Record<string, unknown>[];
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
|
-
* Parse JavaScript/TypeScript source code into a Yuku AST.
|
|
27
|
-
*
|
|
28
|
-
* @param source - Source code to parse
|
|
29
|
-
* @param options - Parse options
|
|
30
|
-
* @returns Yuku AST
|
|
31
|
-
* @throws Error if parsing fails
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* ```ts
|
|
35
|
-
* const ast = await parse('const x = 5;', {
|
|
36
|
-
* sourceType: 'module',
|
|
37
|
-
* lang: 'js'
|
|
38
|
-
* });
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
declare function parse(source: string, options?: ParseOptions): Promise<YukuAST>;
|
|
26
|
+
* Parse JavaScript/TypeScript source code into a Yuku AST.
|
|
27
|
+
*
|
|
28
|
+
* @param source - Source code to parse
|
|
29
|
+
* @param options - Parse options
|
|
30
|
+
* @returns Yuku AST
|
|
31
|
+
* @throws Error if parsing fails
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const ast = await parse('const x = 5;', {
|
|
36
|
+
* sourceType: 'module',
|
|
37
|
+
* lang: 'js'
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function parse(source: string, options?: ParseOptions): Promise<YukuAST>;
|
|
42
42
|
/**
|
|
43
|
-
* Synchronous parse function (requires WASM to be preloaded).
|
|
44
|
-
* Use `parse()` for most cases.
|
|
45
|
-
*
|
|
46
|
-
* @param source - Source code to parse
|
|
47
|
-
* @param options - Parse options
|
|
48
|
-
* @returns Yuku AST
|
|
49
|
-
* @throws Error if WASM not loaded or parsing fails
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```ts
|
|
53
|
-
* await preload(); // Load WASM first
|
|
54
|
-
* const ast = parseSync('const x = 5;', {
|
|
55
|
-
* sourceType: 'module',
|
|
56
|
-
* lang: 'js'
|
|
57
|
-
* });
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare function parseSync(source: string, options?: ParseOptions): YukuAST;
|
|
43
|
+
* Synchronous parse function (requires WASM to be preloaded).
|
|
44
|
+
* Use `parse()` for most cases.
|
|
45
|
+
*
|
|
46
|
+
* @param source - Source code to parse
|
|
47
|
+
* @param options - Parse options
|
|
48
|
+
* @returns Yuku AST
|
|
49
|
+
* @throws Error if WASM not loaded or parsing fails
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* await preload(); // Load WASM first
|
|
54
|
+
* const ast = parseSync('const x = 5;', {
|
|
55
|
+
* sourceType: 'module',
|
|
56
|
+
* lang: 'js'
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function parseSync(source: string, options?: ParseOptions): YukuAST;
|
|
61
61
|
/**
|
|
62
|
-
* Preload WASM module (optional).
|
|
63
|
-
* Useful if you want to load WASM before first parse.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```ts
|
|
67
|
-
* await preload();
|
|
68
|
-
* // Now you can use parseSync()
|
|
69
|
-
* const ast = parseSync('const x = 5;');
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
declare function preload(): Promise<void>;
|
|
73
|
-
export {
|
|
62
|
+
* Preload WASM module (optional).
|
|
63
|
+
* Useful if you want to load WASM before first parse.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* await preload();
|
|
68
|
+
* // Now you can use parseSync()
|
|
69
|
+
* const ast = parseSync('const x = 5;');
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function preload(): Promise<void>;
|
|
73
|
+
export {};
|
package/dist/browser.js
CHANGED
|
@@ -1,97 +1,141 @@
|
|
|
1
|
-
|
|
1
|
+
export { SourceType };
|
|
2
2
|
var SourceType;
|
|
3
|
-
((
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
})(SourceType
|
|
3
|
+
(function (SourceType) {
|
|
4
|
+
SourceType[SourceType["Script"] = 0] = "Script";
|
|
5
|
+
SourceType[SourceType["Module"] = 1] = "Module";
|
|
6
|
+
})(SourceType || (SourceType = {}));
|
|
7
|
+
export { Lang };
|
|
7
8
|
var Lang;
|
|
8
|
-
((
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
})(Lang
|
|
15
|
-
|
|
9
|
+
(function (Lang) {
|
|
10
|
+
Lang[Lang["JS"] = 0] = "JS";
|
|
11
|
+
Lang[Lang["TS"] = 1] = "TS";
|
|
12
|
+
Lang[Lang["JSX"] = 2] = "JSX";
|
|
13
|
+
Lang[Lang["TSX"] = 3] = "TSX";
|
|
14
|
+
Lang[Lang["DTS"] = 4] = "DTS";
|
|
15
|
+
})(Lang || (Lang = {}));
|
|
16
|
+
let wasmInstance = null;
|
|
16
17
|
async function getWasmInstance() {
|
|
17
|
-
|
|
18
|
+
if (wasmInstance)
|
|
19
|
+
return wasmInstance;
|
|
20
|
+
const wasmModule = await WebAssembly.instantiateStreaming(fetch(new URL('./yuku.wasm', import.meta.url)));
|
|
21
|
+
wasmInstance = wasmModule.instance.exports;
|
|
18
22
|
return wasmInstance;
|
|
19
|
-
const wasmModule = await WebAssembly.instantiateStreaming(fetch(new URL("./yuku.wasm", import.meta.url)));
|
|
20
|
-
wasmInstance = wasmModule.instance.exports;
|
|
21
|
-
return wasmInstance;
|
|
22
23
|
}
|
|
23
24
|
function parseInternal(wasm, source, options) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
if (sourceLen > 0 && sourcePtr) {
|
|
33
|
-
const wasmMemory = new Uint8Array(wasm.memory.buffer, sourcePtr, sourceLen);
|
|
34
|
-
wasmMemory.set(sourceBytes);
|
|
35
|
-
}
|
|
36
|
-
const sourceType = normalizeSourceType(options.sourceType);
|
|
37
|
-
const lang = normalizeLang(options.lang);
|
|
38
|
-
const result = wasm.parse(sourcePtr, sourceLen, sourceType, lang);
|
|
39
|
-
if (result === 0n) {
|
|
40
|
-
throw new Error("Failed to parse source code");
|
|
41
|
-
}
|
|
42
|
-
const resultPtr = Number(result & 0xFFFFFFFFn);
|
|
43
|
-
const jsonLen = Number(result >> 32n);
|
|
44
|
-
if (resultPtr + jsonLen > wasm.memory.buffer.byteLength) {
|
|
45
|
-
throw new Error("Invalid result pointer from WASM parser");
|
|
25
|
+
const encoder = new TextEncoder();
|
|
26
|
+
const sourceBytes = encoder.encode(source);
|
|
27
|
+
const sourceLen = sourceBytes.length;
|
|
28
|
+
// allocate source code buffer, allow empty source by not requiring allocation when length is 0
|
|
29
|
+
const sourcePtr = sourceLen > 0 ? wasm.alloc(sourceLen) : 0;
|
|
30
|
+
if (sourceLen > 0 && !sourcePtr) {
|
|
31
|
+
throw new Error('Failed to allocate memory for source code');
|
|
46
32
|
}
|
|
47
33
|
try {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
34
|
+
if (sourceLen > 0 && sourcePtr) {
|
|
35
|
+
const wasmMemory = new Uint8Array(wasm.memory.buffer, sourcePtr, sourceLen);
|
|
36
|
+
wasmMemory.set(sourceBytes);
|
|
37
|
+
}
|
|
38
|
+
// options are numbers, so normalize them to pass to wasm
|
|
39
|
+
const sourceType = normalizeSourceType(options.sourceType);
|
|
40
|
+
const lang = normalizeLang(options.lang);
|
|
41
|
+
const result = wasm.parse(sourcePtr, sourceLen, sourceType, lang);
|
|
42
|
+
if (result === 0n) {
|
|
43
|
+
throw new Error('Failed to parse source code');
|
|
44
|
+
}
|
|
45
|
+
// unpack u64: high 32 bits = length, low 32 bits = pointer
|
|
46
|
+
const resultPtr = Number(result & 0xffffffffn);
|
|
47
|
+
const jsonLen = Number(result >> 32n);
|
|
48
|
+
if (resultPtr + jsonLen > wasm.memory.buffer.byteLength) {
|
|
49
|
+
throw new Error('Invalid result pointer from WASM parser');
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const decoder = new TextDecoder();
|
|
53
|
+
const jsonBytes = new Uint8Array(wasm.memory.buffer, resultPtr, jsonLen);
|
|
54
|
+
const jsonStr = decoder.decode(jsonBytes);
|
|
55
|
+
return JSON.parse(jsonStr);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
wasm.free(resultPtr, jsonLen);
|
|
59
|
+
}
|
|
54
60
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
finally {
|
|
62
|
+
if (sourceLen > 0 && sourcePtr) {
|
|
63
|
+
wasm.free(sourcePtr, sourceLen);
|
|
64
|
+
}
|
|
58
65
|
}
|
|
59
|
-
}
|
|
60
66
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Parse JavaScript/TypeScript source code into a Yuku AST.
|
|
69
|
+
*
|
|
70
|
+
* @param source - Source code to parse
|
|
71
|
+
* @param options - Parse options
|
|
72
|
+
* @returns Yuku AST
|
|
73
|
+
* @throws Error if parsing fails
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const ast = await parse('const x = 5;', {
|
|
78
|
+
* sourceType: 'module',
|
|
79
|
+
* lang: 'js'
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export async function parse(source, options = {}) {
|
|
84
|
+
const wasm = await getWasmInstance();
|
|
85
|
+
return parseInternal(wasm, source, options);
|
|
64
86
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Synchronous parse function (requires WASM to be preloaded).
|
|
89
|
+
* Use `parse()` for most cases.
|
|
90
|
+
*
|
|
91
|
+
* @param source - Source code to parse
|
|
92
|
+
* @param options - Parse options
|
|
93
|
+
* @returns Yuku AST
|
|
94
|
+
* @throws Error if WASM not loaded or parsing fails
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* await preload(); // Load WASM first
|
|
99
|
+
* const ast = parseSync('const x = 5;', {
|
|
100
|
+
* sourceType: 'module',
|
|
101
|
+
* lang: 'js'
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function parseSync(source, options = {}) {
|
|
106
|
+
if (!wasmInstance) {
|
|
107
|
+
throw new Error('WASM not loaded. Call parse() first or manually call preload()');
|
|
108
|
+
}
|
|
109
|
+
return parseInternal(wasmInstance, source, options);
|
|
70
110
|
}
|
|
71
|
-
|
|
72
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Preload WASM module (optional).
|
|
113
|
+
* Useful if you want to load WASM before first parse.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* await preload();
|
|
118
|
+
* // Now you can use parseSync()
|
|
119
|
+
* const ast = parseSync('const x = 5;');
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export async function preload() {
|
|
123
|
+
await getWasmInstance();
|
|
73
124
|
}
|
|
74
125
|
function normalizeSourceType(sourceType) {
|
|
75
|
-
|
|
126
|
+
return sourceType === 'script' ? SourceType.Script : SourceType.Module;
|
|
76
127
|
}
|
|
77
128
|
function normalizeLang(lang) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
129
|
+
switch (lang) {
|
|
130
|
+
case 'ts':
|
|
131
|
+
return Lang.TS;
|
|
132
|
+
case 'jsx':
|
|
133
|
+
return Lang.JSX;
|
|
134
|
+
case 'tsx':
|
|
135
|
+
return Lang.TSX;
|
|
136
|
+
case 'dts':
|
|
137
|
+
return Lang.DTS;
|
|
138
|
+
default:
|
|
139
|
+
return Lang.JS;
|
|
140
|
+
}
|
|
90
141
|
}
|
|
91
|
-
export {
|
|
92
|
-
preload,
|
|
93
|
-
parseSync,
|
|
94
|
-
parse,
|
|
95
|
-
SourceType,
|
|
96
|
-
Lang
|
|
97
|
-
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
declare function parse(): string;
|
|
2
|
-
export { parse };
|
|
1
|
+
export declare function parse(): string;
|
package/dist/index.js
CHANGED
package/dist/yuku.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuku",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
3
|
+
"version": "0.2.9",
|
|
4
|
+
"author": "Arshad Yaseen <arshadpyaseen@gmail.com> (https://arshad.fyi)",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/arshad-yaseen/yuku.git"
|
|
8
|
+
},
|
|
9
9
|
"module": "./dist/index.js",
|
|
10
|
-
"
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@types/bun": "^1.3.5",
|
|
12
|
+
"@typescript/native-preview": "^7.0.0-dev.20260109.1"
|
|
13
|
+
},
|
|
11
14
|
"exports": {
|
|
12
15
|
".": {
|
|
13
16
|
"import": {
|
|
@@ -21,22 +24,28 @@
|
|
|
21
24
|
},
|
|
22
25
|
"./package.json": "./package.json"
|
|
23
26
|
},
|
|
27
|
+
"description": "A very fast JavaScript/TypeScript parser written in Zig",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"funding": "https://github.com/sponsors/arshad-yaseen",
|
|
32
|
+
"homepage": "https://yuku.fyi",
|
|
24
33
|
"license": "MIT",
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"typescript": "^5.9.3"
|
|
33
|
-
},
|
|
34
|
-
"peerDependencies": {
|
|
35
|
-
"typescript": ">=4.5.0"
|
|
36
|
-
},
|
|
34
|
+
"maintainers": [
|
|
35
|
+
{
|
|
36
|
+
"name": "Arshad Yaseen",
|
|
37
|
+
"email": "arshadpyaseen@gmail.com",
|
|
38
|
+
"url": "https://arshad.fyi"
|
|
39
|
+
}
|
|
40
|
+
],
|
|
37
41
|
"peerDependenciesMeta": {
|
|
38
42
|
"typescript": {
|
|
39
43
|
"optional": true
|
|
40
44
|
}
|
|
41
|
-
}
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsgo && cp ../zig-out/bin/yuku.wasm dist"
|
|
48
|
+
},
|
|
49
|
+
"type": "module",
|
|
50
|
+
"types": "./dist/index.d.ts"
|
|
42
51
|
}
|