mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-17 01:26:45 +00:00
* [Kotlin] Introduction to Kotlin Multiplaform
The first implementation of the Kotlin code generation was made years
ago at the time Kotlin Multiplaform was not stable and Kotlin is mostly
used on JVM-based targets. For this reason the generated code uses java
based runtime.
That design decision comes with many drawbacks, leaving the code
generated more java-like and making it impossible to use more advanced
features of the Kotlin language.
In this change we are adding two parts: A pure, multi-plaform, Kotlin
runtime and a new code generator to accompany it.
* [Kotlin] Remove scalar sign cast from code generation
Now that we have a new runtime the accepts unsigned types, we don't
need to code generate casting back and from signed scalars. This
MR removes this from both code generations and adds the necessary
API to the runtime.
* [Kotlin] Use offset on public API to represent buffer position
Currently, kotlin was following Java's approach of representing objects,
vectors, tables as "Int" (the position of it in the buffer). This change
replaces naked Int with Offset<T>, offering a type-safe API. So,
instead of
fun Table.createTable(b: FlatBufferBuilder, subTable: Int)
We will have
fun Table.createTable(b: FlatBufferBuilder, subTable: Offset<SubTable>)
Making impossible to accidentally switch parameters.
The performance should be similar to use Int as we are using value
class for Offset and ArrayOffset, which most of the time translate to
Int in the bytecode.
* [Kotlin] Add builder for tables
Add builder constructor to make create of table more ergonomic.
For example the movie sample for the test set could be written as:
Movie.createMovie(fbb,
mainCharacterType = Character_.MuLan,
mainCharacter = att) {
charactersType = charsType
this.characters = characters
}
instead of:
Movie.startMovie(fbb)
Movie.addMainCharacterType(fbb, Character_.MuLan)
Movie.addMainCharacter(fbb, att as Offset<Any>)
Movie.addCharactersType(fbb, charsType)
Movie.addCharacters(fbb, charsVec)
Movie.endMovie(fbb)
* [Kotlin] Move enum types to value class
Moving to flatbuffer enums to value class adds type safety for parameters
with minimum to no performance impact.
* [Kotlin] Simplify Union parameters to avoid naked casting
Just a small change on the APIs that receive union as parameters,
creating a typealias UnionOffset to avoid using Offset<Any>. To "convert"
an table offset to an union, one just call Offset.toUnion().
* [Kotlin] Apply clang-format on kotlin code generators
* [Kotlin] Update kotlin generator to follow official naming conventions
Updating directory, package and enum naming to follow Kotlin official
convention.
https://kotlinlang.org/docs/coding-conventions.html#naming-rules
* [Kotlin] Add fixes to improve performance
1 - Add benchmark comparing serialization between Java & Kotlin
2 - ReadWriteBuffer does not auto-grow (thus avoid check size in every op)
3 - Add specialized add functions on FlatBufferBuilder to avoid boxing
offsets.
4 - Remove a few Kotlin syntax sugar that generated performance penalties.
* [Kotlin] Remove builder from Kotlin KMP and add some optimizations
to avoid boxing of Offset classes
---------
Co-authored-by: Derek Bailey <derekbailey@google.com>
143 lines
3.7 KiB
Kotlin
143 lines
3.7 KiB
Kotlin
import org.gradle.internal.impldep.org.fusesource.jansi.AnsiRenderer.test
|
|
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
|
|
import org.jetbrains.kotlin.cli.common.toBooleanLenient
|
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
|
|
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
|
|
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFrameworkConfig
|
|
|
|
plugins {
|
|
kotlin("multiplatform")
|
|
}
|
|
|
|
|
|
val libName = "Flatbuffers"
|
|
group = "com.google.flatbuffers.kotlin"
|
|
version = "2.0.0-SNAPSHOT"
|
|
|
|
kotlin {
|
|
explicitApi()
|
|
jvm()
|
|
js(IR) {
|
|
browser {
|
|
testTask {
|
|
enabled = false
|
|
}
|
|
}
|
|
binaries.executable()
|
|
}
|
|
macosX64()
|
|
macosArm64()
|
|
iosArm64()
|
|
iosSimulatorArm64()
|
|
|
|
sourceSets {
|
|
|
|
val commonMain by getting {
|
|
dependencies {
|
|
implementation(kotlin("stdlib-common"))
|
|
}
|
|
}
|
|
|
|
val commonTest by getting {
|
|
dependencies {
|
|
implementation(kotlin("test"))
|
|
}
|
|
|
|
kotlin.srcDir("src/commonTest/generated/kotlin/")
|
|
}
|
|
val jvmTest by getting {
|
|
dependencies {
|
|
implementation(kotlin("test-junit"))
|
|
implementation("com.google.flatbuffers:flatbuffers-java:2.0.3")
|
|
}
|
|
}
|
|
val jvmMain by getting {
|
|
}
|
|
|
|
val macosX64Main by getting
|
|
val macosArm64Main by getting
|
|
val iosArm64Main by getting
|
|
val iosSimulatorArm64Main by getting
|
|
|
|
val nativeMain by creating {
|
|
// this sourceSet will hold common cold for all iOS targets
|
|
dependsOn(commonMain)
|
|
macosArm64Main.dependsOn(this)
|
|
macosX64Main.dependsOn(this)
|
|
iosArm64Main.dependsOn(this)
|
|
iosSimulatorArm64Main.dependsOn(this)
|
|
}
|
|
|
|
all {
|
|
languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fixes JS issue: https://youtrack.jetbrains.com/issue/KT-49109
|
|
rootProject.plugins.withType<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin> {
|
|
rootProject.the<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension>().nodeVersion = "16.0.0"
|
|
|
|
}
|
|
|
|
// Use the default greeting
|
|
tasks.register<GenerateFBTestClasses>("generateFBTestClassesKt") {
|
|
inputFiles.setFrom("$rootDir/../tests/monster_test.fbs",
|
|
"$rootDir/../tests/dictionary_lookup.fbs",
|
|
// @todo Seems like nesting code generation is broken for all generators.
|
|
// disabling test for now.
|
|
// "$rootDir/../tests/namespace_test/namespace_test1.fbs",
|
|
// "$rootDir/../tests/namespace_test/namespace_test2.fbs",
|
|
"$rootDir/../tests/union_vector/union_vector.fbs",
|
|
"$rootDir/../tests/optional_scalars.fbs")
|
|
includeFolder.set("$rootDir/../tests/include_test")
|
|
outputFolder.set("${projectDir}/src/commonTest/generated/kotlin/")
|
|
variant.set("kotlin-kmp")
|
|
}
|
|
|
|
|
|
project.tasks.forEach {
|
|
if (it.name.contains("compileKotlin"))
|
|
it.dependsOn("generateFBTestClassesKt")
|
|
}
|
|
|
|
fun String.intProperty() = findProperty(this).toString().toInt()
|
|
|
|
abstract class GenerateFBTestClasses : DefaultTask() {
|
|
@get:InputFiles
|
|
abstract val inputFiles: ConfigurableFileCollection
|
|
|
|
@get:Input
|
|
abstract val includeFolder: Property<String>
|
|
|
|
@get:Input
|
|
abstract val outputFolder: Property<String>
|
|
|
|
@get:Input
|
|
abstract val variant: Property<String>
|
|
|
|
@Inject
|
|
protected open fun getExecActionFactory(): org.gradle.process.internal.ExecActionFactory? {
|
|
throw UnsupportedOperationException()
|
|
}
|
|
|
|
init {
|
|
includeFolder.set("")
|
|
}
|
|
|
|
@TaskAction
|
|
fun compile() {
|
|
val execAction = getExecActionFactory()!!.newExecAction()
|
|
val sources = inputFiles.asPath.split(":")
|
|
val args = mutableListOf("flatc","-o", outputFolder.get(), "--${variant.get()}")
|
|
if (includeFolder.get().isNotEmpty()) {
|
|
args.add("-I")
|
|
args.add(includeFolder.get())
|
|
}
|
|
args.addAll(sources)
|
|
println(args)
|
|
execAction.commandLine = args
|
|
print(execAction.execute())
|
|
}
|
|
}
|