Skip to content

Commit

Permalink
feat(generator): create unwrapped methods for methods with dst arg (#263
Browse files Browse the repository at this point in the history
)

Also, minor fixes for binding declarations.
  • Loading branch information
SpaiR authored Aug 21, 2024
1 parent f18c578 commit 2086b79
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 173 deletions.
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/tool/generator/api/jni_content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private fun convertParams2jni(f: Factory, params: List<CtParameter<*>>, defaults
} else if (p.type.isClass) {
when (p.type.simpleName) {
"ImBoolean" -> f.createTypeParam("boolean[]")
"ImShort" -> f.createTypeParam("short[]")
"ImInt" -> f.createTypeParam("int[]")
"ImFloat" -> f.createTypeParam("float[]")
"ImLong" -> f.createTypeParam("long[]")
Expand All @@ -139,7 +140,7 @@ private fun joinInBodyParams(params: List<CtParameter<*>>, defaults: IntArray):
"&${p.simpleName}[0]"
} else if (p.type.isClass) {
when (p.type.simpleName) {
"ImBoolean", "ImInt", "ImFloat", "ImLong", "ImDouble" -> {
"ImBoolean", "ImShort", "ImInt", "ImFloat", "ImLong", "ImDouble" -> {
"(${p.simpleName} != NULL ? &${p.simpleName}[0] : NULL)"
}

Expand Down
94 changes: 48 additions & 46 deletions buildSrc/src/main/kotlin/tool/generator/api/jvm_content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ private fun createMethod(origM: CtMethod<*>, params: List<CtParameter<*>>, defau
return newM
}

private fun methodVecUnwrappedContent(method: CtMethod<*>): String {
private fun methodVecUnwrappedContent(method: CtMethod<*>, fromIndex: Int): String {
val newMethod = method.clone()
val newParams = mutableListOf<CtParameter<*>>()
val vecParamNames = mutableSetOf<String>()

for (p in newMethod.parameters) {
if (p.isType("ImVec2") || p.isType("ImVec4")) {
for ((idx, p) in newMethod.parameters.withIndex()) {
if ((p.isType("ImVec2") || p.isType("ImVec4")) && idx >= fromIndex) {
vecParamNames += p.simpleName

val paramX = p.factory.createParameter<Nothing>()
Expand All @@ -194,9 +194,9 @@ private fun methodVecUnwrappedContent(method: CtMethod<*>): String {
}

getJDoc(newMethod)?.let { jDoc ->
val idx = jDoc.tags.indexOfFirst { it.param == p.simpleName }
if (idx != -1) {
jDoc.removeTag<Nothing>(idx)
val tagIdx = jDoc.tags.indexOfFirst { it.param == p.simpleName }
if (tagIdx != -1) {
jDoc.removeTag<Nothing>(tagIdx)
}
}
} else {
Expand All @@ -223,13 +223,13 @@ private fun methodVecUnwrappedContent(method: CtMethod<*>): String {
return ""
}

private fun methodRectUnwrappedContent(method: CtMethod<*>): String {
private fun methodRectUnwrappedContent(method: CtMethod<*>, fromIndex: Int): String {
val newMethod = method.clone()
val newParams = mutableListOf<CtParameter<*>>()
val paramNames = mutableSetOf<String>()

for (p in newMethod.parameters) {
if (p.isType("ImRect")) {
for ((idx, p) in newMethod.parameters.withIndex()) {
if (p.isType("ImRect") && idx >= fromIndex) {
paramNames += p.simpleName

val paramMinX = p.factory.createParameter<Nothing>()
Expand Down Expand Up @@ -276,13 +276,13 @@ private fun methodRectUnwrappedContent(method: CtMethod<*>): String {
return ""
}

private fun methodPlotPointUnwrappedContent(method: CtMethod<*>): String {
private fun methodPlotPointUnwrappedContent(method: CtMethod<*>, fromIndex: Int): String {
val newMethod = method.clone()
val newParams = mutableListOf<CtParameter<*>>()
val paramNames = mutableSetOf<String>()

for (p in newMethod.parameters) {
if (p.isType("ImPlotPoint")) {
for ((idx, p) in newMethod.parameters.withIndex()) {
if (p.isType("ImPlotPoint") && idx >= fromIndex) {
paramNames += p.simpleName

val paramX = p.factory.createParameter<Nothing>()
Expand Down Expand Up @@ -318,13 +318,13 @@ private fun methodPlotPointUnwrappedContent(method: CtMethod<*>): String {
return ""
}

private fun methodPlotRangeUnwrappedContent(method: CtMethod<*>): String {
private fun methodPlotRangeUnwrappedContent(method: CtMethod<*>, fromIndex: Int): String {
val newMethod = method.clone()
val newParams = mutableListOf<CtParameter<*>>()
val paramNames = mutableSetOf<String>()

for (p in newMethod.parameters) {
if (p.isType("ImPlotRange")) {
for ((idx, p) in newMethod.parameters.withIndex()) {
if (p.isType("ImPlotRange") && idx >= fromIndex) {
paramNames += p.simpleName

val paramX = p.factory.createParameter<Nothing>()
Expand Down Expand Up @@ -360,13 +360,13 @@ private fun methodPlotRangeUnwrappedContent(method: CtMethod<*>): String {
return ""
}

private fun methodPlotLimitsUnwrappedContent(method: CtMethod<*>): String {
private fun methodPlotLimitsUnwrappedContent(method: CtMethod<*>, fromIndex: Int): String {
val newMethod = method.clone()
val newParams = mutableListOf<CtParameter<*>>()
val paramNames = mutableSetOf<String>()

for (p in newMethod.parameters) {
if (p.isType("ImPlotRect")) {
for ((idx, p) in newMethod.parameters.withIndex()) {
if (p.isType("ImPlotRect") && idx >= fromIndex) {
paramNames += p.simpleName

val paramMinX = p.factory.createParameter<Nothing>()
Expand Down Expand Up @@ -413,13 +413,13 @@ private fun methodPlotLimitsUnwrappedContent(method: CtMethod<*>): String {
return ""
}

private fun methodCoordinatesUnwrappedContent(method: CtMethod<*>): String {
private fun methodCoordinatesUnwrappedContent(method: CtMethod<*>, fromIndex: Int): String {
val newMethod = method.clone()
val newParams = mutableListOf<CtParameter<*>>()
val paramNames = mutableSetOf<String>()

for (p in newMethod.parameters) {
if (p.isType("TextEditorCoordinates")) {
for ((idx, p) in newMethod.parameters.withIndex()) {
if (p.isType("TextEditorCoordinates") && idx >= fromIndex) {
paramNames += p.simpleName

val paramX = p.factory.createParameter<Nothing>()
Expand Down Expand Up @@ -506,34 +506,34 @@ private fun transformMethodToContent(
params: List<CtParameter<*>> = emptyList(),
defaults: IntArray = intArrayOf()
): List<String> {
fun methodUnwrapped(method: CtMethod<*>, fromIndex: Int = 0): Set<String> {
val result = mutableSetOf<String>()
if (params.find { it.isType("ImVec2") || it.isType("ImVec4") } != null) {
methodVecUnwrappedContent(method, fromIndex).takeIf(String::isNotEmpty)?.run(result::add)
}
if (params.find { it.isType("ImRect") } != null) {
methodRectUnwrappedContent(method, fromIndex).takeIf(String::isNotEmpty)?.run(result::add)
}
if (params.find { it.isType("ImPlotPoint") } != null) {
methodPlotPointUnwrappedContent(method, fromIndex).takeIf(String::isNotEmpty)?.run(result::add)
}
if (params.find { it.isType("ImPlotRange") } != null) {
methodPlotRangeUnwrappedContent(method, fromIndex).takeIf(String::isNotEmpty)?.run(result::add)
}
if (params.find { it.isType("ImPlotRect") } != null) {
methodPlotLimitsUnwrappedContent(method, fromIndex).takeIf(String::isNotEmpty)?.run(result::add)
}
if (params.find { it.isType("TextEditorCoordinates") } != null) {
methodCoordinatesUnwrappedContent(method, fromIndex).takeIf(String::isNotEmpty)?.run(result::add)
}
return result
}

val methods = mutableListOf<String>()
val mNew = createMethod(mOrig, params, defaults)

methods += mNew.prettyprint()

if (params.find { it.isType("ImVec2") || it.isType("ImVec4") } != null) {
methodVecUnwrappedContent(mNew).takeIf(String::isNotEmpty)?.run(methods::add)
}

if (params.find { it.isType("ImRect") } != null) {
methodRectUnwrappedContent(mNew).takeIf(String::isNotEmpty)?.run(methods::add)
}

if (params.find { it.isType("ImPlotPoint") } != null) {
methodPlotPointUnwrappedContent(mNew).takeIf(String::isNotEmpty)?.run(methods::add)
}

if (params.find { it.isType("ImPlotRange") } != null) {
methodPlotRangeUnwrappedContent(mNew).takeIf(String::isNotEmpty)?.run(methods::add)
}

if (params.find { it.isType("ImPlotRect") } != null) {
methodPlotLimitsUnwrappedContent(mNew).takeIf(String::isNotEmpty)?.run(methods::add)
}

if (params.find { it.isType("TextEditorCoordinates") } != null) {
methodCoordinatesUnwrappedContent(mNew).takeIf(String::isNotEmpty)?.run(methods::add)
}
methods += methodUnwrapped(mNew)

if (mOrig.isType("ImVec2") || mOrig.isType("ImVec4")) {
methods += createMethodVecValueReturn("x", mNew, params, defaults).prettyprint()
Expand All @@ -545,7 +545,9 @@ private fun transformMethodToContent(
}

if (DST_RETURN_TYPE_SET.contains(mOrig.type.simpleName)) {
methods += createMethodDstReturn(mNew, params, defaults).prettyprint()
val dstMethod = createMethodDstReturn(mNew, params, defaults)
methods += dstMethod.prettyprint()
methods += methodUnwrapped(dstMethod, 1)
}

return methods
Expand Down
Loading

0 comments on commit 2086b79

Please sign in to comment.