yuku-parser-wasm 0.2.14
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/index.d.ts +65 -0
- package/dist/index.global.d.ts +65 -0
- package/dist/index.global.js +136 -0
- package/dist/index.js +98 -0
- package/dist/yuku.wasm +0 -0
- package/package.json +52 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
type YukuAST = Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Parse JavaScript/TypeScript source code into a Yuku AST.
|
|
19
|
+
*
|
|
20
|
+
* @param source - Source code to parse
|
|
21
|
+
* @param options - Parse options
|
|
22
|
+
* @returns Yuku AST
|
|
23
|
+
* @throws Error if parsing fails
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const ast = await parse('const x = 5;', {
|
|
28
|
+
* sourceType: 'module',
|
|
29
|
+
* lang: 'js'
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function parse(source: string, options?: ParseOptions): Promise<YukuAST>;
|
|
34
|
+
/**
|
|
35
|
+
* Synchronous parse function (requires WASM to be preloaded).
|
|
36
|
+
* Use `parse()` for most cases.
|
|
37
|
+
*
|
|
38
|
+
* @param source - Source code to parse
|
|
39
|
+
* @param options - Parse options
|
|
40
|
+
* @returns Yuku AST
|
|
41
|
+
* @throws Error if WASM not loaded or parsing fails
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* await preload(); // Load WASM first
|
|
46
|
+
* const ast = parseSync('const x = 5;', {
|
|
47
|
+
* sourceType: 'module',
|
|
48
|
+
* lang: 'js'
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function parseSync(source: string, options?: ParseOptions): YukuAST;
|
|
53
|
+
/**
|
|
54
|
+
* Preload WASM module (optional).
|
|
55
|
+
* Useful if you want to load WASM before first parse.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* await preload();
|
|
60
|
+
* // Now you can use parseSync()
|
|
61
|
+
* const ast = parseSync('const x = 5;');
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare function preload(): Promise<void>;
|
|
65
|
+
export { preload, parseSync, parse, SourceType, ParseOptions, Lang };
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
type YukuAST = Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Parse JavaScript/TypeScript source code into a Yuku AST.
|
|
19
|
+
*
|
|
20
|
+
* @param source - Source code to parse
|
|
21
|
+
* @param options - Parse options
|
|
22
|
+
* @returns Yuku AST
|
|
23
|
+
* @throws Error if parsing fails
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const ast = await parse('const x = 5;', {
|
|
28
|
+
* sourceType: 'module',
|
|
29
|
+
* lang: 'js'
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function parse(source: string, options?: ParseOptions): Promise<YukuAST>;
|
|
34
|
+
/**
|
|
35
|
+
* Synchronous parse function (requires WASM to be preloaded).
|
|
36
|
+
* Use `parse()` for most cases.
|
|
37
|
+
*
|
|
38
|
+
* @param source - Source code to parse
|
|
39
|
+
* @param options - Parse options
|
|
40
|
+
* @returns Yuku AST
|
|
41
|
+
* @throws Error if WASM not loaded or parsing fails
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* await preload(); // Load WASM first
|
|
46
|
+
* const ast = parseSync('const x = 5;', {
|
|
47
|
+
* sourceType: 'module',
|
|
48
|
+
* lang: 'js'
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function parseSync(source: string, options?: ParseOptions): YukuAST;
|
|
53
|
+
/**
|
|
54
|
+
* Preload WASM module (optional).
|
|
55
|
+
* Useful if you want to load WASM before first parse.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* await preload();
|
|
60
|
+
* // Now you can use parseSync()
|
|
61
|
+
* const ast = parseSync('const x = 5;');
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare function preload(): Promise<void>;
|
|
65
|
+
export { preload, parseSync, parse, SourceType, ParseOptions, Lang };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
30
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
31
|
+
}) : x)(function(x) {
|
|
32
|
+
if (typeof require !== "undefined")
|
|
33
|
+
return require.apply(this, arguments);
|
|
34
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// index.ts
|
|
38
|
+
var exports_parser_wasm = {};
|
|
39
|
+
__export(exports_parser_wasm, {
|
|
40
|
+
preload: () => preload,
|
|
41
|
+
parseSync: () => parseSync,
|
|
42
|
+
parse: () => parse,
|
|
43
|
+
SourceType: () => SourceType,
|
|
44
|
+
Lang: () => Lang
|
|
45
|
+
});
|
|
46
|
+
var import_yuku_shared = __require("yuku-shared");
|
|
47
|
+
var SourceType;
|
|
48
|
+
((SourceType2) => {
|
|
49
|
+
SourceType2[SourceType2["Script"] = 0] = "Script";
|
|
50
|
+
SourceType2[SourceType2["Module"] = 1] = "Module";
|
|
51
|
+
})(SourceType ||= {});
|
|
52
|
+
var Lang;
|
|
53
|
+
((Lang2) => {
|
|
54
|
+
Lang2[Lang2["JS"] = 0] = "JS";
|
|
55
|
+
Lang2[Lang2["TS"] = 1] = "TS";
|
|
56
|
+
Lang2[Lang2["JSX"] = 2] = "JSX";
|
|
57
|
+
Lang2[Lang2["TSX"] = 3] = "TSX";
|
|
58
|
+
Lang2[Lang2["DTS"] = 4] = "DTS";
|
|
59
|
+
})(Lang ||= {});
|
|
60
|
+
var wasmInstance = null;
|
|
61
|
+
async function getWasmInstance() {
|
|
62
|
+
if (wasmInstance)
|
|
63
|
+
return wasmInstance;
|
|
64
|
+
const wasmModule = await WebAssembly.instantiateStreaming(fetch(new URL("./yuku.wasm", import.meta.url)));
|
|
65
|
+
wasmInstance = wasmModule.instance.exports;
|
|
66
|
+
return wasmInstance;
|
|
67
|
+
}
|
|
68
|
+
function parseInternal(wasm, source, options) {
|
|
69
|
+
const encoder = new TextEncoder;
|
|
70
|
+
const sourceBytes = encoder.encode(source);
|
|
71
|
+
const sourceLen = sourceBytes.length;
|
|
72
|
+
const sourcePtr = sourceLen > 0 ? wasm.alloc(sourceLen) : 0;
|
|
73
|
+
if (sourceLen > 0 && !sourcePtr) {
|
|
74
|
+
throw new Error("Failed to allocate memory for source code");
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
if (sourceLen > 0 && sourcePtr) {
|
|
78
|
+
const wasmMemory = new Uint8Array(wasm.memory.buffer, sourcePtr, sourceLen);
|
|
79
|
+
wasmMemory.set(sourceBytes);
|
|
80
|
+
}
|
|
81
|
+
const sourceType = normalizeSourceType(options.sourceType);
|
|
82
|
+
const lang = normalizeLang(options.lang);
|
|
83
|
+
const result = wasm.parse(sourcePtr, sourceLen, sourceType, lang);
|
|
84
|
+
if (result === 0n) {
|
|
85
|
+
throw new Error("Failed to parse source code");
|
|
86
|
+
}
|
|
87
|
+
const resultPtr = Number(result & 0xffffffffn);
|
|
88
|
+
const jsonLen = Number(result >> 32n);
|
|
89
|
+
if (resultPtr + jsonLen > wasm.memory.buffer.byteLength) {
|
|
90
|
+
throw new Error("Invalid result pointer from WASM parser");
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const decoder = new TextDecoder;
|
|
94
|
+
const jsonBytes = new Uint8Array(wasm.memory.buffer, resultPtr, jsonLen);
|
|
95
|
+
const jsonStr = decoder.decode(jsonBytes);
|
|
96
|
+
return import_yuku_shared.deserializeAstJson(jsonStr);
|
|
97
|
+
} finally {
|
|
98
|
+
wasm.free(resultPtr, jsonLen);
|
|
99
|
+
}
|
|
100
|
+
} finally {
|
|
101
|
+
if (sourceLen > 0 && sourcePtr) {
|
|
102
|
+
wasm.free(sourcePtr, sourceLen);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function parse(source, options = {}) {
|
|
107
|
+
const wasm = await getWasmInstance();
|
|
108
|
+
return parseInternal(wasm, source, options);
|
|
109
|
+
}
|
|
110
|
+
function parseSync(source, options = {}) {
|
|
111
|
+
if (!wasmInstance) {
|
|
112
|
+
throw new Error("WASM not loaded. Call parse() first or manually call preload()");
|
|
113
|
+
}
|
|
114
|
+
return parseInternal(wasmInstance, source, options);
|
|
115
|
+
}
|
|
116
|
+
async function preload() {
|
|
117
|
+
await getWasmInstance();
|
|
118
|
+
}
|
|
119
|
+
function normalizeSourceType(sourceType) {
|
|
120
|
+
return sourceType === "script" ? 0 /* Script */ : 1 /* Module */;
|
|
121
|
+
}
|
|
122
|
+
function normalizeLang(lang) {
|
|
123
|
+
switch (lang) {
|
|
124
|
+
case "ts":
|
|
125
|
+
return 1 /* TS */;
|
|
126
|
+
case "jsx":
|
|
127
|
+
return 2 /* JSX */;
|
|
128
|
+
case "tsx":
|
|
129
|
+
return 3 /* TSX */;
|
|
130
|
+
case "dts":
|
|
131
|
+
return 4 /* DTS */;
|
|
132
|
+
default:
|
|
133
|
+
return 0 /* JS */;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
})();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { deserializeAstJson } from "yuku-shared";
|
|
3
|
+
var SourceType;
|
|
4
|
+
((SourceType2) => {
|
|
5
|
+
SourceType2[SourceType2["Script"] = 0] = "Script";
|
|
6
|
+
SourceType2[SourceType2["Module"] = 1] = "Module";
|
|
7
|
+
})(SourceType ||= {});
|
|
8
|
+
var Lang;
|
|
9
|
+
((Lang2) => {
|
|
10
|
+
Lang2[Lang2["JS"] = 0] = "JS";
|
|
11
|
+
Lang2[Lang2["TS"] = 1] = "TS";
|
|
12
|
+
Lang2[Lang2["JSX"] = 2] = "JSX";
|
|
13
|
+
Lang2[Lang2["TSX"] = 3] = "TSX";
|
|
14
|
+
Lang2[Lang2["DTS"] = 4] = "DTS";
|
|
15
|
+
})(Lang ||= {});
|
|
16
|
+
var wasmInstance = null;
|
|
17
|
+
async function getWasmInstance() {
|
|
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;
|
|
22
|
+
return wasmInstance;
|
|
23
|
+
}
|
|
24
|
+
function parseInternal(wasm, source, options) {
|
|
25
|
+
const encoder = new TextEncoder;
|
|
26
|
+
const sourceBytes = encoder.encode(source);
|
|
27
|
+
const sourceLen = sourceBytes.length;
|
|
28
|
+
const sourcePtr = sourceLen > 0 ? wasm.alloc(sourceLen) : 0;
|
|
29
|
+
if (sourceLen > 0 && !sourcePtr) {
|
|
30
|
+
throw new Error("Failed to allocate memory for source code");
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (sourceLen > 0 && sourcePtr) {
|
|
34
|
+
const wasmMemory = new Uint8Array(wasm.memory.buffer, sourcePtr, sourceLen);
|
|
35
|
+
wasmMemory.set(sourceBytes);
|
|
36
|
+
}
|
|
37
|
+
const sourceType = normalizeSourceType(options.sourceType);
|
|
38
|
+
const lang = normalizeLang(options.lang);
|
|
39
|
+
const result = wasm.parse(sourcePtr, sourceLen, sourceType, lang);
|
|
40
|
+
if (result === 0n) {
|
|
41
|
+
throw new Error("Failed to parse source code");
|
|
42
|
+
}
|
|
43
|
+
const resultPtr = Number(result & 0xffffffffn);
|
|
44
|
+
const jsonLen = Number(result >> 32n);
|
|
45
|
+
if (resultPtr + jsonLen > wasm.memory.buffer.byteLength) {
|
|
46
|
+
throw new Error("Invalid result pointer from WASM parser");
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const decoder = new TextDecoder;
|
|
50
|
+
const jsonBytes = new Uint8Array(wasm.memory.buffer, resultPtr, jsonLen);
|
|
51
|
+
const jsonStr = decoder.decode(jsonBytes);
|
|
52
|
+
return deserializeAstJson(jsonStr);
|
|
53
|
+
} finally {
|
|
54
|
+
wasm.free(resultPtr, jsonLen);
|
|
55
|
+
}
|
|
56
|
+
} finally {
|
|
57
|
+
if (sourceLen > 0 && sourcePtr) {
|
|
58
|
+
wasm.free(sourcePtr, sourceLen);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function parse(source, options = {}) {
|
|
63
|
+
const wasm = await getWasmInstance();
|
|
64
|
+
return parseInternal(wasm, source, options);
|
|
65
|
+
}
|
|
66
|
+
function parseSync(source, options = {}) {
|
|
67
|
+
if (!wasmInstance) {
|
|
68
|
+
throw new Error("WASM not loaded. Call parse() first or manually call preload()");
|
|
69
|
+
}
|
|
70
|
+
return parseInternal(wasmInstance, source, options);
|
|
71
|
+
}
|
|
72
|
+
async function preload() {
|
|
73
|
+
await getWasmInstance();
|
|
74
|
+
}
|
|
75
|
+
function normalizeSourceType(sourceType) {
|
|
76
|
+
return sourceType === "script" ? 0 /* Script */ : 1 /* Module */;
|
|
77
|
+
}
|
|
78
|
+
function normalizeLang(lang) {
|
|
79
|
+
switch (lang) {
|
|
80
|
+
case "ts":
|
|
81
|
+
return 1 /* TS */;
|
|
82
|
+
case "jsx":
|
|
83
|
+
return 2 /* JSX */;
|
|
84
|
+
case "tsx":
|
|
85
|
+
return 3 /* TSX */;
|
|
86
|
+
case "dts":
|
|
87
|
+
return 4 /* DTS */;
|
|
88
|
+
default:
|
|
89
|
+
return 0 /* JS */;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
preload,
|
|
94
|
+
parseSync,
|
|
95
|
+
parse,
|
|
96
|
+
SourceType,
|
|
97
|
+
Lang
|
|
98
|
+
};
|
package/dist/yuku.wasm
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "yuku-parser-wasm",
|
|
3
|
+
"description": "WebAssembly module for Yuku parser, the JavaScript/TypeScript parser",
|
|
4
|
+
"version": "0.2.14",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"funding": "https://github.com/sponsors/arshad-yaseen",
|
|
21
|
+
"homepage": "https://yuku.fyi",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"maintainers": [
|
|
24
|
+
{
|
|
25
|
+
"name": "Arshad Yaseen",
|
|
26
|
+
"email": "arshadpyaseen@gmail.com",
|
|
27
|
+
"url": "https://arshad.fyi"
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "bunup --exports --target browser --format iife,esm && cp ../../zig-out/bin/yuku.wasm dist"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"yuku-shared": "workspace:*"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"typescript": ">=4.5.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"typescript": {
|
|
41
|
+
"optional": true
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"author": "Arshad Yaseen <arshadpyaseen@gmail.com> (https://arshad.fyi)",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/yuku-toolchain/yuku.git"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"bunup": "^0.16.22"
|
|
51
|
+
}
|
|
52
|
+
}
|