@@ -12,6 +12,7 @@ import { coreCount, threadCount } from "../util/cpu.js";
1212import { diff } from "../util/text.js" ;
1313import { Rtrace } from "../lib/rtrace/index.js" ;
1414import asc from "../dist/asc.js" ;
15+ import { exec } from "child_process" ;
1516
1617const dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
1718const require = createRequire ( import . meta. url ) ;
@@ -143,7 +144,10 @@ async function runTest(basename) {
143144 // Makes sure to reset the environment after
144145 function prepareResult ( code , message = null ) {
145146 if ( v8_no_flags ) v8 . setFlagsFromString ( v8_no_flags ) ;
146- if ( ! args . createBinary ) fs . unlink ( path . join ( basedir , basename + ".debug.wasm" ) , err => { /* nop */ } ) ;
147+ if ( ! args . createBinary ) {
148+ fs . unlink ( path . join ( basedir , basename + ".debug.wasm" ) , err => { /* nop */ } ) ;
149+ fs . unlink ( path . join ( basedir , basename + ".debug.wasm.map" ) , err => { /* nop */ } ) ;
150+ }
147151 return { code, message } ;
148152 }
149153
@@ -253,6 +257,40 @@ async function runTest(basename) {
253257 }
254258 compareFixture . end ( SUCCESS ) ;
255259 }
260+
261+ // compare fixture source map
262+ const sourcemapCompareFixture = section ( "compare fixture source map" ) ;
263+ const debugWasmPath = path . join ( basedir , basename + ".debug.wasm" ) ;
264+ const debugSourceMapPath = path . join ( basedir , basename + ".debug.wasm.map" ) ;
265+ const sourceMapFixturePath = path . join ( basedir , basename + ".debug.sourcemap.wat" ) ;
266+
267+ if ( fs . existsSync ( debugSourceMapPath ) && fs . existsSync ( sourceMapFixturePath ) ) {
268+ const sourceMapCmd = `node node_modules/.bin/wasm-opt ${ debugWasmPath } -ism ${ debugSourceMapPath } --print` ;
269+ exec ( sourceMapCmd , ( err , stdout , stderr ) => {
270+ const actual = stdout . toString ( ) . replace ( / \r \n / g, "\n" ) ;
271+ if ( args . create ) {
272+ fs . writeFileSync ( sourceMapFixturePath , actual , { encoding : "utf8" } ) ;
273+ console . log ( " " + stdoutColors . yellow ( "Created fixture" ) ) ;
274+ sourcemapCompareFixture . end ( SKIPPED ) ;
275+ } else {
276+ const expected = fs . readFileSync ( sourceMapFixturePath , { encoding : "utf8" } ) . replace ( / \r \n / g, "\n" ) ;
277+ if ( args . noDiff ) {
278+ if ( expected != actual ) {
279+ compareFixture . end ( FAILURE ) ;
280+ return prepareResult ( FAILURE , "fixture mismatch" ) ;
281+ }
282+ } else {
283+ let diffs = diff ( basename + ".debug.sourcemap.wat" , expected , actual ) ;
284+ if ( diffs !== null ) {
285+ console . log ( diffs ) ;
286+ compareFixture . end ( FAILURE ) ;
287+ return prepareResult ( FAILURE , "fixture mismatch" ) ;
288+ }
289+ }
290+ compareFixture . end ( SUCCESS ) ;
291+ }
292+ } ) ;
293+ }
256294 }
257295
258296 stdout . length = 0 ;
0 commit comments