forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1288 lines (1178 loc) · 42.9 KB
/
index.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2020 Daniel Wirtz / The AssemblyScript Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Compiler frontend for node.js
*
* Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
* JavaScript as well as the compiler compiled to WebAssembly (eventually).
*
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
* in the build step. See dist/asc.js for the bundle.
*/
import { fs, module, path, process, url } from "../util/node.js";
import { Colors } from "../util/terminal.js";
import { utf8 } from "../util/text.js";
import * as optionsUtil from "../util/options.js";
import * as generated from "./index.generated.js";
import binaryen from "../lib/binaryen.js";
import * as assemblyscriptJS from "assemblyscript";
// Use the TS->JS variant by default
let assemblyscript = assemblyscriptJS;
// Use the AS->Wasm variant as an option (experimental)
const wasmPos = process.argv.indexOf("--wasm");
if (~wasmPos) {
const wasmPath = String(process.argv[wasmPos + 1]);
process.argv.splice(wasmPos, 2);
assemblyscript = await import(new URL(wasmPath, url.pathToFileURL(process.cwd() + "/")));
}
const require = module.createRequire(import.meta.url);
const WIN = process.platform === "win32";
const EOL = WIN ? "\r\n" : "\n";
const SEP = WIN ? "\\" : "/";
const extension = ".ts";
const extension_d = `.d${extension}`;
const extension_re = new RegExp("\\" + extension + "$");
const extension_re_except_d = new RegExp("^(?!.*\\.d\\" + extension + "$).*\\" + extension + "$");
function toUpperSnakeCase(str) {
return str.replace(/-/g, "_").toUpperCase();
}
function isNonEmptyString(value) {
return typeof value === "string" && value !== "";
}
/** Ensures that an object is a wrapper class instead of just a pointer. */
// function __wrap(ptrOrObj, wrapperClass) {
// if (typeof ptrOrObj === "number") {
// return ptrOrObj === 0 ? null : wrapperClass.wrap(ptrOrObj);
// }
// return ptrOrObj;
// }
/** AssemblyScript version. */
export const version = generated.version;
/** Available CLI options. */
export const options = generated.options;
/** Prefix used for library files. */
export const libraryPrefix = generated.libraryPrefix;
/** Bundled library files. */
export const libraryFiles = generated.libraryFiles;
/** Bundled definition files. */
export const definitionFiles = generated.definitionFiles;
/** Default Binaryen optimization level. */
export const defaultOptimizeLevel = 3;
/** Default Binaryen shrink level. */
export const defaultShrinkLevel = 0;
/** Converts a configuration object to an arguments array. */
export function configToArguments(options, argv = []) {
Object.keys(options || {}).forEach(key => {
const val = options[key];
const opt = generated.options[key];
if (opt && opt.type === "b") {
if (val) argv.push(`--${key}`);
} else {
if (Array.isArray(val)) {
val.forEach(val => { argv.push(`--${key}`, String(val)); });
}
else argv.push(`--${key}`, String(val));
}
});
return argv;
}
/** Convenience function that parses and compiles source strings directly. */
export async function compileString(sources, config = {}) {
if (typeof sources === "string") sources = { [`input${extension}`]: sources };
let argv = [
"--outFile", "binary",
"--textFile", "text",
];
configToArguments(config, argv);
const output = {};
const result = await main(argv.concat(Object.keys(sources)), {
readFile: name => Object.prototype.hasOwnProperty.call(sources, name) ? sources[name] : null,
writeFile: (name, contents) => { output[name] = contents; },
listFiles: () => []
});
return Object.assign(result, output);
}
/** Runs the command line utility using the specified arguments array. */
export async function main(argv, options) {
if (!Array.isArray(argv)) argv = configToArguments(argv);
if (!options) options = {};
const stats = options.stats || new Stats();
const statsBegin = stats.begin();
// Bundle semantic version
let bundleMinorVersion = 0, bundleMajorVersion = 0, bundlePatchVersion = 0;
const versionParts = (version || "").split(".");
if (versionParts.length === 3) {
bundleMajorVersion = parseInt(versionParts[0]) | 0;
bundleMinorVersion = parseInt(versionParts[1]) | 0;
bundlePatchVersion = parseInt(versionParts[2]) | 0;
}
const stdout = options.stdout || createMemoryStream();
const stderr = options.stderr || createMemoryStream();
const readFile = options.readFile || readFileNode;
const writeFile = options.writeFile || writeFileNode;
const listFiles = options.listFiles || listFilesNode;
// Parse command line options but do not populate option defaults yet
const optionsResult = optionsUtil.parse(argv, generated.options, false);
let opts = optionsResult.options;
argv = optionsResult.arguments;
const stdoutColors = new Colors(stdout);
const stderrColors = new Colors(stderr);
if (opts.noColors) {
stdoutColors.enabled = false;
stderrColors.enabled = false;
}
// Check for unknown options
const unknownOpts = optionsResult.unknown;
if (unknownOpts.length) {
unknownOpts.forEach(arg => {
stderr.write(
`${stderrColors.yellow("WARNING ")}Unknown option '${arg}'${EOL}`
);
});
}
// Check for trailing arguments
const trailingArgv = optionsResult.trailing;
if (trailingArgv.length) {
stderr.write(
`${stderrColors.yellow("WARNING ")}Unsupported trailing arguments: ${trailingArgv.join(" ")}${EOL}`
);
}
let module = null;
let binaryenModule = null;
// Prepares the result object
let prepareResult = (error, result = {}) => {
if (error) {
stderr.write(`${stderrColors.red("FAILURE ")}${error.stack.replace(/^ERROR: /i, "")}${EOL}`);
}
if (binaryenModule) binaryenModule.dispose();
if (!stats.total) stats.total = stats.end(statsBegin);
return Object.assign({ error, stdout, stderr, stats }, result);
};
// Just print the version if requested
if (opts.version) {
stdout.write(`Version ${version}${EOL}`);
return prepareResult(null);
}
// Set up base directory
const baseDir = path.normalize(opts.baseDir || ".");
// Check if a config file is present
let configPath = optionsUtil.resolvePath(opts.config || "asconfig.json", baseDir);
let configFile = path.basename(configPath);
let configDir = path.dirname(configPath);
let config = await getConfig(configFile, configDir, readFile);
let configHasEntries = config != null && Array.isArray(config.entries) && config.entries.length;
// Print the help message if requested or no source files are provided
if (opts.help || (!argv.length && !configHasEntries)) {
let out = opts.help ? stdout : stderr;
let colors = opts.help ? stdoutColors : stderrColors;
out.write([
colors.white("SYNTAX"),
" " + colors.cyan("asc") + " [entryFile ...] [options]",
"",
colors.white("EXAMPLES"),
" " + colors.cyan("asc") + " hello" + extension,
" " + colors.cyan("asc") + " hello" + extension + " -o hello.wasm -t hello.wat",
" " + colors.cyan("asc") + " hello1" + extension + " hello2" + extension + " -o -O > hello.wasm",
" " + colors.cyan("asc") + " --config asconfig.json --target release",
"",
colors.white("OPTIONS"),
].concat(
optionsUtil.help(generated.options, 24, EOL)
).join(EOL) + EOL);
return prepareResult(null);
}
// I/O must be specified if not present in the environment
if (!(fs.promises && fs.promises.readFile)) {
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
}
// Load additional options from asconfig.json
const seenAsconfig = new Set();
seenAsconfig.add(configPath);
const target = opts.target || "release";
while (config) {
// Merge target first
if (config.targets) {
const targetOptions = config.targets[target];
if (targetOptions) {
opts = optionsUtil.merge(generated.options, opts, targetOptions, configDir);
}
}
// Merge general options
const generalOptions = config.options;
if (generalOptions) {
opts = optionsUtil.merge(generated.options, opts, generalOptions, configDir);
}
// Append entries
if (config.entries) {
for (let entry of config.entries) {
argv.push(optionsUtil.resolvePath(entry, configDir));
}
}
// Look up extended asconfig and repeat
if (config.extends) {
configPath = optionsUtil.resolvePath(config.extends, configDir, true);
configFile = path.basename(configPath);
configDir = path.dirname(configPath);
if (seenAsconfig.has(configPath)) break;
seenAsconfig.add(configPath);
config = await getConfig(configFile, configDir, readFile);
} else {
break;
}
}
// Populate option defaults once user-defined options are set
optionsUtil.addDefaults(generated.options, opts);
// If showConfig print options and exit
if (opts.showConfig) {
stderr.write(JSON.stringify({
options: opts,
entries: argv
}, null, 2));
return prepareResult(null);
}
// create a unique set of values
function unique(values) {
return [...new Set(values)];
}
// Set up options
let program, runtime, uncheckedBehavior;
const compilerOptions = assemblyscript.newOptions();
switch (opts.runtime) {
case "stub": runtime = 0; break;
case "minimal": runtime = 1; break;
/* incremental */
default: runtime = 2; break;
}
switch (opts.uncheckedBehavior) {
/* default */
default: uncheckedBehavior = 0; break;
case "never": uncheckedBehavior = 1; break;
case "always": uncheckedBehavior = 2; break;
}
assemblyscript.setTarget(compilerOptions, 0);
assemblyscript.setDebugInfo(compilerOptions, !!opts.debug);
assemblyscript.setRuntime(compilerOptions, runtime);
assemblyscript.setNoAssert(compilerOptions, opts.noAssert);
assemblyscript.setExportMemory(compilerOptions, !opts.noExportMemory);
assemblyscript.setImportMemory(compilerOptions, opts.importMemory);
assemblyscript.setInitialMemory(compilerOptions, opts.initialMemory >>> 0);
assemblyscript.setMaximumMemory(compilerOptions, opts.maximumMemory >>> 0);
assemblyscript.setSharedMemory(compilerOptions, opts.sharedMemory);
assemblyscript.setImportTable(compilerOptions, opts.importTable);
assemblyscript.setExportTable(compilerOptions, opts.exportTable);
if (opts.exportStart != null) {
assemblyscript.setExportStart(compilerOptions, isNonEmptyString(opts.exportStart) ? opts.exportStart : "_start");
}
assemblyscript.setMemoryBase(compilerOptions, opts.memoryBase >>> 0);
assemblyscript.setTableBase(compilerOptions, opts.tableBase >>> 0);
assemblyscript.setSourceMap(compilerOptions, opts.sourceMap != null);
assemblyscript.setUncheckedBehavior(compilerOptions, uncheckedBehavior);
assemblyscript.setNoUnsafe(compilerOptions, opts.noUnsafe);
assemblyscript.setPedantic(compilerOptions, opts.pedantic);
assemblyscript.setLowMemoryLimit(compilerOptions, opts.lowMemoryLimit >>> 0);
assemblyscript.setExportRuntime(compilerOptions, opts.exportRuntime);
assemblyscript.setBundleVersion(compilerOptions, bundleMajorVersion, bundleMinorVersion, bundlePatchVersion);
if (!opts.stackSize && runtime === 2 /* incremental */) {
opts.stackSize = assemblyscript.DEFAULT_STACK_SIZE;
}
assemblyscript.setStackSize(compilerOptions, opts.stackSize);
assemblyscript.setBindingsHint(compilerOptions, opts.bindings && opts.bindings.length > 0);
// Instrument callback to perform GC
// prepareResult = (original => {
// return function gcBeforePrepareResult(err) {
// __unpin(compilerOptions);
// if (program) __unpin(program);
// __collect();
// return original(err);
// };
// })(prepareResult);
// Add or override aliases if specified
if (opts.use) {
let aliases = opts.use;
for (let i = 0, k = aliases.length; i < k; ++i) {
let part = aliases[i];
let p = part.indexOf("=");
if (p < 0) return prepareResult(Error(`Global alias '${part}' is invalid.`));
let alias = part.substring(0, p).trim();
let name = part.substring(p + 1).trim();
if (!alias.length) {
return prepareResult(Error(`Global alias '${part}' is invalid.`));
}
assemblyscript.addGlobalAlias(compilerOptions, alias, name);
}
}
// Disable default features if specified
let features;
if ((features = opts.disable) != null) {
if (typeof features === "string") features = features.split(",");
for (let i = 0, k = features.length; i < k; ++i) {
let name = features[i].trim();
let flag = assemblyscript[`FEATURE_${toUpperSnakeCase(name)}`];
if (!flag) return prepareResult(Error(`Feature '${name}' is unknown.`));
assemblyscript.setFeature(compilerOptions, flag, false);
}
}
// Enable experimental features if specified
if ((features = opts.enable) != null) {
if (typeof features === "string") features = features.split(",");
for (let i = 0, k = features.length; i < k; ++i) {
let name = features[i].trim();
let flag = assemblyscript[`FEATURE_${toUpperSnakeCase(name)}`];
if (!flag) return prepareResult(Error(`Feature '${name}' is unknown.`));
assemblyscript.setFeature(compilerOptions, flag, true);
}
}
// Set up optimization levels
let optimizeLevel = 0;
let shrinkLevel = 0;
if (opts.optimize) {
optimizeLevel = defaultOptimizeLevel;
shrinkLevel = defaultShrinkLevel;
}
if (typeof opts.optimizeLevel === "number") optimizeLevel = opts.optimizeLevel;
if (typeof opts.shrinkLevel === "number") shrinkLevel = opts.shrinkLevel;
optimizeLevel = Math.min(Math.max(optimizeLevel, 0), 3);
shrinkLevel = Math.min(Math.max(shrinkLevel, 0), 2);
assemblyscript.setOptimizeLevelHints(compilerOptions, optimizeLevel, shrinkLevel);
// Initialize the program
program = assemblyscript.newProgram(compilerOptions);
// Collect transforms *constructors* from the `--transform` CLI flag as well
// as the `transform` option into the `transforms` array.
let transforms = [];
// `transform` option from `main()`
if (Array.isArray(options.transforms)) {
transforms.push(...options.transforms);
}
// `--transform` CLI flag
if (opts.transform) {
let transformArgs = unique(opts.transform);
for (let i = 0, k = transformArgs.length; i < k; ++i) {
let filename = transformArgs[i].trim();
let resolved;
let transform;
if (require.resolve) {
try {
resolved = require.resolve(filename, { paths: [process.cwd(), baseDir] });
transform = await import(url.pathToFileURL(resolved));
if (transform.default) transform = transform.default;
} catch (e1) {
try {
transform = require(resolved);
} catch (e2) {
return prepareResult(e1);
}
}
} else {
try {
transform = await import(new URL(filename, import.meta.url));
if (transform.default) transform = transform.default;
} catch (e) {
return prepareResult(e);
}
}
if (!transform || (typeof transform !== "function" && typeof transform !== "object")) {
return prepareResult(Error("not a transform: " + transformArgs[i]));
}
transforms.push(transform);
}
}
// Fix up the prototype of the transforms’ constructors and instantiate them.
try {
transforms = transforms.map(transform => {
if (typeof transform === "function") {
Object.assign(transform.prototype, {
program,
baseDir,
stdout,
stderr,
log: console.error,
readFile,
writeFile,
listFiles
});
transform = new transform();
}
return transform;
});
} catch (e) {
return prepareResult(e);
}
async function applyTransform(name, ...args) {
for (let i = 0, k = transforms.length; i < k; ++i) {
let transform = transforms[i];
if (typeof transform[name] === "function") {
try {
let start = stats.begin();
stats.transformCount++;
await transform[name](...args);
stats.transformTime += stats.end(start);
} catch (e) {
return e;
}
}
}
}
// Parse library files
Object.keys(libraryFiles).forEach(libPath => {
if (libPath.includes("/")) return; // in sub-directory: imported on demand
let begin = stats.begin();
stats.parseCount++;
assemblyscript.parse(program, libraryFiles[libPath], libraryPrefix + libPath + extension, false);
stats.parseTime += stats.end(begin);
});
let customLibDirs = [];
if (opts.lib) {
let lib = opts.lib;
if (typeof lib === "string") lib = lib.split(",");
customLibDirs.push(...lib.map(p => p.trim()));
customLibDirs = unique(customLibDirs); // `lib` and `customLibDirs` may include duplicates
for (let i = 0, k = customLibDirs.length; i < k; ++i) { // custom
let libDir = customLibDirs[i];
let libFiles;
if (libDir.endsWith(extension)) {
libFiles = [ path.basename(libDir) ];
libDir = path.dirname(libDir);
} else {
libFiles = await listFiles(libDir, baseDir) || [];
}
for (let libPath of libFiles) {
let libText = await readFile(libPath, libDir);
if (libText == null) {
return prepareResult(Error(`Library file '${libPath}' not found.`));
}
libraryFiles[libPath.replace(extension_re, "")] = libText;
let begin = stats.begin();
stats.parseCount++;
assemblyscript.parse(program, libText, libraryPrefix + libPath, false);
stats.parseTime += stats.end(begin);
}
}
}
opts.path = opts.path || [];
// Maps package names to parent directory
const packageBases = new Map();
// Gets the file matching the specified source path, imported at the given dependee path
async function getFile(internalPath, dependeePath) {
let sourceText = null; // text reported back to the compiler
let sourcePath = null; // path reported back to the compiler
// Try file.ext, file/index.ext, file.d.ext
if (!internalPath.startsWith(libraryPrefix)) {
if ((sourceText = await readFile(sourcePath = internalPath + extension, baseDir)) == null) {
if ((sourceText = await readFile(sourcePath = internalPath + "/index" + extension, baseDir)) == null) {
// portable d.ext: uses the .js file next to it in JS or becomes an import in Wasm
sourcePath = internalPath + extension;
sourceText = await readFile(internalPath + extension_d, baseDir);
}
}
// Search library in this order: stdlib, custom lib dirs, paths
} else {
const plainName = internalPath.substring(libraryPrefix.length);
const indexName = `${plainName}/index`;
if (Object.prototype.hasOwnProperty.call(libraryFiles, plainName)) {
sourceText = libraryFiles[plainName];
sourcePath = libraryPrefix + plainName + extension;
} else if (Object.prototype.hasOwnProperty.call(libraryFiles, indexName)) {
sourceText = libraryFiles[indexName];
sourcePath = libraryPrefix + indexName + extension;
} else { // custom lib dirs
for (const libDir of customLibDirs) {
if ((sourceText = await readFile(plainName + extension, libDir)) != null) {
sourcePath = libraryPrefix + plainName + extension;
break;
} else {
if ((sourceText = await readFile(indexName + extension, libDir)) != null) {
sourcePath = libraryPrefix + indexName + extension;
break;
}
}
}
if (sourceText == null) { // paths
const match = internalPath.match(/^~lib\/((?:@[^/]+\/)?[^/]+)(?:\/(.+))?/); // ~lib/(pkg)/(path), ~lib/(@org/pkg)/(path)
if (match) {
const packageName = match[1];
const filePath = match[2] || "index";
const basePath = packageBases.has(dependeePath) ? packageBases.get(dependeePath) : ".";
const paths = [];
const parts = path.resolve(baseDir, basePath).split(SEP);
for (let i = parts.length, k = WIN ? 1 : 0; i >= k; --i) {
if (parts[i - 1] !== "node_modules") {
paths.push(`${parts.slice(0, i).join(SEP)}${SEP}node_modules`);
}
}
paths.push(...opts.path);
for (const currentDir of paths.map(p => path.relative(baseDir, p))) {
const plainName = filePath;
if ((sourceText = await readFile(path.join(currentDir, packageName, plainName + extension), baseDir)) != null) {
sourcePath = `${libraryPrefix}${packageName}/${plainName}${extension}`;
packageBases.set(sourcePath.replace(extension_re, ""), path.join(currentDir, packageName));
break;
}
const indexName = `${filePath}/index`;
if ((sourceText = await readFile(path.join(currentDir, packageName, indexName + extension), baseDir)) != null) {
sourcePath = `${libraryPrefix}${packageName}/${indexName}${extension}`;
packageBases.set(sourcePath.replace(extension_re, ""), path.join(currentDir, packageName));
break;
}
}
}
}
}
}
// No such file
if (sourceText == null) return null;
return { sourceText, sourcePath };
}
// Gets all pending imported files from the the backlog
function getBacklog(paths = []) {
do {
let internalPath = assemblyscript.nextFile(program);
if (internalPath == null) break;
paths.push(internalPath);
} while (true);
return paths;
}
// Parses the backlog of imported files after including entry files
async function parseBacklog() {
let backlog;
while ((backlog = getBacklog()).length) {
let files = [];
for (let internalPath of backlog) {
const dependee = assemblyscript.getDependee(program, internalPath);
files.push(getFile(internalPath, dependee)); // queue
}
files = await Promise.all(files); // parallel
for (let i = 0, k = backlog.length; i < k; ++i) {
const internalPath = backlog[i];
const file = files[i];
const begin = stats.begin();
stats.parseCount++;
if (file) {
assemblyscript.parse(program, file.sourceText, file.sourcePath, false);
} else {
assemblyscript.parse(program, null, internalPath + extension, false);
}
stats.parseTime += stats.end(begin);
}
}
const numErrors = checkDiagnostics(program, stderr, opts.disableWarning, options.reportDiagnostic, stderrColors.enabled);
if (numErrors) {
const err = Error(`${numErrors} parse error(s)`);
err.stack = err.message; // omit stack
return prepareResult(err);
}
}
// Include runtime before entry files so its setup runs first
{
let runtimeName = String(opts.runtime);
let runtimePath = `rt/index-${runtimeName}`;
let runtimeText = libraryFiles[runtimePath];
if (runtimeText == null) {
runtimePath = runtimeName;
runtimeText = await readFile(runtimePath + extension, baseDir);
if (runtimeText == null) return prepareResult(Error(`Runtime '${path.resolve(baseDir, runtimePath + extension)}' is not found.`));
} else {
runtimePath = `~lib/${runtimePath}`;
}
let begin = stats.begin();
stats.parseCount++;
assemblyscript.parse(program, runtimeText, runtimePath + extension, true);
stats.parseTime += stats.end(begin);
}
// Include entry files
for (let i = 0, k = argv.length; i < k; ++i) {
const filename = String(argv[i]);
// Setting the path to relative path
let sourcePath = path.isAbsolute(filename)
? path.relative(baseDir, filename)
: path.normalize(filename);
sourcePath = sourcePath
.replace(/\\/g, "/")
.replace(extension_re, "")
.replace(/\/$/, "");
// Try entryPath.ext, then entryPath/index.ext
let sourceText = await readFile(sourcePath + extension, baseDir);
if (sourceText == null) {
const path = `${sourcePath}/index${extension}`;
sourceText = await readFile(path, baseDir);
if (sourceText != null) sourcePath = path;
else sourcePath += extension;
} else {
sourcePath += extension;
}
let begin = stats.begin();
stats.parseCount++;
assemblyscript.parse(program, sourceText, sourcePath, true);
stats.parseTime += stats.end(begin);
}
// Parse entry files
{
let code = await parseBacklog();
if (code) return code;
}
// Call afterParse transform hook
{
let error = await applyTransform("afterParse", program.parser);
if (error) return prepareResult(error);
}
// Parse additional files, if any
{
let code = await parseBacklog();
if (code) return code;
}
// Pre-emptively initialize the program
{
let begin = stats.begin();
stats.initializeCount++;
try {
assemblyscript.initializeProgram(program);
} catch (e) {
crash("initialize", e);
}
stats.initializeTime += stats.end(begin);
}
// Call afterInitialize transform hook
{
let error = await applyTransform("afterInitialize", program);
if (error) return prepareResult(error);
}
// Compile the program
{
let begin = stats.begin();
stats.compileCount++;
try {
module = assemblyscript.compile(program);
} catch (e) {
crash("compile", e);
}
stats.compileTime += stats.end(begin);
}
// From here on we are going to use Binaryen.js
binaryenModule = binaryen.wrapModule(
typeof module === "number" || module instanceof Number
? assemblyscript.getBinaryenModuleRef(module)
: module.ref
);
let numErrors = checkDiagnostics(program, stderr, opts.disableWarning, options.reportDiagnostic, stderrColors.enabled);
if (numErrors) {
const err = Error(`${numErrors} compile error(s)`);
err.stack = err.message; // omit stack
return prepareResult(err);
}
// Call afterCompile transform hook
{
let error = await applyTransform("afterCompile", binaryenModule);
if (error) return prepareResult(error);
}
numErrors = checkDiagnostics(program, stderr, opts.disableWarning, options.reportDiagnostic, stderrColors.enabled);
if (numErrors) {
const err = Error(`${numErrors} afterCompile error(s)`);
err.stack = err.message; // omit stack
return prepareResult(err);
}
// Validate the module if requested
if (!opts.noValidate) {
let begin = stats.begin();
stats.validateCount++;
let isValid = assemblyscript.validate(module);
stats.validateTime += stats.end(begin);
if (!isValid) {
return prepareResult(Error("validate error"));
}
}
// Set Binaryen-specific options
if (opts.trapMode === "clamp" || opts.trapMode === "js") {
let begin = stats.begin();
try {
binaryenModule.runPasses([`trap-mode-${opts.trapMode}`]);
} catch (e) {
crash("runPasses", e);
}
stats.compileTime += stats.end(begin);
} else if (opts.trapMode !== "allow") {
return prepareResult(Error("Unsupported trap mode"));
}
// Optimize the module
const debugInfo = opts.debug;
const converge = opts.converge;
const zeroFilledMemory = opts.importMemory
? opts.zeroFilledMemory
: false;
const runPasses = [];
if (opts.runPasses) {
if (typeof opts.runPasses === "string") {
opts.runPasses = opts.runPasses.split(",");
}
if (opts.runPasses.length) {
opts.runPasses.forEach(pass => {
if (!runPasses.includes(pass = pass.trim())) {
runPasses.push(pass);
}
});
}
}
{
let begin = stats.begin();
try {
stats.optimizeCount++;
assemblyscript.optimize(module, optimizeLevel, shrinkLevel, debugInfo, zeroFilledMemory);
} catch (e) {
crash("optimize", e);
}
try {
binaryenModule.runPasses(runPasses);
} catch (e) {
crash("runPasses", e);
}
if (converge) {
let last;
try {
let begin = stats.begin();
stats.emitCount++;
last = binaryenModule.emitBinary();
stats.emitTime += stats.end(begin);
} catch (e) {
crash("emitBinary (converge)", e);
}
do {
try {
stats.optimizeCount++;
assemblyscript.optimize(module, optimizeLevel, shrinkLevel, debugInfo, zeroFilledMemory);
} catch (e) {
crash("optimize (converge)", e);
}
try {
binaryenModule.runPasses(runPasses);
} catch (e) {
crash("runPasses (converge)", e);
}
let next;
try {
let begin = stats.begin();
stats.emitCount++;
next = binaryenModule.emitBinary();
stats.emitTime += stats.end(begin);
} catch (e) {
crash("emitBinary (converge)", e);
}
if (next.length >= last.length) {
if (next.length > last.length) {
stderr.write(`Last converge was suboptimal.${EOL}`);
}
break;
}
last = next;
} while (true);
}
stats.optimizeTime += stats.end(begin);
}
const pending = [];
// Prepare output
if (!opts.noEmit) {
if (opts.binaryFile) {
// We catched lagacy field for binary output (before 0.20)
return prepareResult(Error("Usage of the --binaryFile compiler option is no longer supported. Use --outFile instead."));
}
let bindings = opts.bindings || [];
let hasStdout = false;
let hasOutFile = opts.outFile != null;
let hasTextFile = opts.textFile != null;
let hasOutput = hasOutFile || hasTextFile;
let hasFileOutput = (hasOutFile && opts.outFile.length > 0) || (hasTextFile && opts.textFile.length > 0);
let basepath = hasFileOutput
? (opts.outFile || opts.textFile).replace(/\.\w+$/, "")
: null;
let basename = hasFileOutput
? path.basename(basepath)
: "output";
assemblyscript.setBasenameHint(compilerOptions, basename);
// Write binary
if (opts.outFile != null) {
let sourceMapURL = opts.sourceMap != null
? opts.sourceMap.length
? opts.sourceMap
: `./${basename}.wasm.map`
: null;
let begin = stats.begin();
stats.emitCount++;
let wasm;
try {
wasm = binaryenModule.emitBinary(sourceMapURL);
} catch (e) {
crash("emitBinary", e);
}
stats.emitTime += stats.end(begin);
if (opts.outFile.length) {
pending.push(
writeFile(opts.outFile, wasm.binary, baseDir)
);
} else {
hasStdout = true;
writeStdout(wasm.binary);
}
// Post-process source map
if (wasm.sourceMap != "") {
if (opts.outFile.length) {
let map = JSON.parse(wasm.sourceMap);
map.sourceRoot = `./${basename}`;
let contents = [];
for (let i = 0, k = map.sources.length; i < k; ++i) {
let name = map.sources[i];
let text = assemblyscript.getSource(program, name.replace(extension_re, ""));
if (text == null) return prepareResult(Error(`Source of file '${name}' not found.`));
contents[i] = text;
}
map.sourcesContent = contents;
pending.push(
writeFile(path.join(
path.dirname(opts.outFile),
path.basename(sourceMapURL)
).replace(/^\.\//, ""), JSON.stringify(map), baseDir)
);
} else {
stderr.write(`Skipped source map (no output path)${EOL}`);
}
}
}
// Write text (also fallback)
if (opts.textFile != null || !hasOutput) {
let begin = stats.begin();
stats.emitCount++;
let out;
try {
// use superset text format when extension is `.wast`.
// Otherwise use official stack IR format (wat).
out = opts.textFile?.endsWith(".wast")
? binaryenModule.emitText()
: binaryenModule.emitStackIR(true);
} catch (e) {
crash("emitText", e);
}
stats.emitTime += stats.end(begin);
if (opts.textFile != null && opts.textFile.length) {
pending.push(
writeFile(opts.textFile, out, baseDir)
);
} else if (!hasStdout) {
hasStdout = true;
writeStdout(out);
}
}
// Write TypeScript definition
const bindingsEsm = bindings.includes("esm");
const bindingsRaw = !bindingsEsm && bindings.includes("raw");
if (bindingsEsm || bindingsRaw) {
if (basepath) {
let begin = stats.begin();
stats.emitCount++;
let source;
try {
source = assemblyscript.buildTSD(program, bindingsEsm);
} catch (e) {
crash("buildTSD", e);
}
stats.emitTime += stats.end(begin);
pending.push(
writeFile(basepath + ".d.ts", source, baseDir)
);
} else {
stderr.write(`Skipped TypeScript binding (no output path)${EOL}`);
}
}
// Write JavaScript bindings
if (bindingsEsm || bindingsRaw) {
if (basepath) {
let begin = stats.begin();
stats.emitCount++;
let source;
try {
source = assemblyscript.buildJS(program, bindingsEsm);
} catch (e) {
crash("buildJS", e);
}
stats.emitTime += stats.end(begin);
pending.push(
writeFile(basepath + ".js", source, baseDir)