13
13
14
14
const fs = require ( "fs" ) ;
15
15
const path = require ( "path" ) ;
16
- const os = require ( "os" ) ;
16
+ const utf8 = require ( "./util/utf8" ) ;
17
+ const EOL = process . platform === "win32" ? "\r\n" : "\n" ;
17
18
18
19
// Use distribution files if present, otherwise run the sources directly
19
20
var assemblyscript , isDev ;
@@ -152,15 +153,15 @@ exports.main = function main(argv, options, callback) {
152
153
if ( ! callback ) callback = function defaultCallback ( err ) {
153
154
var code = 0 ;
154
155
if ( err ) {
155
- stderr . write ( err . stack + os . EOL ) ;
156
+ stderr . write ( err . stack + EOL ) ;
156
157
code = 1 ;
157
158
}
158
159
return code ;
159
160
} ;
160
161
161
162
// Just print the version if requested
162
163
if ( args . version ) {
163
- stdout . write ( "Version " + exports . version + ( isDev ? "-dev" : "" ) + os . EOL ) ;
164
+ stdout . write ( "Version " + exports . version + ( isDev ? "-dev" : "" ) + EOL ) ;
164
165
return callback ( null ) ;
165
166
}
166
167
// Print the help message if requested or no source files are provided
@@ -181,7 +182,7 @@ exports.main = function main(argv, options, callback) {
181
182
for ( let i = 0 ; i < indent ; ++ i ) {
182
183
line = " " + line ;
183
184
}
184
- return os . EOL + line ;
185
+ return EOL + line ;
185
186
} ) . join ( "" ) ) ;
186
187
} else {
187
188
opts . push ( text + option . desc ) ;
@@ -197,7 +198,7 @@ exports.main = function main(argv, options, callback) {
197
198
" asc hello1.ts hello2.ts -b -O > hello.wasm" ,
198
199
"" ,
199
200
"Options:"
200
- ] . concat ( opts ) . join ( os . EOL ) + os . EOL ) ;
201
+ ] . concat ( opts ) . join ( EOL ) + EOL ) ;
201
202
return callback ( null ) ;
202
203
}
203
204
@@ -566,7 +567,7 @@ exports.main = function main(argv, options, callback) {
566
567
path . basename ( sourceMapURL )
567
568
) , JSON . stringify ( sourceMap ) ) ;
568
569
} else {
569
- stderr . write ( "Skipped source map (stdout already occupied)" + os . EOL ) ;
570
+ stderr . write ( "Skipped source map (stdout already occupied)" + EOL ) ;
570
571
}
571
572
}
572
573
}
@@ -741,7 +742,7 @@ function checkDiagnostics(emitter, stderr) {
741
742
while ( ( diagnostic = assemblyscript . nextDiagnostic ( emitter ) ) != null ) {
742
743
stderr . write (
743
744
assemblyscript . formatDiagnostic ( diagnostic , stderr . isTTY , true ) +
744
- os . EOL + os . EOL
745
+ EOL + EOL
745
746
) ;
746
747
if ( assemblyscript . isError ( diagnostic ) ) hasErrors = true ;
747
748
}
@@ -803,24 +804,36 @@ function printStats(stats, output) {
803
804
"Emit : " + format ( stats . emitTime , stats . emitCount ) ,
804
805
"Validate : " + format ( stats . validateTime , stats . validateCount ) ,
805
806
"Optimize : " + format ( stats . optimizeTime , stats . optimizeCount )
806
- ] . join ( os . EOL ) + os . EOL ) ;
807
+ ] . join ( EOL ) + EOL ) ;
807
808
}
808
809
809
810
exports . printStats = printStats ;
810
811
812
+ var Buf = typeof global !== "undefined" && global . Buffer || Uint8Array ;
813
+
811
814
/** Creates a memory stream that can be used in place of stdout/stderr. */
812
815
function createMemoryStream ( fn ) {
813
816
var stream = [ ] ;
814
817
stream . write = function ( chunk ) {
815
818
if ( typeof chunk === "string" ) {
816
- this . push ( Buffer . from ( chunk , "utf8" ) ) ;
817
- } else {
818
- this . push ( chunk ) ;
819
+ let buffer = new Buf ( utf8 . length ( chunk ) ) ;
820
+ utf8 . write ( chunk , buffer , 0 ) ;
821
+ chunk = buffer ;
819
822
}
823
+ this . push ( chunk ) ;
820
824
if ( fn ) fn ( chunk ) ;
821
825
} ;
822
826
stream . toBuffer = function ( ) {
823
- return Buffer . concat ( this ) ;
827
+ var offset = 0 , i = 0 , k = this . length ;
828
+ while ( i < k ) offset += this [ i ++ ] . length ;
829
+ var buffer = new Buf ( offset ) ;
830
+ offset = i = 0 ;
831
+ while ( i < k ) {
832
+ buffer . set ( this [ i ] , offset ) ;
833
+ offset += this [ i ] . length ;
834
+ ++ i ;
835
+ }
836
+ return buffer ;
824
837
} ;
825
838
stream . toString = function ( ) {
826
839
return this . toBuffer ( ) . toString ( "utf8" ) ;
0 commit comments