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.
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))
@@ -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) {