Skip to content

Conversation

paulovap
Copy link
Contributor

@paulovap paulovap commented Feb 5, 2023

This PR adds a new kotlin code generator and new runtime for Kotlin multiplaform.

Previous implementation of the kotlin generator relies on java libraries and limited to JVM only implementation. This new generator breaks away for that approach by creating a pure kotlin runtime, which can be used, in theory, on any platform that Kotlin multiplaforms supports.

Aside that, the implementation adds more type safety in the API at little to no cost, due to the value class types on kotlin.

For now the new kotlin code can be generated with the flag --kotlin-kmp on flatc. This will change as soon as we publish the libraries on maven, making it the default implementation in the future. Deprecating the old one over time.

Summary of features:

  • 1:1 feature parity with old kotlin implementation
  • add type safe API for passing offsets as parameters
  • enums are represented as value classes
  • Supports iOS and MacOS as targets, more will come later.

What is missing:

  • As the old Kotlin and Java API, enums can only return Tables.
  • Support Java's ByteBuffer
  • Support Okio

@github-actions github-actions bot added c++ codegen Involving generating code from schema kotlin labels Feb 5, 2023
@paulovap
Copy link
Contributor Author

paulovap commented Feb 5, 2023

Address issues #7786, #7716, #7240, #7185

@paulovap paulovap force-pushed the pp/kotlin-kmp branch 5 times, most recently from 2966ca6 to f57216a Compare February 7, 2023 15:08
@paulovap paulovap force-pushed the pp/kotlin-kmp branch 3 times, most recently from a253802 to d1f191a Compare February 8, 2023 10:32
@github-actions github-actions bot added the CI Continuous Integration label Feb 8, 2023
@paulovap paulovap force-pushed the pp/kotlin-kmp branch 10 times, most recently from e3f6e7d to fa5cb13 Compare February 14, 2023 08:37
@paulovap paulovap force-pushed the pp/kotlin-kmp branch 4 times, most recently from 9ef416d to b2f9821 Compare February 21, 2023 08:52
@paulovap paulovap marked this pull request as draft February 24, 2023 10:49
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.
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.
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.
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)
Moving to flatbuffer enums to value class adds type safety for parameters
with minimum to no performance impact.
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().
@github-actions
Copy link
Contributor

github-actions bot commented Apr 6, 2023

This pull request is stale because it has been open 3 weeks with no activity. Please comment or label not-stale, or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Apr 6, 2023
@github-actions
Copy link
Contributor

This pull request was automatically closed due to no activity for 3 weeks plus the 7 day notice period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ CI Continuous Integration codegen Involving generating code from schema kotlin stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants