[Wasm] Support main function

This commit is contained in:
Svyatoslav Kuzmich
2021-10-13 12:08:54 +03:00
parent af18b10da9
commit 0f66f85cf9
22 changed files with 259 additions and 122 deletions
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.js.test.ir.semantics.*
import org.jetbrains.kotlin.js.test.semantics.*
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractIrCodegenBoxWasmTest
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractIrCodegenWasmJsInteropWasmTest
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractKotlinTestBoxWasmTest
import org.jetbrains.kotlin.js.test.wasm.semantics.AbstractJsTranslatorWasmTest
import org.jetbrains.kotlin.test.TargetBackend
fun main(args: Array<String>) {
@@ -32,14 +32,15 @@ fun main(args: Array<String>) {
model("box/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS)
}
testClass<AbstractKotlinTestBoxWasmTest> {
model("box/kotlin.test/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.WASM)
}
testClass<AbstractIrBoxJsTest> {
model("box/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS_IR)
}
testClass<AbstractJsTranslatorWasmTest> {
model("box/main", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.WASM)
model("box/kotlin.test/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.WASM)
}
testClass<AbstractIrBoxJsES6Test> {
model("box/", pattern = "^([^_](.+))\\.kt$", targetBackend = TargetBackend.JS_IR_ES6)
}
@@ -140,7 +140,7 @@ abstract class BasicBoxTest(
val actualMainCallParameters = if (CALL_MAIN_PATTERN.matcher(fileContent).find())
MainCallParameters.mainWithArguments(listOf("testArg"))
MainCallParameters.mainWithArguments(listOf())
else
mainCallParameters
@@ -189,9 +189,10 @@ abstract class BasicWasmBoxTest(
const wasmBinary = read(String.raw`${outputWasmFile.absoluteFile}`, 'binary');
const wasmModule = new WebAssembly.Module(wasmBinary);
wasmInstance = new WebAssembly.Instance(wasmModule, { runtime, js_code });
const ${sanitizeName(config.moduleId)} = wasmInstance.exports;
wasmInstance.exports.__init();
const ${sanitizeName(config.moduleId)} = wasmInstance.exports;
wasmInstance.exports.startUnitTests?.();
const actualResult = importStringFromWasm(wasmInstance.exports.$testFunction());
@@ -212,7 +213,9 @@ abstract class BasicWasmBoxTest(
const response = await fetch("index.wasm");
const wasmBinary = await response.arrayBuffer();
wasmInstance = (await WebAssembly.instantiate(wasmBinary, { runtime, js_code })).instance;
wasmInstance.exports.__init();
wasmInstance.exports.startUnitTests?.();
const actualResult = importStringFromWasm(wasmInstance.exports.box());
if (actualResult !== "OK")
throw `Wrong box result '${'$'}{actualResult}'; Expected "OK"`;
@@ -17,7 +17,7 @@ abstract class AbstractIrCodegenWasmJsInteropWasmTest : BasicWasmBoxTest(
"codegen/wasmJsInteropJs"
)
abstract class AbstractKotlinTestBoxWasmTest : BasicWasmBoxTest(
"js/js.translator/testData/box/kotlin.test",
"codegen/wasmBoxKotlinTest"
)
abstract class AbstractJsTranslatorWasmTest : BasicWasmBoxTest(
TEST_DATA_DIR_PATH + "box/",
"js.translator/wasmBox"
)
@@ -0,0 +1,133 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.js.test.wasm.semantics;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@RunWith(JUnit3RunnerWithInners.class)
public class JsTranslatorWasmTestGenerated extends AbstractJsTranslatorWasmTest {
@TestMetadata("js/js.translator/testData/box/main")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Main extends AbstractJsTranslatorWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInMain() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/main"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@TestMetadata("differentMains.kt")
public void testDifferentMains() throws Exception {
runTest("js/js.translator/testData/box/main/differentMains.kt");
}
@TestMetadata("incremental.kt")
public void testIncremental() throws Exception {
runTest("js/js.translator/testData/box/main/incremental.kt");
}
@TestMetadata("noArgs.kt")
public void testNoArgs() throws Exception {
runTest("js/js.translator/testData/box/main/noArgs.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("js/js.translator/testData/box/main/simple.kt");
}
@TestMetadata("suspendMain.kt")
public void testSuspendMain() throws Exception {
runTest("js/js.translator/testData/box/main/suspendMain.kt");
}
@TestMetadata("suspendMainNoArgs.kt")
public void testSuspendMainNoArgs() throws Exception {
runTest("js/js.translator/testData/box/main/suspendMainNoArgs.kt");
}
@TestMetadata("suspendMainThrows.kt")
public void testSuspendMainThrows() throws Exception {
runTest("js/js.translator/testData/box/main/suspendMainThrows.kt");
}
@TestMetadata("twoMains.kt")
public void testTwoMains() throws Exception {
runTest("js/js.translator/testData/box/main/twoMains.kt");
}
}
@TestMetadata("js/js.translator/testData/box/kotlin.test")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kotlin_test extends AbstractJsTranslatorWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInKotlin_test() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/kotlin.test"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@TestMetadata("beforeAfter.kt")
public void testBeforeAfter() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/beforeAfter.kt");
}
@TestMetadata("ignore.kt")
public void testIgnore() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/ignore.kt");
}
@TestMetadata("illegalParameters.kt")
public void testIllegalParameters() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/illegalParameters.kt");
}
@TestMetadata("incremental.kt")
public void testIncremental() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/incremental.kt");
}
@TestMetadata("inherited.kt")
public void testInherited() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/inherited.kt");
}
@TestMetadata("mpp.kt")
public void testMpp() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/mpp.kt");
}
@TestMetadata("nested.kt")
public void testNested() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/nested.kt");
}
@TestMetadata("returnTestResult.kt")
public void testReturnTestResult() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/returnTestResult.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/simple.kt");
}
}
}
@@ -1,77 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.js.test.wasm.semantics;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("js/js.translator/testData/box/kotlin.test")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class KotlinTestBoxWasmTestGenerated extends AbstractKotlinTestBoxWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInKotlin_test() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/kotlin.test"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@TestMetadata("beforeAfter.kt")
public void testBeforeAfter() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/beforeAfter.kt");
}
@TestMetadata("ignore.kt")
public void testIgnore() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/ignore.kt");
}
@TestMetadata("illegalParameters.kt")
public void testIllegalParameters() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/illegalParameters.kt");
}
@TestMetadata("incremental.kt")
public void testIncremental() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/incremental.kt");
}
@TestMetadata("inherited.kt")
public void testInherited() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/inherited.kt");
}
@TestMetadata("mpp.kt")
public void testMpp() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/mpp.kt");
}
@TestMetadata("nested.kt")
public void testNested() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/nested.kt");
}
@TestMetadata("returnTestResult.kt")
public void testReturnTestResult() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/returnTestResult.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("js/js.translator/testData/box/kotlin.test/simple.kt");
}
}
+1 -2
View File
@@ -4,8 +4,7 @@
var ok: String = "fail"
fun main(args: Array<String>) {
assertEquals(1, args.size)
assertEquals("testArg", args[0])
if (0 != args.size) error("fail")
ok = "OK"
}
+2 -3
View File
@@ -8,8 +8,7 @@ var ok: String = "fail"
var callback: () -> Unit = {}
suspend fun main(args: Array<String>) {
assertEquals(1, args.size)
assertEquals("testArg", args[0])
if (0 != args.size) error("Fail")
suspendCoroutine<Unit> { cont ->
callback = {
@@ -21,7 +20,7 @@ suspend fun main(args: Array<String>) {
}
fun box(): String {
assertEquals("fail", ok)
if ("fail" != ok) return "Fail"
callback()
return ok
}
+1 -1
View File
@@ -18,7 +18,7 @@ suspend fun main() {
}
fun box(): String {
assertEquals("fail", ok)
if ("fail" != ok) error("Fail")
callback()
return ok
}
+1 -1
View File
@@ -22,7 +22,7 @@ fun box(): String {
try {
callback()
} catch (e: Exception) {
assertTrue(e === exception)
if (e !== exception) return "Fail"
}
return "OK"
+1 -2
View File
@@ -9,8 +9,7 @@ fun main() {
}
fun main(args: Array<String>) {
assertEquals(1, args.size)
assertEquals("testArg", args[0])
if (args.size != 0) error("Fail")
o = "O"
}