Skip to content

Rework Java API for Signal/Callable #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: rework/signal-callable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class GenerationContext(
return nextSingletonIndex++
}

fun isNativeStructure(name: String) = name in nativeStructureMap

fun generateEnumDefaultValue(type: GenerationType, value: Long): String {
val simpleNames = type.className.simpleNames
val className: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package godot.codegen.generation.rule

import com.squareup.kotlinpoet.ANY
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.TypeVariableName
import godot.codegen.generation.GenerationContext
import godot.codegen.generation.task.EnrichedClassTask
import godot.codegen.generation.task.EnrichedConstantTask
Expand All @@ -16,6 +20,7 @@ import godot.tools.common.constants.GODOT_BASE_TYPE
import godot.tools.common.constants.KT_OBJECT
import godot.tools.common.constants.TYPE_MANAGER
import godot.tools.common.constants.VOID_PTR
import godot.tools.common.constants.godotCorePackage


class MemberRule : GodotApiRule<EnrichedClassTask>() {
Expand All @@ -31,17 +36,8 @@ class MemberRule : GodotApiRule<EnrichedClassTask>() {
}

for (method in clazz.methods) {
if (context.isNativeStructure(method.type.identifier)) {
continue
}
var shouldGenerate = true
for (argument in method.arguments) {
if (context.isNativeStructure(argument.type.identifier)) {
shouldGenerate = false
break
}
}
if (!shouldGenerate) {

if (!method.canGenerate) {
continue
}

Expand Down Expand Up @@ -121,9 +117,10 @@ class BindingRule : GodotApiRule<EnrichedClassTask>() {
clazz.methods
.filter { !it.isVirtual }
.onEach {
if(!it.canGenerate) return@onEach
classTask.bindings.addProperty(
PropertySpec
.builder(it.voidPtrVariableName, VOID_PTR)
.builder("${it.name}Ptr", VOID_PTR)
.addModifiers(KModifier.INTERNAL)
.initializer(
"%T.getMethodBindPtr(%S,·%S,·%L)",
Expand All @@ -137,3 +134,35 @@ class BindingRule : GodotApiRule<EnrichedClassTask>() {
}
}
}


class MethodNameRule : GodotApiRule<EnrichedClassTask>() {
override fun apply(classTask: EnrichedClassTask, context: GenerationContext) = configure(classTask.builder) {
val clazz = classTask.clazz
clazz.methods
.filter { !it.isVirtual }
.onEach {
if (it.isVararg) return@onEach
if(!it.canGenerate) return@onEach

val argCount = it.arguments.size
val methodStringClassName = ClassName(
godotCorePackage,
"MethodStringName$argCount"
).parameterizedBy(
listOf(classTask.clazz.className, it.getCastedType()) + it.arguments.map { it.getCastedType()}
)

classTask.companion.addProperty(
PropertySpec
.builder("${it.name}Name", methodStringClassName)
.initializer(
"%T(\"${it.godotName}\")",
methodStringClassName
)
.addAnnotation(JvmStatic::class)
.build()
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class MethodRule : GodotApiRule<EnrichedMethodTask>(), BaseMethodeRule {
"%T.callMethod($ptr, %T.%M, %T)",
TRANSFER_CONTEXT,
clazz.className.nestedClass(methodBindingsInnerClassName),
MemberName(godotApiPackage, method.voidPtrVariableName),
MemberName(godotApiPackage, "${method.name}Ptr"),
method.type.getVariantConverter()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class RegistrationRule() : GodotApiRule<ApiTask>() {

fun RegistrationTask.addVariantMapping(enrichedClass: EnrichedClass) {
variantMapper.addStatement(
"%M[%T::class] = %T",
MemberName(godotCorePackage, "variantMapper"),
"%M(%T::class, %T)",
MemberName(godotCorePackage, "addVariantMapping"),
enrichedClass.className,
enrichedClass.getVariantConverter()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EnrichedClassTask(

override val builder = if (clazz.isSingleton) TypeSpec.objectBuilder(clazz.className) else TypeSpec.classBuilder(clazz.className)
override val companion = if (clazz.isSingleton) builder else TypeSpec.companionObjectBuilder()

val bindings = TypeSpec.objectBuilder(clazz.className.nestedClass(methodBindingsInnerClassName))

val enums by back<EnrichedEnumTask>(innerClasses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import godot.codegen.models.Method
import godot.codegen.models.traits.CallableGeneratorTrait
import godot.codegen.models.traits.DocumentedGenerationTrait
import godot.codegen.models.traits.GenerationType
import godot.codegen.models.traits.Nature
import godot.codegen.workarounds.sanitizeApiType
import godot.common.constants.Constraints
import godot.common.extensions.convertToCamelCase
Expand All @@ -23,7 +24,6 @@ class EnrichedMethod(model: Method) : CallableGeneratorTrait, DocumentedGenerati
it
}
}
override val voidPtrVariableName = "${name}Ptr"
override val arguments = model.arguments?.toEnriched() ?: listOf()
override val isVararg = model.isVararg
override var description = model.description
Expand All @@ -33,6 +33,18 @@ class EnrichedMethod(model: Method) : CallableGeneratorTrait, DocumentedGenerati
val isStatic = model.isStatic
val godotName = model.name

val canGenerate: Boolean = run{
if (type.isNativeType()) {
return@run false
}
for (argument in arguments) {
if (argument.type.isNativeType()) {
return@run false
}
}
return@run true
}

init {
if (arguments.size > Constraints.MAX_FUNCTION_ARG_COUNT) {
throw TooManyMethodArgument(model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package godot.codegen.models.traits
interface CallableGeneratorTrait : HasTypeGenerationTrait, MetaGenerationTrait {
val arguments: List<HasTypeGenerationTrait>
val isVararg: Boolean
val voidPtrVariableName: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ interface TypeGenerationTrait {
else -> VARIANT_PARSER_OBJECT
}


fun isVoid() = nature == Nature.VOID
fun isCoreType() = nature == Nature.CORE || nature == Nature.TYPED_ARRAY
fun isPrimitive() = nature == Nature.PRIMITIVE
Expand All @@ -90,6 +89,7 @@ interface TypeGenerationTrait {
fun isObjectSubClass() = nature == Nature.CLASS
fun isVariant() = isCoreType() && identifier == GodotTypes.variant
fun isCoreClass() = identifier == GodotTypes.godotObject || identifier == GodotTypes.refCounted
fun isNativeType() = nature == Nature.NATIVE
}

fun ClassName.Companion.from(type: TypeGenerationTrait) = when {
Expand Down Expand Up @@ -156,6 +156,7 @@ class GenerationType(rawIdentifier: String) : TypeGenerationTrait {
identifier.startsWith(enumPrefix) -> Nature.ENUM
identifier.startsWith(bitfieldPrefix) -> Nature.BITFIELD
identifier.startsWith(GodotTypes.typedArray) == true -> Nature.TYPED_ARRAY
identifier.contains("*") -> Nature.NATIVE
else -> Nature.CLASS
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package godot.codegen.models.traits

import godot.codegen.constants.GodotMeta
import godot.tools.common.constants.GODOT_CALLABLE
import godot.tools.common.constants.GODOT_VARIANT_CALLABLE
import godot.tools.common.constants.GodotKotlinJvmTypes
import godot.tools.common.constants.GodotTypes
import godot.tools.common.constants.godotCorePackage
Expand Down Expand Up @@ -40,7 +41,7 @@ interface WithDefaultValueTrait : MetaGenerationTrait {
.replace(",", ".0,")
.replace(")", ".0)") to arrayOf()

identifier == GodotTypes.callable -> "%T()" to arrayOf(GODOT_CALLABLE)
identifier == GodotTypes.callable -> "%T()" to arrayOf(GODOT_VARIANT_CALLABLE)

identifier == GodotTypes.rid ||
identifier == GodotTypes.dictionary ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.TypeVariableName
import org.gradle.declarative.dsl.schema.FqName.Empty.packageName

/**
* Provides utilities to handle generic classes and generate KotlinPoet builders for them.
Expand All @@ -18,7 +19,7 @@ import com.squareup.kotlinpoet.TypeVariableName
* @param numberOfGenericParameters The number of generic type parameters for the class.
*/
class GenericClassNameInfo(
val className: ClassName,
val className: ClassName = ClassName("dummy.package", "DummyClass"),
numberOfGenericParameters: Int
) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import godot.codegen.generation.rule.HeaderCommentRule
import godot.codegen.generation.rule.ImportRule
import godot.codegen.generation.rule.LocalCopyHelperRule
import godot.codegen.generation.rule.MemberRule
import godot.codegen.generation.rule.MethodNameRule
import godot.codegen.generation.rule.MethodRule
import godot.codegen.generation.rule.ObjectRule
import godot.codegen.generation.rule.OverLoadRule
Expand Down Expand Up @@ -65,6 +66,7 @@ class ApiGenerationService(
subRule(EnrichedClassTask::enrichedMethods, ::OverLoadRule)
subRule(EnrichedClassTask::enrichedStaticMethods, ::OverLoadRule)
rule(::BindingRule)
rule(::MethodNameRule)
rule(::LocalCopyHelperRule)
}
subRule(FileTask::enums, ::EnumRule)
Expand Down
Loading
Loading