[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 {
val runtime = """
const runtime = {
String_plus(str1, str2) {
return str1 + String(str2);
},
String_getLength(str) {
return str.length;
},
String_getChar(str, index) {
return str.charCodeAt(index);
},
@@ -163,5 +155,8 @@ fun WasmCompiledModuleFragment.generateJs(): String {
};
""".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.
// 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)
if (resT is WasmRefNullType) {
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.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.lower.wasmSignature
import org.jetbrains.kotlin.backend.wasm.utils.*
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
@@ -35,12 +36,23 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
// 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) {
// Inline class constructors are currently empty
if (declaration is IrConstructor && backendContext.inlineClassesUtils.isClassInlineLike(declaration.parentAsClass))
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
if (isIntrinsic) {
@@ -46,7 +46,10 @@ class WasmCompiledModuleFragment {
ReferencableAndDefinable<IrClassSymbol, ConstantDataElement>()
val exports = mutableListOf<WasmExport<*>>()
//
class JsCodeSnippet(val importName: String, val jsCode: String)
val jsFuns = mutableListOf<JsCodeSnippet>()
var startFunction: WasmFunction? = null
open class ReferencableElements<Ir, Wasm : Any> {
@@ -22,6 +22,7 @@ interface WasmModuleCodegenContext : WasmBaseCodegenContext {
fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration)
fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal)
fun defineFunctionType(irFunction: IrFunctionSymbol, wasmFunctionType: WasmFunctionType)
fun addJsFun(importName: String, jsCode: String)
fun setStartFunction(wasmFunction: WasmFunction)
fun addExport(wasmExport: WasmExport<*>)
@@ -166,4 +166,9 @@ class WasmModuleCodegenContextImpl(
val fieldId = metadata.fields.indexOf(field)
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
)
}
fun IrAnnotationContainer.getJsFunAnnotation(): String? =
getAnnotation(FqName("kotlin.JsFun"))?.getSingleConstStringArgument()