1
- var fs = require ( "fs" ) ;
2
- var path = require ( "path" ) ;
3
- var minimist = require ( "minimist" ) ;
4
- var glob = require ( "glob" ) ;
1
+ #!/usr/bin/env node
2
+ const fs = require ( "fs" ) ;
3
+ const path = require ( "path" ) ;
4
+ const minimist = require ( "minimist" ) ;
5
+ const glob = require ( "glob" ) ;
6
+ const { SourceMapConsumer, SourceMapGenerator } = require ( "source-map" ) ;
5
7
6
8
var assemblyscript ;
7
9
var isDev = true ;
15
17
assemblyscript = require ( "../src" ) ;
16
18
}
17
19
18
- var conf = require ( "./asc.json" ) ;
19
- var opts = { } ;
20
+ const conf = require ( "./asc.json" ) ;
21
+ const opts = { } ;
20
22
21
23
Object . keys ( conf ) . forEach ( key => {
22
24
var opt = conf [ key ] ;
@@ -30,9 +32,10 @@ Object.keys(conf).forEach(key => {
30
32
( opts . boolean || ( opts . boolean = [ ] ) ) . push ( key ) ;
31
33
} ) ;
32
34
33
- var args = minimist ( process . argv . slice ( 2 ) , opts ) ;
35
+ const args = minimist ( process . argv . slice ( 2 ) , opts ) ;
36
+ const indent = 24 ;
37
+
34
38
var version = require ( "../package.json" ) . version ;
35
- var indent = 24 ;
36
39
if ( isDev ) version += "-dev" ;
37
40
38
41
if ( args . version ) {
@@ -43,7 +46,7 @@ if (args.version) {
43
46
}
44
47
45
48
if ( args . help || args . _ . length < 1 ) {
46
- var options = [ ] ;
49
+ const options = [ ] ;
47
50
Object . keys ( conf ) . forEach ( name => {
48
51
var option = conf [ name ] ;
49
52
var text = " " ;
@@ -79,6 +82,7 @@ var parser = null;
79
82
var readTime = 0 ;
80
83
var readCount = 0 ;
81
84
var writeTime = 0 ;
85
+ var writeCount = 0 ;
82
86
var parseTime = 0 ;
83
87
var compileTime = 0 ;
84
88
var validateTime = 0 ;
@@ -122,7 +126,7 @@ libDirs.forEach(libDir => {
122
126
var nextText = fs . readFileSync ( path . join ( libDir , file ) , { encoding : "utf8" } ) ;
123
127
++ readCount ;
124
128
var time = measure ( ( ) => {
125
- parser = assemblyscript . parseFile ( nextText , "std: " + file , parser , false ) ;
129
+ parser = assemblyscript . parseFile ( nextText , ". std/ " + file , parser , false ) ;
126
130
} ) ;
127
131
parseTime += time ;
128
132
notIoTime += time ;
@@ -139,12 +143,14 @@ args._.forEach(filename => {
139
143
try {
140
144
readTime += measure ( ( ) => {
141
145
entryText = fs . readFileSync ( entryPath + ".ts" , { encoding : "utf8" } ) ;
146
+ entryPath += ".ts" ;
142
147
} ) ;
143
148
++ readCount ;
144
149
} catch ( e ) {
145
150
try {
146
151
readTime += measure ( ( ) => {
147
152
entryText = fs . readFileSync ( entryPath + "/index.ts" , { encoding : "utf8" } ) ;
153
+ entryPath += "/index.ts" ;
148
154
} ) ;
149
155
++ readCount ;
150
156
entryPath = entryPath + "/index" ;
@@ -154,21 +160,22 @@ args._.forEach(filename => {
154
160
}
155
161
}
156
162
157
- var nextPath ;
163
+ var nextFile ;
158
164
var nextText ;
159
165
160
166
// Load entry text
161
167
parseTime += measure ( ( ) => {
162
168
parser = assemblyscript . parseFile ( entryText , entryPath , parser , true ) ;
163
169
} ) ;
164
170
165
- while ( ( nextPath = parser . nextFile ( ) ) != null ) {
171
+ while ( ( nextFile = parser . nextFile ( ) ) != null ) {
166
172
var found = false ;
167
- if ( nextPath . startsWith ( "std: " ) ) {
173
+ if ( nextFile . startsWith ( ". std/ " ) ) {
168
174
for ( var i = 0 ; i < libDirs . length ; ++ i ) {
169
175
readTime += measure ( ( ) => {
170
176
try {
171
- nextText = fs . readFileSync ( libDirs [ i ] + "/" + nextPath . substring ( 4 ) + ".ts" , { encoding : "utf8" } ) ;
177
+ nextText = fs . readFileSync ( path . join ( libDirs [ i ] , nextFile . substring ( 4 ) + ".ts" ) , { encoding : "utf8" } ) ;
178
+ nextFile = nextFile + ".ts" ;
172
179
found = true ;
173
180
} catch ( e ) { }
174
181
} ) ;
@@ -179,27 +186,29 @@ args._.forEach(filename => {
179
186
} else {
180
187
readTime += measure ( ( ) => {
181
188
try {
182
- nextText = fs . readFileSync ( nextPath + "/index.ts" , { encoding : "utf8" } ) ;
189
+ nextText = fs . readFileSync ( nextFile + ".ts" , { encoding : "utf8" } ) ;
190
+ nextFile = nextFile + ".ts" ;
183
191
found = true ;
184
192
} catch ( e ) { }
185
193
} ) ;
186
194
++ readCount ;
187
195
if ( ! found ) {
188
196
readTime += measure ( ( ) => {
189
197
try {
190
- nextText = fs . readFileSync ( nextPath + ".ts" , { encoding : "utf8" } ) ;
198
+ nextText = fs . readFileSync ( nextFile + "/index.ts" , { encoding : "utf8" } ) ;
199
+ nextFile = nextFile + "/index.ts" ;
191
200
found = true ;
192
201
} catch ( e ) { }
193
202
} ) ;
194
203
++ readCount ;
195
204
}
196
205
}
197
206
if ( ! found ) {
198
- console . error ( "Imported file '" + nextPath + ".ts' not found." ) ;
207
+ console . error ( "Imported file '" + nextFile + ".ts' not found." ) ;
199
208
process . exit ( 1 ) ;
200
209
}
201
210
parseTime += measure ( ( ) => {
202
- assemblyscript . parseFile ( nextText , nextPath , parser ) ;
211
+ assemblyscript . parseFile ( nextText , nextFile , parser ) ;
203
212
} ) ;
204
213
}
205
214
checkDiagnostics ( parser ) ;
@@ -210,6 +219,7 @@ assemblyscript.setTarget(options, 0);
210
219
assemblyscript . setNoTreeShaking ( options , args . noTreeShaking ) ;
211
220
assemblyscript . setNoAssert ( options , args . noAssert ) ;
212
221
assemblyscript . setNoMemory ( options , args . noMemory ) ;
222
+ assemblyscript . setSourceMap ( options , args . sourceMap != null ) ;
213
223
214
224
var module ;
215
225
compileTime += measure ( ( ) => {
@@ -221,6 +231,7 @@ if (args.validate)
221
231
validateTime += measure ( ( ) => {
222
232
if ( ! module . validate ( ) ) {
223
233
module . dispose ( ) ;
234
+ console . error ( "Validation failed" ) ;
224
235
process . exit ( 1 ) ;
225
236
}
226
237
} ) ;
@@ -234,7 +245,7 @@ else if (args.trapMode === "js")
234
245
module . runPasses ( [ "trap-mode-js" ] ) ;
235
246
} ) ;
236
247
else if ( args . trapMode !== "allow" ) {
237
- console . log ( "Unsupported trap mode: " + args . trapMode ) ;
248
+ console . error ( "Unsupported trap mode: " + args . trapMode ) ;
238
249
process . exit ( 1 ) ;
239
250
}
240
251
@@ -298,6 +309,40 @@ if (runPasses.length)
298
309
module . runPasses ( runPasses . map ( pass => pass . trim ( ) ) ) ;
299
310
} ) ;
300
311
312
+ function processSourceMap ( sourceMap , sourceMapURL ) {
313
+ var json = JSON . parse ( sourceMap ) ;
314
+ return SourceMapConsumer . with ( sourceMap , sourceMapURL , consumer => {
315
+ var generator = SourceMapGenerator . fromSourceMap ( consumer ) ;
316
+ json . sources . forEach ( name => {
317
+ var text , found = false ;
318
+ if ( name . startsWith ( ".std/" ) ) {
319
+ for ( var i = 0 , k = libDirs . length ; i < k ; ++ i ) {
320
+ readTime += measure ( ( ) => {
321
+ try {
322
+ text = fs . readFileSync ( path . join ( libDirs [ i ] , name . substring ( 4 ) ) , { encoding : "utf8" } ) ;
323
+ found = true ;
324
+ } catch ( e ) { }
325
+ } ) ;
326
+ ++ readCount ;
327
+ }
328
+ } else {
329
+ readTime += measure ( ( ) => {
330
+ try {
331
+ text = fs . readFileSync ( name , { encoding : "utf8" } ) ;
332
+ found = true ;
333
+ } catch ( e ) { }
334
+ } ) ;
335
+ ++ readCount ;
336
+ }
337
+ if ( found )
338
+ generator . setSourceContent ( name , text ) ;
339
+ else
340
+ console . error ( "No source content found for file '" + name + "'." ) ;
341
+ } ) ;
342
+ return generator . toString ( ) ;
343
+ } ) ;
344
+ }
345
+
301
346
if ( ! args . noEmit ) {
302
347
var hasOutput = false ;
303
348
@@ -310,47 +355,69 @@ if (!args.noEmit) {
310
355
args . binaryFile = args . outFile ;
311
356
}
312
357
if ( args . binaryFile != null && args . binaryFile . length ) {
358
+ var sourceMapURL = args . sourceMap != null
359
+ ? args . sourceMap . length
360
+ ? args . sourceMap
361
+ : path . basename ( args . binaryFile ) + ".map"
362
+ : null ;
363
+ var binary ;
313
364
writeTime += measure ( ( ) => {
314
- fs . writeFileSync ( args . binaryFile , module . toBinary ( ) ) ;
365
+ binary = module . toBinary ( sourceMapURL ) ; // FIXME: 'not a valid URL' in FF
366
+ fs . writeFileSync ( args . binaryFile , binary . output ) ;
315
367
} ) ;
368
+ ++ writeCount ;
369
+ if ( binary . sourceMap != null )
370
+ processSourceMap ( binary . sourceMap ) . then ( sourceMap => {
371
+ writeTime += measure ( ( ) => {
372
+ fs . writeFileSync ( path . join ( path . dirname ( args . binaryFile ) , path . basename ( sourceMapURL ) ) , sourceMap , { encoding : "utf8" } ) ;
373
+ } , err => {
374
+ throw err ;
375
+ } ) ;
376
+ ++ writeCount ;
377
+ } ) ;
316
378
hasOutput = true ;
317
379
}
318
380
if ( args . textFile != null && args . textFile . length ) {
319
381
writeTime += measure ( ( ) => {
320
382
fs . writeFileSync ( args . textFile , module . toText ( ) , { encoding : "utf8" } ) ;
321
383
} ) ;
384
+ ++ writeCount ;
322
385
hasOutput = true ;
323
386
}
324
387
if ( args . asmjsFile != null && args . asmjsFile . length ) {
325
388
writeTime += measure ( ( ) => {
326
389
fs . writeFileSync ( args . asmjsFile , module . toAsmjs ( ) , { encoding : "utf8" } ) ;
327
390
} ) ;
391
+ ++ writeCount ;
328
392
hasOutput = true ;
329
393
}
330
394
if ( ! hasOutput ) {
331
- if ( args . binaryFile === "" )
395
+ if ( args . binaryFile === "" ) {
332
396
writeTime += measure ( ( ) => {
333
397
process . stdout . write ( Buffer . from ( module . toBinary ( ) ) ) ;
334
398
} ) ;
335
- else if ( args . asmjsFile === "" )
399
+ ++ writeCount ;
400
+ } else if ( args . asmjsFile === "" ) {
336
401
writeTime += measure ( ( ) => {
337
402
module . printAsmjs ( ) ;
338
403
} ) ;
339
- else
404
+ ++ writeCount ;
405
+ } else {
340
406
writeTime += measure ( ( ) => {
341
407
module . print ( ) ;
342
408
} ) ;
409
+ ++ writeCount ;
410
+ }
343
411
}
344
412
}
345
413
346
414
module . dispose ( ) ;
347
415
348
- if ( args . measure )
349
- console . error ( [
350
- "I/O Read : " + ( readTime ? ( readTime / 1e6 ) . toFixed ( 3 ) + " ms (" + readCount + " files)" : "N/A" ) ,
351
- "I/O Write : " + ( writeTime ? ( writeTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
352
- "Parse : " + ( parseTime ? ( parseTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
353
- "Compile : " + ( compileTime ? ( compileTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
354
- "Validate : " + ( validateTime ? ( validateTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
355
- "Optimize : " + ( optimizeTime ? ( optimizeTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" )
356
- ] . join ( "\n" ) ) ;
416
+ if ( args . measure ) process . on ( "beforeExit" , ( ) => console . error ( [
417
+ "I/O Read : " + ( readTime ? ( readTime / 1e6 ) . toFixed ( 3 ) + " ms (" + readCount + " files)" : "N/A" ) ,
418
+ "I/O Write : " + ( writeTime ? ( writeTime / 1e6 ) . toFixed ( 3 ) + " ms (" + writeCount + " files)" : "N/A" ) ,
419
+ "Parse : " + ( parseTime ? ( parseTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
420
+ "Compile : " + ( compileTime ? ( compileTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
421
+ "Validate : " + ( validateTime ? ( validateTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" ) ,
422
+ "Optimize : " + ( optimizeTime ? ( optimizeTime / 1e6 ) . toFixed ( 3 ) + " ms" : "N/A" )
423
+ ] . join ( "\n" ) ) ) ;
0 commit comments