From 625bf54944bfb2160ab6feef1f2086b71ccf9ff4 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Fri, 17 Nov 2017 15:04:02 +0300 Subject: [PATCH] Moved wasm stub generator from explicit hasGetter/hasSetter to somewhat more natural readOnly attributes. --- .../src/stubGenerator/kotlin/idl.kt | 20 ++++++++--------- .../src/stubGenerator/kotlin/stubGenerator.kt | 22 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/samples/html5Canvas/src/stubGenerator/kotlin/idl.kt b/samples/html5Canvas/src/stubGenerator/kotlin/idl.kt index dfa9b17bc53..52fa6cfe7cb 100644 --- a/samples/html5Canvas/src/stubGenerator/kotlin/idl.kt +++ b/samples/html5Canvas/src/stubGenerator/kotlin/idl.kt @@ -5,9 +5,9 @@ import org.jetbrains.kotlin.konan.jsinterop.tool.Type.* // This shall be an output of Web IDL parser. val all = listOf( Interface("Context", - Attribute("lineWidth", Integer, hasSetter = true), - Attribute("fillStyle", idlString, hasSetter = true), - Attribute("strokeStyle", idlString, hasSetter = true), + Attribute("lineWidth", Integer), + Attribute("fillStyle", idlString), + Attribute("strokeStyle", idlString), Operation("lineTo", Void, Arg("x", Integer), Arg("y", Integer)), Operation("moveTo", Void, Arg("x", Integer), Arg("y", Integer)), @@ -19,10 +19,10 @@ val all = listOf( Operation("closePath", Void) ), Interface("DOMRect", - Attribute("left", Integer, hasGetter = true), - Attribute("right", Integer, hasGetter = true), - Attribute("top", Integer, hasGetter = true), - Attribute("bottom", Integer, hasGetter = true) + Attribute("left", Integer), + Attribute("right", Integer), + Attribute("top", Integer), + Attribute("bottom", Integer) ), Interface("Canvas", Operation("getContext", InterfaceRef("Context"), Arg("context", idlString)), @@ -32,8 +32,8 @@ val all = listOf( Operation("getElementById", Object, Arg("id", idlString)) ), Interface("MouseEvent", - Attribute("clientX", Integer, hasGetter = true), - Attribute("clientY", Integer, hasGetter = true) + Attribute("clientX", Integer, readOnly = true), + Attribute("clientY", Integer, readOnly = true) ), Interface("Response", Operation("json", Object) @@ -42,7 +42,7 @@ val all = listOf( Operation("then", InterfaceRef("Promise"), Arg("lambda", Function)) ), Interface("__Global", - Attribute("document", InterfaceRef("Document"), hasGetter = true), + Attribute("document", InterfaceRef("Document"), readOnly = true), Operation("fetch", InterfaceRef("Promise"), Arg("url", idlString)), Operation("setInterval", Void, Arg("lambda", Function), Arg("interval", Integer)) diff --git a/samples/html5Canvas/src/stubGenerator/kotlin/stubGenerator.kt b/samples/html5Canvas/src/stubGenerator/kotlin/stubGenerator.kt index 28605299f38..8aca2908981 100644 --- a/samples/html5Canvas/src/stubGenerator/kotlin/stubGenerator.kt +++ b/samples/html5Canvas/src/stubGenerator/kotlin/stubGenerator.kt @@ -15,7 +15,7 @@ object Object: Type object Function: Type data class Attribute(val name: String, val type: Type, - val hasGetter: Boolean = false, val hasSetter: Boolean = false): Member + val readOnly: Boolean = false): Member data class Arg(val name: String, val type: Type) @@ -76,9 +76,9 @@ fun Type.wasmReturnMapping(value: String): String { is Void -> "" is Integer -> value is Floating -> value - is idlString -> TODO("Implement me") + is idlString -> "TODO(\"Implement me\")" is Object -> "JsValue(ArenaManager.currentArena, $value)" - is Function -> TODO("Implement me") + is Function -> "TODO(\"Implement me\")" is InterfaceRef -> "$name(ArenaManager.currentArena, $value)" else -> error("Unexpected type") } @@ -132,7 +132,6 @@ fun Attribute.generateKotlinSetter(parent: Interface): String { } fun Attribute.generateKotlinGetter(parent: Interface): String { - //val kotlinType = type.toKotlinType() return " get() {\n" + " val wasmRetVal = ${wasmGetterName(name, parent.name)}(${(parent.wasmReceiverArgs + parent.wasmReturnArg()).joinToString(", ")})\n" + " return ${type.wasmReturnMapping("wasmRetVal")}\n"+ @@ -142,9 +141,10 @@ fun Attribute.generateKotlinGetter(parent: Interface): String { fun Attribute.generateKotlin(parent: Interface): String { val kotlinType = type.toKotlinType(name) - return " var $name: $kotlinType\n" + - (if (hasGetter) generateKotlinGetter(parent) else " get() = error(\"There is no getter for $name\")\n") + - (if (hasSetter) generateKotlinSetter(parent) else " set(_) = error(\"There is no setter for $name\")\n") + val varOrVal = if (readOnly) "val" else "var" + return " $varOrVal $name: $kotlinType\n" + + generateKotlinGetter(parent) + + if (!readOnly) generateKotlinSetter(parent) else "" } val Interface.wasmTypedReceiverArgs get() = @@ -171,8 +171,8 @@ fun Attribute.generateWasmGetterStub(parent: Interface): String { "external public fun $wasmGetter($allArgs): Int\n\n" } fun Attribute.generateWasmStubs(parent: Interface) = - (if (hasGetter) generateWasmGetterStub(parent) else "") + - (if (hasSetter) generateWasmSetterStub(parent) else "") + generateWasmGetterStub(parent) + + if (!readOnly) generateWasmSetterStub(parent) else "" // TODO: consider using virtual mathods fun Member.generateKotlin(parent: Interface): String { @@ -310,8 +310,8 @@ fun Attribute.generateJsGetter(parent: Interface): String { } fun Attribute.generateJs(parent: Interface) = - (if (hasGetter) generateJsGetter(parent) else "") + - (if (hasSetter) generateJsSetter(parent) else "") + generateJsGetter(parent) + + if (!readOnly) ",\n${generateJsSetter(parent)}" else "" fun Member.generateJs(parent: Interface): String { return when (this) {