33
33
const fs = require ( "fs" ) ;
34
34
const path = require ( "path" ) ;
35
35
const process = require ( "process" ) ; // ensure shim
36
-
37
- process . exit = ( ( exit ) => function ( code ) {
38
- if ( code ) console . log ( new Error ( "exit " + code . toString ( ) ) . stack ) ;
39
- exit ( code ) ;
40
- } ) ( process . exit ) ;
41
-
42
36
const utf8 = require ( "./util/utf8" ) ;
43
37
const colorsUtil = require ( "./util/colors" ) ;
44
38
const optionsUtil = require ( "./util/options" ) ;
@@ -795,7 +789,11 @@ exports.main = function main(argv, options, callback) {
795
789
// Pre-emptively initialize the program
796
790
stats . initializeCount ++ ;
797
791
stats . initializeTime += measure ( ( ) => {
798
- assemblyscript . initializeProgram ( program ) ;
792
+ try {
793
+ assemblyscript . initializeProgram ( program ) ;
794
+ } catch ( e ) {
795
+ crash ( "initialize" , e ) ;
796
+ }
799
797
} ) ;
800
798
801
799
// Call afterInitialize transform hook
@@ -807,7 +805,11 @@ exports.main = function main(argv, options, callback) {
807
805
var module ;
808
806
stats . compileCount ++ ;
809
807
stats . compileTime += measure ( ( ) => {
810
- module = assemblyscript . compile ( program ) ;
808
+ try {
809
+ module = assemblyscript . compile ( program ) ;
810
+ } catch ( e ) {
811
+ crash ( "compile" , e ) ;
812
+ }
811
813
// From here on we are going to use Binaryen.js, except that we keep pass
812
814
// order as defined in the compiler.
813
815
if ( typeof module === "number" ) { // Wasm
@@ -877,23 +879,50 @@ exports.main = function main(argv, options, callback) {
877
879
}
878
880
if ( opts . runPasses . length ) {
879
881
opts . runPasses . forEach ( pass => {
880
- if ( runPasses . indexOf ( pass = pass . trim ( ) ) < 0 )
882
+ if ( runPasses . indexOf ( pass = pass . trim ( ) ) < 0 ) {
881
883
runPasses . push ( pass ) ;
884
+ }
882
885
} ) ;
883
886
}
884
887
}
885
888
886
889
stats . optimizeTime += measure ( ( ) => {
887
890
stats . optimizeCount ++ ;
888
- module . optimize ( optimizeLevel , shrinkLevel , debugInfo ) ;
889
- module . runPasses ( runPasses ) ;
891
+ try {
892
+ module . optimize ( optimizeLevel , shrinkLevel , debugInfo ) ;
893
+ } catch ( e ) {
894
+ crash ( "optimize" , e ) ;
895
+ }
896
+ try {
897
+ module . runPasses ( runPasses ) ;
898
+ } catch ( e ) {
899
+ crash ( "runPasses" , e ) ;
900
+ }
890
901
if ( converge ) {
891
- let last = module . emitBinary ( ) ;
902
+ let last ;
903
+ try {
904
+ last = module . emitBinary ( ) ;
905
+ } catch ( e ) {
906
+ crash ( "emitBinary (converge)" , e ) ;
907
+ }
892
908
do {
893
909
stats . optimizeCount ++ ;
894
- module . optimize ( optimizeLevel , shrinkLevel , debugInfo ) ;
895
- module . runPasses ( runPasses ) ;
896
- let next = module . emitBinary ( ) ;
910
+ try {
911
+ module . optimize ( optimizeLevel , shrinkLevel , debugInfo ) ;
912
+ } catch ( e ) {
913
+ crash ( "optimize (converge)" , e ) ;
914
+ }
915
+ try {
916
+ module . runPasses ( runPasses ) ;
917
+ } catch ( e ) {
918
+ crash ( "runPasses (converge)" , e ) ;
919
+ }
920
+ let next ;
921
+ try {
922
+ next = module . emitBinary ( ) ;
923
+ } catch ( e ) {
924
+ crash ( "emitBinary (converge)" , e ) ;
925
+ }
897
926
if ( next . length >= last . length ) {
898
927
if ( next . length > last . length ) {
899
928
stderr . write ( "Last converge was suboptimial." + EOL ) ;
@@ -936,7 +965,11 @@ exports.main = function main(argv, options, callback) {
936
965
let wasm ;
937
966
stats . emitCount ++ ;
938
967
stats . emitTime += measure ( ( ) => {
939
- wasm = module . emitBinary ( sourceMapURL ) ;
968
+ try {
969
+ wasm = module . emitBinary ( sourceMapURL ) ;
970
+ } catch ( e ) {
971
+ crash ( "emitBinary" , e ) ;
972
+ }
940
973
} ) ;
941
974
942
975
if ( opts . binaryFile . length ) {
@@ -977,17 +1010,25 @@ exports.main = function main(argv, options, callback) {
977
1010
let wastFormat = opts . textFile . endsWith ( '.wast' ) ;
978
1011
stats . emitCount ++ ;
979
1012
stats . emitTime += measure ( ( ) => {
980
- if ( wastFormat ) {
981
- out = module . emitText ( ) ;
982
- } else {
983
- out = module . emitStackIR ( true ) ;
1013
+ try {
1014
+ if ( wastFormat ) {
1015
+ out = module . emitText ( ) ;
1016
+ } else {
1017
+ out = module . emitStackIR ( true ) ;
1018
+ }
1019
+ } catch ( e ) {
1020
+ crash ( "emitText" , e ) ;
984
1021
}
985
1022
} ) ;
986
1023
writeFile ( opts . textFile , out , baseDir ) ;
987
1024
} else if ( ! hasStdout ) {
988
1025
stats . emitCount ++ ;
989
1026
stats . emitTime += measure ( ( ) => {
990
- out = module . emitStackIR ( true ) ;
1027
+ try {
1028
+ out = module . emitStackIR ( true ) ;
1029
+ } catch ( e ) {
1030
+ crash ( "emitText" , e ) ;
1031
+ }
991
1032
} ) ;
992
1033
writeStdout ( out ) ;
993
1034
}
@@ -999,13 +1040,21 @@ exports.main = function main(argv, options, callback) {
999
1040
if ( opts . idlFile . length ) {
1000
1041
stats . emitCount ++ ;
1001
1042
stats . emitTime += measure ( ( ) => {
1002
- idl = assemblyscript . buildIDL ( program ) ;
1043
+ try {
1044
+ idl = assemblyscript . buildIDL ( program ) ;
1045
+ } catch ( e ) {
1046
+ crash ( "buildIDL" , e ) ;
1047
+ }
1003
1048
} ) ;
1004
1049
writeFile ( opts . idlFile , __getString ( idl ) , baseDir ) ;
1005
1050
} else if ( ! hasStdout ) {
1006
1051
stats . emitCount ++ ;
1007
1052
stats . emitTime += measure ( ( ) => {
1008
- idl = assemblyscript . buildIDL ( program ) ;
1053
+ try {
1054
+ idl = assemblyscript . buildIDL ( program ) ;
1055
+ } catch ( e ) {
1056
+ crash ( "buildIDL" , e ) ;
1057
+ }
1009
1058
} ) ;
1010
1059
writeStdout ( __getString ( idl ) ) ;
1011
1060
hasStdout = true ;
@@ -1018,13 +1067,21 @@ exports.main = function main(argv, options, callback) {
1018
1067
if ( opts . tsdFile . length ) {
1019
1068
stats . emitCount ++ ;
1020
1069
stats . emitTime += measure ( ( ) => {
1021
- tsd = assemblyscript . buildTSD ( program ) ;
1070
+ try {
1071
+ tsd = assemblyscript . buildTSD ( program ) ;
1072
+ } catch ( e ) {
1073
+ crash ( "buildTSD" , e ) ;
1074
+ }
1022
1075
} ) ;
1023
1076
writeFile ( opts . tsdFile , __getString ( tsd ) , baseDir ) ;
1024
1077
} else if ( ! hasStdout ) {
1025
1078
stats . emitCount ++ ;
1026
1079
stats . emitTime += measure ( ( ) => {
1027
- tsd = assemblyscript . buildTSD ( program ) ;
1080
+ try {
1081
+ tsd = assemblyscript . buildTSD ( program ) ;
1082
+ } catch ( e ) {
1083
+ crash ( "buildTSD" , e ) ;
1084
+ }
1028
1085
} ) ;
1029
1086
writeStdout ( __getString ( tsd ) ) ;
1030
1087
hasStdout = true ;
@@ -1037,13 +1094,21 @@ exports.main = function main(argv, options, callback) {
1037
1094
if ( opts . jsFile . length ) {
1038
1095
stats . emitCount ++ ;
1039
1096
stats . emitTime += measure ( ( ) => {
1040
- js = module . emitAsmjs ( ) ;
1097
+ try {
1098
+ js = module . emitAsmjs ( ) ;
1099
+ } catch ( e ) {
1100
+ crash ( "emitJS" , e ) ;
1101
+ }
1041
1102
} ) ;
1042
1103
writeFile ( opts . jsFile , js , baseDir ) ;
1043
1104
} else if ( ! hasStdout ) {
1044
1105
stats . emitCount ++ ;
1045
1106
stats . emitTime += measure ( ( ) => {
1046
- js = module . emitAsmjs ( ) ;
1107
+ try {
1108
+ js = module . emitAsmjs ( ) ;
1109
+ } catch ( e ) {
1110
+ crash ( "emitJS" , e ) ;
1111
+ }
1047
1112
} ) ;
1048
1113
writeStdout ( js ) ;
1049
1114
}
@@ -1332,3 +1397,25 @@ exports.tscOptions = {
1332
1397
types : [ ] ,
1333
1398
allowJs : false
1334
1399
} ;
1400
+
1401
+ // Gracefully handle crashes
1402
+ function crash ( stage , e ) {
1403
+ const BAR = colorsUtil . red ( "▌ " ) ;
1404
+ console . error ( [
1405
+ EOL ,
1406
+ BAR , "Whoops, the AssemblyScript compiler has crashed during " , stage , " :-(" , EOL ,
1407
+ BAR , EOL ,
1408
+ BAR , "Here is a stack trace that may or may not be useful:" , EOL ,
1409
+ BAR , EOL ,
1410
+ e . stack . replace ( / ^ / mg, BAR ) , EOL ,
1411
+ BAR , EOL ,
1412
+ BAR , "If it refers to the dist files, try to 'npm install source-map-support' and" , EOL ,
1413
+ BAR , "run again, which should then show the actual code location in the sources." , EOL ,
1414
+ BAR , EOL ,
1415
+ BAR , "If you see where the error is, feel free to send us a pull request. If not," , EOL ,
1416
+ BAR , "please let us know: https://github.com/AssemblyScript/assemblyscript/issues" , EOL ,
1417
+ BAR , EOL ,
1418
+ BAR , "Thank you!" , EOL
1419
+ ] . join ( "" ) ) ;
1420
+ process . exit ( 1 ) ;
1421
+ }
0 commit comments