improved docs on embedding
This commit is contained in:
parent
c351e6ff1e
commit
d6a535590e
@ -240,19 +240,22 @@ Notes:
|
|||||||
|
|
||||||
For `extern object` declarations, bind implementations to the singleton instance using `ModuleScope.bindObject`.
|
For `extern object` declarations, bind implementations to the singleton instance using `ModuleScope.bindObject`.
|
||||||
This mirrors class binding but targets an already created object instance.
|
This mirrors class binding but targets an already created object instance.
|
||||||
|
As with class binding, you must first add/evaluate the Lyng declaration into that module scope, then bind Kotlin handlers.
|
||||||
```lyng
|
|
||||||
// Lyng side (in a module)
|
|
||||||
extern object HostObject {
|
|
||||||
extern fun add(a: Int, b: Int): Int
|
|
||||||
extern val status: String
|
|
||||||
extern var count: Int
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
// Kotlin side (binding)
|
// Kotlin side (binding)
|
||||||
val moduleScope = importManager.createModuleScope(Pos.builtIn, "bridge.obj")
|
val moduleScope = importManager.createModuleScope(Pos.builtIn, "bridge.obj")
|
||||||
|
|
||||||
|
// 1) Seed the module with the Lyng declaration first
|
||||||
|
moduleScope.eval("""
|
||||||
|
extern object HostObject {
|
||||||
|
extern fun add(a: Int, b: Int): Int
|
||||||
|
extern val status: String
|
||||||
|
extern var count: Int
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
|
||||||
|
// 2) Then bind Kotlin implementations to that declared object
|
||||||
moduleScope.bindObject("HostObject") {
|
moduleScope.bindObject("HostObject") {
|
||||||
classData = "OK"
|
classData = "OK"
|
||||||
init { _ -> data = 0L }
|
init { _ -> data = 0L }
|
||||||
@ -272,6 +275,8 @@ moduleScope.bindObject("HostObject") {
|
|||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
|
|
||||||
|
- Required order: declare/eval Lyng object in the module first, then call `bindObject(...)`.
|
||||||
|
This is the pattern covered by `BridgeBindingTest.testExternObjectBinding`.
|
||||||
- Members must be marked `extern` so the compiler emits ABI slots for Kotlin bindings.
|
- Members must be marked `extern` so the compiler emits ABI slots for Kotlin bindings.
|
||||||
- You can also bind by name/module via `LyngObjectBridge.bind(...)`.
|
- You can also bind by name/module via `LyngObjectBridge.bind(...)`.
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user