Skip to content

Commit 9f8295d

Browse files
authored
Merge pull request tanishiking#85 from sjrd/use-config
Honor some of the linker config options to emit less stuff
2 parents 038d769 + c556be5 commit 9f8295d

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ jobs:
3838
run: sbt scalajs-test-suite/Test/fastLinkJS
3939
- name: Run the Scala.js test suite
4040
run: sbt scalajs-test-suite/test
41+
- name: Link and run the Scala.js test suite with fullLinkJS
42+
run: sbt 'set Global/scalaJSStage := FullOptStage' scalajs-test-suite/test
43+
44+
# Make sure we can emit the .wat file without crashing
45+
- name: Link the Scala.js test suite with PrettyPrint
46+
run: sbt 'set `scalajs-test-suite`/scalaJSLinkerConfig ~= { _.withPrettyPrint(true) }' scalajs-test-suite/Test/fastLinkJS
4147

4248
- name: Format
4349
run: sbt scalafmtCheckAll

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ You can build and run it like any other Scala.js project from sbt:
3535
You may want to look at the output in `sample/target/scala-2.12/sample-fastopt/` to convince yourself that it was compiled to WebAssembly.
3636

3737
In that directory, you will also find a `main.wat` file, which is not used for execution.
38-
It contains the WebAsembly Text Format representation of `main.wasm`, for debugging purposes.
38+
It contains the WebAsembly Text Format representation of `main.wasm`, for exploratory and debugging purposes.
39+
This is only true by default for the `sample`.
3940

4041
:warning: If you modify the linker code, you need to `reload` and `sample/clean` for your changes to take effect on the sample.
4142

@@ -59,6 +60,9 @@ Run the unit test suite with `tests/test`.
5960
- Add a file under `test-suite`
6061
- Add a test case to `tests/src/test/scala/tests/TestSuites.scala`
6162

63+
By default, `.wat` files are not generated, as they are quite big (several hundreds of KB for most of the tests).
64+
You can enable them by adding `withPrettyPrint(true)` to the linker configuration in `tests/src/test/scala/tests/CoreTests.scala`.
65+
6266
### Scala.js integration test suite
6367

