-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Object and companion object support (#15)
* Expose plugin version * KneeObject annotation, collect objects * Basic ObjectCodec * Support functions inside objects * Support properties inside objects * Support companion objects * Object docs * Fix override modifier
- Loading branch information
Showing
27 changed files
with
359 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ docs: | |
- builtin-types | ||
- enums | ||
- classes | ||
- objects | ||
- interfaces | ||
- buffers | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: Objects | ||
description: > | ||
Understand how Knee compiler plugin can serialize declared objects and let you pass them from Kotlin Native | ||
to the JVM and vice versa, including support for externally defined objects. | ||
--- | ||
|
||
# Objects | ||
|
||
## Annotating objects | ||
|
||
Whenever you declare an object, you can use the `@KneeObject` annotation to tell the compiler that it should be processed. | ||
Knee supports objects in different scenarios: | ||
|
||
- top level objects | ||
- objects nested inside another declaration | ||
- `companion` objects | ||
|
||
```kotlin | ||
@KneeObject object Foo { | ||
... | ||
} | ||
|
||
class Utilities { | ||
@KneeObject object Bar { ... } | ||
@KneeObject companion object { ... } | ||
} | ||
``` | ||
|
||
Under the hood, objects are *not* actually serialized and passed through the JNI interface: since there can only be a single | ||
instance of an object, no extra information is needed and the compiler can retrieve the object field statically on both | ||
platforms. | ||
|
||
## Annotating members | ||
|
||
All callable members (functions, properties, constructors) of an object can be made available to the JVM side, but | ||
they must be explicitly marked with the `@Knee` annotation as described in the [callables](callables) documentation. | ||
|
||
```kotlin | ||
@KneeObject object Game { | ||
@Knee fun start() { ... } | ||
fun loop() { ... } | ||
} | ||
``` | ||
|
||
In the example above, only the `start` function will be available on the JVM side. | ||
|
||
## Importing objects | ||
|
||
If you wish to annotate existing objects that you don't control, for example those coming from a different module, | ||
you can technically use `@KneeObject` on type aliases. Unfortunately as of now, this functionality is very limited in that you | ||
can't choose which declarations will be imported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.deepmedia.tools.knee.plugin.compiler | ||
|
||
import com.squareup.kotlinpoet.* | ||
import io.deepmedia.tools.knee.plugin.compiler.codegen.CodegenClass | ||
import io.deepmedia.tools.knee.plugin.compiler.codegen.KneeCodegen | ||
import io.deepmedia.tools.knee.plugin.compiler.context.KneeContext | ||
import io.deepmedia.tools.knee.plugin.compiler.symbols.KneeSymbols | ||
import io.deepmedia.tools.knee.plugin.compiler.jni.JniType | ||
import io.deepmedia.tools.knee.plugin.compiler.codec.CodegenCodecContext | ||
import io.deepmedia.tools.knee.plugin.compiler.codec.IrCodecContext | ||
import io.deepmedia.tools.knee.plugin.compiler.codec.Codec | ||
import io.deepmedia.tools.knee.plugin.compiler.features.KneeObject | ||
import io.deepmedia.tools.knee.plugin.compiler.instances.InstancesCodegen.HandleField | ||
import io.deepmedia.tools.knee.plugin.compiler.utils.asModifier | ||
import io.deepmedia.tools.knee.plugin.compiler.utils.asTypeSpec | ||
import io.deepmedia.tools.knee.plugin.compiler.utils.codegenFqName | ||
import org.jetbrains.kotlin.ir.builders.* | ||
import org.jetbrains.kotlin.ir.declarations.IrClass | ||
import org.jetbrains.kotlin.ir.declarations.IrValueDeclaration | ||
import org.jetbrains.kotlin.ir.expressions.IrExpression | ||
import org.jetbrains.kotlin.ir.util.* | ||
|
||
fun preprocessObject(klass: KneeObject, context: KneeContext) { | ||
context.mapper.register(ObjectCodec( | ||
symbols = context.symbols, | ||
irClass = klass.source, | ||
)) | ||
} | ||
|
||
fun processObject(klass: KneeObject, context: KneeContext, codegen: KneeCodegen) { | ||
klass.makeCodegen(codegen) | ||
} | ||
|
||
private fun KneeObject.makeCodegen(codegen: KneeCodegen) { | ||
val container = codegen.prepareContainer(source, importInfo) | ||
codegenClone = container.addChildIfNeeded(CodegenClass(source.asTypeSpec())).apply { | ||
if (codegen.verbose) spec.addKdoc("knee:objects") | ||
spec.addModifiers(source.visibility.asModifier()) | ||
codegenProducts.add(this) | ||
} | ||
} | ||
|
||
class ObjectCodec( | ||
symbols: KneeSymbols, | ||
private val irClass: IrClass, | ||
) : Codec(irClass.defaultType, JniType.Byte(symbols)) { | ||
|
||
override fun IrStatementsBuilder<*>.irEncode(irContext: IrCodecContext, local: IrValueDeclaration): IrExpression { | ||
return irByte(0) | ||
} | ||
|
||
override fun IrStatementsBuilder<*>.irDecode(irContext: IrCodecContext, jni: IrValueDeclaration): IrExpression { | ||
return irGetObject(irClass.symbol) | ||
} | ||
|
||
override fun CodeBlock.Builder.codegenDecode(codegenContext: CodegenCodecContext, jni: String): String { | ||
return irClass.codegenFqName.asString() | ||
} | ||
|
||
override fun CodeBlock.Builder.codegenEncode(codegenContext: CodegenCodecContext, local: String): String { | ||
return "0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.