Skip to content

Commit ed1cd09

Browse files
authored
fix: Emit text format before converting to JS (#1372)
1 parent af3b489 commit ed1cd09

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Diff for: cli/asc.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,11 @@ exports.main = function main(argv, options, callback) {
719719
// Prepare output
720720
if (!args.noEmit) {
721721
let hasStdout = false;
722-
let hasOutput = false;
722+
let hasOutput = args.textFile != null
723+
|| args.binaryFile != null
724+
|| args.jsFile != null
725+
|| args.tsdFile != null
726+
|| args.idlFile != null;
723727

724728
if (args.outFile != null) {
725729
if (/\.was?t$/.test(args.outFile) && args.textFile == null) {
@@ -752,7 +756,6 @@ exports.main = function main(argv, options, callback) {
752756
writeStdout(wasm.output);
753757
hasStdout = true;
754758
}
755-
hasOutput = true;
756759

757760
// Post-process source map
758761
if (wasm.sourceMap != null) {
@@ -776,24 +779,22 @@ exports.main = function main(argv, options, callback) {
776779
}
777780
}
778781

779-
// Write JS
780-
if (args.jsFile != null) {
781-
let js;
782-
if (args.jsFile.length) {
782+
// Write text (also fallback)
783+
if (args.textFile != null || !hasOutput) {
784+
let wat;
785+
if (args.textFile != null && args.textFile.length) {
783786
stats.emitCount++;
784787
stats.emitTime += measure(() => {
785-
js = module.toAsmjs();
788+
wat = module.toText();
786789
});
787-
writeFile(args.jsFile, js, baseDir);
790+
writeFile(args.textFile, wat, baseDir);
788791
} else if (!hasStdout) {
789792
stats.emitCount++;
790793
stats.emitTime += measure(() => {
791-
js = module.toAsmjs();
794+
wat = module.toText();
792795
});
793-
writeStdout(js);
794-
hasStdout = true;
796+
writeStdout(wat);
795797
}
796-
hasOutput = true;
797798
}
798799

799800
// Write WebIDL
@@ -813,7 +814,6 @@ exports.main = function main(argv, options, callback) {
813814
writeStdout(idl);
814815
hasStdout = true;
815816
}
816-
hasOutput = true;
817817
}
818818

819819
// Write TypeScript definition
@@ -833,24 +833,23 @@ exports.main = function main(argv, options, callback) {
833833
writeStdout(tsd);
834834
hasStdout = true;
835835
}
836-
hasOutput = true;
837836
}
838837

839-
// Write text (must be last)
840-
if (args.textFile != null || !hasOutput) {
841-
let wat;
842-
if (args.textFile && args.textFile.length) {
838+
// Write JS (modifies the binary, so must be last)
839+
if (args.jsFile != null) {
840+
let js;
841+
if (args.jsFile.length) {
843842
stats.emitCount++;
844843
stats.emitTime += measure(() => {
845-
wat = module.toText();
844+
js = module.toAsmjs();
846845
});
847-
writeFile(args.textFile, wat, baseDir);
846+
writeFile(args.jsFile, js, baseDir);
848847
} else if (!hasStdout) {
849848
stats.emitCount++;
850849
stats.emitTime += measure(() => {
851-
wat = module.toText();
850+
js = module.toAsmjs();
852851
});
853-
writeStdout(wat);
852+
writeStdout(js);
854853
}
855854
}
856855
}

0 commit comments

Comments
 (0)