Skip to content

Commit

Permalink
Add explicit type casting for JNI
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Jan 15, 2024
1 parent 38f22b8 commit 83bc8f5
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ private fun declParamToDsl(decl: AstFunctionDecl, param: AstParmVarDecl, dsl: Ar
defaultJniValue(param.defaultValue)
}

data.jniCast = "(${param.qualType})"

when (param.qualType) {
"ImTextureID" -> {
data.jniCast = "(ImTextureID)($CAST_PTR_JNI)"
data.jniCast += "($CAST_PTR_JNI)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import tool.generator.api.definition.node.type.method.ArgDefinitionNode
import tool.generator.api.definition.node.type.method.MethodDefinitionNode
import tool.generator.api.definition.node.type.method.ext.*
import tool.generator.api.definition.node.type.method.ext.arg.ArgTypeNull
import tool.generator.api.definition.node.type.method.ext.arg.ArgTypePrimitive
import tool.generator.api.definition.node.type.method.ext.arg.ArgTypeStruct
import tool.generator.api.definition.node.type.method.ext.arg.ArgTypeVec

Expand Down Expand Up @@ -37,7 +36,7 @@ object `set args call for auto body from jni to native` : TransformationChain.Tr
var argCall = if (arg.argType.hasFlag(ArgTypeFlag.POINTER) || arg.argType.hasFlag(ArgTypeFlag.ARRAY)) {
"&${arg.name}[0]"
} else if (argType is ArgTypeStruct) {
"(${argType.value.substringAfterLast('.')}*)${arg.name}"
arg.name
} else if (argType == ArgTypeVec.V2) {
"ImVec2(${arg.name}X, ${arg.name}Y)"
} else if (argType == ArgTypeVec.V4) {
Expand All @@ -52,8 +51,8 @@ object `set args call for auto body from jni to native` : TransformationChain.Tr
arg.name
}

getDefaultJniCast(arg)?.let {
argCall = "($it)$argCall"
if (argType is ArgTypeStruct && !arg.hasJniCast) {
arg.jniCast = "(${argType.value.substringAfterLast('.')}*)"
}

if (arg.hasJniCast) {
Expand All @@ -62,14 +61,4 @@ object `set args call for auto body from jni to native` : TransformationChain.Tr

return argCall
}

/**
* Default JNI cast needed to ensure native code works with types it expects.
*/
private fun getDefaultJniCast(arg: ArgDefinitionNode): String? {
if (arg.argType.type == ArgTypePrimitive.BOOL && !arg.argType.hasFlag(ArgTypeFlag.POINTER)) {
return "bool"
}
return null
}
}
Loading

0 comments on commit 83bc8f5

Please sign in to comment.