1- "use strict" ;
21/**
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
422 *
523 * Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
624 * JavaScript as well as the compiler compiled to WebAssembly (eventually). Runs the sources
725 * directly through ts-node if distribution files are not present (indicated by a `-dev` version).
826 *
927 * Can also be packaged as a bundle suitable for in-browser use with the standard library injected
1028 * in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
11- *
12- * @module cli/asc
1329 */
1430
1531// Use "." instead of "/" as cwd in browsers
@@ -390,7 +406,8 @@ exports.main = function main(argv, options, callback) {
390406 if ( ( sourceText = readFile ( sourcePath = internalPath + ".ts" , baseDir ) ) == null ) {
391407 if ( ( sourceText = readFile ( sourcePath = internalPath + "/index.ts" , baseDir ) ) == null ) {
392408 // 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 ) ;
394411 }
395412 }
396413
@@ -478,13 +495,16 @@ exports.main = function main(argv, options, callback) {
478495 var internalPath ;
479496 while ( ( internalPath = assemblyscript . nextFile ( program ) ) != null ) {
480497 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." ) )
482499 stats . parseCount ++ ;
483500 stats . parseTime += measure ( ( ) => {
484501 assemblyscript . parse ( program , file . sourceText , file . sourcePath , false ) ;
485502 } ) ;
486503 }
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+ }
488508 }
489509
490510 // Include runtime template before entry files so its setup runs first
@@ -570,6 +590,20 @@ exports.main = function main(argv, options, callback) {
570590 optimizeLevel = Math . min ( Math . max ( optimizeLevel , 0 ) , 3 ) ;
571591 shrinkLevel = Math . min ( Math . max ( shrinkLevel , 0 ) , 2 ) ;
572592
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+
573607 var module ;
574608 stats . compileCount ++ ;
575609 try {
@@ -579,9 +613,10 @@ exports.main = function main(argv, options, callback) {
579613 } catch ( e ) {
580614 return callback ( e ) ;
581615 }
582- if ( checkDiagnostics ( program , stderr ) ) {
616+ var numErrors = checkDiagnostics ( program , stderr ) ;
617+ if ( numErrors ) {
583618 if ( module ) module . dispose ( ) ;
584- return callback ( Error ( "Compile error") ) ;
619+ return callback ( Error ( numErrors + " compile error(s) ") ) ;
585620 }
586621
587622 // Call afterCompile transform hook
@@ -1023,17 +1058,17 @@ exports.main = function main(argv, options, callback) {
10231058/** Checks diagnostics emitted so far for errors. */
10241059function checkDiagnostics ( program , stderr ) {
10251060 var diagnostic ;
1026- var hasErrors = false ;
1061+ var numErrors = 0 ;
10271062 while ( ( diagnostic = assemblyscript . nextDiagnostic ( program ) ) != null ) {
10281063 if ( stderr ) {
10291064 stderr . write (
10301065 assemblyscript . formatDiagnostic ( diagnostic , stderr . isTTY , true ) +
10311066 EOL + EOL
10321067 ) ;
10331068 }
1034- if ( assemblyscript . isError ( diagnostic ) ) hasErrors = true ;
1069+ if ( assemblyscript . isError ( diagnostic ) ) ++ numErrors ;
10351070 }
1036- return hasErrors ;
1071+ return numErrors ;
10371072}
10381073
10391074exports . checkDiagnostics = checkDiagnostics ;
0 commit comments