Moved wasm stub generator from explicit hasGetter/hasSetter

to somewhat more natural readOnly attributes.
This commit is contained in:
Alexander Gorshenev
2017-11-17 15:04:02 +03:00
committed by alexander-gorshenev
parent 779a926b94
commit 625bf54944
2 changed files with 21 additions and 21 deletions
@@ -5,9 +5,9 @@ import org.jetbrains.kotlin.konan.jsinterop.tool.Type.*
// This shall be an output of Web IDL parser. // This shall be an output of Web IDL parser.
val all = listOf( val all = listOf(
Interface("Context", Interface("Context",
Attribute("lineWidth", Integer, hasSetter = true), Attribute("lineWidth", Integer),
Attribute("fillStyle", idlString, hasSetter = true), Attribute("fillStyle", idlString),
Attribute("strokeStyle", idlString, hasSetter = true), Attribute("strokeStyle", idlString),
Operation("lineTo", Void, Arg("x", Integer), Arg("y", Integer)), Operation("lineTo", Void, Arg("x", Integer), Arg("y", Integer)),
Operation("moveTo", 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) Operation("closePath", Void)
), ),
Interface("DOMRect", Interface("DOMRect",
Attribute("left", Integer, hasGetter = true), Attribute("left", Integer),
Attribute("right", Integer, hasGetter = true), Attribute("right", Integer),
Attribute("top", Integer, hasGetter = true), Attribute("top", Integer),
Attribute("bottom", Integer, hasGetter = true) Attribute("bottom", Integer)
), ),
Interface("Canvas", Interface("Canvas",
Operation("getContext", InterfaceRef("Context"), Arg("context", idlString)), Operation("getContext", InterfaceRef("Context"), Arg("context", idlString)),
@@ -32,8 +32,8 @@ val all = listOf(
Operation("getElementById", Object, Arg("id", idlString)) Operation("getElementById", Object, Arg("id", idlString))
), ),
Interface("MouseEvent", Interface("MouseEvent",
Attribute("clientX", Integer, hasGetter = true), Attribute("clientX", Integer, readOnly = true),
Attribute("clientY", Integer, hasGetter = true) Attribute("clientY", Integer, readOnly = true)
), ),
Interface("Response", Interface("Response",
Operation("json", Object) Operation("json", Object)
@@ -42,7 +42,7 @@ val all = listOf(
Operation("then", InterfaceRef("Promise"), Arg("lambda", Function)) Operation("then", InterfaceRef("Promise"), Arg("lambda", Function))
), ),
Interface("__Global", Interface("__Global",
Attribute("document", InterfaceRef("Document"), hasGetter = true), Attribute("document", InterfaceRef("Document"), readOnly = true),
Operation("fetch", InterfaceRef("Promise"), Arg("url", idlString)), Operation("fetch", InterfaceRef("Promise"), Arg("url", idlString)),
Operation("setInterval", Void, Arg("lambda", Function), Arg("interval", Integer)) Operation("setInterval", Void, Arg("lambda", Function), Arg("interval", Integer))
@@ -15,7 +15,7 @@ object Object: Type
object Function: Type object Function: Type
data class Attribute(val name: String, val type: 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) data class Arg(val name: String, val type: Type)
@@ -76,9 +76,9 @@ fun Type.wasmReturnMapping(value: String): String {
is Void -> "" is Void -> ""
is Integer -> value is Integer -> value
is Floating -> value is Floating -> value
is idlString -> TODO("Implement me") is idlString -> "TODO(\"Implement me\")"
is Object -> "JsValue(ArenaManager.currentArena, $value)" is Object -> "JsValue(ArenaManager.currentArena, $value)"
is Function -> TODO("Implement me") is Function -> "TODO(\"Implement me\")"
is InterfaceRef -> "$name(ArenaManager.currentArena, $value)" is InterfaceRef -> "$name(ArenaManager.currentArena, $value)"
else -> error("Unexpected type") else -> error("Unexpected type")
} }
@@ -132,7 +132,6 @@ fun Attribute.generateKotlinSetter(parent: Interface): String {
} }
fun Attribute.generateKotlinGetter(parent: Interface): String { fun Attribute.generateKotlinGetter(parent: Interface): String {
//val kotlinType = type.toKotlinType()
return " get() {\n" + return " get() {\n" +
" val wasmRetVal = ${wasmGetterName(name, parent.name)}(${(parent.wasmReceiverArgs + parent.wasmReturnArg()).joinToString(", ")})\n" + " val wasmRetVal = ${wasmGetterName(name, parent.name)}(${(parent.wasmReceiverArgs + parent.wasmReturnArg()).joinToString(", ")})\n" +
" return ${type.wasmReturnMapping("wasmRetVal")}\n"+ " return ${type.wasmReturnMapping("wasmRetVal")}\n"+
@@ -142,9 +141,10 @@ fun Attribute.generateKotlinGetter(parent: Interface): String {
fun Attribute.generateKotlin(parent: Interface): String { fun Attribute.generateKotlin(parent: Interface): String {
val kotlinType = type.toKotlinType(name) val kotlinType = type.toKotlinType(name)
return " var $name: $kotlinType\n" + val varOrVal = if (readOnly) "val" else "var"
(if (hasGetter) generateKotlinGetter(parent) else " get() = error(\"There is no getter for $name\")\n") + return " $varOrVal $name: $kotlinType\n" +
(if (hasSetter) generateKotlinSetter(parent) else " set(_) = error(\"There is no setter for $name\")\n") generateKotlinGetter(parent) +
if (!readOnly) generateKotlinSetter(parent) else ""
} }
val Interface.wasmTypedReceiverArgs get() = val Interface.wasmTypedReceiverArgs get() =
@@ -171,8 +171,8 @@ fun Attribute.generateWasmGetterStub(parent: Interface): String {
"external public fun $wasmGetter($allArgs): Int\n\n" "external public fun $wasmGetter($allArgs): Int\n\n"
} }
fun Attribute.generateWasmStubs(parent: Interface) = fun Attribute.generateWasmStubs(parent: Interface) =
(if (hasGetter) generateWasmGetterStub(parent) else "") + generateWasmGetterStub(parent) +
(if (hasSetter) generateWasmSetterStub(parent) else "") if (!readOnly) generateWasmSetterStub(parent) else ""
// TODO: consider using virtual mathods // TODO: consider using virtual mathods
fun Member.generateKotlin(parent: Interface): String { fun Member.generateKotlin(parent: Interface): String {
@@ -310,8 +310,8 @@ fun Attribute.generateJsGetter(parent: Interface): String {
} }
fun Attribute.generateJs(parent: Interface) = fun Attribute.generateJs(parent: Interface) =
(if (hasGetter) generateJsGetter(parent) else "") + generateJsGetter(parent) +
(if (hasSetter) generateJsSetter(parent) else "") if (!readOnly) ",\n${generateJsSetter(parent)}" else ""
fun Member.generateJs(parent: Interface): String { fun Member.generateJs(parent: Interface): String {
return when (this) { return when (this) {