@bytecodealliance/jco 1.16.0 → 1.17.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/README.md +6 -6
- package/obj/js-component-bindgen-component.core.wasm +0 -0
- package/obj/js-component-bindgen-component.js +1969 -1664
- package/obj/wasm-tools.core.wasm +0 -0
- package/obj/wasm-tools.js +2072 -1687
- package/package.json +94 -95
- package/src/api.js +5 -14
- package/src/browser.js +1 -1
- package/src/cmd/componentize.d.ts +1 -1
- package/src/cmd/componentize.js +14 -18
- package/src/cmd/opt.d.ts +10 -7
- package/src/cmd/opt.js +44 -96
- package/src/cmd/run.d.ts +1 -1
- package/src/cmd/run.js +31 -48
- package/src/cmd/transpile.d.ts +44 -39
- package/src/cmd/transpile.js +253 -316
- package/src/cmd/types.d.ts +15 -12
- package/src/cmd/types.js +27 -36
- package/src/cmd/wasm-tools.d.ts +1 -1
- package/src/cmd/wasm-tools.js +27 -44
- package/src/common.js +43 -57
- package/src/jco.js +200 -347
- package/types/api.d.ts.map +1 -1
- package/types/common.d.ts.map +1 -1
package/src/cmd/transpile.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/* global Buffer */
|
|
2
2
|
|
|
3
|
-
import { extname, basename, resolve } from
|
|
3
|
+
import { extname, basename, resolve } from "node:path";
|
|
4
4
|
|
|
5
|
-
import { minify } from
|
|
6
|
-
import { fileURLToPath } from
|
|
5
|
+
import { minify } from "terser";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
7
|
|
|
8
|
-
import { optimizeComponent } from
|
|
8
|
+
import { optimizeComponent } from "./opt.js";
|
|
9
9
|
|
|
10
|
-
import { $init, generate } from
|
|
10
|
+
import { $init, generate } from "../../obj/js-component-bindgen-component.js";
|
|
11
11
|
import {
|
|
12
12
|
readFile,
|
|
13
13
|
spawnIOTmp,
|
|
@@ -18,19 +18,19 @@ import {
|
|
|
18
18
|
ASYNC_WASI_IMPORTS,
|
|
19
19
|
ASYNC_WASI_EXPORTS,
|
|
20
20
|
DEFAULT_ASYNC_MODE,
|
|
21
|
-
} from
|
|
22
|
-
import { $init as wasmToolsInit, tools } from
|
|
21
|
+
} from "../common.js";
|
|
22
|
+
import { $init as wasmToolsInit, tools } from "../../obj/wasm-tools.js";
|
|
23
23
|
const { componentEmbed, componentNew } = tools;
|
|
24
24
|
|
|
25
|
-
import ora from
|
|
25
|
+
import ora from "#ora";
|
|
26
26
|
|
|
27
|
-
import { isWindows } from
|
|
27
|
+
import { isWindows } from "../common.js";
|
|
28
28
|
|
|
29
29
|
// These re-exports exist to avoid breaking backwards compatibility
|
|
30
|
-
export { types, guestTypes, typesComponent } from
|
|
30
|
+
export { types, guestTypes, typesComponent } from "./types.js";
|
|
31
31
|
|
|
32
32
|
export async function transpile(witPath, opts, program) {
|
|
33
|
-
const varIdx = program?.parent.rawArgs.indexOf(
|
|
33
|
+
const varIdx = program?.parent.rawArgs.indexOf("--");
|
|
34
34
|
if (varIdx !== undefined && varIdx !== -1) {
|
|
35
35
|
opts.optArgs = program.parent.rawArgs.slice(varIdx + 1);
|
|
36
36
|
}
|
|
@@ -43,9 +43,9 @@ export async function transpile(witPath, opts, program) {
|
|
|
43
43
|
component = componentNew(
|
|
44
44
|
componentEmbed({
|
|
45
45
|
dummy: true,
|
|
46
|
-
witPath: (isWindows ?
|
|
46
|
+
witPath: (isWindows ? "//?/" : "") + resolve(witPath),
|
|
47
47
|
}),
|
|
48
|
-
[]
|
|
48
|
+
[],
|
|
49
49
|
);
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -53,14 +53,10 @@ export async function transpile(witPath, opts, program) {
|
|
|
53
53
|
setShowSpinner(true);
|
|
54
54
|
}
|
|
55
55
|
if (!opts.name) {
|
|
56
|
-
opts.name = basename(
|
|
57
|
-
witPath.slice(0, -extname(witPath).length || Infinity)
|
|
58
|
-
);
|
|
56
|
+
opts.name = basename(witPath.slice(0, -extname(witPath).length || Infinity));
|
|
59
57
|
}
|
|
60
58
|
if (opts.map) {
|
|
61
|
-
opts.map = Object.fromEntries(
|
|
62
|
-
opts.map.map((mapping) => mapping.split('='))
|
|
63
|
-
);
|
|
59
|
+
opts.map = Object.fromEntries(opts.map.map((mapping) => mapping.split("=")));
|
|
64
60
|
}
|
|
65
61
|
|
|
66
62
|
if (opts.asyncWasiImports) {
|
|
@@ -71,10 +67,7 @@ export async function transpile(witPath, opts, program) {
|
|
|
71
67
|
}
|
|
72
68
|
|
|
73
69
|
const { files } = await transpileComponent(component, opts);
|
|
74
|
-
await writeFiles(
|
|
75
|
-
files,
|
|
76
|
-
opts.quiet ? false : 'Transpiled JS Component Files'
|
|
77
|
-
);
|
|
70
|
+
await writeFiles(files, opts.quiet ? false : "Transpiled JS Component Files");
|
|
78
71
|
}
|
|
79
72
|
|
|
80
73
|
/**
|
|
@@ -82,14 +75,12 @@ export async function transpile(witPath, opts, program) {
|
|
|
82
75
|
* @returns {Promise<Uint8Array>}
|
|
83
76
|
*/
|
|
84
77
|
async function wasm2Js(source) {
|
|
85
|
-
const wasm2jsPath = fileURLToPath(
|
|
86
|
-
import.meta.resolve('binaryen/bin/wasm2js')
|
|
87
|
-
);
|
|
78
|
+
const wasm2jsPath = fileURLToPath(import.meta.resolve("binaryen/bin/wasm2js"));
|
|
88
79
|
|
|
89
80
|
try {
|
|
90
|
-
return await spawnIOTmp(wasm2jsPath, source, [
|
|
81
|
+
return await spawnIOTmp(wasm2jsPath, source, ["-Oz", "-o"]);
|
|
91
82
|
} catch (e) {
|
|
92
|
-
if (e.toString().includes(
|
|
83
|
+
if (e.toString().includes("BasicBlock requested")) {
|
|
93
84
|
return wasm2Js(source);
|
|
94
85
|
}
|
|
95
86
|
throw e;
|
|
@@ -145,16 +136,15 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
145
136
|
if (opts.wasiShim !== false) {
|
|
146
137
|
opts.map = Object.assign(
|
|
147
138
|
{
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
'wasi:sockets/*': '@bytecodealliance/preview2-shim/sockets#*',
|
|
139
|
+
"wasi:cli/*": "@bytecodealliance/preview2-shim/cli#*",
|
|
140
|
+
"wasi:clocks/*": "@bytecodealliance/preview2-shim/clocks#*",
|
|
141
|
+
"wasi:filesystem/*": "@bytecodealliance/preview2-shim/filesystem#*",
|
|
142
|
+
"wasi:http/*": "@bytecodealliance/preview2-shim/http#*",
|
|
143
|
+
"wasi:io/*": "@bytecodealliance/preview2-shim/io#*",
|
|
144
|
+
"wasi:random/*": "@bytecodealliance/preview2-shim/random#*",
|
|
145
|
+
"wasi:sockets/*": "@bytecodealliance/preview2-shim/sockets#*",
|
|
156
146
|
},
|
|
157
|
-
opts.map || {}
|
|
147
|
+
opts.map || {},
|
|
158
148
|
);
|
|
159
149
|
}
|
|
160
150
|
|
|
@@ -166,7 +156,7 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
166
156
|
}
|
|
167
157
|
// Otherwise, if `--js` is present, an `instantiate` function is required.
|
|
168
158
|
else if (opts.js) {
|
|
169
|
-
instantiation = { tag:
|
|
159
|
+
instantiation = { tag: "async" };
|
|
170
160
|
}
|
|
171
161
|
|
|
172
162
|
// Get the configured async mode then transform it into what the types component expects
|
|
@@ -174,22 +164,18 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
174
164
|
let asyncImports = new Set([...(opts.asyncImports ?? [])]);
|
|
175
165
|
let asyncExports = new Set([...(opts.asyncExports ?? [])]);
|
|
176
166
|
let asyncMode = opts.asyncMode ?? DEFAULT_ASYNC_MODE;
|
|
177
|
-
if (asyncMode ===
|
|
178
|
-
throw new Error(
|
|
179
|
-
'async exports cannot be specified in sync mode (consider adding --async-mode=jspi)'
|
|
180
|
-
);
|
|
167
|
+
if (asyncMode === "sync" && asyncExports.size > 0) {
|
|
168
|
+
throw new Error("async exports cannot be specified in sync mode (consider adding --async-mode=jspi)");
|
|
181
169
|
}
|
|
182
|
-
if (asyncMode ===
|
|
183
|
-
throw new Error(
|
|
184
|
-
'async imports cannot be specified in sync mode (consider adding --async-mode=jspi)'
|
|
185
|
-
);
|
|
170
|
+
if (asyncMode === "sync" && asyncImports.size > 0) {
|
|
171
|
+
throw new Error("async imports cannot be specified in sync mode (consider adding --async-mode=jspi)");
|
|
186
172
|
}
|
|
187
173
|
let asyncModeObj;
|
|
188
|
-
if (asyncMode ===
|
|
174
|
+
if (asyncMode === "sync") {
|
|
189
175
|
asyncModeObj = null;
|
|
190
|
-
} else if (asyncMode ===
|
|
176
|
+
} else if (asyncMode === "jspi") {
|
|
191
177
|
asyncModeObj = {
|
|
192
|
-
tag:
|
|
178
|
+
tag: "jspi",
|
|
193
179
|
val: {
|
|
194
180
|
imports: [...asyncImports],
|
|
195
181
|
exports: [...asyncExports],
|
|
@@ -200,13 +186,11 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
200
186
|
}
|
|
201
187
|
|
|
202
188
|
let { files, imports, exports } = generate(component, {
|
|
203
|
-
name: opts.name ??
|
|
189
|
+
name: opts.name ?? "component",
|
|
204
190
|
map: Object.entries(opts.map ?? {}),
|
|
205
191
|
instantiation,
|
|
206
192
|
asyncMode: asyncModeObj,
|
|
207
|
-
importBindings: opts.importBindings
|
|
208
|
-
? { tag: opts.importBindings }
|
|
209
|
-
: null,
|
|
193
|
+
importBindings: opts.importBindings ? { tag: opts.importBindings } : null,
|
|
210
194
|
validLiftingOptimization: opts.validLiftingOptimization ?? false,
|
|
211
195
|
tracing: opts.tracing ?? false,
|
|
212
196
|
noNodejsCompat: opts.nodejsCompat === false,
|
|
@@ -218,13 +202,13 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
218
202
|
idlImports: opts.experimentalIdlImports === true,
|
|
219
203
|
});
|
|
220
204
|
|
|
221
|
-
let outDir = (opts.outDir ??
|
|
222
|
-
if (!outDir.endsWith(
|
|
223
|
-
outDir +=
|
|
205
|
+
let outDir = (opts.outDir ?? "").replace(/\\/g, "/");
|
|
206
|
+
if (!outDir.endsWith("/") && outDir !== "") {
|
|
207
|
+
outDir += "/";
|
|
224
208
|
}
|
|
225
209
|
files = files.map(([name, source]) => [`${outDir}${name}`, source]);
|
|
226
210
|
|
|
227
|
-
const jsFile = files.find(([name]) => name.endsWith(
|
|
211
|
+
const jsFile = files.find(([name]) => name.endsWith(".js"));
|
|
228
212
|
|
|
229
213
|
// Generate code for the `--js` option.
|
|
230
214
|
//
|
|
@@ -273,25 +257,19 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
273
257
|
// non-async.
|
|
274
258
|
if (opts.js) {
|
|
275
259
|
const withInstantiation = opts.instantiation !== undefined;
|
|
276
|
-
const async_ = instantiation.tag ==
|
|
277
|
-
const await_ = instantiation.tag ==
|
|
260
|
+
const async_ = instantiation.tag == "async" ? "async " : "";
|
|
261
|
+
const await_ = instantiation.tag == "async" ? "await " : "";
|
|
278
262
|
|
|
279
263
|
// Format the previously generated code.
|
|
280
264
|
const source = Buffer.from(jsFile[1])
|
|
281
|
-
.toString(
|
|
265
|
+
.toString("utf8")
|
|
282
266
|
// update imports manging to match emscripten asm
|
|
283
|
-
.replace(
|
|
284
|
-
|
|
285
|
-
(_, i, s) => `exports${i}['${asmMangle(s)}']`
|
|
286
|
-
)
|
|
287
|
-
.replace(
|
|
288
|
-
/export (async )?function instantiate/,
|
|
289
|
-
'$1function _instantiate'
|
|
290
|
-
);
|
|
267
|
+
.replace(/exports(\d+)\['([^']+)']/g, (_, i, s) => `exports${i}['${asmMangle(s)}']`)
|
|
268
|
+
.replace(/export (async )?function instantiate/, "$1function _instantiate");
|
|
291
269
|
|
|
292
270
|
// Collect all Wasm files.
|
|
293
|
-
const wasmFiles = files.filter(([name]) => name.endsWith(
|
|
294
|
-
files = files.filter(([name]) => !name.endsWith(
|
|
271
|
+
const wasmFiles = files.filter(([name]) => name.endsWith(".wasm"));
|
|
272
|
+
files = files.filter(([name]) => !name.endsWith(".wasm"));
|
|
295
273
|
|
|
296
274
|
// Configure the spinner.
|
|
297
275
|
let completed = 0;
|
|
@@ -299,8 +277,8 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
299
277
|
`${styleText("cyan", `${completed} / ${wasmFiles.length}`)} Running Binaryen wasm2js on Wasm core modules (this takes a while)...\n`;
|
|
300
278
|
if (showSpinner) {
|
|
301
279
|
spinner = ora({
|
|
302
|
-
color:
|
|
303
|
-
spinner:
|
|
280
|
+
color: "cyan",
|
|
281
|
+
spinner: "bouncingBar",
|
|
304
282
|
}).start();
|
|
305
283
|
spinner.text = spinnerText();
|
|
306
284
|
}
|
|
@@ -309,40 +287,36 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
309
287
|
try {
|
|
310
288
|
const asmFiles = await Promise.all(
|
|
311
289
|
wasmFiles.map(async ([, source]) => {
|
|
312
|
-
const output = (await wasm2Js(source)).toString(
|
|
290
|
+
const output = (await wasm2Js(source)).toString("utf8");
|
|
313
291
|
if (spinner) {
|
|
314
292
|
completed++;
|
|
315
293
|
spinner.text = spinnerText();
|
|
316
294
|
}
|
|
317
295
|
return output;
|
|
318
|
-
})
|
|
296
|
+
}),
|
|
319
297
|
);
|
|
320
298
|
|
|
321
299
|
const asms = asmFiles
|
|
322
300
|
.map(
|
|
323
301
|
(asm, nth) => `function asm${nth}(imports) {
|
|
324
302
|
${
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
303
|
+
// strip and replace the asm instantiation wrapper
|
|
304
|
+
asm
|
|
305
|
+
.replace(/import \* as [^ ]+ from '[^']*';/g, "")
|
|
306
|
+
.replace("function asmFunc(imports) {", "")
|
|
307
|
+
.replace(/export var ([^ ]+) = ([^. ]+)\.([^ ]+);/g, "")
|
|
308
|
+
.replace(/var retasmFunc = [\s\S]*$/, "")
|
|
309
|
+
.replace(/var memasmFunc = new ArrayBuffer\(0\);/g, "")
|
|
310
|
+
.replace("memory.grow = __wasm_memory_grow;", "")
|
|
311
|
+
.trim()
|
|
312
|
+
}`,
|
|
335
313
|
)
|
|
336
|
-
.join(
|
|
314
|
+
.join(",\n");
|
|
337
315
|
|
|
338
316
|
// The `instantiate` function.
|
|
339
|
-
const instantiateFunction = `${
|
|
340
|
-
withInstantiation ? 'export ' : ''
|
|
341
|
-
}${async_}function instantiate(imports) {
|
|
317
|
+
const instantiateFunction = `${withInstantiation ? "export " : ""}${async_}function instantiate(imports) {
|
|
342
318
|
const wasm_file_to_asm_index = {
|
|
343
|
-
${wasmFiles
|
|
344
|
-
.map(([path], nth) => `'${basename(path)}': ${nth}`)
|
|
345
|
-
.join(',\n ')}
|
|
319
|
+
${wasmFiles.map(([path], nth) => `'${basename(path)}': ${nth}`).join(",\n ")}
|
|
346
320
|
};
|
|
347
321
|
|
|
348
322
|
return ${await_}_instantiate(
|
|
@@ -353,75 +327,70 @@ export async function transpileComponent(component, opts = {}) {
|
|
|
353
327
|
}`;
|
|
354
328
|
|
|
355
329
|
// If `--js` is used without `--instantiation`.
|
|
356
|
-
let importDirectives =
|
|
357
|
-
let exportDirectives =
|
|
358
|
-
let exportTrampolines =
|
|
359
|
-
let autoInstantiate =
|
|
330
|
+
let importDirectives = "";
|
|
331
|
+
let exportDirectives = "";
|
|
332
|
+
let exportTrampolines = "";
|
|
333
|
+
let autoInstantiate = "";
|
|
360
334
|
|
|
361
335
|
if (!withInstantiation) {
|
|
362
336
|
importDirectives = imports
|
|
363
|
-
.map(
|
|
364
|
-
|
|
365
|
-
`import * as import${nth} from '${import_file}';`
|
|
366
|
-
)
|
|
367
|
-
.join('\n');
|
|
337
|
+
.map((import_file, nth) => `import * as import${nth} from '${import_file}';`)
|
|
338
|
+
.join("\n");
|
|
368
339
|
|
|
369
340
|
if (exports.length > 0 || opts.tlaCompat) {
|
|
370
341
|
exportDirectives = `export {
|
|
371
342
|
${
|
|
372
343
|
// Exporting `$init` must come first to not break the transpiling tests.
|
|
373
|
-
opts.tlaCompat ?
|
|
344
|
+
opts.tlaCompat ? " $init,\n" : ""
|
|
374
345
|
}${exports
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
346
|
+
.map(([name]) => {
|
|
347
|
+
if (name === asmMangle(name)) {
|
|
348
|
+
return ` ${name},`;
|
|
349
|
+
} else {
|
|
350
|
+
return ` ${asmMangle(name)} as '${name}',`;
|
|
351
|
+
}
|
|
352
|
+
})
|
|
353
|
+
.join("\n")}
|
|
383
354
|
}`;
|
|
384
355
|
}
|
|
385
356
|
|
|
386
357
|
exportTrampolines = `let ${exports
|
|
387
|
-
.filter(([, ty]) => ty ===
|
|
358
|
+
.filter(([, ty]) => ty === "function")
|
|
388
359
|
.map(([name]) => `_${asmMangle(name)}`)
|
|
389
|
-
.join(
|
|
360
|
+
.join(", ")};
|
|
390
361
|
${exports
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
362
|
+
.map(([name, ty]) => {
|
|
363
|
+
if (ty === "function") {
|
|
364
|
+
return `\nfunction ${asmMangle(name)} () {
|
|
394
365
|
return _${asmMangle(name)}.apply(this, arguments);
|
|
395
366
|
}`;
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
367
|
+
} else {
|
|
368
|
+
return `\nlet ${asmMangle(name)};`;
|
|
369
|
+
}
|
|
370
|
+
})
|
|
371
|
+
.join("\n")}`;
|
|
401
372
|
|
|
402
373
|
autoInstantiate = `${async_}function $init() {
|
|
403
374
|
( {
|
|
404
375
|
${exports
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
376
|
+
.map(([name, ty]) => {
|
|
377
|
+
if (ty === "function") {
|
|
378
|
+
return ` '${name}': _${asmMangle(name)},`;
|
|
379
|
+
} else if (asmMangle(name) === name) {
|
|
380
|
+
return ` ${name},`;
|
|
381
|
+
} else {
|
|
382
|
+
return ` '${name}': ${asmMangle(name)},`;
|
|
383
|
+
}
|
|
384
|
+
})
|
|
385
|
+
.join("\n")}
|
|
415
386
|
} = ${await_}instantiate(
|
|
416
387
|
{
|
|
417
|
-
${imports
|
|
418
|
-
.map((import_file, nth) => ` '${import_file}': import${nth},`)
|
|
419
|
-
.join('\n')}
|
|
388
|
+
${imports.map((import_file, nth) => ` '${import_file}': import${nth},`).join("\n")}
|
|
420
389
|
}
|
|
421
390
|
) )
|
|
422
391
|
}
|
|
423
392
|
|
|
424
|
-
${opts.tlaCompat ?
|
|
393
|
+
${opts.tlaCompat ? "" : `${await_}$init();`}`;
|
|
425
394
|
}
|
|
426
395
|
|
|
427
396
|
// Prepare the final generated code.
|
|
@@ -450,19 +419,16 @@ ${autoInstantiate}`;
|
|
|
450
419
|
|
|
451
420
|
if (opts.minify) {
|
|
452
421
|
try {
|
|
453
|
-
({ code: jsFile[1] } = await minify(
|
|
454
|
-
|
|
455
|
-
{
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
},
|
|
464
|
-
}
|
|
465
|
-
));
|
|
422
|
+
({ code: jsFile[1] } = await minify(Buffer.from(jsFile[1]).toString("utf8"), {
|
|
423
|
+
module: true,
|
|
424
|
+
compress: {
|
|
425
|
+
ecma: 9,
|
|
426
|
+
unsafe: true,
|
|
427
|
+
},
|
|
428
|
+
mangle: {
|
|
429
|
+
keep_classnames: true,
|
|
430
|
+
},
|
|
431
|
+
}));
|
|
466
432
|
} catch (err) {
|
|
467
433
|
console.error(`error while minifying JS: ${err}`);
|
|
468
434
|
throw err;
|
|
@@ -476,8 +442,8 @@ ${autoInstantiate}`;
|
|
|
476
442
|
// for imports to match up we must do the same
|
|
477
443
|
// See https://github.com/WebAssembly/binaryen/blob/main/src/asmjs/asmangle.cpp
|
|
478
444
|
function asmMangle(name) {
|
|
479
|
-
if (name ===
|
|
480
|
-
return
|
|
445
|
+
if (name === "") {
|
|
446
|
+
return "$";
|
|
481
447
|
}
|
|
482
448
|
|
|
483
449
|
let mightBeKeyword = true;
|
|
@@ -485,210 +451,181 @@ function asmMangle(name) {
|
|
|
485
451
|
|
|
486
452
|
// Names must start with a character, $ or _
|
|
487
453
|
switch (name[0]) {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
mightBeKeyword = false;
|
|
505
|
-
break;
|
|
506
|
-
}
|
|
507
|
-
default: {
|
|
508
|
-
let chNum = name.charCodeAt(0);
|
|
509
|
-
if (
|
|
510
|
-
!(chNum >= 97 && chNum <= 122) &&
|
|
511
|
-
!(chNum >= 65 && chNum <= 90)
|
|
512
|
-
) {
|
|
513
|
-
name = '$' + name.substr(1);
|
|
454
|
+
case "0":
|
|
455
|
+
case "1":
|
|
456
|
+
case "2":
|
|
457
|
+
case "3":
|
|
458
|
+
case "4":
|
|
459
|
+
case "5":
|
|
460
|
+
case "6":
|
|
461
|
+
case "7":
|
|
462
|
+
case "8":
|
|
463
|
+
case "9": {
|
|
464
|
+
name = "$" + name;
|
|
465
|
+
i = 2;
|
|
466
|
+
// fallthrough
|
|
467
|
+
}
|
|
468
|
+
case "$":
|
|
469
|
+
case "_": {
|
|
514
470
|
mightBeKeyword = false;
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
default: {
|
|
474
|
+
let chNum = name.charCodeAt(0);
|
|
475
|
+
if (!(chNum >= 97 && chNum <= 122) && !(chNum >= 65 && chNum <= 90)) {
|
|
476
|
+
name = "$" + name.substr(1);
|
|
477
|
+
mightBeKeyword = false;
|
|
478
|
+
}
|
|
515
479
|
}
|
|
516
|
-
}
|
|
517
480
|
}
|
|
518
481
|
|
|
519
482
|
// Names must contain only characters, digits, $ or _
|
|
520
483
|
let len = name.length;
|
|
521
484
|
for (; i < len; ++i) {
|
|
522
485
|
switch (name[i]) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
mightBeKeyword = false;
|
|
536
|
-
break;
|
|
537
|
-
}
|
|
538
|
-
default: {
|
|
539
|
-
let chNum = name.charCodeAt(i);
|
|
540
|
-
if (
|
|
541
|
-
!(chNum >= 97 && chNum <= 122) &&
|
|
542
|
-
!(chNum >= 65 && chNum <= 90)
|
|
543
|
-
) {
|
|
544
|
-
name = name.substr(0, i) + '_' + name.substr(i + 1);
|
|
486
|
+
case "0":
|
|
487
|
+
case "1":
|
|
488
|
+
case "2":
|
|
489
|
+
case "3":
|
|
490
|
+
case "4":
|
|
491
|
+
case "5":
|
|
492
|
+
case "6":
|
|
493
|
+
case "7":
|
|
494
|
+
case "8":
|
|
495
|
+
case "9":
|
|
496
|
+
case "$":
|
|
497
|
+
case "_": {
|
|
545
498
|
mightBeKeyword = false;
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
default: {
|
|
502
|
+
let chNum = name.charCodeAt(i);
|
|
503
|
+
if (!(chNum >= 97 && chNum <= 122) && !(chNum >= 65 && chNum <= 90)) {
|
|
504
|
+
name = name.substr(0, i) + "_" + name.substr(i + 1);
|
|
505
|
+
mightBeKeyword = false;
|
|
506
|
+
}
|
|
546
507
|
}
|
|
547
|
-
}
|
|
548
508
|
}
|
|
549
509
|
}
|
|
550
510
|
|
|
551
511
|
// Names must not collide with keywords
|
|
552
512
|
if (mightBeKeyword && len >= 2 && len <= 10) {
|
|
553
513
|
switch (name[0]) {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
514
|
+
case "a": {
|
|
515
|
+
if (name == "arguments") {
|
|
516
|
+
return name + "_";
|
|
517
|
+
}
|
|
518
|
+
break;
|
|
557
519
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
520
|
+
case "b": {
|
|
521
|
+
if (name == "break") {
|
|
522
|
+
return name + "_";
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
563
525
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
name == 'continue' ||
|
|
570
|
-
name == 'catch' ||
|
|
571
|
-
name == 'const' ||
|
|
572
|
-
name == 'class'
|
|
573
|
-
) {
|
|
574
|
-
return name + '_';
|
|
526
|
+
case "c": {
|
|
527
|
+
if (name == "case" || name == "continue" || name == "catch" || name == "const" || name == "class") {
|
|
528
|
+
return name + "_";
|
|
529
|
+
}
|
|
530
|
+
break;
|
|
575
531
|
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
532
|
+
case "d": {
|
|
533
|
+
if (name == "do" || name == "default" || name == "debugger") {
|
|
534
|
+
return name + "_";
|
|
535
|
+
}
|
|
536
|
+
break;
|
|
581
537
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
name ==
|
|
588
|
-
name ==
|
|
589
|
-
|
|
590
|
-
name
|
|
591
|
-
|
|
592
|
-
|
|
538
|
+
case "e": {
|
|
539
|
+
if (
|
|
540
|
+
name == "else" ||
|
|
541
|
+
name == "enum" ||
|
|
542
|
+
name == "eval" || // to be sure
|
|
543
|
+
name == "export" ||
|
|
544
|
+
name == "extends"
|
|
545
|
+
) {
|
|
546
|
+
return name + "_";
|
|
547
|
+
}
|
|
548
|
+
break;
|
|
593
549
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
name == 'false' ||
|
|
600
|
-
name == 'finally' ||
|
|
601
|
-
name == 'function'
|
|
602
|
-
) {
|
|
603
|
-
return name + '_';
|
|
550
|
+
case "f": {
|
|
551
|
+
if (name == "for" || name == "false" || name == "finally" || name == "function") {
|
|
552
|
+
return name + "_";
|
|
553
|
+
}
|
|
554
|
+
break;
|
|
604
555
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
name ==
|
|
611
|
-
name ==
|
|
612
|
-
name ==
|
|
613
|
-
|
|
614
|
-
name
|
|
615
|
-
|
|
616
|
-
|
|
556
|
+
case "i": {
|
|
557
|
+
if (
|
|
558
|
+
name == "if" ||
|
|
559
|
+
name == "in" ||
|
|
560
|
+
name == "import" ||
|
|
561
|
+
name == "interface" ||
|
|
562
|
+
name == "implements" ||
|
|
563
|
+
name == "instanceof"
|
|
564
|
+
) {
|
|
565
|
+
return name + "_";
|
|
566
|
+
}
|
|
567
|
+
break;
|
|
617
568
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
569
|
+
case "l": {
|
|
570
|
+
if (name == "let") {
|
|
571
|
+
return name + "_";
|
|
572
|
+
}
|
|
573
|
+
break;
|
|
623
574
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
575
|
+
case "n": {
|
|
576
|
+
if (name == "new" || name == "null") {
|
|
577
|
+
return name + "_";
|
|
578
|
+
}
|
|
579
|
+
break;
|
|
629
580
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
name == 'package' ||
|
|
636
|
-
name == 'private' ||
|
|
637
|
-
name == 'protected'
|
|
638
|
-
) {
|
|
639
|
-
return name + '_';
|
|
581
|
+
case "p": {
|
|
582
|
+
if (name == "public" || name == "package" || name == "private" || name == "protected") {
|
|
583
|
+
return name + "_";
|
|
584
|
+
}
|
|
585
|
+
break;
|
|
640
586
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
587
|
+
case "r": {
|
|
588
|
+
if (name == "return") {
|
|
589
|
+
return name + "_";
|
|
590
|
+
}
|
|
591
|
+
break;
|
|
646
592
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
593
|
+
case "s": {
|
|
594
|
+
if (name == "super" || name == "static" || name == "switch") {
|
|
595
|
+
return name + "_";
|
|
596
|
+
}
|
|
597
|
+
break;
|
|
652
598
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
name == 'this' ||
|
|
659
|
-
name == 'true' ||
|
|
660
|
-
name == 'throw' ||
|
|
661
|
-
name == 'typeof'
|
|
662
|
-
) {
|
|
663
|
-
return name + '_';
|
|
599
|
+
case "t": {
|
|
600
|
+
if (name == "try" || name == "this" || name == "true" || name == "throw" || name == "typeof") {
|
|
601
|
+
return name + "_";
|
|
602
|
+
}
|
|
603
|
+
break;
|
|
664
604
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
605
|
+
case "v": {
|
|
606
|
+
if (name == "var" || name == "void") {
|
|
607
|
+
return name + "_";
|
|
608
|
+
}
|
|
609
|
+
break;
|
|
670
610
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
611
|
+
case "w": {
|
|
612
|
+
if (name == "with" || name == "while") {
|
|
613
|
+
return name + "_";
|
|
614
|
+
}
|
|
615
|
+
break;
|
|
676
616
|
}
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
617
|
+
case "y": {
|
|
618
|
+
if (name == "yield") {
|
|
619
|
+
return name + "_";
|
|
620
|
+
}
|
|
621
|
+
break;
|
|
682
622
|
}
|
|
683
|
-
break;
|
|
684
|
-
}
|
|
685
623
|
}
|
|
686
624
|
}
|
|
687
625
|
return name;
|
|
688
626
|
}
|
|
689
627
|
|
|
690
628
|
// see: https://github.com/vitest-dev/vitest/issues/6953#issuecomment-2505310022
|
|
691
|
-
if (typeof __vite_ssr_import_meta__ !==
|
|
692
|
-
__vite_ssr_import_meta__.resolve = (path) =>
|
|
693
|
-
'file://' + globalCreateRequire(import.meta.url).resolve(path);
|
|
629
|
+
if (typeof __vite_ssr_import_meta__ !== "undefined") {
|
|
630
|
+
__vite_ssr_import_meta__.resolve = (path) => "file://" + globalCreateRequire(import.meta.url).resolve(path);
|
|
694
631
|
}
|