Skip to content

Commit

Permalink
Allow passing Strings without allocating manually (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
lopcode authored Aug 19, 2024
1 parent 55b6f41 commit a1ccc9d
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 305 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ includes_filtered.txt
hs_err*

sample_run
.kotlin
27 changes: 22 additions & 5 deletions helper-generator/src/main/kotlin/vipsffm/GenerateHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object GenerateHelpers {
private val vipsOptionType = ClassName.get("app.photofox.vipsffm.helper", "VipsOption")
private val vipsRawType = ClassName.get("app.photofox.vipsffm.generated", "VipsRaw")
private val memorySegmentType = ClassName.get("java.lang.foreign", "MemorySegment")
private val stringType = ClassName.get("java.lang", "String")
private val vipsOptionArrayType = ArrayTypeName.of(vipsOptionType)

@JvmStatic
Expand Down Expand Up @@ -124,8 +125,13 @@ object GenerateHelpers {
var returnPoetType = classDescToPoetType(qualifiedReturnType, typeDisplayName)

val args = applyMethod.methodTypeSymbol().parameterArray().mapIndexedNotNull { index, parameter ->
if (externMetadata.arguments[index].type == "...") {
val externArgMetadata = externMetadata.arguments[index]
if (externArgMetadata.type == "...") {
ParameterSpec.builder(vipsOptionArrayType, "options").build()
} else if (parameter.displayName() == "MemorySegment" &&
externArgMetadata.type == "const char" &&
externArgMetadata.pointerDepth == 1) {
ParameterSpec.builder(stringType, "${externArgMetadata.name}String").build()
} else {
val typeName = classDescToPoetType(parameter, parameter.displayName())
ParameterSpec.builder(typeName, externMetadata.arguments[index].name).build()
Expand All @@ -134,7 +140,13 @@ object GenerateHelpers {

val invokerArgs = args.dropLast(1).toMutableList()
invokerArgs += ParameterSpec.builder(vipsOptionArrayType, "invokerArgs").build()
val invokerArgsJoined = invokerArgs.joinToString { it.name }
val invokerArgsJoined = invokerArgs.joinToString {
if (it.type == stringType) {
it.name.removeSuffix("String")
} else {
it.name
}
}

val methodBuilder = MethodSpec.methodBuilder(newName)
.addJavadoc(
Expand All @@ -154,9 +166,14 @@ object GenerateHelpers {
args.forEachIndexed { index, parameter ->
val externArgMetadata = externMetadata.arguments[index]
if (externArgMetadata.pointerDepth == 1) {
methodBuilder.addCode(
makeInputValidatorCodeBlock(externArgMetadata, vipsValidatorType, rawMethodName)
)
if (parameter.type == stringType) {
val newName = parameter.name.removeSuffix("String")
methodBuilder.addStatement("var $newName = arena.allocateFrom(${parameter.name})")
} else {
methodBuilder.addCode(
makeInputValidatorCodeBlock(externArgMetadata, vipsValidatorType, rawMethodName)
)
}
}
}

Expand Down
Loading

0 comments on commit a1ccc9d

Please sign in to comment.