yuku 0.2.0 → 0.2.1
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.js +29 -54
- package/package.json +5 -2
package/dist/browser.js
CHANGED
|
@@ -22,42 +22,30 @@ async function getWasmInstance() {
|
|
|
22
22
|
wasmInstance = wasmModule.instance.exports;
|
|
23
23
|
return wasmInstance;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
}
|
|
25
|
+
function normalizeSourceType(sourceType) {
|
|
26
|
+
return sourceType === "script" ? 0 /* Script */ : 1 /* Module */;
|
|
55
27
|
}
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
28
|
+
function normalizeLang(lang) {
|
|
29
|
+
switch (lang) {
|
|
30
|
+
case "ts":
|
|
31
|
+
return 1 /* TS */;
|
|
32
|
+
case "jsx":
|
|
33
|
+
return 2 /* JSX */;
|
|
34
|
+
case "tsx":
|
|
35
|
+
return 3 /* TSX */;
|
|
36
|
+
case "dts":
|
|
37
|
+
return 4 /* DTS */;
|
|
38
|
+
default:
|
|
39
|
+
return 0 /* JS */;
|
|
59
40
|
}
|
|
60
|
-
|
|
41
|
+
}
|
|
42
|
+
function readLengthPrefixedData(memory, ptr) {
|
|
43
|
+
const view = new DataView(memory.buffer);
|
|
44
|
+
const length = view.getUint32(ptr, true);
|
|
45
|
+
const dataPtr = ptr + 4;
|
|
46
|
+
return { length, dataPtr };
|
|
47
|
+
}
|
|
48
|
+
function parseInternal(wasm, source, options) {
|
|
61
49
|
const encoder = new TextEncoder;
|
|
62
50
|
const sourceBytes = encoder.encode(source);
|
|
63
51
|
const sourceLen = sourceBytes.length;
|
|
@@ -87,28 +75,15 @@ function parseSync(source, options = {}) {
|
|
|
87
75
|
wasm.free(sourcePtr, sourceLen);
|
|
88
76
|
}
|
|
89
77
|
}
|
|
90
|
-
function
|
|
91
|
-
|
|
78
|
+
async function parse(source, options = {}) {
|
|
79
|
+
const wasm = await getWasmInstance();
|
|
80
|
+
return parseInternal(wasm, source, options);
|
|
92
81
|
}
|
|
93
|
-
function
|
|
94
|
-
|
|
95
|
-
|
|
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 */;
|
|
82
|
+
function parseSync(source, options = {}) {
|
|
83
|
+
if (!wasmInstance) {
|
|
84
|
+
throw new Error("WASM not loaded. Call parse() first or manually call preload()");
|
|
105
85
|
}
|
|
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 };
|
|
86
|
+
return parseInternal(wasmInstance, source, options);
|
|
112
87
|
}
|
|
113
88
|
async function preload() {
|
|
114
89
|
await getWasmInstance();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuku",
|
|
3
3
|
"description": "A very fast JavaScript/TypeScript parser written in Zig",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
".": {
|
|
14
14
|
"browser": {
|
|
15
15
|
"types": "./dist/browser.d.ts",
|
|
16
|
-
"import": "./dist/browser.js",
|
|
17
16
|
"default": "./dist/browser.js"
|
|
18
17
|
},
|
|
19
18
|
"import": {
|
|
@@ -21,6 +20,10 @@
|
|
|
21
20
|
"default": "./dist/index.js"
|
|
22
21
|
}
|
|
23
22
|
},
|
|
23
|
+
"./browser": {
|
|
24
|
+
"types": "./dist/browser.d.ts",
|
|
25
|
+
"default": "./dist/browser.js"
|
|
26
|
+
},
|
|
24
27
|
"./package.json": "./package.json"
|
|
25
28
|
},
|
|
26
29
|
"license": "MIT",
|