1313
1414const fs = require ( "fs" ) ;
1515const path = require ( "path" ) ;
16- const os = require ( "os" ) ;
16+ const utf8 = require ( "./util/utf8" ) ;
17+ const EOL = process . platform === "win32" ? "\r\n" : "\n" ;
1718
1819// Use distribution files if present, otherwise run the sources directly
1920var assemblyscript , isDev ;
@@ -152,15 +153,15 @@ exports.main = function main(argv, options, callback) {
152153 if ( ! callback ) callback = function defaultCallback ( err ) {
153154 var code = 0 ;
154155 if ( err ) {
155- stderr . write ( err . stack + os . EOL ) ;
156+ stderr . write ( err . stack + EOL ) ;
156157 code = 1 ;
157158 }
158159 return code ;
159160 } ;
160161
161162 // Just print the version if requested
162163 if ( args . version ) {
163- stdout . write ( "Version " + exports . version + ( isDev ? "-dev" : "" ) + os . EOL ) ;
164+ stdout . write ( "Version " + exports . version + ( isDev ? "-dev" : "" ) + EOL ) ;
164165 return callback ( null ) ;
165166 }
166167 // Print the help message if requested or no source files are provided
@@ -181,7 +182,7 @@ exports.main = function main(argv, options, callback) {
181182 for ( let i = 0 ; i < indent ; ++ i ) {
182183 line = " " + line ;
183184 }
184- return os . EOL + line ;
185+ return EOL + line ;
185186 } ) . join ( "" ) ) ;
186187 } else {
187188 opts . push ( text + option . desc ) ;
@@ -197,7 +198,7 @@ exports.main = function main(argv, options, callback) {
197198 " asc hello1.ts hello2.ts -b -O > hello.wasm" ,
198199 "" ,
199200 "Options:"
200- ] . concat ( opts ) . join ( os . EOL ) + os . EOL ) ;
201+ ] . concat ( opts ) . join ( EOL ) + EOL ) ;
201202 return callback ( null ) ;
202203 }
203204
@@ -566,7 +567,7 @@ exports.main = function main(argv, options, callback) {
566567 path . basename ( sourceMapURL )
567568 ) , JSON . stringify ( sourceMap ) ) ;
568569 } else {
569- stderr . write ( "Skipped source map (stdout already occupied)" + os . EOL ) ;
570+ stderr . write ( "Skipped source map (stdout already occupied)" + EOL ) ;
570571 }
571572 }
572573 }
@@ -741,7 +742,7 @@ function checkDiagnostics(emitter, stderr) {
741742 while ( ( diagnostic = assemblyscript . nextDiagnostic ( emitter ) ) != null ) {
742743 stderr . write (
743744 assemblyscript . formatDiagnostic ( diagnostic , stderr . isTTY , true ) +
744- os . EOL + os . EOL
745+ EOL + EOL
745746 ) ;
746747 if ( assemblyscript . isError ( diagnostic ) ) hasErrors = true ;
747748 }
@@ -803,24 +804,36 @@ function printStats(stats, output) {
803804 "Emit : " + format ( stats . emitTime , stats . emitCount ) ,
804805 "Validate : " + format ( stats . validateTime , stats . validateCount ) ,
805806 "Optimize : " + format ( stats . optimizeTime , stats . optimizeCount )
806- ] . join ( os . EOL ) + os . EOL ) ;
807+ ] . join ( EOL ) + EOL ) ;
807808}
808809
809810exports . printStats = printStats ;
810811
812+ var Buf = typeof global !== "undefined" && global . Buffer || Uint8Array ;
813+
811814/** Creates a memory stream that can be used in place of stdout/stderr. */
812815function createMemoryStream ( fn ) {
813816 var stream = [ ] ;
814817 stream . write = function ( chunk ) {
815818 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 ;
819822 }
823+ this . push ( chunk ) ;
820824 if ( fn ) fn ( chunk ) ;
821825 } ;
822826 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 ;
824837 } ;
825838 stream . toString = function ( ) {
826839 return this . toBuffer ( ) . toString ( "utf8" ) ;
0 commit comments