Commit 82d1b12c authored by Shagen Ogandzhanian's avatar Shagen Ogandzhanian

Minimal testing setup for wasm / kjs interop

parent 56b88584
...@@ -114,7 +114,15 @@ kotlin { ...@@ -114,7 +114,15 @@ kotlin {
} }
js(IR) { js(IR) {
browser() browser() {
testTask {
testLogging.showStandardStreams = true
dependsOn(project.tasks.named("wasmCompile"))
useKarma() {
useChromeHeadless()
}
}
}
binaries.executable() binaries.executable()
} }
...@@ -163,6 +171,14 @@ kotlin { ...@@ -163,6 +171,14 @@ kotlin {
implementation(kotlin("test-junit")) implementation(kotlin("test-junit"))
} }
} }
val jsTest by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
implementation(kotlin("test-js"))
}
}
if (supportNative) { if (supportNative) {
val macosX64Main by getting { val macosX64Main by getting {
} }
...@@ -328,6 +344,7 @@ project.tasks.register<Exec>("wasmCompile") { ...@@ -328,6 +344,7 @@ project.tasks.register<Exec>("wasmCompile") {
"-I$skiaDir/include", "-I$skiaDir/include",
"-I$skiaDir/include/gpu", "-I$skiaDir/include/gpu",
"-std=c++17", "-std=c++17",
"--bind",
"-o", outJs, "-o", outJs,
*libs.files.map { it.absolutePath }.toTypedArray(), *libs.files.map { it.absolutePath }.toTypedArray(),
*srcs *srcs
......
// see https://kotlinlang.org/docs/js-project-setup.html#webpack-configuration-file
const path = require("path");
config.browserConsoleLogOptions.level = "debug";
const basePath = config.basePath;
const projectPath = path.resolve(basePath, "..", "..", "..", "..", "..");
const setupPath = path.resolve(projectPath, "src", "jsTest", "config");
const wasmPath = path.resolve(projectPath, "build", "wasm")
const debug = message => console.log(`[karma-config] ${message}`);
debug(`karma basePath: ${basePath}`);
debug(`karma setupPath: ${setupPath}`);
debug(`karma wasmPath: ${wasmPath}`);
config.proxies = {
"/wasm/": wasmPath
}
config.files = [
path.resolve(setupPath, "wasmsetup.js"),
path.resolve(wasmPath, "skiko.js"),
{pattern: path.resolve(wasmPath, "skiko.wasm"), included: false, served: true, watched: false},
].concat(config.files);
const fs = require("fs");
fs.writeFileSync("KARMA.txt", JSON.stringify(config, null, 4));
#include "GrBackendSurface.h" #include "GrBackendSurface.h"
#include "GrDirectContext.h" #include "GrDirectContext.h"
#include <emscripten.h> #include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_KEEPALIVE
extern "C" void* init_surface() { extern "C" void* init_surface() {
return nullptr; return nullptr;
} }
int main() { bool ping() {
printf("Hello from WASM\n"); return true;
//void* ctx = GrDirectContext::MakeGL().release();
//printf("Context is %p\n");
return 0;
} }
EMSCRIPTEN_BINDINGS(Skiko) {
function("ping", &ping);
};
var Module = {};
Module['locateFile'] = (path, scriptDirectory) => {
return "/wasm/skiko.wasm";
};
var ModulePromised = new Promise(function(resolve, reject) {
Module['onRuntimeInitialized'] = _ => {
resolve(Module);
};
});
package org.jetbrains.skiko.wasm
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
import kotlin.coroutines.*
import kotlin.js.Promise
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
external interface ModuleInterface {
fun ping(): Boolean
}
external val ModulePromised: Promise<ModuleInterface>
suspend fun <T> Promise<T>.await(): T = suspendCoroutine { cont ->
then({ cont.resume(it) }, { cont.resumeWithException(it) })
}
private fun async(block: suspend () -> Unit): dynamic = GlobalScope.promise {
block()
}
private fun withModule(block: ModuleInterface.() -> Unit) = async {
val module = ModulePromised.await()
block.invoke(module)
}
class WasmTests {
@Test
fun pingTest() = withModule {
assertTrue(this.ping())
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment