Bind KotlinIterator via stdlib extern class

This commit is contained in:
Sergey Chernov 2026-02-15 01:37:40 +03:00
parent 6295cea6ad
commit d8d2eff436
3 changed files with 43 additions and 15 deletions

View File

@ -26,6 +26,7 @@ import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.stdlib_included.rootLyng
import net.sergeych.lyng.bridge.LyngClassBridge
import net.sergeych.lynon.ObjLynonClass
import net.sergeych.mp_tools.globalDefer
import kotlin.math.*
@ -560,9 +561,12 @@ class Script(
val defaultImportManager: ImportManager by lazy {
ImportManager(rootScope, SecurityManager.allowAll).apply {
addTextPackages(
rootLyng
)
addPackage("lyng.stdlib") { module ->
module.eval(Source("lyng.stdlib", rootLyng))
val cls = module["KotlinIterator"]?.value as? ObjClass
?: module.raiseSymbolNotFound("KotlinIterator")
ObjKotlinIterator.bindTo(cls)
}
addPackage("lyng.buffer") {
it.addConstDoc(
name = "Buffer",

View File

@ -22,6 +22,7 @@ package net.sergeych.lyng.obj
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import net.sergeych.lyng.Scope
import net.sergeych.lyng.bridge.LyngClassBridge
/**
* Iterator wrapper to allow Kotlin collections to be returned from Lyng objects;
@ -32,9 +33,33 @@ class ObjKotlinIterator(val iterator: Iterator<Any?>) : Obj() {
override val objClass get() = type
companion object {
val type = ObjClass("KotlinIterator", ObjIterator).apply {
addFn("next") { thisAs<ObjKotlinIterator>().iterator.next().toObj() }
addFn("hasNext") { thisAs<ObjKotlinIterator>().iterator.hasNext().toObj() }
private var boundType: ObjClass? = null
val type: ObjClass
get() = boundType ?: error("KotlinIterator class is not bound; import lyng.stdlib first")
internal fun bindTo(cls: ObjClass) {
if (boundType != null) {
if (boundType === cls) return
return
}
boundType = cls
LyngClassBridge.bind(cls) {
addFun("next") { scope, self, _ ->
when (self) {
is ObjKotlinIterator -> self.iterator.next().toObj()
is ObjKotlinObjIterator -> self.iterator.next()
else -> scope.raiseClassCastError("Expected KotlinIterator, got ${self.objClass.className}")
}
}
addFun("hasNext") { scope, self, _ ->
when (self) {
is ObjKotlinIterator -> self.iterator.hasNext().toObj()
is ObjKotlinObjIterator -> self.iterator.hasNext().toObj()
else -> scope.raiseClassCastError("Expected KotlinIterator, got ${self.objClass.className}")
}
}
}
}
}
@ -49,14 +74,8 @@ class ObjKotlinObjIterator(val iterator: Iterator<Obj>) : Obj() {
override val objClass get() = type
companion object {
val type = ObjClass("KotlinIterator", ObjIterator).apply {
addFn("next") {
thisAs<ObjKotlinObjIterator>().iterator.next()
}
addFn("hasNext") {
thisAs<ObjKotlinObjIterator>().iterator.hasNext().toObj()
}
}
val type: ObjClass
get() = ObjKotlinIterator.type
}
}
@ -75,4 +94,3 @@ fun Obj.toFlow(scope: Scope): Flow<Obj> = flow {
emit(next.invoke(scope, iterator))
}
}

View File

@ -21,6 +21,12 @@ extern class Iterator<T> {
fun toList(): List<T>
}
// Host-provided iterator wrapper for Kotlin collections.
class KotlinIterator<T> : Iterator<T> {
extern override fun hasNext(): Bool
extern override fun next(): T
}
extern class Collection<T> : Iterable<T> {
}