Bind KotlinIterator via stdlib extern class
This commit is contained in:
parent
6295cea6ad
commit
d8d2eff436
@ -26,6 +26,7 @@ import net.sergeych.lyng.miniast.*
|
|||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
import net.sergeych.lyng.pacman.ImportManager
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
import net.sergeych.lyng.stdlib_included.rootLyng
|
import net.sergeych.lyng.stdlib_included.rootLyng
|
||||||
|
import net.sergeych.lyng.bridge.LyngClassBridge
|
||||||
import net.sergeych.lynon.ObjLynonClass
|
import net.sergeych.lynon.ObjLynonClass
|
||||||
import net.sergeych.mp_tools.globalDefer
|
import net.sergeych.mp_tools.globalDefer
|
||||||
import kotlin.math.*
|
import kotlin.math.*
|
||||||
@ -560,9 +561,12 @@ class Script(
|
|||||||
|
|
||||||
val defaultImportManager: ImportManager by lazy {
|
val defaultImportManager: ImportManager by lazy {
|
||||||
ImportManager(rootScope, SecurityManager.allowAll).apply {
|
ImportManager(rootScope, SecurityManager.allowAll).apply {
|
||||||
addTextPackages(
|
addPackage("lyng.stdlib") { module ->
|
||||||
rootLyng
|
module.eval(Source("lyng.stdlib", rootLyng))
|
||||||
)
|
val cls = module["KotlinIterator"]?.value as? ObjClass
|
||||||
|
?: module.raiseSymbolNotFound("KotlinIterator")
|
||||||
|
ObjKotlinIterator.bindTo(cls)
|
||||||
|
}
|
||||||
addPackage("lyng.buffer") {
|
addPackage("lyng.buffer") {
|
||||||
it.addConstDoc(
|
it.addConstDoc(
|
||||||
name = "Buffer",
|
name = "Buffer",
|
||||||
|
|||||||
@ -22,6 +22,7 @@ package net.sergeych.lyng.obj
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import net.sergeych.lyng.Scope
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.bridge.LyngClassBridge
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterator wrapper to allow Kotlin collections to be returned from Lyng objects;
|
* 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
|
override val objClass get() = type
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val type = ObjClass("KotlinIterator", ObjIterator).apply {
|
private var boundType: ObjClass? = null
|
||||||
addFn("next") { thisAs<ObjKotlinIterator>().iterator.next().toObj() }
|
|
||||||
addFn("hasNext") { thisAs<ObjKotlinIterator>().iterator.hasNext().toObj() }
|
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
|
override val objClass get() = type
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val type = ObjClass("KotlinIterator", ObjIterator).apply {
|
val type: ObjClass
|
||||||
addFn("next") {
|
get() = ObjKotlinIterator.type
|
||||||
thisAs<ObjKotlinObjIterator>().iterator.next()
|
|
||||||
}
|
|
||||||
addFn("hasNext") {
|
|
||||||
thisAs<ObjKotlinObjIterator>().iterator.hasNext().toObj()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,4 +94,3 @@ fun Obj.toFlow(scope: Scope): Flow<Obj> = flow {
|
|||||||
emit(next.invoke(scope, iterator))
|
emit(next.invoke(scope, iterator))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,12 @@ extern class Iterator<T> {
|
|||||||
fun toList(): List<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> {
|
extern class Collection<T> : Iterable<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user