11import * as fs from 'fs' ;
22import * as os from 'os' ;
33import * as path from 'path' ;
4- import { exec , ExecException } from 'child_process' ;
4+ import { exec , ExecException , spawn } from 'child_process' ;
55import { SourceMap } from '../sourcemap/SourceMap' ;
66import * as readline from 'readline' ;
77import { EventEmitter } from 'events' ;
@@ -94,17 +94,16 @@ export class WatCompiler extends Compiler {
9494 // compile WAT to Wasm
9595 return new Promise < CompileOutput > ( ( resolve , reject ) => {
9696 const file = `${ dir } /upload.wasm` ;
97- const command = `${ this . wabt } wat2wasm --no-canonicalize-leb128s --disable-bulk-memory --debug-names -v -o ${ file } ${ program } ` ;
97+ const command = `${ this . wabt } wat2wasm` ;
98+ const args = [ '--no-canonicalize-leb128s' , '--disable-bulk-memory' , '--debug-names' , '-v' , '-o' , file , program ] ;
9899 let out : string = '' ;
99100 let err : string = '' ;
100101
101- function handle ( error : ExecException | null , stdout : string ) {
102- out = stdout ;
103- err = error ?. message ?? '' ;
104- }
102+ const compile = spawn ( command , args ) ;
103+ this . emit ( CompilationEvents . started , `${ command } ${ args . join ( ' ' ) } ` ) ;
105104
106- const compile = exec ( command , handle ) ;
107- this . emit ( CompilationEvents . started , command ) ;
105+ compile . stdout . on ( 'data' , ( data ) => out += data . toString ( ) ) ;
106+ compile . stderr . on ( 'data' , ( data ) => err += data . toString ( ) ) ;
108107
109108 compile . on ( 'close' , ( code ) => {
110109 if ( code !== 0 ) {
@@ -122,24 +121,28 @@ export class WatCompiler extends Compiler {
122121 public dump ( output : CompileOutput ) : Promise < CompileOutput > {
123122 // object dump
124123 return new Promise < CompileOutput > ( ( resolve , reject ) => {
125- const command = `${ this . wabt } wasm-objdump -x -m ${ output . file } ` ;
124+ const executable = `${ this . wabt } wasm-objdump` ;
125+ const args = [ '-x' , '-m' , output . file ] ;
126+ let stdout = '' ;
126127
127- const compile = exec ( command , ( error : ExecException | null , stdout : string ) => {
128- output . map = this . parseWasmObjDump ( output , stdout . toString ( ) ) ;
129- this . emit ( CompilationEvents . sourcemap ) ;
130- resolve ( output ) ;
131- } ) ;
128+ const dump = spawn ( executable , args ) ;
132129
133- compile . on ( 'close' , ( code ) => {
130+ dump . stdout . on ( 'data' , ( data ) => stdout += data . toString ( ) ) ;
131+ dump . on ( 'error' , ( err ) => reject ( `wasm-objdump error: ${ err . message } ` ) ) ;
132+
133+ dump . on ( 'close' , ( code ) => {
134134 if ( code !== 0 ) {
135135 reject ( `wasm-objdump exited with code ${ code } ` ) ;
136136 return ;
137137 }
138+ output . map = this . parseWasmObjDump ( output , stdout ) ;
139+ this . emit ( CompilationEvents . sourcemap ) ;
140+ resolve ( output ) ;
138141 } ) ;
139142 } ) ;
140143 }
141144
142- public async map ( program : string ) : Promise < CompileOutput > {
145+ public async map ( program : string ) : Promise < CompileOutput > {
143146 return this . compile ( program ) . then ( ( output ) => {
144147 return this . dump ( output ) ;
145148 } ) ;
0 commit comments