1
- "use strict" ;
2
1
/**
3
- * Compiler frontend for node.js
2
+ * @license
3
+ * Copyright 2020 Daniel Wirtz / The AssemblyScript Authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ * SPDX-License-Identifier: Apache-2.0
18
+ */
19
+
20
+ /**
21
+ * @fileoverview Compiler frontend for node.js
4
22
*
5
23
* Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
6
24
* JavaScript as well as the compiler compiled to WebAssembly (eventually). Runs the sources
7
25
* directly through ts-node if distribution files are not present (indicated by a `-dev` version).
8
26
*
9
27
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
10
28
* in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
11
- *
12
- * @module cli/asc
13
29
*/
14
30
15
31
// Use "." instead of "/" as cwd in browsers
@@ -390,7 +406,8 @@ exports.main = function main(argv, options, callback) {
390
406
if ( ( sourceText = readFile ( sourcePath = internalPath + ".ts" , baseDir ) ) == null ) {
391
407
if ( ( sourceText = readFile ( sourcePath = internalPath + "/index.ts" , baseDir ) ) == null ) {
392
408
// portable d.ts: uses the .js file next to it in JS or becomes an import in Wasm
393
- sourceText = readFile ( sourcePath = internalPath + ".d.ts" , baseDir ) ;
409
+ sourcePath = internalPath + ".ts" ;
410
+ sourceText = readFile ( internalPath + ".d.ts" , baseDir ) ;
394
411
}
395
412
}
396
413
@@ -478,13 +495,16 @@ exports.main = function main(argv, options, callback) {
478
495
var internalPath ;
479
496
while ( ( internalPath = assemblyscript . nextFile ( program ) ) != null ) {
480
497
let file = getFile ( internalPath , assemblyscript . getDependee ( program , internalPath ) ) ;
481
- if ( ! file ) return callback ( Error ( "Import file '" + internalPath + ".ts ' not found." ) )
498
+ if ( ! file ) return callback ( Error ( "Import '" + internalPath + "' not found." ) )
482
499
stats . parseCount ++ ;
483
500
stats . parseTime += measure ( ( ) => {
484
501
assemblyscript . parse ( program , file . sourceText , file . sourcePath , false ) ;
485
502
} ) ;
486
503
}
487
- if ( checkDiagnostics ( program , stderr ) ) return callback ( Error ( "Parse error" ) ) ;
504
+ var numErrors = checkDiagnostics ( program , stderr ) ;
505
+ if ( numErrors ) {
506
+ return callback ( Error ( numErrors + " parse error(s)" ) ) ;
507
+ }
488
508
}
489
509
490
510
// Include runtime template before entry files so its setup runs first
@@ -570,6 +590,20 @@ exports.main = function main(argv, options, callback) {
570
590
optimizeLevel = Math . min ( Math . max ( optimizeLevel , 0 ) , 3 ) ;
571
591
shrinkLevel = Math . min ( Math . max ( shrinkLevel , 0 ) , 2 ) ;
572
592
593
+ try {
594
+ stats . compileTime += measure ( ( ) => {
595
+ assemblyscript . initializeProgram ( program , compilerOptions ) ;
596
+ } ) ;
597
+ } catch ( e ) {
598
+ return callback ( e ) ;
599
+ }
600
+
601
+ // Call afterInitialize transform hook
602
+ {
603
+ let error = applyTransform ( "afterInitialize" , program ) ;
604
+ if ( error ) return callback ( error ) ;
605
+ }
606
+
573
607
var module ;
574
608
stats . compileCount ++ ;
575
609
try {
@@ -579,9 +613,10 @@ exports.main = function main(argv, options, callback) {
579
613
} catch ( e ) {
580
614
return callback ( e ) ;
581
615
}
582
- if ( checkDiagnostics ( program , stderr ) ) {
616
+ var numErrors = checkDiagnostics ( program , stderr ) ;
617
+ if ( numErrors ) {
583
618
if ( module ) module . dispose ( ) ;
584
- return callback ( Error ( "Compile error") ) ;
619
+ return callback ( Error ( numErrors + " compile error(s) ") ) ;
585
620
}
586
621
587
622
// Call afterCompile transform hook
@@ -1023,17 +1058,17 @@ exports.main = function main(argv, options, callback) {
1023
1058
/** Checks diagnostics emitted so far for errors. */
1024
1059
function checkDiagnostics ( program , stderr ) {
1025
1060
var diagnostic ;
1026
- var hasErrors = false ;
1061
+ var numErrors = 0 ;
1027
1062
while ( ( diagnostic = assemblyscript . nextDiagnostic ( program ) ) != null ) {
1028
1063
if ( stderr ) {
1029
1064
stderr . write (
1030
1065
assemblyscript . formatDiagnostic ( diagnostic , stderr . isTTY , true ) +
1031
1066
EOL + EOL
1032
1067
) ;
1033
1068
}
1034
- if ( assemblyscript . isError ( diagnostic ) ) hasErrors = true ;
1069
+ if ( assemblyscript . isError ( diagnostic ) ) ++ numErrors ;
1035
1070
}
1036
- return hasErrors ;
1071
+ return numErrors ;
1037
1072
}
1038
1073
1039
1074
exports . checkDiagnostics = checkDiagnostics ;
0 commit comments