@@ -3,28 +3,32 @@ const fs = require("fs");
3
3
const os = require ( "os" ) ;
4
4
5
5
// Use distribution files if present, otherwise run the sources directly
6
- const { assemblyscript, isDev } = ( function bootstrap ( ) {
7
- var assemblyscript , isDev ;
6
+ var assemblyscript , isDev ;
7
+ try {
8
+ assemblyscript = require ( "../dist/assemblyscript.js" ) ;
9
+ isDev = false ;
10
+ try { require ( "source-map-support" ) . install ( ) ; } catch ( e ) { } // optional
11
+ } catch ( e ) {
8
12
try {
9
- assemblyscript = require ( "../dist/assemblyscript.js" ) ;
10
- isDev = false ;
11
- try { require ( "source-map-support" ) . install ( ) ; } catch ( e ) { } // optional
12
- } catch ( e ) {
13
13
require ( "ts-node" ) . register ( { project : require ( "path" ) . join ( __dirname , ".." , "src" ) } ) ;
14
14
require ( "../src/glue/js" ) ;
15
15
assemblyscript = require ( "../src" ) ;
16
16
isDev = true ;
17
+ } catch ( e ) {
18
+ assemblyscript = require ( "./assemblyscript" ) ; // last resort: browser bundle under node
19
+ isDev = false ;
17
20
}
18
- return { assemblyscript, isDev } ;
19
- } ) ( ) ;
21
+ }
20
22
21
23
// Common constants
22
- const VERSION = require ( "../package.json" ) . version + ( isDev ? "-dev" : "" ) ;
24
+
25
+ const VERSION = typeof BUNDLE_VERSION === "string" ? BUNDLE_VERSION : require ( "../package.json" ) . version + ( isDev ? "-dev" : "" ) ;
23
26
const OPTIONS = require ( "./asc.json" ) ;
24
27
const SOURCEMAP_ROOT = "assemblyscript:///" ;
25
28
const LIBRARY_PREFIX = assemblyscript . LIBRARY_PREFIX ;
26
29
const DEFAULT_OPTIMIZE_LEVEL = 2 ;
27
30
const DEFAULT_SHRINK_LEVEL = 1 ;
31
+ const LIBRARY = typeof BUNDLE_LIBRARY !== "undefined" ? BUNDLE_LIBRARY : { } ;
28
32
29
33
exports . VERSION = VERSION ;
30
34
@@ -37,6 +41,18 @@ function main(argv, options, callback) {
37
41
38
42
const stdout = options . stdout || process . stdout ;
39
43
const stderr = options . stderr || process . stderr ;
44
+ const readFile = options . readFile || readFileNode ;
45
+ const writeFile = options . writeFile || writeFileNode ;
46
+ const listFiles = options . listFiles || listFilesNode ;
47
+
48
+ // All of the above must be specified in browser environments
49
+ if ( ! stdout ) throw Error ( "'options.stdout' must be specified" ) ;
50
+ if ( ! stderr ) throw Error ( "'options.stderr' must be specified" ) ;
51
+ if ( ! fs . readFileSync ) {
52
+ if ( readFile === readFileNode ) throw Error ( "'options.readFile' must be specified" ) ;
53
+ if ( writeFile === writeFileNode ) throw Error ( "'options.writeFile' must be specified" ) ;
54
+ if ( listFiles === listFilesNode ) throw Error ( "'options.listFiles' must be specified" ) ;
55
+ }
40
56
41
57
// Record compilation times
42
58
const stats = createStats ( ) ;
@@ -98,7 +114,8 @@ function main(argv, options, callback) {
98
114
const baseDir = args . baseDir != null ? path . resolve ( args . baseDir ) : process . cwd ( ) ;
99
115
100
116
// Include standard library if --noLib isn't set
101
- const libDirs = args . noLib ? [ ] : [ path . join ( __dirname , ".." , "std" , "assembly" ) ] ;
117
+ const stdLibDir = path . join ( __dirname , ".." , "std" , "assembly" ) ;
118
+ const libDirs = args . noLib ? [ ] : [ stdLibDir ] ;
102
119
103
120
// Include custom library components (with or without stdlib)
104
121
if ( args . lib ) {
@@ -136,10 +153,14 @@ function main(argv, options, callback) {
136
153
// Load library file if explicitly requested
137
154
if ( sourcePath . startsWith ( LIBRARY_PREFIX ) ) {
138
155
for ( let i = 0 , k = libDirs . length ; i < k ; ++ i ) {
139
- sourceText = readFile ( path . join ( libDirs [ i ] , sourcePath . substring ( LIBRARY_PREFIX . length ) + ".ts" ) ) ;
140
- if ( sourceText !== null ) {
141
- sourcePath += ".ts" ;
142
- break ;
156
+ if ( LIBRARY . hasOwnProperty ( sourcePath ) )
157
+ sourceText = LIBRARY [ sourcePath ] ;
158
+ else {
159
+ sourceText = readFile ( path . join ( libDirs [ i ] , sourcePath . substring ( LIBRARY_PREFIX . length ) + ".ts" ) ) ;
160
+ if ( sourceText !== null ) {
161
+ sourcePath += ".ts" ;
162
+ break ;
163
+ }
143
164
}
144
165
}
145
166
@@ -149,11 +170,15 @@ function main(argv, options, callback) {
149
170
if ( sourceText === null ) {
150
171
sourceText = readFile ( path . join ( baseDir , sourcePath , "index.ts" ) ) ;
151
172
if ( sourceText === null ) {
152
- for ( let i = 0 , k = libDirs . length ; i < k ; ++ i ) {
153
- sourceText = readFile ( path . join ( libDirs [ i ] , sourcePath + ".ts" ) ) ;
154
- if ( sourceText !== null ) {
155
- sourcePath = LIBRARY_PREFIX + sourcePath + ".ts" ;
156
- break ;
173
+ for ( let i = 0 , k = libDirs . length ; i < k ; ++ i ) {
174
+ if ( LIBRARY . hasOwnProperty ( LIBRARY_PREFIX + sourcePath ) )
175
+ sourceText = LIBRARY [ LIBRARY_PREFIX + sourcePath ] ;
176
+ else {
177
+ sourceText = readFile ( path . join ( libDirs [ i ] , sourcePath + ".ts" ) ) ;
178
+ if ( sourceText !== null ) {
179
+ sourcePath = LIBRARY_PREFIX + sourcePath + ".ts" ;
180
+ break ;
181
+ }
157
182
}
158
183
}
159
184
if ( sourceText === null )
@@ -171,10 +196,18 @@ function main(argv, options, callback) {
171
196
}
172
197
173
198
// Include (other) library components
199
+ var hasBundledLibrary = false ;
200
+ if ( ! args . noLib )
201
+ Object . keys ( LIBRARY ) . forEach ( libPath => {
202
+ if ( libPath . lastIndexOf ( "/" ) >= LIBRARY_PREFIX . length ) return ;
203
+ stats . parseCount ++ ;
204
+ stats . parseTime += measure ( ( ) => { parser = assemblyscript . parseFile ( LIBRARY [ libPath ] , libPath + ".ts" , parser , false ) ; } ) ;
205
+ hasBundledLibrary = true ;
206
+ } ) ;
174
207
for ( let i = 0 , k = libDirs . length ; i < k ; ++ i ) {
208
+ if ( i === 0 && hasBundledLibrary ) continue ;
175
209
let libDir = libDirs [ i ] ;
176
- let libFiles ;
177
- stats . readTime += measure ( ( ) => { libFiles = require ( "glob" ) . sync ( "*.ts" , { cwd : libDir } ) } ) ;
210
+ let libFiles = listFiles ( libDir ) ;
178
211
for ( let j = 0 , l = libFiles . length ; j < l ; ++ j ) {
179
212
let libPath = libFiles [ j ] ;
180
213
let libText = readFile ( path . join ( libDir , libPath ) ) ;
@@ -383,18 +416,18 @@ function main(argv, options, callback) {
383
416
printStats ( stats , stderr ) ;
384
417
return callback ( null ) ;
385
418
386
- function readFile ( filename ) {
419
+ function readFileNode ( filename ) {
387
420
try {
388
421
var text ;
389
422
stats . readCount ++ ;
390
- stats . readTime += measure ( ( ) => text = fs . readFileSync ( filename , { encoding : "utf8" } ) ) ;
423
+ stats . readTime += measure ( ( ) => { text = fs . readFileSync ( filename , { encoding : "utf8" } ) ; } ) ;
391
424
return text ;
392
425
} catch ( e ) {
393
426
return null ;
394
427
}
395
428
}
396
429
397
- function writeFile ( filename , contents ) {
430
+ function writeFileNode ( filename , contents ) {
398
431
try {
399
432
stats . writeCount ++ ;
400
433
stats . writeTime += measure ( ( ) => fs . writeFileSync ( filename , contents , typeof contents === "string" ? { encoding : "utf8" } : undefined ) ) ;
@@ -404,6 +437,16 @@ function main(argv, options, callback) {
404
437
}
405
438
}
406
439
440
+ function listFilesNode ( dirname ) {
441
+ var files ;
442
+ try {
443
+ stats . readTime += measure ( ( ) => { files = require ( "glob" ) . sync ( "*.ts" , { cwd : dirname } ) } ) ;
444
+ return files ;
445
+ } catch ( e ) {
446
+ return [ ] ;
447
+ }
448
+ }
449
+
407
450
function writeStdout ( contents ) {
408
451
if ( ! writeStdout . used ) {
409
452
stats . writeCount ++ ;
@@ -464,7 +507,8 @@ function createStats() {
464
507
} ;
465
508
}
466
509
467
- exports . createStats = createStats ;
510
+ if ( ! process . hrtime )
511
+ process . hrtime = require ( "browser-process-hrtime" ) ;
468
512
469
513
function measure ( fn ) {
470
514
const start = process . hrtime ( ) ;
@@ -488,3 +532,24 @@ function printStats(stats, output) {
488
532
}
489
533
490
534
exports . printStats = printStats ;
535
+
536
+ function createMemoryStream ( fn ) {
537
+ var stream = [ ] ;
538
+ stream . write = function ( chunk ) {
539
+ if ( typeof chunk === "string" ) {
540
+ this . push ( Buffer . from ( chunk , "utf8" ) ) ;
541
+ } else {
542
+ this . push ( chunk ) ;
543
+ }
544
+ if ( fn ) fn ( chunk ) ;
545
+ } ;
546
+ stream . toBuffer = function ( ) {
547
+ return Buffer . concat ( this ) ;
548
+ } ;
549
+ stream . toString = function ( ) {
550
+ return this . toBuffer ( ) . toString ( "utf8" ) ;
551
+ } ;
552
+ return stream ;
553
+ }
554
+
555
+ exports . createMemoryStream = createMemoryStream ;
0 commit comments