[WASM] Remove WasmImport annotation

This commit is contained in:
Igor Yakovlev
2022-02-03 16:23:38 +01:00
parent d30a4fa4d5
commit eb8c3aa54e
7 changed files with 24 additions and 60 deletions
@@ -110,33 +110,10 @@ fun WasmCompiledModuleFragment.generateJs(): String {
var wasmInstance = null;
const externrefBoxes = new WeakMap();
const runtime = {
identity(x) {
return x;
},
println(valueAddr) {
console.log(">>> " + importStringFromWasm(valueAddr));
},
printError(valueAddr) {
console.error(">>> " + importStringFromWasm(valueAddr));
},
};
function importStringFromWasm(addr) {
const mem16 = new Uint16Array(wasmInstance.exports.memory.buffer);
const mem32 = new Int32Array(wasmInstance.exports.memory.buffer);
const len = mem32[addr / 4];
const str_start_addr = (addr + 4) / 2;
const slice = mem16.slice(str_start_addr, str_start_addr + len);
return String.fromCharCode.apply(null, slice);
}
""".trimIndent()
val jsCode =
"\nconst js_code = {${jsFuns.joinToString(",\n") { "\"" + it.importName + "\" : " + it.jsCode }}};"
val jsFuns = jsFuns.joinToString(",\n") { "\"" + it.importName + "\" : " + it.jsCode }
val jsCode = "\nconst js_code = {$jsFuns};"
return runtime + jsCode
}
@@ -65,15 +65,12 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext, private val al
}
val jsCode = declaration.getJsFunAnnotation() ?: if (declaration.isExternal) declaration.name.asString() else null
val importedName = if (jsCode != null) {
val importedName = jsCode?.let {
val jsCodeName = jsCodeName(declaration)
context.addJsFun(jsCodeName, jsCode)
context.addJsFun(jsCodeName, it)
WasmImportPair("js_code", jsCodeName(declaration))
} else {
declaration.getWasmImportAnnotation()
}
if (declaration.isFakeOverride)
return
@@ -29,15 +29,6 @@ fun IrAnnotationContainer.hasWasmNoOpCastAnnotation(): Boolean =
fun IrAnnotationContainer.hasWasmAutoboxedAnnotation(): Boolean =
hasAnnotation(FqName("kotlin.wasm.internal.WasmAutoboxed"))
fun IrAnnotationContainer.getWasmImportAnnotation(): WasmImportPair? =
getAnnotation(FqName("kotlin.wasm.internal.WasmImport"))?.let {
WasmImportPair(
(it.getValueArgument(0) as IrConst<*>).value as String,
(it.getValueArgument(1) as IrConst<*>).value as String
)
}
class WasmArrayInfo(val klass: IrClass, val isNullable: Boolean) {
val type = klass.defaultType.let { if (isNullable) it.makeNullable() else it }
}
@@ -218,7 +218,7 @@ abstract class BasicWasmBoxTest(
val testRunner = """
const wasmBinary = read(String.raw`${outputWasmFile.absoluteFile}`, 'binary');
const wasmModule = new WebAssembly.Module(wasmBinary);
wasmInstance = new WebAssembly.Instance(wasmModule, { runtime, js_code });
wasmInstance = new WebAssembly.Instance(wasmModule, { js_code });
const ${sanitizeName(config.moduleId)} = wasmInstance.exports;
${createJsRun(wasmInstance = "wasmInstance", testFunction = testFunction, dceEnabled = dceEnabled)}
""".trimIndent()
@@ -261,7 +261,7 @@ abstract class BasicWasmBoxTest(
"""
const response = await fetch("index.wasm");
const wasmBinary = await response.arrayBuffer();
wasmInstance = (await WebAssembly.instantiate(wasmBinary, { runtime, js_code })).instance;
wasmInstance = (await WebAssembly.instantiate(wasmBinary, { js_code })).instance;
${createJsRun(wasmInstance = "wasmInstance", testFunction = "box", dceEnabled = dceEnabled)}
console.log("Test passed!");
""".trimIndent()
@@ -138,6 +138,15 @@ internal fun anyToExternRef(x: Any): ExternalInterfaceType {
x.asWasmExternRef()
}
@JsFun("""(addr) => {
const mem16 = new Uint16Array(wasmInstance.exports.memory.buffer);
const mem32 = new Int32Array(wasmInstance.exports.memory.buffer);
const len = mem32[addr / 4];
const str_start_addr = (addr + 4) / 2;
const slice = mem16.slice(str_start_addr, str_start_addr + len);
return String.fromCharCode.apply(null, slice);
}
""")
internal external fun importStringFromWasm(addr: Int): ExternalInterfaceType
@JsFun("x => x.length")
@@ -13,10 +13,6 @@ import kotlin.reflect.KClass
@Retention(AnnotationRetention.BINARY)
internal annotation class ExcludedFromCodegen
@Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER)
@Retention(AnnotationRetention.BINARY)
internal annotation class WasmImport(val module: String, val name: String)
@Target(CLASS)
@Retention(AnnotationRetention.BINARY)
internal annotation class WasmArrayOf(
+9 -15
View File
@@ -7,14 +7,20 @@ package kotlin.io
import kotlin.wasm.internal.*
@JsFun("(error) => console.error(\">>> \" + error)")
internal external fun printError(error: String?): Unit
@JsFun("(message) => console.log(\">>> \" + message)")
private external fun printlnImpl(error: String?): Unit
/** Prints the line separator to the standard output stream. */
public actual fun println() {
println("")
printlnImpl("")
}
/** Prints the given [message] and the line separator to the standard output stream. */
public actual fun println(message: Any?) {
printlnImpl(exportString(message.toString()))
printlnImpl(message?.toString())
}
/** Prints the given [message] to the standard output stream. */
@@ -29,16 +35,4 @@ public actual fun readln(): String = throw UnsupportedOperationException("readln
@SinceKotlin("1.6")
public actual fun readlnOrNull(): String? = throw UnsupportedOperationException("readlnOrNull is not supported in Kotlin/WASM")
internal actual interface Serializable
@WasmImport("runtime", "println")
private fun printlnImpl(messageAddr: Int): Unit =
implementedAsIntrinsic
internal fun printError(error: Any?) {
printErrorImpl(exportString(error.toString()))
}
@WasmImport("runtime", "printError")
private fun printErrorImpl(errorAddr: Int): Unit =
implementedAsIntrinsic
internal actual interface Serializable