[Wasm] Add interop annotation @JsFun

This commit is contained in:
Svyatoslav Kuzmich
2020-10-20 11:36:43 +03:00
parent e51a76bc4e
commit 4d59a58291
11 changed files with 59 additions and 18 deletions
@@ -99,14 +99,6 @@ fun compileWasm(
fun WasmCompiledModuleFragment.generateJs(): String { fun WasmCompiledModuleFragment.generateJs(): String {
val runtime = """ val runtime = """
const runtime = { const runtime = {
String_plus(str1, str2) {
return str1 + String(str2);
},
String_getLength(str) {
return str.length;
},
String_getChar(str, index) { String_getChar(str, index) {
return str.charCodeAt(index); return str.charCodeAt(index);
}, },
@@ -163,5 +155,8 @@ fun WasmCompiledModuleFragment.generateJs(): String {
}; };
""".trimIndent() """.trimIndent()
return runtime + generateStringLiteralsSupport(stringLiterals) val jsCode =
"\nconst js_code = {${jsFuns.joinToString(",\n") { "\"" + it.importName + "\" : " + it.jsCode }}};"
return runtime + generateStringLiteralsSupport(stringLiterals) + jsCode
} }
@@ -208,7 +208,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
// Return types of imported functions cannot have concrete struct/array references. // Return types of imported functions cannot have concrete struct/array references.
// Non-primitive return types are represented as eqref which need to be casted back to expected type on call site. // Non-primitive return types are represented as eqref which need to be casted back to expected type on call site.
if (function.getWasmImportAnnotation() != null) { if (function.getWasmImportAnnotation() != null || function.getJsFunAnnotation() != null) {
val resT = context.transformResultType(function.returnType) val resT = context.transformResultType(function.returnType)
if (resT is WasmRefNullType) { if (resT is WasmRefNullType) {
generateTypeRTT(function.returnType) generateTypeRTT(function.returnType)
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.backend.wasm.ir2wasm
import org.jetbrains.kotlin.backend.common.ir.isOverridableOrOverrides import org.jetbrains.kotlin.backend.common.ir.isOverridableOrOverrides
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.lower.wasmSignature
import org.jetbrains.kotlin.backend.wasm.utils.* import org.jetbrains.kotlin.backend.wasm.utils.*
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
@@ -35,12 +36,23 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
// Type aliases are not material // Type aliases are not material
} }
private fun jsCodeName(declaration: IrFunction): String {
return declaration.fqNameWhenAvailable!!.asString() + "_" + (declaration as IrSimpleFunction).wasmSignature(irBuiltIns).hashCode()
}
override fun visitFunction(declaration: IrFunction) { override fun visitFunction(declaration: IrFunction) {
// Inline class constructors are currently empty // Inline class constructors are currently empty
if (declaration is IrConstructor && backendContext.inlineClassesUtils.isClassInlineLike(declaration.parentAsClass)) if (declaration is IrConstructor && backendContext.inlineClassesUtils.isClassInlineLike(declaration.parentAsClass))
return return
val importedName = declaration.getWasmImportAnnotation() val jsCode = declaration.getJsFunAnnotation()
val importedName = if (jsCode != null) {
val jsCodeName = jsCodeName(declaration)
context.addJsFun(jsCodeName, jsCode)
WasmImportPair("js_code", jsCodeName(declaration))
} else {
declaration.getWasmImportAnnotation()
}
val isIntrinsic = declaration.hasWasmReinterpretAnnotation() || declaration.getWasmOpAnnotation() != null val isIntrinsic = declaration.hasWasmReinterpretAnnotation() || declaration.getWasmOpAnnotation() != null
if (isIntrinsic) { if (isIntrinsic) {
@@ -46,7 +46,10 @@ class WasmCompiledModuleFragment {
ReferencableAndDefinable<IrClassSymbol, ConstantDataElement>() ReferencableAndDefinable<IrClassSymbol, ConstantDataElement>()
val exports = mutableListOf<WasmExport<*>>() val exports = mutableListOf<WasmExport<*>>()
// class JsCodeSnippet(val importName: String, val jsCode: String)
val jsFuns = mutableListOf<JsCodeSnippet>()
var startFunction: WasmFunction? = null var startFunction: WasmFunction? = null
open class ReferencableElements<Ir, Wasm : Any> { open class ReferencableElements<Ir, Wasm : Any> {
@@ -22,6 +22,7 @@ interface WasmModuleCodegenContext : WasmBaseCodegenContext {
fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration) fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration)
fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal) fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal)
fun defineFunctionType(irFunction: IrFunctionSymbol, wasmFunctionType: WasmFunctionType) fun defineFunctionType(irFunction: IrFunctionSymbol, wasmFunctionType: WasmFunctionType)
fun addJsFun(importName: String, jsCode: String)
fun setStartFunction(wasmFunction: WasmFunction) fun setStartFunction(wasmFunction: WasmFunction)
fun addExport(wasmExport: WasmExport<*>) fun addExport(wasmExport: WasmExport<*>)
@@ -166,4 +166,9 @@ class WasmModuleCodegenContextImpl(
val fieldId = metadata.fields.indexOf(field) val fieldId = metadata.fields.indexOf(field)
return WasmSymbol(fieldId) return WasmSymbol(fieldId)
} }
override fun addJsFun(importName: String, jsCode: String) {
wasmFragment.jsFuns +=
WasmCompiledModuleFragment.JsCodeSnippet(importName = importName, jsCode = jsCode)
}
} }
@@ -35,3 +35,6 @@ fun IrAnnotationContainer.getWasmImportAnnotation(): WasmImportPair? =
(it.getValueArgument(1) as IrConst<*>).value as String (it.getValueArgument(1) as IrConst<*>).value as String
) )
} }
fun IrAnnotationContainer.getJsFunAnnotation(): String? =
getAnnotation(FqName("kotlin.JsFun"))?.getSingleConstStringArgument()
@@ -115,7 +115,7 @@ abstract class BasicWasmBoxTest(
testFunction: String testFunction: String
) { ) {
val filesToCompile = units.map { (it as TranslationUnit.SourceFile).file } val filesToCompile = units.map { (it as TranslationUnit.SourceFile).file }
val debugMode = getBoolean("kotlin.js.debugMode") val debugMode = getBoolean("kotlin.wasm.debugMode")
val phaseConfig = if (debugMode) { val phaseConfig = if (debugMode) {
val allPhasesSet = wasmPhases.toPhaseMap().values.toSet() val allPhasesSet = wasmPhases.toPhaseMap().values.toSet()
@@ -154,7 +154,7 @@ abstract class BasicWasmBoxTest(
val testRunner = """ val testRunner = """
const wasmBinary = read(String.raw`${outputWasmFile.absoluteFile}`, 'binary'); const wasmBinary = read(String.raw`${outputWasmFile.absoluteFile}`, 'binary');
const wasmModule = new WebAssembly.Module(wasmBinary); const wasmModule = new WebAssembly.Module(wasmBinary);
const wasmInstance = new WebAssembly.Instance(wasmModule, { runtime }); const wasmInstance = new WebAssembly.Instance(wasmModule, { runtime, js_code });
const actualResult = wasmInstance.exports.$testFunction(); const actualResult = wasmInstance.exports.$testFunction();
if (actualResult !== "OK") if (actualResult !== "OK")
@@ -51,11 +51,11 @@ public class String constructor(public val string: String) : Comparable<String>,
public override fun hashCode(): Int = 10 public override fun hashCode(): Int = 10
} }
@WasmImport("runtime", "String_plus") @JsFun("(it, other) => it + String(other)")
private fun stringPlusImpl(it: String, other: String): String = private fun stringPlusImpl(it: String, other: String): String =
implementedAsIntrinsic implementedAsIntrinsic
@WasmImport("runtime", "String_getLength") @JsFun("(it) => it.length")
private fun stringLengthImpl(it: String): Int = private fun stringLengthImpl(it: String): Int =
implementedAsIntrinsic implementedAsIntrinsic
@@ -72,11 +72,11 @@ internal fun JsArray_set_WasmExternRef(array: WasmExternRef, index: Int, value:
internal fun JsArray_getSize(array: WasmExternRef): Int = internal fun JsArray_getSize(array: WasmExternRef): Int =
implementedAsIntrinsic implementedAsIntrinsic
@WasmImport("runtime", "identity") @JsFun("(x) => x")
internal fun Any?.toWasmExternRef(): WasmExternRef = internal fun Any?.toWasmExternRef(): WasmExternRef =
implementedAsIntrinsic implementedAsIntrinsic
@WasmImport("runtime", "identity") @JsFun("(x) => x")
internal fun WasmExternRefToAny(ref: WasmExternRef): Any? = internal fun WasmExternRefToAny(ref: WasmExternRef): Any? =
implementedAsIntrinsic implementedAsIntrinsic
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2020 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 kotlin
/**
* Implements annotated function in JavaScript and automatically imports is to Wasm.
* [code] string must contain JS expression that evaluates to JS function with signature that matches annotated kotlin function
*
* For example, a function that adds two Doubles via JS:
*
* @JsFun("(x, y) => x + y")
* fun jsAdd(x: Double, y: Double): Double =
* error("...")
*
* This is a temporary annotation because K/Wasm <-> JS interop is not designed yet.
*/
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.BINARY)
public annotation class JsFun(val code: String)