6468
Run the entire Scala.js test suite, linked with the WebAssembly backend, with:
@@ -74,6 +78,13 @@ Since recompiling the test suite from scratch every time is slow, you can replac
7478
$ rm -r scalajs-test-suite/target/scala-2.12/scalajs-test-suite-test-fastopt/
7579
```
7680

81+
By default, `.wat` files are not generated for the Scala.js test suite, as they are very big (they exceed 100 MB).
82+
It is usually easier to minimize an issue in `sample/test`, but if you really want the big `.wat` file for the Scala.js test suite, you can enable it with
83+
84+
```scala
85+
> set `scalajs-test-suite`/scalaJSLinkerConfig ~= { _.withPrettyPrint(true) }
86+
```
87+
7788
### Debugging tools
7889

7990
- The WasmGC reference interpreter can be used to validate and convert between the binary and text form:

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ lazy val sample = project
5353
.enablePlugins(WasmLinkerPlugin, ScalaJSJUnitPlugin)
5454
.settings(
5555
scalaVersion := scalaV,
56-
scalaJSUseMainModuleInitializer := true
56+
scalaJSUseMainModuleInitializer := true,
57+
// Emit .wat files for exploratory and debugging purposes
58+
scalaJSLinkerConfig ~= { _.withPrettyPrint(true) },
5759
)
5860

5961
lazy val testSuite = project

wasm/src/main/scala/WebAssemblyLinkerBackend.scala

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,48 @@ final class WebAssemblyLinkerBackend(
106106
val jsFileName = OutputPatternsImpl.jsFile(linkerConfig.outputPatterns, moduleID)
107107
val loaderJSFileName = OutputPatternsImpl.jsFile(linkerConfig.outputPatterns, "__loader")
108108

109-
val textOutput = new converters.WasmTextWriter().write(wasmModule)
110-
val textOutputBytes = textOutput.getBytes(StandardCharsets.UTF_8)
111-
val binaryOutput = new converters.WasmBinaryWriter(wasmModule).write()
112-
val loaderOutput = LoaderContent.bytesContent
113-
val jsFileOutput =
114-
buildJSFileOutput(onlyModule, loaderJSFileName, wasmFileName, context.allImportedModules)
115-
val jsFileOutputBytes = jsFileOutput.getBytes(StandardCharsets.UTF_8)
116-
117-
val filesToProduce = Set(
118-
watFileName,
109+
val filesToProduce0 = Set(
119110
wasmFileName,
120111
loaderJSFileName,
121112
jsFileName
122113
)
114+
val filesToProduce =
115+
if (linkerConfig.prettyPrint) filesToProduce0 + watFileName
116+
else filesToProduce0
117+
118+
def maybeWriteWatFile(): Future[Unit] = {
119+
if (linkerConfig.prettyPrint) {
120+
val textOutput = new converters.WasmTextWriter().write(wasmModule)
121+
val textOutputBytes = textOutput.getBytes(StandardCharsets.UTF_8)
122+
outputImpl.writeFull(watFileName, ByteBuffer.wrap(textOutputBytes))
123+
} else {
124+
Future.unit
125+
}
126+
}
127+
128+
def writeWasmFile(): Future[Unit] = {
129+
val emitDebugInfo = !linkerConfig.minify
130+
val binaryOutput = new converters.WasmBinaryWriter(wasmModule, emitDebugInfo).write()
131+
outputImpl.writeFull(wasmFileName, ByteBuffer.wrap(binaryOutput))
132+
}
133+
134+
def writeLoaderFile(): Future[Unit] =
135+
outputImpl.writeFull(loaderJSFileName, ByteBuffer.wrap(LoaderContent.bytesContent))
136+
137+
def writeJSFile(): Future[Unit] = {
138+
val jsFileOutput =
139+
buildJSFileOutput(onlyModule, loaderJSFileName, wasmFileName, context.allImportedModules)
140+
val jsFileOutputBytes = jsFileOutput.getBytes(StandardCharsets.UTF_8)
141+
outputImpl.writeFull(jsFileName, ByteBuffer.wrap(jsFileOutputBytes))
142+
}
123143

124144
for {
125145
existingFiles <- outputImpl.listFiles()
126146
_ <- Future.sequence(existingFiles.filterNot(filesToProduce).map(outputImpl.delete(_)))
127-
_ <- outputImpl.writeFull(watFileName, ByteBuffer.wrap(textOutputBytes))
128-
_ <- outputImpl.writeFull(wasmFileName, ByteBuffer.wrap(binaryOutput))
129-
_ <- outputImpl.writeFull(loaderJSFileName, ByteBuffer.wrap(loaderOutput))
130-
_ <- outputImpl.writeFull(jsFileName, ByteBuffer.wrap(jsFileOutputBytes))
147+
_ <- maybeWriteWatFile()
148+
_ <- writeWasmFile()
149+
_ <- writeLoaderFile()
150+
_ <- writeJSFile()
131151
} yield {
132152
val reportModule = new ReportImpl.ModuleImpl(
133153
moduleID,

wasm/src/main/scala/converters/WasmBinaryWriter.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import wasm.wasm4s.Types.WasmHeapType.Func
1414
import wasm.wasm4s.Types.WasmHeapType.Simple
1515
import wasm.wasm4s.WasmInstr.END
1616

17-
final class WasmBinaryWriter(module: WasmModule) {
17+
final class WasmBinaryWriter(module: WasmModule, emitDebugInfo: Boolean) {
1818
import WasmBinaryWriter._
1919

2020
private val allTypeDefinitions: List[WasmTypeDefinition[_ <: WasmTypeName]] = {
@@ -103,7 +103,9 @@ final class WasmBinaryWriter(module: WasmModule) {
103103
writeSection(fullOutput, SectionDataCount)(writeDataCountSection(_))
104104
writeSection(fullOutput, SectionCode)(writeCodeSection(_))
105105
writeSection(fullOutput, SectionData)(writeDataSection(_))
106-
writeCustomSection(fullOutput, "name")(writeNameCustomSection(_))
106+
107+
if (emitDebugInfo)
108+
writeCustomSection(fullOutput, "name")(writeNameCustomSection(_))
107109

108110
fullOutput.result()
109111
}

0 commit comments

Comments
 (0)