@@ -96,9 +96,7 @@ exports.compileString = (sources, options) => {
96
96
if ( typeof sources === "string" ) sources = { "input.ts" : sources } ;
97
97
const output = Object . create ( {
98
98
stdout : createMemoryStream ( ) ,
99
- stderr : createMemoryStream ( ) ,
100
- binary : null ,
101
- text : null
99
+ stderr : createMemoryStream ( )
102
100
} ) ;
103
101
var argv = [
104
102
"--binaryFile" , "binary" ,
@@ -270,7 +268,7 @@ exports.main = function main(argv, options, callback) {
270
268
}
271
269
for ( let j = 0 , l = libFiles . length ; j < l ; ++ j ) {
272
270
let libPath = libFiles [ j ] ;
273
- let libText = readFile ( path . join ( libDir , libPath ) ) ;
271
+ let libText = readFile ( libPath , libDir ) ;
274
272
if ( libText === null ) return callback ( Error ( "Library file '" + libPath + "' not found." ) ) ;
275
273
stats . parseCount ++ ;
276
274
stats . parseTime += measure ( ( ) => {
@@ -285,33 +283,10 @@ exports.main = function main(argv, options, callback) {
285
283
}
286
284
}
287
285
288
- // Include entry files
289
- for ( let i = 0 , k = argv . length ; i < k ; ++ i ) {
290
- const filename = argv [ i ] ;
291
-
292
- let sourcePath = String ( filename ) . replace ( / \\ / g, "/" ) . replace ( / ( \. t s | \/ ) $ / , "" ) ;
293
-
294
- // Try entryPath.ts, then entryPath/index.ts
295
- let sourceText = readFile ( path . join ( baseDir , sourcePath ) + ".ts" ) ;
296
- if ( sourceText === null ) {
297
- sourceText = readFile ( path . join ( baseDir , sourcePath , "index.ts" ) ) ;
298
- if ( sourceText === null ) {
299
- return callback ( Error ( "Entry file '" + sourcePath + ".ts' not found." ) ) ;
300
- } else {
301
- sourcePath += "/index.ts" ;
302
- }
303
- } else {
304
- sourcePath += ".ts" ;
305
- }
306
-
307
- stats . parseCount ++ ;
308
- stats . parseTime += measure ( ( ) => {
309
- parser = assemblyscript . parseFile ( sourceText , sourcePath , true , parser ) ;
310
- } ) ;
311
-
312
- // Process backlog
286
+ // Parses the backlog of imported files after including entry files
287
+ function parseBacklog ( ) {
288
+ var sourcePath , sourceText ;
313
289
while ( ( sourcePath = parser . nextFile ( ) ) != null ) {
314
- let found = false ;
315
290
316
291
// Load library file if explicitly requested
317
292
if ( sourcePath . startsWith ( exports . libraryPrefix ) ) {
@@ -325,13 +300,12 @@ exports.main = function main(argv, options, callback) {
325
300
sourcePath = exports . libraryPrefix + indexName + ".ts" ;
326
301
} else {
327
302
for ( let i = 0 , k = customLibDirs . length ; i < k ; ++ i ) {
328
- const dir = customLibDirs [ i ] ;
329
- sourceText = readFile ( path . join ( dir , plainName + ".ts" ) ) ;
303
+ sourceText = readFile ( plainName + ".ts" , customLibDirs [ i ] ) ;
330
304
if ( sourceText !== null ) {
331
305
sourcePath = exports . libraryPrefix + plainName + ".ts" ;
332
306
break ;
333
307
} else {
334
- sourceText = readFile ( path . join ( dir , indexName + ".ts" ) ) ;
308
+ sourceText = readFile ( indexName + ".ts" , customLibDirs [ i ] ) ;
335
309
if ( sourceText !== null ) {
336
310
sourcePath = exports . libraryPrefix + indexName + ".ts" ;
337
311
break ;
@@ -344,11 +318,11 @@ exports.main = function main(argv, options, callback) {
344
318
} else {
345
319
const plainName = sourcePath ;
346
320
const indexName = sourcePath + "/index" ;
347
- sourceText = readFile ( path . join ( baseDir , plainName + ".ts" ) ) ;
321
+ sourceText = readFile ( plainName + ".ts" , baseDir ) ;
348
322
if ( sourceText !== null ) {
349
323
sourcePath = plainName + ".ts" ;
350
324
} else {
351
- sourceText = readFile ( path . join ( baseDir , indexName + ".ts" ) ) ;
325
+ sourceText = readFile ( indexName + ".ts" , baseDir ) ;
352
326
if ( sourceText !== null ) {
353
327
sourcePath = indexName + ".ts" ;
354
328
} else if ( ! plainName . startsWith ( "." ) ) {
@@ -361,12 +335,12 @@ exports.main = function main(argv, options, callback) {
361
335
} else {
362
336
for ( let i = 0 , k = customLibDirs . length ; i < k ; ++ i ) {
363
337
const dir = customLibDirs [ i ] ;
364
- sourceText = readFile ( path . join ( dir , plainName + ".ts" ) ) ;
338
+ sourceText = readFile ( plainName + ".ts" , customLibDirs [ i ] ) ;
365
339
if ( sourceText !== null ) {
366
340
sourcePath = exports . libraryPrefix + plainName + ".ts" ;
367
341
break ;
368
342
} else {
369
- sourceText = readFile ( path . join ( dir , indexName + ".ts" ) ) ;
343
+ sourceText = readFile ( indexName + ".ts" , customLibDirs [ i ] ) ;
370
344
if ( sourceText !== null ) {
371
345
sourcePath = exports . libraryPrefix + indexName + ".ts" ;
372
346
break ;
@@ -390,7 +364,38 @@ exports.main = function main(argv, options, callback) {
390
364
}
391
365
}
392
366
367
+ // Include entry files
368
+ for ( let i = 0 , k = argv . length ; i < k ; ++ i ) {
369
+ const filename = argv [ i ] ;
370
+
371
+ let sourcePath = String ( filename ) . replace ( / \\ / g, "/" ) . replace ( / ( \. t s | \/ ) $ / , "" ) ;
372
+
373
+ // Try entryPath.ts, then entryPath/index.ts
374
+ let sourceText = readFile ( sourcePath + ".ts" , baseDir ) ;
375
+ if ( sourceText === null ) {
376
+ sourceText = readFile ( sourcePath + "/index.ts" , baseDir ) ;
377
+ if ( sourceText === null ) {
378
+ return callback ( Error ( "Entry file '" + sourcePath + ".ts' not found." ) ) ;
379
+ } else {
380
+ sourcePath += "/index.ts" ;
381
+ }
382
+ } else {
383
+ sourcePath += ".ts" ;
384
+ }
385
+
386
+ stats . parseCount ++ ;
387
+ stats . parseTime += measure ( ( ) => {
388
+ parser = assemblyscript . parseFile ( sourceText , sourcePath , true , parser ) ;
389
+ } ) ;
390
+ let code = parseBacklog ( ) ;
391
+ if ( code ) return code ;
392
+ }
393
+
393
394
applyTransform ( "afterParse" , parser ) ;
395
+ {
396
+ let code = parseBacklog ( ) ;
397
+ if ( code ) return code ;
398
+ }
394
399
395
400
// Finish parsing
396
401
const program = assemblyscript . finishParsing ( parser ) ;
@@ -568,7 +573,7 @@ exports.main = function main(argv, options, callback) {
568
573
} ) ;
569
574
570
575
if ( args . binaryFile . length ) {
571
- writeFile ( path . join ( baseDir , args . binaryFile ) , wasm . output ) ;
576
+ writeFile ( args . binaryFile , wasm . output , baseDir ) ;
572
577
} else {
573
578
writeStdout ( wasm . output ) ;
574
579
hasStdout = true ;
@@ -588,15 +593,12 @@ exports.main = function main(argv, options, callback) {
588
593
text = exports . libraryFiles [ stdName ] ;
589
594
} else {
590
595
for ( let i = 0 , k = customLibDirs . length ; i < k ; ++ i ) {
591
- text = readFile ( path . join (
592
- customLibDirs [ i ] ,
593
- name . substring ( exports . libraryPrefix . length ) )
594
- ) ;
596
+ text = readFile ( name . substring ( exports . libraryPrefix . length ) , customLibDirs [ i ] ) ;
595
597
if ( text !== null ) break ;
596
598
}
597
599
}
598
600
} else {
599
- text = readFile ( path . join ( baseDir , name ) ) ;
601
+ text = readFile ( name , baseDir ) ;
600
602
}
601
603
if ( text === null ) {
602
604
return callback ( Error ( "Source file '" + name + "' not found." ) ) ;
@@ -605,10 +607,9 @@ exports.main = function main(argv, options, callback) {
605
607
sourceMap . sourceContents [ index ] = text ;
606
608
} ) ;
607
609
writeFile ( path . join (
608
- baseDir ,
609
610
path . dirname ( args . binaryFile ) ,
610
611
path . basename ( sourceMapURL )
611
- ) , JSON . stringify ( sourceMap ) ) ;
612
+ ) . replace ( / ^ \. \/ / , "" ) , JSON . stringify ( sourceMap ) , baseDir ) ;
612
613
} else {
613
614
stderr . write ( "Skipped source map (stdout already occupied)" + EOL ) ;
614
615
}
@@ -623,7 +624,7 @@ exports.main = function main(argv, options, callback) {
623
624
stats . emitTime += measure ( ( ) => {
624
625
asm = module . toAsmjs ( ) ;
625
626
} ) ;
626
- writeFile ( path . join ( baseDir , args . asmjsFile ) , asm ) ;
627
+ writeFile ( args . asmjsFile , asm , baseDir ) ;
627
628
} else if ( ! hasStdout ) {
628
629
stats . emitCount ++ ;
629
630
stats . emitTime += measure ( ( ) => {
@@ -643,7 +644,7 @@ exports.main = function main(argv, options, callback) {
643
644
stats . emitTime += measure ( ( ) => {
644
645
idl = assemblyscript . buildIDL ( program ) ;
645
646
} ) ;
646
- writeFile ( path . join ( baseDir , args . idlFile ) , idl ) ;
647
+ writeFile ( args . idlFile , idl , baseDir ) ;
647
648
} else if ( ! hasStdout ) {
648
649
stats . emitCount ++ ;
649
650
stats . emitTime += measure ( ( ) => {
@@ -663,7 +664,7 @@ exports.main = function main(argv, options, callback) {
663
664
stats . emitTime += measure ( ( ) => {
664
665
tsd = assemblyscript . buildTSD ( program ) ;
665
666
} ) ;
666
- writeFile ( path . join ( baseDir , args . tsdFile ) , tsd ) ;
667
+ writeFile ( args . tsdFile , tsd , baseDir ) ;
667
668
} else if ( ! hasStdout ) {
668
669
stats . emitCount ++ ;
669
670
stats . emitTime += measure ( ( ) => {
@@ -683,7 +684,7 @@ exports.main = function main(argv, options, callback) {
683
684
stats . emitTime += measure ( ( ) => {
684
685
wat = module . toText ( ) ;
685
686
} ) ;
686
- writeFile ( path . join ( baseDir , args . textFile ) , wat ) ;
687
+ writeFile ( args . textFile , wat , baseDir ) ;
687
688
} else if ( ! hasStdout ) {
688
689
stats . emitCount ++ ;
689
690
stats . emitTime += measure ( ( ) => {
@@ -700,28 +701,28 @@ exports.main = function main(argv, options, callback) {
700
701
}
701
702
return callback ( null ) ;
702
703
703
- function readFileNode ( filename ) {
704
+ function readFileNode ( filename , baseDir ) {
704
705
try {
705
706
let text ;
706
707
stats . readCount ++ ;
707
708
stats . readTime += measure ( ( ) => {
708
- text = fs . readFileSync ( filename , { encoding : "utf8" } ) ;
709
+ text = fs . readFileSync ( path . join ( baseDir , filename ) , { encoding : "utf8" } ) ;
709
710
} ) ;
710
711
return text ;
711
712
} catch ( e ) {
712
713
return null ;
713
714
}
714
715
}
715
716
716
- function writeFileNode ( filename , contents ) {
717
+ function writeFileNode ( filename , contents , baseDir ) {
717
718
try {
718
719
stats . writeCount ++ ;
719
720
stats . writeTime += measure ( ( ) => {
720
- mkdirp ( path . dirname ( filename ) ) ;
721
+ mkdirp ( path . join ( baseDir , path . dirname ( filename ) ) ) ;
721
722
if ( typeof contents === "string" ) {
722
- fs . writeFileSync ( filename , contents , { encoding : "utf8" } ) ;
723
+ fs . writeFileSync ( path . join ( baseDir , filename ) , contents , { encoding : "utf8" } ) ;
723
724
} else {
724
- fs . writeFileSync ( filename , contents ) ;
725
+ fs . writeFileSync ( path . join ( baseDir , filename ) , contents ) ;
725
726
}
726
727
} ) ;
727
728
return true ;
@@ -730,11 +731,11 @@ exports.main = function main(argv, options, callback) {
730
731
}
731
732
}
732
733
733
- function listFilesNode ( dirname ) {
734
+ function listFilesNode ( dirname , baseDir ) {
734
735
var files ;
735
736
try {
736
737
stats . readTime += measure ( ( ) => {
737
- files = fs . readdirSync ( dirname ) . filter ( file => / ^ (? ! .* \. d \. t s $ ) .* \. t s $ / . test ( file ) ) ;
738
+ files = fs . readdirSync ( path . join ( baseDir , dirname ) ) . filter ( file => / ^ (? ! .* \. d \. t s $ ) .* \. t s $ / . test ( file ) ) ;
738
739
} ) ;
739
740
return files ;
740
741
} catch ( e ) {
0 commit comments