-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtranspile.js
775 lines (711 loc) · 24.4 KB
/
transpile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
import { platform } from 'node:process';
import { writeFile, stat } from 'node:fs/promises';
import { mkdir } from 'node:fs/promises';
import { dirname, extname, basename, resolve } from 'node:path';
import c from 'chalk-template';
import { minify } from 'terser';
import { fileURLToPath } from 'url';
import {
$init,
generate,
generateTypes,
} from '../../obj/js-component-bindgen-component.js';
import {
readFile,
sizeStr,
table,
spawnIOTmp,
setShowSpinner,
getShowSpinner,
} from '../common.js';
import { optimizeComponent } from './opt.js';
import { $init as wasmToolsInit, tools } from '../../obj/wasm-tools.js';
const { componentEmbed, componentNew } = tools;
import ora from '#ora';
const isWindows = platform === 'win32';
const ASYNC_WASI_IMPORTS = [
'wasi:io/poll#poll',
'wasi:io/poll#[method]pollable.block',
'wasi:io/streams#[method]input-stream.blocking-read',
'wasi:io/streams#[method]input-stream.blocking-skip',
'wasi:io/streams#[method]output-stream.blocking-flush',
'wasi:io/streams#[method]output-stream.blocking-write-and-flush',
'wasi:io/streams#[method]output-stream.blocking-write-zeroes-and-flush',
'wasi:io/streams#[method]output-stream.blocking-splice',
];
const ASYNC_WASI_EXPORTS = [
'wasi:cli/run#run',
'wasi:http/incoming-handler#handle',
];
export async function types(witPath, opts) {
const files = await typesComponent(witPath, opts);
await writeFiles(files, opts.quiet ? false : 'Generated Type Files');
}
export async function guestTypes(witPath, opts) {
const files = await typesComponent(witPath, { ...opts, guest: true });
await writeFiles(
files,
opts.quiet
? false
: 'Generated Guest Typescript Definition Files (.d.ts)'
);
}
/**
* @param {string} witPath
* @param {{
* name?: string,
* worldName?: string,
* instantiation?: 'async' | 'sync',
* tlaCompat?: bool,
* asyncMode?: string,
* asyncImports?: string[],
* asyncExports?: string[],
* outDir?: string,
* features?: string[] | 'all',
* guest?: bool,
* }} opts
* @returns {Promise<{ [filename: string]: Uint8Array }>}
*/
export async function typesComponent(witPath, opts) {
await $init;
const name =
opts.name ||
(opts.worldName
? opts.worldName.split(':').pop().split('/').pop()
: basename(witPath.slice(0, -extname(witPath).length || Infinity)));
let instantiation;
if (opts.instantiation) {
instantiation = { tag: opts.instantiation };
}
let outDir = (opts.outDir ?? '').replace(/\\/g, '/');
if (!outDir.endsWith('/') && outDir !== '') outDir += '/';
let features = null;
if (opts.allFeatures) {
features = { tag: 'all' };
} else if (Array.isArray(opts.feature)) {
features = { tag: 'list', val: opts.feature };
}
if (opts.asyncWasiImports)
opts.asyncImports = ASYNC_WASI_IMPORTS.concat(opts.asyncImports || []);
if (opts.asyncWasiExports)
opts.asyncExports = ASYNC_WASI_EXPORTS.concat(opts.asyncExports || []);
const asyncMode =
!opts.asyncMode || opts.asyncMode === 'sync'
? null
: {
tag: opts.asyncMode,
val: {
imports: opts.asyncImports || [],
exports: opts.asyncExports || [],
},
};
let types;
const absWitPath = resolve(witPath);
try {
types = generateTypes(name, {
wit: { tag: 'path', val: (isWindows ? '//?/' : '') + absWitPath },
instantiation,
tlaCompat: opts.tlaCompat ?? false,
world: opts.worldName,
features,
guest: opts.guest ?? false,
asyncMode,
}).map(([name, file]) => [`${outDir}${name}`, file]);
} catch (err) {
if (err.toString().includes('does not match previous package name')) {
const hint = await printWITLayoutHint(absWitPath);
if (err.message) {
err.message += `\n${hint}`;
}
throw err;
}
throw err;
}
return Object.fromEntries(types);
}
/**
* Print a hint about WIT folder layout
*
* @param {(string, any) => void} consoleFn
*/
async function printWITLayoutHint(witPath) {
const pathMeta = await stat(witPath);
let output = '\n';
if (!pathMeta.isFile() && !pathMeta.isDirectory()) {
output += c`{yellow.bold warning} The supplited WIT path [${witPath}] is neither a file or directory.\n`;
return output;
}
const ftype = pathMeta.isDirectory() ? 'directory' : 'file';
output += c`{yellow.bold warning} Your WIT ${ftype} [${witPath}] may be laid out incorrectly\n`;
output += c`{yellow.bold warning} Keep in mind the following rules:\n`;
output += c`{yellow.bold warning} - Top level WIT files are in the same package (i.e. "ns:pkg" in "wit/*.wit")\n`;
output += c`{yellow.bold warning} - All package dependencies should be in "wit/deps" (i.e. "some:dep" in "wit/deps/some-dep.wit"\n`;
return output;
}
async function writeFiles(files, summaryTitle) {
await Promise.all(
Object.entries(files).map(async ([name, file]) => {
await mkdir(dirname(name), { recursive: true });
await writeFile(name, file);
})
);
if (!summaryTitle) return;
console.log(c`
{bold ${summaryTitle}:}
${table(
Object.entries(files).map(([name, source]) => [
c` - {italic ${name}} `,
c`{black.italic ${sizeStr(source.length)}}`,
])
)}`);
}
export async function transpile(componentPath, opts, program) {
const varIdx = program?.parent.rawArgs.indexOf('--');
if (varIdx !== undefined && varIdx !== -1)
opts.optArgs = program.parent.rawArgs.slice(varIdx + 1);
let component;
if (!opts.stub) {
component = await readFile(componentPath);
} else {
await wasmToolsInit;
component = componentNew(
componentEmbed({
dummy: true,
witPath: (isWindows ? '//?/' : '') + resolve(componentPath),
}),
[]
);
}
if (!opts.quiet) setShowSpinner(true);
if (!opts.name)
opts.name = basename(
componentPath.slice(0, -extname(componentPath).length || Infinity)
);
if (opts.map)
opts.map = Object.fromEntries(
opts.map.map((mapping) => mapping.split('='))
);
if (opts.asyncWasiImports)
opts.asyncImports = ASYNC_WASI_IMPORTS.concat(opts.asyncImports || []);
if (opts.asyncWasiExports)
opts.asyncExports = ASYNC_WASI_EXPORTS.concat(opts.asyncExports || []);
const { files } = await transpileComponent(component, opts);
await writeFiles(
files,
opts.quiet ? false : 'Transpiled JS Component Files'
);
}
/**
* @param {Uint8Array} source
* @returns {Promise<Uint8Array>}
*/
async function wasm2Js(source) {
const wasm2jsPath = fileURLToPath(
import.meta.resolve('binaryen/bin/wasm2js')
);
try {
return await spawnIOTmp(wasm2jsPath, source, ['-Oz', '-o']);
} catch (e) {
if (e.toString().includes('BasicBlock requested'))
return wasm2Js(source);
throw e;
}
}
/**
*
* @param {Uint8Array} component
* @param {{
* name: string,
* instantiation?: 'async' | 'sync',
* importBindings?: 'js' | 'optimized' | 'hybrid' | 'direct-optimized',
* map?: Record<string, string>,
* asyncMode?: string,
* asyncImports?: string[],
* asyncExports?: string[],
* validLiftingOptimization?: bool,
* tracing?: bool,
* nodejsCompat?: bool,
* tlaCompat?: bool,
* base64Cutoff?: bool,
* js?: bool,
* minify?: bool,
* optimize?: bool,
* namespacedExports?: bool,
* outDir?: string,
* multiMemory?: bool,
* experimentalIdlImports?: bool,
* optArgs?: string[],
* }} opts
* @returns {Promise<{ files: { [filename: string]: Uint8Array }, imports: string[], exports: [string, 'function' | 'instance'][] }>}
*/
export async function transpileComponent(component, opts = {}) {
await $init;
if (opts.instantiation) opts.wasiShim = false;
let spinner;
const showSpinner = getShowSpinner();
if (opts.optimize) {
if (showSpinner) setShowSpinner(true);
({ component } = await optimizeComponent(component, opts));
}
if (opts.wasiShim !== false) {
opts.map = Object.assign(
{
'wasi:cli/*': '@bytecodealliance/preview2-shim/cli#*',
'wasi:clocks/*': '@bytecodealliance/preview2-shim/clocks#*',
'wasi:filesystem/*':
'@bytecodealliance/preview2-shim/filesystem#*',
'wasi:http/*': '@bytecodealliance/preview2-shim/http#*',
'wasi:io/*': '@bytecodealliance/preview2-shim/io#*',
'wasi:random/*': '@bytecodealliance/preview2-shim/random#*',
'wasi:sockets/*': '@bytecodealliance/preview2-shim/sockets#*',
},
opts.map || {}
);
}
let instantiation = null;
// Let's define `instantiation` from `--instantiation` if it's present.
if (opts.instantiation) {
instantiation = { tag: opts.instantiation };
}
// Otherwise, if `--js` is present, an `instantiate` function is required.
else if (opts.js) {
instantiation = { tag: 'async' };
}
const asyncMode =
!opts.asyncMode || opts.asyncMode === 'sync'
? null
: {
tag: opts.asyncMode,
val: {
imports: opts.asyncImports || [],
exports: opts.asyncExports || [],
},
};
let { files, imports, exports } = generate(component, {
name: opts.name ?? 'component',
map: Object.entries(opts.map ?? {}),
instantiation,
asyncMode,
importBindings: opts.importBindings
? { tag: opts.importBindings }
: null,
validLiftingOptimization: opts.validLiftingOptimization ?? false,
tracing: opts.tracing ?? false,
noNodejsCompat: opts.nodejsCompat === false,
noTypescript: opts.typescript === false,
tlaCompat: opts.tlaCompat ?? false,
base64Cutoff: opts.js ? 0 : (opts.base64Cutoff ?? 5000),
noNamespacedExports: opts.namespacedExports === false,
multiMemory: opts.multiMemory === true,
idlImports: opts.experimentalIdlImports === true,
});
let outDir = (opts.outDir ?? '').replace(/\\/g, '/');
if (!outDir.endsWith('/') && outDir !== '') outDir += '/';
files = files.map(([name, source]) => [`${outDir}${name}`, source]);
const jsFile = files.find(([name]) => name.endsWith('.js'));
// Generate code for the `--js` option.
//
// `--js` can be called with or without `--instantiation`. The generated code
// isn't exactly the same!
//
// `--js` needs an `instantiate` function to work, so it might look like
// `--instantiation` is always implied, but actually no. It is correct
// that when `--js` is present, an `instantiate` function _is_ generated,
// but it doesn't mean that we expect the function to be used, it's simply
// not exported, plus `instantiate` is automatically called (if `--tla-compat`
// is `false`). When `--instantiation` is missing, functions are exported
// with the `export` directive, and imports are imported with the `import`
// directive. When `--instantiation` is present, there is no `export` and no
// `import`: only a single exported `instantiate` function.
//
// Basically, we get this:
//
// * `--js` only:
// * `instantiate` is renamed to `_instantiate`,
// * A new `instantiate` function is created, that calls `_instantiate` with
// the correct imports (which are ASM.js code) and returns the exports,
// * A new `$init` function is created, that calls `instantiate` and maps
// the returned exports to their respective trampolines,
// * Trampolines are exported,
// * `$init` is called automatically.
//
// * `--js` with `--tla-compat`:
// * Same as with `--js` only, except that `$init` is exported instead of
// being called immediately.
//
// * `--js` with `--instantiation[=async]`:
// * `instantiate` is renamed to `_instantiate`,
// * A new `instantiate` function is created, that calls `_instantiate` with
// the correct imports (which are ASM.js code) and returns the exports,
// * `instantiate` is exported.
//
// * `--js` with `--instantiation=sync`:
// * Same as `--js` with `--instantiation[=async]`, except that
// `_instantiate` and `instantiate` are non-async.
//
// Be careful with the variables: `opts.instantiation` reflects the presence
// or the absence of the `--instantiation` flag, whilst `instantiation`
// reflects how the `instantiate` function must be generated. We also use
// `instantiation` to know whether the generated code must be async or
// non-async.
if (opts.js) {
const withInstantiation = opts.instantiation !== undefined;
const async_ = instantiation.tag == 'async' ? 'async ' : '';
const await_ = instantiation.tag == 'async' ? 'await ' : '';
// Format the previously generated code.
const source = Buffer.from(jsFile[1])
.toString('utf8')
// update imports manging to match emscripten asm
.replace(
/exports(\d+)\['([^']+)']/g,
(_, i, s) => `exports${i}['${asmMangle(s)}']`
)
.replace(
/export (async )?function instantiate/,
'$1function _instantiate'
);
// Collect all Wasm files.
const wasmFiles = files.filter(([name]) => name.endsWith('.wasm'));
files = files.filter(([name]) => !name.endsWith('.wasm'));
// Configure the spinner.
let completed = 0;
const spinnerText = () =>
c`{cyan ${completed} / ${wasmFiles.length}} Running Binaryen wasm2js on Wasm core modules (this takes a while)...\n`;
if (showSpinner) {
spinner = ora({
color: 'cyan',
spinner: 'bouncingBar',
}).start();
spinner.text = spinnerText();
}
// Compile all Wasm modules into ASM.js codes.
try {
const asmFiles = await Promise.all(
wasmFiles.map(async ([, source]) => {
const output = (await wasm2Js(source)).toString('utf8');
if (spinner) {
completed++;
spinner.text = spinnerText();
}
return output;
})
);
const asms = asmFiles
.map(
(asm, nth) => `function asm${nth}(imports) {
${
// strip and replace the asm instantiation wrapper
asm
.replace(/import \* as [^ ]+ from '[^']*';/g, '')
.replace('function asmFunc(imports) {', '')
.replace(/export var ([^ ]+) = ([^. ]+)\.([^ ]+);/g, '')
.replace(/var retasmFunc = [\s\S]*$/, '')
.replace(/var memasmFunc = new ArrayBuffer\(0\);/g, '')
.replace('memory.grow = __wasm_memory_grow;', '')
.trim()
}`
)
.join(',\n');
// The `instantiate` function.
const instantiateFunction = `${
withInstantiation ? 'export ' : ''
}${async_}function instantiate(imports) {
const wasm_file_to_asm_index = {
${wasmFiles
.map(([path], nth) => `'${basename(path)}': ${nth}`)
.join(',\n ')}
};
return ${await_}_instantiate(
module_name => wasm_file_to_asm_index[module_name],
imports,
(module_index, imports) => ({ exports: asmInit[module_index](imports) })
);
}`;
// If `--js` is used without `--instantiation`.
let importDirectives = '';
let exportDirectives = '';
let exportTrampolines = '';
let autoInstantiate = '';
if (!withInstantiation) {
importDirectives = imports
.map(
(import_file, nth) =>
`import * as import${nth} from '${import_file}';`
)
.join('\n');
if (exports.length > 0 || opts.tlaCompat) {
exportDirectives = `export {
${
// Exporting `$init` must come first to not break the transpiling tests.
opts.tlaCompat ? ' $init,\n' : ''
}${exports
.map(([name]) => {
if (name === asmMangle(name)) {
return ` ${name},`;
} else {
return ` ${asmMangle(name)} as '${name}',`;
}
})
.join('\n')}
}`;
}
exportTrampolines = `let ${exports
.filter(([, ty]) => ty === 'function')
.map(([name]) => `_${asmMangle(name)}`)
.join(', ')};
${exports
.map(([name, ty]) => {
if (ty === 'function') {
return `\nfunction ${asmMangle(name)} () {
return _${asmMangle(name)}.apply(this, arguments);
}`;
} else {
return `\nlet ${asmMangle(name)};`;
}
})
.join('\n')}`;
autoInstantiate = `${async_}function $init() {
( {
${exports
.map(([name, ty]) => {
if (ty === 'function') {
return ` '${name}': _${asmMangle(name)},`;
} else if (asmMangle(name) === name) {
return ` ${name},`;
} else {
return ` '${name}': ${asmMangle(name)},`;
}
})
.join('\n')}
} = ${await_}instantiate(
{
${imports
.map((import_file, nth) => ` '${import_file}': import${nth},`)
.join('\n')}
}
) )
}
${opts.tlaCompat ? '' : `${await_}$init();`}`;
}
// Prepare the final generated code.
const outSource = `${importDirectives}
${source}
const asmInit = [${asms}];
${exportTrampolines}
${instantiateFunction}
${exportDirectives}
${autoInstantiate}`;
// Save the final generated code.
jsFile[1] = Buffer.from(outSource);
} finally {
if (spinner) {
spinner.stop();
}
}
}
if (opts.minify) {
({ code: jsFile[1] } = await minify(
Buffer.from(jsFile[1]).toString('utf8'),
{
module: true,
compress: {
ecma: 9,
unsafe: true,
},
mangle: {
keep_classnames: true,
},
}
));
}
return { files: Object.fromEntries(files), imports, exports };
}
// emscripten asm mangles specifiers to be valid identifiers
// for imports to match up we must do the same
// See https://github.com/WebAssembly/binaryen/blob/main/src/asmjs/asmangle.cpp
function asmMangle(name) {
if (name === '') return '$';
let mightBeKeyword = true;
let i = 1;
// Names must start with a character, $ or _
switch (name[0]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
name = '$' + name;
i = 2;
// fallthrough
}
case '$':
case '_': {
mightBeKeyword = false;
break;
}
default: {
let chNum = name.charCodeAt(0);
if (
!(chNum >= 97 && chNum <= 122) &&
!(chNum >= 65 && chNum <= 90)
) {
name = '$' + name.substr(1);
mightBeKeyword = false;
}
}
}
// Names must contain only characters, digits, $ or _
let len = name.length;
for (; i < len; ++i) {
switch (name[i]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '$':
case '_': {
mightBeKeyword = false;
break;
}
default: {
let chNum = name.charCodeAt(i);
if (
!(chNum >= 97 && chNum <= 122) &&
!(chNum >= 65 && chNum <= 90)
) {
name = name.substr(0, i) + '_' + name.substr(i + 1);
mightBeKeyword = false;
}
}
}
}
// Names must not collide with keywords
if (mightBeKeyword && len >= 2 && len <= 10) {
switch (name[0]) {
case 'a': {
if (name == 'arguments') return name + '_';
break;
}
case 'b': {
if (name == 'break') return name + '_';
break;
}
case 'c': {
if (
name == 'case' ||
name == 'continue' ||
name == 'catch' ||
name == 'const' ||
name == 'class'
)
return name + '_';
break;
}
case 'd': {
if (name == 'do' || name == 'default' || name == 'debugger')
return name + '_';
break;
}
case 'e': {
if (
name == 'else' ||
name == 'enum' ||
name == 'eval' || // to be sure
name == 'export' ||
name == 'extends'
)
return name + '_';
break;
}
case 'f': {
if (
name == 'for' ||
name == 'false' ||
name == 'finally' ||
name == 'function'
)
return name + '_';
break;
}
case 'i': {
if (
name == 'if' ||
name == 'in' ||
name == 'import' ||
name == 'interface' ||
name == 'implements' ||
name == 'instanceof'
)
return name + '_';
break;
}
case 'l': {
if (name == 'let') return name + '_';
break;
}
case 'n': {
if (name == 'new' || name == 'null') return name + '_';
break;
}
case 'p': {
if (
name == 'public' ||
name == 'package' ||
name == 'private' ||
name == 'protected'
)
return name + '_';
break;
}
case 'r': {
if (name == 'return') return name + '_';
break;
}
case 's': {
if (name == 'super' || name == 'static' || name == 'switch')
return name + '_';
break;
}
case 't': {
if (
name == 'try' ||
name == 'this' ||
name == 'true' ||
name == 'throw' ||
name == 'typeof'
)
return name + '_';
break;
}
case 'v': {
if (name == 'var' || name == 'void') return name + '_';
break;
}
case 'w': {
if (name == 'with' || name == 'while') return name + '_';
break;
}
case 'y': {
if (name == 'yield') return name + '_';
break;
}
}
}
return name;
}
// see: https://github.com/vitest-dev/vitest/issues/6953#issuecomment-2505310022
if (typeof __vite_ssr_import_meta__ !== 'undefined') {
__vite_ssr_import_meta__.resolve = (path) =>
'file://' + globalCreateRequire(import.meta.url).resolve(path);
}