Skip to content

Commit

Permalink
wasm gc: preprocess JS runtime with uglifyjs, add modular runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Oct 11, 2024
1 parent 14a4a99 commit cb3ce47
Show file tree
Hide file tree
Showing 12 changed files with 746 additions and 616 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ build
/build-cache
/deploy-with-env.sh
/release-with-env.sh
/.gradle
/build-logic/.gradle
.gradle
/relocated
/samples/.gradle
local.properties
teavm-local.properties
.kotlin
node_modules

# KDE
.directory
Expand Down
56 changes: 56 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.github.gradle.node.npm.task.NpmTask

/*
* Copyright 2023 Alexey Andreev.
*
Expand All @@ -17,10 +19,15 @@
plugins {
`java-library`
`teavm-publish`
alias(libs.plugins.nodejs)
}

description = "Compiler, backends and runtime"

node {
download = true
}

dependencies {
api(project(":interop:core"))
api(project(":metaprogramming:api"))
Expand All @@ -35,6 +42,55 @@ dependencies {
testImplementation(libs.junit)
}

val jsOutputDir = layout.buildDirectory.dir("generated/js")
val jsOutputPackageDir = jsOutputDir.map { it.dir("org/teavm/backend/wasm") }
val jsInputDir = layout.projectDirectory.dir("src/main/js/wasm-gc-runtime")
val jsInput = jsInputDir.file("runtime.js")

fun registerRuntimeTasks(taskName: String, wrapperType: String, outputName: String) {
val generateTask by tasks.register<DefaultTask>("generate${taskName}Runtime") {
val wrapperFile = jsInputDir.file(wrapperType)
val runtimeFile = jsInput
val outputFile = jsOutputPackageDir.map { it.file("$outputName.js") }
inputs.files(wrapperFile, runtimeFile)
outputs.file(outputFile)
doLast {
val wrapper = wrapperFile.asFile.readText()
var runtime = runtimeFile.asFile.readText()
val startText = "// !BEGINNING!\n"
val startIndex = runtime.indexOf(startText)
if (startIndex >= 0) {
runtime = runtime.substring(startIndex + startText.length)
}
outputFile.get().asFile.writeText(wrapper.replace("include();", runtime))
}
}

val optimizeTask = tasks.register<NpmTask>("optimize${taskName}Runtime") {
val inputFiles = generateTask.outputs.files
val outputFile = jsOutputPackageDir.map { it.file("$outputName.min.js") }
inputs.files(inputFiles)
outputs.file(outputFile)
npmCommand.addAll("run", "uglify")
args.addAll(provider {
listOf(
"--",
"-m", "--module", "--toplevel",
inputFiles.singleFile.absolutePath,
"-o", outputFile.get().asFile.absolutePath
)
})
}

sourceSets.main.configure {
output.dir(mapOf("builtBy" to generateTask), jsOutputDir)
output.dir(mapOf("builtBy" to optimizeTask), jsOutputDir)
}
}

registerRuntimeTasks("Simple", "simple-wrapper.js", "wasm-gc-runtime")
registerRuntimeTasks("Module", "module-wrapper.js", "wasm-gc-module-runtime")

teavmPublish {
artifactId = "teavm-core"
}
25 changes: 25 additions & 0 deletions core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"uglify": "uglifyjs"
},
"devDependencies": {
"uglify-js": "3.19.3"
}
}
18 changes: 18 additions & 0 deletions core/src/main/js/wasm-gc-runtime/module-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2024 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

include();
export { load, defaults };
Loading

0 comments on commit cb3ce47

Please sign in to comment.