Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
555c9b94de | ||
|
|
8cd980514b | ||
|
|
fa4dc45f15 | ||
|
|
ae68ecd066 | ||
|
|
ddfdc18ba3 | ||
|
|
1afc6d41bc | ||
|
|
83ab8cdc01 | ||
|
|
26b8370b01 | ||
|
|
660a80a26b | ||
|
|
d91acd593a | ||
|
|
5fc0969491 | ||
|
|
eec732d11a | ||
|
|
8611543623 | ||
|
|
73854f21f3 | ||
|
|
f66e61c185 | ||
|
|
41a3617850 | ||
|
|
f792c73b8f | ||
|
|
20f777f9f6 |
No files matched your search
@@ -21,3 +21,8 @@ xcuserdata
|
|||||||
debug.log
|
debug.log
|
||||||
/build.log
|
/build.log
|
||||||
/test.md
|
/test.md
|
||||||
|
/build_output.txt
|
||||||
|
/build_output_full.txt
|
||||||
|
/check_output.txt
|
||||||
|
/compile_jvm_output.txt
|
||||||
|
/compile_metadata_output.txt
|
||||||
@@ -2,6 +2,17 @@
|
|||||||
|
|
||||||
### Unreleased
|
### Unreleased
|
||||||
|
|
||||||
|
- Language: Added `return` statement
|
||||||
|
- `return [expression]` exits the innermost enclosing callable (function or lambda).
|
||||||
|
- Supports non-local returns using `@label` syntax (e.g., `return@outer 42`).
|
||||||
|
- Named functions automatically provide their name as a label for non-local returns.
|
||||||
|
- Labeled lambdas: lambdas can be explicitly labeled using `@label { ... }`.
|
||||||
|
- Restriction: `return` is forbidden in shorthand function definitions (e.g., `fun f(x) = return x` is a syntax error).
|
||||||
|
- Control Flow: `return` and `break` are now protected from being caught by user-defined `try-catch` blocks in Lyng.
|
||||||
|
- Documentation: New `docs/return_statement.md` and updated `tutorial.md`.
|
||||||
|
|
||||||
|
- Language: stdlib improvements
|
||||||
|
- Added `with(self, block)` function to `root.lyng` which executes a block with `this` set to the provided object.
|
||||||
- Language: Abstract Classes and Interfaces
|
- Language: Abstract Classes and Interfaces
|
||||||
- Support for `abstract` modifier on classes, methods, and variables.
|
- Support for `abstract` modifier on classes, methods, and variables.
|
||||||
- Introduced `interface` as a synonym for `abstract class`, supporting full state (constructors, fields, `init` blocks) and implementation by parts via MI.
|
- Introduced `interface` as a synonym for `abstract class`, supporting full state (constructors, fields, `init` blocks) and implementation by parts via MI.
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
# Lyng: modern scripting for kotlin multiplatform
|
# Lyng: ideal scripting for kotlin multiplatform
|
||||||
|
|
||||||
|
__Please visit the project homepage: [https://lynglang.com](https://lynglang.com) and a [telegram channel](https://t.me/lynglang).__
|
||||||
|
|
||||||
|
__Main development site:__ [https://gitea.sergeych.net/SergeychWorks/lyng](https://gitea.sergeych.net/SergeychWorks/lyng)
|
||||||
|
__github mirror__: [https://github.com/sergeych/lyng](https://github.com/sergeych/lyng)
|
||||||
|
|
||||||
|
We keep github as a mirror and backup for the project, while the main development site is hosted on gitea.sergeych.net. We use gitea for issues and pull requests, and as a main point of trust, as github access now is a thing that can momentarily be revoked for no apparent reason.
|
||||||
|
|
||||||
|
We encourage using the github if the main site is not accessible from your country and vice versa. We recommend to `publishToMavenLocal` and not depend on politics.
|
||||||
|
|
||||||
Please visit the project homepage: [https://lynglang.com](https://lynglang.com) and a [telegram channel](https://t.me/lynglang) for updates.
|
|
||||||
|
|
||||||
- simple, compact, intuitive and elegant modern code:
|
- simple, compact, intuitive and elegant modern code:
|
||||||
|
|
||||||
@@ -21,22 +29,9 @@ fun swapEnds(first, args..., last, f) {
|
|||||||
|
|
||||||
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
|
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
|
||||||
- 100% secure: no access to any API you didn't explicitly provide
|
- 100% secure: no access to any API you didn't explicitly provide
|
||||||
- 100% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]
|
- 100% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]. it is multithreaded on platforms supporting it (automatically, no code changes required, just `launch` more coroutines and they will be executed concurrently if possible). See [parallelism]
|
||||||
|
- functional style and OOP together: multiple inheritance (so you got it all - mixins, interfaces, etc.), delegation, sigletons, anonymous classes,extensions.
|
||||||
```
|
- nice literals for maps and arrays, destructuring assignment, ranges.
|
||||||
val deferred = launch {
|
|
||||||
delay(1.5) // coroutine is delayed for 1.5s, thread is not blocked!
|
|
||||||
"done"
|
|
||||||
}
|
|
||||||
// ...
|
|
||||||
// suspend current coroutine, no thread is blocked again,
|
|
||||||
// and wait for deferred to return something:
|
|
||||||
assertEquals("donw", deferred.await())
|
|
||||||
```
|
|
||||||
and it is multithreaded on platforms supporting it (automatically, no code changes required, just
|
|
||||||
`launch` more coroutines and they will be executed concurrently if possible). See [parallelism]
|
|
||||||
|
|
||||||
- functional style and OOP together, multiple inheritance, implementing interfaces for existing classes, writing extensions.
|
|
||||||
- Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`.
|
- Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`.
|
||||||
|
|
||||||
## Resources:
|
## Resources:
|
||||||
@@ -44,6 +39,8 @@ and it is multithreaded on platforms supporting it (automatically, no code chang
|
|||||||
- [Language home](https://lynglang.com)
|
- [Language home](https://lynglang.com)
|
||||||
- [introduction and tutorial](docs/tutorial.md) - start here please
|
- [introduction and tutorial](docs/tutorial.md) - start here please
|
||||||
- [Testing and Assertions](docs/Testing.md)
|
- [Testing and Assertions](docs/Testing.md)
|
||||||
|
- [Filesystem and Processes (lyngio)](docs/lyngio.md)
|
||||||
|
- [Return Statement](docs/return_statement.md)
|
||||||
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
|
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
|
||||||
- [Samples directory](docs/samples)
|
- [Samples directory](docs/samples)
|
||||||
- [Formatter (core + CLI + IDE)](docs/formatter.md)
|
- [Formatter (core + CLI + IDE)](docs/formatter.md)
|
||||||
@@ -191,6 +188,16 @@ Ready features:
|
|||||||
- [x] regular exceptions + extended `when`
|
- [x] regular exceptions + extended `when`
|
||||||
- [x] multiple inheritance for user classes
|
- [x] multiple inheritance for user classes
|
||||||
- [x] class properties (accessors)
|
- [x] class properties (accessors)
|
||||||
|
- [x] `return` statement for local and non-local exit
|
||||||
|
- [x] Unified Delegation model: val, var and fun
|
||||||
|
- [x] `lazy val` using delegation
|
||||||
|
- [x] singletons `object TheOnly { ... }`
|
||||||
|
- [x] object expressions `object: List { ... }`
|
||||||
|
- [x] late-init vals in classes
|
||||||
|
- [x] properties with getters and setters
|
||||||
|
- [x] assign-if-null operator `?=`
|
||||||
|
|
||||||
|
All of this is documented in the [language site](https://lynglang.com) and locally [docs/language.md](docs/tutorial.md). the current nightly builds published on the site and in the private maven repository.
|
||||||
|
|
||||||
## plan: towards v1.5 Enhancing
|
## plan: towards v1.5 Enhancing
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# Obsolete files
|
||||||
|
|
||||||
|
|
||||||
|
__Do not rely on contents of the files in this directory. They are kept for historical reference only and may not be up-to-date or relevant.__
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
This is a tech proposal under construction, please do not use it yet
|
||||||
|
for any purpose
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Abstract delegate can be used to proxy read/wrtie field access
|
||||||
|
or method call. Default implementation reports error.
|
||||||
|
*/
|
||||||
|
interface Delegate {
|
||||||
|
fun getValue() = Unset
|
||||||
|
fun setValue(newValue) { throw NotImplementedException("delegate setter is not implemented") }
|
||||||
|
fun invoke(args...) { throw NotImplementedException("delegate setter is not implemented") }
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Delegate cam be used to implement a val, var or fun, so there are
|
||||||
|
access type enum to distinguish:
|
||||||
|
*/
|
||||||
|
enum DelegateAccess {
|
||||||
|
Val,
|
||||||
|
Var,
|
||||||
|
Callable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delegate can be associated by a val/var/fun in a declaraion site using `by` keyword
|
||||||
|
|
||||||
|
val proxiedVal by proxy(1)
|
||||||
|
var proxiedVar by proxy(2, 3)
|
||||||
|
fun proxiedFun by proxy()
|
||||||
|
|
||||||
|
// each proxy is a Lyng expression returning instance of the Proxy interface:
|
||||||
|
|
||||||
|
/*
|
||||||
|
Proxy interface is connecting some named property of a given kind with the `Delegate`.
|
||||||
|
It removes the burden of dealing with property name and this ref on each get/set value
|
||||||
|
or invoke allowing having one delegate per instance, execution buff.
|
||||||
|
*/
|
||||||
|
interface Proxy {
|
||||||
|
fun getDelegate(propertyName: String,access: DelegateAccess,thisRef: Obj?): Delegate
|
||||||
|
}
|
||||||
|
|
||||||
|
// val, var and fun can be delegated, local or class instance:
|
||||||
|
class TestProxy: Proxy {
|
||||||
|
override getDelegate(name,access,thisRef) {
|
||||||
|
Delegate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val proxy = TestProxy()
|
||||||
|
|
||||||
|
class Allowed {
|
||||||
|
val v1 by proxy
|
||||||
|
var v2 by proxy
|
||||||
|
fun f1 by proxy
|
||||||
|
}
|
||||||
|
val v3 by proxy
|
||||||
|
var v4 by proxy
|
||||||
|
fun f2 by proxy
|
||||||
|
|
||||||
|
/*
|
||||||
|
It means that for example
|
||||||
|
Allowed().f1("foo")
|
||||||
|
would call a delegate.invoke("foo") on the `Delegate` instance supplied by `proxy`, etc.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// The practic sample: lazy value
|
||||||
|
|
||||||
|
/*
|
||||||
|
The delegate that caches single time evaluated value
|
||||||
|
*/
|
||||||
|
class LazyDelegate(creator): Delegate {
|
||||||
|
private var currentValue=Unset
|
||||||
|
|
||||||
|
override fun getValue() {
|
||||||
|
if( currentValue == Unset )
|
||||||
|
currentValue = creator()
|
||||||
|
currentValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
The proxy to assign it
|
||||||
|
*/
|
||||||
|
class LazyProxy(creator) {
|
||||||
|
fun getDelegate(name,access,thisRef) {
|
||||||
|
if( access != DelegateAccess.Val )
|
||||||
|
throw IllegalArgumentException("only lazy val are allowed")
|
||||||
|
LazyDelegate(creator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
A helper function to simplify creation:
|
||||||
|
*/
|
||||||
|
fun lazy(creator) {
|
||||||
|
LazyProxy(creator)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usage sample and the test:
|
||||||
|
var callCounter = 0
|
||||||
|
assertEquals(0, clallCounter)
|
||||||
|
|
||||||
|
val lazyText by lazy { "evaluated text" }
|
||||||
|
|
||||||
|
// the lazy property is not yet evaluated:
|
||||||
|
assertEquals(0, clallCounter)
|
||||||
|
// now evaluate it by using it:
|
||||||
|
assertEquals("evaluated text", lazyText)
|
||||||
|
assertEquals(1, callCounter)
|
||||||
|
|
||||||
|
// lazy delegate should fail on vars or funs:
|
||||||
|
assertThrows { var bad by lazy { "should not happen" } }
|
||||||
|
assertThrows { fun bad by lazy { 42 } }
|
||||||
|
|
||||||
|
|
||||||
File renamed without changes.
File renamed without changes.
+42
@@ -71,6 +71,48 @@ object DefaultLogger : Logger("Default") {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Object Expressions
|
||||||
|
|
||||||
|
Object expressions allow you to create an instance of an anonymous class. This is useful when you need to provide a one-off implementation of an interface or inherit from a class without declaring a new named subclass.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
val worker = object : Runnable {
|
||||||
|
override fun run() {
|
||||||
|
println("Working...")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Object expressions can implement multiple interfaces and inherit from one class:
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
val x = object : Base(arg1), Interface1, Interface2 {
|
||||||
|
val property = 42
|
||||||
|
override fun method() = property * 2
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Scoping and `this@object`
|
||||||
|
|
||||||
|
Object expressions capture their lexical scope, meaning they can access local variables and members of the outer class. When `this` is rebound (for example, inside an `apply` block), you can use the reserved alias `this@object` to refer to the innermost anonymous object instance.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
val handler = object {
|
||||||
|
fun process() {
|
||||||
|
this@object.apply {
|
||||||
|
// here 'this' is rebound to the map/context
|
||||||
|
// but we can still access the anonymous object via this@object
|
||||||
|
println("Processing in " + this@object)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Serialization and Identity
|
||||||
|
|
||||||
|
- **Serialization**: Anonymous objects are **not serializable**. Attempting to encode an anonymous object via `Lynon` will throw a `SerializationException`. This is because their class definition is transient and cannot be safely restored in a different session or process.
|
||||||
|
- **Type Identity**: Every object expression creates a unique anonymous class. Two identical object expressions will result in two different classes with distinct type identities.
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors.
|
Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors.
|
||||||
|
|||||||
+2
-1
@@ -57,7 +57,8 @@ class lazy(val creator) : Delegate {
|
|||||||
|
|
||||||
override fun getValue(thisRef, name) {
|
override fun getValue(thisRef, name) {
|
||||||
if (value == Unset) {
|
if (value == Unset) {
|
||||||
value = creator()
|
// calculate value using thisRef as this:
|
||||||
|
value = with(thisRef) creator()
|
||||||
}
|
}
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-3
@@ -8,7 +8,7 @@ This module provides a uniform, suspend-first filesystem API to Lyng scripts, ba
|
|||||||
|
|
||||||
It exposes a Lyng class `Path` with methods for file and directory operations, including streaming readers for large files.
|
It exposes a Lyng class `Path` with methods for file and directory operations, including streaming readers for large files.
|
||||||
|
|
||||||
It is a separate library because access to teh filesystem is a security risk we compensate with a separate API that user must explicitly include to the dependency and allow. Together with `FsAceessPolicy` that is required to `createFs()` which actually adds the filesystem to the scope, the security risk is isolated.
|
It is a separate library because access to the filesystem is a security risk we compensate with a separate API that user must explicitly include to the dependency and allow. Together with `FsAccessPolicy` that is required to `createFs()` which actually adds the filesystem to the scope, the security risk is isolated.
|
||||||
|
|
||||||
Also, it helps keep Lyng core small and focused.
|
Also, it helps keep Lyng core small and focused.
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ dependencies {
|
|||||||
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
|
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Note on maven repository. Lyngio uses ths same maven as Lyng code (`lynglib`) so it is most likely already in your project. If ont, add it to the proper section of your `build.gradle.kts` or settings.gradle.kts:
|
Note on maven repository. Lyngio uses the same maven as Lyng code (`lynglib`) so it is most likely already in your project. If not, add it to the proper section of your `build.gradle.kts` or settings.gradle.kts:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
repositories {
|
repositories {
|
||||||
@@ -43,9 +43,13 @@ This brings in:
|
|||||||
|
|
||||||
The filesystem module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using the installer. You can customize access control via `FsAccessPolicy`.
|
The filesystem module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using the installer. You can customize access control via `FsAccessPolicy`.
|
||||||
|
|
||||||
Kotlin (host) bootstrap example (imports omitted for brevity):
|
Kotlin (host) bootstrap example:
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.io.fs.createFs
|
||||||
|
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
|
||||||
|
|
||||||
val scope: Scope = Scope.new()
|
val scope: Scope = Scope.new()
|
||||||
val installed: Boolean = createFs(PermitAllAccessPolicy, scope)
|
val installed: Boolean = createFs(PermitAllAccessPolicy, scope)
|
||||||
// installed == true on first registration in this ImportManager, false on repeats
|
// installed == true on first registration in this ImportManager, false on repeats
|
||||||
|
|||||||
@@ -0,0 +1,136 @@
|
|||||||
|
### lyng.io.process — Process execution and control for Lyng scripts
|
||||||
|
|
||||||
|
This module provides a way to run external processes and shell commands from Lyng scripts. It is designed to be multiplatform and uses coroutines for non-blocking execution.
|
||||||
|
|
||||||
|
> **Note:** `lyngio` is a separate library module. It must be explicitly added as a dependency to your host application and initialized in your Lyng scopes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Add the library to your project (Gradle)
|
||||||
|
|
||||||
|
If you use this repository as a multi-module project, add a dependency on `:lyngio`:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
dependencies {
|
||||||
|
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For external projects, ensure you have the appropriate Maven repository configured (see `lyng.io.fs` documentation).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Install the module into a Lyng Scope
|
||||||
|
|
||||||
|
The process module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using `createProcessModule`. You can customize access control via `ProcessAccessPolicy`.
|
||||||
|
|
||||||
|
Kotlin (host) bootstrap example:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.Script
|
||||||
|
import net.sergeych.lyng.io.process.createProcessModule
|
||||||
|
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
|
||||||
|
|
||||||
|
// ... inside a suspend function or runBlocking
|
||||||
|
val scope: Scope = Script.newScope()
|
||||||
|
createProcessModule(PermitAllProcessAccessPolicy, scope)
|
||||||
|
|
||||||
|
// In scripts (or via scope.eval), import the module:
|
||||||
|
scope.eval("import lyng.io.process")
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Using from Lyng scripts
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
import lyng.io.process
|
||||||
|
|
||||||
|
// Execute a process with arguments
|
||||||
|
val p = Process.execute("ls", ["-l", "/tmp"])
|
||||||
|
for (line in p.stdout) {
|
||||||
|
println("OUT: " + line)
|
||||||
|
}
|
||||||
|
val exitCode = p.waitFor()
|
||||||
|
println("Process exited with: " + exitCode)
|
||||||
|
|
||||||
|
// Run a shell command
|
||||||
|
val sh = Process.shell("echo 'Hello from shell' | wc -w")
|
||||||
|
for (line in sh.stdout) {
|
||||||
|
println("Word count: " + line.trim())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Platform information
|
||||||
|
val details = Platform.details()
|
||||||
|
println("OS: " + details.name + " " + details.version + " (" + details.arch + ")")
|
||||||
|
if (details.kernelVersion != null) {
|
||||||
|
println("Kernel: " + details.kernelVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Platform.isSupported()) {
|
||||||
|
println("Processes are supported!")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### API Reference
|
||||||
|
|
||||||
|
##### `Process` (static methods)
|
||||||
|
- `execute(executable: String, args: List<String>): RunningProcess` — Start an external process.
|
||||||
|
- `shell(command: String): RunningProcess` — Run a command through the system shell (e.g., `/bin/sh` or `cmd.exe`).
|
||||||
|
|
||||||
|
##### `RunningProcess` (instance methods)
|
||||||
|
- `stdout: Flow` — Standard output stream as a Lyng Flow of lines.
|
||||||
|
- `stderr: Flow` — Standard error stream as a Lyng Flow of lines.
|
||||||
|
- `waitFor(): Int` — Wait for the process to exit and return the exit code.
|
||||||
|
- `signal(name: String)` — Send a signal to the process (e.g., `"SIGINT"`, `"SIGTERM"`, `"SIGKILL"`).
|
||||||
|
- `destroy()` — Forcefully terminate the process.
|
||||||
|
|
||||||
|
##### `Platform` (static methods)
|
||||||
|
- `details(): Map` — Get platform details. Returned map keys: `name`, `version`, `arch`, `kernelVersion`.
|
||||||
|
- `isSupported(): Bool` — True if process execution is supported on the current platform.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Security Policy
|
||||||
|
|
||||||
|
Process execution is a sensitive operation. `lyngio` uses `ProcessAccessPolicy` to control access to `execute` and `shell` operations.
|
||||||
|
|
||||||
|
- `ProcessAccessPolicy` — Interface for custom policies.
|
||||||
|
- `PermitAllProcessAccessPolicy` — Allows all operations.
|
||||||
|
- `ProcessAccessOp` (sealed) — Operations to check:
|
||||||
|
- `Execute(executable, args)`
|
||||||
|
- `Shell(command)`
|
||||||
|
|
||||||
|
Example of a restricted policy in Kotlin:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
import net.sergeych.lyngio.fs.security.AccessDecision
|
||||||
|
import net.sergeych.lyngio.fs.security.Decision
|
||||||
|
import net.sergeych.lyngio.process.security.ProcessAccessOp
|
||||||
|
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
|
||||||
|
|
||||||
|
val restrictedPolicy = object : ProcessAccessPolicy {
|
||||||
|
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision {
|
||||||
|
return when (op) {
|
||||||
|
is ProcessAccessOp.Execute -> {
|
||||||
|
if (op.executable == "ls") AccessDecision(Decision.Allow)
|
||||||
|
else AccessDecision(Decision.Deny, "Only 'ls' is allowed")
|
||||||
|
}
|
||||||
|
is ProcessAccessOp.Shell -> AccessDecision(Decision.Deny, "Shell is forbidden")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createProcessModule(restrictedPolicy, scope)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Platform Support
|
||||||
|
|
||||||
|
- **JVM:** Full support using `ProcessBuilder`.
|
||||||
|
- **Native (Linux/macOS):** Support via POSIX.
|
||||||
|
- **Windows:** Support planned.
|
||||||
|
- **Android/JS/iOS/Wasm:** Currently not supported; `isSupported()` returns `false` and attempts to run processes will throw `UnsupportedOperationException`.
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
### lyngio — Extended I/O and System Library for Lyng
|
||||||
|
|
||||||
|
`lyngio` is a separate library that extends the Lyng core (`lynglib`) with powerful, multiplatform, and secure I/O capabilities.
|
||||||
|
|
||||||
|
#### Why a separate module?
|
||||||
|
|
||||||
|
1. **Security:** I/O and process execution are sensitive operations. By keeping them in a separate module, we ensure that the Lyng core remains 100% safe by default. You only enable what you explicitly need.
|
||||||
|
2. **Footprint:** Not every script needs filesystem or process access. Keeping these as a separate module helps minimize the dependency footprint for small embedded projects.
|
||||||
|
3. **Control:** `lyngio` provides fine-grained security policies (`FsAccessPolicy`, `ProcessAccessPolicy`) that allow you to control exactly what a script can do.
|
||||||
|
|
||||||
|
#### Included Modules
|
||||||
|
|
||||||
|
- **[lyng.io.fs](lyng.io.fs.md):** Async filesystem access. Provides the `Path` class for file/directory operations, streaming, and globbing.
|
||||||
|
- **[lyng.io.process](lyng.io.process.md):** External process execution and shell commands. Provides `Process`, `RunningProcess`, and `Platform` information.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Quick Start: Embedding lyngio
|
||||||
|
|
||||||
|
##### 1. Add Dependencies (Gradle)
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
repositories {
|
||||||
|
maven("https://gitea.sergeych.net/api/packages/SergeychWorks/maven")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// Both are required for full I/O support
|
||||||
|
implementation("net.sergeych:lynglib:0.0.1-SNAPSHOT")
|
||||||
|
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
##### 2. Initialize in Kotlin (JVM or Native)
|
||||||
|
|
||||||
|
To use `lyngio` modules in your scripts, you must install them into your Lyng scope and provide a security policy.
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
import net.sergeych.lyng.Script
|
||||||
|
import net.sergeych.lyng.io.fs.createFs
|
||||||
|
import net.sergeych.lyng.io.process.createProcessModule
|
||||||
|
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
|
||||||
|
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
|
||||||
|
|
||||||
|
suspend fun runMyScript() {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
|
||||||
|
// Install modules with policies
|
||||||
|
createFs(PermitAllAccessPolicy, scope)
|
||||||
|
createProcessModule(PermitAllProcessAccessPolicy, scope)
|
||||||
|
|
||||||
|
// Now scripts can import them
|
||||||
|
scope.eval("""
|
||||||
|
import lyng.io.fs
|
||||||
|
import lyng.io.process
|
||||||
|
|
||||||
|
println("Working dir: " + Path(".").readUtf8())
|
||||||
|
println("OS: " + Platform.details().name)
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Security Tools
|
||||||
|
|
||||||
|
`lyngio` is built with a "Secure by Default" philosophy. Every I/O or process operation is checked against a policy.
|
||||||
|
|
||||||
|
- **Filesystem Security:** Implement `FsAccessPolicy` to restrict access to specific paths or operations (e.g., read-only access to a sandbox directory).
|
||||||
|
- **Process Security:** Implement `ProcessAccessPolicy` to restrict which executables can be run or to disable shell execution entirely.
|
||||||
|
|
||||||
|
For more details, see the specific module documentation:
|
||||||
|
- [Filesystem Security Details](lyng.io.fs.md#access-policy-security)
|
||||||
|
- [Process Security Details](lyng.io.process.md#security-policy)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Platform Support Overview
|
||||||
|
|
||||||
|
| Platform | lyng.io.fs | lyng.io.process |
|
||||||
|
| :--- | :---: | :---: |
|
||||||
|
| **JVM** | ✅ | ✅ |
|
||||||
|
| **Native (Linux/macOS)** | ✅ | ✅ |
|
||||||
|
| **Native (Windows)** | ✅ | 🚧 (Planned) |
|
||||||
|
| **Android** | ✅ | ❌ |
|
||||||
|
| **NodeJS** | ✅ | ❌ |
|
||||||
|
| **Browser / Wasm** | ✅ (In-memory) | ❌ |
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
# The `return` statement
|
||||||
|
|
||||||
|
The `return` statement is used to terminate the execution of the innermost enclosing callable (a function or a lambda) and optionally return a value to the caller.
|
||||||
|
|
||||||
|
## Basic Usage
|
||||||
|
|
||||||
|
By default, Lyng functions and blocks return the value of their last expression. However, `return` allows you to exit early, which is particularly useful for guard clauses.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun divide(a, b) {
|
||||||
|
if (b == 0) return null // Guard clause: early exit
|
||||||
|
a / b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If no expression is provided, `return` returns `void`:
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun logIfDebug(msg) {
|
||||||
|
if (!DEBUG) return
|
||||||
|
println("[DEBUG] " + msg)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Scoping Rules
|
||||||
|
|
||||||
|
In Lyng, `return` always exits the **innermost enclosing callable**. Callables include:
|
||||||
|
* Named functions (`fun` or `fn`)
|
||||||
|
* Anonymous functions/lambdas (`{ ... }`)
|
||||||
|
|
||||||
|
Standard control flow blocks like `if`, `while`, `do`, and `for` are **not** callables; `return` inside these blocks will return from the function or lambda that contains them.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun findFirstPositive(list) {
|
||||||
|
list.forEach {
|
||||||
|
if (it > 0) return it // ERROR: This returns from the lambda, not findFirstPositive!
|
||||||
|
}
|
||||||
|
null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
*Note: To return from an outer scope, use [Non-local Returns](#non-local-returns).*
|
||||||
|
|
||||||
|
## Non-local Returns
|
||||||
|
|
||||||
|
Lyng supports returning from outer scopes using labels. This is a powerful feature for a closure-intensive language.
|
||||||
|
|
||||||
|
### Named Functions as Labels
|
||||||
|
Every named function automatically provides its name as a label.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun findFirstPositive(list) {
|
||||||
|
list.forEach {
|
||||||
|
if (it > 0) return@findFirstPositive it // Returns from findFirstPositive
|
||||||
|
}
|
||||||
|
null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Labeled Lambdas
|
||||||
|
You can explicitly label a lambda using the `@label` syntax to return from it specifically when nested.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
val process = @outer { x ->
|
||||||
|
val result = {
|
||||||
|
if (x < 0) return@outer "negative" // Returns from the outer lambda
|
||||||
|
x * 2
|
||||||
|
}()
|
||||||
|
"Result: " + result
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Restriction on Shorthand Functions
|
||||||
|
|
||||||
|
To maintain Lyng's clean, expression-oriented style, the `return` keyword is **forbidden** in shorthand function definitions (those using `=`).
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun square(x) = x * x // Correct
|
||||||
|
fun square(x) = return x * x // Syntax Error: 'return' not allowed here
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
* `return [expression]` exits the innermost `fun` or `{}`.
|
||||||
|
* Use `return@label` for non-local returns.
|
||||||
|
* Named functions provide automatic labels.
|
||||||
|
* Cannot be used in `=` shorthand functions.
|
||||||
|
* Consistency: Mirrors the syntax and behavior of `break@label expression`.
|
||||||
+35
-10
@@ -8,7 +8,7 @@ __Other documents to read__ maybe after this one:
|
|||||||
|
|
||||||
- [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.md), [Scopes and Closures](scopes_and_closures.md)
|
- [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.md), [Scopes and Closures](scopes_and_closures.md)
|
||||||
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
|
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
|
||||||
- [math in Lyng](math.md), [the `when` statement](when.md)
|
- [math in Lyng](math.md), [the `when` statement](when.md), [return statement](return_statement.md)
|
||||||
- [Testing and Assertions](Testing.md)
|
- [Testing and Assertions](Testing.md)
|
||||||
- [time](time.md) and [parallelism](parallelism.md)
|
- [time](time.md) and [parallelism](parallelism.md)
|
||||||
- [parallelism] - multithreaded code, coroutines, etc.
|
- [parallelism] - multithreaded code, coroutines, etc.
|
||||||
@@ -32,6 +32,15 @@ any block also returns it's last expression:
|
|||||||
}
|
}
|
||||||
>>> 6
|
>>> 6
|
||||||
|
|
||||||
|
If you want to exit a function or lambda earlier, use the `return` statement:
|
||||||
|
|
||||||
|
fn divide(a, b) {
|
||||||
|
if( b == 0 ) return null
|
||||||
|
a / b
|
||||||
|
}
|
||||||
|
|
||||||
|
See [return statement](return_statement.md) for more details on scoping and non-local returns.
|
||||||
|
|
||||||
If you don't want block to return anything, use `void`:
|
If you don't want block to return anything, use `void`:
|
||||||
|
|
||||||
fn voidFunction() {
|
fn voidFunction() {
|
||||||
@@ -179,14 +188,13 @@ Note that assignment operator returns rvalue, it can't be assigned.
|
|||||||
## Modifying arithmetics
|
## Modifying arithmetics
|
||||||
|
|
||||||
There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`.
|
There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`.
|
||||||
|
There is also a special null-aware assignment operator `?=`: it performs the assignment only if the lvalue is `null`.
|
||||||
|
|
||||||
var x = 5
|
var x = null
|
||||||
assert( 25 == (x*=5) )
|
x ?= 10
|
||||||
assert( 25 == x)
|
assertEquals(10, x)
|
||||||
assert( 24 == (x-=1) )
|
x ?= 20
|
||||||
assert( 12 == (x/=2) )
|
assertEquals(10, x)
|
||||||
x
|
|
||||||
>>> 12
|
|
||||||
|
|
||||||
Notice the parentheses here: the assignment has low priority!
|
Notice the parentheses here: the assignment has low priority!
|
||||||
|
|
||||||
@@ -239,6 +247,13 @@ There is also "elvis operator", null-coalesce infix operator '?:' that returns r
|
|||||||
null ?: "nothing"
|
null ?: "nothing"
|
||||||
>>> "nothing"
|
>>> "nothing"
|
||||||
|
|
||||||
|
There is also a null-aware assignment operator `?=`, which assigns a value only if the target is `null`:
|
||||||
|
|
||||||
|
var config = null
|
||||||
|
config ?= { port: 8080 }
|
||||||
|
config ?= { port: 9000 } // no-op, config is already not null
|
||||||
|
assertEquals(8080, config.port)
|
||||||
|
|
||||||
## Utility functions
|
## Utility functions
|
||||||
|
|
||||||
The following functions simplify nullable values processing and
|
The following functions simplify nullable values processing and
|
||||||
@@ -298,6 +313,16 @@ It works much like `also`, but is executed in the context of the source object:
|
|||||||
assertEquals(p, Point(2,3))
|
assertEquals(p, Point(2,3))
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
|
## with
|
||||||
|
|
||||||
|
Sets `this` to the first argument and executes the block. Returns the value returned by the block:
|
||||||
|
|
||||||
|
class Point(x,y)
|
||||||
|
val p = Point(1,2)
|
||||||
|
val sum = with(p) { x + y }
|
||||||
|
assertEquals(3, sum)
|
||||||
|
>>> void
|
||||||
|
|
||||||
## run
|
## run
|
||||||
|
|
||||||
Executes a block after it returning the value passed by the block. for example, can be used with elvis operator:
|
Executes a block after it returning the value passed by the block. for example, can be used with elvis operator:
|
||||||
@@ -1519,7 +1544,7 @@ See [math functions](math.md). Other general purpose functions are:
|
|||||||
| flow {} | create flow sequence, see [parallelism] |
|
| flow {} | create flow sequence, see [parallelism] |
|
||||||
| delay, launch, yield | see [parallelism] |
|
| delay, launch, yield | see [parallelism] |
|
||||||
| cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) |
|
| cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) |
|
||||||
| let, also, apply, run | see above, flow controls |
|
| let, also, apply, run, with | see above, flow controls |
|
||||||
|
|
||||||
(1)
|
(1)
|
||||||
: `fn` is optional lambda returning string message to add to exception string.
|
: `fn` is optional lambda returning string message to add to exception string.
|
||||||
@@ -1546,7 +1571,7 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
|
|||||||
|
|
||||||
[Range]: Range.md
|
[Range]: Range.md
|
||||||
|
|
||||||
[String]: development/String.md
|
[String]: ../archived/development/String.md
|
||||||
|
|
||||||
[string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
|
[string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
|
||||||
|
|
||||||
|
|||||||
@@ -105,6 +105,22 @@ object Config {
|
|||||||
Config.show()
|
Config.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Object Expressions
|
||||||
|
You can now create anonymous objects that inherit from classes or interfaces using the `object : Base { ... }` syntax. These expressions capture their lexical scope and support multiple inheritance.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
val worker = object : Runnable {
|
||||||
|
override fun run() = println("Working...")
|
||||||
|
}
|
||||||
|
|
||||||
|
val x = object : Base(arg1), Interface1 {
|
||||||
|
val property = 42
|
||||||
|
override fun method() = this@object.property * 2
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `this@object` to refer to the innermost anonymous object instance when `this` is rebound.
|
||||||
|
|
||||||
### Unified Delegation Model
|
### Unified Delegation Model
|
||||||
A powerful new delegation system allows `val`, `var`, and `fun` members to delegate their logic to other objects using the `by` keyword.
|
A powerful new delegation system allows `val`, `var`, and `fun` members to delegate their logic to other objects using the `by` keyword.
|
||||||
|
|
||||||
@@ -123,6 +139,21 @@ var name by Observable("initial") { n, old, new ->
|
|||||||
|
|
||||||
The system features a unified interface (`getValue`, `setValue`, `invoke`) and a `bind` hook for initialization-time validation and configuration. See the [Delegation Guide](delegation.md) for more.
|
The system features a unified interface (`getValue`, `setValue`, `invoke`) and a `bind` hook for initialization-time validation and configuration. See the [Delegation Guide](delegation.md) for more.
|
||||||
|
|
||||||
|
### Assign-if-null Operator (`?=`)
|
||||||
|
The new `?=` operator provides a concise way to assign a value only if the target is `null`. It is especially useful for setting default values or lazy initialization.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
var x = null
|
||||||
|
x ?= 42 // x is now 42
|
||||||
|
x ?= 100 // x remains 42 (not null)
|
||||||
|
|
||||||
|
// Works with properties and index access
|
||||||
|
config.port ?= 8080
|
||||||
|
settings["theme"] ?= "dark"
|
||||||
|
```
|
||||||
|
|
||||||
|
The operator returns the final value of the receiver (the original value if it was not `null`, or the new value if the assignment occurred).
|
||||||
|
|
||||||
## Tooling and Infrastructure
|
## Tooling and Infrastructure
|
||||||
|
|
||||||
### CLI: Formatting Command
|
### CLI: Formatting Command
|
||||||
|
|||||||
@@ -42,7 +42,7 @@
|
|||||||
{ "name": "constant.numeric.decimal.lyng", "match": "(?<![A-Za-z_])(?:[0-9][0-9_]*)\\.(?:[0-9_]+)(?:[eE][+-]?[0-9_]+)?|(?<![A-Za-z_])(?:[0-9][0-9_]*)(?:[eE][+-]?[0-9_]+)?" }
|
{ "name": "constant.numeric.decimal.lyng", "match": "(?<![A-Za-z_])(?:[0-9][0-9_]*)\\.(?:[0-9_]+)(?:[eE][+-]?[0-9_]+)?|(?<![A-Za-z_])(?:[0-9][0-9_]*)(?:[eE][+-]?[0-9_]+)?" }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"annotations": { "patterns": [ { "name": "entity.name.label.at.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*:" }, { "name": "storage.modifier.annotation.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*" } ] },
|
"annotations": { "patterns": [ { "name": "entity.name.label.at.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*" } ] },
|
||||||
"mapLiterals": {
|
"mapLiterals": {
|
||||||
"patterns": [
|
"patterns": [
|
||||||
{
|
{
|
||||||
@@ -74,11 +74,11 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*:" } ] },
|
"labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*@" } ] },
|
||||||
"directives": { "patterns": [ { "name": "meta.directive.lyng", "match": "^\\s*#[_A-Za-z][_A-Za-z0-9]*" } ] },
|
"directives": { "patterns": [ { "name": "meta.directive.lyng", "match": "^\\s*#[_A-Za-z][_A-Za-z0-9]*" } ] },
|
||||||
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum|interface)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] },
|
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum|interface|object)(?:\\s+([\\p{L}_][\\p{L}\\p{N}_]*))?", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] },
|
||||||
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|interface|val|var|import|package|constructor|property|abstract|override|open|closed|extern|private|protected|static|get|set)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] },
|
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|interface|val|var|import|package|constructor|property|abstract|override|open|closed|extern|private|protected|static|get|set|object|init|by)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] },
|
||||||
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this)\\b|π)" } ] },
|
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this(?:@[\\p{L}_][\\p{L}\\p{N}_]*)?)\\b|π)" } ] },
|
||||||
"types": { "patterns": [ { "name": "storage.type.lyng", "match": "\\b(?:Int|Real|String|Bool|Char|Regex)\\b" }, { "name": "entity.name.type.lyng", "match": "\\b[A-Z][A-Za-z0-9_]*\\b(?!\\s*\\()" } ] },
|
"types": { "patterns": [ { "name": "storage.type.lyng", "match": "\\b(?:Int|Real|String|Bool|Char|Regex)\\b" }, { "name": "entity.name.type.lyng", "match": "\\b[A-Z][A-Za-z0-9_]*\\b(?!\\s*\\()" } ] },
|
||||||
"operators": { "patterns": [ { "name": "keyword.operator.comparison.lyng", "match": "===|!==|==|!=|<=|>=|<|>" }, { "name": "keyword.operator.shuttle.lyng", "match": "<=>" }, { "name": "keyword.operator.arrow.lyng", "match": "=>|->|::" }, { "name": "keyword.operator.range.lyng", "match": "\\.\\.\\.|\\.\\.<|\\.\\." }, { "name": "keyword.operator.nullsafe.lyng", "match": "\\?\\.|\\?\\[|\\?\\(|\\?\\{|\\?:|\\?\\?" }, { "name": "keyword.operator.assignment.lyng", "match": "(?:\\+=|-=|\\*=|/=|%=|=)" }, { "name": "keyword.operator.logical.lyng", "match": "&&|\\|\\|" }, { "name": "keyword.operator.bitwise.lyng", "match": "<<|>>|&|\\||\\^|~" }, { "name": "keyword.operator.match.lyng", "match": "=~|!~" }, { "name": "keyword.operator.arithmetic.lyng", "match": "\\+\\+|--|[+\\-*/%]" }, { "name": "keyword.operator.other.lyng", "match": "[!?]" } ] },
|
"operators": { "patterns": [ { "name": "keyword.operator.comparison.lyng", "match": "===|!==|==|!=|<=|>=|<|>" }, { "name": "keyword.operator.shuttle.lyng", "match": "<=>" }, { "name": "keyword.operator.arrow.lyng", "match": "=>|->|::" }, { "name": "keyword.operator.range.lyng", "match": "\\.\\.\\.|\\.\\.<|\\.\\." }, { "name": "keyword.operator.nullsafe.lyng", "match": "\\?\\.|\\?\\[|\\?\\(|\\?\\{|\\?:|\\?\\?" }, { "name": "keyword.operator.assignment.lyng", "match": "(?:\\+=|-=|\\*=|/=|%=|=)" }, { "name": "keyword.operator.logical.lyng", "match": "&&|\\|\\|" }, { "name": "keyword.operator.bitwise.lyng", "match": "<<|>>|&|\\||\\^|~" }, { "name": "keyword.operator.match.lyng", "match": "=~|!~" }, { "name": "keyword.operator.arithmetic.lyng", "match": "\\+\\+|--|[+\\-*/%]" }, { "name": "keyword.operator.other.lyng", "match": "[!?]" } ] },
|
||||||
"punctuation": { "patterns": [ { "name": "punctuation.separator.comma.lyng", "match": "," }, { "name": "punctuation.terminator.statement.lyng", "match": ";" }, { "name": "punctuation.section.block.begin.lyng", "match": "[(]{1}|[{]{1}|\\[" }, { "name": "punctuation.section.block.end.lyng", "match": "[)]{1}|[}]{1}|\\]" }, { "name": "punctuation.accessor.dot.lyng", "match": "\\." }, { "name": "punctuation.separator.colon.lyng", "match": ":" } ] }
|
"punctuation": { "patterns": [ { "name": "punctuation.separator.comma.lyng", "match": "," }, { "name": "punctuation.terminator.statement.lyng", "match": ";" }, { "name": "punctuation.section.block.begin.lyng", "match": "[(]{1}|[{]{1}|\\[" }, { "name": "punctuation.section.block.end.lyng", "match": "[)]{1}|[}]{1}|\\]" }, { "name": "punctuation.accessor.dot.lyng", "match": "\\." }, { "name": "punctuation.separator.colon.lyng", "match": ":" } ] }
|
||||||
|
|||||||
+44
-13
@@ -94,6 +94,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
|
|
||||||
val out = ArrayList<Span>(256)
|
val out = ArrayList<Span>(256)
|
||||||
|
|
||||||
|
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
|
||||||
|
var i = rangeEnd
|
||||||
|
while (i < text.length) {
|
||||||
|
val ch = text[i]
|
||||||
|
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
|
||||||
|
return ch == '(' || ch == '{'
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
|
fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
|
||||||
if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key)
|
if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key)
|
||||||
}
|
}
|
||||||
@@ -207,16 +217,6 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
|
|
||||||
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
||||||
|
|
||||||
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
|
|
||||||
var i = rangeEnd
|
|
||||||
while (i < text.length) {
|
|
||||||
val ch = text[i]
|
|
||||||
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
|
|
||||||
return ch == '(' || ch == '{'
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build simple name -> role map for top-level vals/vars and parameters
|
// Build simple name -> role map for top-level vals/vars and parameters
|
||||||
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
|
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
|
||||||
for (d in mini.declarations) when (d) {
|
for (d in mini.declarations) when (d) {
|
||||||
@@ -253,7 +253,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
|
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add annotation coloring using token highlighter (treat @Label as annotation)
|
// Add annotation/label coloring using token highlighter
|
||||||
run {
|
run {
|
||||||
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
||||||
for (s in tokens) if (s.kind == HighlightKind.Label) {
|
for (s in tokens) if (s.kind == HighlightKind.Label) {
|
||||||
@@ -261,8 +261,29 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
val end = s.range.endExclusive
|
val end = s.range.endExclusive
|
||||||
if (start in 0..end && end <= text.length && start < end) {
|
if (start in 0..end && end <= text.length && start < end) {
|
||||||
val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null }
|
val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null }
|
||||||
if (lexeme != null && lexeme.startsWith("@")) {
|
if (lexeme != null) {
|
||||||
putRange(start, end, LyngHighlighterColors.ANNOTATION)
|
// Heuristic: if it starts with @ and follows a control keyword, it's likely a label
|
||||||
|
// Otherwise if it starts with @ it's an annotation.
|
||||||
|
// If it ends with @ it's a loop label.
|
||||||
|
when {
|
||||||
|
lexeme.endsWith("@") -> putRange(start, end, LyngHighlighterColors.LABEL)
|
||||||
|
lexeme.startsWith("@") -> {
|
||||||
|
// Try to see if it's an exit label
|
||||||
|
val prevNonWs = prevNonWs(text, start)
|
||||||
|
val prevWord = if (prevNonWs >= 0) {
|
||||||
|
var wEnd = prevNonWs + 1
|
||||||
|
var wStart = prevNonWs
|
||||||
|
while (wStart > 0 && text[wStart - 1].isLetter()) wStart--
|
||||||
|
text.substring(wStart, wEnd)
|
||||||
|
} else null
|
||||||
|
|
||||||
|
if (prevWord in setOf("return", "break", "continue") || isFollowedByParenOrBlock(end)) {
|
||||||
|
putRange(start, end, LyngHighlighterColors.LABEL)
|
||||||
|
} else {
|
||||||
|
putRange(start, end, LyngHighlighterColors.ANNOTATION)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -364,6 +385,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
private val CACHE_KEY: Key<Result> = Key.create("LYNG_SEMANTIC_CACHE")
|
private val CACHE_KEY: Key<Result> = Key.create("LYNG_SEMANTIC_CACHE")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun prevNonWs(text: String, idxExclusive: Int): Int {
|
||||||
|
var i = idxExclusive - 1
|
||||||
|
while (i >= 0) {
|
||||||
|
val ch = text[i]
|
||||||
|
if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r') return i
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the error highlight a bit wider than a single character so it is easier to see and click.
|
* Make the error highlight a bit wider than a single character so it is easier to see and click.
|
||||||
* Strategy:
|
* Strategy:
|
||||||
|
|||||||
@@ -43,10 +43,15 @@ class LyngColorSettingsPage : ColorSettingsPage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var counter = 0
|
var counter = 0
|
||||||
counter = counter + 1
|
outer@ while (counter < 10) {
|
||||||
|
if (counter == 5) return@outer
|
||||||
|
counter = counter + 1
|
||||||
|
}
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
|
|
||||||
override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey>? = null
|
override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey> = mutableMapOf(
|
||||||
|
"label" to LyngHighlighterColors.LABEL
|
||||||
|
)
|
||||||
|
|
||||||
override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf(
|
override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf(
|
||||||
AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD),
|
AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD),
|
||||||
@@ -58,6 +63,7 @@ class LyngColorSettingsPage : ColorSettingsPage {
|
|||||||
AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT),
|
AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT),
|
||||||
// Semantic
|
// Semantic
|
||||||
AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION),
|
AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION),
|
||||||
|
AttributesDescriptor("Label (semantic)", LyngHighlighterColors.LABEL),
|
||||||
AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE),
|
AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE),
|
||||||
AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE),
|
AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE),
|
||||||
AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION),
|
AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION),
|
||||||
|
|||||||
@@ -82,4 +82,9 @@ object LyngHighlighterColors {
|
|||||||
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
|
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
|
||||||
"LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD
|
"LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Labels (label@ or @label used as exit target)
|
||||||
|
val LABEL: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
|
||||||
|
"LYNG_LABEL", DefaultLanguageHighlighterColors.LABEL
|
||||||
|
)
|
||||||
}
|
}
|
||||||
@@ -33,10 +33,10 @@ class LyngLexer : LexerBase() {
|
|||||||
|
|
||||||
private val keywords = setOf(
|
private val keywords = setOf(
|
||||||
"fun", "val", "var", "class", "interface", "type", "import", "as",
|
"fun", "val", "var", "class", "interface", "type", "import", "as",
|
||||||
"abstract", "closed", "override",
|
"abstract", "closed", "override", "static", "extern", "open", "private", "protected",
|
||||||
"if", "else", "for", "while", "return", "true", "false", "null",
|
"if", "else", "for", "while", "return", "true", "false", "null",
|
||||||
"when", "in", "is", "break", "continue", "try", "catch", "finally",
|
"when", "in", "is", "break", "continue", "try", "catch", "finally",
|
||||||
"get", "set"
|
"get", "set", "object", "enum", "init", "by", "property", "constructor"
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
|
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
|
||||||
@@ -133,10 +133,24 @@ class LyngLexer : LexerBase() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identifier / keyword
|
// Labels / Annotations: @label or label@
|
||||||
|
if (ch == '@') {
|
||||||
|
i++
|
||||||
|
while (i < endOffset && (buffer[i].isIdentifierPart())) i++
|
||||||
|
myTokenEnd = i
|
||||||
|
myTokenType = LyngTokenTypes.LABEL
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (ch.isIdentifierStart()) {
|
if (ch.isIdentifierStart()) {
|
||||||
i++
|
i++
|
||||||
while (i < endOffset && buffer[i].isIdentifierPart()) i++
|
while (i < endOffset && buffer[i].isIdentifierPart()) i++
|
||||||
|
if (i < endOffset && buffer[i] == '@') {
|
||||||
|
i++
|
||||||
|
myTokenEnd = i
|
||||||
|
myTokenType = LyngTokenTypes.LABEL
|
||||||
|
return
|
||||||
|
}
|
||||||
myTokenEnd = i
|
myTokenEnd = i
|
||||||
val text = buffer.subSequence(myTokenStart, myTokenEnd).toString()
|
val text = buffer.subSequence(myTokenStart, myTokenEnd).toString()
|
||||||
myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER
|
myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER
|
||||||
@@ -160,5 +174,5 @@ class LyngLexer : LexerBase() {
|
|||||||
private fun Char.isDigit(): Boolean = this in '0'..'9'
|
private fun Char.isDigit(): Boolean = this in '0'..'9'
|
||||||
private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter()
|
private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter()
|
||||||
private fun Char.isIdentifierPart(): Boolean = this.isIdentifierStart() || this.isDigit()
|
private fun Char.isIdentifierPart(): Boolean = this.isIdentifierStart() || this.isDigit()
|
||||||
private fun isPunct(c: Char): Boolean = c in setOf('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~')
|
private fun isPunct(c: Char): Boolean = c in setOf('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~', '@')
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@ class LyngSyntaxHighlighter : SyntaxHighlighter {
|
|||||||
LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT)
|
LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT)
|
||||||
LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT)
|
LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT)
|
||||||
LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER)
|
LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER)
|
||||||
|
LyngTokenTypes.LABEL -> pack(LyngHighlighterColors.LABEL)
|
||||||
else -> emptyArray()
|
else -> emptyArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ object LyngTokenTypes {
|
|||||||
val NUMBER = LyngTokenType("NUMBER")
|
val NUMBER = LyngTokenType("NUMBER")
|
||||||
val KEYWORD = LyngTokenType("KEYWORD")
|
val KEYWORD = LyngTokenType("KEYWORD")
|
||||||
val IDENTIFIER = LyngTokenType("IDENTIFIER")
|
val IDENTIFIER = LyngTokenType("IDENTIFIER")
|
||||||
|
val LABEL = LyngTokenType("LABEL")
|
||||||
val PUNCT = LyngTokenType("PUNCT")
|
val PUNCT = LyngTokenType("PUNCT")
|
||||||
val BAD_CHAR = LyngTokenType("BAD_CHAR")
|
val BAD_CHAR = LyngTokenType("BAD_CHAR")
|
||||||
}
|
}
|
||||||
+13
-14
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,7 +20,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
||||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
|
||||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
@@ -53,11 +52,11 @@ kotlin {
|
|||||||
browser()
|
browser()
|
||||||
nodejs()
|
nodejs()
|
||||||
}
|
}
|
||||||
@OptIn(ExperimentalWasmDsl::class)
|
// @OptIn(ExperimentalWasmDsl::class)
|
||||||
wasmJs() {
|
// wasmJs() {
|
||||||
browser()
|
// browser()
|
||||||
nodejs()
|
// nodejs()
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Keep expect/actual warning suppressed consistently with other modules
|
// Keep expect/actual warning suppressed consistently with other modules
|
||||||
targets.configureEach {
|
targets.configureEach {
|
||||||
@@ -94,13 +93,13 @@ kotlin {
|
|||||||
implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}")
|
implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// For Wasm we use in-memory VFS for now
|
// // For Wasm we use in-memory VFS for now
|
||||||
val wasmJsMain by getting {
|
// val wasmJsMain by getting {
|
||||||
dependencies {
|
// dependencies {
|
||||||
api(libs.okio)
|
// api(libs.okio)
|
||||||
implementation(libs.okio.fakefilesystem)
|
// implementation(libs.okio.fakefilesystem)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
actual fun getPlatformDetails(): PlatformDetails {
|
||||||
|
return PlatformDetails(
|
||||||
|
name = "Android",
|
||||||
|
version = android.os.Build.VERSION.RELEASE,
|
||||||
|
arch = android.os.Build.SUPPORTED_ABIS.firstOrNull() ?: "unknown",
|
||||||
|
kernelVersion = System.getProperty("os.version")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun isProcessSupported(): Boolean = false
|
||||||
|
|
||||||
|
actual fun getSystemProcessRunner(): LyngProcessRunner {
|
||||||
|
throw UnsupportedOperationException("Processes are not supported on Android yet")
|
||||||
|
}
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
/*
|
||||||
|
* 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.io.process
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import net.sergeych.lyng.ModuleScope
|
||||||
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.miniast.*
|
||||||
|
import net.sergeych.lyng.obj.*
|
||||||
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
|
import net.sergeych.lyng.statement
|
||||||
|
import net.sergeych.lyngio.process.*
|
||||||
|
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
|
||||||
|
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Install Lyng module `lyng.io.process` into the given scope's ImportManager.
|
||||||
|
*/
|
||||||
|
fun createProcessModule(policy: ProcessAccessPolicy, scope: Scope): Boolean =
|
||||||
|
createProcessModule(policy, scope.importManager)
|
||||||
|
|
||||||
|
/** Same as [createProcessModule] but with explicit [ImportManager]. */
|
||||||
|
fun createProcessModule(policy: ProcessAccessPolicy, manager: ImportManager): Boolean {
|
||||||
|
val name = "lyng.io.process"
|
||||||
|
if (manager.packageNames.contains(name)) return false
|
||||||
|
|
||||||
|
manager.addPackage(name) { module ->
|
||||||
|
buildProcessModule(module, policy)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAccessPolicy) {
|
||||||
|
val runner = try {
|
||||||
|
SecuredLyngProcessRunner(getSystemProcessRunner(), policy)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
val runningProcessType = object : ObjClass("RunningProcess") {}
|
||||||
|
|
||||||
|
runningProcessType.apply {
|
||||||
|
addFnDoc(
|
||||||
|
name = "stdout",
|
||||||
|
doc = "Get standard output stream as a Flow of lines.",
|
||||||
|
returns = type("lyng.Flow"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
val self = thisAs<ObjRunningProcess>()
|
||||||
|
self.process.stdout.toLyngFlow(this)
|
||||||
|
}
|
||||||
|
addFnDoc(
|
||||||
|
name = "stderr",
|
||||||
|
doc = "Get standard error stream as a Flow of lines.",
|
||||||
|
returns = type("lyng.Flow"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
val self = thisAs<ObjRunningProcess>()
|
||||||
|
self.process.stderr.toLyngFlow(this)
|
||||||
|
}
|
||||||
|
addFnDoc(
|
||||||
|
name = "signal",
|
||||||
|
doc = "Send a signal to the process (e.g. 'SIGINT', 'SIGTERM', 'SIGKILL').",
|
||||||
|
params = listOf(ParamDoc("signal", type("lyng.String"))),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
processGuard {
|
||||||
|
val sigStr = requireOnlyArg<ObjString>().value.uppercase()
|
||||||
|
val sig = try {
|
||||||
|
ProcessSignal.valueOf(sigStr)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
try {
|
||||||
|
ProcessSignal.valueOf("SIG$sigStr")
|
||||||
|
} catch (e2: Exception) {
|
||||||
|
raiseIllegalArgument("Unknown signal: $sigStr")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thisAs<ObjRunningProcess>().process.sendSignal(sig)
|
||||||
|
ObjVoid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addFnDoc(
|
||||||
|
name = "waitFor",
|
||||||
|
doc = "Wait for the process to exit and return its exit code.",
|
||||||
|
returns = type("lyng.Int"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
processGuard {
|
||||||
|
thisAs<ObjRunningProcess>().process.waitFor().toObj()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addFnDoc(
|
||||||
|
name = "destroy",
|
||||||
|
doc = "Forcefully terminate the process.",
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
thisAs<ObjRunningProcess>().process.destroy()
|
||||||
|
ObjVoid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val processType = object : ObjClass("Process") {}
|
||||||
|
|
||||||
|
processType.apply {
|
||||||
|
addClassFnDoc(
|
||||||
|
name = "execute",
|
||||||
|
doc = "Execute a process with arguments.",
|
||||||
|
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
|
||||||
|
returns = type("RunningProcess"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
if (runner == null) raiseError("Processes are not supported on this platform")
|
||||||
|
processGuard {
|
||||||
|
val executable = requiredArg<ObjString>(0).value
|
||||||
|
val args = requiredArg<ObjList>(1).list.map { it.toString() }
|
||||||
|
val lp = runner.execute(executable, args)
|
||||||
|
ObjRunningProcess(runningProcessType, lp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addClassFnDoc(
|
||||||
|
name = "shell",
|
||||||
|
doc = "Execute a command via system shell.",
|
||||||
|
params = listOf(ParamDoc("command", type("lyng.String"))),
|
||||||
|
returns = type("RunningProcess"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
if (runner == null) raiseError("Processes are not supported on this platform")
|
||||||
|
processGuard {
|
||||||
|
val command = requireOnlyArg<ObjString>().value
|
||||||
|
val lp = runner.shell(command)
|
||||||
|
ObjRunningProcess(runningProcessType, lp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val platformType = object : ObjClass("Platform") {}
|
||||||
|
|
||||||
|
platformType.apply {
|
||||||
|
addClassFnDoc(
|
||||||
|
name = "details",
|
||||||
|
doc = "Get platform core details.",
|
||||||
|
returns = type("lyng.Map"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
val d = getPlatformDetails()
|
||||||
|
ObjMap(mutableMapOf(
|
||||||
|
ObjString("name") to ObjString(d.name),
|
||||||
|
ObjString("version") to ObjString(d.version),
|
||||||
|
ObjString("arch") to ObjString(d.arch),
|
||||||
|
ObjString("kernelVersion") to (d.kernelVersion?.toObj() ?: ObjNull)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
addClassFnDoc(
|
||||||
|
name = "isSupported",
|
||||||
|
doc = "Check if processes are supported on this platform.",
|
||||||
|
returns = type("lyng.Bool"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
) {
|
||||||
|
isProcessSupported().toObj()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.addConstDoc(
|
||||||
|
name = "Process",
|
||||||
|
value = processType,
|
||||||
|
doc = "Process execution and control.",
|
||||||
|
type = type("Process"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
)
|
||||||
|
module.addConstDoc(
|
||||||
|
name = "Platform",
|
||||||
|
value = platformType,
|
||||||
|
doc = "Platform information.",
|
||||||
|
type = type("Platform"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
)
|
||||||
|
module.addConstDoc(
|
||||||
|
name = "RunningProcess",
|
||||||
|
value = runningProcessType,
|
||||||
|
doc = "Handle to a running process.",
|
||||||
|
type = type("RunningProcess"),
|
||||||
|
moduleName = module.packageName
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
class ObjRunningProcess(
|
||||||
|
override val objClass: ObjClass,
|
||||||
|
val process: LyngProcess
|
||||||
|
) : Obj() {
|
||||||
|
override fun toString(): String = "RunningProcess($process)"
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend inline fun Scope.processGuard(crossinline block: suspend () -> Obj): Obj {
|
||||||
|
return try {
|
||||||
|
block()
|
||||||
|
} catch (e: ProcessAccessDeniedException) {
|
||||||
|
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "process access denied"))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
raiseError(ObjIllegalOperationException(this, e.message ?: "process error"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
|
||||||
|
val producer = statement {
|
||||||
|
val builder = (this as? net.sergeych.lyng.ClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
|
||||||
|
?: this.thisObj as? ObjFlowBuilder
|
||||||
|
|
||||||
|
this@toLyngFlow.collect {
|
||||||
|
try {
|
||||||
|
builder?.output?.send(ObjString(it))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// Channel closed or other error, stop collecting
|
||||||
|
return@collect
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ObjVoid
|
||||||
|
}
|
||||||
|
return ObjFlow(producer, flowScope)
|
||||||
|
}
|
||||||
@@ -1,3 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Filesystem module builtin docs registration, located in lyngio so core library
|
* Filesystem module builtin docs registration, located in lyngio so core library
|
||||||
* does not depend on external packages. The IDEA plugin (and any other tooling)
|
* does not depend on external packages. The IDEA plugin (and any other tooling)
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import net.sergeych.lyngio.process.security.ProcessAccessOp
|
||||||
|
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Common signals for process control.
|
||||||
|
*/
|
||||||
|
enum class ProcessSignal {
|
||||||
|
SIGINT, SIGTERM, SIGKILL
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiplatform process representation.
|
||||||
|
*/
|
||||||
|
interface LyngProcess {
|
||||||
|
/**
|
||||||
|
* Standard output stream as a flow of strings (lines).
|
||||||
|
*/
|
||||||
|
val stdout: Flow<String>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Standard error stream as a flow of strings (lines).
|
||||||
|
*/
|
||||||
|
val stderr: Flow<String>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a signal to the process.
|
||||||
|
* Throws exception if signals are not supported on the platform or for this process.
|
||||||
|
*/
|
||||||
|
suspend fun sendSignal(signal: ProcessSignal)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for the process to exit and return the exit code.
|
||||||
|
*/
|
||||||
|
suspend fun waitFor(): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forcefully terminate the process.
|
||||||
|
*/
|
||||||
|
fun destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for running processes.
|
||||||
|
*/
|
||||||
|
interface LyngProcessRunner {
|
||||||
|
/**
|
||||||
|
* Execute a process with the given executable and arguments.
|
||||||
|
*/
|
||||||
|
suspend fun execute(executable: String, args: List<String>): LyngProcess
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a command via the platform's default shell.
|
||||||
|
*/
|
||||||
|
suspend fun shell(command: String): LyngProcess
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Secured implementation of [LyngProcessRunner] that checks against a [ProcessAccessPolicy].
|
||||||
|
*/
|
||||||
|
class SecuredLyngProcessRunner(
|
||||||
|
private val runner: LyngProcessRunner,
|
||||||
|
private val policy: ProcessAccessPolicy
|
||||||
|
) : LyngProcessRunner {
|
||||||
|
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
|
||||||
|
policy.require(ProcessAccessOp.Execute(executable, args))
|
||||||
|
return runner.execute(executable, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun shell(command: String): LyngProcess {
|
||||||
|
policy.require(ProcessAccessOp.Shell(command))
|
||||||
|
return runner.shell(command)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Platform core details.
|
||||||
|
*/
|
||||||
|
data class PlatformDetails(
|
||||||
|
val name: String,
|
||||||
|
val version: String,
|
||||||
|
val arch: String,
|
||||||
|
val kernelVersion: String? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current platform core details.
|
||||||
|
*/
|
||||||
|
expect fun getPlatformDetails(): PlatformDetails
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the current platform supports processes and shell execution.
|
||||||
|
*/
|
||||||
|
expect fun isProcessSupported(): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the system default [LyngProcessRunner].
|
||||||
|
* Throws [UnsupportedOperationException] if processes are not supported on this platform.
|
||||||
|
*/
|
||||||
|
expect fun getSystemProcessRunner(): LyngProcessRunner
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process.security
|
||||||
|
|
||||||
|
import net.sergeych.lyngio.fs.security.AccessContext
|
||||||
|
import net.sergeych.lyngio.fs.security.AccessDecision
|
||||||
|
import net.sergeych.lyngio.fs.security.Decision
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Primitive process operations for access control decisions.
|
||||||
|
*/
|
||||||
|
sealed interface ProcessAccessOp {
|
||||||
|
data class Execute(val executable: String, val args: List<String>) : ProcessAccessOp
|
||||||
|
data class Shell(val command: String) : ProcessAccessOp
|
||||||
|
}
|
||||||
|
|
||||||
|
class ProcessAccessDeniedException(
|
||||||
|
val op: ProcessAccessOp,
|
||||||
|
val reasonDetail: String? = null,
|
||||||
|
) : IllegalStateException("Process access denied for $op" + (reasonDetail?.let { ": $it" } ?: ""))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Policy interface that decides whether a specific process operation is allowed.
|
||||||
|
*/
|
||||||
|
interface ProcessAccessPolicy {
|
||||||
|
suspend fun check(op: ProcessAccessOp, ctx: AccessContext = AccessContext()): AccessDecision
|
||||||
|
|
||||||
|
// Convenience helpers
|
||||||
|
suspend fun require(op: ProcessAccessOp, ctx: AccessContext = AccessContext()) {
|
||||||
|
val res = check(op, ctx)
|
||||||
|
if (!res.isAllowed()) throw ProcessAccessDeniedException(op, res.reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun canExecute(executable: String, args: List<String>, ctx: AccessContext = AccessContext()) =
|
||||||
|
check(ProcessAccessOp.Execute(executable, args), ctx).isAllowed()
|
||||||
|
|
||||||
|
suspend fun canShell(command: String, ctx: AccessContext = AccessContext()) =
|
||||||
|
check(ProcessAccessOp.Shell(command), ctx).isAllowed()
|
||||||
|
}
|
||||||
|
|
||||||
|
object PermitAllProcessAccessPolicy : ProcessAccessPolicy {
|
||||||
|
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision =
|
||||||
|
AccessDecision(Decision.Allow)
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
internal actual fun getNativeKernelVersion(): String? = null
|
||||||
|
|
||||||
|
internal actual fun isNativeProcessSupported(): Boolean = false
|
||||||
|
|
||||||
|
internal actual fun getNativeProcessRunner(): LyngProcessRunner {
|
||||||
|
throw UnsupportedOperationException("Processes are not supported on iOS")
|
||||||
|
}
|
||||||
@@ -0,0 +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.lyngio.process
|
||||||
|
|
||||||
|
actual fun getPlatformDetails(): PlatformDetails {
|
||||||
|
return PlatformDetails(
|
||||||
|
name = "JavaScript",
|
||||||
|
version = "unknown",
|
||||||
|
arch = "unknown"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun isProcessSupported(): Boolean = false
|
||||||
|
|
||||||
|
actual fun getSystemProcessRunner(): LyngProcessRunner {
|
||||||
|
throw UnsupportedOperationException("Processes are not supported on JS")
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
actual fun getPlatformDetails(): PlatformDetails {
|
||||||
|
val osName = System.getProperty("os.name")
|
||||||
|
return PlatformDetails(
|
||||||
|
name = osName,
|
||||||
|
version = System.getProperty("os.version"),
|
||||||
|
arch = System.getProperty("os.arch"),
|
||||||
|
kernelVersion = if (osName.lowercase().contains("linux")) {
|
||||||
|
System.getProperty("os.version")
|
||||||
|
} else null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual fun isProcessSupported(): Boolean = true
|
||||||
|
|
||||||
|
actual fun getSystemProcessRunner(): LyngProcessRunner = JvmProcessRunner
|
||||||
|
|
||||||
|
object JvmProcessRunner : LyngProcessRunner {
|
||||||
|
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
|
||||||
|
val process = ProcessBuilder(listOf(executable) + args)
|
||||||
|
.start()
|
||||||
|
return JvmLyngProcess(process)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun shell(command: String): LyngProcess {
|
||||||
|
val os = System.getProperty("os.name").lowercase()
|
||||||
|
val shellCmd = if (os.contains("win")) {
|
||||||
|
listOf("cmd.exe", "/c", command)
|
||||||
|
} else {
|
||||||
|
listOf("sh", "-c", command)
|
||||||
|
}
|
||||||
|
val process = ProcessBuilder(shellCmd)
|
||||||
|
.start()
|
||||||
|
return JvmLyngProcess(process)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JvmLyngProcess(private val process: Process) : LyngProcess {
|
||||||
|
override val stdout: Flow<String> = flow {
|
||||||
|
val reader = process.inputStream.bufferedReader()
|
||||||
|
while (true) {
|
||||||
|
val line = reader.readLine() ?: break
|
||||||
|
emit(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override val stderr: Flow<String> = flow {
|
||||||
|
val reader = process.errorStream.bufferedReader()
|
||||||
|
while (true) {
|
||||||
|
val line = reader.readLine() ?: break
|
||||||
|
emit(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun sendSignal(signal: ProcessSignal) {
|
||||||
|
when (signal) {
|
||||||
|
ProcessSignal.SIGINT -> {
|
||||||
|
// SIGINT is hard on JVM without native calls or external 'kill'
|
||||||
|
val os = System.getProperty("os.name").lowercase()
|
||||||
|
if (os.contains("win")) {
|
||||||
|
throw UnsupportedOperationException("SIGINT not supported on Windows JVM")
|
||||||
|
} else {
|
||||||
|
// Try to use kill -2 <pid>
|
||||||
|
try {
|
||||||
|
val pid = process.pid()
|
||||||
|
Runtime.getRuntime().exec(arrayOf("kill", "-2", pid.toString())).waitFor()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw UnsupportedOperationException("Failed to send SIGINT: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ProcessSignal.SIGTERM -> process.destroy()
|
||||||
|
ProcessSignal.SIGKILL -> process.destroyForcibly()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun waitFor(): Int = withContext(Dispatchers.IO) {
|
||||||
|
process.waitFor()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun destroy() {
|
||||||
|
process.destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* 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.io.process
|
||||||
|
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import net.sergeych.lyng.Compiler
|
||||||
|
import net.sergeych.lyng.Script
|
||||||
|
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class LyngProcessModuleTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLyngProcess() = runBlocking {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
createProcessModule(PermitAllProcessAccessPolicy, scope)
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.io.process
|
||||||
|
|
||||||
|
var p = Process.execute("echo", ["hello", "lyng"])
|
||||||
|
var output = []
|
||||||
|
for (line in p.stdout()) {
|
||||||
|
output.add(line)
|
||||||
|
}
|
||||||
|
p.waitFor()
|
||||||
|
output
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val script = Compiler.compile(code)
|
||||||
|
val result = script.execute(scope)
|
||||||
|
assertTrue(result.inspect(scope).contains("hello lyng"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLyngShell() = runBlocking {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
createProcessModule(PermitAllProcessAccessPolicy, scope)
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.io.process
|
||||||
|
|
||||||
|
var p = Process.shell("echo 'shell lyng'")
|
||||||
|
var output = ""
|
||||||
|
for (line in p.stdout()) {
|
||||||
|
output = output + line
|
||||||
|
}
|
||||||
|
p.waitFor()
|
||||||
|
output
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val script = Compiler.compile(code)
|
||||||
|
val result = script.execute(scope)
|
||||||
|
assertTrue(result.inspect(scope).contains("shell lyng"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testPlatformDetails() = runBlocking {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
createProcessModule(PermitAllProcessAccessPolicy, scope)
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.io.process
|
||||||
|
Platform.details()
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val script = Compiler.compile(code)
|
||||||
|
val result = script.execute(scope)
|
||||||
|
assertTrue(result.inspect(scope).contains("name"), "Result should contain 'name', but was: ${result.inspect(scope)}")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.toList
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class JvmProcessTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testExecute() = runBlocking {
|
||||||
|
val runner = getSystemProcessRunner()
|
||||||
|
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
|
||||||
|
|
||||||
|
val process = secured.execute("echo", listOf("hello", "world"))
|
||||||
|
val output = process.stdout.toList()
|
||||||
|
assertEquals(listOf("hello world"), output)
|
||||||
|
assertEquals(0, process.waitFor())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testShell() = runBlocking {
|
||||||
|
val runner = getSystemProcessRunner()
|
||||||
|
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
|
||||||
|
|
||||||
|
val process = secured.shell("echo 'hello shell'")
|
||||||
|
val output = process.stdout.toList()
|
||||||
|
assertEquals(listOf("hello shell"), output)
|
||||||
|
assertEquals(0, process.waitFor())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testPlatformDetails() {
|
||||||
|
val details = getPlatformDetails()
|
||||||
|
assertTrue(details.name.isNotEmpty())
|
||||||
|
assertTrue(isProcessSupported())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.cinterop.*
|
||||||
|
import platform.posix.*
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
internal actual fun getNativeKernelVersion(): String? {
|
||||||
|
return memScoped {
|
||||||
|
val u = alloc<utsname>()
|
||||||
|
if (uname(u.ptr) == 0) {
|
||||||
|
u.release.toKString()
|
||||||
|
} else null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal actual fun isNativeProcessSupported(): Boolean = true
|
||||||
|
|
||||||
|
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.cinterop.*
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.flow.flowOn
|
||||||
|
import platform.posix.*
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
internal class NativeLyngProcess(
|
||||||
|
private val pid: pid_t,
|
||||||
|
private val stdoutFd: Int,
|
||||||
|
private val stderrFd: Int
|
||||||
|
) : LyngProcess {
|
||||||
|
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
|
||||||
|
override val stderr: Flow<String> = createPipeFlow(stderrFd)
|
||||||
|
|
||||||
|
override suspend fun sendSignal(signal: ProcessSignal) {
|
||||||
|
val sig = when (signal) {
|
||||||
|
ProcessSignal.SIGINT -> SIGINT
|
||||||
|
ProcessSignal.SIGTERM -> SIGTERM
|
||||||
|
ProcessSignal.SIGKILL -> SIGKILL
|
||||||
|
}
|
||||||
|
if (kill(pid, sig) != 0) {
|
||||||
|
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
|
||||||
|
memScoped {
|
||||||
|
val status = alloc<IntVar>()
|
||||||
|
if (waitpid(pid, status.ptr, 0) == -1) {
|
||||||
|
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
|
||||||
|
}
|
||||||
|
val s = status.value
|
||||||
|
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun destroy() {
|
||||||
|
kill(pid, SIGKILL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
private fun createPipeFlow(fd: Int): Flow<String> = flow {
|
||||||
|
val buffer = ByteArray(4096)
|
||||||
|
val lineBuffer = StringBuilder()
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
val bytesRead = buffer.usePinned { pinned ->
|
||||||
|
read(fd, pinned.addressOf(0), buffer.size.toULong())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bytesRead <= 0L) break
|
||||||
|
|
||||||
|
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
|
||||||
|
lineBuffer.append(text)
|
||||||
|
|
||||||
|
var newlineIdx = lineBuffer.indexOf('\n')
|
||||||
|
while (newlineIdx != -1) {
|
||||||
|
val line = lineBuffer.substring(0, newlineIdx)
|
||||||
|
emit(line)
|
||||||
|
lineBuffer.deleteRange(0, newlineIdx + 1)
|
||||||
|
newlineIdx = lineBuffer.indexOf('\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (lineBuffer.isNotEmpty()) {
|
||||||
|
emit(lineBuffer.toString())
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
close(fd)
|
||||||
|
}
|
||||||
|
}.flowOn(Dispatchers.Default)
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
object PosixProcessRunner : LyngProcessRunner {
|
||||||
|
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
|
||||||
|
memScoped {
|
||||||
|
val pipeStdout = allocArray<IntVar>(2)
|
||||||
|
val pipeStderr = allocArray<IntVar>(2)
|
||||||
|
|
||||||
|
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
|
||||||
|
if (pipe(pipeStderr) != 0) {
|
||||||
|
close(pipeStdout[0])
|
||||||
|
close(pipeStdout[1])
|
||||||
|
throw RuntimeException("Failed to create stderr pipe")
|
||||||
|
}
|
||||||
|
|
||||||
|
val pid = fork()
|
||||||
|
if (pid == -1) {
|
||||||
|
close(pipeStdout[0])
|
||||||
|
close(pipeStdout[1])
|
||||||
|
close(pipeStderr[0])
|
||||||
|
close(pipeStderr[1])
|
||||||
|
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
|
// Child process
|
||||||
|
dup2(pipeStdout[1], 1)
|
||||||
|
dup2(pipeStderr[1], 2)
|
||||||
|
|
||||||
|
close(pipeStdout[0])
|
||||||
|
close(pipeStdout[1])
|
||||||
|
close(pipeStderr[0])
|
||||||
|
close(pipeStderr[1])
|
||||||
|
|
||||||
|
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
|
||||||
|
argv[0] = executable.cstr.ptr
|
||||||
|
for (i in args.indices) {
|
||||||
|
argv[i + 1] = args[i].cstr.ptr
|
||||||
|
}
|
||||||
|
argv[args.size + 1] = null
|
||||||
|
|
||||||
|
execvp(executable, argv)
|
||||||
|
|
||||||
|
// If we are here, exec failed
|
||||||
|
_exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parent process
|
||||||
|
close(pipeStdout[1])
|
||||||
|
close(pipeStderr[1])
|
||||||
|
|
||||||
|
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun shell(command: String): LyngProcess {
|
||||||
|
return execute("/bin/sh", listOf("-c", command))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.toList
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import net.sergeych.lyng.Compiler
|
||||||
|
import net.sergeych.lyng.Script
|
||||||
|
import net.sergeych.lyng.io.process.createProcessModule
|
||||||
|
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class LinuxProcessTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testExecuteEcho() = runBlocking {
|
||||||
|
val process = PosixProcessRunner.execute("echo", listOf("hello", "native"))
|
||||||
|
val stdout = process.stdout.toList()
|
||||||
|
val exitCode = process.waitFor()
|
||||||
|
|
||||||
|
assertEquals(0, exitCode)
|
||||||
|
assertEquals(listOf("hello native"), stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testShellCommand() = runBlocking {
|
||||||
|
val process = PosixProcessRunner.shell("echo 'shell native' && printf 'line2'")
|
||||||
|
val stdout = process.stdout.toList()
|
||||||
|
val exitCode = process.waitFor()
|
||||||
|
|
||||||
|
assertEquals(0, exitCode)
|
||||||
|
assertEquals(listOf("shell native", "line2"), stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testStderrCapture() = runBlocking {
|
||||||
|
val process = PosixProcessRunner.shell("echo 'to stdout'; echo 'to stderr' >&2")
|
||||||
|
val stdout = process.stdout.toList()
|
||||||
|
val stderr = process.stderr.toList()
|
||||||
|
process.waitFor()
|
||||||
|
|
||||||
|
assertEquals(listOf("to stdout"), stdout)
|
||||||
|
assertEquals(listOf("to stderr"), stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testPlatformDetails() {
|
||||||
|
val details = getPlatformDetails()
|
||||||
|
assertEquals("LINUX", details.name)
|
||||||
|
assertTrue(details.kernelVersion != null)
|
||||||
|
assertTrue(details.kernelVersion!!.isNotEmpty())
|
||||||
|
println("Linux Native Details: $details")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLyngModuleNative() = runBlocking {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
createProcessModule(PermitAllProcessAccessPolicy, scope)
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.io.process
|
||||||
|
|
||||||
|
var p = Process.execute("echo", ["hello", "lyng", "native"])
|
||||||
|
var output = []
|
||||||
|
for (line in p.stdout()) {
|
||||||
|
output.add(line)
|
||||||
|
}
|
||||||
|
p.waitFor()
|
||||||
|
println(output)
|
||||||
|
assertEquals("hello lyng native", output.joinToString(" "))
|
||||||
|
output
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val script = Compiler.compile(code)
|
||||||
|
val result = script.execute(scope)
|
||||||
|
assertTrue(result.inspect(scope).contains("hello lyng native"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.cinterop.*
|
||||||
|
import platform.posix.*
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
internal actual fun getNativeKernelVersion(): String? {
|
||||||
|
return memScoped {
|
||||||
|
val u = alloc<utsname>()
|
||||||
|
if (uname(u.ptr) == 0) {
|
||||||
|
u.release.toKString()
|
||||||
|
} else null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal actual fun isNativeProcessSupported(): Boolean = true
|
||||||
|
|
||||||
|
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlinx.cinterop.*
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.flow.flowOn
|
||||||
|
import platform.posix.*
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
internal class NativeLyngProcess(
|
||||||
|
private val pid: pid_t,
|
||||||
|
private val stdoutFd: Int,
|
||||||
|
private val stderrFd: Int
|
||||||
|
) : LyngProcess {
|
||||||
|
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
|
||||||
|
override val stderr: Flow<String> = createPipeFlow(stderrFd)
|
||||||
|
|
||||||
|
override suspend fun sendSignal(signal: ProcessSignal) {
|
||||||
|
val sig = when (signal) {
|
||||||
|
ProcessSignal.SIGINT -> SIGINT
|
||||||
|
ProcessSignal.SIGTERM -> SIGTERM
|
||||||
|
ProcessSignal.SIGKILL -> SIGKILL
|
||||||
|
}
|
||||||
|
if (kill(pid, sig) != 0) {
|
||||||
|
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
|
||||||
|
memScoped {
|
||||||
|
val status = alloc<IntVar>()
|
||||||
|
if (waitpid(pid, status.ptr, 0) == -1) {
|
||||||
|
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
|
||||||
|
}
|
||||||
|
val s = status.value
|
||||||
|
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun destroy() {
|
||||||
|
kill(pid, SIGKILL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
private fun createPipeFlow(fd: Int): Flow<String> = flow {
|
||||||
|
val buffer = ByteArray(4096)
|
||||||
|
val lineBuffer = StringBuilder()
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
val bytesRead = buffer.usePinned { pinned ->
|
||||||
|
read(fd, pinned.addressOf(0), buffer.size.toULong())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bytesRead <= 0L) break
|
||||||
|
|
||||||
|
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
|
||||||
|
lineBuffer.append(text)
|
||||||
|
|
||||||
|
var newlineIdx = lineBuffer.indexOf('\n')
|
||||||
|
while (newlineIdx != -1) {
|
||||||
|
val line = lineBuffer.substring(0, newlineIdx)
|
||||||
|
emit(line)
|
||||||
|
lineBuffer.deleteRange(0, newlineIdx + 1)
|
||||||
|
newlineIdx = lineBuffer.indexOf('\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (lineBuffer.isNotEmpty()) {
|
||||||
|
emit(lineBuffer.toString())
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
close(fd)
|
||||||
|
}
|
||||||
|
}.flowOn(Dispatchers.Default)
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
|
object PosixProcessRunner : LyngProcessRunner {
|
||||||
|
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
|
||||||
|
memScoped {
|
||||||
|
val pipeStdout = allocArray<IntVar>(2)
|
||||||
|
val pipeStderr = allocArray<IntVar>(2)
|
||||||
|
|
||||||
|
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
|
||||||
|
if (pipe(pipeStderr) != 0) {
|
||||||
|
close(pipeStdout[0])
|
||||||
|
close(pipeStdout[1])
|
||||||
|
throw RuntimeException("Failed to create stderr pipe")
|
||||||
|
}
|
||||||
|
|
||||||
|
val pid = fork()
|
||||||
|
if (pid == -1) {
|
||||||
|
close(pipeStdout[0])
|
||||||
|
close(pipeStdout[1])
|
||||||
|
close(pipeStderr[0])
|
||||||
|
close(pipeStderr[1])
|
||||||
|
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
|
// Child process
|
||||||
|
dup2(pipeStdout[1], 1)
|
||||||
|
dup2(pipeStderr[1], 2)
|
||||||
|
|
||||||
|
close(pipeStdout[0])
|
||||||
|
close(pipeStdout[1])
|
||||||
|
close(pipeStderr[0])
|
||||||
|
close(pipeStderr[1])
|
||||||
|
|
||||||
|
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
|
||||||
|
argv[0] = executable.cstr.ptr
|
||||||
|
for (i in args.indices) {
|
||||||
|
argv[i + 1] = args[i].cstr.ptr
|
||||||
|
}
|
||||||
|
argv[args.size + 1] = null
|
||||||
|
|
||||||
|
execvp(executable, argv)
|
||||||
|
|
||||||
|
// If we are here, exec failed
|
||||||
|
_exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parent process
|
||||||
|
close(pipeStdout[1])
|
||||||
|
close(pipeStderr[1])
|
||||||
|
|
||||||
|
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun shell(command: String): LyngProcess {
|
||||||
|
return execute("/bin/sh", listOf("-c", command))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
internal actual fun getNativeKernelVersion(): String? = null
|
||||||
|
|
||||||
|
internal actual fun isNativeProcessSupported(): Boolean = true
|
||||||
|
|
||||||
|
internal actual fun getNativeProcessRunner(): LyngProcessRunner = WindowsProcessRunner
|
||||||
|
|
||||||
|
object WindowsProcessRunner : LyngProcessRunner {
|
||||||
|
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
|
||||||
|
throw UnsupportedOperationException("Windows native process execution not implemented yet")
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun shell(command: String): LyngProcess {
|
||||||
|
return execute("cmd.exe", listOf("/c", command))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* 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.lyngio.process
|
||||||
|
|
||||||
|
import kotlin.experimental.ExperimentalNativeApi
|
||||||
|
|
||||||
|
@OptIn(ExperimentalNativeApi::class)
|
||||||
|
actual fun getPlatformDetails(): PlatformDetails {
|
||||||
|
return PlatformDetails(
|
||||||
|
name = Platform.osFamily.name,
|
||||||
|
version = "unknown",
|
||||||
|
arch = Platform.cpuArchitecture.name,
|
||||||
|
kernelVersion = getNativeKernelVersion()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal expect fun getNativeKernelVersion(): String?
|
||||||
|
|
||||||
|
actual fun isProcessSupported(): Boolean = isNativeProcessSupported()
|
||||||
|
|
||||||
|
internal expect fun isNativeProcessSupported(): Boolean
|
||||||
|
|
||||||
|
actual fun getSystemProcessRunner(): LyngProcessRunner = getNativeProcessRunner()
|
||||||
|
|
||||||
|
internal expect fun getNativeProcessRunner(): LyngProcessRunner
|
||||||
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
|||||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
group = "net.sergeych"
|
group = "net.sergeych"
|
||||||
version = "1.1.0-SNAPSHOT"
|
version = "1.1.1-SNAPSHOT"
|
||||||
|
|
||||||
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,11 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var anonCounter = 0
|
||||||
|
private fun generateAnonName(pos: Pos): String {
|
||||||
|
return "${"$"}${"Anon"}_${pos.line+1}_${pos.column}_${++anonCounter}"
|
||||||
|
}
|
||||||
|
|
||||||
private fun pushPendingDocToken(t: Token) {
|
private fun pushPendingDocToken(t: Token) {
|
||||||
val s = stripCommentLexeme(t.value)
|
val s = stripCommentLexeme(t.value)
|
||||||
if (pendingDocStart == null) pendingDocStart = t.pos
|
if (pendingDocStart == null) pendingDocStart = t.pos
|
||||||
@@ -170,7 +175,6 @@ class Compiler(
|
|||||||
// A standalone newline not immediately following a comment resets doc buffer
|
// A standalone newline not immediately following a comment resets doc buffer
|
||||||
if (!prevWasComment) clearPendingDoc() else prevWasComment = false
|
if (!prevWasComment) clearPendingDoc() else prevWasComment = false
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
cc.next()
|
cc.next()
|
||||||
@@ -248,7 +252,7 @@ class Compiler(
|
|||||||
when (t.type) {
|
when (t.type) {
|
||||||
Token.Type.RBRACE, Token.Type.EOF, Token.Type.SEMICOLON -> {}
|
Token.Type.RBRACE, Token.Type.EOF, Token.Type.SEMICOLON -> {}
|
||||||
else ->
|
else ->
|
||||||
throw ScriptError(t.pos, "unexpeced `${t.value}` here")
|
throw ScriptError(t.pos, "unexpected `${t.value}` here")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -280,9 +284,11 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var lastAnnotation: (suspend (Scope, ObjString, Statement) -> Statement)? = null
|
private var lastAnnotation: (suspend (Scope, ObjString, Statement) -> Statement)? = null
|
||||||
|
private var lastLabel: String? = null
|
||||||
|
|
||||||
private suspend fun parseStatement(braceMeansLambda: Boolean = false): Statement? {
|
private suspend fun parseStatement(braceMeansLambda: Boolean = false): Statement? {
|
||||||
lastAnnotation = null
|
lastAnnotation = null
|
||||||
|
lastLabel = null
|
||||||
while (true) {
|
while (true) {
|
||||||
val t = cc.next()
|
val t = cc.next()
|
||||||
return when (t.type) {
|
return when (t.type) {
|
||||||
@@ -300,6 +306,10 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Token.Type.ATLABEL -> {
|
Token.Type.ATLABEL -> {
|
||||||
|
val label = t.value
|
||||||
|
if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||||
|
lastLabel = label
|
||||||
|
}
|
||||||
lastAnnotation = parseAnnotation(t)
|
lastAnnotation = parseAnnotation(t)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -648,6 +658,7 @@ class Compiler(
|
|||||||
private suspend fun parseLambdaExpression(): ObjRef {
|
private suspend fun parseLambdaExpression(): ObjRef {
|
||||||
// lambda args are different:
|
// lambda args are different:
|
||||||
val startPos = cc.currentPos()
|
val startPos = cc.currentPos()
|
||||||
|
val label = lastLabel
|
||||||
val argsDeclaration = parseArgsDeclaration()
|
val argsDeclaration = parseArgsDeclaration()
|
||||||
if (argsDeclaration != null && argsDeclaration.endTokenType != Token.Type.ARROW)
|
if (argsDeclaration != null && argsDeclaration.endTokenType != Token.Type.ARROW)
|
||||||
throw ScriptError(
|
throw ScriptError(
|
||||||
@@ -655,7 +666,9 @@ class Compiler(
|
|||||||
"lambda must have either valid arguments declaration with '->' or no arguments"
|
"lambda must have either valid arguments declaration with '->' or no arguments"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
label?.let { cc.labels.add(it) }
|
||||||
val body = parseBlock(skipLeadingBrace = true)
|
val body = parseBlock(skipLeadingBrace = true)
|
||||||
|
label?.let { cc.labels.remove(it) }
|
||||||
|
|
||||||
return ValueFnRef { closureScope ->
|
return ValueFnRef { closureScope ->
|
||||||
statement {
|
statement {
|
||||||
@@ -679,7 +692,12 @@ class Compiler(
|
|||||||
// assign vars as declared the standard way
|
// assign vars as declared the standard way
|
||||||
argsDeclaration.assignToContext(context, defaultAccessType = AccessType.Val)
|
argsDeclaration.assignToContext(context, defaultAccessType = AccessType.Val)
|
||||||
}
|
}
|
||||||
body.execute(context)
|
try {
|
||||||
|
body.execute(context)
|
||||||
|
} catch (e: ReturnException) {
|
||||||
|
if (e.label == null || e.label == label) e.result
|
||||||
|
else throw e
|
||||||
|
}
|
||||||
}.asReadonly
|
}.asReadonly
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1248,6 +1266,8 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Token.Type.OBJECT -> StatementRef(parseObjectDeclaration())
|
||||||
|
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1418,6 +1438,7 @@ class Compiler(
|
|||||||
"while" -> parseWhileStatement()
|
"while" -> parseWhileStatement()
|
||||||
"do" -> parseDoWhileStatement()
|
"do" -> parseDoWhileStatement()
|
||||||
"for" -> parseForStatement()
|
"for" -> parseForStatement()
|
||||||
|
"return" -> parseReturnStatement(id.pos)
|
||||||
"break" -> parseBreakStatement(id.pos)
|
"break" -> parseBreakStatement(id.pos)
|
||||||
"continue" -> parseContinueStatement(id.pos)
|
"continue" -> parseContinueStatement(id.pos)
|
||||||
"if" -> parseIfStatement()
|
"if" -> parseIfStatement()
|
||||||
@@ -1773,6 +1794,10 @@ class Compiler(
|
|||||||
try {
|
try {
|
||||||
// body is a parsed block, it already has separate context
|
// body is a parsed block, it already has separate context
|
||||||
result = body.execute(this)
|
result = body.execute(this)
|
||||||
|
} catch (e: ReturnException) {
|
||||||
|
throw e
|
||||||
|
} catch (e: LoopBreakContinueException) {
|
||||||
|
throw e
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// convert to appropriate exception
|
// convert to appropriate exception
|
||||||
val objException = when (e) {
|
val objException = when (e) {
|
||||||
@@ -1860,8 +1885,12 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun parseObjectDeclaration(): Statement {
|
private suspend fun parseObjectDeclaration(): Statement {
|
||||||
val nameToken = cc.requireToken(Token.Type.ID)
|
val next = cc.peekNextNonWhitespace()
|
||||||
val startPos = pendingDeclStart ?: nameToken.pos
|
val nameToken = if (next.type == Token.Type.ID) cc.requireToken(Token.Type.ID) else null
|
||||||
|
|
||||||
|
val startPos = pendingDeclStart ?: nameToken?.pos ?: cc.current().pos
|
||||||
|
val className = nameToken?.value ?: generateAnonName(startPos)
|
||||||
|
|
||||||
val doc = pendingDeclDoc ?: consumePendingDoc()
|
val doc = pendingDeclDoc ?: consumePendingDoc()
|
||||||
pendingDeclDoc = null
|
pendingDeclDoc = null
|
||||||
pendingDeclStart = null
|
pendingDeclStart = null
|
||||||
@@ -1887,11 +1916,11 @@ class Compiler(
|
|||||||
|
|
||||||
// Robust body detection
|
// Robust body detection
|
||||||
var classBodyRange: MiniRange? = null
|
var classBodyRange: MiniRange? = null
|
||||||
val bodyInit: Statement? = run {
|
val bodyInit: Statement? = inCodeContext(CodeContext.ClassBody(className)) {
|
||||||
val saved = cc.savePos()
|
val saved = cc.savePos()
|
||||||
val next = cc.nextNonWhitespace()
|
val nextBody = cc.nextNonWhitespace()
|
||||||
if (next.type == Token.Type.LBRACE) {
|
if (nextBody.type == Token.Type.LBRACE) {
|
||||||
val bodyStart = next.pos
|
val bodyStart = nextBody.pos
|
||||||
val st = withLocalNames(emptySet()) {
|
val st = withLocalNames(emptySet()) {
|
||||||
parseScript()
|
parseScript()
|
||||||
}
|
}
|
||||||
@@ -1906,15 +1935,15 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val initScope = popInitScope()
|
val initScope = popInitScope()
|
||||||
val className = nameToken.value
|
|
||||||
|
|
||||||
return statement(startPos) { context ->
|
return statement(startPos) { context ->
|
||||||
val parentClasses = baseSpecs.map { baseSpec ->
|
val parentClasses = baseSpecs.map { baseSpec ->
|
||||||
val rec = context[baseSpec.name] ?: throw ScriptError(nameToken.pos, "unknown base class: ${baseSpec.name}")
|
val rec = context[baseSpec.name] ?: throw ScriptError(startPos, "unknown base class: ${baseSpec.name}")
|
||||||
(rec.value as? ObjClass) ?: throw ScriptError(nameToken.pos, "${baseSpec.name} is not a class")
|
(rec.value as? ObjClass) ?: throw ScriptError(startPos, "${baseSpec.name} is not a class")
|
||||||
}
|
}
|
||||||
|
|
||||||
val newClass = ObjInstanceClass(className, *parentClasses.toTypedArray())
|
val newClass = ObjInstanceClass(className, *parentClasses.toTypedArray())
|
||||||
|
newClass.isAnonymous = nameToken == null
|
||||||
for (i in parentClasses.indices) {
|
for (i in parentClasses.indices) {
|
||||||
val argsList = baseSpecs[i].args
|
val argsList = baseSpecs[i].args
|
||||||
// In object, we evaluate parent args once at creation time
|
// In object, we evaluate parent args once at creation time
|
||||||
@@ -1924,12 +1953,14 @@ class Compiler(
|
|||||||
val classScope = context.createChildScope(newThisObj = newClass)
|
val classScope = context.createChildScope(newThisObj = newClass)
|
||||||
classScope.currentClassCtx = newClass
|
classScope.currentClassCtx = newClass
|
||||||
newClass.classScope = classScope
|
newClass.classScope = classScope
|
||||||
|
classScope.addConst("object", newClass)
|
||||||
|
|
||||||
bodyInit?.execute(classScope)
|
bodyInit?.execute(classScope)
|
||||||
|
|
||||||
// Create instance (singleton)
|
// Create instance (singleton)
|
||||||
val instance = newClass.callOn(context.createChildScope(Arguments.EMPTY))
|
val instance = newClass.callOn(context.createChildScope(Arguments.EMPTY))
|
||||||
context.addItem(className, false, instance)
|
if (nameToken != null)
|
||||||
|
context.addItem(className, false, instance)
|
||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2475,6 +2506,38 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun parseReturnStatement(start: Pos): Statement {
|
||||||
|
var t = cc.next()
|
||||||
|
|
||||||
|
val label = if (t.pos.line != start.line || t.type != Token.Type.ATLABEL) {
|
||||||
|
cc.previous()
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
t.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// expression?
|
||||||
|
t = cc.next()
|
||||||
|
cc.previous()
|
||||||
|
val resultExpr = if (t.pos.line == start.line && (!t.isComment &&
|
||||||
|
t.type != Token.Type.SEMICOLON &&
|
||||||
|
t.type != Token.Type.NEWLINE &&
|
||||||
|
t.type != Token.Type.RBRACE &&
|
||||||
|
t.type != Token.Type.RPAREN &&
|
||||||
|
t.type != Token.Type.RBRACKET &&
|
||||||
|
t.type != Token.Type.COMMA &&
|
||||||
|
t.type != Token.Type.EOF)
|
||||||
|
) {
|
||||||
|
// we have something on this line, could be expression
|
||||||
|
parseExpression()
|
||||||
|
} else null
|
||||||
|
|
||||||
|
return statement(start) {
|
||||||
|
val returnValue = resultExpr?.execute(it) ?: ObjVoid
|
||||||
|
throw ReturnException(returnValue, label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun ensureRparen(): Pos {
|
private fun ensureRparen(): Pos {
|
||||||
val t = cc.next()
|
val t = cc.next()
|
||||||
if (t.type != Token.Type.RPAREN)
|
if (t.type != Token.Type.RPAREN)
|
||||||
@@ -2588,6 +2651,7 @@ class Compiler(
|
|||||||
|
|
||||||
// Capture doc locally to reuse even if we need to emit later
|
// Capture doc locally to reuse even if we need to emit later
|
||||||
val declDocLocal = pendingDeclDoc
|
val declDocLocal = pendingDeclDoc
|
||||||
|
val outerLabel = lastLabel
|
||||||
|
|
||||||
// Emit MiniFunDecl before body parsing (body range unknown yet)
|
// Emit MiniFunDecl before body parsing (body range unknown yet)
|
||||||
run {
|
run {
|
||||||
@@ -2614,6 +2678,8 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return inCodeContext(CodeContext.Function(name)) {
|
return inCodeContext(CodeContext.Function(name)) {
|
||||||
|
cc.labels.add(name)
|
||||||
|
outerLabel?.let { cc.labels.add(it) }
|
||||||
|
|
||||||
val paramNames: Set<String> = argsDeclaration.params.map { it.name }.toSet()
|
val paramNames: Set<String> = argsDeclaration.params.map { it.name }.toSet()
|
||||||
|
|
||||||
@@ -2629,7 +2695,9 @@ class Compiler(
|
|||||||
val next = cc.peekNextNonWhitespace()
|
val next = cc.peekNextNonWhitespace()
|
||||||
if (next.type == Token.Type.ASSIGN) {
|
if (next.type == Token.Type.ASSIGN) {
|
||||||
cc.nextNonWhitespace() // consume '='
|
cc.nextNonWhitespace() // consume '='
|
||||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected function body expression")
|
if (cc.peekNextNonWhitespace().value == "return")
|
||||||
|
throw ScriptError(cc.currentPos(), "return is not allowed in shorthand function")
|
||||||
|
val expr = parseExpression() ?: throw ScriptError(cc.currentPos(), "Expected function body expression")
|
||||||
// Shorthand function returns the expression value
|
// Shorthand function returns the expression value
|
||||||
statement(expr.pos) { scope ->
|
statement(expr.pos) { scope ->
|
||||||
expr.execute(scope)
|
expr.execute(scope)
|
||||||
@@ -2661,8 +2729,15 @@ class Compiler(
|
|||||||
if (extTypeName != null) {
|
if (extTypeName != null) {
|
||||||
context.thisObj = callerContext.thisObj
|
context.thisObj = callerContext.thisObj
|
||||||
}
|
}
|
||||||
fnStatements?.execute(context) ?: ObjVoid
|
try {
|
||||||
|
fnStatements?.execute(context) ?: ObjVoid
|
||||||
|
} catch (e: ReturnException) {
|
||||||
|
if (e.label == null || e.label == name || e.label == outerLabel) e.result
|
||||||
|
else throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
cc.labels.remove(name)
|
||||||
|
outerLabel?.let { cc.labels.remove(it) }
|
||||||
// parentContext
|
// parentContext
|
||||||
val fnCreateStatement = statement(start) { context ->
|
val fnCreateStatement = statement(start) { context ->
|
||||||
if (isDelegated) {
|
if (isDelegated) {
|
||||||
@@ -2675,7 +2750,7 @@ class Compiler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (extTypeName != null) {
|
if (extTypeName != null) {
|
||||||
val type = context[extTypeName!!]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
|
val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
|
||||||
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
|
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
|
||||||
context.addExtension(type, name, ObjRecord(ObjUnset, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply {
|
context.addExtension(type, name, ObjRecord(ObjUnset, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply {
|
||||||
delegate = finalDelegate
|
delegate = finalDelegate
|
||||||
@@ -2697,7 +2772,7 @@ class Compiler(
|
|||||||
cls.createField(name, ObjUnset, false, visibility, null, start, declaringClass = cls, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride, type = ObjRecord.Type.Delegated)
|
cls.createField(name, ObjUnset, false, visibility, null, start, declaringClass = cls, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride, type = ObjRecord.Type.Delegated)
|
||||||
cls.instanceInitializers += statement(start) { scp ->
|
cls.instanceInitializers += statement(start) { scp ->
|
||||||
val accessType2 = scp.resolveQualifiedIdentifier("DelegateAccess.Callable")
|
val accessType2 = scp.resolveQualifiedIdentifier("DelegateAccess.Callable")
|
||||||
val initValue2 = delegateExpression!!.execute(scp)
|
val initValue2 = delegateExpression.execute(scp)
|
||||||
val finalDelegate2 = try {
|
val finalDelegate2 = try {
|
||||||
initValue2.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType2, scp.thisObj))
|
initValue2.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType2, scp.thisObj))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -3053,7 +3128,7 @@ class Compiler(
|
|||||||
cc.skipWsTokens()
|
cc.skipWsTokens()
|
||||||
cc.next() // consume '='
|
cc.next() // consume '='
|
||||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected getter expression")
|
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected getter expression")
|
||||||
(expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
expr
|
||||||
} else {
|
} else {
|
||||||
throw ScriptError(cc.current().pos, "Expected { or = after get()")
|
throw ScriptError(cc.current().pos, "Expected { or = after get()")
|
||||||
}
|
}
|
||||||
@@ -3074,7 +3149,7 @@ class Compiler(
|
|||||||
cc.skipWsTokens()
|
cc.skipWsTokens()
|
||||||
cc.next() // consume '='
|
cc.next() // consume '='
|
||||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected setter expression")
|
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected setter expression")
|
||||||
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
val st = expr
|
||||||
statement(st.pos) { scope ->
|
statement(st.pos) { scope ->
|
||||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||||
@@ -3109,7 +3184,7 @@ class Compiler(
|
|||||||
cc.current().pos,
|
cc.current().pos,
|
||||||
"Expected setter expression"
|
"Expected setter expression"
|
||||||
)
|
)
|
||||||
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
val st = expr
|
||||||
statement(st.pos) { scope ->
|
statement(st.pos) { scope ->
|
||||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||||
@@ -3312,7 +3387,7 @@ class Compiler(
|
|||||||
prop
|
prop
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val isLateInitVal = !isMutable && initialExpression == null && getter == null && setter == null
|
val isLateInitVal = !isMutable && initialExpression == null
|
||||||
if (declaringClassName != null && !isStatic) {
|
if (declaringClassName != null && !isStatic) {
|
||||||
val storageName = "$declaringClassName::$name"
|
val storageName = "$declaringClassName::$name"
|
||||||
// If we are in class scope now (defining instance field), defer initialization to instance time
|
// If we are in class scope now (defining instance field), defer initialization to instance time
|
||||||
@@ -3528,6 +3603,9 @@ class Compiler(
|
|||||||
Operator(Token.Type.PERCENTASSIGN, lastPriority) { pos, a, b ->
|
Operator(Token.Type.PERCENTASSIGN, lastPriority) { pos, a, b ->
|
||||||
AssignOpRef(BinOp.PERCENT, a, b, pos)
|
AssignOpRef(BinOp.PERCENT, a, b, pos)
|
||||||
},
|
},
|
||||||
|
Operator(Token.Type.IFNULLASSIGN, lastPriority) { pos, a, b ->
|
||||||
|
AssignIfNullRef(a, b, pos)
|
||||||
|
},
|
||||||
// logical 1
|
// logical 1
|
||||||
Operator(Token.Type.OR, ++lastPriority) { _, a, b ->
|
Operator(Token.Type.OR, ++lastPriority) { _, a, b ->
|
||||||
foldBinary(BinOp.OR, a, b)?.let { return@Operator ConstRef(it.asReadonly) }
|
foldBinary(BinOp.OR, a, b)?.let { return@Operator ConstRef(it.asReadonly) }
|
||||||
|
|||||||
@@ -325,6 +325,7 @@ private class Parser(fromPos: Pos) {
|
|||||||
|
|
||||||
'?' -> {
|
'?' -> {
|
||||||
when (currentChar) {
|
when (currentChar) {
|
||||||
|
'=' -> { pos.advance(); Token("?=", from, Token.Type.IFNULLASSIGN) }
|
||||||
':' -> { pos.advance(); Token("?:", from, Token.Type.ELVIS) }
|
':' -> { pos.advance(); Token("?:", from, Token.Type.ELVIS) }
|
||||||
'?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) }
|
'?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) }
|
||||||
'.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) }
|
'.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) }
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* 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 net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception used to implement `return` statement.
|
||||||
|
* It carries the return value and an optional label for non-local returns.
|
||||||
|
*/
|
||||||
|
class ReturnException(
|
||||||
|
val result: Obj = ObjVoid,
|
||||||
|
val label: String? = null
|
||||||
|
) : RuntimeException()
|
||||||
@@ -314,11 +314,11 @@ open class Scope(
|
|||||||
|
|
||||||
inline fun <reified T : Obj> thisAs(): T {
|
inline fun <reified T : Obj> thisAs(): T {
|
||||||
var s: Scope? = this
|
var s: Scope? = this
|
||||||
do {
|
while (s != null) {
|
||||||
val t = s!!.thisObj
|
val t = s.thisObj
|
||||||
if (t is T) return t
|
if (t is T) return t
|
||||||
s = s.parent
|
s = s.parent
|
||||||
} while (s != null)
|
}
|
||||||
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
|
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -623,9 +623,16 @@ open class Scope(
|
|||||||
|
|
||||||
suspend fun resolve(rec: ObjRecord, name: String): Obj {
|
suspend fun resolve(rec: ObjRecord, name: String): Obj {
|
||||||
if (rec.type == ObjRecord.Type.Delegated) {
|
if (rec.type == ObjRecord.Type.Delegated) {
|
||||||
val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate")
|
val del = rec.delegate ?: run {
|
||||||
|
if (thisObj is ObjInstance) {
|
||||||
|
val res = (thisObj as ObjInstance).resolveRecord(this, rec, name, rec.declaringClass).value
|
||||||
|
rec.value = res
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
raiseError("Internal error: delegated property $name has no delegate")
|
||||||
|
}
|
||||||
val th = if (thisObj === ObjVoid) ObjNull else thisObj
|
val th = if (thisObj === ObjVoid) ObjNull else thisObj
|
||||||
return del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name)), onNotFoundResult = {
|
val res = del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name)), onNotFoundResult = {
|
||||||
// If getValue not found, return a wrapper that calls invoke
|
// If getValue not found, return a wrapper that calls invoke
|
||||||
object : Statement() {
|
object : Statement() {
|
||||||
override val pos: Pos = Pos.builtIn
|
override val pos: Pos = Pos.builtIn
|
||||||
@@ -635,14 +642,22 @@ open class Scope(
|
|||||||
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
|
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})!!
|
})
|
||||||
|
rec.value = res
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
return rec.value
|
return rec.value
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
|
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
|
||||||
if (rec.type == ObjRecord.Type.Delegated) {
|
if (rec.type == ObjRecord.Type.Delegated) {
|
||||||
val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate")
|
val del = rec.delegate ?: run {
|
||||||
|
if (thisObj is ObjInstance) {
|
||||||
|
(thisObj as ObjInstance).writeField(this, name, newValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
raiseError("Internal error: delegated property $name has no delegate")
|
||||||
|
}
|
||||||
val th = if (thisObj === ObjVoid) ObjNull else thisObj
|
val th = if (thisObj === ObjVoid) ObjNull else thisObj
|
||||||
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
|
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class Source(val fileName: String, val text: String) {
|
|||||||
|
|
||||||
fun extractPackageName(): String {
|
fun extractPackageName(): String {
|
||||||
for ((n,line) in lines.withIndex()) {
|
for ((n,line) in lines.withIndex()) {
|
||||||
if( line.isBlank() || line.isEmpty() )
|
if( line.isBlank() )
|
||||||
continue
|
continue
|
||||||
if( line.startsWith("package ") )
|
if( line.startsWith("package ") )
|
||||||
return line.substring(8).trim()
|
return line.substring(8).trim()
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
|
|||||||
LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA,
|
LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA,
|
||||||
SEMICOLON, COLON,
|
SEMICOLON, COLON,
|
||||||
PLUS, MINUS, STAR, SLASH, PERCENT,
|
PLUS, MINUS, STAR, SLASH, PERCENT,
|
||||||
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN,
|
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN,
|
||||||
PLUS2, MINUS2,
|
PLUS2, MINUS2,
|
||||||
IN, NOTIN, IS, NOTIS, BY,
|
IN, NOTIN, IS, NOTIS, BY,
|
||||||
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
|
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
|
||||||
|
|||||||
@@ -477,7 +477,7 @@ private fun splitIntoParts(
|
|||||||
private fun applyMinimalSpacingRules(code: String): String {
|
private fun applyMinimalSpacingRules(code: String): String {
|
||||||
var s = code
|
var s = code
|
||||||
// Ensure space before '(' for control-flow keywords
|
// Ensure space before '(' for control-flow keywords
|
||||||
s = s.replace(Regex("\\b(if|for|while)\\("), "$1 (")
|
s = s.replace(Regex("\\b(if|for|while|return|break|continue)\\("), "$1 (")
|
||||||
// Space before '{' for control-flow headers only (avoid function declarations)
|
// Space before '{' for control-flow headers only (avoid function declarations)
|
||||||
s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
|
s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
|
||||||
s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {")
|
s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {")
|
||||||
@@ -498,6 +498,8 @@ private fun applyMinimalSpacingRules(code: String): String {
|
|||||||
s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {")
|
s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {")
|
||||||
// Ensure space before '(' for catch parameter
|
// Ensure space before '(' for catch parameter
|
||||||
s = s.replace(Regex("\\bcatch\\("), "catch (")
|
s = s.replace(Regex("\\bcatch\\("), "catch (")
|
||||||
|
// Remove space between control keyword and label: return @label -> return@label
|
||||||
|
s = s.replace(Regex("\\b(return|break|continue)\\s+(@[\\p{L}_][\\p{L}\\p{N}_]*)"), "$1$2")
|
||||||
// Remove spaces just inside parentheses/brackets: "( a )" -> "(a)"
|
// Remove spaces just inside parentheses/brackets: "( a )" -> "(a)"
|
||||||
s = s.replace(Regex("\\(\\s+"), "(")
|
s = s.replace(Regex("\\(\\s+"), "(")
|
||||||
// Do not strip leading indentation before a closing bracket/paren on its own line
|
// Do not strip leading indentation before a closing bracket/paren on its own line
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ private fun kindOf(type: Type, value: String): HighlightKind? = when (type) {
|
|||||||
|
|
||||||
// operators and symbolic constructs
|
// operators and symbolic constructs
|
||||||
Type.PLUS, Type.MINUS, Type.STAR, Type.SLASH, Type.PERCENT,
|
Type.PLUS, Type.MINUS, Type.STAR, Type.SLASH, Type.PERCENT,
|
||||||
Type.ASSIGN, Type.PLUSASSIGN, Type.MINUSASSIGN, Type.STARASSIGN, Type.SLASHASSIGN, Type.PERCENTASSIGN,
|
Type.ASSIGN, Type.PLUSASSIGN, Type.MINUSASSIGN, Type.STARASSIGN, Type.SLASHASSIGN, Type.PERCENTASSIGN, Type.IFNULLASSIGN,
|
||||||
Type.PLUS2, Type.MINUS2,
|
Type.PLUS2, Type.MINUS2,
|
||||||
Type.EQ, Type.NEQ, Type.LT, Type.LTE, Type.GT, Type.GTE, Type.REF_EQ, Type.REF_NEQ, Type.MATCH, Type.NOTMATCH,
|
Type.EQ, Type.NEQ, Type.LT, Type.LTE, Type.GT, Type.GTE, Type.REF_EQ, Type.REF_NEQ, Type.MATCH, Type.NOTMATCH,
|
||||||
Type.DOT, Type.ARROW, Type.EQARROW, Type.QUESTION, Type.COLONCOLON,
|
Type.DOT, Type.ARROW, Type.EQARROW, Type.QUESTION, Type.COLONCOLON,
|
||||||
|
|||||||
@@ -96,17 +96,15 @@ object CompletionEngineLight {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
|
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
|
||||||
mini?.let { m ->
|
val decls = mini.declarations
|
||||||
val decls = m.declarations
|
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
funs.forEach { offerDeclAdd(out, prefix, it) }
|
||||||
funs.forEach { offerDeclAdd(out, prefix, it) }
|
classes.forEach { offerDeclAdd(out, prefix, it) }
|
||||||
classes.forEach { offerDeclAdd(out, prefix, it) }
|
enums.forEach { offerDeclAdd(out, prefix, it) }
|
||||||
enums.forEach { offerDeclAdd(out, prefix, it) }
|
vals.forEach { offerDeclAdd(out, prefix, it) }
|
||||||
vals.forEach { offerDeclAdd(out, prefix, it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Imported and builtin
|
// Imported and builtin
|
||||||
val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
|
val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
|
||||||
@@ -192,7 +190,7 @@ object CompletionEngineLight {
|
|||||||
val chosen = variants.asSequence()
|
val chosen = variants.asSequence()
|
||||||
.filterIsInstance<MiniMemberValDecl>()
|
.filterIsInstance<MiniMemberValDecl>()
|
||||||
.firstOrNull { it.type != null } ?: rep
|
.firstOrNull { it.type != null } ?: rep
|
||||||
val ci = CompletionItem(name, Kind.Field, typeText = typeOf((chosen as MiniMemberValDecl).type))
|
val ci = CompletionItem(name, Kind.Field, typeText = typeOf(chosen.type))
|
||||||
if (ci.name.startsWith(prefix, true)) out += ci
|
if (ci.name.startsWith(prefix, true)) out += ci
|
||||||
}
|
}
|
||||||
is MiniInitDecl -> {}
|
is MiniInitDecl -> {}
|
||||||
|
|||||||
@@ -137,11 +137,20 @@ open class Obj {
|
|||||||
// methods that to override
|
// methods that to override
|
||||||
|
|
||||||
open suspend fun compareTo(scope: Scope, other: Obj): Int {
|
open suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||||
if( other === this) return 0
|
if (other === this) return 0
|
||||||
if( other === ObjNull ) return 2
|
if (other === ObjNull || other === ObjUnset || other === ObjVoid) return 2
|
||||||
scope.raiseNotImplemented()
|
scope.raiseNotImplemented()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open suspend fun equals(scope: Scope, other: Obj): Boolean {
|
||||||
|
if (other === this) return true
|
||||||
|
return try {
|
||||||
|
compareTo(scope, other) == 0
|
||||||
|
} catch (e: ExecutionError) {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
open suspend fun contains(scope: Scope, other: Obj): Boolean {
|
open suspend fun contains(scope: Scope, other: Obj): Boolean {
|
||||||
return invokeInstanceMethod(scope, "contains", other).toBool()
|
return invokeInstanceMethod(scope, "contains", other).toBool()
|
||||||
}
|
}
|
||||||
@@ -364,7 +373,7 @@ open class Obj {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
|
open suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
|
||||||
if (obj.type == ObjRecord.Type.Delegated) {
|
if (obj.type == ObjRecord.Type.Delegated) {
|
||||||
val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
|
val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
|
||||||
return obj.copy(
|
return obj.copy(
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ private object ClassIdGen { var c: Long = 1L; fun nextId(): Long = c++ }
|
|||||||
|
|
||||||
val ObjClassType by lazy {
|
val ObjClassType by lazy {
|
||||||
ObjClass("Class").apply {
|
ObjClass("Class").apply {
|
||||||
|
addProperty("className", getter = { thisAs<ObjClass>().classNameObj })
|
||||||
addFnDoc(
|
addFnDoc(
|
||||||
name = "name",
|
name = "name",
|
||||||
doc = "Simple name of this class (without package).",
|
doc = "Simple name of this class (without package).",
|
||||||
@@ -91,6 +92,8 @@ open class ObjClass(
|
|||||||
vararg parents: ObjClass,
|
vararg parents: ObjClass,
|
||||||
) : Obj() {
|
) : Obj() {
|
||||||
|
|
||||||
|
var isAnonymous: Boolean = false
|
||||||
|
|
||||||
var isAbstract: Boolean = false
|
var isAbstract: Boolean = false
|
||||||
|
|
||||||
// Stable identity and simple structural version for PICs
|
// Stable identity and simple structural version for PICs
|
||||||
@@ -181,11 +184,8 @@ open class ObjClass(
|
|||||||
val base = c3Linearize(this, mutableMapOf())
|
val base = c3Linearize(this, mutableMapOf())
|
||||||
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
|
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
|
||||||
else {
|
else {
|
||||||
// During very early bootstrap rootObjectType might not be initialized yet.
|
val root = Obj.rootObjectType
|
||||||
// We use a safe check here.
|
base + root
|
||||||
@Suppress("UNNECESSARY_SAFE_CALL")
|
|
||||||
val root = net.sergeych.lyng.obj.Obj.rootObjectType
|
|
||||||
if (root != null) base + root else base
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,7 +235,7 @@ open class ObjClass(
|
|||||||
// 1) members-defined methods
|
// 1) members-defined methods
|
||||||
for ((k, v) in members) {
|
for ((k, v) in members) {
|
||||||
if (v.value is Statement || v.type == ObjRecord.Type.Delegated) {
|
if (v.value is Statement || v.type == ObjRecord.Type.Delegated) {
|
||||||
instance.instanceScope.objects[k] = v
|
instance.instanceScope.objects[k] = if (v.type == ObjRecord.Type.Delegated) v.copy() else v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 2) class-scope methods registered during class-body execution
|
// 2) class-scope methods registered during class-body execution
|
||||||
@@ -243,7 +243,7 @@ open class ObjClass(
|
|||||||
if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) {
|
if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) {
|
||||||
// if not already present, copy reference for dispatch
|
// if not already present, copy reference for dispatch
|
||||||
if (!instance.instanceScope.objects.containsKey(k)) {
|
if (!instance.instanceScope.objects.containsKey(k)) {
|
||||||
instance.instanceScope.objects[k] = rec
|
instance.instanceScope.objects[k] = if (rec.type == ObjRecord.Type.Delegated) rec.copy() else rec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -480,7 +480,7 @@ open class ObjClass(
|
|||||||
|
|
||||||
fun addClassConst(name: String, value: Obj) = createClassField(name, value)
|
fun addClassConst(name: String, value: Obj) = createClassField(name, value)
|
||||||
fun addClassFn(name: String, isOpen: Boolean = false, code: suspend Scope.() -> Obj) {
|
fun addClassFn(name: String, isOpen: Boolean = false, code: suspend Scope.() -> Obj) {
|
||||||
createClassField(name, statement { code() }, isOpen)
|
createClassField(name, statement { code() }, isOpen, type = ObjRecord.Type.Fun)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -588,11 +588,21 @@ open class ObjClass(
|
|||||||
getInstanceMemberOrNull(name)?.let { rec ->
|
getInstanceMemberOrNull(name)?.let { rec ->
|
||||||
val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this
|
val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this
|
||||||
if (rec.type == ObjRecord.Type.Delegated) {
|
if (rec.type == ObjRecord.Type.Delegated) {
|
||||||
val del = rec.delegate ?: scope.raiseError("Internal error: delegated function $name has no delegate")
|
val del = rec.delegate ?: scope.raiseError("Internal error: delegated member $name has no delegate")
|
||||||
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
|
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
|
||||||
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
|
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs), onNotFoundResult = {
|
||||||
|
// Fallback: property delegation
|
||||||
|
val propVal = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
|
||||||
|
propVal.invoke(scope, this, args, decl)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (rec.type == ObjRecord.Type.Fun) {
|
||||||
|
return rec.value.invoke(scope, this, args, decl)
|
||||||
|
} else {
|
||||||
|
// Resolved field or property value
|
||||||
|
val resolved = readField(scope, name)
|
||||||
|
return resolved.value.invoke(scope, this, args, decl)
|
||||||
}
|
}
|
||||||
return rec.value.invoke(scope, this, args, decl)
|
|
||||||
}
|
}
|
||||||
return super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
|
return super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,10 +92,9 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
del = del ?: obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
|
del = del ?: obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
|
||||||
return obj.copy(
|
val res = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
|
||||||
value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))),
|
obj.value = res
|
||||||
type = ObjRecord.Type.Other
|
return obj
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return super.resolveRecord(scope, obj, name, decl)
|
return super.resolveRecord(scope, obj, name, decl)
|
||||||
}
|
}
|
||||||
@@ -197,18 +196,22 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
|||||||
if (rec.type == ObjRecord.Type.Delegated) {
|
if (rec.type == ObjRecord.Type.Delegated) {
|
||||||
val storageName = "${cls.className}::$name"
|
val storageName = "${cls.className}::$name"
|
||||||
val del = instanceScope[storageName]?.delegate ?: rec.delegate
|
val del = instanceScope[storageName]?.delegate ?: rec.delegate
|
||||||
?: scope.raiseError("Internal error: delegated function $name has no delegate (tried $storageName)")
|
?: scope.raiseError("Internal error: delegated member $name has no delegate (tried $storageName)")
|
||||||
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
|
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
|
||||||
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
|
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs), onNotFoundResult = {
|
||||||
|
// Fallback: property delegation
|
||||||
|
val propVal = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
|
||||||
|
propVal.invoke(scope, this, args, rec.declaringClass ?: cls)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if (rec.type != ObjRecord.Type.Property && !rec.isAbstract) {
|
if (rec.type == ObjRecord.Type.Fun && !rec.isAbstract) {
|
||||||
val decl = rec.declaringClass ?: cls
|
val decl = rec.declaringClass ?: cls
|
||||||
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
|
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
|
||||||
if (!canAccessMember(rec.visibility, decl, caller))
|
if (!canAccessMember(rec.visibility, decl, caller))
|
||||||
scope.raiseError(
|
scope.raiseError(
|
||||||
ObjIllegalAccessException(
|
ObjIllegalAccessException(
|
||||||
scope,
|
scope,
|
||||||
"can't invoke method $name (declared in ${decl.className ?: "?"})"
|
"can't invoke method $name (declared in ${decl.className})"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return rec.value.invoke(
|
return rec.value.invoke(
|
||||||
@@ -217,6 +220,9 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
|||||||
args,
|
args,
|
||||||
decl
|
decl
|
||||||
)
|
)
|
||||||
|
} else if ((rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property) && !rec.isAbstract) {
|
||||||
|
val resolved = readField(scope, name)
|
||||||
|
return resolved.value.invoke(scope, this, args, resolved.declaringClass)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,7 +263,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
|||||||
val params = meta.params.map { readField(scope, it.name).value }
|
val params = meta.params.map { readField(scope, it.name).value }
|
||||||
encoder.encodeAnyList(scope, params)
|
encoder.encodeAnyList(scope, params)
|
||||||
val vars = serializingVars.values.map { it.value }
|
val vars = serializingVars.values.map { it.value }
|
||||||
if (vars.isNotEmpty<Obj>()) {
|
if (vars.isNotEmpty()) {
|
||||||
encoder.encodeAnyList(scope, vars)
|
encoder.encodeAnyList(scope, vars)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,9 +115,9 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||||
if (isIntRange) {
|
if (start is ObjInt && end is ObjInt) {
|
||||||
val s = (start as ObjInt).value
|
val s = start.value
|
||||||
val e = (end as ObjInt).value
|
val e = end.value
|
||||||
if (isEndInclusive) {
|
if (isEndInclusive) {
|
||||||
for (i in s..e) {
|
for (i in s..e) {
|
||||||
if (!callback(ObjInt.of(i))) break
|
if (!callback(ObjInt.of(i))) break
|
||||||
@@ -127,9 +127,9 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
|
|||||||
if (!callback(ObjInt.of(i))) break
|
if (!callback(ObjInt.of(i))) break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isCharRange) {
|
} else if (start is ObjChar && end is ObjChar) {
|
||||||
val s = (start as ObjChar).value
|
val s = start.value
|
||||||
val e = (end as ObjChar).value
|
val e = end.value
|
||||||
if (isEndInclusive) {
|
if (isEndInclusive) {
|
||||||
for (c in s..e) {
|
for (c in s..e) {
|
||||||
if (!callback(ObjChar(c))) break
|
if (!callback(ObjChar(c))) break
|
||||||
|
|||||||
@@ -29,14 +29,19 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
|
|||||||
override val objClass: ObjClass = type
|
override val objClass: ObjClass = type
|
||||||
|
|
||||||
fun Scope.init() {
|
fun Scope.init() {
|
||||||
if (self.start == null || self.end == null)
|
val s = self.start
|
||||||
raiseError("next is only available for finite ranges")
|
val e = self.end
|
||||||
isCharRange = self.isCharRange
|
if (s is ObjInt && e is ObjInt) {
|
||||||
lastIndex = if (self.isIntRange || self.isCharRange) {
|
lastIndex = if (self.isEndInclusive)
|
||||||
if (self.isEndInclusive)
|
(e.value - s.value + 1).toInt()
|
||||||
self.end.toInt() - self.start.toInt() + 1
|
|
||||||
else
|
else
|
||||||
self.end.toInt() - self.start.toInt()
|
(e.value - s.value).toInt()
|
||||||
|
} else if (s is ObjChar && e is ObjChar) {
|
||||||
|
isCharRange = true
|
||||||
|
lastIndex = if (self.isEndInclusive)
|
||||||
|
(e.value.code - s.value.code + 1)
|
||||||
|
else
|
||||||
|
(e.value.code - s.value.code)
|
||||||
} else {
|
} else {
|
||||||
raiseError("not implemented iterator for range of $this")
|
raiseError("not implemented iterator for range of $this")
|
||||||
}
|
}
|
||||||
@@ -46,10 +51,13 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
|
|||||||
|
|
||||||
fun next(scope: Scope): Obj =
|
fun next(scope: Scope): Obj =
|
||||||
if (nextIndex < lastIndex) {
|
if (nextIndex < lastIndex) {
|
||||||
val x = if (self.isEndInclusive)
|
val start = self.start
|
||||||
self.start!!.toLong() + nextIndex++
|
val x = if (start is ObjInt)
|
||||||
|
start.value + nextIndex++
|
||||||
|
else if (start is ObjChar)
|
||||||
|
start.value.code.toLong() + nextIndex++
|
||||||
else
|
else
|
||||||
self.start!!.toLong() + nextIndex++
|
scope.raiseError("iterator error: unsupported range start")
|
||||||
if( isCharRange ) ObjChar(x.toInt().toChar()) else ObjInt(x)
|
if( isCharRange ) ObjChar(x.toInt().toChar()) else ObjInt(x)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -243,8 +243,8 @@ class BinaryOpRef(private val op: BinOp, private val left: ObjRef, private val r
|
|||||||
BinOp.OR -> a.logicalOr(scope, b)
|
BinOp.OR -> a.logicalOr(scope, b)
|
||||||
BinOp.AND -> a.logicalAnd(scope, b)
|
BinOp.AND -> a.logicalAnd(scope, b)
|
||||||
BinOp.EQARROW -> ObjMapEntry(a, b)
|
BinOp.EQARROW -> ObjMapEntry(a, b)
|
||||||
BinOp.EQ -> ObjBool(a.compareTo(scope, b) == 0)
|
BinOp.EQ -> ObjBool(a.equals(scope, b))
|
||||||
BinOp.NEQ -> ObjBool(a.compareTo(scope, b) != 0)
|
BinOp.NEQ -> ObjBool(!a.equals(scope, b))
|
||||||
BinOp.REF_EQ -> ObjBool(a === b)
|
BinOp.REF_EQ -> ObjBool(a === b)
|
||||||
BinOp.REF_NEQ -> ObjBool(a !== b)
|
BinOp.REF_NEQ -> ObjBool(a !== b)
|
||||||
BinOp.MATCH -> a.operatorMatch(scope, b)
|
BinOp.MATCH -> a.operatorMatch(scope, b)
|
||||||
@@ -321,16 +321,23 @@ class CastRef(
|
|||||||
/** Qualified `this@Type`: resolves to a view of current `this` starting dispatch from the ancestor Type. */
|
/** Qualified `this@Type`: resolves to a view of current `this` starting dispatch from the ancestor Type. */
|
||||||
class QualifiedThisRef(private val typeName: String, private val atPos: Pos) : ObjRef {
|
class QualifiedThisRef(private val typeName: String, private val atPos: Pos) : ObjRef {
|
||||||
override suspend fun get(scope: Scope): ObjRecord {
|
override suspend fun get(scope: Scope): ObjRecord {
|
||||||
val thisObj = scope.thisObj
|
|
||||||
val t = scope[typeName]?.value as? ObjClass
|
val t = scope[typeName]?.value as? ObjClass
|
||||||
?: scope.raiseError("unknown type $typeName")
|
?: scope.raiseError("unknown type $typeName")
|
||||||
val inst = (thisObj as? ObjInstance)
|
|
||||||
?: scope.raiseClassCastError("this is not an instance")
|
var s: Scope? = scope
|
||||||
if (!inst.objClass.allParentsSet.contains(t) && inst.objClass !== t)
|
while (s != null) {
|
||||||
scope.raiseClassCastError(
|
val inst = s.thisObj as? ObjInstance
|
||||||
"Qualifier ${'$'}{t.className} is not an ancestor of ${'$'}{inst.objClass.className} (order: ${'$'}{inst.objClass.renderLinearization(true)})"
|
if (inst != null) {
|
||||||
)
|
if (inst.objClass === t || inst.objClass.allParentsSet.contains(t)) {
|
||||||
return ObjQualifiedView(inst, t).asReadonly
|
return ObjQualifiedView(inst, t).asReadonly
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s = s.parent
|
||||||
|
}
|
||||||
|
|
||||||
|
scope.raiseClassCastError(
|
||||||
|
"No instance of type ${t.className} found in the scope chain"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,9 +469,11 @@ class FieldRef(
|
|||||||
// Adaptive PIC (2→4) for reads/writes
|
// Adaptive PIC (2→4) for reads/writes
|
||||||
private var rAccesses: Int = 0; private var rMisses: Int = 0; private var rPromotedTo4: Boolean = false
|
private var rAccesses: Int = 0; private var rMisses: Int = 0; private var rPromotedTo4: Boolean = false
|
||||||
private var wAccesses: Int = 0; private var wMisses: Int = 0; private var wPromotedTo4: Boolean = false
|
private var wAccesses: Int = 0; private var wMisses: Int = 0; private var wPromotedTo4: Boolean = false
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
private inline fun size4ReadsEnabled(): Boolean =
|
private inline fun size4ReadsEnabled(): Boolean =
|
||||||
PerfFlags.FIELD_PIC_SIZE_4 ||
|
PerfFlags.FIELD_PIC_SIZE_4 ||
|
||||||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && rPromotedTo4)
|
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && rPromotedTo4)
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
private inline fun size4WritesEnabled(): Boolean =
|
private inline fun size4WritesEnabled(): Boolean =
|
||||||
PerfFlags.FIELD_PIC_SIZE_4 ||
|
PerfFlags.FIELD_PIC_SIZE_4 ||
|
||||||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && wPromotedTo4)
|
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && wPromotedTo4)
|
||||||
@@ -611,7 +620,7 @@ class FieldRef(
|
|||||||
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
|
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
|
||||||
val fieldPic = PerfFlags.FIELD_PIC
|
val fieldPic = PerfFlags.FIELD_PIC
|
||||||
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
|
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
|
||||||
val base = target.get(scope).value
|
val base = target.evalValue(scope)
|
||||||
if (base == ObjNull && isOptional) {
|
if (base == ObjNull && isOptional) {
|
||||||
// no-op on null receiver for optional chaining assignment
|
// no-op on null receiver for optional chaining assignment
|
||||||
return
|
return
|
||||||
@@ -714,10 +723,9 @@ class FieldRef(
|
|||||||
|
|
||||||
override suspend fun evalValue(scope: Scope): Obj {
|
override suspend fun evalValue(scope: Scope): Obj {
|
||||||
// Mirror get(), but return raw Obj to avoid transient ObjRecord on R-value paths
|
// Mirror get(), but return raw Obj to avoid transient ObjRecord on R-value paths
|
||||||
val fastRval = PerfFlags.RVAL_FASTPATH
|
|
||||||
val fieldPic = PerfFlags.FIELD_PIC
|
val fieldPic = PerfFlags.FIELD_PIC
|
||||||
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
|
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
|
||||||
val base = if (fastRval) target.evalValue(scope) else target.get(scope).value
|
val base = target.evalValue(scope)
|
||||||
if (base == ObjNull && isOptional) return ObjNull
|
if (base == ObjNull && isOptional) return ObjNull
|
||||||
if (fieldPic) {
|
if (fieldPic) {
|
||||||
val (key, ver) = receiverKeyAndVersion(base)
|
val (key, ver) = receiverKeyAndVersion(base)
|
||||||
@@ -1058,6 +1066,7 @@ class MethodCallRef(
|
|||||||
private var mFreezeWindowsLeft: Int = 0
|
private var mFreezeWindowsLeft: Int = 0
|
||||||
private var mWindowAccesses: Int = 0
|
private var mWindowAccesses: Int = 0
|
||||||
private var mWindowMisses: Int = 0
|
private var mWindowMisses: Int = 0
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
private inline fun size4MethodsEnabled(): Boolean =
|
private inline fun size4MethodsEnabled(): Boolean =
|
||||||
PerfFlags.METHOD_PIC_SIZE_4 ||
|
PerfFlags.METHOD_PIC_SIZE_4 ||
|
||||||
((PerfFlags.PIC_ADAPTIVE_2_TO_4 || PerfFlags.PIC_ADAPTIVE_METHODS_ONLY) && mPromotedTo4 && mFreezeWindowsLeft == 0)
|
((PerfFlags.PIC_ADAPTIVE_2_TO_4 || PerfFlags.PIC_ADAPTIVE_METHODS_ONLY) && mPromotedTo4 && mFreezeWindowsLeft == 0)
|
||||||
@@ -1583,7 +1592,7 @@ class FastLocalVarRef(
|
|||||||
if (slot >= 0 && actualOwner != null) {
|
if (slot >= 0 && actualOwner != null) {
|
||||||
val rec = actualOwner.getSlotRecord(slot)
|
val rec = actualOwner.getSlotRecord(slot)
|
||||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
|
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
|
||||||
return actualOwner.resolve(rec, name)
|
return scope.resolve(rec, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Try per-frame local binding maps in the ancestry first
|
// Try per-frame local binding maps in the ancestry first
|
||||||
@@ -1796,6 +1805,21 @@ class RangeRef(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Assignment if null op: target ?= value */
|
||||||
|
class AssignIfNullRef(
|
||||||
|
private val target: ObjRef,
|
||||||
|
private val value: ObjRef,
|
||||||
|
private val atPos: Pos,
|
||||||
|
) : ObjRef {
|
||||||
|
override suspend fun get(scope: Scope): ObjRecord {
|
||||||
|
val current = target.evalValue(scope)
|
||||||
|
if (current != ObjNull) return current.asReadonly
|
||||||
|
val newValue = value.evalValue(scope)
|
||||||
|
target.setAt(atPos, scope, newValue)
|
||||||
|
return newValue.asReadonly
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Simple assignment: target = value */
|
/** Simple assignment: target = value */
|
||||||
class AssignRef(
|
class AssignRef(
|
||||||
private val target: ObjRef,
|
private val target: ObjRef,
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ class ObjRegexMatch(val match: MatchResult) : Obj() {
|
|||||||
// Use groupValues so that index 0 is the whole match and subsequent indices are capturing groups,
|
// Use groupValues so that index 0 is the whole match and subsequent indices are capturing groups,
|
||||||
// which matches the language/tests expectation for `$~[i]`.
|
// which matches the language/tests expectation for `$~[i]`.
|
||||||
ObjList(
|
ObjList(
|
||||||
match.groupValues.map { ObjString(it) as Obj }.toMutableList()
|
match.groupValues.map { ObjString(it) }.toMutableList()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ open class LynonEncoder(val bout: BitOutput, val settings: LynonSettings = Lynon
|
|||||||
* Caching is used automatically.
|
* Caching is used automatically.
|
||||||
*/
|
*/
|
||||||
suspend fun encodeAny(scope: Scope, obj: Obj) {
|
suspend fun encodeAny(scope: Scope, obj: Obj) {
|
||||||
|
if (obj.objClass.isAnonymous) scope.raiseError("Cannot serialize anonymous object")
|
||||||
encodeCached(obj) {
|
encodeCached(obj) {
|
||||||
val type = putTypeRecord(scope, obj, obj.lynonType())
|
val type = putTypeRecord(scope, obj, obj.lynonType())
|
||||||
obj.serialize(scope, this, type)
|
obj.serialize(scope, this, type)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ class BindingHighlightTest {
|
|||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini, "Mini-AST must be built")
|
assertNotNull(mini, "Mini-AST must be built")
|
||||||
|
|
||||||
val binding = Binder.bind(text, mini!!)
|
val binding = Binder.bind(text, mini)
|
||||||
|
|
||||||
// Find the top-level symbol for counter and ensure it is mutable (Variable)
|
// Find the top-level symbol for counter and ensure it is mutable (Variable)
|
||||||
val sym = binding.symbols.firstOrNull { it.name == "counter" }
|
val sym = binding.symbols.firstOrNull { it.name == "counter" }
|
||||||
@@ -78,7 +78,7 @@ class BindingHighlightTest {
|
|||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini, "Mini-AST must be built")
|
assertNotNull(mini, "Mini-AST must be built")
|
||||||
|
|
||||||
val binding = Binder.bind(text, mini!!)
|
val binding = Binder.bind(text, mini)
|
||||||
|
|
||||||
val sym = binding.symbols.firstOrNull { it.name == "answer" }
|
val sym = binding.symbols.firstOrNull { it.name == "answer" }
|
||||||
assertNotNull(sym, "Top-level val 'answer' must be registered as a symbol")
|
assertNotNull(sym, "Top-level val 'answer' must be registered as a symbol")
|
||||||
@@ -114,7 +114,7 @@ class BindingHighlightTest {
|
|||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini, "Mini-AST must be built")
|
assertNotNull(mini, "Mini-AST must be built")
|
||||||
|
|
||||||
val binding = Binder.bind(text, mini!!)
|
val binding = Binder.bind(text, mini)
|
||||||
|
|
||||||
// Ensure we registered the local var/val symbol for `name`
|
// Ensure we registered the local var/val symbol for `name`
|
||||||
val nameSym = binding.symbols.firstOrNull { it.name == "name" }
|
val nameSym = binding.symbols.firstOrNull { it.name == "name" }
|
||||||
@@ -163,7 +163,7 @@ class BindingHighlightTest {
|
|||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini, "Mini-AST must be built")
|
assertNotNull(mini, "Mini-AST must be built")
|
||||||
|
|
||||||
val binding = Binder.bind(text, mini!!)
|
val binding = Binder.bind(text, mini)
|
||||||
|
|
||||||
val nameSym = binding.symbols.firstOrNull { it.name == "name" && (it.kind == SymbolKind.Variable || it.kind == SymbolKind.Value) }
|
val nameSym = binding.symbols.firstOrNull { it.name == "name" && (it.kind == SymbolKind.Variable || it.kind == SymbolKind.Value) }
|
||||||
assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol")
|
assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol")
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class BindingTest {
|
|||||||
Compiler.compileWithMini(src, sink)
|
Compiler.compileWithMini(src, sink)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini, "MiniScript should be built")
|
assertNotNull(mini, "MiniScript should be built")
|
||||||
return Binder.bind(src, mini!!)
|
return Binder.bind(src, mini)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -72,7 +72,7 @@ class BindingTest {
|
|||||||
val xSym = snap.symbols.firstOrNull { it.name == "x" }
|
val xSym = snap.symbols.firstOrNull { it.name == "x" }
|
||||||
assertNotNull(xSym)
|
assertNotNull(xSym)
|
||||||
// One reference usage to top-level x
|
// One reference usage to top-level x
|
||||||
val refs = snap.references.filter { it.symbolId == xSym!!.id }
|
val refs = snap.references.filter { it.symbolId == xSym.id }
|
||||||
assertEquals(1, refs.size)
|
assertEquals(1, refs.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ class BindingTest {
|
|||||||
val fooField = snap.symbols.firstOrNull { it.name == "foo" }
|
val fooField = snap.symbols.firstOrNull { it.name == "foo" }
|
||||||
assertNotNull(fooField)
|
assertNotNull(fooField)
|
||||||
// Should have at least one reference (usage in bar)
|
// Should have at least one reference (usage in bar)
|
||||||
val refs = snap.references.count { it.symbolId == fooField!!.id }
|
val refs = snap.references.count { it.symbolId == fooField.id }
|
||||||
assertEquals(1, refs)
|
assertEquals(1, refs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ class BindingTest {
|
|||||||
)
|
)
|
||||||
val xField = snap.symbols.firstOrNull { it.name == "x" }
|
val xField = snap.symbols.firstOrNull { it.name == "x" }
|
||||||
assertNotNull(xField)
|
assertNotNull(xField)
|
||||||
val refs = snap.references.count { it.symbolId == xField!!.id }
|
val refs = snap.references.count { it.symbolId == xField.id }
|
||||||
assertEquals(1, refs)
|
assertEquals(1, refs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import net.sergeych.lyng.eval
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
class IfNullAssignTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testBasicAssignment() = runTest {
|
||||||
|
eval("""
|
||||||
|
var x = null
|
||||||
|
x ?= 42
|
||||||
|
assertEquals(42, x)
|
||||||
|
x ?= 100
|
||||||
|
assertEquals(42, x)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testPropertyAssignment() = runTest {
|
||||||
|
eval("""
|
||||||
|
class Box(var value)
|
||||||
|
val b = Box(null)
|
||||||
|
b.value ?= 10
|
||||||
|
assertEquals(10, b.value)
|
||||||
|
b.value ?= 20
|
||||||
|
assertEquals(10, b.value)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testIndexAssignment() = runTest {
|
||||||
|
eval("""
|
||||||
|
val a = [null, 1]
|
||||||
|
a[0] ?= 10
|
||||||
|
assertEquals(10, a[0])
|
||||||
|
a[1] ?= 20
|
||||||
|
assertEquals(1, a[1])
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testOptionalChaining() = runTest {
|
||||||
|
eval("""
|
||||||
|
class Inner(var value)
|
||||||
|
class Outer(var inner)
|
||||||
|
|
||||||
|
var o = null
|
||||||
|
o?.inner?.value ?= 10 // should do nothing
|
||||||
|
assertEquals(null, o)
|
||||||
|
|
||||||
|
o = Outer(null)
|
||||||
|
o?.inner?.value ?= 10 // should do nothing because inner is null
|
||||||
|
assertEquals(null, o.inner)
|
||||||
|
|
||||||
|
o.inner = Inner(null)
|
||||||
|
o?.inner?.value ?= 42
|
||||||
|
assertEquals(42, o.inner.value)
|
||||||
|
o?.inner?.value ?= 100
|
||||||
|
assertEquals(42, o.inner.value)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDoubleEvaluation() = runTest {
|
||||||
|
eval("""
|
||||||
|
var count = 0
|
||||||
|
fun getIdx() {
|
||||||
|
count = count + 1
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
val a = [null]
|
||||||
|
a[getIdx()] ?= 10
|
||||||
|
|
||||||
|
// Current behavior: double evaluation happens in ObjRef for compound ops
|
||||||
|
// getIdx() is called once for checking if null, and once for setting if it was null.
|
||||||
|
assertEquals(10, a[0])
|
||||||
|
assertEquals(2, count)
|
||||||
|
|
||||||
|
a[getIdx()] ?= 20
|
||||||
|
// If it's NOT null, it only evaluates once to check the value.
|
||||||
|
assertEquals(10, a[0])
|
||||||
|
assertEquals(3, count)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testReturnValue() = runTest {
|
||||||
|
eval("""
|
||||||
|
var x = null
|
||||||
|
val r1 = (x ?= 42)
|
||||||
|
assertEquals(42, r1)
|
||||||
|
assertEquals(42, x)
|
||||||
|
|
||||||
|
val r2 = (x ?= 100)
|
||||||
|
assertEquals(42, r2)
|
||||||
|
assertEquals(42, x)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,7 +44,7 @@ class MiniAstTest {
|
|||||||
val (_, sink) = compileWithMini(code)
|
val (_, sink) = compileWithMini(code)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini)
|
assertNotNull(mini)
|
||||||
val imps = mini!!.imports
|
val imps = mini.imports
|
||||||
assertTrue(imps.isNotEmpty(), "imports should be captured")
|
assertTrue(imps.isNotEmpty(), "imports should be captured")
|
||||||
val first = imps.first()
|
val first = imps.first()
|
||||||
val segNames = first.segments.map { it.name }
|
val segNames = first.segments.map { it.name }
|
||||||
@@ -68,12 +68,12 @@ class MiniAstTest {
|
|||||||
val (_, sink) = compileWithMini(code)
|
val (_, sink) = compileWithMini(code)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini)
|
assertNotNull(mini)
|
||||||
val fn = mini!!.declarations.filterIsInstance<MiniFunDecl>().firstOrNull { it.name == "foo" }
|
val fn = mini.declarations.filterIsInstance<MiniFunDecl>().firstOrNull { it.name == "foo" }
|
||||||
assertNotNull(fn, "function decl should be captured")
|
assertNotNull(fn, "function decl should be captured")
|
||||||
// Doc
|
// Doc
|
||||||
assertNotNull(fn.doc)
|
assertNotNull(fn.doc)
|
||||||
assertEquals("Summary: does foo", fn.doc!!.summary)
|
assertEquals("Summary: does foo", fn.doc.summary)
|
||||||
assertTrue(fn.doc!!.raw.contains("details"))
|
assertTrue(fn.doc.raw.contains("details"))
|
||||||
// Params
|
// Params
|
||||||
assertEquals(2, fn.params.size)
|
assertEquals(2, fn.params.size)
|
||||||
val p1 = fn.params[0]
|
val p1 = fn.params[0]
|
||||||
@@ -99,10 +99,10 @@ class MiniAstTest {
|
|||||||
val (_, sink) = compileWithMini(code)
|
val (_, sink) = compileWithMini(code)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini)
|
assertNotNull(mini)
|
||||||
val vd = mini!!.declarations.filterIsInstance<net.sergeych.lyng.miniast.MiniValDecl>().firstOrNull { it.name == "x" }
|
val vd = mini.declarations.filterIsInstance<net.sergeych.lyng.miniast.MiniValDecl>().firstOrNull { it.name == "x" }
|
||||||
assertNotNull(vd)
|
assertNotNull(vd)
|
||||||
assertNotNull(vd.doc)
|
assertNotNull(vd.doc)
|
||||||
assertEquals("docs for x", vd.doc!!.summary)
|
assertEquals("docs for x", vd.doc.summary)
|
||||||
val ty = vd.type
|
val ty = vd.type
|
||||||
assertNotNull(ty)
|
assertNotNull(ty)
|
||||||
val gen = ty as MiniGenericType
|
val gen = ty as MiniGenericType
|
||||||
@@ -126,10 +126,10 @@ class MiniAstTest {
|
|||||||
val (_, sink) = compileWithMini(code)
|
val (_, sink) = compileWithMini(code)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini)
|
assertNotNull(mini)
|
||||||
val cd = mini!!.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" }
|
val cd = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" }
|
||||||
assertNotNull(cd)
|
assertNotNull(cd)
|
||||||
assertNotNull(cd.doc, "Class doc should be preserved even with members")
|
assertNotNull(cd.doc, "Class doc should be preserved even with members")
|
||||||
assertTrue(cd.doc!!.raw.contains("Class C docs"))
|
assertTrue(cd.doc.raw.contains("Class C docs"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -141,10 +141,10 @@ class MiniAstTest {
|
|||||||
val (_, sink) = compileWithMini(code)
|
val (_, sink) = compileWithMini(code)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini)
|
assertNotNull(mini)
|
||||||
val cd = mini!!.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" }
|
val cd = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" }
|
||||||
assertNotNull(cd)
|
assertNotNull(cd)
|
||||||
assertNotNull(cd.doc)
|
assertNotNull(cd.doc)
|
||||||
assertTrue(cd.doc!!.raw.contains("Class C docs"))
|
assertTrue(cd.doc.raw.contains("Class C docs"))
|
||||||
// Bases captured as plain names for now
|
// Bases captured as plain names for now
|
||||||
assertEquals(listOf("Base1", "Base2"), cd.bases)
|
assertEquals(listOf("Base1", "Base2"), cd.bases)
|
||||||
}
|
}
|
||||||
@@ -162,10 +162,10 @@ class MiniAstTest {
|
|||||||
val (_, sink) = compileWithMini(code)
|
val (_, sink) = compileWithMini(code)
|
||||||
val mini = sink.build()
|
val mini = sink.build()
|
||||||
assertNotNull(mini)
|
assertNotNull(mini)
|
||||||
val ed = mini!!.declarations.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == "E" }
|
val ed = mini.declarations.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == "E" }
|
||||||
assertNotNull(ed)
|
assertNotNull(ed)
|
||||||
assertNotNull(ed.doc)
|
assertNotNull(ed.doc)
|
||||||
assertTrue(ed.doc!!.raw.contains("Enum E docs"))
|
assertTrue(ed.doc.raw.contains("Enum E docs"))
|
||||||
assertEquals(listOf("A", "B", "C"), ed.entries)
|
assertEquals(listOf("A", "B", "C"), ed.entries)
|
||||||
assertEquals("E", ed.name)
|
assertEquals("E", ed.name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -711,4 +711,24 @@ class OOTest {
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
fun testBasicObjectExpression() = runTest {
|
||||||
|
eval("""
|
||||||
|
val x = object { val y = 1 }
|
||||||
|
assertEquals(1, x.y)
|
||||||
|
|
||||||
|
class Base(v) {
|
||||||
|
val value = v
|
||||||
|
fun squares() = value * value
|
||||||
|
}
|
||||||
|
|
||||||
|
val y = object : Base(2) {
|
||||||
|
override val value = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(25, y.squares())
|
||||||
|
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import net.sergeych.lynon.lynonEncodeAny
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
|
|
||||||
|
class ObjectExpressionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testBasicObjectExpression() = runTest {
|
||||||
|
eval("""
|
||||||
|
val x = object { val y = 1 }
|
||||||
|
assertEquals(1, x.y)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testInheritanceWithArgs() = runTest {
|
||||||
|
eval("""
|
||||||
|
class Base(x) {
|
||||||
|
val value = x
|
||||||
|
val squares = x * x
|
||||||
|
}
|
||||||
|
|
||||||
|
val y = object : Base(5) {
|
||||||
|
val z = value + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(5, y.value)
|
||||||
|
assertEquals(25, y.squares)
|
||||||
|
assertEquals(6, y.z)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMultipleInheritance() = runTest {
|
||||||
|
eval("""
|
||||||
|
interface A { fun a() = "A" }
|
||||||
|
interface B { fun b() = "B" }
|
||||||
|
|
||||||
|
val x = object : A, B {
|
||||||
|
fun c() = a() + b()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("AB", x.c())
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testScopeCapture() = runTest {
|
||||||
|
eval("""
|
||||||
|
fun createCounter(start) {
|
||||||
|
var count = start
|
||||||
|
object {
|
||||||
|
fun next() {
|
||||||
|
val res = count
|
||||||
|
count = count + 1
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val c = createCounter(10)
|
||||||
|
assertEquals(10, c.next())
|
||||||
|
assertEquals(11, c.next())
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testThisObjectAlias() = runTest {
|
||||||
|
eval("""
|
||||||
|
val x = object {
|
||||||
|
val value = 42
|
||||||
|
fun self() = this@object
|
||||||
|
fun getValue() = this@object.value
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(42, x.getValue())
|
||||||
|
// assert(x === x.self()) // Lyng might not have === for identity yet, checking if it compiles and runs
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSerializationRejection() = runTest {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
val obj = scope.eval("object { val x = 1 }")
|
||||||
|
assertFailsWith<Exception> {
|
||||||
|
lynonEncodeAny(scope, obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testQualifiedThis() = runTest {
|
||||||
|
eval("""
|
||||||
|
class Outer {
|
||||||
|
val value = 1
|
||||||
|
fun getObj() {
|
||||||
|
object {
|
||||||
|
fun getOuterValue() = this@Outer.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val o = Outer()
|
||||||
|
val x = o.getObj()
|
||||||
|
assertEquals(1, x.getOuterValue())
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDiagnosticName() = runTest {
|
||||||
|
// This is harder to test directly, but we can check if it has a class and if that class name looks "anonymous"
|
||||||
|
eval("""
|
||||||
|
val x = object { }
|
||||||
|
val name = x::class.className
|
||||||
|
assert(name.startsWith("${'$'}Anon_"))
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import net.sergeych.lyng.ScriptError
|
||||||
|
import net.sergeych.lyng.eval
|
||||||
|
import net.sergeych.lyng.obj.toInt
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
|
|
||||||
|
class ReturnStatementTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testBasicReturn() = runTest {
|
||||||
|
assertEquals(10, eval("""
|
||||||
|
fun foo() {
|
||||||
|
return 10
|
||||||
|
20
|
||||||
|
}
|
||||||
|
foo()
|
||||||
|
""").toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testReturnFromIf() = runTest {
|
||||||
|
assertEquals(5, eval("""
|
||||||
|
fun foo(x) {
|
||||||
|
if (x > 0) return 5
|
||||||
|
10
|
||||||
|
}
|
||||||
|
foo(1)
|
||||||
|
""").toInt())
|
||||||
|
|
||||||
|
assertEquals(10, eval("""
|
||||||
|
fun foo(x) {
|
||||||
|
if (x > 0) return 5
|
||||||
|
10
|
||||||
|
}
|
||||||
|
foo(-1)
|
||||||
|
""").toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testReturnFromLambda() = runTest {
|
||||||
|
assertEquals(2, eval("""
|
||||||
|
val f = { x ->
|
||||||
|
if (x < 0) return 0
|
||||||
|
x * 2
|
||||||
|
}
|
||||||
|
f(1)
|
||||||
|
""").toInt())
|
||||||
|
|
||||||
|
assertEquals(0, eval("""
|
||||||
|
val f = { x ->
|
||||||
|
if (x < 0) return 0
|
||||||
|
x * 2
|
||||||
|
}
|
||||||
|
f(-1)
|
||||||
|
""").toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testNonLocalReturn() = runTest {
|
||||||
|
assertEquals(100, eval("""
|
||||||
|
fun outer() {
|
||||||
|
[1, 2, 3].forEach {
|
||||||
|
if (it == 2) return@outer 100
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
outer()
|
||||||
|
""").toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLabeledLambdaReturn() = runTest {
|
||||||
|
assertEquals(42, eval("""
|
||||||
|
val f = @inner { x ->
|
||||||
|
if (x == 0) return@inner 42
|
||||||
|
x
|
||||||
|
}
|
||||||
|
f(0)
|
||||||
|
""").toInt())
|
||||||
|
|
||||||
|
assertEquals(5, eval("""
|
||||||
|
val f = @inner { x ->
|
||||||
|
if (x == 0) return@inner 42
|
||||||
|
x
|
||||||
|
}
|
||||||
|
f(5)
|
||||||
|
""").toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testForbidEqualReturn() = runTest {
|
||||||
|
assertFailsWith<ScriptError> {
|
||||||
|
eval("fun foo(x) = return x")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testDeepNestedReturn() = runTest {
|
||||||
|
assertEquals(42, eval("""
|
||||||
|
fun find() {
|
||||||
|
val data = [[1, 2], [3, 42], [5, 6]]
|
||||||
|
data.forEach { row ->
|
||||||
|
row.forEach { item ->
|
||||||
|
if (item == 42) return@find item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
find()
|
||||||
|
""").toInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testReturnFromOuterLambda() = runTest {
|
||||||
|
assertEquals("found", eval("""
|
||||||
|
val f_outer = @outer {
|
||||||
|
val f_inner = {
|
||||||
|
return@outer "found"
|
||||||
|
}
|
||||||
|
f_inner()
|
||||||
|
"not found"
|
||||||
|
}
|
||||||
|
f_outer()
|
||||||
|
""").toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4560,4 +4560,16 @@ class ScriptTest {
|
|||||||
""".trimIndent())
|
""".trimIndent())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testOptOnNullAssignment() = runTest {
|
||||||
|
eval("""
|
||||||
|
var x = null
|
||||||
|
assertEquals(null, x)
|
||||||
|
x ?= 1
|
||||||
|
assertEquals(1, x)
|
||||||
|
x ?= 2
|
||||||
|
assertEquals(1, x)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -113,4 +113,22 @@ class StdlibTest {
|
|||||||
assertEquals(5, (1..10).toList().count { it % 2 == 1 } )
|
assertEquals(5, (1..10).toList().count { it % 2 == 1 } )
|
||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testWith() = runTest {
|
||||||
|
eval("""
|
||||||
|
class Person(val name, var age)
|
||||||
|
val p = Person("Alice", 30)
|
||||||
|
|
||||||
|
val result = with(p) {
|
||||||
|
assertEquals("Alice", name)
|
||||||
|
assertEquals(30, age)
|
||||||
|
age = 31
|
||||||
|
"done"
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("done", result)
|
||||||
|
assertEquals(31, p.age)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -210,4 +210,117 @@ class DelegationTest {
|
|||||||
assert(l is Delegate)
|
assert(l is Delegate)
|
||||||
""")
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testRealLifeBug1() = runTest {
|
||||||
|
eval("""
|
||||||
|
class Cell {
|
||||||
|
val tags = [1,2,3]
|
||||||
|
}
|
||||||
|
class T {
|
||||||
|
val cell by lazy { Cell() }
|
||||||
|
val tags get() = cell.tags
|
||||||
|
}
|
||||||
|
assertEquals([1,2,3], T().tags)
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testInstanceIsolation() = runTest {
|
||||||
|
eval("""
|
||||||
|
class CounterDelegate() {
|
||||||
|
private var count = 0
|
||||||
|
fun getValue(thisRef, name) = ++count
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
val x by CounterDelegate()
|
||||||
|
}
|
||||||
|
|
||||||
|
val f1 = Foo()
|
||||||
|
val f2 = Foo()
|
||||||
|
|
||||||
|
assertEquals(1, f1.x)
|
||||||
|
assertEquals(1, f2.x)
|
||||||
|
assertEquals(2, f1.x)
|
||||||
|
assertEquals(2, f2.x)
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLazyRegexBug() = runTest {
|
||||||
|
eval("""
|
||||||
|
class T {
|
||||||
|
val re by lazy { Regex(".*") }
|
||||||
|
}
|
||||||
|
val t = T()
|
||||||
|
t.re
|
||||||
|
// Second access triggered the bug before fix (value == Unset failed)
|
||||||
|
t.re
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testEqualityRobustness() = runTest {
|
||||||
|
eval("""
|
||||||
|
val re1 = Regex("a")
|
||||||
|
val re2 = Regex("a")
|
||||||
|
// Equality should not throw even if types don't implement compareTo
|
||||||
|
assertEquals(true, re1 == re1)
|
||||||
|
assertEquals(false, re1 == re2)
|
||||||
|
assertEquals(false, re1 == Unset)
|
||||||
|
assertEquals(false, re1 == null)
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLazy2() = runTest {
|
||||||
|
eval("""
|
||||||
|
class A {
|
||||||
|
val tags = [1,2,3]
|
||||||
|
}
|
||||||
|
class B {
|
||||||
|
val tags by lazy { myA.tags }
|
||||||
|
val myA by lazy { A() }
|
||||||
|
}
|
||||||
|
assert( B().tags == [1,2,3])
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testThisInLazy() = runTest {
|
||||||
|
eval("""
|
||||||
|
class A {
|
||||||
|
val numbers = [1,2,3]
|
||||||
|
val tags by lazy { this.numbers }
|
||||||
|
}
|
||||||
|
class B {
|
||||||
|
val a by lazy { A() }
|
||||||
|
val test by lazy { a.tags + [4] }
|
||||||
|
}
|
||||||
|
assertEquals( [1,2,3], A().tags)
|
||||||
|
assertEquals( [1,2,3,4], B().test)
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testScopeInLazy() = runTest {
|
||||||
|
val s1 = Script.newScope()
|
||||||
|
s1.eval("""
|
||||||
|
class A {
|
||||||
|
val tags by lazy { GLOBAL_NUMBERS }
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
// on the same scope, it will see my GLOBAL_NUMBERS:
|
||||||
|
s1.eval("""
|
||||||
|
val GLOBAL_NUMBERS = [1,2,3]
|
||||||
|
class B {
|
||||||
|
val a by lazy { A() }
|
||||||
|
val test by lazy { a.tags + [4] }
|
||||||
|
}
|
||||||
|
assertEquals( [1,2,3], A().tags)
|
||||||
|
assertEquals( [1,2,3,4], B().test)
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ class BlockReindentTest {
|
|||||||
val open = BraceUtils.findMatchingOpenBrace(text, close)
|
val open = BraceUtils.findMatchingOpenBrace(text, close)
|
||||||
assertNotNull(open)
|
assertNotNull(open)
|
||||||
// The char at open must be '{'
|
// The char at open must be '{'
|
||||||
assertEquals('{', text[open!!])
|
assertEquals('{', text[open])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -55,7 +55,7 @@ class BlockReindentTest {
|
|||||||
val range = BraceUtils.findEnclosingBlockRange(text, close, includeTrailingNewline = true)
|
val range = BraceUtils.findEnclosingBlockRange(text, close, includeTrailingNewline = true)
|
||||||
assertNotNull(range)
|
assertNotNull(range)
|
||||||
// The range must start at the line start of the matching '{' and end at or after the newline after '}'
|
// The range must start at the line start of the matching '{' and end at or after the newline after '}'
|
||||||
val start = range!!.first
|
val start = range.first
|
||||||
val end = range.last + 1
|
val end = range.last + 1
|
||||||
val startLinePrefix = text.substring(BraceUtils.lineStart(text, start), start)
|
val startLinePrefix = text.substring(BraceUtils.lineStart(text, start), start)
|
||||||
// start at column 0 of the line
|
// start at column 0 of the line
|
||||||
|
|||||||
@@ -21,6 +21,15 @@ import kotlin.test.assertEquals
|
|||||||
|
|
||||||
class LyngFormatterTest {
|
class LyngFormatterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun labelFormatting() {
|
||||||
|
val src = "return @label; break @outer; continue @inner"
|
||||||
|
val expected = "return@label; break@outer; continue@inner"
|
||||||
|
val cfg = LyngFormatConfig(applySpacing = true)
|
||||||
|
val out = LyngFormatter.format(src, cfg)
|
||||||
|
assertEquals(expected, out)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun reindent_simpleFunction() {
|
fun reindent_simpleFunction() {
|
||||||
val src = """
|
val src = """
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class CommentEolTest {
|
|||||||
assertTrue(cmt != null, "Expected a comment span")
|
assertTrue(cmt != null, "Expected a comment span")
|
||||||
// It should start at 0 and extend exactly to the end of the line (before \n)
|
// It should start at 0 and extend exactly to the end of the line (before \n)
|
||||||
val eol = text.indexOf('\n')
|
val eol = text.indexOf('\n')
|
||||||
assertEquals(0, cmt!!.range.start, "Comment should start at column 0")
|
assertEquals(0, cmt.range.start, "Comment should start at column 0")
|
||||||
assertEquals(eol, cmt.range.endExclusive, "Comment should extend to EOL")
|
assertEquals(eol, cmt.range.endExclusive, "Comment should extend to EOL")
|
||||||
// Ensure there is no other span overlapping within the same line
|
// Ensure there is no other span overlapping within the same line
|
||||||
spans.filter { it !== cmt }.forEach {
|
spans.filter { it !== cmt }.forEach {
|
||||||
@@ -50,7 +50,7 @@ class CommentEolTest {
|
|||||||
assertTrue(cmt != null, "Expected a block comment span")
|
assertTrue(cmt != null, "Expected a block comment span")
|
||||||
// The comment should end right after "/* block */"
|
// The comment should end right after "/* block */"
|
||||||
val expectedEnd = "/* block */".length
|
val expectedEnd = "/* block */".length
|
||||||
assertEquals(expectedEnd, cmt!!.range.endExclusive, "Block comment should not be extended to EOL")
|
assertEquals(expectedEnd, cmt.range.endExclusive, "Block comment should not be extended to EOL")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -25,6 +25,16 @@ class HighlightMappingTest {
|
|||||||
private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> =
|
private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> =
|
||||||
spans.map { text.substring(it.range.start, it.range.endExclusive) to it.kind }
|
spans.map { text.substring(it.range.start, it.range.endExclusive) to it.kind }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun returnAndExits() {
|
||||||
|
val text = "return 42; break@outer null"
|
||||||
|
val spans = SimpleLyngHighlighter().highlight(text)
|
||||||
|
val labeled = spansToLabeled(text, spans)
|
||||||
|
assertTrue(labeled.any { it.first == "return" && it.second == HighlightKind.Keyword })
|
||||||
|
assertTrue(labeled.any { it.first == "break" && it.second == HighlightKind.Keyword })
|
||||||
|
assertTrue(labeled.any { it.first == "@outer" && it.second == HighlightKind.Label })
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun keywordsAndIdentifiers() {
|
fun keywordsAndIdentifiers() {
|
||||||
val text = "a and b or not c"
|
val text = "a and b or not c"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package net.sergeych.lyng.miniast
|
|||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertFalse
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertNotNull
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
class CompletionEngineLightTest {
|
class CompletionEngineLightTest {
|
||||||
@@ -89,9 +90,9 @@ class CompletionEngineLightTest {
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
|
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
|
||||||
val reItem = items.firstOrNull { it.name == "re" }
|
val reItem = items.firstOrNull { it.name == "re" }
|
||||||
assertTrue(reItem != null, "Expected to find 're' in String members, got: ${items.map { it.name }}")
|
assertNotNull(reItem, "Expected to find 're' in String members, got: ${items.map { it.name }}")
|
||||||
// Type text should contain ": Regex"
|
// Type text should contain ": Regex"
|
||||||
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected type text to contain 'Regex', was: ${reItem.typeText}")
|
assertTrue(reItem.typeText?.contains("Regex") == true, "Expected type text to contain 'Regex', was: ${reItem.typeText}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -106,8 +107,8 @@ class CompletionEngineLightTest {
|
|||||||
val names = items.map { it.name }
|
val names = items.map { it.name }
|
||||||
assertTrue(names.isNotEmpty(), "Expected String members for parenthesized literal, got empty list")
|
assertTrue(names.isNotEmpty(), "Expected String members for parenthesized literal, got empty list")
|
||||||
val reItem = items.firstOrNull { it.name == "re" }
|
val reItem = items.firstOrNull { it.name == "re" }
|
||||||
assertTrue(reItem != null, "Expected to find 're' for parenthesized String literal, got: $names")
|
assertNotNull(reItem, "Expected to find 're' for parenthesized String literal, got: $names")
|
||||||
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected ': Regex' for re(), was: ${reItem.typeText}")
|
assertTrue(reItem.typeText?.contains("Regex") == true, "Expected ': Regex' for re(), was: ${reItem.typeText}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -120,8 +121,8 @@ class CompletionEngineLightTest {
|
|||||||
val names = items.map { it.name }
|
val names = items.map { it.name }
|
||||||
assertTrue(names.isNotEmpty(), "Expected String members without explicit imports, got empty list")
|
assertTrue(names.isNotEmpty(), "Expected String members without explicit imports, got empty list")
|
||||||
val reItem = items.firstOrNull { it.name == "re" }
|
val reItem = items.firstOrNull { it.name == "re" }
|
||||||
assertTrue(reItem != null, "Expected to find 're' without explicit imports, got: $names")
|
assertNotNull(reItem, "Expected to find 're' without explicit imports, got: $names")
|
||||||
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected ': Regex' for re() without imports, was: ${reItem.typeText}")
|
assertTrue(reItem.typeText?.contains("Regex") == true, "Expected ': Regex' for re() without imports, was: ${reItem.typeText}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package lyng.stdlib
|
package lyng.stdlib
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Wrap a builder into a zero-argument thunk that computes once and caches the result.
|
Wrap a builder into a zero-argument thunk that computes once and caches the result.
|
||||||
The first call invokes builder() and stores the value; subsequent calls return the cached value.
|
The first call invokes builder() and stores the value; subsequent calls return the cached value.
|
||||||
*/
|
*/
|
||||||
fun cached(builder) {
|
fun cached(builder) {
|
||||||
var calculated = false
|
var calculated = false
|
||||||
@@ -16,7 +16,7 @@ fun cached(builder) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Filter elements of this iterable using the provided predicate and provide a flow
|
/* Filter elements of this iterable using the provided predicate and provide a flow
|
||||||
of results. Coudl be used to map infinte flows, etc.
|
of results. Coudl be used to map infinte flows, etc.
|
||||||
*/
|
*/
|
||||||
fun Iterable.filterFlow(predicate): Flow {
|
fun Iterable.filterFlow(predicate): Flow {
|
||||||
val list = this
|
val list = this
|
||||||
@@ -30,7 +30,7 @@ fun Iterable.filterFlow(predicate): Flow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Filter this iterable and return List of elements
|
Filter this iterable and return List of elements
|
||||||
*/
|
*/
|
||||||
fun Iterable.filter(predicate) {
|
fun Iterable.filter(predicate) {
|
||||||
val result = []
|
val result = []
|
||||||
@@ -39,7 +39,7 @@ fun Iterable.filter(predicate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Count all items in this iterable for which predicate return true
|
Count all items in this iterable for which predicate return true
|
||||||
*/
|
*/
|
||||||
fun Iterable.count(predicate): Int {
|
fun Iterable.count(predicate): Int {
|
||||||
var hits = 0
|
var hits = 0
|
||||||
@@ -49,8 +49,8 @@ fun Iterable.count(predicate): Int {
|
|||||||
hits
|
hits
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
filter out all null elements from this collection (Iterable); flow of
|
filter out all null elements from this collection (Iterable); flow of
|
||||||
non-null elements is returned
|
non-null elements is returned
|
||||||
*/
|
*/
|
||||||
fun Iterable.filterFlowNotNull(): Flow {
|
fun Iterable.filterFlowNotNull(): Flow {
|
||||||
filterFlow { it != null }
|
filterFlow { it != null }
|
||||||
@@ -72,12 +72,12 @@ fun Iterable.drop(n) {
|
|||||||
val Iterable.first get() {
|
val Iterable.first get() {
|
||||||
val i = iterator()
|
val i = iterator()
|
||||||
if( !i.hasNext() ) throw NoSuchElementException()
|
if( !i.hasNext() ) throw NoSuchElementException()
|
||||||
i.next().also { i.cancelIteration() }
|
i.next().also { i.cancelIteration() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Return the first element that matches the predicate or throws
|
Return the first element that matches the predicate or throws
|
||||||
NuSuchElementException
|
NuSuchElementException
|
||||||
*/
|
*/
|
||||||
fun Iterable.findFirst(predicate) {
|
fun Iterable.findFirst(predicate) {
|
||||||
for( x in this ) {
|
for( x in this ) {
|
||||||
@@ -88,7 +88,7 @@ fun Iterable.findFirst(predicate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
return the first element matching the predicate or null
|
return the first element matching the predicate or null
|
||||||
*/
|
*/
|
||||||
fun Iterable.findFirstOrNull(predicate) {
|
fun Iterable.findFirstOrNull(predicate) {
|
||||||
for( x in this ) {
|
for( x in this ) {
|
||||||
@@ -108,7 +108,7 @@ val Iterable.last get() {
|
|||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
if( !found ) throw NoSuchElementException()
|
if( !found ) throw NoSuchElementException()
|
||||||
element
|
element
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Emit all but the last N elements of this iterable. */
|
/* Emit all but the last N elements of this iterable. */
|
||||||
@@ -215,8 +215,8 @@ fun Iterable.shuffled() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Returns a single list of all elements from all collections in the given collection.
|
Returns a single list of all elements from all collections in the given collection.
|
||||||
@return List
|
@return List
|
||||||
*/
|
*/
|
||||||
fun Iterable.flatten() {
|
fun Iterable.flatten() {
|
||||||
val result = []
|
val result = []
|
||||||
@@ -227,8 +227,8 @@ fun Iterable.flatten() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Returns a single list of all elements yielded from results of transform function being
|
Returns a single list of all elements yielded from results of transform function being
|
||||||
invoked on each element of original collection.
|
invoked on each element of original collection.
|
||||||
*/
|
*/
|
||||||
fun Iterable.flatMap(transform): List {
|
fun Iterable.flatMap(transform): List {
|
||||||
map(transform).flatten()
|
map(transform).flatten()
|
||||||
@@ -279,8 +279,8 @@ val String.re get() = Regex(this)
|
|||||||
fun TODO(message=null) = throw NotImplementedException(message ?: "not implemented")
|
fun TODO(message=null) = throw NotImplementedException(message ?: "not implemented")
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Provides different access types for delegates.
|
Provides different access types for delegates.
|
||||||
Used in the 'bind' hook to validate delegate usage.
|
Used in the 'bind' hook to validate delegate usage.
|
||||||
*/
|
*/
|
||||||
enum DelegateAccess {
|
enum DelegateAccess {
|
||||||
Val,
|
Val,
|
||||||
@@ -289,9 +289,9 @@ enum DelegateAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Base interface for all delegates.
|
Base interface for all delegates.
|
||||||
Implementing this interface is optional as Lyng uses dynamic dispatch,
|
Implementing this interface is optional as Lyng uses dynamic dispatch,
|
||||||
but it is recommended for documentation and clarity.
|
but it is recommended for documentation and clarity.
|
||||||
*/
|
*/
|
||||||
interface Delegate {
|
interface Delegate {
|
||||||
/* Called when a delegated 'val' or 'var' is read. */
|
/* Called when a delegated 'val' or 'var' is read. */
|
||||||
@@ -304,16 +304,26 @@ interface Delegate {
|
|||||||
fun invoke(thisRef, name, args...) = TODO("delegate invoke is not implemented")
|
fun invoke(thisRef, name, args...) = TODO("delegate invoke is not implemented")
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Called once during initialization to configure or validate the delegate.
|
Called once during initialization to configure or validate the delegate.
|
||||||
Should return the delegate object to be used (usually 'this').
|
Should return the delegate object to be used (usually 'this').
|
||||||
*/
|
*/
|
||||||
fun bind(name, access, thisRef) = this
|
fun bind(name, access, thisRef) = this
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Standard implementation of a lazy-initialized property delegate.
|
Executes the block with `this` set to self and
|
||||||
The provided creator lambda is called once on the first access to compute the value.
|
returns what the block returns.
|
||||||
Can only be used with 'val' properties.
|
*/
|
||||||
|
fun with(self, block) {
|
||||||
|
var result = Unset
|
||||||
|
self.apply { result = block() }
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Standard implementation of a lazy-initialized property delegate.
|
||||||
|
The provided creator lambda is called once on the first access to compute the value.
|
||||||
|
Can only be used with 'val' properties.
|
||||||
*/
|
*/
|
||||||
class lazy(creatorParam) : Delegate {
|
class lazy(creatorParam) : Delegate {
|
||||||
private val creator = creatorParam
|
private val creator = creatorParam
|
||||||
@@ -325,7 +335,8 @@ class lazy(creatorParam) : Delegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getValue(thisRef, name) {
|
override fun getValue(thisRef, name) {
|
||||||
if (value == Unset) value = creator()
|
if (value == Unset)
|
||||||
|
value = with(thisRef,creator)
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -530,7 +530,7 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
|
|||||||
fun isIdentPart(ch: Char) = ch == '_' || ch == '$' || ch == '~' || ch.isLetterOrDigit()
|
fun isIdentPart(ch: Char) = ch == '_' || ch == '$' || ch == '~' || ch.isLetterOrDigit()
|
||||||
// A conservative list of language keywords to avoid misclassifying as function calls
|
// A conservative list of language keywords to avoid misclassifying as function calls
|
||||||
val kw = setOf(
|
val kw = setOf(
|
||||||
"package", "import", "fun", "fn", "class", "interface", "enum", "val", "var",
|
"package", "import", "fun", "fn", "class", "interface", "enum", "object", "val", "var",
|
||||||
"if", "else", "while", "do", "for", "when", "try", "catch", "finally",
|
"if", "else", "while", "do", "for", "when", "try", "catch", "finally",
|
||||||
"throw", "return", "break", "continue", "in", "is", "as", "as?", "not",
|
"throw", "return", "break", "continue", "in", "is", "as", "as?", "not",
|
||||||
"true", "false", "null", "private", "protected", "abstract", "closed", "override", "open", "extern", "static",
|
"true", "false", "null", "private", "protected", "abstract", "closed", "override", "open", "extern", "static",
|
||||||
@@ -640,8 +640,12 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
|
|||||||
i = p
|
i = p
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (text.startsWith("class", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+5 >= n || !isIdentPart(text.getOrNull(i+5) ?: '\u0000'))) {
|
if ((text.startsWith("class", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+5 >= n || !isIdentPart(text.getOrNull(i+5) ?: '\u0000'))) ||
|
||||||
var p = skipWs(i + 5)
|
(text.startsWith("object", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+6 >= n || !isIdentPart(text.getOrNull(i+6) ?: '\u0000'))) ||
|
||||||
|
(text.startsWith("enum", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+4 >= n || !isIdentPart(text.getOrNull(i+4) ?: '\u0000'))) ||
|
||||||
|
(text.startsWith("interface", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+9 >= n || !isIdentPart(text.getOrNull(i+9) ?: '\u0000')))) {
|
||||||
|
val kwLen = if (text.startsWith("class", i)) 5 else if (text.startsWith("object", i)) 6 else if (text.startsWith("enum", i)) 4 else 9
|
||||||
|
var p = skipWs(i + kwLen)
|
||||||
val name = readIdent(p)
|
val name = readIdent(p)
|
||||||
if (name != null) result[p to name.second] = "hl-class"
|
if (name != null) result[p to name.second] = "hl-class"
|
||||||
i = p
|
i = p
|
||||||
|
|||||||
@@ -77,6 +77,14 @@ fun HomePage() {
|
|||||||
// Create, transform, and verify — the Lyng way
|
// Create, transform, and verify — the Lyng way
|
||||||
import lyng.stdlib
|
import lyng.stdlib
|
||||||
|
|
||||||
|
fun findFirstPositive(list) {
|
||||||
|
list.forEach {
|
||||||
|
if (it > 0) return@findFirstPositive it
|
||||||
|
}
|
||||||
|
null
|
||||||
|
}
|
||||||
|
assertEquals(42, findFirstPositive([-1, 42, -5]))
|
||||||
|
|
||||||
val data = 1..5 // or [1,2,3,4,5]
|
val data = 1..5 // or [1,2,3,4,5]
|
||||||
val evens2 = data.filter { it % 2 == 0 }.map { it * it }
|
val evens2 = data.filter { it % 2 == 0 }.map { it * it }
|
||||||
assertEquals([4, 16], evens2)
|
assertEquals([4, 16], evens2)
|
||||||
@@ -86,8 +94,12 @@ val base = { a: 1, b: 2 }
|
|||||||
val patch = { b: 3, c: }
|
val patch = { b: 3, c: }
|
||||||
val m = { "a": 0, ...base, ...patch, d: 4 }
|
val m = { "a": 0, ...base, ...patch, d: 4 }
|
||||||
assertEquals(1, m["a"]) // base overwrites 0
|
assertEquals(1, m["a"]) // base overwrites 0
|
||||||
assertEquals(3, m["b"]) // patch overwrites base
|
|
||||||
assertEquals(4, m["d"]) // literal key
|
// Object expressions: anonymous classes on the fly
|
||||||
|
val worker = object : Runnable {
|
||||||
|
override fun run() = println("Working...")
|
||||||
|
}
|
||||||
|
worker.run()
|
||||||
>>> void
|
>>> void
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
val mapHtml = "<pre><code>" + htmlEscape(code) + "</code></pre>"
|
val mapHtml = "<pre><code>" + htmlEscape(code) + "</code></pre>"
|
||||||
|
|||||||
@@ -329,7 +329,7 @@
|
|||||||
<!-- Top-left version ribbon -->
|
<!-- Top-left version ribbon -->
|
||||||
<div class="corner-ribbon bg-danger text-white">
|
<div class="corner-ribbon bg-danger text-white">
|
||||||
<span style="margin-left: -5em">
|
<span style="margin-left: -5em">
|
||||||
v1.1.0-rc
|
v1.1.1-SNAPSHOT
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<!-- Fixed top navbar for the whole site -->
|
<!-- Fixed top navbar for the whole site -->
|
||||||
|
|||||||
@@ -20,6 +20,22 @@ import kotlin.test.assertFalse
|
|||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
class LyngHighlightTest {
|
class LyngHighlightTest {
|
||||||
|
@Test
|
||||||
|
fun highlightsReturnAndLabels() {
|
||||||
|
val md = """
|
||||||
|
```lyng
|
||||||
|
return 42
|
||||||
|
break@outer null
|
||||||
|
return@fn val
|
||||||
|
```
|
||||||
|
""".trimIndent()
|
||||||
|
val html = renderMarkdown(md)
|
||||||
|
|
||||||
|
assertTrue(html.contains("hl-kw"), "Expected keyword class for 'return': $html")
|
||||||
|
assertTrue(html.contains("hl-lbl") || html.contains("hl-ann"), "Expected label/annotation class for @outer/@fn: $html")
|
||||||
|
assertTrue(html.contains(">>>").xor(true), "Should not contain prompt marker unless expected")
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun highlightsLyngFencedBlock() {
|
fun highlightsLyngFencedBlock() {
|
||||||
val md = """
|
val md = """
|
||||||
|
|||||||
Reference in new issue
Block a user