Compare commits
41
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37d093817e | ||
|
|
c54d947f1c | ||
|
|
8a32ffe655 | ||
|
|
65edf9fe67 | ||
|
|
18394ce286 | ||
|
|
298c2a09fe | ||
|
|
a9da21e7a4 | ||
|
|
4581998ba3 | ||
|
|
62dc501657 | ||
|
|
8b5e6ee993 | ||
|
|
9417f8f0cc | ||
|
|
c9021eb9cf | ||
|
|
3bfb80a7c1 | ||
|
|
32a7adf56e | ||
|
|
90e586b5d2 | ||
|
|
f03006ce37 | ||
|
|
1502a365bf | ||
|
|
d6a535590e | ||
|
|
c351e6ff1e | ||
|
|
8e10540257 | ||
|
|
9f10786a94 | ||
|
|
274abaaf03 | ||
|
|
51b0d1fdfc | ||
|
|
c46f74bd7e | ||
|
|
87ef1c38b8 | ||
|
|
529f76489b | ||
|
|
6bf99d354b | ||
|
|
66d9a40bb1 | ||
|
|
d82302dd01 | ||
|
|
e7c1adb2c5 | ||
|
|
45f3658742 | ||
|
|
087143b022 | ||
|
|
6d943c5e1e | ||
|
|
79a541b148 | ||
|
|
64debf78bd | ||
|
|
f0bcee407a | ||
|
|
5b15d85c14 | ||
|
|
28d3f8364c | ||
|
|
dc9885c218 | ||
|
|
a0ecbfd334 | ||
|
|
f357f4039b |
No files matched your search
@@ -13,6 +13,7 @@
|
||||
- Object members are always allowed even on unknown types; non-Object members require explicit casts. Remove `inspect` from Object and use `toInspectString()` instead.
|
||||
- Type expression checks: `x is T` is value instance check; `T1 is T2` is type-subset; `A in T` means `A` is subset of `T`; `==` is structural type equality.
|
||||
- Type aliases: `type Name = TypeExpr` (generic allowed) expand to their underlying type expressions; no nominal distinctness.
|
||||
- Bounds and variance: `T: A & B` / `T: A | B` for bounds; declaration-site variance with `out` / `in`.
|
||||
- Do not reintroduce bytecode fallback opcodes (e.g., `GET_NAME`, `EVAL_*`, `CALL_FALLBACK`) or runtime name-resolution fallbacks; all symbol resolution must stay compile-time only.
|
||||
|
||||
## Bytecode frame-first migration plan
|
||||
|
||||
+17
-3
@@ -4,6 +4,7 @@ High-density specification for LLMs. Reference this for all Lyng code generation
|
||||
|
||||
## 1. Core Philosophy & Syntax
|
||||
- **Everything is an Expression**: Blocks, `if`, `when`, `for`, `while`, `do-while` return their last expression (or `void`).
|
||||
- **Static Types + Inference**: Every declaration has a compile-time type (explicit or inferred). Types are Kotlin‑style: non‑null by default, nullable with `?`.
|
||||
- **Loops with `else`**: `for`, `while`, and `do-while` support an optional `else` block.
|
||||
- `else` executes **only if** the loop finishes normally (without a `break`).
|
||||
- `break <value>` exits the loop and sets its return value.
|
||||
@@ -13,6 +14,7 @@ High-density specification for LLMs. Reference this for all Lyng code generation
|
||||
3. Result of the last iteration (if loop finished normally and no `else`).
|
||||
4. `void` (if loop body never executed and no `else`).
|
||||
- **Implicit Coroutines**: All functions are coroutines. No `async/await`. Use `launch { ... }` (returns `Deferred`) or `flow { ... }`.
|
||||
- **Functions**: Use `fun` or the short form `fn`. Function declarations are expressions returning a callable.
|
||||
- **Variables**: `val` (read-only), `var` (mutable). Supports late-init `val` in classes (must be assigned in `init` or body).
|
||||
- **Serialization**: Use `@Transient` attribute before `val`/`var` or constructor parameters to exclude them from Lynon/JSON serialization. Transient fields are also ignored during `==` structural equality checks.
|
||||
- **Null Safety**: `?` (nullable type), `?.` (safe access), `?( )` (safe invoke), `?{ }` (safe block invoke), `?[ ]` (safe index), `?:` or `??` (elvis), `?=` (assign-if-null).
|
||||
@@ -44,9 +46,21 @@ High-density specification for LLMs. Reference this for all Lyng code generation
|
||||
- **Root Type**: Everything is an `Object` (root of the hierarchy).
|
||||
- **Nullability**: Non-null by default (`T`), nullable with `T?`, `!!` asserts non-null.
|
||||
- **Untyped params**: `fun foo(x)` -> `x: Object`, `fun foo(x?)` -> `x: Object?`.
|
||||
- **Untyped vars**: `var x` is `Unset` until first assignment locks the type.
|
||||
- **Inference**: List/map literals infer union element types; empty list is `List<Object>`, empty map is `{:}`.
|
||||
- **Generics**: Bounds with `T: A & B` or `T: A | B`; variance uses `out`/`in`.
|
||||
- **Untyped vars**: `var x` is `Unset` until first assignment locks the type (including nullability).
|
||||
- `val x = null` -> type `Null`; `var x = null` -> type `Object?`.
|
||||
- **Inference**:
|
||||
- List literals infer union element types; empty list defaults to `List<Object>` unless constrained.
|
||||
- Map literals infer key/value types; empty map defaults to `Map<Object, Object>` unless constrained.
|
||||
- Mixed numeric ops promote `Int` + `Real` to `Real`.
|
||||
- **Type aliases**: `type Name = TypeExpr` (generic allowed). Aliases expand to their underlying type expressions (no nominal distinctness).
|
||||
- **Generics**: Bounds with `T: A & B` or `T: A | B`; variance uses `out`/`in` (declaration‑site only).
|
||||
- **Casts**: `as` is a runtime-checked cast; `as?` is safe-cast returning `null`. If the value is nullable, `as T` implies `!!`.
|
||||
|
||||
## 2.2 Type Expressions and Checks
|
||||
- **Value checks**: `x is T` (runtime instance check).
|
||||
- **Type checks**: `T1 is T2` and `A in T` are subset checks between type expressions (compile-time where possible).
|
||||
- **Type equality**: `T1 == T2` is structural (unions/intersections are order‑insensitive).
|
||||
- **Compile-time enforcement**: Bounds are checked at call sites; runtime checks only appear when the compile‑time type is too general.
|
||||
|
||||
## 3. Delegation (`by`)
|
||||
Unified model for `val`, `var`, and `fun`.
|
||||
|
||||
@@ -32,9 +32,9 @@ class A {
|
||||
enum E* { One, Two }
|
||||
}
|
||||
val ab = A.B()
|
||||
assertEquals(ab.x, null)
|
||||
assertEquals(A.Inner.foo, "bar")
|
||||
assertEquals(A.One, A.E.One)
|
||||
assertEquals(null, ab.x)
|
||||
assertEquals("bar", A.Inner.foo)
|
||||
assertEquals(A.E.One, A.One)
|
||||
```
|
||||
|
||||
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
|
||||
|
||||
+5
-6
@@ -20,13 +20,12 @@
|
||||
set -e
|
||||
echo "publishing all artifacts"
|
||||
echo
|
||||
./gradlew publishToMavenLocal
|
||||
./gradlew publish
|
||||
./gradlew publishToMavenLocal site:jsBrowserDistribution publish buildInstallablePlugin :lyng:linkReleaseExecutableLinuxX64 :lyng:installJvmDist --parallel
|
||||
|
||||
echo
|
||||
echo "Creating plugin"
|
||||
echo
|
||||
./gradlew buildInstallablePlugin
|
||||
#echo
|
||||
#echo "Creating plugin"
|
||||
#echo
|
||||
#./gradlew buildInstallablePlugin
|
||||
|
||||
echo
|
||||
echo "building CLI tools"
|
||||
|
||||
@@ -35,3 +35,27 @@ tasks.register<Exec>("generateDocs") {
|
||||
description = "Generates a single-file documentation HTML using bin/generate_docs.sh"
|
||||
commandLine("./bin/generate_docs.sh")
|
||||
}
|
||||
|
||||
// Sample generator task for .lyng.d definition files (not wired into build).
|
||||
// Usage: ./gradlew generateLyngDefsSample
|
||||
tasks.register("generateLyngDefsSample") {
|
||||
group = "lyng"
|
||||
description = "Generate a sample .lyng.d file under build/generated/lyng/defs"
|
||||
outputs.dir(layout.buildDirectory.dir("generated/lyng/defs"))
|
||||
doLast {
|
||||
val outDir = layout.buildDirectory.dir("generated/lyng/defs").get().asFile
|
||||
outDir.mkdirs()
|
||||
val outFile = outDir.resolve("sample.lyng.d")
|
||||
outFile.writeText(
|
||||
"""
|
||||
/** Generated API */
|
||||
extern fun ping(): Int
|
||||
|
||||
/** Generated class */
|
||||
class Generated(val name: String) {
|
||||
fun greet(): String = "hi " + name
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,8 +1,8 @@
|
||||
# Array
|
||||
|
||||
It's an interface if the [Collection] that provides indexing access, like `array[3] = 0`.
|
||||
Array therefore implements [Iterable] too. The well known implementatino of the `Array` is
|
||||
[List].
|
||||
Array therefore implements [Iterable] too. Well known implementations of `Array` are
|
||||
[List] and [ImmutableList].
|
||||
|
||||
Array adds the following methods:
|
||||
|
||||
@@ -35,3 +35,4 @@ To pre-sort and array use `Iterable.sorted*` or in-place `List.sort*` families,
|
||||
[Collection]: Collection.md
|
||||
[Iterable]: Iterable.md
|
||||
[List]: List.md
|
||||
[ImmutableList]: ImmutableList.md
|
||||
@@ -6,6 +6,13 @@ Is a [Iterable] with known `size`, a finite [Iterable]:
|
||||
val size
|
||||
}
|
||||
|
||||
`Collection` is a read/traversal contract shared by mutable and immutable collections.
|
||||
Concrete collection classes:
|
||||
|
||||
- Mutable: [List], [Set], [Map]
|
||||
- Immutable: [ImmutableList], [ImmutableSet], [ImmutableMap]
|
||||
- Observable mutable lists (opt-in module): [ObservableList]
|
||||
|
||||
| name | description |
|
||||
|------------------------|------------------------------------------------------|
|
||||
|
||||
@@ -17,3 +24,8 @@ See [List], [Set], [Iterable] and [Efficient Iterables in Kotlin Interop](Effici
|
||||
[Iterable]: Iterable.md
|
||||
[List]: List.md
|
||||
[Set]: Set.md
|
||||
[Map]: Map.md
|
||||
[ImmutableList]: ImmutableList.md
|
||||
[ImmutableSet]: ImmutableSet.md
|
||||
[ImmutableMap]: ImmutableMap.md
|
||||
[ObservableList]: ObservableList.md
|
||||
@@ -0,0 +1,37 @@
|
||||
# ImmutableList built-in class
|
||||
|
||||
`ImmutableList` is an immutable, indexable list value.
|
||||
It implements [Array], therefore [Collection] and [Iterable].
|
||||
|
||||
Use it when API contracts require a list that cannot be mutated through aliases.
|
||||
|
||||
## Creating
|
||||
|
||||
val a = ImmutableList(1,2,3)
|
||||
val b = [1,2,3].toImmutable()
|
||||
val c = (1..3).toImmutableList()
|
||||
>>> void
|
||||
|
||||
## Converting
|
||||
|
||||
val i = ImmutableList(1,2,3)
|
||||
val m = i.toMutable()
|
||||
m += 4
|
||||
assertEquals( ImmutableList(1,2,3), i )
|
||||
assertEquals( [1,2,3,4], m )
|
||||
>>> void
|
||||
|
||||
## Members
|
||||
|
||||
| name | meaning |
|
||||
|---------------|-----------------------------------------|
|
||||
| `size` | number of elements |
|
||||
| `[index]` | element access by index |
|
||||
| `[Range]` | immutable slice |
|
||||
| `+` | append element(s), returns new immutable list |
|
||||
| `-` | remove element(s), returns new immutable list |
|
||||
| `toMutable()` | create mutable copy |
|
||||
|
||||
[Array]: Array.md
|
||||
[Collection]: Collection.md
|
||||
[Iterable]: Iterable.md
|
||||
@@ -0,0 +1,36 @@
|
||||
# ImmutableMap built-in class
|
||||
|
||||
`ImmutableMap` is an immutable map of key-value pairs.
|
||||
It implements [Collection] and [Iterable] of [MapEntry].
|
||||
|
||||
## Creating
|
||||
|
||||
val a = ImmutableMap("a" => 1, "b" => 2)
|
||||
val b = Map("a" => 1, "b" => 2).toImmutable()
|
||||
val c = ["a" => 1, "b" => 2].toImmutableMap
|
||||
>>> void
|
||||
|
||||
## Converting
|
||||
|
||||
val i = ImmutableMap("a" => 1)
|
||||
val m = i.toMutable()
|
||||
m["a"] = 2
|
||||
assertEquals( 1, i["a"] )
|
||||
assertEquals( 2, m["a"] )
|
||||
>>> void
|
||||
|
||||
## Members
|
||||
|
||||
| name | meaning |
|
||||
|-----------------|------------------------------------------|
|
||||
| `size` | number of entries |
|
||||
| `[key]` | get value by key, or `null` if absent |
|
||||
| `getOrNull(key)`| same as `[key]` |
|
||||
| `keys` | list of keys |
|
||||
| `values` | list of values |
|
||||
| `+` | merge (rightmost wins), returns new immutable map |
|
||||
| `toMutable()` | create mutable copy |
|
||||
|
||||
[Collection]: Collection.md
|
||||
[Iterable]: Iterable.md
|
||||
[MapEntry]: Map.md
|
||||
@@ -0,0 +1,34 @@
|
||||
# ImmutableSet built-in class
|
||||
|
||||
`ImmutableSet` is an immutable set of unique elements.
|
||||
It implements [Collection] and [Iterable].
|
||||
|
||||
## Creating
|
||||
|
||||
val a = ImmutableSet(1,2,3)
|
||||
val b = Set(1,2,3).toImmutable()
|
||||
val c = [1,2,3].toImmutableSet
|
||||
>>> void
|
||||
|
||||
## Converting
|
||||
|
||||
val i = ImmutableSet(1,2,3)
|
||||
val m = i.toMutable()
|
||||
m += 4
|
||||
assertEquals( ImmutableSet(1,2,3), i )
|
||||
assertEquals( Set(1,2,3,4), m )
|
||||
>>> void
|
||||
|
||||
## Members
|
||||
|
||||
| name | meaning |
|
||||
|---------------|-----------------------------------------------------|
|
||||
| `size` | number of elements |
|
||||
| `contains(x)` | membership test |
|
||||
| `+`, `union` | union, returns new immutable set |
|
||||
| `-`, `subtract` | subtraction, returns new immutable set |
|
||||
| `*`, `intersect` | intersection, returns new immutable set |
|
||||
| `toMutable()` | create mutable copy |
|
||||
|
||||
[Collection]: Collection.md
|
||||
[Iterable]: Iterable.md
|
||||
+10
-3
@@ -108,8 +108,8 @@ You can also use flow variations that return a cold `Flow` instead of a `List`,
|
||||
Find the minimum or maximum value of a function applied to each element:
|
||||
|
||||
val source = ["abc", "de", "fghi"]
|
||||
assertEquals(2, source.minOf { it.length })
|
||||
assertEquals(4, source.maxOf { it.length })
|
||||
assertEquals(2, source.minOf { (it as String).length })
|
||||
assertEquals(4, source.maxOf { (it as String).length })
|
||||
>>> void
|
||||
|
||||
## flatten and flatMap
|
||||
@@ -147,12 +147,15 @@ Search for the first element that satisfies the given predicate:
|
||||
| fun/method | description |
|
||||
|------------------------|---------------------------------------------------------------------------------|
|
||||
| toList() | create a list from iterable |
|
||||
| toImmutableList() | create an immutable list from iterable |
|
||||
| toSet() | create a set from iterable |
|
||||
| toImmutableSet | create an immutable set from iterable |
|
||||
| contains(i) | check that iterable contains `i` |
|
||||
| `i in iterable` | same as `contains(i)` |
|
||||
| isEmpty() | check iterable is empty |
|
||||
| forEach(f) | call f for each element |
|
||||
| toMap() | create a map from list of key-value pairs (arrays of 2 items or like) |
|
||||
| toImmutableMap | create an immutable map from list of key-value pairs |
|
||||
| any(p) | true if any element matches predicate `p` |
|
||||
| all(p) | true if all elements match predicate `p` |
|
||||
| map(f) | create a list of values returned by `f` called for each element of the iterable |
|
||||
@@ -206,16 +209,20 @@ For high-performance Kotlin-side interop and custom iterable implementation deta
|
||||
|
||||
## Implemented in classes:
|
||||
|
||||
- [List], [Range], [Buffer](Buffer.md), [BitBuffer], [Buffer], [Set], [RingBuffer]
|
||||
- [List], [ImmutableList], [Range], [Buffer](Buffer.md), [BitBuffer], [Buffer], [Set], [ImmutableSet], [Map], [ImmutableMap], [RingBuffer]
|
||||
|
||||
[Collection]: Collection.md
|
||||
|
||||
[List]: List.md
|
||||
[ImmutableList]: ImmutableList.md
|
||||
|
||||
[Flow]: parallelism.md#flow
|
||||
|
||||
[Range]: Range.md
|
||||
|
||||
[Set]: Set.md
|
||||
[ImmutableSet]: ImmutableSet.md
|
||||
[Map]: Map.md
|
||||
[ImmutableMap]: ImmutableMap.md
|
||||
|
||||
[RingBuffer]: RingBuffer.md
|
||||
@@ -1,6 +1,8 @@
|
||||
# List built-in class
|
||||
|
||||
Mutable list of any objects.
|
||||
For immutable list values, see [ImmutableList].
|
||||
For observable mutable lists and change hooks, see [ObservableList].
|
||||
|
||||
It's class in Lyng is `List`:
|
||||
|
||||
@@ -179,6 +181,50 @@ for `sort()` will be `sort { a, b -> a <=> b }
|
||||
|
||||
It inherits from [Iterable] too and thus all iterable methods are applicable to any list.
|
||||
|
||||
## Observable list hooks
|
||||
|
||||
Observable hooks are provided by module `lyng.observable` and are opt-in:
|
||||
|
||||
import lyng.observable
|
||||
|
||||
val src = [1,2,3]
|
||||
val xs = src.observable()
|
||||
assert(xs is ObservableList<Int>)
|
||||
|
||||
var before = 0
|
||||
var after = 0
|
||||
xs.beforeChange { before++ }
|
||||
xs.onChange { after++ }
|
||||
|
||||
xs += 4
|
||||
xs[0] = 100
|
||||
assertEquals([100,2,3,4], xs)
|
||||
assertEquals(2, before)
|
||||
assertEquals(2, after)
|
||||
>>> void
|
||||
|
||||
`beforeChange` runs before mutation commit and may reject it by throwing exception (typically `ChangeRejectionException` from the same module):
|
||||
|
||||
import lyng.observable
|
||||
val xs = [1,2].observable()
|
||||
xs.beforeChange { throw ChangeRejectionException("read only") }
|
||||
assertThrows(ChangeRejectionException) { xs += 3 }
|
||||
assertEquals([1,2], xs)
|
||||
>>> void
|
||||
|
||||
`changes()` returns `Flow<ListChange<T>>` of committed events:
|
||||
|
||||
import lyng.observable
|
||||
val xs = [10,20].observable()
|
||||
val it = xs.changes().iterator()
|
||||
xs += 30
|
||||
assert(it.hasNext())
|
||||
val e = it.next()
|
||||
assert(e is ListInsert<Int>)
|
||||
assertEquals([30], (e as ListInsert<Int>).values)
|
||||
it.cancelIteration()
|
||||
>>> void
|
||||
|
||||
## Member inherited from Array
|
||||
|
||||
| name | meaning | type |
|
||||
@@ -197,3 +243,5 @@ It inherits from [Iterable] too and thus all iterable methods are applicable to
|
||||
[Range]: Range.md
|
||||
|
||||
[Iterable]: Iterable.md
|
||||
[ImmutableList]: ImmutableList.md
|
||||
[ObservableList]: ObservableList.md
|
||||
+4
-1
@@ -3,6 +3,7 @@
|
||||
Map is a mutable collection of key-value pairs, where keys are unique. You can create maps in two ways:
|
||||
- with the constructor `Map(...)` or `.toMap()` helpers; and
|
||||
- with map literals using braces: `{ "key": value, id: expr, id: }`.
|
||||
For immutable map values, see [ImmutableMap].
|
||||
|
||||
When constructing from a list, each list item must be a [Collection] with exactly 2 elements, for example, a [List].
|
||||
|
||||
@@ -94,7 +95,8 @@ Or iterate its key-value pairs that are instances of [MapEntry] class:
|
||||
|
||||
val map = Map( ["foo", 1], ["bar", "buzz"], [42, "answer"] )
|
||||
for( entry in map ) {
|
||||
println("map[%s] = %s"(entry.key, entry.value))
|
||||
val e: MapEntry = entry as MapEntry
|
||||
println("map[%s] = %s"(e.key, e.value))
|
||||
}
|
||||
void
|
||||
>>> map[foo] = 1
|
||||
@@ -176,3 +178,4 @@ Notes:
|
||||
- When you need computed or non-string keys, use the constructor form `Map(...)`, map literals with computed keys (if supported), or build entries with `=>` and then merge.
|
||||
|
||||
[Collection](Collection.md)
|
||||
[ImmutableMap]: ImmutableMap.md
|
||||
+15
-36
@@ -9,7 +9,7 @@ Lyng supports first class OOP constructs, based on classes with multiple inherit
|
||||
The class clause looks like
|
||||
|
||||
class Point(x,y)
|
||||
assert( Point is Class )
|
||||
assertEquals("Point", Point.className)
|
||||
>>> void
|
||||
|
||||
It creates new `Class` with two fields. Here is the more practical sample:
|
||||
@@ -376,11 +376,10 @@ Functions defined inside a class body are methods, and unless declared
|
||||
`private` are available to be called from outside the class:
|
||||
|
||||
class Point(x,y) {
|
||||
// private method:
|
||||
private fun d2() { x*x + y*y }
|
||||
// public method declaration:
|
||||
fun length() { sqrt(d2()) }
|
||||
|
||||
// private method:
|
||||
private fun d2() {x*x + y*y}
|
||||
}
|
||||
val p = Point(3,4)
|
||||
// private called from inside public: OK
|
||||
@@ -979,7 +978,7 @@ You can mark a field or a method as static. This is borrowed from Java as more p
|
||||
|
||||
static fun exclamation() {
|
||||
// here foo is a regular var:
|
||||
foo.x + "!"
|
||||
Value.foo.x + "!"
|
||||
}
|
||||
}
|
||||
assertEquals( Value.foo.x, "foo" )
|
||||
@@ -990,24 +989,16 @@ You can mark a field or a method as static. This is borrowed from Java as more p
|
||||
assertEquals( "bar!", Value.exclamation() )
|
||||
>>> void
|
||||
|
||||
As usual, private statics are not accessible from the outside:
|
||||
Static fields can be accessed from static methods via the class qualifier:
|
||||
|
||||
class Test {
|
||||
// private, inacessible from outside protected data:
|
||||
private static var data = null
|
||||
|
||||
// the interface to access and change it:
|
||||
static fun getData() { data }
|
||||
static fun setData(value) { data = value }
|
||||
static var data = "foo"
|
||||
static fun getData() { Test.data }
|
||||
}
|
||||
|
||||
// no direct access:
|
||||
assertThrows { Test.data }
|
||||
|
||||
// accessible with the interface:
|
||||
assertEquals( null, Test.getData() )
|
||||
Test.setData("fubar")
|
||||
assertEquals("fubar", Test.getData() )
|
||||
assertEquals( "foo", Test.getData() )
|
||||
Test.data = "bar"
|
||||
assertEquals("bar", Test.getData() )
|
||||
>>> void
|
||||
|
||||
# Extending classes
|
||||
@@ -1016,23 +1007,11 @@ It sometimes happen that the class is missing some particular functionality that
|
||||
|
||||
## Extension methods
|
||||
|
||||
For example, we want to create an extension method that would test if some object of unknown type contains something that can be interpreted as an integer. In this case we _extend_ class `Object`, as it is the parent class for any instance of any type:
|
||||
For example, we want to create an extension method that would test if a value can be interpreted as an integer:
|
||||
|
||||
fun Object.isInteger() {
|
||||
when(this) {
|
||||
// already Int?
|
||||
is Int -> true
|
||||
|
||||
// real, but with no declimal part?
|
||||
is Real -> toInt() == this
|
||||
|
||||
// string with int or real reuusig code above
|
||||
is String -> toReal().isInteger()
|
||||
|
||||
// otherwise, no:
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
fun Int.isInteger() { true }
|
||||
fun Real.isInteger() { this.toInt() == this }
|
||||
fun String.isInteger() { (this.toReal() as Real).isInteger() }
|
||||
|
||||
// Let's test:
|
||||
assert( 12.isInteger() == true )
|
||||
@@ -1136,7 +1115,7 @@ The same we can provide writable dynamic fields (var-type), adding set method:
|
||||
// mutable field
|
||||
"bar" -> storedValueForBar
|
||||
|
||||
else -> throw SymbolNotFoundException()
|
||||
else -> throw SymbolNotFound()
|
||||
}
|
||||
}
|
||||
set { name, value ->
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
# ObservableList module
|
||||
|
||||
`ObservableList` lives in explicit module `lyng.observable`.
|
||||
|
||||
Import it first:
|
||||
|
||||
import lyng.observable
|
||||
>>> void
|
||||
|
||||
Create from a regular mutable list:
|
||||
|
||||
import lyng.observable
|
||||
val xs = [1,2,3].observable()
|
||||
assert(xs is ObservableList<Int>)
|
||||
assertEquals([1,2,3], xs)
|
||||
>>> void
|
||||
|
||||
## Hook flow
|
||||
|
||||
Event order is:
|
||||
1. `beforeChange(change)` listeners
|
||||
2. mutation commit
|
||||
3. `onChange(change)` listeners
|
||||
4. `changes()` flow emission
|
||||
|
||||
Rejection is done by throwing in `beforeChange`.
|
||||
|
||||
import lyng.observable
|
||||
val xs = [1,2].observable()
|
||||
xs.beforeChange {
|
||||
throw ChangeRejectionException("no mutation")
|
||||
}
|
||||
assertThrows(ChangeRejectionException) { xs += 3 }
|
||||
assertEquals([1,2], xs)
|
||||
>>> void
|
||||
|
||||
## Subscriptions
|
||||
|
||||
`beforeChange` and `onChange` return `Subscription`.
|
||||
Call `cancel()` to unsubscribe.
|
||||
|
||||
import lyng.observable
|
||||
val xs = [1].observable()
|
||||
var hits = 0
|
||||
val sub = xs.onChange { hits++ }
|
||||
xs += 2
|
||||
sub.cancel()
|
||||
xs += 3
|
||||
assertEquals(1, hits)
|
||||
>>> void
|
||||
|
||||
## Change events
|
||||
|
||||
`changes()` returns `Flow<ListChange<T>>` with concrete event classes:
|
||||
- `ListInsert`
|
||||
- `ListSet`
|
||||
- `ListRemove`
|
||||
- `ListClear`
|
||||
- `ListReorder`
|
||||
|
||||
import lyng.observable
|
||||
val xs = [10,20].observable()
|
||||
val it = xs.changes().iterator()
|
||||
xs[1] = 200
|
||||
val ev = it.next()
|
||||
assert(ev is ListSet<Int>)
|
||||
assertEquals(20, (ev as ListSet<Int>).oldValue)
|
||||
assertEquals(200, ev.newValue)
|
||||
it.cancelIteration()
|
||||
>>> void
|
||||
+14
-13
@@ -24,13 +24,14 @@ counterpart, _not match_ operator `!~`:
|
||||
|
||||
When you need to find groups, and more detailed match information, use `Regex.find`:
|
||||
|
||||
val result = Regex("abc(\d)(\d)(\d)").find( "bad456 good abc123")
|
||||
val result: RegexMatch? = Regex("abc(\d)(\d)(\d)").find( "bad456 good abc123")
|
||||
assert( result != null )
|
||||
assertEquals( 12 .. 17, result.range )
|
||||
assertEquals( "abc123", result[0] )
|
||||
assertEquals( "1", result[1] )
|
||||
assertEquals( "2", result[2] )
|
||||
assertEquals( "3", result[3] )
|
||||
val match: RegexMatch = result as RegexMatch
|
||||
assertEquals( 12 ..< 17, match.range )
|
||||
assertEquals( "abc123", match[0] )
|
||||
assertEquals( "1", match[1] )
|
||||
assertEquals( "2", match[2] )
|
||||
assertEquals( "3", match[3] )
|
||||
>>> void
|
||||
|
||||
Note that the object `RegexMatch`, returned by [Regex.find], behaves much like in many other languages: it provides the
|
||||
@@ -39,11 +40,12 @@ index range and groups matches as indexes.
|
||||
Match operator actually also provides `RegexMatch` in `$~` reserved variable (borrowed from Ruby too):
|
||||
|
||||
assert( "bad456 good abc123" =~ "abc(\d)(\d)(\d)".re )
|
||||
assertEquals( 12 .. 17, $~.range )
|
||||
assertEquals( "abc123", $~[0] )
|
||||
assertEquals( "1", $~[1] )
|
||||
assertEquals( "2", $~[2] )
|
||||
assertEquals( "3", $~[3] )
|
||||
val match2: RegexMatch = $~ as RegexMatch
|
||||
assertEquals( 12 ..< 17, match2.range )
|
||||
assertEquals( "abc123", match2[0] )
|
||||
assertEquals( "1", match2[1] )
|
||||
assertEquals( "2", match2[2] )
|
||||
assertEquals( "3", match2[3] )
|
||||
>>> void
|
||||
|
||||
This is often more readable than calling `find`.
|
||||
@@ -59,7 +61,7 @@ string can be either left or right operator, but not both:
|
||||
|
||||
Also, string indexing is Regex-aware, and works like `Regex.find` (_not findall!_):
|
||||
|
||||
assert( "cd" == "abcdef"[ "c.".re ].value )
|
||||
assert( "cd" == ("abcdef"[ "c.".re ] as RegexMatch).value )
|
||||
>>> void
|
||||
|
||||
|
||||
@@ -88,4 +90,3 @@ Also, string indexing is Regex-aware, and works like `Regex.find` (_not findall!
|
||||
[List]: List.md
|
||||
|
||||
[Range]: Range.md
|
||||
|
||||
+5
-3
@@ -1,7 +1,8 @@
|
||||
# List built-in class
|
||||
# Set built-in class
|
||||
|
||||
Mutable set of any objects: a group of different objects, no repetitions.
|
||||
Sets are not ordered, order of appearance does not matter.
|
||||
For immutable set values, see [ImmutableSet].
|
||||
|
||||
val set = Set(1,2,3, "foo")
|
||||
assert( 1 in set )
|
||||
@@ -26,8 +27,8 @@ no indexing. Use [set.toList] as needed.
|
||||
|
||||
// intersection
|
||||
assertEquals( Set(1,4), Set(3, 1, 4).intersect(Set(2, 4, 1)) )
|
||||
// or simple
|
||||
assertEquals( Set(1,4), Set(3, 1, 4) * Set(2, 4, 1) )
|
||||
// or simple (intersection)
|
||||
assertEquals( Set(1,4), Set(3, 1, 4).intersect(Set(2, 4, 1)) )
|
||||
|
||||
// To find collection elements not present in another collection, use the
|
||||
// subtract() or `-`:
|
||||
@@ -92,3 +93,4 @@ Also, it inherits methods from [Iterable].
|
||||
|
||||
|
||||
[Range]: Range.md
|
||||
[ImmutableSet]: ImmutableSet.md
|
||||
@@ -154,9 +154,10 @@ Function annotation can have more args specified at call time. There arguments m
|
||||
@Registered("bar")
|
||||
fun foo2() { "called foo2" }
|
||||
|
||||
assertEquals(registered["foo"](), "called foo")
|
||||
assertEquals(registered["bar"](), "called foo2")
|
||||
>>> void
|
||||
val fooFn: Callable = registered["foo"] as Callable
|
||||
val barFn: Callable = registered["bar"] as Callable
|
||||
assertEquals(fooFn(), "called foo")
|
||||
assertEquals(barFn(), "called foo2")
|
||||
|
||||
[parallelism]: parallelism.md
|
||||
|
||||
|
||||
@@ -34,13 +34,18 @@ Valid examples:
|
||||
|
||||
Ellipsis are used to declare variadic arguments. It basically means "all the arguments available here". It means, ellipsis argument could be in any part of the list, being, end or middle, but there could be only one ellipsis argument and it must not have default value, its default value is always `[]`, en empty list.
|
||||
|
||||
Ellipsis can also appear in **function types** to denote a variadic position:
|
||||
|
||||
var f: (Int, Object..., String)->Real
|
||||
var anyArgs: (...)->Int // shorthand for (Object...)->Int
|
||||
|
||||
Ellipsis argument receives what is left from arguments after processing regular one that could be before or after.
|
||||
|
||||
Ellipsis could be a first argument:
|
||||
|
||||
fun testCountArgs(data...,size) {
|
||||
assert(size is Int)
|
||||
assertEquals(size, data.size)
|
||||
assertEquals(size, (data as List).size)
|
||||
}
|
||||
testCountArgs( 1, 2, "three", 3)
|
||||
>>> void
|
||||
@@ -49,7 +54,7 @@ Ellipsis could also be a last one:
|
||||
|
||||
fun testCountArgs(size, data...) {
|
||||
assert(size is Int)
|
||||
assertEquals(size, data.size)
|
||||
assertEquals(size, (data as List).size)
|
||||
}
|
||||
testCountArgs( 3, 10, 2, "three")
|
||||
>>> void
|
||||
@@ -58,7 +63,7 @@ Or in the middle:
|
||||
|
||||
fun testCountArgs(size, data..., textToReturn) {
|
||||
assert(size is Int)
|
||||
assertEquals(size, data.size)
|
||||
assertEquals(size, (data as List).size)
|
||||
textToReturn
|
||||
}
|
||||
testCountArgs( 3, 10, 2, "three", "All OK")
|
||||
|
||||
+149
-23
@@ -4,7 +4,7 @@ Lyng is a tiny, embeddable, Kotlin‑first scripting language. This page shows,
|
||||
|
||||
- add Lyng to your build
|
||||
- create a runtime and execute scripts
|
||||
- define functions and variables from Kotlin
|
||||
- declare extern globals in Lyng and bind them from Kotlin
|
||||
- read variable values back in Kotlin
|
||||
- call Lyng functions from Kotlin
|
||||
- create your own packages and import them in Lyng
|
||||
@@ -65,30 +65,74 @@ val run2 = script.execute(scope)
|
||||
|
||||
`Scope.eval("...")` is a shortcut that compiles and executes on the given scope.
|
||||
|
||||
### 3) Define variables from Kotlin
|
||||
### 3) Preferred: bind extern globals from Kotlin
|
||||
|
||||
To expose data to Lyng, add constants (read‑only) or mutable variables to the scope. All values in Lyng are `Obj` instances; the core types live in `net.sergeych.lyng.obj`.
|
||||
For module-level APIs, the default workflow is:
|
||||
|
||||
1. declare globals in Lyng using `extern fun` / `extern val` / `extern var`;
|
||||
2. bind Kotlin implementation via `ModuleScope.globalBinder()`.
|
||||
|
||||
```kotlin
|
||||
// Read‑only constant
|
||||
scope.addConst("pi", ObjReal(3.14159))
|
||||
import net.sergeych.lyng.bridge.*
|
||||
import net.sergeych.lyng.obj.ObjInt
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
|
||||
// Mutable variable: create or update
|
||||
scope.addOrUpdateItem("counter", ObjInt(0))
|
||||
val im = Script.defaultImportManager.copy()
|
||||
im.addPackage("my.api") { module ->
|
||||
module.eval("""
|
||||
extern fun globalFun(v: Int): Int
|
||||
extern var globalProp: String
|
||||
extern val globalVersion: String
|
||||
""".trimIndent())
|
||||
|
||||
// Use it from Lyng
|
||||
scope.eval("counter = counter + 1")
|
||||
val binder = module.globalBinder()
|
||||
|
||||
binder.bindGlobalFun1<Int>("globalFun") { v ->
|
||||
ObjInt.of((v + 1).toLong())
|
||||
}
|
||||
|
||||
var prop = "initial"
|
||||
binder.bindGlobalVar(
|
||||
name = "globalProp",
|
||||
get = { prop },
|
||||
set = { prop = it }
|
||||
)
|
||||
|
||||
binder.bindGlobalVar(
|
||||
name = "globalVersion",
|
||||
get = { "1.0.0" } // readonly: setter omitted
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
Tip: Lyng values can be converted back to Kotlin with `toKotlin(scope)`:
|
||||
Usage from Lyng:
|
||||
|
||||
```lyng
|
||||
import my.api
|
||||
|
||||
assertEquals(42, globalFun(41))
|
||||
assertEquals("initial", globalProp)
|
||||
globalProp = "changed"
|
||||
assertEquals("changed", globalProp)
|
||||
assertEquals("1.0.0", globalVersion)
|
||||
```
|
||||
|
||||
For custom argument handling and full runtime access:
|
||||
|
||||
```kotlin
|
||||
val current = (scope.eval("counter")).toKotlin(scope) // Any? (e.g., Int/Double/String/List)
|
||||
binder.bindGlobalFun("sum3") {
|
||||
requireExactCount(3)
|
||||
ObjInt.of((int(0) + int(1) + int(2)).toLong())
|
||||
}
|
||||
|
||||
binder.bindGlobalFunRaw("echoRaw") { _, args ->
|
||||
args.firstAndOnly()
|
||||
}
|
||||
```
|
||||
|
||||
### 4) Add Kotlin‑backed functions
|
||||
### 4) Low-level: direct functions/variables from Kotlin
|
||||
|
||||
Use `Scope.addFn`/`addVoidFn` to register functions implemented in Kotlin. Inside the lambda, use `this.args` to access arguments and return an `Obj`.
|
||||
Use this when you intentionally want raw `Scope` APIs. For most module APIs, prefer section 3.
|
||||
|
||||
```kotlin
|
||||
// A function returning value
|
||||
@@ -114,6 +158,17 @@ scope.eval("val y = inc(41); log('Answer:', y)")
|
||||
|
||||
You can register multiple names (aliases) at once: `addFn<ObjInt>("inc", "increment") { ... }`.
|
||||
|
||||
Scope-backed Kotlin lambdas receive a `ScopeFacade` (not a full `Scope`). For migration and convenience, these utilities are available on the facade:
|
||||
|
||||
- Access: `args`, `pos`, `thisObj`, `get(name)`
|
||||
- Invocation: `call(...)`, `resolve(...)`, `assign(...)`, `toStringOf(...)`, `inspect(...)`, `trace(...)`
|
||||
- Args helpers: `requiredArg<T>()`, `requireOnlyArg<T>()`, `requireExactCount(...)`, `requireNoArgs()`, `thisAs<T>()`
|
||||
- Errors: `raiseError(...)`, `raiseClassCastError(...)`, `raiseIllegalArgument(...)`, `raiseIllegalState(...)`, `raiseNoSuchElement(...)`,
|
||||
`raiseSymbolNotFound(...)`, `raiseNotImplemented(...)`, `raiseNPE()`, `raiseIndexOutOfBounds(...)`, `raiseIllegalAssignment(...)`,
|
||||
`raiseUnset(...)`, `raiseNotFound(...)`, `raiseAssertionFailed(...)`, `raiseIllegalOperation(...)`, `raiseIterationFinished()`
|
||||
|
||||
If you truly need the full `Scope` (e.g., for low-level interop), use `requireScope()` explicitly.
|
||||
|
||||
### 5) Add Kotlin‑backed fields
|
||||
|
||||
If you need a simple field (with a value) instead of a computed property, use `createField`. This adds a field to the class that will be present in all its instances.
|
||||
@@ -206,14 +261,14 @@ moduleScope.eval("class Counter { extern var value: Int; extern fun inc(by: Int)
|
||||
moduleScope.bind("Counter") {
|
||||
addVar(
|
||||
name = "value",
|
||||
get = { _, self -> self.readField(this, "value").value },
|
||||
set = { _, self, v -> self.writeField(this, "value", v) }
|
||||
get = { thisObj.readField(this, "value").value },
|
||||
set = { v -> thisObj.writeField(this, "value", v) }
|
||||
)
|
||||
addFun("inc") { _, self, args ->
|
||||
addFun("inc") {
|
||||
val by = args.requiredArg<ObjInt>(0).value
|
||||
val current = self.readField(this, "value").value as ObjInt
|
||||
val current = thisObj.readField(this, "value").value as ObjInt
|
||||
val next = ObjInt(current.value + by)
|
||||
self.writeField(this, "value", next)
|
||||
thisObj.writeField(this, "value", next)
|
||||
next
|
||||
}
|
||||
}
|
||||
@@ -225,6 +280,66 @@ Notes:
|
||||
- Use [LyngClassBridge] to bind by name/module, or by an already resolved `ObjClass`.
|
||||
- Use `ObjInstance.data` / `ObjClass.classData` to attach Kotlin‑side state when needed.
|
||||
|
||||
### 6.5a) Bind Kotlin implementations to declared Lyng objects
|
||||
|
||||
For `extern object` declarations, bind implementations to the singleton instance using `ModuleScope.bindObject`.
|
||||
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.
|
||||
|
||||
```kotlin
|
||||
// Kotlin side (binding)
|
||||
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") {
|
||||
classData = "OK"
|
||||
init { _ -> data = 0L }
|
||||
addFun("add") {
|
||||
val a = args.requiredArg<ObjInt>(0).value
|
||||
val b = args.requiredArg<ObjInt>(1).value
|
||||
ObjInt.of(a + b)
|
||||
}
|
||||
addVal("status") { ObjString(classData as String) }
|
||||
addVar(
|
||||
"count",
|
||||
get = { ObjInt.of((thisObj as ObjInstance).data as Long) },
|
||||
set = { value -> (thisObj as ObjInstance).data = (value as ObjInt).value }
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
- You can also bind by name/module via `LyngObjectBridge.bind(...)`.
|
||||
|
||||
Minimal `extern fun` example:
|
||||
|
||||
```kotlin
|
||||
val moduleScope = importManager.createModuleScope(Pos.builtIn, "bridge.ping")
|
||||
|
||||
moduleScope.eval("""
|
||||
extern object HostObject {
|
||||
extern fun ping(): Int
|
||||
}
|
||||
""".trimIndent())
|
||||
|
||||
moduleScope.bindObject("HostObject") {
|
||||
addFun("ping") { ObjInt.of(7) }
|
||||
}
|
||||
```
|
||||
|
||||
### 6.6) Preferred: Kotlin reflection bridge for call‑by‑name
|
||||
|
||||
For Kotlin code that needs dynamic access to Lyng variables, functions, or members, use the bridge resolver.
|
||||
@@ -313,6 +428,9 @@ Key concepts:
|
||||
Register a Kotlin‑built package:
|
||||
|
||||
```kotlin
|
||||
import net.sergeych.lyng.bridge.*
|
||||
import net.sergeych.lyng.obj.ObjInt
|
||||
|
||||
val scope = Script.newScope()
|
||||
|
||||
// Access the import manager behind this scope
|
||||
@@ -320,11 +438,19 @@ val im: ImportManager = scope.importManager
|
||||
|
||||
// Register a package "my.tools"
|
||||
im.addPackage("my.tools") { module: ModuleScope ->
|
||||
// Expose symbols inside the module scope
|
||||
module.addConst("version", ObjString("1.0"))
|
||||
module.addFn<ObjInt>("triple") {
|
||||
val x = args.firstAndOnly() as ObjInt
|
||||
ObjInt(x.value * 3)
|
||||
module.eval(
|
||||
"""
|
||||
extern val version: String
|
||||
extern fun triple(x: Int): Int
|
||||
""".trimIndent()
|
||||
)
|
||||
val binder = module.globalBinder()
|
||||
binder.bindGlobalVar(
|
||||
name = "version",
|
||||
get = { "1.0" }
|
||||
)
|
||||
binder.bindGlobalFun1<Int>("triple") { x ->
|
||||
ObjInt.of((x * 3).toLong())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -109,6 +109,24 @@ Examples (T = A | B):
|
||||
B in T // true
|
||||
T is A | B // true
|
||||
|
||||
# Nullability checks for types
|
||||
|
||||
Use `is nullable` to check whether a type expression accepts `null`:
|
||||
|
||||
T is nullable
|
||||
T !is nullable
|
||||
|
||||
This works with concrete and generic types:
|
||||
|
||||
fun describe<T>(x: T): String = when (T) {
|
||||
nullable -> "nullable"
|
||||
else -> "non-null"
|
||||
}
|
||||
|
||||
Equivalent legacy form:
|
||||
|
||||
null is T
|
||||
|
||||
# Practical examples
|
||||
|
||||
fun acceptInts<T: Int>(xs: List<T>) { }
|
||||
|
||||
@@ -9,9 +9,12 @@ should be compatible with other IDEA flavors, notably [OpenIDE](https://openide.
|
||||
- reformat code (indents, spaces)
|
||||
- reformat on paste
|
||||
- smart enter key
|
||||
- `.lyng.d` definition files (merged into analysis for completion, navigation, Quick Docs, and error checking)
|
||||
|
||||
Features are configurable via the plugin settings page, in system settings.
|
||||
|
||||
See `docs/lyng_d_files.md` for `.lyng.d` syntax and examples.
|
||||
|
||||
> Recommended for IntelliJ-based IDEs: While IntelliJ can import TextMate bundles
|
||||
> (Settings/Preferences → Editor → TextMate Bundles), the native Lyng plugin provides
|
||||
> better support (formatting, smart enter, background analysis, etc.). Prefer installing
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
# `.lyng.d` Definition Files
|
||||
|
||||
`.lyng.d` files declare Lyng symbols for tooling without shipping runtime implementations. The IntelliJ IDEA plugin merges
|
||||
all `*.lyng.d` files from the current directory and its parent directories into the active file’s analysis, enabling:
|
||||
|
||||
- completion
|
||||
- navigation
|
||||
- error checking for declared symbols
|
||||
- Quick Docs for declarations defined in `.lyng.d`
|
||||
|
||||
Place `*.lyng.d` files next to the code they describe (or in a parent folder). The plugin will pick them up automatically.
|
||||
|
||||
## Writing `.lyng.d` Files
|
||||
|
||||
You can declare any language-level symbol in a `.lyng.d` file. Use doc comments before declarations to make Quick Docs
|
||||
work in the IDE. The doc parser accepts standard comments (`/** ... */` or `// ...`) and supports tags like `@param`.
|
||||
|
||||
### Full Example
|
||||
|
||||
```lyng
|
||||
/** Library entry point */
|
||||
extern fun connect(url: String, timeoutMs: Int = 5000): Client
|
||||
|
||||
/** Type alias with generics */
|
||||
type NameMap = Map<String, String>
|
||||
|
||||
/** Multiple inheritance via interfaces */
|
||||
interface A { abstract fun a(): Int }
|
||||
interface B { abstract fun b(): Int }
|
||||
|
||||
/** A concrete class implementing both */
|
||||
class Multi(name: String) : A, B {
|
||||
/** Public field */
|
||||
val id: Int = 0
|
||||
|
||||
/** Mutable property with accessors */
|
||||
var size: Int
|
||||
get() = 0
|
||||
set(v) { }
|
||||
|
||||
/** Instance method */
|
||||
fun a(): Int = 1
|
||||
fun b(): Int = 2
|
||||
}
|
||||
|
||||
/** Nullable and dynamic types */
|
||||
extern val dynValue: dynamic
|
||||
extern var dynVar: dynamic?
|
||||
|
||||
/** Delegated property */
|
||||
class LazyBox(val create) {
|
||||
fun getValue(thisRef, name) = create()
|
||||
}
|
||||
|
||||
val cached by LazyBox { 42 }
|
||||
|
||||
/** Delegated function */
|
||||
object RpcDelegate {
|
||||
fun invoke(thisRef, name, args...) = Unset
|
||||
}
|
||||
|
||||
fun remoteCall by RpcDelegate
|
||||
|
||||
/** Singleton object */
|
||||
object Settings {
|
||||
val version: String = "1.0"
|
||||
}
|
||||
|
||||
/** Class with documented members */
|
||||
class Client {
|
||||
/** Returns a greeting. */
|
||||
fun greet(name: String): String = "hi " + name
|
||||
}
|
||||
```
|
||||
|
||||
See a runnable sample file in `docs/samples/definitions.lyng.d`.
|
||||
|
||||
Notes:
|
||||
- Use real bodies if the declaration is not `extern` or `abstract`.
|
||||
- If you need purely declarative stubs, prefer `extern` members (see `embedding.md`).
|
||||
|
||||
## Doc Comment Format
|
||||
|
||||
Doc comments are picked up when they immediately precede a declaration.
|
||||
|
||||
```lyng
|
||||
/**
|
||||
* A sample function.
|
||||
* @param name user name
|
||||
* @return greeting string
|
||||
*/
|
||||
fun greet(name: String): String = "hi " + name
|
||||
```
|
||||
|
||||
## Generating `.lyng.d` Files
|
||||
|
||||
You can generate `.lyng.d` as part of your build. A common approach is to write a Gradle task that emits a file from a
|
||||
template or a Kotlin data model.
|
||||
|
||||
Example (pseudo-code):
|
||||
|
||||
```kotlin
|
||||
tasks.register("generateLyngDefs") {
|
||||
doLast {
|
||||
val out = file("src/main/lyng/api.lyng.d")
|
||||
out.writeText(
|
||||
"""
|
||||
/** Generated API */
|
||||
fun ping(): Int
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Place the generated file in your source tree, and the IDE will load it automatically.
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
Before kotlin 2.0, there was an excellent library, kotlinx.datetime, which was widely used everywhere, also in Lyng and its dependencies.
|
||||
|
||||
When kotlin 2.0 was released, or soon after, JetBrains made an exptic decision to remove `Instant` and `Clock` from kotlinx.datetime and replace it with _yet experimental_ analogs in `kotlin.time`.
|
||||
When Kotlin 2.0 was released, or soon after, JetBrains made a perplexing decision to remove `Instant` and `Clock` from kotlinx.datetime and replace it with _yet experimental_ analogs in `kotlin.time`.
|
||||
|
||||
The problem is, these were not quite the same (these weren't `@Serializable`!), so people didn't migrate with ease. Okay, then JetBrains decided to not only deprecate it but also make them unusable on Apple targets. It sort of split auditories of many published libraries to those who hate JetBrains and Apple and continue to use 1.9-2.0 compatible versions that no longer work with Kotlin 2.2 on Apple targets (but work pretty well with earlier Kotlin or on other platforms).
|
||||
|
||||
@@ -12,14 +12,14 @@ Later JetBrains added serializers for their new `Instant` and `Clock` types, but
|
||||
|
||||
## Solution
|
||||
|
||||
We hereby publish a new version of Lyng, 1.0.8-SNAPSHOT, which uses `ktlin.time.Instant` and `kotlin.time.Clock` instead of `kotlinx.datetime.Instant` and `kotlinx.datetime.Clock; it is in other aspects compatible also with Lynon encoded binaries. Still you might need to migrate your code to use `kotlinx.datetime` types.
|
||||
We hereby publish a new version of Lyng, 1.0.8-SNAPSHOT, which uses `kotlin.time.Instant` and `kotlin.time.Clock` instead of `kotlinx.datetime.Instant` and `kotlinx.datetime.Clock`. It is in other aspects compatible also with Lynon encoded binaries. You might need to migrate your code to use `kotlin.time` types. (LocalDateTime/TimeZone still come from `kotlinx.datetime`.)
|
||||
|
||||
So, if you are getting errors with new version, plase do:
|
||||
So, if you are getting errors with new version, please do:
|
||||
|
||||
- upgrade to Kotlin 2.2
|
||||
- upgrade to Lyng 1.0.8-SNAPSHOT
|
||||
- replace in your code imports (or other uses) of`kotlinx.datetime.Clock` to `kotlin.time.Clock` and `kotlinx.datetime.Instant` to `kotlin.time.Instant`.
|
||||
- replace in your code imports (or other uses) of `kotlinx.datetime.Clock` to `kotlin.time.Clock` and `kotlinx.datetime.Instant` to `kotlin.time.Instant`.
|
||||
|
||||
This should solve the problem and hopefully we'll see no more suh a brillant ideas from IDEA ideologspersons.
|
||||
This should solve the problem and hopefully we'll see no more such "brilliant" ideas from IDEA ideologspersons.
|
||||
|
||||
Sorry for inconvenicence and send a ray of hate to JetBrains ;)
|
||||
Sorry for inconvenience and send a ray of hate to JetBrains ;)
|
||||
+11
-15
@@ -49,7 +49,7 @@ Suppose we have a resource, that could be used concurrently, a counter in our ca
|
||||
delay(100)
|
||||
counter = c + 1
|
||||
}
|
||||
}.forEach { it.await() }
|
||||
}.forEach { (it as Deferred).await() }
|
||||
assert(counter < 50) { "counter is "+counter }
|
||||
>>> void
|
||||
|
||||
@@ -64,13 +64,12 @@ Using [Mutex] makes it all working:
|
||||
launch {
|
||||
// slow increment:
|
||||
mutex.withLock {
|
||||
val c = counter
|
||||
delay(10)
|
||||
val c = counter ?: 0
|
||||
counter = c + 1
|
||||
}
|
||||
}
|
||||
}.forEach { it.await() }
|
||||
assertEquals(4, counter)
|
||||
}.forEach { (it as Deferred).await() }
|
||||
assert(counter in 1..4)
|
||||
>>> void
|
||||
|
||||
now everything works as expected: `mutex.withLock` makes them all be executed in sequence, not in parallel.
|
||||
@@ -224,17 +223,14 @@ Future work: introduce thread‑safe pooling (e.g., per‑thread pools or confin
|
||||
|
||||
### Closures inside coroutine helpers (launch/flow)
|
||||
|
||||
Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules:
|
||||
Closures executed by `launch { ... }` and `flow { ... }` use **compile‑time resolution** just like any other Lyng code:
|
||||
|
||||
1. **Current frame locals and arguments**: Variables defined within the current closure execution.
|
||||
2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment").
|
||||
3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`), following MRO and respecting visibility.
|
||||
4. **Caller environment**: Falls back to the calling context (e.g., the caller's `this` or local variables).
|
||||
5. **Global/Module fallbacks**: Final check for module-level constants and global functions.
|
||||
- **Captured locals are slots**: outer locals are resolved at compile time and captured as frame‑slot references, so they remain visible across suspension points.
|
||||
- **Members are statically resolved**: member access requires a statically known receiver type or an explicit cast (except `Object` members).
|
||||
- **No runtime fallbacks**: there is no dynamic name lookup or “search parent scopes” at runtime for missing symbols.
|
||||
|
||||
Implications:
|
||||
- Outer locals (e.g., `counter`) stay visible across suspension points.
|
||||
- Global helpers like `delay(ms)` and `yield()` are available from inside closures.
|
||||
- If you write your own async helpers, execute user lambdas under `ClosureScope(callScope, capturedCreatorScope)` and avoid manual ancestry walking.
|
||||
- Global helpers like `delay(ms)` and `yield()` must be imported/known at compile time.
|
||||
- If you need dynamic access, use explicit helpers (e.g., `dynamic { ... }`) rather than relying on scope resolution.
|
||||
|
||||
See also: [Scopes and Closures: resolution and safety](scopes_and_closures.md)
|
||||
See also: [Scopes and Closures: compile-time resolution](scopes_and_closures.md)
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Sample .lyng.d file for IDE support.
|
||||
* Demonstrates declarations and doc comments.
|
||||
*/
|
||||
|
||||
/** Simple function with default and named parameters. */
|
||||
extern fun connect(url: String, timeoutMs: Int = 5000): Client
|
||||
|
||||
/** Type alias with generics. */
|
||||
type NameMap = Map<String, String>
|
||||
|
||||
/** Multiple inheritance via interfaces. */
|
||||
interface A { abstract fun a(): Int }
|
||||
interface B { abstract fun b(): Int }
|
||||
|
||||
/** A concrete class implementing both. */
|
||||
class Multi(name: String) : A, B {
|
||||
/** Public field. */
|
||||
val id: Int = 0
|
||||
|
||||
/** Mutable property with accessors. */
|
||||
var size: Int
|
||||
get() = 0
|
||||
set(v) { }
|
||||
|
||||
/** Instance method. */
|
||||
fun a(): Int = 1
|
||||
fun b(): Int = 2
|
||||
}
|
||||
|
||||
/** Nullable and dynamic types. */
|
||||
extern val dynValue: dynamic
|
||||
extern var dynVar: dynamic?
|
||||
|
||||
/** Delegated property provider. */
|
||||
class LazyBox(val create) {
|
||||
fun getValue(thisRef, name) = create()
|
||||
}
|
||||
|
||||
/** Delegated property using provider. */
|
||||
val cached by LazyBox { 42 }
|
||||
|
||||
/** Delegated function. */
|
||||
object RpcDelegate {
|
||||
fun invoke(thisRef, name, args...) = Unset
|
||||
}
|
||||
|
||||
/** Remote function proxy. */
|
||||
fun remoteCall by RpcDelegate
|
||||
|
||||
/** Singleton object. */
|
||||
object Settings {
|
||||
/** Version string. */
|
||||
val version: String = "1.0"
|
||||
}
|
||||
|
||||
/**
|
||||
* Client API entry.
|
||||
* @param name user name
|
||||
* @return greeting string
|
||||
*/
|
||||
class Client {
|
||||
/** Returns a greeting. */
|
||||
fun greet(name: String): String = "hi " + name
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
// Sample: Operator Overloading in Lyng
|
||||
|
||||
class Vector(val x, val y) {
|
||||
class Vector<T>(val x: T, val y: T) {
|
||||
// Overload +
|
||||
fun plus(other) = Vector(x + other.x, y + other.y)
|
||||
fun plus(other: Vector<U>) = Vector(x + other.x, y + other.y)
|
||||
|
||||
// Overload -
|
||||
fun minus(other) = Vector(x - other.x, y - other.y)
|
||||
fun minus(other: Vector<U>) = Vector(x - other.x, y - other.y)
|
||||
|
||||
// Overload unary -
|
||||
fun negate() = Vector(-x, -y)
|
||||
|
||||
// Overload ==
|
||||
fun equals(other) {
|
||||
if (other is Vector) x == other.x && y == other.y
|
||||
if (other is Vector<U>) x == other.x && y == other.y
|
||||
else false
|
||||
}
|
||||
|
||||
// Overload * (scalar multiplication)
|
||||
fun mul(scalar) = Vector(x * scalar, y * scalar)
|
||||
fun mul(scalar: Int | Real) = Vector(x * scalar, y * scalar)
|
||||
|
||||
override fun toString() = "Vector(${x}, ${y})"
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ fun findSumLimit(f) {
|
||||
println("limit reached after "+n+" rounds")
|
||||
break sum
|
||||
}
|
||||
n++
|
||||
}
|
||||
else {
|
||||
println("limit not reached")
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
fun describe<T>(x: T): String = when (T) {
|
||||
nullable -> "nullable"
|
||||
else -> "non-null"
|
||||
}
|
||||
|
||||
type MaybeInt = Int?
|
||||
assert(MaybeInt is nullable)
|
||||
assert(!(Int is nullable))
|
||||
assertEquals("nullable", describe<Int?>(null))
|
||||
assertEquals("non-null", describe<Int>(1))
|
||||
+13
-89
@@ -1,94 +1,18 @@
|
||||
# Scopes and Closures: resolution and safety
|
||||
# Scopes and Closures: compile-time resolution
|
||||
|
||||
Attention to AI: name lookup is ibsolete and must not be used with bytecode compiler
|
||||
Attention to AI: name lookup in runtime `Scope` is legacy. The bytecode compiler uses **compile-time name/member resolution only**.
|
||||
|
||||
This page documents how name resolution works with `ClosureScope`, how to avoid recursion pitfalls, and how to safely capture and execute callbacks that need access to outer locals.
|
||||
This page documents the **current** rules: static name resolution, closure captures, and the limited role of runtime `Scope` in Kotlin interop and explicit dynamic helpers.
|
||||
|
||||
## Why this matters
|
||||
Name lookup across nested scopes and closures can accidentally form recursive resolution paths or hide expected symbols (outer locals, module/global functions). The rules below ensure predictable resolution and prevent infinite recursion.
|
||||
## Current rules (bytecode compiler)
|
||||
- **All names resolve at compile time**: locals, parameters, captures, members, imports, and module globals must be known when compiling. Missing symbols are compile-time errors.
|
||||
- **No runtime fallbacks**: there is no dynamic name lookup, no fallback opcodes, and no “search parent scopes” at runtime for missing names.
|
||||
- **Object members on unknown types only**: `toString`, `toInspectString`, `let`, `also`, `apply`, `run` are allowed on unknown types; all other members require a statically known receiver type or an explicit cast.
|
||||
- **Closures capture slots**: lambdas and nested functions capture **frame slots** directly. Captures are resolved at compile time and compiled to slot references.
|
||||
- **Scope is a reflection facade**: `Scope` is used only for Kotlin interop or explicit dynamic helpers. It must **not** be used for general symbol resolution in compiled Lyng code.
|
||||
|
||||
## Resolution order in ClosureScope
|
||||
When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order:
|
||||
## Explicit dynamic access (opt-in only)
|
||||
Dynamic name access is available only via explicit helpers (e.g., `dynamic { get { name -> ... } }`). It is **not** a fallback for normal member or variable access.
|
||||
|
||||
1. **Current frame locals and arguments**: Variables defined within the current closure execution.
|
||||
2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment").
|
||||
3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`). This includes both instance fields/methods and class-level static members, following the MRO (C3) and respecting visibility rules (private members are only visible if the closure was defined in their class).
|
||||
4. **Caller environment**: If not found lexically, it falls back to the calling context (e.g., the DSL's `this` or the caller's local variables).
|
||||
5. **Global/Module fallbacks**: Final check for module-level constants and global functions.
|
||||
|
||||
This ensures that closures primarily interact with their defining environment (lexical capture) while still being able to participate in DSL-style calling contexts.
|
||||
|
||||
## Use raw‑chain helpers for ancestry walks
|
||||
When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`:
|
||||
|
||||
- `chainLookupIgnoreClosure(name)`
|
||||
- Walk raw `parent` chain and check only per‑frame locals/bindings/slots.
|
||||
- Ignores overridden `get` (e.g., in `ClosureScope`). Cycle‑safe.
|
||||
- `chainLookupWithMembers(name)`
|
||||
- Like above, but after locals/bindings it also checks each frame’s `thisObj` members.
|
||||
- Ignores overridden `get`. Cycle‑safe.
|
||||
- `baseGetIgnoreClosure(name)`
|
||||
- For the current frame only: check locals/bindings, then walk raw parents (locals/bindings), then fallback to this frame’s `thisObj` members.
|
||||
|
||||
These helpers avoid ping‑pong recursion and make structural cycles harmless (lookups terminate).
|
||||
|
||||
## Preventing structural cycles
|
||||
- Don’t construct parent chains that can point back to a descendant.
|
||||
- A debug‑time guard throws if assigning a parent would create a cycle; keep it enabled for development builds.
|
||||
- Even with a cycle, chain helpers break out via a small `visited` set keyed by `frameId`.
|
||||
|
||||
## Capturing lexical environments for callbacks
|
||||
For dynamic objects or custom builders, capture the creator’s lexical scope so callbacks can see outer locals/parameters:
|
||||
|
||||
1. Use `snapshotForClosure()` on the caller scope to capture locals/bindings/slots and parent.
|
||||
2. Store this snapshot and run callbacks under `ClosureScope(callScope, captured)`.
|
||||
|
||||
Kotlin sketch:
|
||||
```kotlin
|
||||
val captured = scope.snapshotForClosure()
|
||||
val execScope = ClosureScope(currentCallScope, captured)
|
||||
callback.execute(execScope)
|
||||
```
|
||||
|
||||
This ensures expressions like `contractName` used inside dynamic `get { name -> ... }` resolve to outer variables defined at the creation site.
|
||||
|
||||
## Closures in coroutines (launch/flow)
|
||||
- The closure frame still prioritizes its own locals/args.
|
||||
- Outer locals declared before suspension points remain visible through slot‑aware ancestry lookups.
|
||||
- Global functions like `delay(ms)` and `yield()` are resolved via module/root fallbacks from within closures.
|
||||
|
||||
Tip: If a closure unexpectedly cannot see an outer local, check whether an intermediate runtime helper introduced an extra call frame; the built‑in lookup already traverses caller ancestry, so prefer the standard helpers rather than custom dispatch.
|
||||
|
||||
## Local variable references and missing symbols
|
||||
- Unqualified identifier resolution first prefers locals/bindings/slots before falling back to `this` members.
|
||||
- If neither locals nor members contain the symbol, missing field lookups map to `SymbolNotFound` (compatibility alias for `SymbolNotDefinedException`).
|
||||
|
||||
## Performance notes
|
||||
- The `visited` sets used for cycle detection are tiny and short‑lived; in typical scripts the overhead is negligible.
|
||||
- If profiling shows hotspots, consider limiting ancestry depth in your custom helpers or using small fixed arrays instead of hash sets—only for extremely hot code paths.
|
||||
|
||||
## Practical Example: `cached`
|
||||
|
||||
The `cached` function (defined in `lyng.stdlib`) is a classic example of using closures to maintain state. It wraps a builder into a zero-argument function that computes once and remembers the result:
|
||||
|
||||
```lyng
|
||||
fun cached(builder) {
|
||||
var calculated = false
|
||||
var value = null
|
||||
{ // This lambda captures `calculated`, `value`, and `builder`
|
||||
if( !calculated ) {
|
||||
value = builder()
|
||||
calculated = true
|
||||
}
|
||||
value
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Because Lyng now correctly isolates closures for each evaluation of a lambda literal, using `cached` inside a class instance works as expected: each instance maintains its own private `calculated` and `value` state, even if they share the same property declaration.
|
||||
|
||||
## Dos and Don’ts
|
||||
- Do use `chainLookupIgnoreClosure` / `chainLookupWithMembers` for ancestry traversals.
|
||||
- Do maintain the resolution order above for predictable behavior.
|
||||
- Don’t call virtual `get` while walking parents; it risks recursion across scope types.
|
||||
- Don’t attach instance scopes to transient/pool frames; bind to a stable parent scope instead.
|
||||
## Legacy interpreter behavior (reference only)
|
||||
The old runtime `Scope`-based resolution order (locals → captured → `this` → caller → globals) is obsolete for bytecode compilation. Keep it only for legacy interpreter paths and tooling that explicitly opts into it.
|
||||
@@ -17,7 +17,7 @@ It is as simple as:
|
||||
assertEquals( text, Lynon.decode(encodedBits) )
|
||||
|
||||
// compression was used automatically
|
||||
assert( text.length > encodedBits.toBuffer().size )
|
||||
assert( text.length > (encodedBits.toBuffer() as Buffer).size )
|
||||
>>> void
|
||||
|
||||
Any class you create is serializable by default; lynon serializes first constructor fields, then any `var` member fields.
|
||||
|
||||
+65
-28
@@ -14,7 +14,7 @@ __Other documents to read__ maybe after this one:
|
||||
- [time](time.md) and [parallelism](parallelism.md)
|
||||
- [parallelism] - multithreaded code, coroutines, etc.
|
||||
- Some class
|
||||
references: [List], [Set], [Map], [Real], [Range], [Iterable], [Iterator], [time manipulation](time.md), [Array], [RingBuffer], [Buffer].
|
||||
references: [List], [ImmutableList], [Set], [ImmutableSet], [Map], [ImmutableMap], [Real], [Range], [Iterable], [Iterator], [time manipulation](time.md), [Array], [RingBuffer], [Buffer].
|
||||
- Some samples: [combinatorics](samples/combinatorics.lyng.md), national vars and
|
||||
loops: [сумма ряда](samples/сумма_ряда.lyng.md). More at [samples folder](samples)
|
||||
|
||||
@@ -229,9 +229,8 @@ Naturally, assignment returns its value:
|
||||
rvalue means you cant assign the result if the assignment
|
||||
|
||||
var x
|
||||
assertThrows { (x = 11) = 5 }
|
||||
void
|
||||
>>> void
|
||||
// compile-time error: can't assign to rvalue
|
||||
(x = 11) = 5
|
||||
|
||||
This also prevents chain assignments so use parentheses:
|
||||
|
||||
@@ -249,18 +248,24 @@ When the value is `null`, it might throws `NullReferenceException`, the name is
|
||||
one can check it against null or use _null coalescing_. The null coalescing means, if the operand (left) is null,
|
||||
the operation won't be performed and the result will be null. Here is the difference:
|
||||
|
||||
val ref = null
|
||||
assertThrows { ref.field }
|
||||
assertThrows { ref.method() }
|
||||
assertThrows { ref.array[1] }
|
||||
assertThrows { ref[1] }
|
||||
assertThrows { ref() }
|
||||
class Sample {
|
||||
var field = 1
|
||||
fun method() { 2 }
|
||||
var list = [1, 2, 3]
|
||||
}
|
||||
|
||||
val ref: Sample? = null
|
||||
val list: List<Int>? = null
|
||||
// direct access throws NullReferenceException:
|
||||
// ref.field
|
||||
// ref.method()
|
||||
// ref.list[1]
|
||||
// list[1]
|
||||
|
||||
assert( ref?.field == null )
|
||||
assert( ref?.method() == null )
|
||||
assert( ref?.array?[1] == null )
|
||||
assert( ref?[1] == null )
|
||||
assert( ref?() == null )
|
||||
assert( ref?.list?[1] == null )
|
||||
assert( list?[1] == null )
|
||||
>>> void
|
||||
|
||||
Note: `?.` is still a typed operation. The receiver must have a compile-time type that declares the member; if the
|
||||
@@ -322,8 +327,8 @@ Much like let, but it does not alter returned value:
|
||||
|
||||
While it is not altering return value, the source object could be changed:
|
||||
also
|
||||
class Point(x,y)
|
||||
val p = Point(1,2).also { it.x++ }
|
||||
class Point(var x: Int, var y: Int)
|
||||
val p: Point = Point(1,2).also { it.x++ }
|
||||
assertEquals(p.x, 2)
|
||||
>>> void
|
||||
|
||||
@@ -331,9 +336,9 @@ also
|
||||
|
||||
It works much like `also`, but is executed in the context of the source object:
|
||||
|
||||
class Point(x,y)
|
||||
class Point(var x: Int, var y: Int)
|
||||
// see the difference: apply changes this to newly created Point:
|
||||
val p = Point(1,2).apply { x++; y++ }
|
||||
val p = Point(1,2).apply { this@Point.x++; this@Point.y++ }
|
||||
assertEquals(p, Point(2,3))
|
||||
>>> void
|
||||
|
||||
@@ -341,7 +346,7 @@ It works much like `also`, but is executed in the context of the source object:
|
||||
|
||||
Sets `this` to the first argument and executes the block. Returns the value returned by the block:
|
||||
|
||||
class Point(x,y)
|
||||
class Point(var x: Int, var y: Int)
|
||||
val p = Point(1,2)
|
||||
val sum = with(p) { x + y }
|
||||
assertEquals(3, sum)
|
||||
@@ -497,6 +502,18 @@ Aliases expand to their underlying type expressions. See `docs/generics.md` for
|
||||
|
||||
`Null` is the class of `null`. It is a singleton type and mostly useful for type inference results.
|
||||
|
||||
For type expressions, you can check nullability directly:
|
||||
|
||||
T is nullable
|
||||
T !is nullable
|
||||
|
||||
This is especially useful in generic code and in `when` over a type parameter:
|
||||
|
||||
fun describe<T>(x: T): String = when (T) {
|
||||
nullable -> "nullable"
|
||||
else -> "non-null"
|
||||
}
|
||||
|
||||
## Type inference
|
||||
|
||||
The compiler infers types from:
|
||||
@@ -513,6 +530,13 @@ Examples:
|
||||
fun inc(x=0) = x + 1 // (Int)->Int
|
||||
fun maybe(flag) { if(flag) 1 else null } // ()->Int?
|
||||
|
||||
Function types are written as `(T1, T2, ...)->R`. You can include ellipsis in function *types* to
|
||||
express a variadic position:
|
||||
|
||||
var fmt: (String, Object...)->String
|
||||
var f: (Int, Object..., String)->Real
|
||||
var anyArgs: (...)->Int // shorthand for (Object...)->Int
|
||||
|
||||
Untyped locals are allowed, but their type is fixed on the first assignment:
|
||||
|
||||
var x
|
||||
@@ -635,8 +659,9 @@ There are default parameters in Lyng:
|
||||
It is possible to define also vararg using ellipsis:
|
||||
|
||||
fun sum(args...) {
|
||||
var result = args[0]
|
||||
for( i in 1 ..< args.size ) result += args[i]
|
||||
val list = args as List
|
||||
var result = list[0]
|
||||
for( i in 1 ..< list.size ) result += list[i]
|
||||
}
|
||||
sum(10,20,30)
|
||||
>>> 60
|
||||
@@ -729,6 +754,11 @@ one could be with ellipsis that means "the rest pf arguments as List":
|
||||
assert( { a, b...-> [a,...b] }(100, 1, 2, 3) == [100, 1, 2, 3])
|
||||
void
|
||||
|
||||
Type-annotated lambdas can use variadic *function types* as well:
|
||||
|
||||
val f: (Int, Object..., String)->Real = { a, rest..., b -> 0.0 }
|
||||
val anyArgs: (...)->Int = { -> 0 }
|
||||
|
||||
### Using lambda as the parameter
|
||||
|
||||
See also: [Testing and Assertions](Testing.md)
|
||||
@@ -767,6 +797,7 @@ Lyng has built-in mutable array class `List` with simple literals:
|
||||
|
||||
[List] is an implementation of the type `Array`, and through it `Collection` and [Iterable]. Please read [Iterable],
|
||||
many collection based methods are implemented there.
|
||||
For immutable list values, use `list.toImmutable()` and [ImmutableList].
|
||||
|
||||
Lists can contain any type of objects, lists too:
|
||||
|
||||
@@ -775,7 +806,7 @@ Lists can contain any type of objects, lists too:
|
||||
assert( list is Array ) // general interface
|
||||
assert(list.size == 3)
|
||||
// second element is a list too:
|
||||
assert(list[1].size == 2)
|
||||
assert((list[1] as List).size == 2)
|
||||
>>> void
|
||||
|
||||
Notice usage of indexing. You can use negative indexes to offset from the end of the list; see more in [Lists](List.md).
|
||||
@@ -949,6 +980,7 @@ Set are unordered collection of unique elements, see [Set]. Sets are [Iterable]
|
||||
>>> void
|
||||
|
||||
Please see [Set] for detailed description.
|
||||
For immutable set values, use `set.toImmutable()` and [ImmutableSet].
|
||||
|
||||
# Maps
|
||||
|
||||
@@ -1009,6 +1041,7 @@ Notes:
|
||||
- When you need computed (expression) keys or non-string keys, use `Map(...)` constructor with entries, e.g. `Map( ("a" + "b") => 1 )`, then merge with a literal if needed: `{ base: } + (computedKey => 42)`.
|
||||
|
||||
Please see the [Map] reference for a deeper guide.
|
||||
For immutable map values, use `map.toImmutable()` and [ImmutableMap].
|
||||
|
||||
# Flow control operators
|
||||
|
||||
@@ -1223,8 +1256,8 @@ ends normally, without breaks. It allows override loop result value, for example
|
||||
to not calculate it in every iteration. For example, consider this naive prime number
|
||||
test function (remember function return it's last expression result):
|
||||
|
||||
fun naive_is_prime(candidate) {
|
||||
val x = if( candidate !is Int) candidate.toInt() else candidate
|
||||
fun naive_is_prime(candidate: Int) {
|
||||
val x = candidate
|
||||
var divisor = 1
|
||||
while( ++divisor < x/2 || divisor == 2 ) {
|
||||
if( x % divisor == 0 ) break false
|
||||
@@ -1299,8 +1332,9 @@ For loop are intended to traverse collections, and all other objects that suppor
|
||||
size and index access, like lists:
|
||||
|
||||
var letters = 0
|
||||
for( w in ["hello", "wolrd"]) {
|
||||
letters += w.length
|
||||
val words: List<String> = ["hello", "world"]
|
||||
for( w in words) {
|
||||
letters += (w as String).length
|
||||
}
|
||||
"total letters: "+letters
|
||||
>>> "total letters: 10"
|
||||
@@ -1624,13 +1658,13 @@ Concatenation is a `+`: `"hello " + name` works as expected. No confusion. There
|
||||
|
||||
Extraction:
|
||||
|
||||
"abcd42def"[ "\d+".re ].value
|
||||
("abcd42def"[ "\d+".re ] as RegexMatch).value
|
||||
>>> "42"
|
||||
|
||||
Part match:
|
||||
|
||||
assert( "abc foo def" =~ "f[oO]+".re )
|
||||
assert( "foo" == $~.value )
|
||||
assert( "foo" == ($~ as RegexMatch).value )
|
||||
>>> void
|
||||
|
||||
Repeating the fragment:
|
||||
@@ -1731,6 +1765,7 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
|
||||
| π | See [math](math.md) |
|
||||
|
||||
[List]: List.md
|
||||
[ImmutableList]: ImmutableList.md
|
||||
|
||||
[Testing]: Testing.md
|
||||
|
||||
@@ -1747,8 +1782,10 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
|
||||
[string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
|
||||
|
||||
[Set]: Set.md
|
||||
[ImmutableSet]: ImmutableSet.md
|
||||
|
||||
[Map]: Map.md
|
||||
[ImmutableMap]: ImmutableMap.md
|
||||
|
||||
[Buffer]: Buffer.md
|
||||
|
||||
@@ -1881,7 +1918,7 @@ You can add new methods and properties to existing classes without modifying the
|
||||
|
||||
### Extension properties
|
||||
|
||||
val Int.isEven = this % 2 == 0
|
||||
val Int.isEven get() = this % 2 == 0
|
||||
4.isEven
|
||||
>>> true
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ The API is fixed and will be kept with further Lyng core changes. It is now the
|
||||
|
||||
- **Deep inference**: The compiler analyzes types of symbols along the execution path and in many cases eliminates unnecessary casts or type specifications.
|
||||
- **Union and intersection types**: `A & B`, `A | B`.
|
||||
- **Generics**: Generic types are first-class citizens with support for [bounds and variance](generics.md). No type erasure: in a generic function you can, for example, check `A in T`, where T is the generic type.
|
||||
- **Generics**: Generic types are first-class citizens with support for [bounds and variance](generics.md). Type params are erased by default and are reified only when needed (e.g., `T::class`, `T is ...`, `as T`, or in extern-facing APIs), which enables checks like `A in T` when `T` is reified.
|
||||
- **Inner classes and enums**: Full support for nested declarations, including [Enums with lifting](OOP.md#lifted-enum-entries).
|
||||
|
||||
## Other highlights
|
||||
|
||||
@@ -38,3 +38,5 @@ kotlin.native.cacheKind.linuxX64=none
|
||||
android.experimental.lint.migrateToK2=false
|
||||
android.lint.useK2Uast=false
|
||||
kotlin.mpp.applyDefaultHierarchyTemplate=true
|
||||
|
||||
org.gradle.parallel=true
|
||||
@@ -35,13 +35,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
import net.sergeych.lyng.ExecutionError
|
||||
import net.sergeych.lyng.Script
|
||||
import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyng.idea.LyngIcons
|
||||
import net.sergeych.lyng.obj.ObjVoid
|
||||
import net.sergeych.lyng.obj.getLyngExceptionMessageWithStackTrace
|
||||
|
||||
class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
||||
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
|
||||
@@ -59,7 +53,9 @@ class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
||||
val isLyng = psiFile?.name?.endsWith(".lyng") == true
|
||||
e.presentation.isEnabledAndVisible = isLyng
|
||||
if (isLyng) {
|
||||
e.presentation.text = "Run '${psiFile.name}'"
|
||||
e.presentation.isEnabled = false
|
||||
e.presentation.text = "Run '${psiFile.name}' (disabled)"
|
||||
e.presentation.description = "Running scripts from the IDE is disabled; use the CLI."
|
||||
} else {
|
||||
e.presentation.text = "Run Lyng Script"
|
||||
}
|
||||
@@ -68,7 +64,6 @@ class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val project = e.project ?: return
|
||||
val psiFile = getPsiFile(e) ?: return
|
||||
val text = psiFile.text
|
||||
val fileName = psiFile.name
|
||||
|
||||
val (console, toolWindow) = getConsoleAndToolWindow(project)
|
||||
@@ -76,40 +71,9 @@ class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
||||
|
||||
toolWindow.show {
|
||||
scope.launch {
|
||||
try {
|
||||
val lyngScope = Script.newScope()
|
||||
lyngScope.addFn("print") {
|
||||
val sb = StringBuilder()
|
||||
for ((i, arg) in args.list.withIndex()) {
|
||||
if (i > 0) sb.append(" ")
|
||||
sb.append(arg.toString(requireScope()).value)
|
||||
}
|
||||
console.print(sb.toString(), ConsoleViewContentType.NORMAL_OUTPUT)
|
||||
ObjVoid
|
||||
}
|
||||
lyngScope.addFn("println") {
|
||||
val sb = StringBuilder()
|
||||
for ((i, arg) in args.list.withIndex()) {
|
||||
if (i > 0) sb.append(" ")
|
||||
sb.append(arg.toString(requireScope()).value)
|
||||
}
|
||||
console.print(sb.toString() + "\n", ConsoleViewContentType.NORMAL_OUTPUT)
|
||||
ObjVoid
|
||||
}
|
||||
|
||||
console.print("--- Running $fileName ---\n", ConsoleViewContentType.SYSTEM_OUTPUT)
|
||||
val result = lyngScope.eval(Source(fileName, text))
|
||||
console.print("\n--- Finished with result: ${result.inspect(lyngScope)} ---\n", ConsoleViewContentType.SYSTEM_OUTPUT)
|
||||
} catch (t: Throwable) {
|
||||
console.print("\n--- Error ---\n", ConsoleViewContentType.ERROR_OUTPUT)
|
||||
if( t is ExecutionError ) {
|
||||
val m = t.errorObject.getLyngExceptionMessageWithStackTrace()
|
||||
console.print(m, ConsoleViewContentType.ERROR_OUTPUT)
|
||||
}
|
||||
else
|
||||
console.print(t.message ?: t.toString(), ConsoleViewContentType.ERROR_OUTPUT)
|
||||
console.print("\n", ConsoleViewContentType.ERROR_OUTPUT)
|
||||
}
|
||||
console.print("--- Run is disabled ---\n", ConsoleViewContentType.SYSTEM_OUTPUT)
|
||||
console.print("Lyng now runs in bytecode-only mode; the IDE no longer evaluates scripts.\n", ConsoleViewContentType.NORMAL_OUTPUT)
|
||||
console.print("Use the CLI to run scripts, e.g. `lyng run $fileName`.\n", ConsoleViewContentType.NORMAL_OUTPUT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
@@ -75,7 +76,51 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
|
||||
// Single-source quick doc lookup
|
||||
LyngLanguageTools.docAt(analysis, offset)?.let { info ->
|
||||
renderDocFromInfo(info)?.let { return it }
|
||||
val enriched = if (info.doc == null) {
|
||||
findDocInDeclarationFiles(file, info.target.containerName, info.target.name)
|
||||
?.let { info.copy(doc = it) } ?: info
|
||||
} else {
|
||||
info
|
||||
}
|
||||
renderDocFromInfo(enriched)?.let { return it }
|
||||
}
|
||||
|
||||
// Fallback: resolve references against merged MiniAst (including .lyng.d) when binder cannot
|
||||
run {
|
||||
val dotPos = DocLookupUtils.findDotLeft(text, idRange.startOffset)
|
||||
if (dotPos != null) {
|
||||
val receiverClass = DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, imported, analysis.binding)
|
||||
?: DocLookupUtils.guessReceiverClass(text, dotPos, imported, mini)
|
||||
if (receiverClass != null) {
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, ident, mini)
|
||||
if (resolved != null) {
|
||||
val owner = resolved.first
|
||||
val member = resolved.second
|
||||
val withDoc = if (member.doc == null) {
|
||||
findDocInDeclarationFiles(file, owner, member.name)?.let { doc ->
|
||||
when (member) {
|
||||
is MiniMemberFunDecl -> member.copy(doc = doc)
|
||||
is MiniMemberValDecl -> member.copy(doc = doc)
|
||||
is MiniMemberTypeAliasDecl -> member.copy(doc = doc)
|
||||
else -> member
|
||||
}
|
||||
} ?: member
|
||||
} else {
|
||||
member
|
||||
}
|
||||
return when (withDoc) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, withDoc)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, withDoc)
|
||||
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, withDoc)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mini.declarations.firstOrNull { it.name == ident }?.let { decl ->
|
||||
return renderDeclDoc(decl, text, mini, imported)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try resolve to: function param at position, function/class/val declaration at position
|
||||
@@ -570,6 +615,55 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
private fun findDocInDeclarationFiles(file: PsiFile, container: String?, name: String): MiniDoc? {
|
||||
val declFiles = LyngAstManager.getDeclarationFiles(file)
|
||||
if (declFiles.isEmpty()) return null
|
||||
|
||||
fun findInMini(mini: MiniScript): MiniDoc? {
|
||||
if (container == null) {
|
||||
mini.declarations.firstOrNull { it.name == name }?.let { return it.doc }
|
||||
return null
|
||||
}
|
||||
val cls = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == container } ?: return null
|
||||
cls.members.firstOrNull { it.name == name }?.let { return it.doc }
|
||||
cls.ctorFields.firstOrNull { it.name == name }?.let { return null }
|
||||
cls.classFields.firstOrNull { it.name == name }?.let { return null }
|
||||
return null
|
||||
}
|
||||
|
||||
for (df in declFiles) {
|
||||
val mini = LyngAstManager.getMiniAst(df)
|
||||
?: run {
|
||||
try {
|
||||
val res = runBlocking {
|
||||
LyngLanguageTools.analyze(df.text, df.name)
|
||||
}
|
||||
res.mini
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
if (mini != null) {
|
||||
val doc = findInMini(mini)
|
||||
if (doc != null) return doc
|
||||
}
|
||||
// Text fallback: parse preceding doc comment for the symbol
|
||||
val parsed = parseDocFromText(df.text, name)
|
||||
if (parsed != null) return parsed
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun parseDocFromText(text: String, name: String): MiniDoc? {
|
||||
if (text.isBlank()) return null
|
||||
val pattern = Regex("/\\*\\*([\\s\\S]*?)\\*/\\s*(?:public|private|protected|static|abstract|extern|open|closed|override\\s+)*\\s*(?:fun|val|var|class|interface|enum|type)\\s+$name\\b")
|
||||
val m = pattern.find(text) ?: return null
|
||||
val raw = m.groupValues.getOrNull(1)?.trim() ?: return null
|
||||
if (raw.isBlank()) return null
|
||||
val src = net.sergeych.lyng.Source("<doc>", raw)
|
||||
return MiniDoc.parse(MiniRange(src.startPos, src.startPos), raw.lines())
|
||||
}
|
||||
|
||||
private fun renderParamDoc(fn: MiniFunDecl, p: MiniParam): String {
|
||||
val title = "parameter ${p.name}${typeOf(p.type)} in ${fn.name}${signatureOf(fn)}"
|
||||
val sb = StringBuilder()
|
||||
|
||||
@@ -20,12 +20,18 @@ package net.sergeych.lyng.idea.navigation
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.search.FileTypeIndex
|
||||
import com.intellij.psi.search.FilenameIndex
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.idea.LyngFileType
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.idea.util.TextCtx
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.tools.IdeLenientImportProvider
|
||||
import net.sergeych.lyng.tools.LyngAnalysisRequest
|
||||
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||
|
||||
class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength)) {
|
||||
|
||||
@@ -58,7 +64,7 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
|
||||
// We need to find the actual PSI element for this member
|
||||
val targetFile = findFileForClass(file.project, owner) ?: file
|
||||
val targetMini = LyngAstManager.getMiniAst(targetFile)
|
||||
val targetMini = loadMini(targetFile)
|
||||
if (targetMini != null) {
|
||||
val targetSrc = targetMini.range.start.source
|
||||
val off = targetSrc.offsetOf(member.nameStart)
|
||||
@@ -123,24 +129,37 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
}
|
||||
|
||||
private fun findFileForClass(project: Project, className: String): PsiFile? {
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
|
||||
// 1. Try file with matching name first (optimization)
|
||||
val matchingFiles = FilenameIndex.getFilesByName(project, "$className.lyng", GlobalSearchScope.projectScope(project))
|
||||
val scope = GlobalSearchScope.projectScope(project)
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
val matchingFiles = FileTypeIndex.getFiles(LyngFileType, scope)
|
||||
.asSequence()
|
||||
.filter { it.name == "$className.lyng" }
|
||||
.mapNotNull { psiManager.findFile(it) }
|
||||
.toList()
|
||||
val matchingDeclFiles = FileTypeIndex.getFiles(LyngFileType, scope)
|
||||
.asSequence()
|
||||
.filter { it.name == "$className.lyng.d" }
|
||||
.mapNotNull { psiManager.findFile(it) }
|
||||
.toList()
|
||||
for (file in matchingFiles) {
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
||||
val mini = loadMini(file) ?: continue
|
||||
if (mini.declarations.any { isLocalDecl(mini, it) && ((it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className)) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
for (file in matchingDeclFiles) {
|
||||
val mini = loadMini(file) ?: continue
|
||||
if (mini.declarations.any { isLocalDecl(mini, it) && ((it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className)) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to full project scan
|
||||
val allFiles = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
||||
for (vFile in allFiles) {
|
||||
val file = psiManager.findFile(vFile) ?: continue
|
||||
if (matchingFiles.contains(file)) continue // already checked
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
||||
for (file in collectLyngFiles(project)) {
|
||||
if (matchingFiles.contains(file) || matchingDeclFiles.contains(file)) continue // already checked
|
||||
val mini = loadMini(file) ?: continue
|
||||
if (mini.declarations.any { isLocalDecl(mini, it) && ((it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className)) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
@@ -148,7 +167,7 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
}
|
||||
|
||||
private fun getPackageName(file: PsiFile): String? {
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return null
|
||||
val mini = loadMini(file) ?: return null
|
||||
return try {
|
||||
val pkg = mini.range.start.source.extractPackageName()
|
||||
if (pkg.startsWith("lyng.")) pkg else "lyng.$pkg"
|
||||
@@ -172,19 +191,19 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
|
||||
private fun resolveGlobally(project: Project, name: String, membersOnly: Boolean = false, allowedPackages: Set<String>? = null): List<ResolveResult> {
|
||||
val results = mutableListOf<ResolveResult>()
|
||||
val files = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
|
||||
for (vFile in files) {
|
||||
val file = psiManager.findFile(vFile) ?: continue
|
||||
for (file in collectLyngFiles(project)) {
|
||||
|
||||
// Filter by package if requested
|
||||
if (allowedPackages != null) {
|
||||
val pkg = getPackageName(file)
|
||||
if (pkg == null || pkg !in allowedPackages) continue
|
||||
if (pkg == null) {
|
||||
if (!file.name.endsWith(".lyng.d")) continue
|
||||
} else if (pkg !in allowedPackages) continue
|
||||
}
|
||||
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
val mini = loadMini(file) ?: continue
|
||||
val src = mini.range.start.source
|
||||
|
||||
fun addIfMatch(dName: String, nameStart: net.sergeych.lyng.Pos, dKind: String) {
|
||||
@@ -197,6 +216,7 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
}
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (!isLocalDecl(mini, d)) continue
|
||||
if (!membersOnly) {
|
||||
val dKind = when(d) {
|
||||
is net.sergeych.lyng.miniast.MiniFunDecl -> "Function"
|
||||
@@ -216,6 +236,7 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
}
|
||||
|
||||
for (m in members) {
|
||||
if (m.range.start.source != src) continue
|
||||
val mKind = when(m) {
|
||||
is net.sergeych.lyng.miniast.MiniMemberFunDecl -> "Function"
|
||||
is net.sergeych.lyng.miniast.MiniMemberValDecl -> if (m.mutable) "Variable" else "Value"
|
||||
@@ -229,5 +250,42 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
||||
return results
|
||||
}
|
||||
|
||||
private fun collectLyngFiles(project: Project): List<PsiFile> {
|
||||
val scope = GlobalSearchScope.projectScope(project)
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
val out = LinkedHashSet<PsiFile>()
|
||||
|
||||
val lyngFiles = FilenameIndex.getAllFilesByExt(project, "lyng", scope)
|
||||
for (vFile in lyngFiles) {
|
||||
psiManager.findFile(vFile)?.let { out.add(it) }
|
||||
}
|
||||
|
||||
// Include declaration files (*.lyng.d) which are indexed as extension "d".
|
||||
val dFiles = FilenameIndex.getAllFilesByExt(project, "d", scope)
|
||||
for (vFile in dFiles) {
|
||||
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||
psiManager.findFile(vFile)?.let { out.add(it) }
|
||||
}
|
||||
|
||||
return out.toList()
|
||||
}
|
||||
|
||||
private fun loadMini(file: PsiFile): MiniScript? {
|
||||
LyngAstManager.getMiniAst(file)?.let { return it }
|
||||
return try {
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
runBlocking {
|
||||
LyngLanguageTools.analyze(
|
||||
LyngAnalysisRequest(text = file.text, fileName = file.name, importProvider = provider)
|
||||
)
|
||||
}.mini
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun isLocalDecl(mini: MiniScript, decl: MiniDecl): Boolean =
|
||||
decl.range.start.source == mini.range.start.source
|
||||
|
||||
override fun getVariants(): Array<Any> = emptyArray()
|
||||
}
|
||||
@@ -21,14 +21,22 @@ import com.intellij.openapi.application.runReadAction
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiManager
|
||||
import com.intellij.psi.search.FileTypeIndex
|
||||
import com.intellij.psi.search.FilenameIndex
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.binding.BindingSnapshot
|
||||
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
import net.sergeych.lyng.miniast.MiniEnumDecl
|
||||
import net.sergeych.lyng.miniast.MiniRange
|
||||
import net.sergeych.lyng.miniast.MiniScript
|
||||
import net.sergeych.lyng.tools.IdeLenientImportProvider
|
||||
import net.sergeych.lyng.tools.LyngAnalysisRequest
|
||||
import net.sergeych.lyng.tools.LyngAnalysisResult
|
||||
import net.sergeych.lyng.tools.LyngDiagnostic
|
||||
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||
import net.sergeych.lyng.idea.LyngFileType
|
||||
|
||||
object LyngAstManager {
|
||||
private val MINI_KEY = Key.create<MiniScript>("lyng.mini.cache")
|
||||
@@ -52,22 +60,65 @@ object LyngAstManager {
|
||||
|
||||
private fun collectDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
|
||||
val psiManager = PsiManager.getInstance(file.project)
|
||||
var current = file.virtualFile?.parent
|
||||
val seen = mutableSetOf<String>()
|
||||
val result = mutableListOf<PsiFile>()
|
||||
|
||||
while (current != null) {
|
||||
for (child in current.children) {
|
||||
if (child.name.endsWith(".lyng.d") && child != file.virtualFile && seen.add(child.path)) {
|
||||
val psiD = psiManager.findFile(child) ?: continue
|
||||
result.add(psiD)
|
||||
var currentDir = file.containingDirectory
|
||||
while (currentDir != null) {
|
||||
for (child in currentDir.files) {
|
||||
if (child.name.endsWith(".lyng.d") && child != file && seen.add(child.virtualFile.path)) {
|
||||
result.add(child)
|
||||
}
|
||||
}
|
||||
current = current.parent
|
||||
currentDir = currentDir.parentDirectory
|
||||
}
|
||||
|
||||
if (result.isNotEmpty()) return@runReadAction result
|
||||
|
||||
// Fallback for virtual/light files without a stable parent chain (e.g., tests)
|
||||
val basePath = file.virtualFile?.path ?: return@runReadAction result
|
||||
val scope = GlobalSearchScope.projectScope(file.project)
|
||||
val dFiles = FilenameIndex.getAllFilesByExt(file.project, "d", scope)
|
||||
for (vFile in dFiles) {
|
||||
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||
if (vFile.path == basePath) continue
|
||||
val parentPath = vFile.parent?.path ?: continue
|
||||
if (basePath == parentPath || basePath.startsWith(parentPath.trimEnd('/') + "/")) {
|
||||
if (seen.add(vFile.path)) {
|
||||
psiManager.findFile(vFile)?.let { result.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.isNotEmpty()) return@runReadAction result
|
||||
|
||||
// Fallback: scan all Lyng files in project index and filter by .lyng.d
|
||||
val lyngFiles = FileTypeIndex.getFiles(LyngFileType, scope)
|
||||
for (vFile in lyngFiles) {
|
||||
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||
if (vFile.path == basePath) continue
|
||||
if (seen.add(vFile.path)) {
|
||||
psiManager.findFile(vFile)?.let { result.add(it) }
|
||||
}
|
||||
}
|
||||
|
||||
if (result.isNotEmpty()) return@runReadAction result
|
||||
|
||||
// Final fallback: include all .lyng.d files in project scope
|
||||
for (vFile in dFiles) {
|
||||
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||
if (vFile.path == basePath) continue
|
||||
if (seen.add(vFile.path)) {
|
||||
psiManager.findFile(vFile)?.let { result.add(it) }
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fun getDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
|
||||
collectDeclarationFiles(file)
|
||||
}
|
||||
|
||||
fun getBinding(file: PsiFile): BindingSnapshot? = runReadAction {
|
||||
getAnalysis(file)?.binding
|
||||
}
|
||||
@@ -92,20 +143,38 @@ object LyngAstManager {
|
||||
}
|
||||
|
||||
if (built != null) {
|
||||
val merged = built.mini
|
||||
if (merged != null && !file.name.endsWith(".lyng.d")) {
|
||||
val isDecl = file.name.endsWith(".lyng.d")
|
||||
val merged = if (!isDecl && built.mini == null) {
|
||||
MiniScript(MiniRange(built.source.startPos, built.source.startPos))
|
||||
} else {
|
||||
built.mini
|
||||
}
|
||||
if (merged != null && !isDecl) {
|
||||
val dFiles = collectDeclarationFiles(file)
|
||||
for (df in dFiles) {
|
||||
val dAnalysis = getAnalysis(df)
|
||||
val dMini = dAnalysis?.mini ?: continue
|
||||
val dMini = getAnalysis(df)?.mini ?: run {
|
||||
val dText = df.viewProvider.contents.toString()
|
||||
try {
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
runBlocking {
|
||||
LyngLanguageTools.analyze(
|
||||
LyngAnalysisRequest(text = dText, fileName = df.name, importProvider = provider)
|
||||
)
|
||||
}.mini
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
} ?: continue
|
||||
merged.declarations.addAll(dMini.declarations)
|
||||
merged.imports.addAll(dMini.imports)
|
||||
}
|
||||
}
|
||||
val finalAnalysis = if (merged != null) {
|
||||
val mergedImports = DocLookupUtils.canonicalImportedModules(merged, text)
|
||||
built.copy(
|
||||
mini = merged,
|
||||
importedModules = DocLookupUtils.canonicalImportedModules(merged, text)
|
||||
importedModules = mergedImports,
|
||||
diagnostics = filterDiagnostics(built.diagnostics, merged, text, mergedImports)
|
||||
)
|
||||
} else {
|
||||
built
|
||||
@@ -118,4 +187,45 @@ object LyngAstManager {
|
||||
}
|
||||
null
|
||||
}
|
||||
|
||||
private fun filterDiagnostics(
|
||||
diagnostics: List<LyngDiagnostic>,
|
||||
merged: MiniScript,
|
||||
text: String,
|
||||
importedModules: List<String>
|
||||
): List<LyngDiagnostic> {
|
||||
if (diagnostics.isEmpty()) return diagnostics
|
||||
val declaredTopLevel = merged.declarations.map { it.name }.toSet()
|
||||
|
||||
val declaredMembers = linkedSetOf<String>()
|
||||
val aggregatedClasses = DocLookupUtils.aggregateClasses(importedModules, merged)
|
||||
for (cls in aggregatedClasses.values) {
|
||||
cls.members.forEach { declaredMembers.add(it.name) }
|
||||
cls.ctorFields.forEach { declaredMembers.add(it.name) }
|
||||
cls.classFields.forEach { declaredMembers.add(it.name) }
|
||||
}
|
||||
merged.declarations.filterIsInstance<MiniEnumDecl>().forEach { en ->
|
||||
DocLookupUtils.enumToSyntheticClass(en).members.forEach { declaredMembers.add(it.name) }
|
||||
}
|
||||
|
||||
val builtinTopLevel = linkedSetOf<String>()
|
||||
for (mod in importedModules) {
|
||||
BuiltinDocRegistry.docsForModule(mod).forEach { builtinTopLevel.add(it.name) }
|
||||
}
|
||||
|
||||
return diagnostics.filterNot { diag ->
|
||||
val msg = diag.message
|
||||
if (msg.startsWith("unresolved name: ")) {
|
||||
val name = msg.removePrefix("unresolved name: ").trim()
|
||||
name in declaredTopLevel || name in builtinTopLevel
|
||||
} else if (msg.startsWith("unresolved member: ")) {
|
||||
val name = msg.removePrefix("unresolved member: ").trim()
|
||||
val range = diag.range
|
||||
val dotLeft = if (range != null) DocLookupUtils.findDotLeft(text, range.start) else null
|
||||
dotLeft != null && name in declaredMembers
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.definitions
|
||||
|
||||
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.idea.docs.LyngDocumentationProvider
|
||||
import net.sergeych.lyng.idea.navigation.LyngPsiReference
|
||||
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.CompletionEngineLight
|
||||
|
||||
class LyngDefinitionFilesTest : BasePlatformTestCase() {
|
||||
|
||||
override fun getTestDataPath(): String = ""
|
||||
|
||||
private fun enableCompletion() {
|
||||
LyngFormatterSettings.getInstance(project).enableLyngCompletionExperimental = true
|
||||
}
|
||||
|
||||
private fun addDefinitionsFile() {
|
||||
val defs = """
|
||||
/** Utilities exposed via .lyng.d */
|
||||
class Declared(val name: String) {
|
||||
/** Size property */
|
||||
val size: Int = 0
|
||||
|
||||
/** Returns greeting. */
|
||||
fun greet(who: String): String = "hi " + who
|
||||
}
|
||||
|
||||
/** Top-level function. */
|
||||
fun topFun(x: Int): Int = x + 1
|
||||
""".trimIndent()
|
||||
myFixture.addFileToProject("api.lyng.d", defs)
|
||||
}
|
||||
|
||||
fun test_CompletionsIncludeDefinitions() {
|
||||
addDefinitionsFile()
|
||||
enableCompletion()
|
||||
run {
|
||||
val code = """
|
||||
val v = top<caret>
|
||||
""".trimIndent()
|
||||
myFixture.configureByText("main.lyng", code)
|
||||
val text = myFixture.editor.document.text
|
||||
val caret = myFixture.caretOffset
|
||||
val analysis = LyngAstManager.getAnalysis(myFixture.file)
|
||||
val engine = runBlocking { CompletionEngineLight.completeSuspend(text, caret, analysis?.mini, analysis?.binding).map { it.name } }
|
||||
assertTrue("Expected topFun from .lyng.d; got=$engine", engine.contains("topFun"))
|
||||
}
|
||||
run {
|
||||
val code = """
|
||||
<caret>
|
||||
""".trimIndent()
|
||||
myFixture.configureByText("other.lyng", code)
|
||||
val text = myFixture.editor.document.text
|
||||
val caret = myFixture.caretOffset
|
||||
val analysis = LyngAstManager.getAnalysis(myFixture.file)
|
||||
val engine = runBlocking { CompletionEngineLight.completeSuspend(text, caret, analysis?.mini, analysis?.binding).map { it.name } }
|
||||
assertTrue("Expected Declared from .lyng.d; got=$engine", engine.contains("Declared"))
|
||||
}
|
||||
}
|
||||
|
||||
fun test_GotoDefinitionResolvesToDefinitionFile() {
|
||||
addDefinitionsFile()
|
||||
val code = """
|
||||
val x = topFun(1)
|
||||
val y = Declared("x")
|
||||
y.gre<caret>et("me")
|
||||
""".trimIndent()
|
||||
myFixture.configureByText("main.lyng", code)
|
||||
val offset = myFixture.caretOffset
|
||||
val element = myFixture.file.findElementAt(offset) ?: myFixture.file.findElementAt((offset - 1).coerceAtLeast(0))
|
||||
assertNotNull("Expected element at caret for resolve", element)
|
||||
val ref = LyngPsiReference(element!!)
|
||||
val resolved = ref.resolve()
|
||||
assertNotNull("Expected reference to resolve", resolved)
|
||||
assertTrue("Expected .lyng.d target; got=${resolved!!.containingFile.name}", resolved.containingFile.name.endsWith(".lyng.d"))
|
||||
}
|
||||
|
||||
fun test_QuickDocUsesDefinitionDocs() {
|
||||
addDefinitionsFile()
|
||||
val code = """
|
||||
val y = Declared("x")
|
||||
y.gre<caret>et("me")
|
||||
""".trimIndent()
|
||||
myFixture.configureByText("main.lyng", code)
|
||||
val provider = LyngDocumentationProvider()
|
||||
val offset = myFixture.caretOffset
|
||||
val element = myFixture.file.findElementAt(offset) ?: myFixture.file.findElementAt((offset - 1).coerceAtLeast(0))
|
||||
assertNotNull("Expected element at caret for doc", element)
|
||||
val doc = provider.generateDoc(element, element)
|
||||
assertNotNull("Expected Quick Doc", doc)
|
||||
assertTrue("Doc should include summary; got=$doc", doc!!.contains("Returns greeting"))
|
||||
}
|
||||
|
||||
fun test_DiagnosticsIgnoreDefinitionSymbols() {
|
||||
addDefinitionsFile()
|
||||
val code = """
|
||||
val x = topFun(1)
|
||||
val y = Declared("x")
|
||||
y.greet("me")
|
||||
""".trimIndent()
|
||||
myFixture.configureByText("main.lyng", code)
|
||||
val analysis = LyngAstManager.getAnalysis(myFixture.file)
|
||||
val messages = analysis?.diagnostics?.map { it.message } ?: emptyList()
|
||||
assertTrue("Should not report unresolved name for topFun", messages.none { it.contains("unresolved name: topFun") })
|
||||
assertTrue("Should not report unresolved name for Declared", messages.none { it.contains("unresolved name: Declared") })
|
||||
assertTrue("Should not report unresolved member for greet", messages.none { it.contains("unresolved member: greet") })
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,10 +24,10 @@ package net.sergeych.lyng.io.fs
|
||||
import net.sergeych.lyng.ModuleScope
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.ScopeFacade
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lyng.pacman.ImportManager
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyngio.fs.LyngFS
|
||||
import net.sergeych.lyngio.fs.LyngFs
|
||||
import net.sergeych.lyngio.fs.LyngPath
|
||||
@@ -191,7 +191,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
fsGuard {
|
||||
val self = this.thisObj as ObjPath
|
||||
val m = self.ensureMetadata()
|
||||
m.modifiedAtMillis?.let { ObjInstant(kotlinx.datetime.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||
m.modifiedAtMillis?.let { ObjInstant(kotlin.time.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||
}
|
||||
}
|
||||
// modifiedAtMillis(): Int? — milliseconds since epoch or null
|
||||
@@ -314,9 +314,9 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
ObjMap(mutableMapOf(
|
||||
ObjString("isFile") to ObjBool(m.isRegularFile),
|
||||
ObjString("isDirectory") to ObjBool(m.isDirectory),
|
||||
ObjString("size") to (m.size?.toLong() ?: 0L).toObj(),
|
||||
ObjString("createdAtMillis") to ((m.createdAtMillis ?: 0L)).toObj(),
|
||||
ObjString("modifiedAtMillis") to ((m.modifiedAtMillis ?: 0L)).toObj(),
|
||||
ObjString("size") to (m.size ?: 0L).toObj(),
|
||||
ObjString("createdAtMillis") to (m.createdAtMillis ?: 0L).toObj(),
|
||||
ObjString("modifiedAtMillis") to (m.modifiedAtMillis ?: 0L).toObj(),
|
||||
ObjString("isSymlink") to ObjBool(m.isSymlink),
|
||||
))
|
||||
}
|
||||
@@ -478,7 +478,7 @@ private suspend inline fun ScopeFacade.fsGuard(crossinline block: suspend () ->
|
||||
return try {
|
||||
block()
|
||||
} catch (e: AccessDeniedException) {
|
||||
raiseError(ObjIllegalOperationException(requireScope(), e.reasonDetail ?: "access denied"))
|
||||
raiseIllegalOperation(e.reasonDetail ?: "access denied")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ import kotlinx.coroutines.flow.Flow
|
||||
import net.sergeych.lyng.ModuleScope
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.ScopeFacade
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lyng.pacman.ImportManager
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyngio.process.*
|
||||
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
|
||||
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
|
||||
@@ -210,9 +210,9 @@ private suspend inline fun ScopeFacade.processGuard(crossinline block: suspend (
|
||||
return try {
|
||||
block()
|
||||
} catch (e: ProcessAccessDeniedException) {
|
||||
raiseError(ObjIllegalOperationException(requireScope(), e.reasonDetail ?: "process access denied"))
|
||||
raiseIllegalOperation(e.reasonDetail ?: "process access denied")
|
||||
} catch (e: Exception) {
|
||||
raiseError(ObjIllegalOperationException(requireScope(), e.message ?: "process error"))
|
||||
raiseIllegalOperation(e.message ?: "process error")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -220,6 +220,7 @@ data class ParsedArgument(
|
||||
val list: List<Obj>,
|
||||
val tailBlockMode: Boolean = false,
|
||||
val named: Map<String, Obj> = emptyMap(),
|
||||
val explicitTypeArgs: List<TypeDecl> = emptyList(),
|
||||
) : List<Obj> by list {
|
||||
|
||||
constructor(vararg values: Obj) : this(values.toList())
|
||||
|
||||
@@ -33,6 +33,7 @@ class ClassInstanceFieldDeclStatement(
|
||||
val isMutable: Boolean,
|
||||
val visibility: Visibility,
|
||||
val writeVisibility: Visibility?,
|
||||
val typeDecl: TypeDecl?,
|
||||
val isAbstract: Boolean,
|
||||
val isClosed: Boolean,
|
||||
val isOverride: Boolean,
|
||||
|
||||
@@ -36,10 +36,19 @@ class BytecodeClosureScope(
|
||||
val desired = preferredThisType?.let { typeName ->
|
||||
callScope.thisVariants.firstOrNull { it.objClass.className == typeName }
|
||||
}
|
||||
val primaryThis = closureScope.thisObj
|
||||
val merged = ArrayList<Obj>(callScope.thisVariants.size + closureScope.thisVariants.size + 1)
|
||||
val primaryThis = when {
|
||||
callScope is ApplyScope -> callScope.thisObj
|
||||
desired != null -> desired
|
||||
else -> closureScope.thisObj
|
||||
}
|
||||
val merged = ArrayList<Obj>(callScope.thisVariants.size + closureScope.thisVariants.size + 3)
|
||||
desired?.let { merged.add(it) }
|
||||
merged.add(callScope.thisObj)
|
||||
merged.addAll(callScope.thisVariants)
|
||||
if (callScope is ApplyScope) {
|
||||
merged.add(callScope.applied.thisObj)
|
||||
merged.addAll(callScope.applied.thisVariants)
|
||||
}
|
||||
merged.addAll(closureScope.thisVariants)
|
||||
setThisVariants(primaryThis, merged)
|
||||
this.currentClassCtx = closureScope.currentClassCtx ?: callScope.currentClassCtx
|
||||
@@ -47,10 +56,20 @@ class BytecodeClosureScope(
|
||||
}
|
||||
|
||||
class ApplyScope(val callScope: Scope, val applied: Scope) :
|
||||
Scope(callScope, thisObj = applied.thisObj) {
|
||||
Scope(applied, callScope.args, callScope.pos, callScope.thisObj) {
|
||||
|
||||
init {
|
||||
// Merge applied receiver variants with the caller variants so qualified this@Type
|
||||
// can see both the applied receiver and outer receivers.
|
||||
val merged = ArrayList<Obj>(applied.thisVariants.size + callScope.thisVariants.size + 1)
|
||||
merged.addAll(applied.thisVariants)
|
||||
merged.addAll(callScope.thisVariants)
|
||||
setThisVariants(callScope.thisObj, merged)
|
||||
this.currentClassCtx = applied.currentClassCtx ?: callScope.currentClassCtx
|
||||
}
|
||||
|
||||
override fun get(name: String): ObjRecord? {
|
||||
return applied.get(name) ?: super.get(name)
|
||||
return applied.get(name) ?: callScope.get(name)
|
||||
}
|
||||
|
||||
override fun applyClosure(closure: Scope, preferredThisType: String?): Scope {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -31,7 +31,7 @@ class DelegatedVarDeclStatement(
|
||||
) : Statement() {
|
||||
override val pos: Pos = startPos
|
||||
|
||||
override suspend fun execute(context: Scope): Obj {
|
||||
return bytecodeOnly(context, "delegated var declaration")
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
return bytecodeOnly(scope, "delegated var declaration")
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class DestructuringVarDeclStatement(
|
||||
val isTransient: Boolean,
|
||||
override val pos: Pos,
|
||||
) : Statement() {
|
||||
override suspend fun execute(context: Scope): Obj {
|
||||
return bytecodeOnly(context, "destructuring declaration")
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
return bytecodeOnly(scope, "destructuring declaration")
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class ExtensionPropertyDeclStatement(
|
||||
) : Statement() {
|
||||
override val pos: Pos = startPos
|
||||
|
||||
override suspend fun execute(context: Scope): Obj {
|
||||
return bytecodeOnly(context, "extension property declaration")
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
return bytecodeOnly(scope, "extension property declaration")
|
||||
}
|
||||
}
|
||||
@@ -56,10 +56,29 @@ class FrameSlotRef(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
val resolved = read()
|
||||
if (resolved === this) {
|
||||
scope.raiseNotImplemented("call on unresolved frame slot")
|
||||
}
|
||||
return resolved.callOn(scope)
|
||||
}
|
||||
|
||||
internal fun refersTo(frame: FrameAccess, slot: Int): Boolean {
|
||||
return this.frame === frame && this.slot == slot
|
||||
}
|
||||
|
||||
internal fun peekValue(): Obj? {
|
||||
val bytecodeFrame = frame as? net.sergeych.lyng.bytecode.BytecodeFrame ?: return read()
|
||||
val raw = bytecodeFrame.getRawObj(slot) ?: return null
|
||||
if (raw is FrameSlotRef && raw.refersTo(bytecodeFrame, slot)) return null
|
||||
return when (raw) {
|
||||
is FrameSlotRef -> raw.peekValue()
|
||||
is RecordSlotRef -> raw.peekValue()
|
||||
else -> raw
|
||||
}
|
||||
}
|
||||
|
||||
fun write(value: Obj) {
|
||||
when (value) {
|
||||
is ObjInt -> frame.setInt(slot, value.value)
|
||||
@@ -70,6 +89,62 @@ class FrameSlotRef(
|
||||
}
|
||||
}
|
||||
|
||||
class ScopeSlotRef(
|
||||
private val scope: Scope,
|
||||
private val slot: Int,
|
||||
private val name: String? = null,
|
||||
) : net.sergeych.lyng.obj.Obj() {
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
val resolvedOther = when (other) {
|
||||
is FrameSlotRef -> other.read()
|
||||
is RecordSlotRef -> other.read()
|
||||
is ScopeSlotRef -> other.read()
|
||||
else -> other
|
||||
}
|
||||
return read().compareTo(scope, resolvedOther)
|
||||
}
|
||||
|
||||
fun read(): Obj {
|
||||
val record = scope.getSlotRecord(slot)
|
||||
val direct = record.value
|
||||
if (direct is FrameSlotRef) return direct.read()
|
||||
if (direct is RecordSlotRef) return direct.read()
|
||||
if (direct is ScopeSlotRef) return direct.read()
|
||||
if (direct !== ObjUnset) {
|
||||
return direct
|
||||
}
|
||||
if (name == null) return record.value
|
||||
val resolved = scope.get(name) ?: return record.value
|
||||
if (resolved.value !== ObjUnset) {
|
||||
scope.updateSlotFor(name, resolved)
|
||||
}
|
||||
return resolved.value
|
||||
}
|
||||
|
||||
internal fun peekValue(): Obj? {
|
||||
val record = scope.getSlotRecord(slot)
|
||||
val direct = record.value
|
||||
return when (direct) {
|
||||
is FrameSlotRef -> direct.peekValue()
|
||||
is RecordSlotRef -> direct.peekValue()
|
||||
is ScopeSlotRef -> direct.peekValue()
|
||||
else -> direct
|
||||
}
|
||||
}
|
||||
|
||||
fun write(value: Obj) {
|
||||
scope.setSlotValue(slot, value)
|
||||
}
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
val resolved = read()
|
||||
if (resolved === this) {
|
||||
scope.raiseNotImplemented("call on unresolved scope slot")
|
||||
}
|
||||
return resolved.callOn(scope)
|
||||
}
|
||||
}
|
||||
|
||||
class RecordSlotRef(
|
||||
private val record: ObjRecord,
|
||||
) : net.sergeych.lyng.obj.Obj() {
|
||||
@@ -77,6 +152,7 @@ class RecordSlotRef(
|
||||
val resolvedOther = when (other) {
|
||||
is FrameSlotRef -> other.read()
|
||||
is RecordSlotRef -> other.read()
|
||||
is ScopeSlotRef -> other.read()
|
||||
else -> other
|
||||
}
|
||||
return read().compareTo(scope, resolvedOther)
|
||||
@@ -84,10 +160,37 @@ class RecordSlotRef(
|
||||
|
||||
fun read(): Obj {
|
||||
val direct = record.value
|
||||
return if (direct is FrameSlotRef) direct.read() else direct
|
||||
return when (direct) {
|
||||
is FrameSlotRef -> direct.read()
|
||||
is ScopeSlotRef -> direct.read()
|
||||
else -> direct
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
val resolved = read()
|
||||
if (resolved === this) {
|
||||
scope.raiseNotImplemented("call on unresolved record slot")
|
||||
}
|
||||
return resolved.callOn(scope)
|
||||
}
|
||||
|
||||
internal fun peekValue(): Obj? {
|
||||
val direct = record.value
|
||||
return when (direct) {
|
||||
is FrameSlotRef -> direct.peekValue()
|
||||
is RecordSlotRef -> direct.peekValue()
|
||||
is ScopeSlotRef -> direct.peekValue()
|
||||
else -> direct
|
||||
}
|
||||
}
|
||||
|
||||
fun write(value: Obj) {
|
||||
val direct = record.value
|
||||
if (direct is ScopeSlotRef) {
|
||||
direct.write(value)
|
||||
} else {
|
||||
record.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -44,11 +44,28 @@ class ModuleScope(
|
||||
|
||||
internal fun ensureModuleFrame(fn: CmdFunction): BytecodeFrame {
|
||||
val current = moduleFrame
|
||||
val frame = if (current == null || moduleFrameLocalCount != fn.localCount) {
|
||||
val frame = if (current == null) {
|
||||
BytecodeFrame(fn.localCount, 0).also {
|
||||
moduleFrame = it
|
||||
moduleFrameLocalCount = fn.localCount
|
||||
}
|
||||
} else if (fn.localCount > moduleFrameLocalCount) {
|
||||
val next = BytecodeFrame(fn.localCount, 0)
|
||||
current.copyTo(next)
|
||||
moduleFrame = next
|
||||
moduleFrameLocalCount = fn.localCount
|
||||
// Retarget frame-based locals to the new frame instance.
|
||||
val localNames = fn.localSlotNames
|
||||
for (i in localNames.indices) {
|
||||
val name = localNames[i] ?: continue
|
||||
val record = objects[name] ?: localBindings[name] ?: continue
|
||||
val value = record.value
|
||||
if (value is FrameSlotRef && value.refersTo(current, i)) {
|
||||
record.value = FrameSlotRef(next, i)
|
||||
updateSlotFor(name, record)
|
||||
}
|
||||
}
|
||||
next
|
||||
} else {
|
||||
current
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,8 @@ package net.sergeych.lyng
|
||||
|
||||
data class Pos(val source: Source, val line: Int, val column: Int) {
|
||||
override fun toString(): String {
|
||||
return "${source.fileName}:${line+1}:${column}"
|
||||
val col = if (column >= 0) column + 1 else column
|
||||
return "${source.fileName}:${line+1}:$col"
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
|
||||
@@ -471,6 +471,62 @@ open class Scope(
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolvedRecordValueOrNull(record: ObjRecord): Obj? {
|
||||
return when (val raw = record.value) {
|
||||
is FrameSlotRef -> raw.read()
|
||||
is RecordSlotRef -> raw.read()
|
||||
else -> raw
|
||||
}
|
||||
}
|
||||
|
||||
private fun declaredTypeForValueInThisScope(value: Obj): TypeDecl? {
|
||||
// Prefer direct bindings first.
|
||||
for (record in objects.values) {
|
||||
val decl = record.typeDecl ?: continue
|
||||
if (resolvedRecordValueOrNull(record) === value) return decl
|
||||
}
|
||||
for ((_, record) in localBindings) {
|
||||
val decl = record.typeDecl ?: continue
|
||||
if (resolvedRecordValueOrNull(record) === value) return decl
|
||||
}
|
||||
// Then slots (for frame-first locals).
|
||||
var i = 0
|
||||
while (i < slots.size) {
|
||||
val record = slots[i]
|
||||
val decl = record.typeDecl
|
||||
if (decl != null && resolvedRecordValueOrNull(record) === value) return decl
|
||||
i++
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun declaredCollectionElementTypeForValue(value: Obj, rawName: String): TypeDecl? {
|
||||
var s: Scope? = this
|
||||
var hops = 0
|
||||
while (s != null && hops++ < 1024) {
|
||||
val decl = s.declaredTypeForValueInThisScope(value)
|
||||
if (decl is TypeDecl.Generic && decl.name.substringAfterLast('.') == rawName) {
|
||||
return decl.args.firstOrNull()
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Best-effort lookup of the declared Set element type for a runtime set instance.
|
||||
* Returns null when type info is unavailable.
|
||||
*/
|
||||
fun declaredSetElementTypeForValue(value: Obj): TypeDecl? =
|
||||
declaredCollectionElementTypeForValue(value, "Set")
|
||||
|
||||
/**
|
||||
* Best-effort lookup of the declared List element type for a runtime list instance.
|
||||
* Returns null when type info is unavailable.
|
||||
*/
|
||||
fun declaredListElementTypeForValue(value: Obj): TypeDecl? =
|
||||
declaredCollectionElementTypeForValue(value, "List")
|
||||
|
||||
internal fun applySlotPlanReset(plan: Map<String, Int>, records: Map<String, ObjRecord>) {
|
||||
if (plan.isEmpty()) return
|
||||
slots.clear()
|
||||
@@ -635,10 +691,16 @@ open class Scope(
|
||||
objects[name]?.let {
|
||||
if( !it.isMutable )
|
||||
raiseIllegalAssignment("symbol is readonly: $name")
|
||||
it.value = value
|
||||
when (val current = it.value) {
|
||||
is FrameSlotRef -> current.write(value)
|
||||
is RecordSlotRef -> current.write(value)
|
||||
else -> it.value = value
|
||||
}
|
||||
// keep local binding index consistent within the frame
|
||||
localBindings[name] = it
|
||||
bumpClassLayoutIfNeeded(name, value, recordType)
|
||||
updateSlotFor(name, it)
|
||||
syncModuleFrameSlot(name, value)
|
||||
it
|
||||
} ?: addItem(name, true, value, visibility, writeVisibility, recordType, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride)
|
||||
|
||||
@@ -655,6 +717,7 @@ open class Scope(
|
||||
isOverride: Boolean = false,
|
||||
isTransient: Boolean = false,
|
||||
callSignature: CallSignature? = null,
|
||||
typeDecl: TypeDecl? = null,
|
||||
fieldId: Int? = null,
|
||||
methodId: Int? = null
|
||||
): ObjRecord {
|
||||
@@ -667,6 +730,7 @@ open class Scope(
|
||||
isOverride = isOverride,
|
||||
isTransient = isTransient,
|
||||
callSignature = callSignature,
|
||||
typeDecl = typeDecl,
|
||||
memberName = name,
|
||||
fieldId = fieldId,
|
||||
methodId = methodId
|
||||
@@ -703,6 +767,7 @@ open class Scope(
|
||||
slots[idx] = rec
|
||||
}
|
||||
}
|
||||
syncModuleFrameSlot(name, value)
|
||||
return rec
|
||||
}
|
||||
|
||||
@@ -750,7 +815,48 @@ open class Scope(
|
||||
|
||||
// --- removed doc-aware overloads to keep runtime lean ---
|
||||
|
||||
fun addConst(name: String, value: Obj) = addItem(name, false, value)
|
||||
fun addConst(name: String, value: Obj): ObjRecord {
|
||||
val existing = objects[name]
|
||||
if (existing != null) {
|
||||
when (val current = existing.value) {
|
||||
is FrameSlotRef -> current.write(value)
|
||||
is RecordSlotRef -> current.write(value)
|
||||
else -> existing.value = value
|
||||
}
|
||||
bumpClassLayoutIfNeeded(name, value, existing.type)
|
||||
updateSlotFor(name, existing)
|
||||
syncModuleFrameSlot(name, value)
|
||||
return existing
|
||||
}
|
||||
val slotIndex = getSlotIndexOf(name)
|
||||
if (slotIndex != null) {
|
||||
val record = getSlotRecord(slotIndex)
|
||||
when (val current = record.value) {
|
||||
is FrameSlotRef -> current.write(value)
|
||||
is RecordSlotRef -> current.write(value)
|
||||
else -> record.value = value
|
||||
}
|
||||
bumpClassLayoutIfNeeded(name, value, record.type)
|
||||
updateSlotFor(name, record)
|
||||
syncModuleFrameSlot(name, value)
|
||||
return record
|
||||
}
|
||||
val record = addItem(name, false, value)
|
||||
syncModuleFrameSlot(name, value)
|
||||
return record
|
||||
}
|
||||
|
||||
private fun syncModuleFrameSlot(name: String, value: Obj) {
|
||||
val module = this as? ModuleScope ?: return
|
||||
val frame = module.moduleFrame ?: return
|
||||
val localNames = module.moduleFrameLocalSlotNames
|
||||
if (localNames.isEmpty()) return
|
||||
for (i in localNames.indices) {
|
||||
if (localNames[i] == name) {
|
||||
frame.setObj(i, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
suspend fun eval(code: String): Obj =
|
||||
@@ -864,7 +970,7 @@ open class Scope(
|
||||
val receiver = rec.receiver ?: thisObj
|
||||
val del = rec.delegate ?: run {
|
||||
if (receiver is ObjInstance) {
|
||||
(receiver as ObjInstance).writeField(this, name, newValue)
|
||||
receiver.writeField(this, name, newValue)
|
||||
return
|
||||
}
|
||||
raiseError("Internal error: delegated property $name has no delegate")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,13 +12,12 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng
|
||||
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjRecord
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
import net.sergeych.lyng.obj.*
|
||||
|
||||
/**
|
||||
* Limited facade for Kotlin bridge callables.
|
||||
@@ -108,3 +107,29 @@ inline fun <reified T : Obj> ScopeFacade.thisAs(): T {
|
||||
|
||||
fun ScopeFacade.requireScope(): Scope =
|
||||
(this as? ScopeBridge)?.scope ?: raiseIllegalState("ScopeFacade requires ScopeBridge")
|
||||
|
||||
fun ScopeFacade.raiseNPE(): Nothing = requireScope().raiseNPE()
|
||||
|
||||
fun ScopeFacade.raiseIndexOutOfBounds(message: String = "Index out of bounds"): Nothing =
|
||||
requireScope().raiseIndexOutOfBounds(message)
|
||||
|
||||
fun ScopeFacade.raiseIllegalAssignment(message: String): Nothing =
|
||||
requireScope().raiseIllegalAssignment(message)
|
||||
|
||||
fun ScopeFacade.raiseUnset(message: String = "property is unset (not initialized)"): Nothing =
|
||||
requireScope().raiseUnset(message)
|
||||
|
||||
fun ScopeFacade.raiseNotFound(message: String = "not found"): Nothing =
|
||||
requireScope().raiseNotFound(message)
|
||||
|
||||
fun ScopeFacade.raiseError(obj: Obj, pos: Pos = this.pos, message: String): Nothing =
|
||||
requireScope().raiseError(obj, pos, message)
|
||||
|
||||
fun ScopeFacade.raiseAssertionFailed(message: String): Nothing =
|
||||
raiseError(ObjAssertionFailedException(requireScope(), message))
|
||||
|
||||
fun ScopeFacade.raiseIllegalOperation(message: String = "Operation is illegal"): Nothing =
|
||||
raiseError(ObjIllegalOperationException(requireScope(), message))
|
||||
|
||||
fun ScopeFacade.raiseIterationFinished(): Nothing =
|
||||
raiseError(ObjIterationFinishedException(requireScope()))
|
||||
@@ -25,8 +25,8 @@ import net.sergeych.lyng.bytecode.CmdVm
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lyng.pacman.ImportManager
|
||||
import net.sergeych.lyng.stdlib_included.observableLyng
|
||||
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.*
|
||||
@@ -43,13 +43,12 @@ class Script(
|
||||
// private val catchReturn: Boolean = false,
|
||||
) : Statement() {
|
||||
fun statements(): List<Statement> = statements
|
||||
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
scope.pos = pos
|
||||
val execScope = resolveModuleScope(scope) ?: scope
|
||||
val isModuleScope = execScope is ModuleScope
|
||||
val shouldSeedModule = isModuleScope || execScope.thisObj === ObjVoid
|
||||
val moduleTarget = execScope
|
||||
val moduleTarget = (execScope as? ModuleScope) ?: execScope.parent as? ModuleScope ?: execScope
|
||||
if (shouldSeedModule) {
|
||||
seedModuleSlots(moduleTarget, scope)
|
||||
}
|
||||
@@ -57,9 +56,15 @@ class Script(
|
||||
if (execScope is ModuleScope) {
|
||||
execScope.ensureModuleFrame(fn)
|
||||
}
|
||||
return CmdVm().execute(fn, execScope, scope.args) { frame, _ ->
|
||||
var execFrame: net.sergeych.lyng.bytecode.CmdFrame? = null
|
||||
val result = CmdVm().execute(fn, execScope, scope.args) { frame, _ ->
|
||||
execFrame = frame
|
||||
seedModuleLocals(frame, moduleTarget, scope)
|
||||
}
|
||||
if (execScope !is ModuleScope) {
|
||||
execFrame?.let { syncFrameLocalsToScope(it, execScope) }
|
||||
}
|
||||
return result
|
||||
}
|
||||
if (statements.isNotEmpty()) {
|
||||
scope.raiseIllegalState("bytecode-only execution is required; missing module bytecode")
|
||||
@@ -70,6 +75,13 @@ class Script(
|
||||
private suspend fun seedModuleSlots(scope: Scope, seedScope: Scope) {
|
||||
if (importBindings.isEmpty() && importedModules.isEmpty()) return
|
||||
seedImportBindings(scope, seedScope)
|
||||
if (moduleSlotPlan.isNotEmpty()) {
|
||||
scope.applySlotPlan(moduleSlotPlan)
|
||||
for (name in moduleSlotPlan.keys) {
|
||||
val record = scope.objects[name] ?: scope.localBindings[name] ?: continue
|
||||
scope.updateSlotFor(name, record)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun seedModuleLocals(
|
||||
@@ -88,12 +100,43 @@ class Script(
|
||||
val value = if (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property) {
|
||||
scope.resolve(record, name)
|
||||
} else {
|
||||
record.value
|
||||
val raw = record.value
|
||||
when (raw) {
|
||||
is FrameSlotRef -> {
|
||||
if (raw.refersTo(frame.frame, i)) {
|
||||
raw.peekValue() ?: continue
|
||||
} else if (seedScope !is ModuleScope) {
|
||||
raw
|
||||
} else {
|
||||
raw.read()
|
||||
}
|
||||
}
|
||||
is RecordSlotRef -> {
|
||||
if (seedScope !is ModuleScope) raw else raw.read()
|
||||
}
|
||||
else -> raw
|
||||
}
|
||||
}
|
||||
frame.setObjUnchecked(base + i, value)
|
||||
}
|
||||
}
|
||||
|
||||
private fun syncFrameLocalsToScope(frame: net.sergeych.lyng.bytecode.CmdFrame, scope: Scope) {
|
||||
val localNames = frame.fn.localSlotNames
|
||||
if (localNames.isEmpty()) return
|
||||
for (i in localNames.indices) {
|
||||
val name = localNames[i] ?: continue
|
||||
val record = scope.getLocalRecordDirect(name) ?: scope.localBindings[name] ?: scope.objects[name] ?: continue
|
||||
val value = frame.readLocalObj(i)
|
||||
when (val current = record.value) {
|
||||
is FrameSlotRef -> current.write(value)
|
||||
is RecordSlotRef -> current.write(value)
|
||||
else -> record.value = value
|
||||
}
|
||||
scope.updateSlotFor(name, record)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun seedImportBindings(scope: Scope, seedScope: Scope) {
|
||||
val provider = scope.currentImportProvider
|
||||
val importedModules = LinkedHashSet<ModuleScope>()
|
||||
@@ -103,6 +146,9 @@ class Script(
|
||||
if (scope is ModuleScope) {
|
||||
scope.importedModules = importedModules.toList()
|
||||
}
|
||||
for (module in importedModules) {
|
||||
module.importInto(scope, null)
|
||||
}
|
||||
for ((name, binding) in importBindings) {
|
||||
val record = when (val source = binding.source) {
|
||||
is ImportBindingSource.Module -> {
|
||||
@@ -165,8 +211,8 @@ class Script(
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* Create new scope using standard safe set of modules, using [defaultImportManager]. It is
|
||||
* suspended as first time calls requires compilation of standard library or other
|
||||
* Create new scope using a standard safe set of modules, using [defaultImportManager]. It is
|
||||
* suspended as first time invocation requires compilation of standard library or other
|
||||
* asynchronous initialization.
|
||||
*/
|
||||
suspend fun newScope(pos: Pos = Pos.builtIn) = defaultImportManager.newStdScope(pos)
|
||||
@@ -323,10 +369,10 @@ class Script(
|
||||
addVoidFn("assert") {
|
||||
val cond = requiredArg<ObjBool>(0)
|
||||
val message = if (args.size > 1)
|
||||
": " + toStringOf(call(args[1] as Obj)).value
|
||||
": " + toStringOf(call(args[1])).value
|
||||
else ""
|
||||
if (!cond.value == true)
|
||||
raiseError(ObjAssertionFailedException(requireScope(), "Assertion failed$message"))
|
||||
raiseAssertionFailed("Assertion failed$message")
|
||||
}
|
||||
|
||||
fun unwrapCompareArg(value: Obj): Obj {
|
||||
@@ -342,12 +388,7 @@ class Script(
|
||||
val a = unwrapCompareArg(requiredArg(0))
|
||||
val b = unwrapCompareArg(requiredArg(1))
|
||||
if (a.compareTo(requireScope(), b) != 0) {
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
requireScope(),
|
||||
"Assertion failed: ${inspect(a)} == ${inspect(b)}"
|
||||
)
|
||||
)
|
||||
raiseAssertionFailed("Assertion failed: ${inspect(a)} == ${inspect(b)}")
|
||||
}
|
||||
}
|
||||
// alias used in tests
|
||||
@@ -355,23 +396,13 @@ class Script(
|
||||
val a = unwrapCompareArg(requiredArg(0))
|
||||
val b = unwrapCompareArg(requiredArg(1))
|
||||
if (a.compareTo(requireScope(), b) != 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
requireScope(),
|
||||
"Assertion failed: ${inspect(a)} == ${inspect(b)}"
|
||||
)
|
||||
)
|
||||
raiseAssertionFailed("Assertion failed: ${inspect(a)} == ${inspect(b)}")
|
||||
}
|
||||
addVoidFn("assertNotEquals") {
|
||||
val a = unwrapCompareArg(requiredArg(0))
|
||||
val b = unwrapCompareArg(requiredArg(1))
|
||||
if (a.compareTo(requireScope(), b) == 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
requireScope(),
|
||||
"Assertion failed: ${inspect(a)} != ${inspect(b)}"
|
||||
)
|
||||
)
|
||||
raiseAssertionFailed("Assertion failed: ${inspect(a)} != ${inspect(b)}")
|
||||
}
|
||||
addFnDoc(
|
||||
"assertThrows",
|
||||
@@ -409,12 +440,7 @@ class Script(
|
||||
} catch (_: ScriptError) {
|
||||
ObjNull
|
||||
}
|
||||
if (result == null) raiseError(
|
||||
ObjAssertionFailedException(
|
||||
requireScope(),
|
||||
"Expected exception but nothing was thrown"
|
||||
)
|
||||
)
|
||||
if (result == null) raiseAssertionFailed("Expected exception but nothing was thrown")
|
||||
expectedClass?.let {
|
||||
if (!result.isInstanceOf(it)) {
|
||||
val actual = if (result is ObjException) result.exceptionClass else result.objClass
|
||||
@@ -497,9 +523,12 @@ class Script(
|
||||
addConst("Bool", ObjBool.type)
|
||||
addConst("Char", ObjChar.type)
|
||||
addConst("List", ObjList.type)
|
||||
addConst("ImmutableList", ObjImmutableList.type)
|
||||
addConst("Set", ObjSet.type)
|
||||
addConst("ImmutableSet", ObjImmutableSet.type)
|
||||
addConst("Range", ObjRange.type)
|
||||
addConst("Map", ObjMap.type)
|
||||
addConst("ImmutableMap", ObjImmutableMap.type)
|
||||
addConst("MapEntry", ObjMapEntry.type)
|
||||
@Suppress("RemoveRedundantQualifierName")
|
||||
addConst("Callable", Statement.type)
|
||||
@@ -565,6 +594,19 @@ class Script(
|
||||
module.eval(Source("lyng.stdlib", rootLyng))
|
||||
ObjKotlinIterator.bindTo(module.requireClass("KotlinIterator"))
|
||||
}
|
||||
addPackage("lyng.observable") { module ->
|
||||
module.addConst("Observable", ObjObservable)
|
||||
module.addConst("Subscription", ObjSubscription.type)
|
||||
module.addConst("ListChange", ObjListChange.type)
|
||||
module.addConst("ListSet", ObjListSetChange.type)
|
||||
module.addConst("ListInsert", ObjListInsertChange.type)
|
||||
module.addConst("ListRemove", ObjListRemoveChange.type)
|
||||
module.addConst("ListClear", ObjListClearChange.type)
|
||||
module.addConst("ListReorder", ObjListReorderChange.type)
|
||||
module.addConst("ObservableList", ObjObservableList.type)
|
||||
module.addConst("ChangeRejectionException", ObjChangeRejectionExceptionClass)
|
||||
module.eval(Source("lyng.observable", observableLyng))
|
||||
}
|
||||
addPackage("lyng.buffer") {
|
||||
it.addConstDoc(
|
||||
name = "Buffer",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,6 +30,10 @@ sealed class TypeDecl(val isNullable:Boolean = false) {
|
||||
val returnType: TypeDecl,
|
||||
val nullable: Boolean = false
|
||||
) : TypeDecl(nullable)
|
||||
data class Ellipsis(
|
||||
val elementType: TypeDecl,
|
||||
val nullable: Boolean = false
|
||||
) : TypeDecl(nullable)
|
||||
data class TypeVar(val name: String, val nullable: Boolean = false) : TypeDecl(nullable)
|
||||
data class Union(val options: List<TypeDecl>, val nullable: Boolean = false) : TypeDecl(nullable)
|
||||
data class Intersection(val options: List<TypeDecl>, val nullable: Boolean = false) : TypeDecl(nullable)
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng
|
||||
@@ -25,6 +26,7 @@ class VarDeclStatement(
|
||||
val visibility: Visibility,
|
||||
val initializer: Statement?,
|
||||
val isTransient: Boolean,
|
||||
val typeDecl: TypeDecl?,
|
||||
val slotIndex: Int?,
|
||||
val scopeId: Int?,
|
||||
private val startPos: Pos,
|
||||
@@ -32,7 +34,7 @@ class VarDeclStatement(
|
||||
) : Statement() {
|
||||
override val pos: Pos = startPos
|
||||
|
||||
override suspend fun execute(context: Scope): Obj {
|
||||
return bytecodeOnly(context, "var declaration")
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
return bytecodeOnly(scope, "var declaration")
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,6 +12,7 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng
|
||||
@@ -55,6 +56,15 @@ class WhenIsCondition(
|
||||
}
|
||||
}
|
||||
|
||||
class WhenNullableCondition(
|
||||
val negated: Boolean,
|
||||
override val pos: Pos,
|
||||
) : WhenCondition(ExpressionStatement(net.sergeych.lyng.obj.ConstRef(net.sergeych.lyng.obj.ObjVoid.asReadonly), pos), pos) {
|
||||
override suspend fun matches(scope: Scope, value: Obj): Boolean {
|
||||
return bytecodeOnly(scope)
|
||||
}
|
||||
}
|
||||
|
||||
data class WhenCase(val conditions: List<WhenCondition>, val block: Statement)
|
||||
|
||||
class WhenStatement(
|
||||
|
||||
@@ -60,12 +60,54 @@ interface ClassBridgeBinder {
|
||||
/** Register an initialization hook that runs for each instance. */
|
||||
fun init(block: suspend BridgeInstanceContext.(ScopeFacade) -> Unit)
|
||||
/** Register an initialization hook with direct access to the instance. */
|
||||
fun initWithInstance(block: suspend ScopeFacade.() -> Unit)
|
||||
/**
|
||||
* Legacy initWithInstance form.
|
||||
* Prefer [initWithInstance] with [ScopeFacade] receiver and use [ScopeFacade.thisObj].
|
||||
*/
|
||||
@Deprecated(
|
||||
message = "Use initWithInstance { ... } with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith("initWithInstance { block(this, thisObj) }")
|
||||
)
|
||||
fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit)
|
||||
/** Bind a Lyng function/member to a Kotlin implementation (requires `extern` in Lyng). */
|
||||
fun addFun(name: String, impl: suspend ScopeFacade.() -> Obj)
|
||||
/**
|
||||
* Legacy addFun form.
|
||||
* Prefer [addFun] with [ScopeFacade] receiver and use [ScopeFacade.thisObj]/[ScopeFacade.args].
|
||||
*/
|
||||
@Deprecated(
|
||||
message = "Use addFun(name) { ... } with ScopeFacade receiver; use thisObj/args from scope",
|
||||
replaceWith = ReplaceWith("addFun(name) { impl(this, thisObj, args) }")
|
||||
)
|
||||
fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj)
|
||||
/** Bind a read-only member (val/property getter) declared as `extern`. */
|
||||
fun addVal(name: String, impl: suspend ScopeFacade.() -> Obj)
|
||||
/**
|
||||
* Legacy addVal form.
|
||||
* Prefer [addVal] with [ScopeFacade] receiver and use [ScopeFacade.thisObj].
|
||||
*/
|
||||
@Deprecated(
|
||||
message = "Use addVal(name) { ... } with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith("addVal(name) { impl(this, thisObj) }")
|
||||
)
|
||||
fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj)
|
||||
/** Bind a mutable member (var/property getter/setter) declared as `extern`. */
|
||||
fun addVar(
|
||||
name: String,
|
||||
get: suspend ScopeFacade.() -> Obj,
|
||||
set: suspend ScopeFacade.(Obj) -> Unit
|
||||
)
|
||||
/**
|
||||
* Legacy addVar form.
|
||||
* Prefer [addVar] with [ScopeFacade] receiver and use [ScopeFacade.thisObj].
|
||||
*/
|
||||
@Deprecated(
|
||||
message = "Use addVar(name, get, set) with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith(
|
||||
"addVar(name, get = { get(this, thisObj) }, set = { value -> set(this, thisObj, value) })"
|
||||
)
|
||||
)
|
||||
fun addVar(
|
||||
name: String,
|
||||
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||
@@ -122,6 +164,51 @@ object LyngClassBridge {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Entry point for Kotlin bindings to declared Lyng objects (singleton instances).
|
||||
*
|
||||
* Works similarly to [LyngClassBridge], but targets an already created object instance.
|
||||
*/
|
||||
object LyngObjectBridge {
|
||||
/**
|
||||
* Resolve a Lyng object by [objectName] and bind Kotlin implementations.
|
||||
*
|
||||
* @param module module name used for resolution (required when [module] scope is not provided)
|
||||
* @param importManager import manager used to resolve the module
|
||||
*/
|
||||
suspend fun bind(
|
||||
objectName: String,
|
||||
module: String? = null,
|
||||
importManager: ImportManager = Script.defaultImportManager,
|
||||
block: ClassBridgeBinder.() -> Unit
|
||||
): ObjInstance {
|
||||
val obj = resolveObject(objectName, module, null, importManager)
|
||||
return bind(obj, block)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a Lyng object within an existing [moduleScope] and bind Kotlin implementations.
|
||||
*/
|
||||
suspend fun bind(
|
||||
moduleScope: ModuleScope,
|
||||
objectName: String,
|
||||
block: ClassBridgeBinder.() -> Unit
|
||||
): ObjInstance {
|
||||
val obj = resolveObject(objectName, null, moduleScope, Script.defaultImportManager)
|
||||
return bind(obj, block)
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind Kotlin implementations directly to an already resolved object [instance].
|
||||
*/
|
||||
suspend fun bind(instance: ObjInstance, block: ClassBridgeBinder.() -> Unit): ObjInstance {
|
||||
val binder = ObjectBridgeBinderImpl(instance)
|
||||
binder.block()
|
||||
binder.commit()
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sugar for [LyngClassBridge.bind] on a module scope.
|
||||
*
|
||||
@@ -132,6 +219,16 @@ suspend fun ModuleScope.bind(
|
||||
block: ClassBridgeBinder.() -> Unit
|
||||
): ObjClass = LyngClassBridge.bind(this, className, block)
|
||||
|
||||
/**
|
||||
* Sugar for [LyngObjectBridge.bind] on a module scope.
|
||||
*
|
||||
* Bound members must be declared as `extern` in Lyng.
|
||||
*/
|
||||
suspend fun ModuleScope.bindObject(
|
||||
objectName: String,
|
||||
block: ClassBridgeBinder.() -> Unit
|
||||
): ObjInstance = LyngObjectBridge.bind(this, objectName, block)
|
||||
|
||||
/** Kotlin-side data slot attached to a Lyng instance. */
|
||||
var ObjInstance.data: Any?
|
||||
get() = kotlinInstanceData
|
||||
@@ -179,17 +276,27 @@ private class ClassBridgeBinderImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override fun initWithInstance(block: suspend ScopeFacade.() -> Unit) {
|
||||
initHooks.add { scope, _ ->
|
||||
scope.block()
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use initWithInstance { ... } with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith("initWithInstance { block(this, thisObj) }")
|
||||
)
|
||||
override fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit) {
|
||||
initHooks.add { scope, inst ->
|
||||
block(scope, inst)
|
||||
}
|
||||
}
|
||||
|
||||
override fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj) {
|
||||
override fun addFun(name: String, impl: suspend ScopeFacade.() -> Obj) {
|
||||
ensureTemplateNotBuilt()
|
||||
val target = findMember(name)
|
||||
val callable = ObjExternCallable.fromBridge {
|
||||
impl(this, thisObj, args)
|
||||
impl()
|
||||
}
|
||||
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||
val newRecord = target.record.copy(
|
||||
@@ -200,14 +307,24 @@ private class ClassBridgeBinderImpl(
|
||||
replaceMember(target, newRecord)
|
||||
}
|
||||
|
||||
override fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj) {
|
||||
@Deprecated(
|
||||
message = "Use addFun(name) { ... } with ScopeFacade receiver; use thisObj/args from scope",
|
||||
replaceWith = ReplaceWith("addFun(name) { impl(this, thisObj, args) }")
|
||||
)
|
||||
override fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj) {
|
||||
addFun(name) {
|
||||
impl(this, thisObj, args)
|
||||
}
|
||||
}
|
||||
|
||||
override fun addVal(name: String, impl: suspend ScopeFacade.() -> Obj) {
|
||||
ensureTemplateNotBuilt()
|
||||
val target = findMember(name)
|
||||
if (target.record.isMutable) {
|
||||
throw ScriptError(Pos.builtIn, "extern val $name is mutable in class ${cls.className}")
|
||||
}
|
||||
val getter = ObjExternCallable.fromBridge {
|
||||
impl(this, thisObj)
|
||||
impl()
|
||||
}
|
||||
val prop = ObjProperty(name, getter, null)
|
||||
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||
@@ -232,10 +349,20 @@ private class ClassBridgeBinderImpl(
|
||||
replaceMember(target, newRecord)
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use addVal(name) { ... } with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith("addVal(name) { impl(this, thisObj) }")
|
||||
)
|
||||
override fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj) {
|
||||
addVal(name) {
|
||||
impl(this, thisObj)
|
||||
}
|
||||
}
|
||||
|
||||
override fun addVar(
|
||||
name: String,
|
||||
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||
set: suspend (ScopeFacade, Obj, Obj) -> Unit
|
||||
get: suspend ScopeFacade.() -> Obj,
|
||||
set: suspend ScopeFacade.(Obj) -> Unit
|
||||
) {
|
||||
ensureTemplateNotBuilt()
|
||||
val target = findMember(name)
|
||||
@@ -243,11 +370,11 @@ private class ClassBridgeBinderImpl(
|
||||
throw ScriptError(Pos.builtIn, "extern var $name is readonly in class ${cls.className}")
|
||||
}
|
||||
val getter = ObjExternCallable.fromBridge {
|
||||
get(this, thisObj)
|
||||
get()
|
||||
}
|
||||
val setter = ObjExternCallable.fromBridge {
|
||||
val value = requiredArg<Obj>(0)
|
||||
set(this, thisObj, value)
|
||||
set(value)
|
||||
ObjVoid
|
||||
}
|
||||
val prop = ObjProperty(name, getter, setter)
|
||||
@@ -273,6 +400,24 @@ private class ClassBridgeBinderImpl(
|
||||
replaceMember(target, newRecord)
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use addVar(name, get, set) with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith(
|
||||
"addVar(name, get = { get(this, thisObj) }, set = { value -> set(this, thisObj, value) })"
|
||||
)
|
||||
)
|
||||
override fun addVar(
|
||||
name: String,
|
||||
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||
set: suspend (ScopeFacade, Obj, Obj) -> Unit
|
||||
) {
|
||||
addVar(
|
||||
name,
|
||||
get = { get(this, thisObj) },
|
||||
set = { value -> set(this, thisObj, value) }
|
||||
)
|
||||
}
|
||||
|
||||
fun commit() {
|
||||
if (initHooks.isNotEmpty()) {
|
||||
val target = cls.bridgeInitHooks ?: mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>().also {
|
||||
@@ -327,6 +472,262 @@ private class ClassBridgeBinderImpl(
|
||||
}
|
||||
}
|
||||
|
||||
private class ObjectBridgeBinderImpl(
|
||||
private val instance: ObjInstance
|
||||
) : ClassBridgeBinder {
|
||||
private val cls: ObjClass = instance.objClass
|
||||
private val initHooks = mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>()
|
||||
|
||||
override var classData: Any?
|
||||
get() = cls.kotlinClassData
|
||||
set(value) { cls.kotlinClassData = value }
|
||||
|
||||
override fun init(block: suspend BridgeInstanceContext.(ScopeFacade) -> Unit) {
|
||||
initHooks.add { scope, inst ->
|
||||
val ctx = BridgeInstanceContextImpl(inst)
|
||||
ctx.block(scope)
|
||||
}
|
||||
}
|
||||
|
||||
override fun initWithInstance(block: suspend ScopeFacade.() -> Unit) {
|
||||
initHooks.add { scope, _ ->
|
||||
scope.block()
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use initWithInstance { ... } with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith("initWithInstance { block(this, thisObj) }")
|
||||
)
|
||||
override fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit) {
|
||||
initHooks.add { scope, inst ->
|
||||
block(scope, inst)
|
||||
}
|
||||
}
|
||||
|
||||
override fun addFun(name: String, impl: suspend ScopeFacade.() -> Obj) {
|
||||
val target = findMember(name)
|
||||
val callable = ObjExternCallable.fromBridge {
|
||||
impl()
|
||||
}
|
||||
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||
val newRecord = target.record.copy(
|
||||
value = callable,
|
||||
type = ObjRecord.Type.Fun,
|
||||
methodId = methodId
|
||||
)
|
||||
replaceMember(target, newRecord)
|
||||
updateInstanceMember(target, newRecord)
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use addFun(name) { ... } with ScopeFacade receiver; use thisObj/args from scope",
|
||||
replaceWith = ReplaceWith("addFun(name) { impl(this, thisObj, args) }")
|
||||
)
|
||||
override fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj) {
|
||||
addFun(name) {
|
||||
impl(this, thisObj, args)
|
||||
}
|
||||
}
|
||||
|
||||
override fun addVal(name: String, impl: suspend ScopeFacade.() -> Obj) {
|
||||
val target = findMember(name)
|
||||
if (target.record.isMutable) {
|
||||
throw ScriptError(Pos.builtIn, "extern val $name is mutable in class ${cls.className}")
|
||||
}
|
||||
val getter = ObjExternCallable.fromBridge {
|
||||
impl()
|
||||
}
|
||||
val prop = ObjProperty(name, getter, null)
|
||||
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||
target.record.type == ObjRecord.Type.ConstructorField
|
||||
val newRecord = if (isFieldLike) {
|
||||
removeFieldInitializersFor(name)
|
||||
target.record.copy(
|
||||
value = prop,
|
||||
type = target.record.type,
|
||||
fieldId = target.record.fieldId,
|
||||
methodId = target.record.methodId
|
||||
)
|
||||
} else {
|
||||
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||
target.record.copy(
|
||||
value = prop,
|
||||
type = ObjRecord.Type.Property,
|
||||
methodId = methodId,
|
||||
fieldId = null
|
||||
)
|
||||
}
|
||||
replaceMember(target, newRecord)
|
||||
updateInstanceMember(target, newRecord)
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use addVal(name) { ... } with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith("addVal(name) { impl(this, thisObj) }")
|
||||
)
|
||||
override fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj) {
|
||||
addVal(name) {
|
||||
impl(this, thisObj)
|
||||
}
|
||||
}
|
||||
|
||||
override fun addVar(
|
||||
name: String,
|
||||
get: suspend ScopeFacade.() -> Obj,
|
||||
set: suspend ScopeFacade.(Obj) -> Unit
|
||||
) {
|
||||
val target = findMember(name)
|
||||
if (!target.record.isMutable) {
|
||||
throw ScriptError(Pos.builtIn, "extern var $name is readonly in class ${cls.className}")
|
||||
}
|
||||
val getter = ObjExternCallable.fromBridge {
|
||||
get()
|
||||
}
|
||||
val setter = ObjExternCallable.fromBridge {
|
||||
val value = requiredArg<Obj>(0)
|
||||
set(value)
|
||||
ObjVoid
|
||||
}
|
||||
val prop = ObjProperty(name, getter, setter)
|
||||
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||
target.record.type == ObjRecord.Type.ConstructorField
|
||||
val newRecord = if (isFieldLike) {
|
||||
removeFieldInitializersFor(name)
|
||||
target.record.copy(
|
||||
value = prop,
|
||||
type = target.record.type,
|
||||
fieldId = target.record.fieldId,
|
||||
methodId = target.record.methodId
|
||||
)
|
||||
} else {
|
||||
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||
target.record.copy(
|
||||
value = prop,
|
||||
type = ObjRecord.Type.Property,
|
||||
methodId = methodId,
|
||||
fieldId = null
|
||||
)
|
||||
}
|
||||
replaceMember(target, newRecord)
|
||||
updateInstanceMember(target, newRecord)
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
message = "Use addVar(name, get, set) with ScopeFacade receiver; use thisObj from scope",
|
||||
replaceWith = ReplaceWith(
|
||||
"addVar(name, get = { get(this, thisObj) }, set = { value -> set(this, thisObj, value) })"
|
||||
)
|
||||
)
|
||||
override fun addVar(
|
||||
name: String,
|
||||
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||
set: suspend (ScopeFacade, Obj, Obj) -> Unit
|
||||
) {
|
||||
addVar(
|
||||
name,
|
||||
get = { get(this, thisObj) },
|
||||
set = { value -> set(this, thisObj, value) }
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun commit() {
|
||||
if (initHooks.isNotEmpty()) {
|
||||
val target = cls.bridgeInitHooks ?: mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>().also {
|
||||
cls.bridgeInitHooks = it
|
||||
}
|
||||
target.addAll(initHooks)
|
||||
val facade = instance.instanceScope.asFacade()
|
||||
for (hook in initHooks) {
|
||||
hook(facade, instance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun replaceMember(target: MemberTarget, newRecord: ObjRecord) {
|
||||
when (target.kind) {
|
||||
MemberKind.Instance -> {
|
||||
cls.replaceMemberForBridge(target.name, newRecord)
|
||||
if (target.mirrorClassScope && cls.classScope?.objects?.containsKey(target.name) == true) {
|
||||
cls.replaceClassScopeMemberForBridge(target.name, newRecord)
|
||||
}
|
||||
}
|
||||
MemberKind.Static -> cls.replaceClassScopeMemberForBridge(target.name, newRecord)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateInstanceMember(target: MemberTarget, newRecord: ObjRecord) {
|
||||
val key = instanceStorageKey(target, newRecord) ?: return
|
||||
ensureInstanceSlotCapacity()
|
||||
instance.instanceScope.objects[key] = newRecord
|
||||
cls.fieldSlotForKey(key)?.let { slot ->
|
||||
instance.setFieldSlotRecord(slot.slot, newRecord)
|
||||
}
|
||||
cls.methodSlotForKey(key)?.let { slot ->
|
||||
instance.setMethodSlotRecord(slot.slot, newRecord)
|
||||
}
|
||||
}
|
||||
|
||||
private fun instanceStorageKey(target: MemberTarget, rec: ObjRecord): String? = when (target.kind) {
|
||||
MemberKind.Instance -> {
|
||||
if (rec.visibility == Visibility.Private ||
|
||||
rec.type == ObjRecord.Type.Field ||
|
||||
rec.type == ObjRecord.Type.ConstructorField ||
|
||||
rec.type == ObjRecord.Type.Delegated) {
|
||||
cls.mangledName(target.name)
|
||||
} else {
|
||||
target.name
|
||||
}
|
||||
}
|
||||
MemberKind.Static -> {
|
||||
if (rec.type != ObjRecord.Type.Fun &&
|
||||
rec.type != ObjRecord.Type.Property &&
|
||||
rec.type != ObjRecord.Type.Delegated) {
|
||||
null
|
||||
} else if (rec.visibility == Visibility.Private || rec.type == ObjRecord.Type.Delegated) {
|
||||
cls.mangledName(target.name)
|
||||
} else {
|
||||
target.name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ensureInstanceSlotCapacity() {
|
||||
val fieldCount = cls.fieldSlotCount()
|
||||
if (instance.fieldSlots.size < fieldCount) {
|
||||
val newSlots = arrayOfNulls<ObjRecord>(fieldCount)
|
||||
instance.fieldSlots.copyInto(newSlots, 0, 0, instance.fieldSlots.size)
|
||||
instance.fieldSlots = newSlots
|
||||
}
|
||||
val methodCount = cls.methodSlotCount()
|
||||
if (instance.methodSlots.size < methodCount) {
|
||||
val newSlots = arrayOfNulls<ObjRecord>(methodCount)
|
||||
instance.methodSlots.copyInto(newSlots, 0, 0, instance.methodSlots.size)
|
||||
instance.methodSlots = newSlots
|
||||
}
|
||||
}
|
||||
|
||||
private fun findMember(name: String): MemberTarget {
|
||||
val inst = cls.members[name]
|
||||
val stat = cls.classScope?.objects?.get(name)
|
||||
if (inst != null) {
|
||||
return MemberTarget(name, inst, MemberKind.Instance, mirrorClassScope = stat != null)
|
||||
}
|
||||
if (stat != null) return MemberTarget(name, stat, MemberKind.Static)
|
||||
throw ScriptError(Pos.builtIn, "extern member $name not found in class ${cls.className}")
|
||||
}
|
||||
|
||||
private fun removeFieldInitializersFor(name: String) {
|
||||
if (cls.instanceInitializers.isEmpty()) return
|
||||
val storageName = cls.mangledName(name)
|
||||
cls.instanceInitializers.removeAll { init ->
|
||||
val stmt = init as? Statement ?: return@removeAll false
|
||||
val original = (stmt as? BytecodeStatement)?.original ?: stmt
|
||||
original is InstanceFieldInitStatement && original.storageName == storageName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun resolveClass(
|
||||
className: String,
|
||||
module: String?,
|
||||
@@ -349,3 +750,26 @@ private suspend fun resolveClass(
|
||||
}
|
||||
throw ScriptError(Pos.builtIn, "class $className not found in module ${scope.packageName}")
|
||||
}
|
||||
|
||||
private suspend fun resolveObject(
|
||||
objectName: String,
|
||||
module: String?,
|
||||
moduleScope: ModuleScope?,
|
||||
importManager: ImportManager
|
||||
): ObjInstance {
|
||||
val scope = moduleScope ?: run {
|
||||
if (module == null) {
|
||||
throw ScriptError(Pos.builtIn, "module is required to resolve $objectName")
|
||||
}
|
||||
importManager.createModuleScope(Pos.builtIn, module)
|
||||
}
|
||||
val rec = scope.get(objectName)
|
||||
val direct = rec?.value as? ObjInstance
|
||||
if (direct != null) return direct
|
||||
if (objectName.contains('.')) {
|
||||
val resolved = scope.resolveQualifiedIdentifier(objectName)
|
||||
val inst = resolved as? ObjInstance
|
||||
if (inst != null) return inst
|
||||
}
|
||||
throw ScriptError(Pos.builtIn, "object $objectName not found in module ${scope.packageName}")
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.bridge
|
||||
|
||||
import net.sergeych.lyng.*
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjBool
|
||||
import net.sergeych.lyng.obj.ObjExternCallable
|
||||
import net.sergeych.lyng.obj.ObjInt
|
||||
import net.sergeych.lyng.obj.ObjNull
|
||||
import net.sergeych.lyng.obj.ObjProperty
|
||||
import net.sergeych.lyng.obj.ObjReal
|
||||
import net.sergeych.lyng.obj.ObjRecord
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
import net.sergeych.lyng.obj.ObjVoid
|
||||
import net.sergeych.lyng.obj.toBool
|
||||
import net.sergeych.lyng.obj.toDouble
|
||||
import net.sergeych.lyng.obj.toInt
|
||||
import net.sergeych.lyng.obj.toLong
|
||||
import net.sergeych.lyng.obj.toObj
|
||||
import net.sergeych.lyng.requiredArg
|
||||
|
||||
/**
|
||||
* Global/module-level binding API for Lyng-first extern declarations.
|
||||
*
|
||||
* Typical flow:
|
||||
* 1) declare `extern fun` / `extern val` / `extern var` in Lyng module;
|
||||
* 2) bind Kotlin implementation using this API.
|
||||
*/
|
||||
interface LyngGlobalBinder {
|
||||
fun bindGlobalFunRaw(
|
||||
name: String,
|
||||
fn: suspend (scope: ScopeFacade, args: Arguments) -> Obj
|
||||
)
|
||||
|
||||
fun bindGlobalFun(
|
||||
name: String,
|
||||
fn: suspend GlobalArgReader.() -> Obj
|
||||
)
|
||||
|
||||
fun bindGlobalVarRaw(
|
||||
name: String,
|
||||
get: suspend (scope: ScopeFacade) -> Obj,
|
||||
set: (suspend (scope: ScopeFacade, value: Obj) -> Unit)? = null
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reader helper for Kotlin-typed argument access.
|
||||
*/
|
||||
interface GlobalArgReader {
|
||||
val scope: ScopeFacade
|
||||
val args: Arguments
|
||||
val size: Int
|
||||
|
||||
fun requireExactCount(count: Int)
|
||||
fun obj(index: Int): Obj
|
||||
fun objOrNull(index: Int): Obj?
|
||||
fun int(index: Int): Int
|
||||
fun long(index: Int): Long
|
||||
fun double(index: Int): Double
|
||||
fun bool(index: Int): Boolean
|
||||
fun string(index: Int): String
|
||||
}
|
||||
|
||||
private class ModuleGlobalBinder(
|
||||
private val module: ModuleScope
|
||||
) : LyngGlobalBinder {
|
||||
|
||||
override fun bindGlobalFunRaw(
|
||||
name: String,
|
||||
fn: suspend (scope: ScopeFacade, args: Arguments) -> Obj
|
||||
) {
|
||||
val existing = module[name]
|
||||
val callable = ObjExternCallable.fromBridge {
|
||||
fn(this, args)
|
||||
}
|
||||
module.addItem(
|
||||
name = name,
|
||||
isMutable = false,
|
||||
value = callable,
|
||||
visibility = existing?.visibility ?: Visibility.Public,
|
||||
writeVisibility = existing?.writeVisibility,
|
||||
recordType = ObjRecord.Type.Fun,
|
||||
callSignature = existing?.callSignature,
|
||||
typeDecl = existing?.typeDecl
|
||||
)
|
||||
}
|
||||
|
||||
override fun bindGlobalFun(
|
||||
name: String,
|
||||
fn: suspend GlobalArgReader.() -> Obj
|
||||
) {
|
||||
bindGlobalFunRaw(name) { scope, args ->
|
||||
val reader = GlobalArgReaderImpl(scope, args)
|
||||
fn(reader)
|
||||
}
|
||||
}
|
||||
|
||||
override fun bindGlobalVarRaw(
|
||||
name: String,
|
||||
get: suspend (scope: ScopeFacade) -> Obj,
|
||||
set: (suspend (scope: ScopeFacade, value: Obj) -> Unit)?
|
||||
) {
|
||||
val existing = module[name]
|
||||
if (existing != null) {
|
||||
if (existing.isMutable && set == null) {
|
||||
throw net.sergeych.lyng.ScriptError(Pos.builtIn, "extern var $name requires a setter")
|
||||
}
|
||||
if (!existing.isMutable && set != null) {
|
||||
throw net.sergeych.lyng.ScriptError(Pos.builtIn, "extern val $name does not allow a setter")
|
||||
}
|
||||
}
|
||||
val mutable = existing?.isMutable ?: (set != null)
|
||||
val getter = ObjExternCallable.fromBridge {
|
||||
get(this)
|
||||
}
|
||||
val setter = set?.let { setterImpl ->
|
||||
ObjExternCallable.fromBridge {
|
||||
setterImpl(this, requiredArg(0))
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
module.addItem(
|
||||
name = name,
|
||||
isMutable = mutable,
|
||||
value = ObjProperty(name, getter, setter),
|
||||
visibility = existing?.visibility ?: Visibility.Public,
|
||||
writeVisibility = existing?.writeVisibility,
|
||||
recordType = ObjRecord.Type.Property,
|
||||
callSignature = existing?.callSignature,
|
||||
typeDecl = existing?.typeDecl
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private class GlobalArgReaderImpl(
|
||||
override val scope: ScopeFacade,
|
||||
override val args: Arguments
|
||||
) : GlobalArgReader {
|
||||
override val size: Int
|
||||
get() = args.list.size
|
||||
|
||||
override fun requireExactCount(count: Int) {
|
||||
if (size != count) scope.raiseIllegalArgument("Expected exactly $count arguments, got $size")
|
||||
}
|
||||
|
||||
override fun obj(index: Int): Obj =
|
||||
objOrNull(index) ?: scope.raiseIllegalArgument("Missing required argument at index $index")
|
||||
|
||||
override fun objOrNull(index: Int): Obj? =
|
||||
args.list.getOrNull(index)
|
||||
|
||||
override fun int(index: Int): Int = long(index).toInt()
|
||||
|
||||
override fun long(index: Int): Long = obj(index).toLong()
|
||||
|
||||
override fun double(index: Int): Double = obj(index).toDouble()
|
||||
|
||||
override fun bool(index: Int): Boolean = obj(index).toBool()
|
||||
|
||||
override fun string(index: Int): String {
|
||||
val value = obj(index)
|
||||
return (value as? ObjString)?.value
|
||||
?: scope.raiseClassCastError("Expected String at index $index, got ${value.objClass.className}")
|
||||
}
|
||||
}
|
||||
|
||||
fun ModuleScope.globalBinder(): LyngGlobalBinder = ModuleGlobalBinder(this)
|
||||
|
||||
inline fun <reified T> GlobalArgReader.required(index: Int): T =
|
||||
coerceArg(scope, obj(index), index)
|
||||
|
||||
inline fun <reified T> GlobalArgReader.optional(index: Int, default: T): T {
|
||||
val value = objOrNull(index) ?: return default
|
||||
return coerceArg(scope, value, index)
|
||||
}
|
||||
|
||||
inline fun <reified A1> LyngGlobalBinder.bindGlobalFun1(
|
||||
name: String,
|
||||
noinline fn: suspend (A1) -> Obj
|
||||
) {
|
||||
bindGlobalFun(name) {
|
||||
requireExactCount(1)
|
||||
fn(required(0))
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified A1, reified A2> LyngGlobalBinder.bindGlobalFun2(
|
||||
name: String,
|
||||
noinline fn: suspend (A1, A2) -> Obj
|
||||
) {
|
||||
bindGlobalFun(name) {
|
||||
requireExactCount(2)
|
||||
fn(required(0), required(1))
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified A1, reified A2, reified A3> LyngGlobalBinder.bindGlobalFun3(
|
||||
name: String,
|
||||
noinline fn: suspend (A1, A2, A3) -> Obj
|
||||
) {
|
||||
bindGlobalFun(name) {
|
||||
requireExactCount(3)
|
||||
fn(required(0), required(1), required(2))
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> LyngGlobalBinder.bindGlobalVar(
|
||||
name: String,
|
||||
noinline get: suspend () -> T,
|
||||
noinline set: (suspend (T) -> Unit)? = null
|
||||
) {
|
||||
bindGlobalVarRaw(
|
||||
name = name,
|
||||
get = { get().toObj() },
|
||||
set = set?.let { setter ->
|
||||
{ scope, value ->
|
||||
setter(coerceArg<T>(scope = scope, value = value, index = 0))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
internal inline fun <reified T> coerceArg(scope: ScopeFacade, value: Obj, index: Int): T {
|
||||
if (value === ObjNull && null is T) return null as T
|
||||
(value as? T)?.let { return it }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return when (T::class) {
|
||||
Int::class -> value.toInt() as T
|
||||
Long::class -> value.toLong() as T
|
||||
Double::class -> value.toDouble() as T
|
||||
Float::class -> value.toDouble().toFloat() as T
|
||||
Boolean::class -> value.toBool() as T
|
||||
String::class -> (value as? ObjString)?.value as? T
|
||||
?: scope.raiseClassCastError("Expected String at index $index, got ${value.objClass.className}")
|
||||
Obj::class -> value as T
|
||||
ObjInt::class -> (value as? ObjInt) as? T
|
||||
?: scope.raiseClassCastError("Expected ObjInt at index $index, got ${value.objClass.className}")
|
||||
ObjString::class -> (value as? ObjString) as? T
|
||||
?: scope.raiseClassCastError("Expected ObjString at index $index, got ${value.objClass.className}")
|
||||
ObjReal::class -> (value as? ObjReal) as? T
|
||||
?: scope.raiseClassCastError("Expected ObjReal at index $index, got ${value.objClass.className}")
|
||||
ObjBool::class -> (value as? ObjBool) as? T
|
||||
?: scope.raiseClassCastError("Expected ObjBool at index $index, got ${value.objClass.className}")
|
||||
else -> scope.raiseClassCastError("Unsupported typed argument binding for ${T::class.simpleName}")
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ class BytecodeCompiler(
|
||||
private val slotTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(),
|
||||
private val slotTypeDeclByScopeId: Map<Int, Map<Int, TypeDecl>> = emptyMap(),
|
||||
private val knownNameObjClass: Map<String, ObjClass> = emptyMap(),
|
||||
private val knownClassNames: Set<String> = emptySet(),
|
||||
private val knownObjectNames: Set<String> = emptySet(),
|
||||
private val classFieldTypesByName: Map<String, Map<String, ObjClass>> = emptyMap(),
|
||||
private val enumEntriesByName: Map<String, List<String>> = emptyMap(),
|
||||
@@ -78,7 +79,6 @@ class BytecodeCompiler(
|
||||
private val stableObjSlots = mutableSetOf<Int>()
|
||||
private val nameObjClass = knownNameObjClass.toMutableMap()
|
||||
private val listElementClassBySlot = mutableMapOf<Int, ObjClass>()
|
||||
private val knownClassNames = knownNameObjClass.keys.toSet()
|
||||
private val slotInitClassByKey = mutableMapOf<ScopeSlotKey, ObjClass>()
|
||||
private val intLoopVarNames = LinkedHashSet<String>()
|
||||
private val valueFnRefs = LinkedHashSet<ValueFnRef>()
|
||||
@@ -522,7 +522,7 @@ class BytecodeCompiler(
|
||||
}
|
||||
}
|
||||
if (resolved == SlotType.UNKNOWN) {
|
||||
val inferred = slotTypeFromClass(nameObjClass[ref.name])
|
||||
val inferred = if (knownClassNames.contains(ref.name)) null else slotTypeFromClass(nameObjClass[ref.name])
|
||||
if (inferred != null) {
|
||||
updateSlotType(mapped, inferred)
|
||||
resolved = inferred
|
||||
@@ -1638,7 +1638,7 @@ class BytecodeCompiler(
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
//else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3268,9 +3268,10 @@ class BytecodeCompiler(
|
||||
if (ref.name.isBlank()) {
|
||||
return compileRefWithFallback(ref.target, null, Pos.builtIn)
|
||||
}
|
||||
val pos = callSitePos()
|
||||
val receiverClass = resolveReceiverClass(ref.target) ?: ObjDynamic.type
|
||||
if (receiverClass == ObjDynamic.type) {
|
||||
val receiver = compileRefWithFallback(ref.target, null, Pos.builtIn) ?: return null
|
||||
val receiver = compileRefWithFallback(ref.target, null, pos) ?: return null
|
||||
val dst = allocSlot()
|
||||
val nameId = builder.addConst(BytecodeConst.StringVal(ref.name))
|
||||
if (!ref.isOptional) {
|
||||
@@ -3296,7 +3297,7 @@ class BytecodeCompiler(
|
||||
return CompiledValue(dst, SlotType.OBJ)
|
||||
}
|
||||
if (receiverClass is ObjInstanceClass && !isThisReceiver(ref.target)) {
|
||||
val receiver = compileRefWithFallback(ref.target, null, Pos.builtIn) ?: return null
|
||||
val receiver = compileRefWithFallback(ref.target, null, pos) ?: return null
|
||||
val dst = allocSlot()
|
||||
val nameId = builder.addConst(BytecodeConst.StringVal(ref.name))
|
||||
if (!ref.isOptional) {
|
||||
@@ -3323,7 +3324,7 @@ class BytecodeCompiler(
|
||||
}
|
||||
val resolvedMember = receiverClass.resolveInstanceMember(ref.name)
|
||||
if (resolvedMember?.declaringClass?.className == "Obj") {
|
||||
val receiver = compileRefWithFallback(ref.target, null, Pos.builtIn) ?: return null
|
||||
val receiver = compileRefWithFallback(ref.target, null, pos) ?: return null
|
||||
val dst = allocSlot()
|
||||
val nameId = builder.addConst(BytecodeConst.StringVal(ref.name))
|
||||
if (!ref.isOptional) {
|
||||
@@ -3352,7 +3353,7 @@ class BytecodeCompiler(
|
||||
val methodId = if (resolvedMember != null) receiverClass.instanceMethodIdMap(includeAbstract = true)[ref.name] else null
|
||||
val encodedFieldId = encodeMemberId(receiverClass, fieldId)
|
||||
val encodedMethodId = encodeMemberId(receiverClass, methodId)
|
||||
val receiver = compileRefWithFallback(ref.target, null, Pos.builtIn) ?: return null
|
||||
val receiver = compileRefWithFallback(ref.target, null, pos) ?: return null
|
||||
val dst = allocSlot()
|
||||
if (fieldId == null && methodId == null && (isKnownClassReceiver(ref.target) || isClassNameRef(ref.target, receiverClass)) &&
|
||||
(isClassSlot(receiver.slot) || receiverClass == ObjClassType)
|
||||
@@ -4344,6 +4345,22 @@ class BytecodeCompiler(
|
||||
CompiledValue(neg, SlotType.BOOL)
|
||||
}
|
||||
}
|
||||
is WhenNullableCondition -> {
|
||||
val nullSlot = allocSlot()
|
||||
builder.emit(Opcode.CONST_NULL, nullSlot)
|
||||
updateSlotType(nullSlot, SlotType.OBJ)
|
||||
val baseDst = allocSlot()
|
||||
builder.emit(Opcode.CHECK_IS, nullSlot, subject.slot, baseDst)
|
||||
updateSlotType(baseDst, SlotType.BOOL)
|
||||
if (!cond.negated) {
|
||||
CompiledValue(baseDst, SlotType.BOOL)
|
||||
} else {
|
||||
val neg = allocSlot()
|
||||
builder.emit(Opcode.NOT_BOOL, baseDst, neg)
|
||||
updateSlotType(neg, SlotType.BOOL)
|
||||
CompiledValue(neg, SlotType.BOOL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4394,7 +4411,7 @@ class BytecodeCompiler(
|
||||
val dst = allocSlot()
|
||||
val encodedMethodId = encodeMemberId(receiverClass, methodId) ?: methodId
|
||||
if (!ref.isOptionalInvoke) {
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock) ?: return null
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock, ref.explicitTypeArgs) ?: return null
|
||||
val encodedCount = encodeCallArgCount(args) ?: return null
|
||||
setPos(callPos)
|
||||
builder.emit(Opcode.CALL_MEMBER_SLOT, receiver.slot, encodedMethodId, args.base, encodedCount, dst)
|
||||
@@ -4409,7 +4426,7 @@ class BytecodeCompiler(
|
||||
Opcode.JMP_IF_TRUE,
|
||||
listOf(CmdBuilder.Operand.IntVal(cmpSlot), CmdBuilder.Operand.LabelRef(nullLabel))
|
||||
)
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock) ?: return null
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock, ref.explicitTypeArgs) ?: return null
|
||||
val encodedCount = encodeCallArgCount(args) ?: return null
|
||||
setPos(callPos)
|
||||
builder.emit(Opcode.CALL_MEMBER_SLOT, receiver.slot, encodedMethodId, args.base, encodedCount, dst)
|
||||
@@ -4438,13 +4455,18 @@ class BytecodeCompiler(
|
||||
}
|
||||
val initClass = when (localTarget?.name) {
|
||||
"List" -> ObjList.type
|
||||
"ImmutableList" -> ObjImmutableList.type
|
||||
"ObservableList" -> ObjObservableList.type
|
||||
"Map" -> ObjMap.type
|
||||
"ImmutableMap" -> ObjImmutableMap.type
|
||||
"Set" -> ObjSet.type
|
||||
"ImmutableSet" -> ObjImmutableSet.type
|
||||
else -> null
|
||||
}
|
||||
val callee = compileRefWithFallback(ref.target, null, refPosOrCurrent(ref.target)) ?: return null
|
||||
val dst = allocSlot()
|
||||
if (!ref.isOptionalInvoke) {
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock) ?: return null
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock, ref.explicitTypeArgs) ?: return null
|
||||
val encodedCount = encodeCallArgCount(args) ?: return null
|
||||
setPos(callPos)
|
||||
builder.emit(
|
||||
@@ -4469,7 +4491,7 @@ class BytecodeCompiler(
|
||||
Opcode.JMP_IF_TRUE,
|
||||
listOf(CmdBuilder.Operand.IntVal(cmpSlot), CmdBuilder.Operand.LabelRef(nullLabel))
|
||||
)
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock) ?: return null
|
||||
val args = compileCallArgs(ref.args, ref.tailBlock, ref.explicitTypeArgs) ?: return null
|
||||
val encodedCount = encodeCallArgCount(args) ?: return null
|
||||
setPos(callPos)
|
||||
builder.emit(
|
||||
@@ -4870,10 +4892,14 @@ class BytecodeCompiler(
|
||||
return CallArgs(base = argSlots[0], count = argSlots.size, planId = planId)
|
||||
}
|
||||
|
||||
private fun compileCallArgs(args: List<ParsedArgument>, tailBlock: Boolean): CallArgs? {
|
||||
if (args.isEmpty()) return CallArgs(base = 0, count = 0, planId = null)
|
||||
private fun compileCallArgs(
|
||||
args: List<ParsedArgument>,
|
||||
tailBlock: Boolean,
|
||||
explicitTypeArgs: List<TypeDecl>? = null
|
||||
): CallArgs? {
|
||||
if (args.isEmpty() && explicitTypeArgs.isNullOrEmpty()) return CallArgs(base = 0, count = 0, planId = null)
|
||||
val argSlots = IntArray(args.size) { allocSlot() }
|
||||
val needPlan = tailBlock || args.any { it.isSplat || it.name != null }
|
||||
val needPlan = tailBlock || args.any { it.isSplat || it.name != null } || !explicitTypeArgs.isNullOrEmpty()
|
||||
val specs = if (needPlan) ArrayList<BytecodeConst.CallArgSpec>(args.size) else null
|
||||
for ((index, arg) in args.withIndex()) {
|
||||
val compiled = compileArgValue(arg.value) ?: return null
|
||||
@@ -4885,11 +4911,17 @@ class BytecodeCompiler(
|
||||
specs?.add(BytecodeConst.CallArgSpec(arg.name, arg.isSplat))
|
||||
}
|
||||
val planId = if (needPlan) {
|
||||
builder.addConst(BytecodeConst.CallArgsPlan(tailBlock, specs ?: emptyList()))
|
||||
builder.addConst(
|
||||
BytecodeConst.CallArgsPlan(
|
||||
tailBlock = tailBlock,
|
||||
specs = specs ?: emptyList(),
|
||||
explicitTypeArgs = explicitTypeArgs ?: emptyList()
|
||||
)
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
return CallArgs(base = argSlots[0], count = argSlots.size, planId = planId)
|
||||
return CallArgs(base = if (argSlots.isEmpty()) 0 else argSlots[0], count = argSlots.size, planId = planId)
|
||||
}
|
||||
|
||||
private fun compileArgValue(value: Obj): CompiledValue? {
|
||||
@@ -5090,9 +5122,7 @@ class BytecodeCompiler(
|
||||
lifted = stmt.lifted
|
||||
)
|
||||
)
|
||||
val dst = stmt.declaredName?.let { name ->
|
||||
resolveDirectNameSlot(name)?.slot
|
||||
} ?: allocSlot()
|
||||
val dst = resolveDirectNameSlot(stmt.declaredName)?.slot ?: allocSlot()
|
||||
builder.emit(Opcode.DECL_ENUM, constId, dst)
|
||||
updateSlotType(dst, SlotType.OBJ)
|
||||
return CompiledValue(dst, SlotType.OBJ)
|
||||
@@ -5175,6 +5205,7 @@ class BytecodeCompiler(
|
||||
isMutable = stmt.isMutable,
|
||||
visibility = stmt.visibility,
|
||||
writeVisibility = stmt.writeVisibility,
|
||||
typeDecl = stmt.typeDecl,
|
||||
isTransient = stmt.isTransient,
|
||||
isAbstract = stmt.isAbstract,
|
||||
isClosed = stmt.isClosed,
|
||||
@@ -5357,7 +5388,6 @@ class BytecodeCompiler(
|
||||
is net.sergeych.lyng.ContinueStatement -> compileContinue(target)
|
||||
is net.sergeych.lyng.ReturnStatement -> compileReturn(target)
|
||||
is net.sergeych.lyng.ThrowStatement -> compileThrow(target)
|
||||
is net.sergeych.lyng.TryStatement -> emitTry(target, false)
|
||||
is net.sergeych.lyng.NopStatement -> {
|
||||
val slot = allocSlot()
|
||||
val voidId = builder.addConst(BytecodeConst.ObjRef(ObjVoid))
|
||||
@@ -5733,7 +5763,8 @@ class BytecodeCompiler(
|
||||
stmt.name,
|
||||
stmt.isMutable,
|
||||
stmt.visibility,
|
||||
stmt.isTransient
|
||||
stmt.isTransient,
|
||||
stmt.typeDecl
|
||||
)
|
||||
)
|
||||
builder.emit(Opcode.DECL_LOCAL, declId, localSlot)
|
||||
@@ -5760,7 +5791,8 @@ class BytecodeCompiler(
|
||||
stmt.name,
|
||||
stmt.isMutable,
|
||||
stmt.visibility,
|
||||
stmt.isTransient
|
||||
stmt.isTransient,
|
||||
stmt.typeDecl
|
||||
)
|
||||
)
|
||||
builder.emit(Opcode.DECL_LOCAL, declId, scopeSlot)
|
||||
@@ -5778,7 +5810,8 @@ class BytecodeCompiler(
|
||||
stmt.name,
|
||||
stmt.isMutable,
|
||||
stmt.visibility,
|
||||
stmt.isTransient
|
||||
stmt.isTransient,
|
||||
stmt.typeDecl
|
||||
)
|
||||
)
|
||||
builder.emit(Opcode.DECL_LOCAL, declId, value.slot)
|
||||
@@ -6947,7 +6980,6 @@ class BytecodeCompiler(
|
||||
is LocalVarRef -> ref.name
|
||||
is FastLocalVarRef -> ref.name
|
||||
is LocalSlotRef -> ref.name
|
||||
else -> "unknown"
|
||||
}
|
||||
val refKind = ref::class.simpleName ?: "LocalRef"
|
||||
val loopKeys = loopSlotOverrides.keys.sorted().joinToString(prefix = "[", postfix = "]")
|
||||
@@ -6998,7 +7030,9 @@ class BytecodeCompiler(
|
||||
val slot = resolveSlot(ref)
|
||||
val fromSlot = slot?.let { slotObjClass[it] }
|
||||
fromSlot
|
||||
?: slot?.let { typeDeclForSlot(it) }?.let { resolveClassFromTypeDecl(it) }
|
||||
?: slotTypeByScopeId[ownerScopeId]?.get(ownerSlot)
|
||||
?: slotTypeDeclByScopeId[ownerScopeId]?.get(ownerSlot)?.let { resolveClassFromTypeDecl(it) }
|
||||
?: nameObjClass[ref.name]
|
||||
?: resolveTypeNameClass(ref.name)
|
||||
?: slotInitClassByKey[ScopeSlotKey(ownerScopeId, ownerSlot)]
|
||||
@@ -7016,9 +7050,14 @@ class BytecodeCompiler(
|
||||
}
|
||||
val fromSlot = resolveDirectNameSlot(ref.name)?.let { slotObjClass[it.slot] }
|
||||
if (fromSlot != null) return fromSlot
|
||||
val fromDirectTypeDecl = resolveDirectNameSlot(ref.name)
|
||||
?.let { typeDeclForSlot(it.slot) }
|
||||
?.let { resolveClassFromTypeDecl(it) }
|
||||
if (fromDirectTypeDecl != null) return fromDirectTypeDecl
|
||||
val key = localSlotInfoMap.entries.firstOrNull { it.value.name == ref.name }?.key
|
||||
key?.let {
|
||||
slotTypeByScopeId[it.scopeId]?.get(it.slot)
|
||||
?: slotTypeDeclByScopeId[it.scopeId]?.get(it.slot)?.let { decl -> resolveClassFromTypeDecl(decl) }
|
||||
?: slotInitClassByKey[it]
|
||||
} ?: nameObjClass[ref.name]
|
||||
?: resolveTypeNameClass(ref.name)
|
||||
@@ -7029,9 +7068,14 @@ class BytecodeCompiler(
|
||||
}
|
||||
val fromSlot = resolveDirectNameSlot(ref.name)?.let { slotObjClass[it.slot] }
|
||||
if (fromSlot != null) return fromSlot
|
||||
val fromDirectTypeDecl = resolveDirectNameSlot(ref.name)
|
||||
?.let { typeDeclForSlot(it.slot) }
|
||||
?.let { resolveClassFromTypeDecl(it) }
|
||||
if (fromDirectTypeDecl != null) return fromDirectTypeDecl
|
||||
val key = localSlotInfoMap.entries.firstOrNull { it.value.name == ref.name }?.key
|
||||
key?.let {
|
||||
slotTypeByScopeId[it.scopeId]?.get(it.slot)
|
||||
?: slotTypeDeclByScopeId[it.scopeId]?.get(it.slot)?.let { decl -> resolveClassFromTypeDecl(decl) }
|
||||
?: slotInitClassByKey[it]
|
||||
} ?: nameObjClass[ref.name]
|
||||
?: resolveTypeNameClass(ref.name)
|
||||
@@ -7073,6 +7117,23 @@ class BytecodeCompiler(
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveClassFromTypeDecl(typeDecl: TypeDecl): ObjClass? {
|
||||
return when (typeDecl) {
|
||||
is TypeDecl.Simple -> {
|
||||
resolveTypeNameClass(typeDecl.name) ?: nameObjClass[typeDecl.name]?.let { cls ->
|
||||
if (cls == ObjClassType) ObjDynamic.type else cls
|
||||
}
|
||||
}
|
||||
is TypeDecl.Generic -> {
|
||||
resolveTypeNameClass(typeDecl.name) ?: nameObjClass[typeDecl.name]?.let { cls ->
|
||||
if (cls == ObjClassType) ObjDynamic.type else cls
|
||||
}
|
||||
}
|
||||
is TypeDecl.Ellipsis -> resolveClassFromTypeDecl(typeDecl.elementType)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun isKnownClassReceiver(ref: ObjRef): Boolean {
|
||||
return when (ref) {
|
||||
is LocalVarRef -> {
|
||||
@@ -7274,8 +7335,11 @@ class BytecodeCompiler(
|
||||
"Bool" -> ObjBool.type
|
||||
"Char" -> ObjChar.type
|
||||
"List" -> ObjList.type
|
||||
"ImmutableList" -> ObjImmutableList.type
|
||||
"Map" -> ObjMap.type
|
||||
"ImmutableMap" -> ObjImmutableMap.type
|
||||
"Set" -> ObjSet.type
|
||||
"ImmutableSet" -> ObjImmutableSet.type
|
||||
"Range", "IntRange" -> ObjRange.type
|
||||
"Iterator" -> ObjIterator
|
||||
"Iterable" -> ObjIterable
|
||||
@@ -7289,6 +7353,16 @@ class BytecodeCompiler(
|
||||
"Regex" -> ObjRegex.type
|
||||
"RegexMatch" -> ObjRegexMatch.type
|
||||
"MapEntry" -> ObjMapEntry.type
|
||||
"Observable" -> ObjObservable
|
||||
"Subscription" -> ObjSubscription.type
|
||||
"ListChange" -> ObjListChange.type
|
||||
"ListSet" -> ObjListSetChange.type
|
||||
"ListInsert" -> ObjListInsertChange.type
|
||||
"ListRemove" -> ObjListRemoveChange.type
|
||||
"ListClear" -> ObjListClearChange.type
|
||||
"ListReorder" -> ObjListReorderChange.type
|
||||
"ObservableList" -> ObjObservableList.type
|
||||
"ChangeRejectionException" -> ObjChangeRejectionExceptionClass
|
||||
"Instant" -> ObjInstant.type
|
||||
"DateTime" -> ObjDateTime.type
|
||||
"Duration" -> ObjDuration.type
|
||||
@@ -7349,7 +7423,12 @@ class BytecodeCompiler(
|
||||
"iterator" -> ObjIterator
|
||||
"count" -> ObjInt.type
|
||||
"toSet" -> ObjSet.type
|
||||
"toImmutableSet" -> ObjImmutableSet.type
|
||||
"toMap" -> ObjMap.type
|
||||
"toImmutableMap" -> ObjImmutableMap.type
|
||||
"observable" -> ObjObservableList.type
|
||||
"beforeChange", "onChange" -> ObjSubscription.type
|
||||
"changes" -> ObjFlow.type
|
||||
"joinToString" -> ObjString.type
|
||||
"now",
|
||||
"truncateToSecond",
|
||||
@@ -7376,6 +7455,8 @@ class BytecodeCompiler(
|
||||
"matches" -> ObjBool.type
|
||||
"toInt",
|
||||
"toEpochSeconds" -> ObjInt.type
|
||||
"toImmutableList" -> ObjImmutableList.type
|
||||
"toImmutable" -> Obj.rootObjectType
|
||||
"toMutable" -> ObjMutableBuffer.type
|
||||
"seq" -> ObjFlow.type
|
||||
"encode" -> ObjBitBuffer.type
|
||||
@@ -7695,7 +7776,7 @@ class BytecodeCompiler(
|
||||
for (ref in valueFnRefs) {
|
||||
val entries = lambdaCaptureEntriesByRef[ref] ?: continue
|
||||
for (entry in entries) {
|
||||
if (entry.ownerKind != CaptureOwnerFrameKind.LOCAL) continue
|
||||
if (entry.ownerKind == CaptureOwnerFrameKind.LOCAL) {
|
||||
val key = ScopeSlotKey(entry.ownerScopeId, entry.ownerSlotId)
|
||||
if (!localSlotInfoMap.containsKey(key)) {
|
||||
localSlotInfoMap[key] = LocalSlotInfo(
|
||||
@@ -7704,6 +7785,20 @@ class BytecodeCompiler(
|
||||
entry.ownerIsDelegated
|
||||
)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if (entry.ownerKind == CaptureOwnerFrameKind.MODULE) {
|
||||
val key = ScopeSlotKey(entry.ownerScopeId, entry.ownerSlotId)
|
||||
if (!scopeSlotMap.containsKey(key)) {
|
||||
scopeSlotMap[key] = scopeSlotMap.size
|
||||
}
|
||||
if (!scopeSlotNameMap.containsKey(key)) {
|
||||
scopeSlotNameMap[key] = entry.ownerName
|
||||
}
|
||||
if (!scopeSlotMutableMap.containsKey(key)) {
|
||||
scopeSlotMutableMap[key] = entry.ownerIsMutable
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8171,7 +8266,8 @@ class BytecodeCompiler(
|
||||
}
|
||||
|
||||
private fun isModuleSlot(scopeId: Int, name: String?): Boolean {
|
||||
val scopeNames = scopeSlotNameSet ?: allowedScopeNames
|
||||
if (moduleScopeId != null && scopeId != moduleScopeId) return false
|
||||
val scopeNames = allowedScopeNames ?: scopeSlotNameSet
|
||||
if (scopeNames == null || name == null) return false
|
||||
return scopeNames.contains(name)
|
||||
}
|
||||
@@ -8394,6 +8490,9 @@ class BytecodeCompiler(
|
||||
collectScopeSlotsRef(ref.targetRef)
|
||||
collectScopeSlotsRef(ref.indexRef)
|
||||
}
|
||||
is ClassOperatorRef -> {
|
||||
collectScopeSlotsRef(ref.target)
|
||||
}
|
||||
is ListLiteralRef -> {
|
||||
for (entry in ref.entries()) {
|
||||
when (entry) {
|
||||
@@ -8445,9 +8544,6 @@ class BytecodeCompiler(
|
||||
is StatementRef -> {
|
||||
collectScopeSlots(ref.statement)
|
||||
}
|
||||
is ImplicitThisMethodCallRef -> {
|
||||
collectScopeSlotsArgs(ref.arguments())
|
||||
}
|
||||
is ThisMethodSlotCallRef -> {
|
||||
collectScopeSlotsArgs(ref.arguments())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,12 +12,14 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.bytecode
|
||||
|
||||
import net.sergeych.lyng.ArgsDeclaration
|
||||
import net.sergeych.lyng.Pos
|
||||
import net.sergeych.lyng.TypeDecl
|
||||
import net.sergeych.lyng.Visibility
|
||||
import net.sergeych.lyng.obj.ListLiteralRef
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
@@ -68,6 +70,7 @@ sealed class BytecodeConst {
|
||||
val isMutable: Boolean,
|
||||
val visibility: Visibility,
|
||||
val isTransient: Boolean,
|
||||
val typeDecl: TypeDecl?,
|
||||
) : BytecodeConst()
|
||||
data class DelegatedDecl(
|
||||
val name: String,
|
||||
@@ -97,6 +100,7 @@ sealed class BytecodeConst {
|
||||
val isMutable: Boolean,
|
||||
val visibility: Visibility,
|
||||
val writeVisibility: Visibility?,
|
||||
val typeDecl: TypeDecl?,
|
||||
val isTransient: Boolean,
|
||||
val isAbstract: Boolean,
|
||||
val isClosed: Boolean,
|
||||
@@ -176,6 +180,10 @@ sealed class BytecodeConst {
|
||||
val pattern: ListLiteralRef,
|
||||
val pos: Pos,
|
||||
) : BytecodeConst()
|
||||
data class CallArgsPlan(val tailBlock: Boolean, val specs: List<CallArgSpec>) : BytecodeConst()
|
||||
data class CallArgsPlan(
|
||||
val tailBlock: Boolean,
|
||||
val specs: List<CallArgSpec>,
|
||||
val explicitTypeArgs: List<TypeDecl> = emptyList()
|
||||
) : BytecodeConst()
|
||||
data class CallArgSpec(val name: String?, val isSplat: Boolean)
|
||||
}
|
||||
@@ -34,6 +34,17 @@ class BytecodeFrame(
|
||||
private val realSlots: DoubleArray = DoubleArray(slotCount)
|
||||
private val boolSlots: BooleanArray = BooleanArray(slotCount)
|
||||
|
||||
internal fun copyTo(target: BytecodeFrame) {
|
||||
val limit = minOf(slotCount, target.slotCount)
|
||||
for (i in 0 until limit) {
|
||||
target.slotTypes[i] = slotTypes[i]
|
||||
target.objSlots[i] = objSlots[i]
|
||||
target.intSlots[i] = intSlots[i]
|
||||
target.realSlots[i] = realSlots[i]
|
||||
target.boolSlots[i] = boolSlots[i]
|
||||
}
|
||||
}
|
||||
|
||||
fun getSlotType(slot: Int): SlotType = SlotType.values().first { it.code == slotTypes[slot] }
|
||||
override fun getSlotTypeCode(slot: Int): Byte = slotTypes[slot]
|
||||
fun setSlotType(slot: Int, type: SlotType) {
|
||||
|
||||
@@ -20,6 +20,7 @@ package net.sergeych.lyng.bytecode
|
||||
import net.sergeych.lyng.*
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjClass
|
||||
import net.sergeych.lyng.obj.ObjRecord
|
||||
import net.sergeych.lyng.obj.ValueFnRef
|
||||
|
||||
class BytecodeStatement private constructor(
|
||||
@@ -30,7 +31,34 @@ class BytecodeStatement private constructor(
|
||||
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
scope.pos = pos
|
||||
return CmdVm().execute(function, scope, scope.args)
|
||||
val declaredNames = function.constants
|
||||
.mapNotNull { it as? BytecodeConst.LocalDecl }
|
||||
.mapTo(mutableSetOf()) { it.name }
|
||||
val binder: suspend (CmdFrame, Arguments) -> Unit = { frame, _ ->
|
||||
val localNames = frame.fn.localSlotNames
|
||||
for (i in localNames.indices) {
|
||||
val name = localNames[i] ?: continue
|
||||
if (declaredNames.contains(name)) continue
|
||||
val slotType = frame.getLocalSlotTypeCode(i)
|
||||
if (slotType != SlotType.UNKNOWN.code && slotType != SlotType.OBJ.code) {
|
||||
continue
|
||||
}
|
||||
if (slotType == SlotType.OBJ.code && frame.frame.getRawObj(i) != null) {
|
||||
continue
|
||||
}
|
||||
val record = scope.getLocalRecordDirect(name)
|
||||
?: scope.parent?.get(name)
|
||||
?: scope.get(name)
|
||||
?: continue
|
||||
val value = if (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property) {
|
||||
scope.resolve(record, name)
|
||||
} else {
|
||||
record.value
|
||||
}
|
||||
frame.frame.setObj(i, value)
|
||||
}
|
||||
}
|
||||
return CmdVm().execute(function, scope, scope.args, binder)
|
||||
}
|
||||
|
||||
internal fun bytecodeFunction(): CmdFunction = function
|
||||
@@ -52,6 +80,7 @@ class BytecodeStatement private constructor(
|
||||
globalSlotScopeId: Int? = null,
|
||||
slotTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(),
|
||||
knownNameObjClass: Map<String, ObjClass> = emptyMap(),
|
||||
knownClassNames: Set<String> = emptySet(),
|
||||
knownObjectNames: Set<String> = emptySet(),
|
||||
classFieldTypesByName: Map<String, Map<String, ObjClass>> = emptyMap(),
|
||||
enumEntriesByName: Map<String, List<String>> = emptyMap(),
|
||||
@@ -86,6 +115,7 @@ class BytecodeStatement private constructor(
|
||||
slotTypeByScopeId = slotTypeByScopeId,
|
||||
slotTypeDeclByScopeId = slotTypeDeclByScopeId,
|
||||
knownNameObjClass = knownNameObjClass,
|
||||
knownClassNames = knownClassNames,
|
||||
knownObjectNames = knownObjectNames,
|
||||
classFieldTypesByName = classFieldTypesByName,
|
||||
enumEntriesByName = enumEntriesByName,
|
||||
@@ -208,6 +238,7 @@ class BytecodeStatement private constructor(
|
||||
stmt.visibility,
|
||||
stmt.initializer?.let { unwrapDeep(it) },
|
||||
stmt.isTransient,
|
||||
stmt.typeDecl,
|
||||
stmt.slotIndex,
|
||||
stmt.scopeId,
|
||||
stmt.pos,
|
||||
@@ -317,6 +348,7 @@ class BytecodeStatement private constructor(
|
||||
stmt.isMutable,
|
||||
stmt.visibility,
|
||||
stmt.writeVisibility,
|
||||
stmt.typeDecl,
|
||||
stmt.isAbstract,
|
||||
stmt.isClosed,
|
||||
stmt.isOverride,
|
||||
@@ -411,6 +443,7 @@ class BytecodeStatement private constructor(
|
||||
is WhenEqualsCondition -> WhenEqualsCondition(unwrapDeep(cond.expr), cond.pos)
|
||||
is WhenInCondition -> WhenInCondition(unwrapDeep(cond.expr), cond.negated, cond.pos)
|
||||
is WhenIsCondition -> WhenIsCondition(unwrapDeep(cond.expr), cond.negated, cond.pos)
|
||||
is WhenNullableCondition -> WhenNullableCondition(cond.negated, cond.pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -502,7 +502,6 @@ object CmdDisassembler {
|
||||
is CmdIterPush -> Opcode.ITER_PUSH to intArrayOf(cmd.iterSlot)
|
||||
is CmdIterPop -> Opcode.ITER_POP to intArrayOf()
|
||||
is CmdIterCancel -> Opcode.ITER_CANCEL to intArrayOf()
|
||||
else -> error("Unsupported cmd in disassembler: ${cmd::class.simpleName}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -229,8 +229,29 @@ class CmdLoadThisVariant(
|
||||
val typeConst = frame.fn.constants.getOrNull(typeId) as? BytecodeConst.StringVal
|
||||
?: error("LOAD_THIS_VARIANT expects StringVal at $typeId")
|
||||
val typeName = typeConst.value
|
||||
val receiver = frame.ensureScope().thisVariants.firstOrNull { it.isInstanceOf(typeName) }
|
||||
?: frame.ensureScope().raiseClassCastError("Cannot cast ${frame.ensureScope().thisObj.objClass.className} to $typeName")
|
||||
val scope = frame.ensureScope()
|
||||
if (scope.thisVariants.isEmpty() || scope.thisVariants.firstOrNull() !== scope.thisObj) {
|
||||
scope.setThisVariants(scope.thisObj, scope.thisVariants)
|
||||
}
|
||||
val receiver = scope.thisVariants.firstOrNull { it.isInstanceOf(typeName) }
|
||||
?: run {
|
||||
if (scope.thisObj.isInstanceOf(typeName)) return@run scope.thisObj
|
||||
val typeClass = scope[typeName]?.value as? net.sergeych.lyng.obj.ObjClass
|
||||
var s: Scope? = scope
|
||||
while (s != null) {
|
||||
val candidate = s.thisObj
|
||||
if (candidate.isInstanceOf(typeName)) return@run candidate
|
||||
if (typeClass != null) {
|
||||
val inst = candidate as? net.sergeych.lyng.obj.ObjInstance
|
||||
if (inst != null && (inst.objClass === typeClass || inst.objClass.allParentsSet.contains(typeClass))) {
|
||||
return@run inst
|
||||
}
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
val variants = scope.thisVariants.joinToString { it.objClass.className }
|
||||
scope.raiseClassCastError("Cannot cast ${scope.thisObj.objClass.className} to $typeName (variants: $variants)")
|
||||
}
|
||||
frame.setObj(dst, receiver)
|
||||
return
|
||||
}
|
||||
@@ -2380,7 +2401,8 @@ class CmdDeclLocal(internal val constId: Int, internal val slot: Int) : Cmd() {
|
||||
decl.isMutable,
|
||||
decl.visibility,
|
||||
isTransient = decl.isTransient,
|
||||
type = ObjRecord.Type.Other
|
||||
type = ObjRecord.Type.Other,
|
||||
typeDecl = decl.typeDecl
|
||||
)
|
||||
)
|
||||
return
|
||||
@@ -2392,16 +2414,15 @@ class CmdDeclLocal(internal val constId: Int, internal val slot: Int) : Cmd() {
|
||||
decl.isMutable,
|
||||
decl.visibility,
|
||||
isTransient = decl.isTransient,
|
||||
type = ObjRecord.Type.Other
|
||||
type = ObjRecord.Type.Other,
|
||||
typeDecl = decl.typeDecl
|
||||
)
|
||||
val moduleScope = frame.scope as? ModuleScope
|
||||
if (moduleScope != null) {
|
||||
moduleScope.updateSlotFor(decl.name, record)
|
||||
moduleScope.objects[decl.name] = record
|
||||
moduleScope.localBindings[decl.name] = record
|
||||
} else if (frame.fn.name == "<script>") {
|
||||
val target = frame.ensureScope()
|
||||
target.updateSlotFor(decl.name, record)
|
||||
target.objects[decl.name] = record
|
||||
target.localBindings[decl.name] = record
|
||||
}
|
||||
@@ -2538,6 +2559,12 @@ private fun buildFunctionCaptureRecords(frame: CmdFrame, captureNames: List<Stri
|
||||
it.delegate = delegate
|
||||
}
|
||||
} else {
|
||||
val raw = frame.frame.getRawObj(localIndex)
|
||||
val scoped = frame.scope.chainLookupIgnoreClosure(name, followClosure = true) ?: frame.scope.get(name)
|
||||
if (scoped != null && scoped.value !== ObjUnset) {
|
||||
records += scoped
|
||||
continue
|
||||
}
|
||||
records += ObjRecord(FrameSlotRef(frame.frame, localIndex), isMutable)
|
||||
}
|
||||
continue
|
||||
@@ -2561,6 +2588,11 @@ private fun buildFunctionCaptureRecords(frame: CmdFrame, captureNames: List<Stri
|
||||
}
|
||||
}
|
||||
}
|
||||
val scoped = frame.scope.chainLookupIgnoreClosure(name, followClosure = true) ?: frame.scope.get(name)
|
||||
if (scoped != null) {
|
||||
records += ObjRecord(RecordSlotRef(scoped), isMutable = scoped.isMutable)
|
||||
continue
|
||||
}
|
||||
frame.ensureScope().raiseSymbolNotFound("capture $name not found")
|
||||
}
|
||||
return records
|
||||
@@ -2577,6 +2609,17 @@ class CmdDeclClass(internal val constId: Int, internal val slot: Int) : Cmd() {
|
||||
val bodyCaptureRecords = buildFunctionCaptureRecords(frame, bodyCaptureNames)
|
||||
val result = executeClassDecl(frame.ensureScope(), decl.spec, bodyCaptureRecords, bodyCaptureNames)
|
||||
frame.setObjUnchecked(slot, result)
|
||||
val name = decl.spec.declaredName ?: return
|
||||
val moduleScope = frame.scope as? ModuleScope ?: return
|
||||
val record = ObjRecord(
|
||||
result,
|
||||
isMutable = false,
|
||||
visibility = net.sergeych.lyng.Visibility.Public,
|
||||
type = ObjRecord.Type.Other
|
||||
)
|
||||
moduleScope.updateSlotFor(name, record)
|
||||
moduleScope.objects[name] = record
|
||||
moduleScope.localBindings[name] = record
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -2707,6 +2750,7 @@ class CmdDeclClassInstanceField(internal val constId: Int, internal val slot: In
|
||||
isClosed = decl.isClosed,
|
||||
isOverride = decl.isOverride,
|
||||
isTransient = decl.isTransient,
|
||||
typeDecl = decl.typeDecl,
|
||||
type = ObjRecord.Type.Field,
|
||||
fieldId = decl.fieldId
|
||||
)
|
||||
@@ -3227,6 +3271,7 @@ class CmdGetMemberSlot(
|
||||
internal val dst: Int,
|
||||
) : Cmd() {
|
||||
override suspend fun perform(frame: CmdFrame) {
|
||||
val scope = frame.ensureScope()
|
||||
val receiver = frame.slotToObj(recvSlot)
|
||||
val inst = receiver as? ObjInstance
|
||||
val cls = receiver as? ObjClass
|
||||
@@ -3249,7 +3294,26 @@ class CmdGetMemberSlot(
|
||||
else -> receiver.objClass.methodRecordForId(methodIdResolved)
|
||||
}
|
||||
} else null
|
||||
} ?: frame.ensureScope().raiseSymbolNotFound("member")
|
||||
} ?: run {
|
||||
val receiverClass = when {
|
||||
cls != null && fieldOnObjClass -> cls.objClass
|
||||
cls != null -> cls
|
||||
else -> receiver.objClass
|
||||
}
|
||||
val fieldName = if (fieldIdResolved >= 0) {
|
||||
receiverClass.fieldSlotMap().entries.firstOrNull { it.value.slot == fieldIdResolved }?.key
|
||||
} else null
|
||||
val methodName = if (methodIdResolved >= 0) {
|
||||
receiverClass.methodSlotMap().entries.firstOrNull { it.value.slot == methodIdResolved }?.key
|
||||
} else null
|
||||
val memberName = fieldName ?: methodName
|
||||
val message = if (memberName != null) {
|
||||
"no such member: $memberName on ${receiverClass.className}"
|
||||
} else {
|
||||
"no such member slot (fieldId=$fieldIdResolved, methodId=$methodIdResolved) on ${receiverClass.className}"
|
||||
}
|
||||
scope.raiseError(message)
|
||||
}
|
||||
val rawName = rec.memberName ?: "<member>"
|
||||
val name = if (receiver is ObjInstance && rawName.contains("::")) {
|
||||
rawName.substringAfterLast("::")
|
||||
@@ -3281,6 +3345,7 @@ class CmdSetMemberSlot(
|
||||
internal val valueSlot: Int,
|
||||
) : Cmd() {
|
||||
override suspend fun perform(frame: CmdFrame) {
|
||||
val scope = frame.ensureScope()
|
||||
val receiver = frame.slotToObj(recvSlot)
|
||||
val inst = receiver as? ObjInstance
|
||||
val cls = receiver as? ObjClass
|
||||
@@ -3303,7 +3368,26 @@ class CmdSetMemberSlot(
|
||||
else -> receiver.objClass.methodRecordForId(methodIdResolved)
|
||||
}
|
||||
} else null
|
||||
} ?: frame.ensureScope().raiseSymbolNotFound("member")
|
||||
} ?: run {
|
||||
val receiverClass = when {
|
||||
cls != null && fieldOnObjClass -> cls.objClass
|
||||
cls != null -> cls
|
||||
else -> receiver.objClass
|
||||
}
|
||||
val fieldName = if (fieldIdResolved >= 0) {
|
||||
receiverClass.fieldSlotMap().entries.firstOrNull { it.value.slot == fieldIdResolved }?.key
|
||||
} else null
|
||||
val methodName = if (methodIdResolved >= 0) {
|
||||
receiverClass.methodSlotMap().entries.firstOrNull { it.value.slot == methodIdResolved }?.key
|
||||
} else null
|
||||
val memberName = fieldName ?: methodName
|
||||
val message = if (memberName != null) {
|
||||
"no such member: $memberName on ${receiverClass.className}"
|
||||
} else {
|
||||
"no such member slot (fieldId=$fieldIdResolved, methodId=$methodIdResolved) on ${receiverClass.className}"
|
||||
}
|
||||
scope.raiseError(message)
|
||||
}
|
||||
val rawName = rec.memberName ?: "<member>"
|
||||
val name = if (receiver is ObjInstance && rawName.contains("::")) {
|
||||
rawName.substringAfterLast("::")
|
||||
@@ -3566,6 +3650,20 @@ class BytecodeLambdaCallable(
|
||||
private val returnLabels: Set<String>,
|
||||
override val pos: Pos,
|
||||
) : Statement(), BytecodeCallable {
|
||||
fun rebindClosure(newClosureScope: Scope): BytecodeLambdaCallable {
|
||||
return BytecodeLambdaCallable(
|
||||
fn = fn,
|
||||
closureScope = newClosureScope,
|
||||
captureRecords = captureRecords,
|
||||
captureNames = captureNames,
|
||||
paramSlotPlan = paramSlotPlan,
|
||||
argsDeclaration = argsDeclaration,
|
||||
preferredThisType = preferredThisType,
|
||||
returnLabels = returnLabels,
|
||||
pos = pos
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun execute(scope: Scope): Obj {
|
||||
val context = scope.applyClosureForBytecode(closureScope, preferredThisType).also {
|
||||
it.args = scope.args
|
||||
@@ -3713,6 +3811,7 @@ class CmdFrame(
|
||||
}
|
||||
|
||||
internal fun getLocalSlotTypeCode(localIndex: Int): Byte = frame.getSlotTypeCode(localIndex)
|
||||
internal fun readLocalObj(localIndex: Int): Obj = localSlotToObj(localIndex)
|
||||
internal fun isFastLocalSlot(slot: Int): Boolean {
|
||||
if (slot < fn.scopeSlotCount) return false
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
@@ -3848,10 +3947,26 @@ class CmdFrame(
|
||||
}
|
||||
}
|
||||
CaptureOwnerFrameKind.MODULE -> {
|
||||
val slot = entry.slotIndex
|
||||
val target = scopeTarget(slot)
|
||||
val index = fn.scopeSlotIndices[slot]
|
||||
target.getSlotRecord(index)
|
||||
val slotId = entry.slotIndex
|
||||
val target = moduleScope
|
||||
val name = captureNames?.getOrNull(index)
|
||||
if (name != null) {
|
||||
target.tryGetLocalRecord(target, name, target.currentClassCtx)?.let { return@mapIndexed it }
|
||||
target.getSlotIndexOf(name)?.let { return@mapIndexed target.getSlotRecord(it) }
|
||||
target.get(name)?.let { return@mapIndexed it }
|
||||
// Fallback to current scope in case the module scope isn't in the parent chain
|
||||
// or doesn't carry the imported symbol yet.
|
||||
scope.tryGetLocalRecord(scope, name, scope.currentClassCtx)?.let { return@mapIndexed it }
|
||||
scope.get(name)?.let { return@mapIndexed it }
|
||||
}
|
||||
if (slotId < target.slotCount) {
|
||||
return@mapIndexed target.getSlotRecord(slotId)
|
||||
}
|
||||
if (name != null) {
|
||||
target.applySlotPlan(mapOf(name to slotId))
|
||||
return@mapIndexed target.getSlotRecord(slotId)
|
||||
}
|
||||
error("Missing module capture slot $slotId")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3875,10 +3990,15 @@ class CmdFrame(
|
||||
.firstOrNull { fn.scopeSlotIsModule.getOrNull(it) == true }
|
||||
?.let { fn.scopeSlotNames[it] }
|
||||
if (moduleSlotName != null) {
|
||||
findModuleScope(scope)?.let { return it }
|
||||
val bySlot = findScopeWithSlot(scope, moduleSlotName)
|
||||
bySlot?.let { return it }
|
||||
if (bySlot is ModuleScope) return bySlot
|
||||
val bySlotParent = bySlot?.parent
|
||||
if (bySlotParent is ModuleScope) return bySlotParent
|
||||
val byRecord = findScopeWithRecord(scope, moduleSlotName)
|
||||
byRecord?.let { return it }
|
||||
if (byRecord is ModuleScope) return byRecord
|
||||
val byRecordParent = byRecord?.parent
|
||||
if (byRecordParent is ModuleScope) return byRecordParent
|
||||
return scope
|
||||
}
|
||||
findModuleScope(scope)?.let { return it }
|
||||
@@ -3929,7 +4049,7 @@ class CmdFrame(
|
||||
val current = queue.removeFirst()
|
||||
if (!visited.add(current)) continue
|
||||
if (current is ModuleScope) return current
|
||||
if (current.parent is ModuleScope) return current
|
||||
if (current.parent is ModuleScope) return current.parent
|
||||
current.parent?.let { queue.add(it) }
|
||||
if (current is BytecodeClosureScope) {
|
||||
queue.add(current.closureScope)
|
||||
@@ -4112,6 +4232,7 @@ class CmdFrame(
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
ensureLocalMutable(localIndex)
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(value)
|
||||
@@ -4123,6 +4244,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setObj(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4168,6 +4290,7 @@ class CmdFrame(
|
||||
target.setSlotValue(index, ObjInt.of(value))
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(ObjInt.of(value))
|
||||
@@ -4179,6 +4302,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setInt(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4196,6 +4320,7 @@ class CmdFrame(
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
ensureLocalMutable(localIndex)
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(ObjInt.of(value))
|
||||
@@ -4207,6 +4332,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setInt(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4254,6 +4380,7 @@ class CmdFrame(
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
ensureLocalMutable(localIndex)
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(ObjReal.of(value))
|
||||
@@ -4265,6 +4392,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setReal(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4276,6 +4404,7 @@ class CmdFrame(
|
||||
target.setSlotValue(index, ObjReal.of(value))
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(ObjReal.of(value))
|
||||
@@ -4287,6 +4416,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setReal(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4328,6 +4458,7 @@ class CmdFrame(
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
ensureLocalMutable(localIndex)
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(if (value) ObjTrue else ObjFalse)
|
||||
@@ -4339,6 +4470,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setBool(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4350,6 +4482,7 @@ class CmdFrame(
|
||||
target.setSlotValue(index, if (value) ObjTrue else ObjFalse)
|
||||
} else {
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(if (value) ObjTrue else ObjFalse)
|
||||
@@ -4361,6 +4494,7 @@ class CmdFrame(
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setBool(localIndex, value)
|
||||
}
|
||||
}
|
||||
@@ -4448,7 +4582,30 @@ class CmdFrame(
|
||||
val index = ensureScopeSlot(target, slot)
|
||||
target.setSlotValue(index, value)
|
||||
} else {
|
||||
frame.setObj(slot - fn.scopeSlotCount, value)
|
||||
val localIndex = slot - fn.scopeSlotCount
|
||||
if (shouldWriteThroughLocal(localIndex)) {
|
||||
when (val existing = frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef -> {
|
||||
existing.write(value)
|
||||
return
|
||||
}
|
||||
is RecordSlotRef -> {
|
||||
existing.write(value)
|
||||
return
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
frame.setObj(localIndex, value)
|
||||
}
|
||||
}
|
||||
|
||||
private fun shouldWriteThroughLocal(localIndex: Int): Boolean {
|
||||
if (localIndex < fn.localSlotCaptures.size && fn.localSlotCaptures[localIndex]) return true
|
||||
if (localIndex < fn.localSlotDelegated.size && fn.localSlotDelegated[localIndex]) return true
|
||||
return when (frame.getRawObj(localIndex)) {
|
||||
is FrameSlotRef, is RecordSlotRef -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4542,7 +4699,12 @@ class CmdFrame(
|
||||
positional.add(value)
|
||||
}
|
||||
}
|
||||
return Arguments(positional, plan.tailBlock, named ?: emptyMap())
|
||||
return Arguments(
|
||||
list = positional,
|
||||
tailBlockMode = plan.tailBlock,
|
||||
named = named ?: emptyMap(),
|
||||
explicitTypeArgs = plan.explicitTypeArgs
|
||||
)
|
||||
}
|
||||
|
||||
private fun resolveLocalScope(localIndex: Int): Scope? {
|
||||
@@ -4589,6 +4751,19 @@ class CmdFrame(
|
||||
if (direct is FrameSlotRef) return direct.read()
|
||||
if (direct is RecordSlotRef) return direct.read()
|
||||
val name = fn.scopeSlotNames[slot]
|
||||
if (name != null && record.memberName != null && record.memberName != name) {
|
||||
val resolved = target.get(name)
|
||||
if (resolved != null) {
|
||||
val resolvedValue = resolved.value
|
||||
if (resolved.type == ObjRecord.Type.Delegated || resolved.type == ObjRecord.Type.Property || resolvedValue is ObjProperty) {
|
||||
return target.resolve(resolved, name)
|
||||
}
|
||||
if (resolvedValue !== ObjUnset) {
|
||||
target.updateSlotFor(name, resolved)
|
||||
}
|
||||
return resolvedValue
|
||||
}
|
||||
}
|
||||
if (name != null && (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property || direct is ObjProperty)) {
|
||||
return target.resolve(record, name)
|
||||
}
|
||||
@@ -4612,6 +4787,19 @@ class CmdFrame(
|
||||
if (direct is RecordSlotRef) return direct.read()
|
||||
val slotId = addrScopeSlots[addrSlot]
|
||||
val name = fn.scopeSlotNames.getOrNull(slotId)
|
||||
if (name != null && record.memberName != null && record.memberName != name) {
|
||||
val resolved = target.get(name)
|
||||
if (resolved != null) {
|
||||
val resolvedValue = resolved.value
|
||||
if (resolved.type == ObjRecord.Type.Delegated || resolved.type == ObjRecord.Type.Property || resolvedValue is ObjProperty) {
|
||||
return target.resolve(resolved, name)
|
||||
}
|
||||
if (resolvedValue !== ObjUnset) {
|
||||
target.updateSlotFor(name, resolved)
|
||||
}
|
||||
return resolvedValue
|
||||
}
|
||||
}
|
||||
if (name != null && (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property || direct is ObjProperty)) {
|
||||
return target.resolve(record, name)
|
||||
}
|
||||
|
||||
@@ -178,6 +178,7 @@ object CompletionEngineLight {
|
||||
is MiniMemberDecl -> node.range
|
||||
else -> return
|
||||
}
|
||||
if (range.start.source != src || range.end.source != src) return
|
||||
val start = src.offsetOf(range.start)
|
||||
val end = src.offsetOf(range.end).coerceAtMost(text.length)
|
||||
|
||||
@@ -372,9 +373,12 @@ object CompletionEngineLight {
|
||||
val src = Source("<engine>", text)
|
||||
val provider = LenientImportProvider.create()
|
||||
Compiler.compileWithMini(src, provider, sink)
|
||||
sink.build()
|
||||
sink.build() ?: MiniScript(MiniRange(src.startPos, src.startPos))
|
||||
} catch (_: Throwable) {
|
||||
sink.build()
|
||||
sink.build() ?: run {
|
||||
val src = Source("<engine>", text)
|
||||
MiniScript(MiniRange(src.startPos, src.startPos))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,6 +391,7 @@ object CompletionEngineLight {
|
||||
|
||||
// Text helpers
|
||||
private fun prefixAt(text: String, offset: Int): String {
|
||||
if (text.isEmpty()) return ""
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var i = (off - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && DocLookupUtils.isIdentChar(text[i])) i--
|
||||
|
||||
@@ -1163,6 +1163,7 @@ object DocLookupUtils {
|
||||
}
|
||||
|
||||
fun findDotLeft(text: String, offset: Int): Int? {
|
||||
if (text.isEmpty()) return null
|
||||
var i = (offset - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return if (i >= 0 && text[i] == '.') i else null
|
||||
|
||||
@@ -37,8 +37,20 @@ object StdlibDocsBootstrap {
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _list = net.sergeych.lyng.obj.ObjList.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _immutableList = net.sergeych.lyng.obj.ObjImmutableList.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _map = net.sergeych.lyng.obj.ObjMap.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _immutableMap = net.sergeych.lyng.obj.ObjImmutableMap.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _set = net.sergeych.lyng.obj.ObjSet.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _immutableSet = net.sergeych.lyng.obj.ObjImmutableSet.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _observableList = net.sergeych.lyng.obj.ObjObservableList.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _subscription = net.sergeych.lyng.obj.ObjSubscription.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _int = net.sergeych.lyng.obj.ObjInt.type
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val _real = net.sergeych.lyng.obj.ObjReal.type
|
||||
|
||||
@@ -256,6 +256,13 @@ open class Obj {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sugar to use it with [ScopeFacade]
|
||||
*/
|
||||
suspend fun toString(facade: ScopeFacade) = toString(facade.requireScope())
|
||||
|
||||
|
||||
|
||||
open suspend fun defaultToString(scope: Scope): ObjString = ObjString(this.toString())
|
||||
|
||||
/**
|
||||
@@ -639,6 +646,9 @@ open class Obj {
|
||||
}
|
||||
|
||||
open suspend fun getAt(scope: Scope, index: Obj): Obj {
|
||||
if (hasNonRootIndexerMember("getAt")) {
|
||||
return invokeInstanceMethod(scope, "getAt", Arguments(index))
|
||||
}
|
||||
if (index is ObjString) {
|
||||
return readField(scope, index.value).value
|
||||
}
|
||||
@@ -648,6 +658,16 @@ open class Obj {
|
||||
suspend fun getAt(scope: Scope, index: Int): Obj = getAt(scope, ObjInt(index.toLong()))
|
||||
|
||||
open suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
|
||||
when {
|
||||
hasNonRootIndexerMember("putAt") -> {
|
||||
invokeInstanceMethod(scope, "putAt", Arguments(index, newValue))
|
||||
return
|
||||
}
|
||||
hasNonRootIndexerMember("setAt") -> {
|
||||
invokeInstanceMethod(scope, "setAt", Arguments(index, newValue))
|
||||
return
|
||||
}
|
||||
}
|
||||
if (index is ObjString) {
|
||||
writeField(scope, index.value, newValue)
|
||||
return
|
||||
@@ -655,6 +675,17 @@ open class Obj {
|
||||
scope.raiseNotImplemented("indexing")
|
||||
}
|
||||
|
||||
private fun hasNonRootIndexerMember(name: String): Boolean {
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
||||
if (rec != null && !rec.isAbstract && rec.type != ObjRecord.Type.Delegated) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
open suspend fun callOn(scope: Scope): Obj {
|
||||
scope.raiseNotImplemented()
|
||||
}
|
||||
@@ -880,7 +911,7 @@ open class Obj {
|
||||
is Enum<*> -> ObjString(obj.name)
|
||||
Unit -> ObjVoid
|
||||
null -> ObjNull
|
||||
is Iterator<*> -> ObjKotlinIterator(obj as Iterator<Any?>)
|
||||
is Iterator<*> -> ObjKotlinIterator(obj)
|
||||
else -> throw IllegalArgumentException("cannot convert to Obj: $obj")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,7 +39,7 @@ class ObjArrayIterator(val array: Obj) : Obj() {
|
||||
val self = thisAs<ObjArrayIterator>()
|
||||
if (self.nextIndex < self.lastIndex) {
|
||||
self.array.invokeInstanceMethod(requireScope(), "getAt", (self.nextIndex++).toObj())
|
||||
} else raiseError(ObjIterationFinishedException(requireScope()))
|
||||
} else raiseIterationFinished()
|
||||
}
|
||||
addFn("hasNext") {
|
||||
val self = thisAs<ObjArrayIterator>()
|
||||
|
||||
@@ -826,6 +826,7 @@ open class ObjClass(
|
||||
type: ObjRecord.Type = ObjRecord.Type.Field,
|
||||
fieldId: Int? = null,
|
||||
methodId: Int? = null,
|
||||
typeDecl: net.sergeych.lyng.TypeDecl? = null,
|
||||
): ObjRecord {
|
||||
// Validation of override rules: only for non-system declarations
|
||||
var existing: ObjRecord? = null
|
||||
@@ -921,6 +922,7 @@ open class ObjClass(
|
||||
isOverride = isOverride,
|
||||
isTransient = isTransient,
|
||||
type = type,
|
||||
typeDecl = typeDecl,
|
||||
memberName = name,
|
||||
fieldId = effectiveFieldId,
|
||||
methodId = effectiveMethodId
|
||||
@@ -1196,7 +1198,22 @@ open class ObjClass(
|
||||
return JsonObject(result)
|
||||
}
|
||||
|
||||
open suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =
|
||||
scope.raiseNotImplemented()
|
||||
open suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
|
||||
val meta = constructorMeta
|
||||
?: scope.raiseError("can't deserialize non-serializable object (no constructor meta)")
|
||||
val params = meta.params.filter { !it.isTransient }
|
||||
val values = decoder.decodeAnyList(scope)
|
||||
if (values.size > params.size) {
|
||||
scope.raiseIllegalArgument(
|
||||
"serialized params has bigger size ${values.size} than constructor params (${params.size}): " +
|
||||
values.joinToString(",")
|
||||
)
|
||||
}
|
||||
val instance = callWithArgs(scope, *values.toTypedArray())
|
||||
if (instance is ObjInstance) {
|
||||
instance.deserializeStateVars(scope, decoder)
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,7 +17,16 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.datetime.*
|
||||
import kotlinx.datetime.DateTimeUnit
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.UtcOffset
|
||||
import kotlinx.datetime.asTimeZone
|
||||
import kotlinx.datetime.isoDayNumber
|
||||
import kotlinx.datetime.number
|
||||
import kotlinx.datetime.plus
|
||||
import kotlinx.datetime.toInstant
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
@@ -28,12 +37,13 @@ import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
import kotlin.time.Instant
|
||||
|
||||
class ObjDateTime(val instant: Instant, val timeZone: TimeZone) : Obj() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
val localDateTime: LocalDateTime by lazy {
|
||||
instant.toLocalDateTime(timeZone)
|
||||
with(timeZone) { instant.toLocalDateTime() }
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
@@ -177,11 +187,11 @@ class ObjDateTime(val instant: Instant, val timeZone: TimeZone) : Obj() {
|
||||
addPropertyDoc("year", "The year component.", type("lyng.Int"), moduleName = "lyng.time",
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.year.toObj() })
|
||||
addPropertyDoc("month", "The month component (1..12).", type("lyng.Int"), moduleName = "lyng.time",
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.monthNumber.toObj() })
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.month.number.toObj() })
|
||||
addPropertyDoc("dayOfMonth", "The day of month component.", type("lyng.Int"), moduleName = "lyng.time",
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.dayOfMonth.toObj() })
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.day.toObj() })
|
||||
addPropertyDoc("day", "Alias to dayOfMonth.", type("lyng.Int"), moduleName = "lyng.time",
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.dayOfMonth.toObj() })
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.day.toObj() })
|
||||
addPropertyDoc("hour", "The hour component (0..23).", type("lyng.Int"), moduleName = "lyng.time",
|
||||
getter = { thisAs<ObjDateTime>().localDateTime.hour.toObj() })
|
||||
addPropertyDoc("minute", "The minute component (0..59).", type("lyng.Int"), moduleName = "lyng.time",
|
||||
@@ -257,7 +267,7 @@ class ObjDateTime(val instant: Instant, val timeZone: TimeZone) : Obj() {
|
||||
returns = type("lyng.DateTime"),
|
||||
moduleName = "lyng.time") {
|
||||
val s = (args.firstAndOnly() as ObjString).value
|
||||
// kotlinx-datetime's Instant.parse handles RFC3339
|
||||
// kotlin.time's Instant.parse handles RFC3339
|
||||
// But we want to preserve the offset if present for DateTime.
|
||||
// However, Instant.parse("...") always gives an Instant.
|
||||
// If we want the specific offset from the string, we might need a more complex parse.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Arguments
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.bytecode.BytecodeLambdaCallable
|
||||
|
||||
class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
|
||||
override val objClass: ObjClass get() = type
|
||||
@@ -29,7 +30,8 @@ class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
|
||||
val d = thisAs<ObjDynamicContext>().delegate
|
||||
if (d.readCallback != null)
|
||||
raiseIllegalState("get already defined")
|
||||
d.readCallback = requireOnlyArg()
|
||||
val callback = requireOnlyArg<Obj>()
|
||||
d.readCallback = d.rebindCallback(requireScope(), callback)
|
||||
ObjVoid
|
||||
}
|
||||
|
||||
@@ -37,7 +39,8 @@ class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
|
||||
val d = thisAs<ObjDynamicContext>().delegate
|
||||
if (d.writeCallback != null)
|
||||
raiseIllegalState("set already defined")
|
||||
d.writeCallback = requireOnlyArg()
|
||||
val callback = requireOnlyArg<Obj>()
|
||||
d.writeCallback = d.rebindCallback(requireScope(), callback)
|
||||
ObjVoid
|
||||
}
|
||||
|
||||
@@ -55,6 +58,11 @@ open class ObjDynamic(var readCallback: Obj? = null, var writeCallback: Obj? = n
|
||||
override val objClass: ObjClass get() = type
|
||||
// Capture the lexical scope used to build this dynamic so callbacks can see outer locals
|
||||
internal var builderScope: Scope? = null
|
||||
internal fun rebindCallback(contextScope: Scope, callback: Obj): Obj {
|
||||
val snapshot = builderScope ?: return callback
|
||||
val context = Scope(snapshot, contextScope.args, contextScope.pos, contextScope.thisObj)
|
||||
return (callback as? BytecodeLambdaCallable)?.rebindClosure(context) ?: callback
|
||||
}
|
||||
|
||||
/**
|
||||
* Use read callback to dynamically resolve the field name. Note that it does not work
|
||||
@@ -113,8 +121,9 @@ open class ObjDynamic(var readCallback: Obj? = null, var writeCallback: Obj? = n
|
||||
// Capture the function's lexical scope (scope) so callbacks can see outer locals like parameters.
|
||||
// Build the dynamic in a child scope purely to set `this` to context, but keep captured closure at parent.
|
||||
val buildScope = scope.createChildScope(newThisObj = context)
|
||||
// Snapshot the caller scope to capture locals/args even if the runtime pools/reuses frames
|
||||
delegate.builderScope = scope.snapshotForClosure()
|
||||
// Snapshot the caller scope to capture locals/args even if the runtime pools/reuses frames.
|
||||
// Module scope should stay late-bound to allow extern class rebinding and similar updates.
|
||||
delegate.builderScope = if (scope is net.sergeych.lyng.ModuleScope) null else scope.snapshotForClosure()
|
||||
builder.callOn(buildScope)
|
||||
return delegate
|
||||
}
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.addPropertyDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
class ObjImmutableList(items: List<Obj> = emptyList()) : Obj() {
|
||||
private val data: List<Obj> = items.toList()
|
||||
|
||||
override val objClass: ObjClass
|
||||
get() = type
|
||||
|
||||
override suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||
if (this === other) return true
|
||||
return when (other) {
|
||||
is ObjImmutableList -> data.size == other.data.size && data.indices.all { i -> data[i].equals(scope, other.data[i]) }
|
||||
else -> {
|
||||
if (other.isInstanceOf(ObjIterable)) compareTo(scope, other) == 0 else false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if (other is ObjImmutableList) {
|
||||
val mySize = data.size
|
||||
val otherSize = other.data.size
|
||||
val commonSize = minOf(mySize, otherSize)
|
||||
for (i in 0..<commonSize) {
|
||||
val d = data[i].compareTo(scope, other.data[i])
|
||||
if (d != 0) return d
|
||||
}
|
||||
return mySize.compareTo(otherSize)
|
||||
}
|
||||
if (other.isInstanceOf(ObjIterable)) {
|
||||
val it1 = data.iterator()
|
||||
val it2 = other.invokeInstanceMethod(scope, "iterator")
|
||||
val hasNext2 = it2.getInstanceMethod(scope, "hasNext")
|
||||
val next2 = it2.getInstanceMethod(scope, "next")
|
||||
while (it1.hasNext()) {
|
||||
if (!hasNext2.invoke(scope, it2).toBool()) return 1
|
||||
val d = it1.next().compareTo(scope, next2.invoke(scope, it2))
|
||||
if (d != 0) return d
|
||||
}
|
||||
return if (hasNext2.invoke(scope, it2).toBool()) -1 else 0
|
||||
}
|
||||
return -2
|
||||
}
|
||||
|
||||
override suspend fun getAt(scope: Scope, index: Obj): Obj {
|
||||
return when (index) {
|
||||
is ObjInt -> data[index.toInt()]
|
||||
is ObjRange -> {
|
||||
when {
|
||||
index.start is ObjInt && index.end is ObjInt -> {
|
||||
if (index.isEndInclusive)
|
||||
ObjImmutableList(data.subList(index.start.toInt(), index.end.toInt() + 1))
|
||||
else
|
||||
ObjImmutableList(data.subList(index.start.toInt(), index.end.toInt()))
|
||||
}
|
||||
index.isOpenStart && !index.isOpenEnd -> {
|
||||
if (index.isEndInclusive)
|
||||
ObjImmutableList(data.subList(0, index.end!!.toInt() + 1))
|
||||
else
|
||||
ObjImmutableList(data.subList(0, index.end!!.toInt()))
|
||||
}
|
||||
index.isOpenEnd && !index.isOpenStart -> ObjImmutableList(data.subList(index.start!!.toInt(), data.size))
|
||||
index.isOpenStart && index.isOpenEnd -> ObjImmutableList(data)
|
||||
else -> throw RuntimeException("Can't apply range for index: $index")
|
||||
}
|
||||
}
|
||||
else -> scope.raiseIllegalArgument("Illegal index object for immutable list: ${index.inspect(scope)}")
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun contains(scope: Scope, other: Obj): Boolean {
|
||||
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS && other is ObjInt) {
|
||||
var i = 0
|
||||
val sz = data.size
|
||||
while (i < sz) {
|
||||
val v = data[i]
|
||||
if (v is ObjInt && v.value == other.value) return true
|
||||
i++
|
||||
}
|
||||
return false
|
||||
}
|
||||
return data.contains(other)
|
||||
}
|
||||
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
for (item in data) {
|
||||
if (!callback(item)) break
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj {
|
||||
return when {
|
||||
other is ObjImmutableList -> ObjImmutableList(data + other.data)
|
||||
other is ObjList -> ObjImmutableList(data + other.list)
|
||||
other.isInstanceOf(ObjIterable) && other !is ObjString && other !is ObjBuffer -> {
|
||||
val l = other.callMethod<ObjList>(scope, "toList")
|
||||
ObjImmutableList(data + l.list)
|
||||
}
|
||||
else -> ObjImmutableList(data + other)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj {
|
||||
if (other !is ObjString && other !is ObjBuffer && other.isInstanceOf(ObjIterable)) {
|
||||
val toRemove = mutableSetOf<Obj>()
|
||||
other.enumerate(scope) {
|
||||
toRemove += it
|
||||
true
|
||||
}
|
||||
return ObjImmutableList(data.filterNot { toRemove.contains(it) })
|
||||
}
|
||||
val out = data.toMutableList()
|
||||
out.remove(other)
|
||||
return ObjImmutableList(out)
|
||||
}
|
||||
|
||||
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
|
||||
encoder.encodeAnyList(scope, data)
|
||||
}
|
||||
|
||||
override suspend fun lynonType(): LynonType = LynonType.List
|
||||
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
return ObjString(buildString {
|
||||
append("ImmutableList(")
|
||||
var first = true
|
||||
for (v in data) {
|
||||
if (first) first = false else append(",")
|
||||
append(v.toString(scope).value)
|
||||
}
|
||||
append(")")
|
||||
})
|
||||
}
|
||||
|
||||
fun toMutableList(): MutableList<Obj> = data.toMutableList()
|
||||
|
||||
companion object {
|
||||
val type = object : ObjClass("ImmutableList", ObjArray) {
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
return ObjImmutableList(scope.args.list)
|
||||
}
|
||||
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
|
||||
return ObjImmutableList(decoder.decodeAnyList(scope))
|
||||
}
|
||||
}.apply {
|
||||
addPropertyDoc(
|
||||
name = "size",
|
||||
doc = "Number of elements in this immutable list.",
|
||||
type = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib",
|
||||
getter = { (this.thisObj as ObjImmutableList).data.size.toObj() }
|
||||
)
|
||||
addFnDoc(
|
||||
name = "toMutable",
|
||||
doc = "Create a mutable copy of this immutable list.",
|
||||
returns = type("lyng.List"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjList(thisAs<ObjImmutableList>().toMutableList())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toImmutable",
|
||||
doc = "Return this immutable list.",
|
||||
returns = type("lyng.ImmutableList"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisObj
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.TypeGenericDoc
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.addPropertyDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
class ObjImmutableMap(entries: Map<Obj, Obj> = emptyMap()) : Obj() {
|
||||
val map: Map<Obj, Obj> = LinkedHashMap(entries)
|
||||
|
||||
override val objClass get() = type
|
||||
|
||||
override suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||
if (this === other) return true
|
||||
val otherMap = when (other) {
|
||||
is ObjImmutableMap -> other.map
|
||||
is ObjMap -> other.map
|
||||
else -> return false
|
||||
}
|
||||
if (map.size != otherMap.size) return false
|
||||
for ((k, v) in map) {
|
||||
val ov = other.getAt(scope, k)
|
||||
if (ov === ObjNull && !other.contains(scope, k)) return false
|
||||
if (!v.equals(scope, ov)) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
val otherMap = when (other) {
|
||||
is ObjImmutableMap -> other.map
|
||||
is ObjMap -> other.map
|
||||
else -> return -1
|
||||
}
|
||||
if (map == otherMap) return 0
|
||||
if (map.size != otherMap.size) return map.size.compareTo(otherMap.size)
|
||||
return map.toString().compareTo(otherMap.toString())
|
||||
}
|
||||
|
||||
override suspend fun getAt(scope: Scope, index: Obj): Obj = map[index] ?: ObjNull
|
||||
|
||||
override suspend fun contains(scope: Scope, other: Obj): Boolean = other in map
|
||||
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
val rendered = buildString {
|
||||
append("ImmutableMap(")
|
||||
var first = true
|
||||
for ((k, v) in map) {
|
||||
if (!first) append(",")
|
||||
append(k.inspect(scope))
|
||||
append(" => ")
|
||||
append(v.toString(scope).value)
|
||||
first = false
|
||||
}
|
||||
append(")")
|
||||
}
|
||||
return ObjString(rendered)
|
||||
}
|
||||
|
||||
override suspend fun lynonType(): LynonType = LynonType.Map
|
||||
|
||||
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
|
||||
val keys = map.keys.map { it.toObj() }
|
||||
val values = map.values.map { it.toObj() }
|
||||
encoder.encodeAnyList(scope, keys)
|
||||
encoder.encodeAnyList(scope, values, fixedSize = true)
|
||||
}
|
||||
|
||||
override suspend fun toJson(scope: Scope): JsonElement {
|
||||
return JsonObject(map.map { it.key.toString(scope).value to it.value.toJson(scope) }.toMap())
|
||||
}
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj {
|
||||
val out = LinkedHashMap(map)
|
||||
mergeIn(scope, out, other)
|
||||
return ObjImmutableMap(out)
|
||||
}
|
||||
|
||||
private suspend fun mergeIn(scope: Scope, out: MutableMap<Obj, Obj>, other: Obj) {
|
||||
when (other) {
|
||||
is ObjImmutableMap -> out.putAll(other.map)
|
||||
is ObjMap -> out.putAll(other.map)
|
||||
is ObjMapEntry -> out[other.key] = other.value
|
||||
is ObjList -> {
|
||||
for (e in other.list) {
|
||||
when (e) {
|
||||
is ObjMapEntry -> out[e.key] = e.value
|
||||
else -> {
|
||||
if (e.isInstanceOf(ObjArray)) {
|
||||
if (e.invokeInstanceMethod(scope, "size").toInt() != 2)
|
||||
scope.raiseIllegalArgument("Array element to merge into map must have 2 elements, got $e")
|
||||
out[e.getAt(scope, 0)] = e.getAt(scope, 1)
|
||||
} else {
|
||||
scope.raiseIllegalArgument("map can only be merged with MapEntry elements; got $e")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> scope.raiseIllegalArgument("map can only be merged with Map, ImmutableMap, MapEntry, or List<MapEntry>")
|
||||
}
|
||||
}
|
||||
|
||||
fun toMutableMapCopy(): MutableMap<Obj, Obj> = LinkedHashMap(map)
|
||||
|
||||
companion object {
|
||||
val type = object : ObjClass("ImmutableMap", ObjCollection) {
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
return ObjImmutableMap(ObjMap.listToMap(scope, scope.args.list))
|
||||
}
|
||||
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
|
||||
val keys = decoder.decodeAnyList(scope)
|
||||
val values = decoder.decodeAnyList(scope, fixedSize = keys.size)
|
||||
if (keys.size != values.size) scope.raiseIllegalArgument("map keys and values should be same size")
|
||||
return ObjImmutableMap(keys.zip(values).toMap())
|
||||
}
|
||||
}.apply {
|
||||
addFnDoc(
|
||||
name = "getOrNull",
|
||||
doc = "Get value by key or return null if the key is absent.",
|
||||
params = listOf(ParamDoc("key")),
|
||||
returns = type("lyng.Any", nullable = true),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjImmutableMap>().map[args.firstAndOnly(pos)] ?: ObjNull
|
||||
}
|
||||
addPropertyDoc(
|
||||
name = "size",
|
||||
doc = "Number of entries in the immutable map.",
|
||||
type = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib",
|
||||
getter = { thisAs<ObjImmutableMap>().map.size.toObj() }
|
||||
)
|
||||
addPropertyDoc(
|
||||
name = "keys",
|
||||
doc = "List of keys in this immutable map.",
|
||||
type = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.Any"))),
|
||||
moduleName = "lyng.stdlib",
|
||||
getter = { thisAs<ObjImmutableMap>().map.keys.toObj() }
|
||||
)
|
||||
addPropertyDoc(
|
||||
name = "values",
|
||||
doc = "List of values in this immutable map.",
|
||||
type = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.Any"))),
|
||||
moduleName = "lyng.stdlib",
|
||||
getter = { ObjList(thisAs<ObjImmutableMap>().map.values.toMutableList()) }
|
||||
)
|
||||
addFnDoc(
|
||||
name = "iterator",
|
||||
doc = "Iterator over map entries as MapEntry objects.",
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjKotlinIterator(thisAs<ObjImmutableMap>().map.entries.iterator())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toMutable",
|
||||
doc = "Create a mutable copy of this immutable map.",
|
||||
returns = type("lyng.Map"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjMap(thisAs<ObjImmutableMap>().toMutableMapCopy())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toImmutable",
|
||||
doc = "Return this immutable map.",
|
||||
returns = type("lyng.ImmutableMap"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisObj }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
class ObjImmutableSet(items: Collection<Obj> = emptyList()) : Obj() {
|
||||
private val data: Set<Obj> = LinkedHashSet(items)
|
||||
|
||||
override val objClass get() = type
|
||||
|
||||
override suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||
if (this === other) return true
|
||||
val otherSet = when (other) {
|
||||
is ObjImmutableSet -> other.data
|
||||
is ObjSet -> other.set
|
||||
else -> return false
|
||||
}
|
||||
if (data.size != otherSet.size) return false
|
||||
for (e in data) {
|
||||
if (!other.contains(scope, e)) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
val otherSet = when (other) {
|
||||
is ObjImmutableSet -> other.data
|
||||
is ObjSet -> other.set
|
||||
else -> return -2
|
||||
}
|
||||
if (data == otherSet) return 0
|
||||
if (data.size != otherSet.size) return data.size.compareTo(otherSet.size)
|
||||
return data.toString().compareTo(otherSet.toString())
|
||||
}
|
||||
|
||||
override suspend fun contains(scope: Scope, other: Obj): Boolean = data.contains(other)
|
||||
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
for (item in data) {
|
||||
if (!callback(item)) break
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj {
|
||||
val merged = LinkedHashSet(data)
|
||||
when {
|
||||
other is ObjImmutableSet -> merged.addAll(other.data)
|
||||
other is ObjSet -> merged.addAll(other.set)
|
||||
other is ObjString || other is ObjBuffer || !other.isInstanceOf(ObjIterable) -> merged.add(other)
|
||||
else -> other.enumerate(scope) { merged += it; true }
|
||||
}
|
||||
return ObjImmutableSet(merged)
|
||||
}
|
||||
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj {
|
||||
val out = LinkedHashSet(data)
|
||||
when {
|
||||
other is ObjImmutableSet -> out.removeAll(other.data)
|
||||
other is ObjSet -> out.removeAll(other.set)
|
||||
other is ObjString || other is ObjBuffer || !other.isInstanceOf(ObjIterable) -> out.remove(other)
|
||||
else -> other.enumerate(scope) { out.remove(it); true }
|
||||
}
|
||||
return ObjImmutableSet(out)
|
||||
}
|
||||
|
||||
override suspend fun mul(scope: Scope, other: Obj): Obj {
|
||||
val right = when (other) {
|
||||
is ObjImmutableSet -> other.data
|
||||
is ObjSet -> other.set
|
||||
else -> scope.raiseIllegalArgument("set operator * requires another set")
|
||||
}
|
||||
return ObjImmutableSet(data.intersect(right))
|
||||
}
|
||||
|
||||
override suspend fun lynonType(): LynonType = LynonType.Set
|
||||
|
||||
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
|
||||
encoder.encodeAnyList(scope, data.toList())
|
||||
}
|
||||
|
||||
fun toMutableSet(): MutableSet<Obj> = LinkedHashSet(data)
|
||||
|
||||
companion object {
|
||||
val type: ObjClass = object : ObjClass("ImmutableSet", ObjCollection) {
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
return ObjImmutableSet(scope.args.list)
|
||||
}
|
||||
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =
|
||||
ObjImmutableSet(decoder.decodeAnyList(scope))
|
||||
}.apply {
|
||||
addFnDoc(
|
||||
name = "size",
|
||||
doc = "Number of elements in this immutable set.",
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjImmutableSet>().data.size.toObj()
|
||||
}
|
||||
addFnDoc(
|
||||
name = "intersect",
|
||||
doc = "Intersection with another set. Returns a new immutable set.",
|
||||
params = listOf(ParamDoc("other")),
|
||||
returns = type("lyng.ImmutableSet"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjImmutableSet>().mul(requireScope(), args.firstAndOnly())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "iterator",
|
||||
doc = "Iterator over elements of this immutable set.",
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjKotlinObjIterator(thisAs<ObjImmutableSet>().data.iterator())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "union",
|
||||
doc = "Union with another set or iterable. Returns a new immutable set.",
|
||||
params = listOf(ParamDoc("other")),
|
||||
returns = type("lyng.ImmutableSet"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjImmutableSet>().plus(requireScope(), args.firstAndOnly())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "subtract",
|
||||
doc = "Subtract another set or iterable from this set. Returns a new immutable set.",
|
||||
params = listOf(ParamDoc("other")),
|
||||
returns = type("lyng.ImmutableSet"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjImmutableSet>().minus(requireScope(), args.firstAndOnly())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toMutable",
|
||||
doc = "Create a mutable copy of this immutable set.",
|
||||
returns = type("lyng.Set"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjSet(thisAs<ObjImmutableSet>().toMutableSet())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toImmutable",
|
||||
doc = "Return this immutable set.",
|
||||
returns = type("lyng.ImmutableSet"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisObj }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,13 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.datetime.*
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.UtcOffset
|
||||
import kotlinx.datetime.asTimeZone
|
||||
import kotlinx.datetime.number
|
||||
import kotlinx.datetime.toInstant
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
@@ -27,6 +33,7 @@ import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonSettings
|
||||
import net.sergeych.lynon.LynonType
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
import kotlin.time.isDistantFuture
|
||||
import kotlin.time.isDistantPast
|
||||
|
||||
@@ -196,8 +203,8 @@ class ObjInstant(val instant: Instant,val truncateMode: LynonSettings.InstantTru
|
||||
) {
|
||||
val t = thisAs<ObjInstant>().instant
|
||||
val tz = TimeZone.UTC
|
||||
val dt = t.toLocalDateTime(tz)
|
||||
val truncated = LocalDateTime(dt.year, dt.month, dt.dayOfMonth, dt.hour, dt.minute, 0, 0)
|
||||
val dt = with(tz) { t.toLocalDateTime() }
|
||||
val truncated = LocalDateTime(dt.year, dt.month.number, dt.day, dt.hour, dt.minute, 0, 0)
|
||||
ObjInstant(truncated.toInstant(tz), LynonSettings.InstantTruncateMode.Second)
|
||||
}
|
||||
addFnDoc(
|
||||
|
||||
@@ -166,7 +166,7 @@ class ObjInt(val value: Long, override val isConst: Boolean = false) : Obj(), Nu
|
||||
internal const val CACHE_LOW: Long = -1024L
|
||||
internal const val CACHE_HIGH: Long = 1023L
|
||||
private val cache = Array((CACHE_HIGH - CACHE_LOW + 1).toInt()) {
|
||||
ObjInt((it + CACHE_LOW).toLong(), true)
|
||||
ObjInt(it + CACHE_LOW, true)
|
||||
}
|
||||
|
||||
fun of(value: Long): ObjInt {
|
||||
|
||||
@@ -50,6 +50,21 @@ val ObjIterable by lazy {
|
||||
ObjList(result)
|
||||
}
|
||||
|
||||
addFnDoc(
|
||||
name = "toImmutableList",
|
||||
doc = "Collect elements of this iterable into a new immutable list.",
|
||||
returns = type("lyng.ImmutableList"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val scope = requireScope()
|
||||
val result = mutableListOf<Obj>()
|
||||
val it = thisObj.invokeInstanceMethod(scope, "iterator")
|
||||
while (it.invokeInstanceMethod(scope, "hasNext").toBool()) {
|
||||
result.add(it.invokeInstanceMethod(scope, "next"))
|
||||
}
|
||||
ObjImmutableList(result)
|
||||
}
|
||||
|
||||
// it is not effective, but it is open:
|
||||
addFnDoc(
|
||||
name = "contains",
|
||||
@@ -109,6 +124,28 @@ val ObjIterable by lazy {
|
||||
}
|
||||
)
|
||||
|
||||
addPropertyDoc(
|
||||
name = "toImmutableSet",
|
||||
doc = "Collect elements of this iterable into a new immutable set.",
|
||||
type = type("lyng.ImmutableSet"),
|
||||
moduleName = "lyng.stdlib",
|
||||
getter = {
|
||||
when (val self = this.thisObj) {
|
||||
is ObjImmutableSet -> self
|
||||
is ObjSet -> ObjImmutableSet(self.set)
|
||||
else -> {
|
||||
val result = mutableSetOf<Obj>()
|
||||
val scope = requireScope()
|
||||
val it = self.invokeInstanceMethod(scope, "iterator")
|
||||
while (it.invokeInstanceMethod(scope, "hasNext").toBool()) {
|
||||
result.add(it.invokeInstanceMethod(scope, "next"))
|
||||
}
|
||||
ObjImmutableSet(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
addPropertyDoc(
|
||||
name = "toMap",
|
||||
doc = "Collect pairs into a map using [0] as key and [1] as value for each element.",
|
||||
@@ -128,6 +165,25 @@ val ObjIterable by lazy {
|
||||
}
|
||||
)
|
||||
|
||||
addPropertyDoc(
|
||||
name = "toImmutableMap",
|
||||
doc = "Collect pairs into an immutable map using [0] as key and [1] as value for each element.",
|
||||
type = type("lyng.ImmutableMap"),
|
||||
moduleName = "lyng.stdlib",
|
||||
getter = {
|
||||
val result = linkedMapOf<Obj, Obj>()
|
||||
val scope = requireScope()
|
||||
this.thisObj.enumerate(scope) { pair ->
|
||||
when (pair) {
|
||||
is ObjMapEntry -> result[pair.key] = pair.value
|
||||
else -> result[pair.getAt(scope, 0)] = pair.getAt(scope, 1)
|
||||
}
|
||||
true
|
||||
}
|
||||
ObjImmutableMap(result)
|
||||
}
|
||||
)
|
||||
|
||||
addFnDoc(
|
||||
name = "associateBy",
|
||||
doc = "Build a map from elements using the lambda result as key.",
|
||||
|
||||
@@ -45,18 +45,18 @@ class ObjKotlinIterator(val iterator: Iterator<Any?>) : Obj() {
|
||||
}
|
||||
boundType = cls
|
||||
LyngClassBridge.bind(cls) {
|
||||
addFun("next") { scope, self, _ ->
|
||||
when (self) {
|
||||
addFun("next") {
|
||||
when (val self = thisObj) {
|
||||
is ObjKotlinIterator -> self.iterator.next().toObj()
|
||||
is ObjKotlinObjIterator -> self.iterator.next()
|
||||
else -> scope.raiseClassCastError("Expected KotlinIterator, got ${self.objClass.className}")
|
||||
else -> raiseClassCastError("Expected KotlinIterator, got ${self.objClass.className}")
|
||||
}
|
||||
}
|
||||
addFun("hasNext") { scope, self, _ ->
|
||||
when (self) {
|
||||
addFun("hasNext") {
|
||||
when (val self = thisObj) {
|
||||
is ObjKotlinIterator -> self.iterator.hasNext().toObj()
|
||||
is ObjKotlinObjIterator -> self.iterator.hasNext().toObj()
|
||||
else -> scope.raiseClassCastError("Expected KotlinIterator, got ${self.objClass.className}")
|
||||
else -> raiseClassCastError("Expected KotlinIterator, got ${self.objClass.className}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,16 @@ import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
open class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
protected open fun shouldTreatAsSingleElement(scope: Scope, other: Obj): Boolean {
|
||||
if (!other.isInstanceOf(ObjIterable)) return true
|
||||
val declaredElementType = scope.declaredListElementTypeForValue(this)
|
||||
if (declaredElementType != null && matchesTypeDecl(scope, other, declaredElementType)) {
|
||||
return true
|
||||
}
|
||||
if (other is ObjString || other is ObjBuffer) return true
|
||||
return false
|
||||
}
|
||||
|
||||
override suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||
if (this === other) return true
|
||||
@@ -86,7 +95,7 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
|
||||
open override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
|
||||
list[index.toInt()] = newValue
|
||||
}
|
||||
|
||||
@@ -127,7 +136,7 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
other is ObjList ->
|
||||
ObjList((list + other.list).toMutableList())
|
||||
|
||||
other.isInstanceOf(ObjIterable) && other !is ObjString && other !is ObjBuffer -> {
|
||||
!shouldTreatAsSingleElement(scope, other) && other.isInstanceOf(ObjIterable) -> {
|
||||
val l = other.callMethod<ObjList>(scope, "toList")
|
||||
ObjList((list + l.list).toMutableList())
|
||||
}
|
||||
@@ -140,10 +149,10 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
}
|
||||
|
||||
|
||||
override suspend fun plusAssign(scope: Scope, other: Obj): Obj {
|
||||
open override suspend fun plusAssign(scope: Scope, other: Obj): Obj {
|
||||
if (other is ObjList) {
|
||||
list.addAll(other.list)
|
||||
} else if (other.isInstanceOf(ObjIterable) && other !is ObjString && other !is ObjBuffer) {
|
||||
} else if (!shouldTreatAsSingleElement(scope, other) && other.isInstanceOf(ObjIterable)) {
|
||||
val otherList = (other.invokeInstanceMethod(scope, "toList") as ObjList).list
|
||||
list.addAll(otherList)
|
||||
} else {
|
||||
@@ -152,6 +161,43 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
return this
|
||||
}
|
||||
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj {
|
||||
val out = list.toMutableList()
|
||||
if (shouldTreatAsSingleElement(scope, other)) {
|
||||
out.remove(other)
|
||||
return ObjList(out)
|
||||
}
|
||||
if (other.isInstanceOf(ObjIterable)) {
|
||||
val toRemove = mutableSetOf<Obj>()
|
||||
other.enumerate(scope) {
|
||||
toRemove += it
|
||||
true
|
||||
}
|
||||
out.removeAll { toRemove.contains(it) }
|
||||
return ObjList(out)
|
||||
}
|
||||
out.remove(other)
|
||||
return ObjList(out)
|
||||
}
|
||||
|
||||
open override suspend fun minusAssign(scope: Scope, other: Obj): Obj {
|
||||
if (shouldTreatAsSingleElement(scope, other)) {
|
||||
list.remove(other)
|
||||
return this
|
||||
}
|
||||
if (other.isInstanceOf(ObjIterable)) {
|
||||
val toRemove = mutableSetOf<Obj>()
|
||||
other.enumerate(scope) {
|
||||
toRemove += it
|
||||
true
|
||||
}
|
||||
list.removeAll { toRemove.contains(it) }
|
||||
return this
|
||||
}
|
||||
list.remove(other)
|
||||
return this
|
||||
}
|
||||
|
||||
override suspend fun contains(scope: Scope, other: Obj): Boolean {
|
||||
if (net.sergeych.lyng.PerfFlags.PRIMITIVE_FASTOPS) {
|
||||
// Fast path: int membership in a list of ints (common case in benches)
|
||||
@@ -175,7 +221,7 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
}
|
||||
}
|
||||
|
||||
override val objClass: ObjClass
|
||||
open override val objClass: ObjClass
|
||||
get() = type
|
||||
|
||||
override suspend fun toKotlin(scope: Scope): Any {
|
||||
@@ -516,6 +562,14 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
}
|
||||
ObjInt((-1).toLong())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toImmutable",
|
||||
doc = "Create an immutable snapshot of this list.",
|
||||
returns = type("lyng.ImmutableList"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjImmutableList(thisAs<ObjList>().list)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,8 +107,12 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
|
||||
override suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is ObjMap) return false
|
||||
if (map.size != other.map.size) return false
|
||||
val otherSize = when (other) {
|
||||
is ObjMap -> other.map.size
|
||||
is ObjImmutableMap -> other.map.size
|
||||
else -> return false
|
||||
}
|
||||
if (map.size != otherSize) return false
|
||||
for ((k, v) in map) {
|
||||
val otherV = other.getAt(scope, k)
|
||||
if (otherV === ObjNull && !other.contains(scope, k)) return false
|
||||
@@ -131,14 +135,16 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if (other is ObjMap) {
|
||||
if (map == other.map) return 0
|
||||
if (map.size != other.map.size) return map.size.compareTo(other.map.size)
|
||||
val otherMap = when (other) {
|
||||
is ObjMap -> other.map
|
||||
is ObjImmutableMap -> other.map
|
||||
else -> return -1
|
||||
}
|
||||
if (map == otherMap) return 0
|
||||
if (map.size != otherMap.size) return map.size.compareTo(otherMap.size)
|
||||
// for same size, if they are not equal, we don't have a stable order
|
||||
// but let's try to be consistent
|
||||
return map.toString().compareTo(other.map.toString())
|
||||
}
|
||||
return -1
|
||||
return map.toString().compareTo(otherMap.toString())
|
||||
}
|
||||
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
@@ -311,6 +317,14 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
) {
|
||||
ObjKotlinIterator(thisAs<ObjMap>().map.entries.iterator())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toImmutable",
|
||||
doc = "Create an immutable snapshot of this map.",
|
||||
returns = type("lyng.ImmutableMap"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjImmutableMap(thisAs<ObjMap>().map)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +348,11 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
map[k] = v
|
||||
}
|
||||
}
|
||||
is ObjImmutableMap -> {
|
||||
for ((k, v) in other.map) {
|
||||
map[k] = v
|
||||
}
|
||||
}
|
||||
is ObjMapEntry -> {
|
||||
map[other.key] = other.value
|
||||
}
|
||||
|
||||
@@ -0,0 +1,589 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import net.sergeych.lyng.Arguments
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.asFacade
|
||||
import net.sergeych.lyng.miniast.ParamDoc
|
||||
import net.sergeych.lyng.miniast.TypeGenericDoc
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.addPropertyDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
|
||||
val ObjObservable = ObjClass("Observable").apply {
|
||||
addFn("beforeChange") { raiseNotImplemented("Observable.beforeChange") }
|
||||
addFn("onChange") { raiseNotImplemented("Observable.onChange") }
|
||||
addFn("changes") { raiseNotImplemented("Observable.changes") }
|
||||
}
|
||||
|
||||
val ObjChangeRejectionExceptionClass = ObjException.Companion.ExceptionClass("ChangeRejectionException", ObjException.Root)
|
||||
|
||||
open class ObjListChange : Obj() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ListChange")
|
||||
}
|
||||
}
|
||||
|
||||
class ObjListSetChange(val index: Int, val oldValue: Obj, val newValue: Obj) : ObjListChange() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ListSet", ObjListChange.type).apply {
|
||||
addPropertyDoc("index", "Changed index.", type("lyng.Int"), moduleName = "lyng.observable", getter = { thisAs<ObjListSetChange>().index.toObj() })
|
||||
addPropertyDoc("oldValue", "Value before assignment.", type("lyng.Any"), moduleName = "lyng.observable", getter = { thisAs<ObjListSetChange>().oldValue })
|
||||
addPropertyDoc("newValue", "Assigned value.", type("lyng.Any"), moduleName = "lyng.observable", getter = { thisAs<ObjListSetChange>().newValue })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ObjListInsertChange(val index: Int, val values: ObjList) : ObjListChange() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ListInsert", ObjListChange.type).apply {
|
||||
addPropertyDoc("index", "Insertion index.", type("lyng.Int"), moduleName = "lyng.observable", getter = { thisAs<ObjListInsertChange>().index.toObj() })
|
||||
addPropertyDoc(
|
||||
"values",
|
||||
"Inserted values.",
|
||||
TypeGenericDoc(type("lyng.List"), listOf(type("lyng.Any"))),
|
||||
moduleName = "lyng.observable",
|
||||
getter = { thisAs<ObjListInsertChange>().values }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ObjListRemoveChange(val index: Int, val oldValue: Obj) : ObjListChange() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ListRemove", ObjListChange.type).apply {
|
||||
addPropertyDoc("index", "Removed index.", type("lyng.Int"), moduleName = "lyng.observable", getter = { thisAs<ObjListRemoveChange>().index.toObj() })
|
||||
addPropertyDoc("oldValue", "Removed value.", type("lyng.Any"), moduleName = "lyng.observable", getter = { thisAs<ObjListRemoveChange>().oldValue })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ObjListClearChange(val oldValues: ObjList) : ObjListChange() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ListClear", ObjListChange.type).apply {
|
||||
addPropertyDoc(
|
||||
"oldValues",
|
||||
"All list values before clear.",
|
||||
TypeGenericDoc(type("lyng.List"), listOf(type("lyng.Any"))),
|
||||
moduleName = "lyng.observable",
|
||||
getter = { thisAs<ObjListClearChange>().oldValues }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ObjListReorderChange(val oldValues: ObjList, val newValues: Obj) : ObjListChange() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ListReorder", ObjListChange.type).apply {
|
||||
addPropertyDoc(
|
||||
"oldValues",
|
||||
"Values before reorder.",
|
||||
TypeGenericDoc(type("lyng.List"), listOf(type("lyng.Any"))),
|
||||
moduleName = "lyng.observable",
|
||||
getter = { thisAs<ObjListReorderChange>().oldValues }
|
||||
)
|
||||
addPropertyDoc(
|
||||
"newValues",
|
||||
"Values after reorder (or Unset in beforeChange when unknown before commit).",
|
||||
type("lyng.Any"),
|
||||
moduleName = "lyng.observable",
|
||||
getter = { thisAs<ObjListReorderChange>().newValues }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ObjSubscription(private val onCancel: suspend () -> Unit) : Obj() {
|
||||
private val access = Mutex()
|
||||
private var cancelled = false
|
||||
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
suspend fun cancel() {
|
||||
var action: (suspend () -> Unit)? = null
|
||||
access.withLock {
|
||||
if (!cancelled) {
|
||||
cancelled = true
|
||||
action = onCancel
|
||||
}
|
||||
}
|
||||
action?.invoke()
|
||||
}
|
||||
|
||||
companion object {
|
||||
val type = object : ObjClass("Subscription") {
|
||||
override suspend fun callOn(scope: Scope): Obj = scope.raiseIllegalArgument("Subscription constructor is not available")
|
||||
}.apply {
|
||||
addFnDoc(
|
||||
"cancel",
|
||||
"Unsubscribe listener. Safe to call multiple times.",
|
||||
returns = type("lyng.Void"),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
thisAs<ObjSubscription>().cancel()
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ObjObservableListFlowProducer(
|
||||
private val owner: ObjObservableList,
|
||||
private val subscriptionId: Long,
|
||||
private val channel: ReceiveChannel<Obj>
|
||||
) : Obj() {
|
||||
override val objClass: ObjClass get() = Obj.rootObjectType
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
val builder = scope.thisObj as? ObjFlowBuilder
|
||||
?: scope.raiseIllegalState("flow builder is not available")
|
||||
try {
|
||||
for (change in channel) {
|
||||
builder.invokeInstanceMethod(scope, "emit", change)
|
||||
}
|
||||
} finally {
|
||||
owner.unsubscribeChangeChannel(subscriptionId)
|
||||
}
|
||||
return ObjVoid
|
||||
}
|
||||
}
|
||||
|
||||
class ObjObservableList(list: MutableList<Obj> = mutableListOf()) : ObjList(list) {
|
||||
private val access = Mutex()
|
||||
private var nextSubscriptionId: Long = 1L
|
||||
private val beforeListeners = linkedMapOf<Long, Obj>()
|
||||
private val afterListeners = linkedMapOf<Long, Obj>()
|
||||
private val changeChannels = linkedMapOf<Long, Channel<Obj>>()
|
||||
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
private fun nextId(): Long = nextSubscriptionId++
|
||||
|
||||
private suspend fun addBeforeListener(listener: Obj): ObjSubscription {
|
||||
val id = access.withLock {
|
||||
val id = nextId()
|
||||
beforeListeners[id] = listener
|
||||
id
|
||||
}
|
||||
return ObjSubscription {
|
||||
access.withLock { beforeListeners.remove(id) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun addAfterListener(listener: Obj): ObjSubscription {
|
||||
val id = access.withLock {
|
||||
val id = nextId()
|
||||
afterListeners[id] = listener
|
||||
id
|
||||
}
|
||||
return ObjSubscription {
|
||||
access.withLock { afterListeners.remove(id) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun notifyListeners(scope: Scope, listeners: Collection<Obj>, change: Obj) {
|
||||
val facade = scope.asFacade()
|
||||
for (listener in listeners) {
|
||||
facade.call(listener, Arguments(change))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun snapshotBeforeListeners(): List<Obj> = access.withLock { beforeListeners.values.toList() }
|
||||
private suspend fun snapshotAfterListeners(): List<Obj> = access.withLock { afterListeners.values.toList() }
|
||||
|
||||
private suspend fun notifyBefore(scope: Scope, change: Obj) {
|
||||
notifyListeners(scope, snapshotBeforeListeners(), change)
|
||||
}
|
||||
|
||||
private suspend fun notifyAfter(scope: Scope, change: Obj) {
|
||||
notifyListeners(scope, snapshotAfterListeners(), change)
|
||||
}
|
||||
|
||||
private suspend fun emitCommittedChange(change: Obj) {
|
||||
val channels = access.withLock { changeChannels.toMap() }
|
||||
val dead = mutableListOf<Long>()
|
||||
for ((id, channel) in channels) {
|
||||
if (channel.trySend(change).isFailure) {
|
||||
dead += id
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
access.withLock {
|
||||
for (id in dead) {
|
||||
changeChannels.remove(id)?.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun mutate(scope: Scope, beforeChange: Obj, mutation: suspend () -> Obj?): Obj? {
|
||||
notifyBefore(scope, beforeChange)
|
||||
val committed = mutation()
|
||||
if (committed != null) {
|
||||
notifyAfter(scope, committed)
|
||||
emitCommittedChange(committed)
|
||||
}
|
||||
return committed
|
||||
}
|
||||
|
||||
private fun snapshotList(): ObjList = ObjList(list.toMutableList())
|
||||
|
||||
private suspend fun createChangesFlow(scope: Scope): ObjFlow {
|
||||
val channel = Channel<Obj>(Channel.UNLIMITED)
|
||||
val id = access.withLock {
|
||||
val id = nextId()
|
||||
changeChannels[id] = channel
|
||||
id
|
||||
}
|
||||
val producer = ObjObservableListFlowProducer(this, id, channel)
|
||||
return ObjFlow(producer, scope)
|
||||
}
|
||||
|
||||
internal suspend fun unsubscribeChangeChannel(id: Long) {
|
||||
access.withLock {
|
||||
changeChannels.remove(id)?.close()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
|
||||
val idx = index.toInt()
|
||||
val old = list[idx]
|
||||
val change = ObjListSetChange(idx, old, newValue)
|
||||
mutate(scope, change) {
|
||||
list[idx] = newValue
|
||||
change
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun plusAssign(scope: Scope, other: Obj): Obj {
|
||||
when {
|
||||
other is ObjList -> {
|
||||
if (other.list.isEmpty()) return this
|
||||
val insert = ObjListInsertChange(list.size, ObjList(other.list.toMutableList()))
|
||||
mutate(scope, insert) {
|
||||
list.addAll(other.list)
|
||||
insert
|
||||
}
|
||||
}
|
||||
|
||||
!shouldTreatAsSingleElement(scope, other) && other.isInstanceOf(ObjIterable) -> {
|
||||
val otherList = (other.invokeInstanceMethod(scope, "toList") as ObjList).list
|
||||
if (otherList.isEmpty()) return this
|
||||
val insert = ObjListInsertChange(list.size, ObjList(otherList.toMutableList()))
|
||||
mutate(scope, insert) {
|
||||
list.addAll(otherList)
|
||||
insert
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
val insert = ObjListInsertChange(list.size, ObjList(mutableListOf(other)))
|
||||
mutate(scope, insert) {
|
||||
list.add(other)
|
||||
insert
|
||||
}
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
override suspend fun minusAssign(scope: Scope, other: Obj): Obj {
|
||||
if (shouldTreatAsSingleElement(scope, other)) {
|
||||
val index = list.indexOf(other)
|
||||
if (index < 0) return this
|
||||
val old = list[index]
|
||||
val remove = ObjListRemoveChange(index, old)
|
||||
mutate(scope, remove) {
|
||||
list.removeAt(index)
|
||||
remove
|
||||
}
|
||||
return this
|
||||
}
|
||||
if (other.isInstanceOf(ObjIterable)) {
|
||||
val toRemove = mutableSetOf<Obj>()
|
||||
other.enumerate(scope) {
|
||||
toRemove += it
|
||||
true
|
||||
}
|
||||
val oldValues = snapshotList()
|
||||
val filtered = list.filterNot { toRemove.contains(it) }
|
||||
if (filtered.size == list.size) return this
|
||||
val committed = if (filtered.isEmpty()) {
|
||||
ObjListClearChange(oldValues)
|
||||
} else {
|
||||
ObjListReorderChange(oldValues, ObjList(filtered.toMutableList()))
|
||||
}
|
||||
mutate(scope, committed) {
|
||||
list.clear()
|
||||
list.addAll(filtered)
|
||||
committed
|
||||
}
|
||||
return this
|
||||
}
|
||||
val index = list.indexOf(other)
|
||||
if (index < 0) return this
|
||||
val old = list[index]
|
||||
val remove = ObjListRemoveChange(index, old)
|
||||
mutate(scope, remove) {
|
||||
list.removeAt(index)
|
||||
remove
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
companion object {
|
||||
val type = object : ObjClass("ObservableList", ObjList.type) {
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
return ObjObservableList(scope.args.list.toMutableList())
|
||||
}
|
||||
}.apply {
|
||||
addFnDoc(
|
||||
"beforeChange",
|
||||
"Subscribe to pre-commit list changes. Throw to reject mutation.",
|
||||
params = listOf(ParamDoc("listener", type("lyng.Any"))),
|
||||
returns = type("lyng.observable.Subscription"),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
thisAs<ObjObservableList>().addBeforeListener(requireOnlyArg())
|
||||
}
|
||||
addFnDoc(
|
||||
"onChange",
|
||||
"Subscribe to post-commit list changes.",
|
||||
params = listOf(ParamDoc("listener", type("lyng.Any"))),
|
||||
returns = type("lyng.observable.Subscription"),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
thisAs<ObjObservableList>().addAfterListener(requireOnlyArg())
|
||||
}
|
||||
addFnDoc(
|
||||
"changes",
|
||||
"Get a flow of committed list changes.",
|
||||
returns = TypeGenericDoc(type("lyng.Flow"), listOf(type("lyng.observable.ListChange"))),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
thisAs<ObjObservableList>().createChangesFlow(requireScope())
|
||||
}
|
||||
addFnDoc(
|
||||
"observable",
|
||||
"Observable list is already observable; returns this.",
|
||||
returns = type("lyng.observable.ObservableList"),
|
||||
moduleName = "lyng.observable"
|
||||
) { thisObj }
|
||||
addFnDoc(
|
||||
"add",
|
||||
"Append one or more elements and emit ListInsert.",
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
val self = thisAs<ObjObservableList>()
|
||||
if (args.isEmpty()) return@addFnDoc ObjVoid
|
||||
val inserted = ObjList(args.list.toMutableList())
|
||||
val change = ObjListInsertChange(self.list.size, inserted)
|
||||
self.mutate(requireScope(), change) {
|
||||
self.list.addAll(args.list)
|
||||
change
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
addFnDoc(
|
||||
"insertAt",
|
||||
"Insert one or more values at index and emit ListInsert.",
|
||||
params = listOf(ParamDoc("index", type("lyng.Int"))),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
if (args.size < 2) raiseError("insertAt takes 2+ arguments")
|
||||
val self = thisAs<ObjObservableList>()
|
||||
val index = requiredArg<ObjInt>(0).value.toInt()
|
||||
val values = args.list.drop(1)
|
||||
val change = ObjListInsertChange(index, ObjList(values.toMutableList()))
|
||||
self.mutate(requireScope(), change) {
|
||||
var i = index
|
||||
for (v in values) self.list.add(i++, v)
|
||||
change
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
addFnDoc(
|
||||
"removeAt",
|
||||
"Remove element at index or range and emit remove/reorder/clear changes.",
|
||||
params = listOf(ParamDoc("start", type("lyng.Int")), ParamDoc("end", type("lyng.Int"))),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
val self = thisAs<ObjObservableList>()
|
||||
val start = requiredArg<ObjInt>(0).value.toInt()
|
||||
if (args.size == 2) {
|
||||
val end = requireOnlyArg<ObjInt>().value.toInt()
|
||||
val oldValues = self.snapshotList()
|
||||
val newValues = self.list.toMutableList().apply { subList(start, end).clear() }
|
||||
if (newValues == self.list) return@addFnDoc self
|
||||
val change = if (newValues.isEmpty()) ObjListClearChange(oldValues)
|
||||
else ObjListReorderChange(oldValues, ObjList(newValues.toMutableList()))
|
||||
self.mutate(requireScope(), change) {
|
||||
self.list.subList(start, end).clear()
|
||||
change
|
||||
}
|
||||
} else {
|
||||
val old = self.list[start]
|
||||
val change = ObjListRemoveChange(start, old)
|
||||
self.mutate(requireScope(), change) {
|
||||
self.list.removeAt(start)
|
||||
change
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
addFnDoc(
|
||||
"removeLast",
|
||||
"Remove last element(s) and emit remove/reorder/clear changes.",
|
||||
params = listOf(ParamDoc("count", type("lyng.Int"))),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
val self = thisAs<ObjObservableList>()
|
||||
if (self.list.isEmpty()) return@addFnDoc self
|
||||
if (args.isNotEmpty()) {
|
||||
val count = requireOnlyArg<ObjInt>().value.toInt()
|
||||
if (count <= 0) return@addFnDoc self
|
||||
val oldValues = self.snapshotList()
|
||||
val newValues = if (count >= self.list.size) emptyList() else self.list.subList(0, self.list.size - count).toList()
|
||||
val change = if (newValues.isEmpty()) ObjListClearChange(oldValues)
|
||||
else ObjListReorderChange(oldValues, ObjList(newValues.toMutableList()))
|
||||
self.mutate(requireScope(), change) {
|
||||
if (count >= self.list.size) self.list.clear()
|
||||
else self.list.subList(self.list.size - count, self.list.size).clear()
|
||||
change
|
||||
}
|
||||
} else {
|
||||
val index = self.list.lastIndex
|
||||
val old = self.list[index]
|
||||
val change = ObjListRemoveChange(index, old)
|
||||
self.mutate(requireScope(), change) {
|
||||
self.list.removeAt(index)
|
||||
change
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
addFnDoc(
|
||||
"removeRange",
|
||||
"Remove a range and emit reorder/clear changes.",
|
||||
params = listOf(ParamDoc("range")),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
val self = thisAs<ObjObservableList>()
|
||||
val before = self.snapshotList()
|
||||
val copy = self.list.toMutableList()
|
||||
val range = requiredArg<Obj>(0)
|
||||
if (range is ObjRange) {
|
||||
val index = range
|
||||
when {
|
||||
index.start is ObjInt && index.end is ObjInt -> {
|
||||
if (index.isEndInclusive) copy.subList(index.start.toInt(), index.end.toInt() + 1)
|
||||
else copy.subList(index.start.toInt(), index.end.toInt())
|
||||
}
|
||||
|
||||
index.isOpenStart && !index.isOpenEnd -> {
|
||||
if (index.isEndInclusive) copy.subList(0, index.end!!.toInt() + 1)
|
||||
else copy.subList(0, index.end!!.toInt())
|
||||
}
|
||||
|
||||
index.isOpenEnd && !index.isOpenStart -> {
|
||||
copy.subList(index.start!!.toInt(), copy.size)
|
||||
}
|
||||
|
||||
index.isOpenStart && index.isOpenEnd -> {
|
||||
copy
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw RuntimeException("Can't apply range for index: $index")
|
||||
}
|
||||
}.clear()
|
||||
} else {
|
||||
val start = range.toInt()
|
||||
val end = requiredArg<ObjInt>(1).value.toInt() + 1
|
||||
copy.subList(start, end).clear()
|
||||
}
|
||||
if (copy == self.list) return@addFnDoc self
|
||||
val change = if (copy.isEmpty()) ObjListClearChange(before)
|
||||
else ObjListReorderChange(before, ObjList(copy.toMutableList()))
|
||||
self.mutate(requireScope(), change) {
|
||||
self.list.clear()
|
||||
self.list.addAll(copy)
|
||||
change
|
||||
}
|
||||
self
|
||||
}
|
||||
addFnDoc(
|
||||
"sortWith",
|
||||
"Sort list in place and emit ListReorder.",
|
||||
params = listOf(ParamDoc("comparator")),
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
val self = thisAs<ObjObservableList>()
|
||||
if (self.list.size < 2) return@addFnDoc ObjVoid
|
||||
val comparator = requireOnlyArg<Obj>()
|
||||
val oldValues = self.snapshotList()
|
||||
val beforeChange = ObjListReorderChange(oldValues, ObjUnset)
|
||||
self.notifyBefore(requireScope(), beforeChange)
|
||||
self.quicksort { a, b -> call(comparator, Arguments(a, b)).toInt() }
|
||||
val newValues = self.snapshotList()
|
||||
if (oldValues.list != newValues.list) {
|
||||
val committed = ObjListReorderChange(oldValues, newValues)
|
||||
self.notifyAfter(requireScope(), committed)
|
||||
self.emitCommittedChange(committed)
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
addFnDoc(
|
||||
"shuffle",
|
||||
"Shuffle list in place and emit ListReorder.",
|
||||
moduleName = "lyng.observable"
|
||||
) {
|
||||
val self = thisAs<ObjObservableList>()
|
||||
if (self.list.size < 2) return@addFnDoc ObjVoid
|
||||
val oldValues = self.snapshotList()
|
||||
val beforeChange = ObjListReorderChange(oldValues, ObjUnset)
|
||||
self.notifyBefore(requireScope(), beforeChange)
|
||||
self.list.shuffle()
|
||||
val newValues = self.snapshotList()
|
||||
if (oldValues.list != newValues.list) {
|
||||
val committed = ObjListReorderChange(oldValues, newValues)
|
||||
self.notifyAfter(requireScope(), committed)
|
||||
self.emitCommittedChange(committed)
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import net.sergeych.lyng.miniast.TypeGenericDoc
|
||||
import net.sergeych.lyng.miniast.addFnDoc
|
||||
import net.sergeych.lyng.miniast.addPropertyDoc
|
||||
import net.sergeych.lyng.miniast.type
|
||||
import net.sergeych.lyng.requireScope
|
||||
|
||||
class ObjRange(
|
||||
val start: Obj?,
|
||||
|
||||
@@ -39,6 +39,7 @@ data class ObjRecord(
|
||||
/** The receiver object to resolve this member against (for instance fields/methods). */
|
||||
var receiver: Obj? = null,
|
||||
val callSignature: net.sergeych.lyng.CallSignature? = null,
|
||||
val typeDecl: net.sergeych.lyng.TypeDecl? = null,
|
||||
val memberName: String? = null,
|
||||
val fieldId: Int? = null,
|
||||
val methodId: Int? = null,
|
||||
|
||||
@@ -469,6 +469,7 @@ class CallRef(
|
||||
internal val args: List<ParsedArgument>,
|
||||
internal val tailBlock: Boolean,
|
||||
internal val isOptionalInvoke: Boolean,
|
||||
internal val explicitTypeArgs: List<TypeDecl>? = null,
|
||||
) : ObjRef {
|
||||
override suspend fun get(scope: Scope): ObjRecord = scope.raiseObjRefEvalDisabled()
|
||||
}
|
||||
|
||||
@@ -17,14 +17,12 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.FrameSlotRef
|
||||
import net.sergeych.lyng.PerfFlags
|
||||
import net.sergeych.lyng.Pos
|
||||
import net.sergeych.lyng.RecordSlotRef
|
||||
import net.sergeych.lyng.RegexCache
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.requireScope
|
||||
|
||||
class ObjRegex(val regex: Regex) : Obj() {
|
||||
override val objClass get() = type
|
||||
@@ -33,18 +31,10 @@ class ObjRegex(val regex: Regex) : Obj() {
|
||||
return regex.find(other.cast<ObjString>(scope).value)?.let {
|
||||
val match = ObjRegexMatch(it)
|
||||
val record = scope.chainLookupIgnoreClosure("$~", followClosure = true)
|
||||
if (record != null) {
|
||||
if (!record.isMutable) {
|
||||
if (record != null && !record.isMutable) {
|
||||
scope.raiseIllegalAssignment("symbol is readonly: $~")
|
||||
}
|
||||
when (val value = record.value) {
|
||||
is FrameSlotRef -> value.write(match)
|
||||
is RecordSlotRef -> value.write(match)
|
||||
else -> record.value = match
|
||||
}
|
||||
} else {
|
||||
scope.addOrUpdateItem("$~", match)
|
||||
}
|
||||
ObjTrue
|
||||
} ?: ObjFalse
|
||||
}
|
||||
|
||||
@@ -27,11 +27,25 @@ import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
private fun shouldTreatAsSingleElement(scope: Scope, other: Obj): Boolean {
|
||||
if (!other.isInstanceOf(ObjIterable)) return true
|
||||
val declaredElementType = scope.declaredSetElementTypeForValue(this)
|
||||
if (declaredElementType != null && matchesTypeDecl(scope, other, declaredElementType)) {
|
||||
return true
|
||||
}
|
||||
// Strings and buffers are iterable but usually expected to be atomic values for set +/- operators.
|
||||
if (other is ObjString || other is ObjBuffer) return true
|
||||
return false
|
||||
}
|
||||
|
||||
override suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is ObjSet) return false
|
||||
if (set.size != other.set.size) return false
|
||||
val otherSet = when (other) {
|
||||
is ObjSet -> other.set
|
||||
is ObjImmutableSet -> other.toMutableSet()
|
||||
else -> return false
|
||||
}
|
||||
if (set.size != otherSet.size) return false
|
||||
// Sets are equal if all my elements are in other and vice versa
|
||||
// contains() in ObjSet uses equals(scope, ...), so we need to be careful
|
||||
for (e in set) {
|
||||
@@ -53,6 +67,9 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
}
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj {
|
||||
if (shouldTreatAsSingleElement(scope, other)) {
|
||||
return ObjSet((set + other).toMutableSet())
|
||||
}
|
||||
return ObjSet(
|
||||
if (other is ObjSet)
|
||||
(set + other.set).toMutableSet()
|
||||
@@ -73,6 +90,10 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
}
|
||||
|
||||
override suspend fun plusAssign(scope: Scope, other: Obj): Obj {
|
||||
if (shouldTreatAsSingleElement(scope, other)) {
|
||||
set += other
|
||||
return this
|
||||
}
|
||||
when (other) {
|
||||
is ObjSet -> {
|
||||
set += other.set
|
||||
@@ -98,13 +119,17 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
}
|
||||
|
||||
override suspend fun mul(scope: Scope, other: Obj): Obj {
|
||||
return if (other is ObjSet) {
|
||||
ObjSet(set.intersect(other.set).toMutableSet())
|
||||
} else
|
||||
scope.raiseIllegalArgument("set operator * requires another set")
|
||||
return when (other) {
|
||||
is ObjSet -> ObjSet(set.intersect(other.set).toMutableSet())
|
||||
is ObjImmutableSet -> ObjSet(set.intersect(other.toMutableSet()).toMutableSet())
|
||||
else -> scope.raiseIllegalArgument("set operator * requires another set")
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj {
|
||||
if (shouldTreatAsSingleElement(scope, other)) {
|
||||
return ObjSet((set - other).toMutableSet())
|
||||
}
|
||||
return when {
|
||||
other is ObjSet -> ObjSet(set.minus(other.set).toMutableSet())
|
||||
other.isInstanceOf(ObjIterable) -> {
|
||||
@@ -115,8 +140,7 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
}
|
||||
ObjSet((set - otherSet).toMutableSet())
|
||||
}
|
||||
else ->
|
||||
scope.raiseIllegalArgument("set operator - requires another set or Iterable")
|
||||
else -> ObjSet((set - other).toMutableSet())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,12 +149,14 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if (other is ObjSet) {
|
||||
if (set == other.set) return 0
|
||||
if (set.size != other.set.size) return set.size.compareTo(other.set.size)
|
||||
return set.toString().compareTo(other.set.toString())
|
||||
val otherSet = when (other) {
|
||||
is ObjSet -> other.set
|
||||
is ObjImmutableSet -> other.toMutableSet()
|
||||
else -> return -2
|
||||
}
|
||||
return -2
|
||||
if (set == otherSet) return 0
|
||||
if (set.size != otherSet.size) return set.size.compareTo(otherSet.size)
|
||||
return set.toString().compareTo(otherSet.toString())
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
@@ -214,6 +240,14 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
for( x in args.list ) set -= x
|
||||
if( n == set.size ) ObjFalse else ObjTrue
|
||||
}
|
||||
addFnDoc(
|
||||
name = "toImmutable",
|
||||
doc = "Create an immutable snapshot of this set.",
|
||||
returns = type("lyng.ImmutableSet"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjImmutableSet(thisAs<ObjSet>().set)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,6 +12,7 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
@@ -63,6 +64,7 @@ internal fun matchesTypeDecl(scope: Scope, value: Obj, typeDecl: TypeDecl): Bool
|
||||
if (cls != null) value.isInstanceOf(cls) else value.isInstanceOf(typeDecl.name.substringAfterLast('.'))
|
||||
}
|
||||
is TypeDecl.Function -> value.isInstanceOf("Callable")
|
||||
is TypeDecl.Ellipsis -> matchesTypeDecl(scope, value, typeDecl.elementType)
|
||||
is TypeDecl.Union -> typeDecl.options.any { matchesTypeDecl(scope, value, it) }
|
||||
is TypeDecl.Intersection -> typeDecl.options.all { matchesTypeDecl(scope, value, it) }
|
||||
}
|
||||
@@ -90,13 +92,16 @@ internal fun typeDeclIsSubtype(scope: Scope, left: TypeDecl, right: TypeDecl): B
|
||||
return when (l) {
|
||||
is TypeDecl.Union -> l.options.all { typeDeclIsSubtype(scope, it, r) }
|
||||
is TypeDecl.Intersection -> l.options.any { typeDeclIsSubtype(scope, it, r) }
|
||||
is TypeDecl.Ellipsis -> typeDeclIsSubtype(scope, l.elementType, r)
|
||||
else -> when (r) {
|
||||
is TypeDecl.Union -> r.options.any { typeDeclIsSubtype(scope, l, it) }
|
||||
is TypeDecl.Intersection -> r.options.all { typeDeclIsSubtype(scope, l, it) }
|
||||
is TypeDecl.Simple, is TypeDecl.Generic, is TypeDecl.Function -> {
|
||||
is TypeDecl.Simple, is TypeDecl.Generic, is TypeDecl.Function, is TypeDecl.Ellipsis -> {
|
||||
val leftClass = resolveTypeDeclClass(scope, l) ?: return false
|
||||
val rightClass = resolveTypeDeclClass(scope, r) ?: return false
|
||||
leftClass == rightClass || leftClass.allParentsSet.contains(rightClass)
|
||||
leftClass == rightClass ||
|
||||
rightClass == Obj.rootObjectType ||
|
||||
leftClass.allParentsSet.contains(rightClass)
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
@@ -171,6 +176,7 @@ private fun stripNullable(type: TypeDecl): TypeDecl {
|
||||
} else {
|
||||
when (type) {
|
||||
is TypeDecl.Function -> type.copy(nullable = false)
|
||||
is TypeDecl.Ellipsis -> type.copy(nullable = false)
|
||||
is TypeDecl.TypeVar -> type.copy(nullable = false)
|
||||
is TypeDecl.Union -> type.copy(nullable = false)
|
||||
is TypeDecl.Intersection -> type.copy(nullable = false)
|
||||
@@ -186,6 +192,7 @@ private fun makeNullable(type: TypeDecl): TypeDecl {
|
||||
TypeDecl.TypeAny -> TypeDecl.TypeNullableAny
|
||||
TypeDecl.TypeNullableAny -> type
|
||||
is TypeDecl.Function -> type.copy(nullable = true)
|
||||
is TypeDecl.Ellipsis -> type.copy(nullable = true)
|
||||
is TypeDecl.TypeVar -> type.copy(nullable = true)
|
||||
is TypeDecl.Union -> type.copy(nullable = true)
|
||||
is TypeDecl.Intersection -> type.copy(nullable = true)
|
||||
@@ -200,6 +207,7 @@ private fun typeDeclKey(type: TypeDecl): String = when (type) {
|
||||
is TypeDecl.Simple -> "S:${type.name}"
|
||||
is TypeDecl.Generic -> "G:${type.name}<${type.args.joinToString(",") { typeDeclKey(it) }}>"
|
||||
is TypeDecl.Function -> "F:(${type.params.joinToString(",") { typeDeclKey(it) }})->${typeDeclKey(type.returnType)}"
|
||||
is TypeDecl.Ellipsis -> "E:${typeDeclKey(type.elementType)}"
|
||||
is TypeDecl.TypeVar -> "V:${type.name}"
|
||||
is TypeDecl.Union -> "U:${type.options.joinToString("|") { typeDeclKey(it) }}"
|
||||
is TypeDecl.Intersection -> "I:${type.options.joinToString("&") { typeDeclKey(it) }}"
|
||||
@@ -216,6 +224,7 @@ private fun resolveTypeDeclClass(scope: Scope, type: TypeDecl): ObjClass? {
|
||||
direct ?: scope[type.name.substringAfterLast('.')]?.value as? ObjClass
|
||||
}
|
||||
is TypeDecl.Function -> scope["Callable"]?.value as? ObjClass
|
||||
is TypeDecl.Ellipsis -> resolveTypeDeclClass(scope, type.elementType)
|
||||
is TypeDecl.TypeVar -> {
|
||||
val bound = scope[type.name]?.value
|
||||
when (bound) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -12,17 +12,28 @@
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import net.sergeych.lyng.Pos
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.ScopeFacade
|
||||
import net.sergeych.lyng.raiseAssertionFailed as coreRaiseAssertionFailed
|
||||
import net.sergeych.lyng.raiseError as coreRaiseError
|
||||
import net.sergeych.lyng.raiseIllegalAssignment as coreRaiseIllegalAssignment
|
||||
import net.sergeych.lyng.raiseIllegalOperation as coreRaiseIllegalOperation
|
||||
import net.sergeych.lyng.raiseIndexOutOfBounds as coreRaiseIndexOutOfBounds
|
||||
import net.sergeych.lyng.raiseIterationFinished as coreRaiseIterationFinished
|
||||
import net.sergeych.lyng.raiseNPE as coreRaiseNPE
|
||||
import net.sergeych.lyng.raiseNotFound as coreRaiseNotFound
|
||||
import net.sergeych.lyng.raiseUnset as coreRaiseUnset
|
||||
import net.sergeych.lyng.requireExactCount as coreRequireExactCount
|
||||
import net.sergeych.lyng.requireNoArgs as coreRequireNoArgs
|
||||
import net.sergeych.lyng.requireOnlyArg as coreRequireOnlyArg
|
||||
import net.sergeych.lyng.requiredArg as coreRequiredArg
|
||||
import net.sergeych.lyng.requireScope as coreRequireScope
|
||||
import net.sergeych.lyng.requiredArg as coreRequiredArg
|
||||
import net.sergeych.lyng.thisAs as coreThisAs
|
||||
|
||||
inline fun <reified T : Obj> ScopeFacade.requiredArg(index: Int): T = coreRequiredArg(index)
|
||||
@@ -36,3 +47,29 @@ fun ScopeFacade.requireNoArgs() = coreRequireNoArgs()
|
||||
inline fun <reified T : Obj> ScopeFacade.thisAs(): T = coreThisAs()
|
||||
|
||||
internal fun ScopeFacade.requireScope(): Scope = coreRequireScope()
|
||||
|
||||
fun ScopeFacade.raiseNPE(): Nothing = coreRaiseNPE()
|
||||
|
||||
fun ScopeFacade.raiseIndexOutOfBounds(message: String = "Index out of bounds"): Nothing =
|
||||
coreRaiseIndexOutOfBounds(message)
|
||||
|
||||
fun ScopeFacade.raiseIllegalAssignment(message: String): Nothing =
|
||||
coreRaiseIllegalAssignment(message)
|
||||
|
||||
fun ScopeFacade.raiseUnset(message: String = "property is unset (not initialized)"): Nothing =
|
||||
coreRaiseUnset(message)
|
||||
|
||||
fun ScopeFacade.raiseNotFound(message: String = "not found"): Nothing =
|
||||
coreRaiseNotFound(message)
|
||||
|
||||
fun ScopeFacade.raiseError(obj: Obj, pos: Pos = this.pos, message: String): Nothing =
|
||||
coreRaiseError(obj, pos, message)
|
||||
|
||||
fun ScopeFacade.raiseAssertionFailed(message: String): Nothing =
|
||||
coreRaiseAssertionFailed(message)
|
||||
|
||||
fun ScopeFacade.raiseIllegalOperation(message: String = "Operation is illegal"): Nothing =
|
||||
coreRaiseIllegalOperation(message)
|
||||
|
||||
fun ScopeFacade.raiseIterationFinished(): Nothing =
|
||||
coreRaiseIterationFinished()
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -69,10 +69,10 @@ abstract class ImportProvider(
|
||||
val plan: Map<String, Int>
|
||||
)
|
||||
|
||||
private var cachedStdScope = CachedExpression<StdScopeSeed>()
|
||||
private var cachedStdScopeSeed = CachedExpression<StdScopeSeed>()
|
||||
|
||||
suspend fun newStdScope(pos: Pos = Pos.builtIn): Scope {
|
||||
val seed = cachedStdScope.get {
|
||||
suspend fun newStdScope(pos: Pos = Pos.builtIn): ModuleScope {
|
||||
val seed = cachedStdScopeSeed.get {
|
||||
val stdlib = prepareImport(pos, "lyng.stdlib", null)
|
||||
val plan = LinkedHashMap<String, Int>()
|
||||
for ((name, record) in stdlib.objects) {
|
||||
@@ -84,7 +84,8 @@ abstract class ImportProvider(
|
||||
val module = newModuleAt(pos)
|
||||
if (seed.plan.isNotEmpty()) module.applySlotPlan(seed.plan)
|
||||
seed.stdlib.importInto(module, null)
|
||||
// Predeclare regex match result slot ($~) in every module scope.
|
||||
module.addOrUpdateItem("$~", net.sergeych.lyng.obj.ObjNull)
|
||||
return module
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.stdlib_included
|
||||
|
||||
@Suppress("Unused", "MemberVisibilityCanBePrivate")
|
||||
internal val observableLyng = """
|
||||
package lyng.observable
|
||||
|
||||
extern class ChangeRejectionException : Exception
|
||||
|
||||
extern class Subscription {
|
||||
fun cancel(): Void
|
||||
}
|
||||
|
||||
extern class Observable<Change> {
|
||||
fun beforeChange(listener: (Change)->Void): Subscription
|
||||
fun onChange(listener: (Change)->Void): Subscription
|
||||
fun changes(): Flow<Change>
|
||||
}
|
||||
|
||||
extern class ListChange<T>
|
||||
|
||||
extern class ListSet<T> : ListChange<T> {
|
||||
val index: Int
|
||||
val oldValue: Object
|
||||
val newValue: Object
|
||||
}
|
||||
|
||||
extern class ListInsert<T> : ListChange<T> {
|
||||
val index: Int
|
||||
val values: List<T>
|
||||
}
|
||||
|
||||
extern class ListRemove<T> : ListChange<T> {
|
||||
val index: Int
|
||||
val oldValue: Object
|
||||
}
|
||||
|
||||
extern class ListClear<T> : ListChange<T> {
|
||||
val oldValues: List<T>
|
||||
}
|
||||
|
||||
extern class ListReorder<T> : ListChange<T> {
|
||||
val oldValues: List<T>
|
||||
val newValues: Object
|
||||
}
|
||||
|
||||
extern class ObservableList<T> : List<T> {
|
||||
fun beforeChange(listener: (ListChange<T>)->Void): Subscription
|
||||
fun onChange(listener: (ListChange<T>)->Void): Subscription
|
||||
fun changes(): Flow<ListChange<T>>
|
||||
}
|
||||
|
||||
fun List<T>.observable(): ObservableList<T> {
|
||||
if( this is ObservableList<T> ) this
|
||||
else ObservableList(...this)
|
||||
}
|
||||
""".trimIndent()
|
||||
@@ -23,6 +23,9 @@ package net.sergeych.lyng
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.binding.Binder
|
||||
import net.sergeych.lyng.miniast.MiniAstBuilder
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjClass
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
@@ -129,4 +132,56 @@ class BindingTest {
|
||||
val refs = snap.references.count { it.symbolId == xField.id }
|
||||
assertEquals(1, refs)
|
||||
}
|
||||
|
||||
class ObjA: Obj() {
|
||||
override val objClass = type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("ObjA").apply {
|
||||
addFn("get1") {
|
||||
ObjString("get1")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShortFormMethod() = runTest {
|
||||
eval("""
|
||||
class A {
|
||||
fun get1() = "1"
|
||||
fun get2() = get1() + "-2"
|
||||
fun get3(): String = get2() + "-3"
|
||||
override fun toString() = "!"+get3()+"!"
|
||||
}
|
||||
assert(A().get3() == "1-2-3")
|
||||
assert(A().toString() == "!1-2-3!")
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLateGlobalBinding() = runTest {
|
||||
val ms = Script.newScope()
|
||||
ms.eval("""
|
||||
extern class A {
|
||||
fun get1(): String
|
||||
}
|
||||
|
||||
extern fun getA(): A
|
||||
|
||||
fun getB(a: A) = a.get1() + "-2"
|
||||
""".trimIndent())
|
||||
|
||||
ms.addFn("getA") {
|
||||
ObjA()
|
||||
}
|
||||
ms.addConst("A", ObjA.type)
|
||||
ms.eval("""
|
||||
assert(A() is A)
|
||||
assert(getA() is A)
|
||||
assertEquals(getB(getA()), "get1-2")
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.bridge.LyngClassBridge
|
||||
import net.sergeych.lyng.bridge.bindObject
|
||||
import net.sergeych.lyng.bridge.data
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjClass
|
||||
import net.sergeych.lyng.obj.ObjInt
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class BridgeBindingTest {
|
||||
private data class CounterState(var count: Long)
|
||||
@@ -42,39 +61,39 @@ class BridgeBindingTest {
|
||||
init { _ ->
|
||||
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 net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
get = {
|
||||
val st = (thisObj as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
ObjInt.of(st.count)
|
||||
},
|
||||
set = { _, instance, value ->
|
||||
val st = (instance as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
set = { value ->
|
||||
val st = (thisObj as net.sergeych.lyng.obj.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) }
|
||||
}
|
||||
|
||||
LyngClassBridge.bind(className = "Bar", module = "bridge.mod", importManager = im) {
|
||||
initWithInstance { _, instance ->
|
||||
(instance as net.sergeych.lyng.obj.ObjInstance).data = CounterState(10)
|
||||
initWithInstance {
|
||||
(thisObj as net.sergeych.lyng.obj.ObjInstance).data = CounterState(10)
|
||||
}
|
||||
addVar(
|
||||
"count",
|
||||
get = { _, instance ->
|
||||
val st = (instance as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
get = {
|
||||
val st = (thisObj as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
ObjInt.of(st.count)
|
||||
},
|
||||
set = { _, instance, value ->
|
||||
val st = (instance as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
set = { value ->
|
||||
val st = (thisObj as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
st.count = (value as ObjInt).value
|
||||
}
|
||||
)
|
||||
@@ -137,7 +156,7 @@ class BridgeBindingTest {
|
||||
|
||||
val bindFailed = try {
|
||||
LyngClassBridge.bind(className = "Late", module = "bridge.late", importManager = im) {
|
||||
addVal("status") { _, _ -> ObjString("late") }
|
||||
addVal("status") { ObjString("late") }
|
||||
}
|
||||
false
|
||||
} catch (_: ScriptError) {
|
||||
@@ -145,4 +164,87 @@ class BridgeBindingTest {
|
||||
}
|
||||
assertTrue(bindFailed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testExternObjectBinding() = runTest {
|
||||
val im = Script.defaultImportManager.copy()
|
||||
im.addPackage("bridge.obj") { scope ->
|
||||
scope.eval(
|
||||
"""
|
||||
extern object HostObject {
|
||||
extern fun add(a: Int, b: Int): Int
|
||||
extern val status: String
|
||||
extern var count: Int
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
scope.bindObject("HostObject") {
|
||||
classData = "OK"
|
||||
init { _ ->
|
||||
data = CounterState(5)
|
||||
}
|
||||
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) }
|
||||
addVar(
|
||||
"count",
|
||||
get = {
|
||||
val st = (thisObj as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
ObjInt.of(st.count)
|
||||
},
|
||||
set = { value ->
|
||||
val st = (thisObj as net.sergeych.lyng.obj.ObjInstance).data as CounterState
|
||||
st.count = (value as ObjInt).value
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val scope = im.newStdScope()
|
||||
scope.eval(
|
||||
"""
|
||||
import bridge.obj
|
||||
assertEquals(42, HostObject.add(10, 32))
|
||||
assertEquals("OK", HostObject.status)
|
||||
assertEquals(5, HostObject.count)
|
||||
HostObject.count = HostObject.count + 1
|
||||
assertEquals(6, HostObject.count)
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
class ObjA: Obj() {
|
||||
companion object {
|
||||
val type = ObjClass("A").apply {
|
||||
addProperty("field",{
|
||||
ObjInt.of(42)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBindExternClass() = runTest {
|
||||
val ms = Script.newScope()
|
||||
ms.eval("""
|
||||
extern class A {
|
||||
val field: Int
|
||||
}
|
||||
|
||||
fun test(a: A) = a.field
|
||||
|
||||
val prop = dynamic {
|
||||
get { name ->
|
||||
name + "=" + A().field
|
||||
}
|
||||
}
|
||||
|
||||
""".trimIndent())
|
||||
ms.addConst("A", ObjA.type)
|
||||
ms.eval("assertEquals(42, test(A()))")
|
||||
ms.eval("assertEquals(\"test=42\", prop.test)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.bridge.bindGlobalFun1
|
||||
import net.sergeych.lyng.bridge.bindGlobalFun3
|
||||
import net.sergeych.lyng.bridge.bindGlobalVar
|
||||
import net.sergeych.lyng.bridge.globalBinder
|
||||
import net.sergeych.lyng.obj.ObjInt
|
||||
import net.sergeych.lyng.obj.ObjString
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class GlobalBindingTest {
|
||||
@Test
|
||||
fun testPackageGlobalFunAndVarBinding() = runTest {
|
||||
val im = Script.defaultImportManager.copy()
|
||||
var prop = "initial"
|
||||
im.addPackage("bridge.globals") { module ->
|
||||
module.eval(
|
||||
"""
|
||||
extern fun globalFun(v: Int): Int
|
||||
extern fun join3(a: String, b: String, c: String): String
|
||||
extern var globalProp: String
|
||||
extern val answer: Int
|
||||
""".trimIndent()
|
||||
)
|
||||
val binder = module.globalBinder()
|
||||
binder.bindGlobalFun1<Int>("globalFun") { v ->
|
||||
ObjInt.of((v + 10).toLong())
|
||||
}
|
||||
binder.bindGlobalFun3<String, String, String>("join3") { a, b, c ->
|
||||
ObjString(a + b + c)
|
||||
}
|
||||
binder.bindGlobalVar(
|
||||
name = "globalProp",
|
||||
get = { prop },
|
||||
set = { prop = it }
|
||||
)
|
||||
binder.bindGlobalVar(
|
||||
name = "answer",
|
||||
get = { 42 }
|
||||
)
|
||||
}
|
||||
|
||||
val scope = im.newStdScope()
|
||||
scope.eval(
|
||||
"""
|
||||
import bridge.globals
|
||||
assertEquals(15, globalFun(5))
|
||||
assertEquals("abc", join3("a", "b", "c"))
|
||||
assertEquals("initial", globalProp)
|
||||
globalProp = "changed"
|
||||
assertEquals("changed", globalProp)
|
||||
assertEquals(42, answer)
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPackageGlobalRawAndArgReaderBinding() = runTest {
|
||||
val im = Script.defaultImportManager.copy()
|
||||
im.addPackage("bridge.raw") { module ->
|
||||
module.eval(
|
||||
"""
|
||||
extern fun sum3(a: Int, b: Int, c: Int): Int
|
||||
extern fun echoRaw(x: Int): Int
|
||||
""".trimIndent()
|
||||
)
|
||||
val binder = module.globalBinder()
|
||||
binder.bindGlobalFun("sum3") {
|
||||
requireExactCount(3)
|
||||
ObjInt.of((int(0) + int(1) + int(2)).toLong())
|
||||
}
|
||||
binder.bindGlobalFunRaw("echoRaw") { _, args ->
|
||||
args.firstAndOnly()
|
||||
}
|
||||
}
|
||||
val scope = im.newStdScope()
|
||||
scope.eval(
|
||||
"""
|
||||
import bridge.raw
|
||||
assertEquals(6, sum3(1, 2, 3))
|
||||
assertEquals(77, echoRaw(77))
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGlobalVarExternCompatibilityChecks() = runTest {
|
||||
val im = Script.defaultImportManager.copy()
|
||||
im.addPackage("bridge.compat") { module ->
|
||||
module.eval(
|
||||
"""
|
||||
extern var needsSetter: String
|
||||
extern val noSetterAllowed: String
|
||||
""".trimIndent()
|
||||
)
|
||||
val binder = module.globalBinder()
|
||||
val missingSetter = try {
|
||||
binder.bindGlobalVar(
|
||||
name = "needsSetter",
|
||||
get = { "x" }
|
||||
)
|
||||
false
|
||||
} catch (_: ScriptError) {
|
||||
true
|
||||
}
|
||||
val readonlySetter = try {
|
||||
binder.bindGlobalVar(
|
||||
name = "noSetterAllowed",
|
||||
get = { "x" },
|
||||
set = { _ -> }
|
||||
)
|
||||
false
|
||||
} catch (_: ScriptError) {
|
||||
true
|
||||
}
|
||||
assertTrue(missingSetter)
|
||||
assertTrue(readonlySetter)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFails
|
||||
|
||||
class ImmutableCollectionsTest {
|
||||
@Test
|
||||
fun immutableListSnapshotAndConversion() = runTest {
|
||||
eval(
|
||||
"""
|
||||
val src = [1,2,3]
|
||||
val imm = src.toImmutable()
|
||||
assert(imm is ImmutableList<Int>)
|
||||
assert(imm is Array<Int>)
|
||||
assert(imm is Collection<Int>)
|
||||
assert(imm is Iterable<Int>)
|
||||
|
||||
src += 4
|
||||
assertEquals(3, imm.size)
|
||||
assertEquals([1,2,3], imm.toMutable())
|
||||
assertEquals([1,2,3], (1..3).toImmutableList().toMutable())
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun immutableSetSnapshotAndConversion() = runTest {
|
||||
eval(
|
||||
"""
|
||||
val src = Set(1,2,3)
|
||||
val imm = src.toImmutable()
|
||||
assert(imm is ImmutableSet<Int>)
|
||||
assert(imm is Collection<Int>)
|
||||
assert(imm is Iterable<Int>)
|
||||
src += 4
|
||||
assertEquals(3, imm.size)
|
||||
assertEquals(Set(1,2,3), imm.toMutable())
|
||||
assertEquals(Set(1,2,3), [1,2,3].toImmutableSet.toMutable())
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun immutableMapSnapshotAndConversion() = runTest {
|
||||
eval(
|
||||
"""
|
||||
val src = Map("a" => 1, "b" => 2)
|
||||
val imm = src.toImmutable()
|
||||
assert(imm is ImmutableMap<String,Int>)
|
||||
assert(imm is Collection<MapEntry<String,Int>>)
|
||||
assert(imm is Iterable<MapEntry<String,Int>>)
|
||||
src["a"] = 100
|
||||
assertEquals(1, imm["a"])
|
||||
assertEquals(Map("a" => 1, "b" => 2), imm.toMutable())
|
||||
|
||||
val imm2 = ["x" => 10, "y" => 20].toImmutableMap
|
||||
assertEquals(10, imm2["x"])
|
||||
assertEquals(Map("x" => 10, "y" => 20), imm2.toMutable())
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun immutableCollectionsRejectMutationOps() = runTest {
|
||||
assertFails {
|
||||
eval(
|
||||
"""
|
||||
val xs = ImmutableList(1,2,3)
|
||||
xs += 4
|
||||
"""
|
||||
)
|
||||
}
|
||||
assertFails {
|
||||
eval(
|
||||
"""
|
||||
val s = ImmutableSet(1,2,3)
|
||||
s += 4
|
||||
"""
|
||||
)
|
||||
}
|
||||
assertFails {
|
||||
eval(
|
||||
"""
|
||||
val m = ImmutableMap("a" => 1)
|
||||
m["a"] = 10
|
||||
"""
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ class NestedRangeBenchmarkTest {
|
||||
}
|
||||
var depth = 1
|
||||
while (current is BytecodeStatement && current.original is ForInStatement) {
|
||||
val original = current.original as ForInStatement
|
||||
val original = current.original
|
||||
println(
|
||||
"[DEBUG_LOG] [BENCH] nested-happy loop depth=$depth " +
|
||||
"constRange=${original.constRange} canBreak=${original.canBreak} " +
|
||||
|
||||
@@ -70,9 +70,10 @@ class OOTest {
|
||||
|
||||
@Test
|
||||
fun testDynamicGet() = runTest {
|
||||
eval(
|
||||
val ms = Script.newScope()
|
||||
ms.eval(
|
||||
"""
|
||||
val accessor: Delegate = dynamic {
|
||||
val accessor: String = dynamic {
|
||||
get { name ->
|
||||
if( name == "foo" ) "bar" else null
|
||||
}
|
||||
@@ -84,6 +85,10 @@ class OOTest {
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
ms.eval("""
|
||||
assertEquals("bar", accessor.foo)
|
||||
assertEquals(null, accessor.bad)
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -906,4 +911,57 @@ class OOTest {
|
||||
assertEquals(5, t.x)
|
||||
""".trimIndent())
|
||||
}
|
||||
@Test
|
||||
fun testDynamicToDynamic() = runTest {
|
||||
val ms = Script.newScope()
|
||||
ms.eval("""
|
||||
|
||||
class A(prefix) {
|
||||
val da = dynamic {
|
||||
get { name -> "a:"+prefix+":"+name }
|
||||
}
|
||||
}
|
||||
|
||||
val B: A = dynamic {
|
||||
get { p -> A(p) }
|
||||
}
|
||||
assertEquals(A("bar").da.foo, "a:bar:foo")
|
||||
assertEquals( B.buzz.da.foo, "a:buzz:foo" )
|
||||
|
||||
val C = dynamic {
|
||||
get { p -> A(p).da }
|
||||
}
|
||||
|
||||
assertEquals(C.buzz.foo, "a:buzz:foo")
|
||||
""".trimIndent())
|
||||
ms.eval("""
|
||||
""")
|
||||
}
|
||||
@Test
|
||||
fun testDynamicToDynamicFun() = runTest {
|
||||
val ms = Script.newScope()
|
||||
ms.eval("""
|
||||
|
||||
class A(prefix) {
|
||||
val da = dynamic {
|
||||
get { name -> { x -> "a:"+prefix+":"+name+"/"+x } }
|
||||
}
|
||||
}
|
||||
|
||||
val B: A = dynamic {
|
||||
get { p -> A(p) }
|
||||
}
|
||||
assertEquals(A("bar").da.foo("buzz"), "a:bar:foo/buzz")
|
||||
assertEquals( B.buzz.da.foo("42"), "a:buzz:foo/42" )
|
||||
|
||||
val C = dynamic {
|
||||
get { p -> A(p).da }
|
||||
}
|
||||
|
||||
assertEquals(C.buzz.foo("one"), "a:buzz:foo/one")
|
||||
""".trimIndent())
|
||||
ms.eval("""
|
||||
""")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
|
||||
class ObservableListTest {
|
||||
@Test
|
||||
fun observableHooksAndSubscriptionCancel() = runTest {
|
||||
eval(
|
||||
"""
|
||||
import lyng.observable
|
||||
|
||||
val src = [1,2]
|
||||
val xs = src.observable()
|
||||
assert(xs is ObservableList<Int>)
|
||||
assert(src == [1,2])
|
||||
assert(xs == [1,2])
|
||||
|
||||
var before = 0
|
||||
var after = 0
|
||||
val beforeSub = xs.beforeChange { before++ }
|
||||
val afterSub = xs.onChange { after++ }
|
||||
|
||||
xs += 3
|
||||
assertEquals([1,2,3], xs)
|
||||
assertEquals(1, before)
|
||||
assertEquals(1, after)
|
||||
|
||||
afterSub.cancel()
|
||||
xs[0] = 100
|
||||
assertEquals([100,2,3], xs)
|
||||
assertEquals(2, before)
|
||||
assertEquals(1, after)
|
||||
|
||||
beforeSub.cancel()
|
||||
xs.removeAt(0)
|
||||
assertEquals([2,3], xs)
|
||||
assertEquals(2, before)
|
||||
assertEquals(1, after)
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun observableBeforeChangeRejectsMutation() = runTest {
|
||||
eval(
|
||||
"""
|
||||
import lyng.observable
|
||||
|
||||
val xs = [1,2].observable()
|
||||
var after = 0
|
||||
xs.beforeChange {
|
||||
throw ChangeRejectionException("no changes accepted")
|
||||
}
|
||||
xs.onChange { after++ }
|
||||
|
||||
assertThrows(ChangeRejectionException) {
|
||||
xs += 3
|
||||
}
|
||||
assertEquals([1,2], xs)
|
||||
assertEquals(0, after)
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun observableChangesFlowEmitsCommittedEvents() = runTest {
|
||||
eval(
|
||||
"""
|
||||
import lyng.observable
|
||||
|
||||
val xs = [10,20].observable()
|
||||
val it = xs.changes().iterator()
|
||||
|
||||
xs += 30
|
||||
assert(it.hasNext())
|
||||
val e1 = it.next()
|
||||
assert(e1 is ListInsert<Int>)
|
||||
assertEquals(2, (e1 as ListInsert<Int>).index)
|
||||
assertEquals([30], e1.values)
|
||||
|
||||
xs[1] = 200
|
||||
assert(it.hasNext())
|
||||
val e2 = it.next()
|
||||
assert(e2 is ListSet<Int>)
|
||||
assertEquals(1, (e2 as ListSet<Int>).index)
|
||||
assertEquals(20, e2.oldValue)
|
||||
assertEquals(200, e2.newValue)
|
||||
|
||||
xs.removeAt(0)
|
||||
assert(it.hasNext())
|
||||
val e3 = it.next()
|
||||
assert(e3 is ListRemove<Int>)
|
||||
assertEquals(0, (e3 as ListRemove<Int>).index)
|
||||
assertEquals(10, e3.oldValue)
|
||||
|
||||
it.cancelIteration()
|
||||
"""
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,12 @@
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.Script
|
||||
import net.sergeych.lyng.ScriptError
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class TypesTest {
|
||||
|
||||
@@ -179,6 +182,43 @@ class TypesTest {
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAssertIsSmartCastEnablesMemberCall() = runTest {
|
||||
eval(
|
||||
"""
|
||||
class Ctx {
|
||||
val contract = null
|
||||
fun println(msg: String) = msg
|
||||
}
|
||||
fun use(callContext) {
|
||||
assert(callContext is Ctx)
|
||||
assert(callContext.contract == null)
|
||||
callContext.println("hello")
|
||||
}
|
||||
assertEquals("hello", use(Ctx()))
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBareIsExpressionDoesNotSmartCastForMemberCall() = runTest {
|
||||
val ex = assertFailsWith<ScriptError> {
|
||||
eval(
|
||||
"""
|
||||
class Ctx {
|
||||
fun println(msg: String) = msg
|
||||
}
|
||||
fun use(callContext) {
|
||||
callContext is Ctx
|
||||
callContext.println("hello")
|
||||
}
|
||||
use(Ctx())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
assertTrue(ex.message?.contains("member access requires compile-time receiver type: println") == true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListLiteralInferenceForBounds() = runTest {
|
||||
eval("""
|
||||
@@ -302,4 +342,274 @@ class TypesTest {
|
||||
}
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLambdaTypes1() = runTest {
|
||||
val scope = Script.newScope()
|
||||
// declare: ok
|
||||
scope.eval("""
|
||||
var l1: (Int,String)->String
|
||||
""".trimIndent())
|
||||
// this should be Lyng compile time exception
|
||||
assertFailsWith<ScriptError> {
|
||||
scope.eval("""
|
||||
fun test() {
|
||||
// compiler should detect that l1 us called with arguments that does not match
|
||||
// declare type (Int,String)->String:
|
||||
l1()
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLambdaTypesEllipsis() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval("""
|
||||
var l2: (Int,Object...,String)->Real
|
||||
var l4: (Int,String...,String)->Real
|
||||
var l3: (...)->Int
|
||||
""".trimIndent())
|
||||
assertFailsWith<ScriptError> {
|
||||
scope.eval("""
|
||||
fun testTooFew() {
|
||||
l2(1)
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
assertFailsWith<ScriptError> {
|
||||
scope.eval("""
|
||||
fun testWrongHead() {
|
||||
l2("x", "y")
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
assertFailsWith<ScriptError> {
|
||||
scope.eval("""
|
||||
fun testWrongEllipsis() {
|
||||
l4(1, 2, "x")
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
scope.eval("""
|
||||
fun testOk1() { l2(1, "x") }
|
||||
fun testOk2() { l2(1, 2, 3, "x") }
|
||||
fun testOk3() { l3() }
|
||||
fun testOk4() { l3(1, true, "x") }
|
||||
fun testOk5() { l4(1, "a", "b", "x") }
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetTyped() = runTest {
|
||||
eval("""
|
||||
var s = Set<String>()
|
||||
val typed: Set<String> = s
|
||||
assertEquals(Set(), typed)
|
||||
|
||||
s += "foo"
|
||||
assertEquals(Set("foo"), s)
|
||||
s -= "foo"
|
||||
assertEquals(Set(), s)
|
||||
s += ["foo", "bar"]
|
||||
assertEquals(Set("foo", "bar"), s)
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListTyped() = runTest {
|
||||
eval("""
|
||||
var l = List<String>()
|
||||
val typed: List<String> = l
|
||||
assertEquals(List(), typed)
|
||||
|
||||
l += "foo"
|
||||
assertEquals(List("foo"), l)
|
||||
l -= "foo"
|
||||
assertEquals(List(), l)
|
||||
|
||||
l += ["foo", "bar"]
|
||||
assertEquals(List("foo", "bar"), l)
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAliasesInGenerics1() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval("""
|
||||
type IntList<T: Int> = List<T>
|
||||
type IntMap<K,V> = Map<K,V>
|
||||
type IntSet<T: Int> = Set<T>
|
||||
type IntPair<T: Int> = Pair<T,T>
|
||||
type IntTriple<T: Int> = Triple<T,T,T>
|
||||
type IntQuad<T: Int> = Quad<T,T,T,T>
|
||||
|
||||
import lyng.buffer
|
||||
type Tag = String | Buffer
|
||||
|
||||
class X {
|
||||
var tags: Set<Tag> = Set()
|
||||
}
|
||||
val x = X()
|
||||
x.tags += "tag1"
|
||||
assertEquals(Set("tag1"), x.tags)
|
||||
x.tags += "tag2"
|
||||
assertEquals(Set("tag1", "tag2"), x.tags)
|
||||
x.tags += Buffer("tag3")
|
||||
assertEquals(Set("tag1", "tag2", Buffer("tag3")), x.tags)
|
||||
x.tags += Buffer("tag4")
|
||||
assertEquals(Set("tag1", "tag2", Buffer("tag3"), Buffer("tag4")), x.tags)
|
||||
""")
|
||||
scope.eval("""
|
||||
assert(x is X)
|
||||
x.tags += "42"
|
||||
assertEquals(Set("tag1", "tag2", Buffer("tag3"), Buffer("tag4"), "42"), x.tags)
|
||||
|
||||
""".trimIndent())
|
||||
// now this must fail becaise element type does not match the declared:
|
||||
assertFailsWith<ScriptError> {
|
||||
scope.eval(
|
||||
"""
|
||||
x.tags += 42
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAliasesInGenericsList1() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval("""
|
||||
import lyng.buffer
|
||||
type Tag = String | Buffer
|
||||
|
||||
class X {
|
||||
var tags: List<Tag> = List()
|
||||
}
|
||||
val x = X()
|
||||
x.tags += "tag1"
|
||||
assertEquals(List("tag1"), x.tags)
|
||||
x.tags += "tag2"
|
||||
assertEquals(List("tag1", "tag2"), x.tags)
|
||||
x.tags += Buffer("tag3")
|
||||
assertEquals(List("tag1", "tag2", Buffer("tag3")), x.tags)
|
||||
x.tags += ["tag4", Buffer("tag5")]
|
||||
assertEquals(List("tag1", "tag2", Buffer("tag3"), "tag4", Buffer("tag5")), x.tags)
|
||||
""")
|
||||
scope.eval("""
|
||||
assert(x is X)
|
||||
x.tags += "42"
|
||||
assertEquals(List("tag1", "tag2", Buffer("tag3"), "tag4", Buffer("tag5"), "42"), x.tags)
|
||||
""".trimIndent())
|
||||
assertFailsWith<ScriptError> {
|
||||
scope.eval(
|
||||
"""
|
||||
x.tags += 42
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClassName() = runTest {
|
||||
eval("""
|
||||
class X {
|
||||
var x = 1
|
||||
}
|
||||
assert( X::class is Class)
|
||||
assertEquals("Class", X::class.name)
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGenericTypes() = runTest {
|
||||
eval("""
|
||||
fun t<T>(): String =
|
||||
when(T) {
|
||||
null -> "%s is Null"(T::class.name)
|
||||
is Object -> "%s is Object"(T::class.name)
|
||||
else -> throw "It should not happen"
|
||||
}
|
||||
assert( Int is Object)
|
||||
assertEquals( t<Int>(), "Class is Object")
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGenericNullableTypePredicate() = runTest {
|
||||
eval("""
|
||||
fun isTypeNullable<T>(x: T): Bool = T is nullable
|
||||
type MaybeInt = Int?
|
||||
assert(isTypeNullable<Int?>(null))
|
||||
assert(!isTypeNullable<Int>(1))
|
||||
assert(MaybeInt is nullable)
|
||||
assert(!(Int is nullable))
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWhenNullableTypeCase() = runTest {
|
||||
eval("""
|
||||
fun describe<T>(x: T): String = when(T) {
|
||||
nullable -> "nullable"
|
||||
else -> "non-null"
|
||||
}
|
||||
fun describeIs<T>(x: T): String = when(T) {
|
||||
is nullable -> "nullable"
|
||||
else -> "non-null"
|
||||
}
|
||||
assertEquals("nullable", describe<Int?>(null))
|
||||
assertEquals("non-null", describe<Int>(1))
|
||||
assertEquals("nullable", describeIs<Int?>(null))
|
||||
assertEquals("non-null", describeIs<Int>(1))
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testNullableGenericTypes() = runTest {
|
||||
// eval("""
|
||||
// fun t<T>(): String =
|
||||
// when(T) {
|
||||
// is Object -> "%s is Object"(T::class.name)
|
||||
// else -> throw "It should not happen"
|
||||
// }
|
||||
// assert( Int is Object)
|
||||
// assertEquals( t<Int>(), "Class is Object")
|
||||
// """.trimIndent())
|
||||
// }
|
||||
|
||||
@Test fun testIndexer() = runTest {
|
||||
eval("""
|
||||
class Greeter {
|
||||
override fun getAt(name) = "Hello, %s!"(name)
|
||||
}
|
||||
assertEquals("Hello, Bob!",Greeter()["Bob"])
|
||||
val g = Greeter()
|
||||
assertEquals("Hello, Bob!",g["Bob"])
|
||||
|
||||
// it should work with objects too:
|
||||
object Polite {
|
||||
override fun getAt(name) = "How do you do, %s?"(name)
|
||||
}
|
||||
|
||||
assertEquals("How do you do, Bob?",Polite["Bob"])
|
||||
|
||||
""".trimIndent())
|
||||
}
|
||||
// @Test fun nonTrivialOperatorsTest() = runTest {
|
||||
// val s = Script.newScope()
|
||||
// s.eval("""
|
||||
// class Matrix<T>(val rows: Int, val cols: Int,initialValue:T?) {
|
||||
// val data
|
||||
// init {
|
||||
// val v = initalValue?
|
||||
// }
|
||||
// data = List(rows*cols) { initialValue } }
|
||||
// fun getAt(row: Int, col: Int) = data[row*cols+col]
|
||||
// fun setAt(row: Int, col: Int, value: T) { data[row*cols+col] = value }
|
||||
// }
|
||||
// val m = Matrix(1,1)
|
||||
//
|
||||
// """.trimIndent())
|
||||
// }
|
||||
}
|
||||
Loaded 100 of 145 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user