bridge: add ScopeFacade receiver overloads for binder callbacks

This commit is contained in:
sergeych committed 2026-03-14 19:53:46 +03:00
1 parent 65edf9fe67
commit 8a32ffe655
6 files changed
+212 -62

No files matched your search

+12
View File
@@ -146,10 +146,22 @@ interface ClassBridgeBinder {
var classData: Any?
fun init(block: suspend BridgeInstanceContext.(ScopeFacade) -> Unit)
fun initWithInstance(block: suspend ScopeFacade.() -> Unit)
@Deprecated("Use receiver-form initWithInstance and read thisObj from ScopeFacade")
fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit)
fun addFun(name: String, impl: suspend ScopeFacade.() -> Obj)
@Deprecated("Use receiver-form addFun and read thisObj/args from ScopeFacade")
fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj)
fun addVal(name: String, impl: suspend ScopeFacade.() -> Obj)
@Deprecated("Use receiver-form addVal and read thisObj from ScopeFacade")
fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj)
fun addVar(
name: String,
get: suspend ScopeFacade.() -> Obj,
set: suspend ScopeFacade.(Obj) -> Unit
)
@Deprecated("Use receiver-form addVar and read thisObj from ScopeFacade")
fun addVar(
name: String,
get: suspend (ScopeFacade, Obj) -> Obj,
+9 -9
View File
@@ -39,34 +39,34 @@ LyngClassBridge.bind(className = "Foo", module = "bridge.mod", importManager = i
data = CounterState(0)
}
addFun("add") { _, _, args ->
addFun("add") {
val a = (args.list[0] as ObjInt).value
val b = (args.list[1] as ObjInt).value
ObjInt.of(a + b)
}
addVal("status") { _, _ -> ObjString(classData as String) }
addVal("status") { ObjString(classData as String) }
addVar(
"count",
get = { _, instance ->
val st = (instance as ObjInstance).data as CounterState
get = {
val st = (thisObj as ObjInstance).data as CounterState
ObjInt.of(st.count)
},
set = { _, instance, value ->
val st = (instance as ObjInstance).data as CounterState
set = { value ->
val st = (thisObj as ObjInstance).data as CounterState
st.count = (value as ObjInt).value
}
)
addFun("secret") { _, _, _ -> ObjInt.of(42) }
addFun("ping") { _, _, _ -> ObjInt.of(7) }
addFun("secret") { ObjInt.of(42) }
addFun("ping") { ObjInt.of(7) }
}
```
## Notes
- Visibility is respected by usual Lyng access rules; private extern members can be used only within class code.
- Use `init { scope -> ... }` for receiver-style init, or `initWithInstance { scope, instance -> ... }` for explicit instance access.
- Use `init { ... }` / `initWithInstance { ... }` with `ScopeFacade` receiver; access instance via `thisObj`.
- `classData` and `instance.data` are Kotlin-only payloads and do not appear in Lyng reflection.
- Binding after the first instance of a class is created throws a `ScriptError`.