88// before this stage, which just does the final conversion to JavaScript.
99
1010import assert from 'node:assert' ;
11+ import * as fs from 'node:fs/promises' ;
1112import * as path from 'node:path' ;
1213import {
1314 ATEXITS ,
@@ -28,7 +29,6 @@ import {
2829 isDecorator ,
2930 isJsOnlySymbol ,
3031 compileTimeContext ,
31- print ,
3232 printErr ,
3333 readFile ,
3434 warn ,
@@ -164,14 +164,23 @@ function preJS() {
164164 return result ;
165165}
166166
167- export function runJSify ( symbolsOnly ) {
167+ export async function runJSify ( outputFile , symbolsOnly ) {
168168 const libraryItems = [ ] ;
169169 const symbolDeps = { } ;
170170 const asyncFuncs = [ ] ;
171171 let postSets = [ ] ;
172172
173173 LibraryManager . load ( ) ;
174174
175+ let outputHandle = process . stdout ;
176+ if ( outputFile ) {
177+ outputHandle = await fs . open ( outputFile , 'w' ) ;
178+ }
179+
180+ async function writeOutput ( str ) {
181+ await outputHandle . write ( str + '\n' ) ;
182+ }
183+
175184 const symbolsNeeded = DEFAULT_LIBRARY_FUNCS_TO_INCLUDE ;
176185 symbolsNeeded . push ( ...extraLibraryFuncs ) ;
177186 for ( const sym of EXPORTED_RUNTIME_METHODS ) {
@@ -702,7 +711,7 @@ function(${args}) {
702711 }
703712
704713 function includeFile ( fileName , alwaysPreprocess = true ) {
705- print ( getIncludeFile ( fileName , alwaysPreprocess ) ) ;
714+ writeOutput ( getIncludeFile ( fileName , alwaysPreprocess ) ) ;
706715 }
707716
708717 function finalCombiner ( ) {
@@ -734,11 +743,11 @@ function(${args}) {
734743 includeFile ( preFile ) ;
735744
736745 for ( const item of libraryItems . concat ( postSets ) ) {
737- print ( indentify ( item || '' , 2 ) ) ;
746+ writeOutput ( indentify ( item || '' , 2 ) ) ;
738747 }
739748
740749 if ( PTHREADS ) {
741- print ( `
750+ writeOutput ( `
742751// proxiedFunctionTable specifies the list of functions that can be called
743752// either synchronously or asynchronously from other threads in postMessage()d
744753// or internally queued events. This way a pthread in a Worker can synchronously
@@ -753,7 +762,7 @@ var proxiedFunctionTable = [
753762 // that we have here, together with the rest of the output
754763 // that we started to print out earlier (see comment on the
755764 // "Final shape that will be created").
756- print ( '// EMSCRIPTEN_END_FUNCS\n' ) ;
765+ writeOutput ( '// EMSCRIPTEN_END_FUNCS\n' ) ;
757766
758767 const postFile = MINIMAL_RUNTIME ? 'postamble_minimal.js' : 'postamble.js' ;
759768 includeFile ( postFile ) ;
@@ -770,7 +779,7 @@ var proxiedFunctionTable = [
770779 throw Error ( 'Aborting compilation due to previous errors' ) ;
771780 }
772781
773- print (
782+ writeOutput (
774783 '//FORWARDED_DATA:' +
775784 JSON . stringify ( {
776785 librarySymbols,
@@ -789,7 +798,7 @@ var proxiedFunctionTable = [
789798 }
790799
791800 if ( symbolsOnly ) {
792- print (
801+ writeOutput (
793802 JSON . stringify ( {
794803 deps : symbolDeps ,
795804 asyncFuncs,
@@ -803,6 +812,8 @@ var proxiedFunctionTable = [
803812 if ( errorOccured ( ) ) {
804813 throw Error ( 'Aborting compilation due to previous errors' ) ;
805814 }
815+
816+ if ( outputFile ) await outputHandle . close ( ) ;
806817}
807818
808819addToCompileTimeContext ( {
0 commit comments