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
|
||||
/build.log
|
||||
/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
|
||||
|
||||
- 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
|
||||
- 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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -21,22 +29,9 @@ fun swapEnds(first, args..., last, f) {
|
||||
|
||||
- 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% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]
|
||||
|
||||
```
|
||||
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.
|
||||
- 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.
|
||||
- Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`.
|
||||
|
||||
## Resources:
|
||||
@@ -44,6 +39,8 @@ and it is multithreaded on platforms supporting it (automatically, no code chang
|
||||
- [Language home](https://lynglang.com)
|
||||
- [introduction and tutorial](docs/tutorial.md) - start here please
|
||||
- [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)
|
||||
- [Samples directory](docs/samples)
|
||||
- [Formatter (core + CLI + IDE)](docs/formatter.md)
|
||||
@@ -191,6 +188,16 @@ Ready features:
|
||||
- [x] regular exceptions + extended `when`
|
||||
- [x] multiple inheritance for user classes
|
||||
- [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
|
||||
|
||||
|
||||
@@ -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 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) {
|
||||
if (value == Unset) {
|
||||
value = creator()
|
||||
// calculate value using thisRef as this:
|
||||
value = with(thisRef) creator()
|
||||
}
|
||||
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 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.
|
||||
|
||||
@@ -23,7 +23,7 @@ dependencies {
|
||||
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
|
||||
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`.
|
||||
|
||||
Kotlin (host) bootstrap example (imports omitted for brevity):
|
||||
Kotlin (host) bootstrap example:
|
||||
|
||||
```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 installed: Boolean = createFs(PermitAllAccessPolicy, scope)
|
||||
// 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)
|
||||
- [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)
|
||||
- [time](time.md) and [parallelism](parallelism.md)
|
||||
- [parallelism] - multithreaded code, coroutines, etc.
|
||||
@@ -32,6 +32,15 @@ any block also returns it's last expression:
|
||||
}
|
||||
>>> 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`:
|
||||
|
||||
fn voidFunction() {
|
||||
@@ -179,14 +188,13 @@ Note that assignment operator returns rvalue, it can't be assigned.
|
||||
## Modifying arithmetics
|
||||
|
||||
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
|
||||
assert( 25 == (x*=5) )
|
||||
assert( 25 == x)
|
||||
assert( 24 == (x-=1) )
|
||||
assert( 12 == (x/=2) )
|
||||
x
|
||||
>>> 12
|
||||
var x = null
|
||||
x ?= 10
|
||||
assertEquals(10, x)
|
||||
x ?= 20
|
||||
assertEquals(10, x)
|
||||
|
||||
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"
|
||||
>>> "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
|
||||
|
||||
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))
|
||||
>>> 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
|
||||
|
||||
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] |
|
||||
| delay, launch, yield | see [parallelism] |
|
||||
| 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)
|
||||
: `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
|
||||
|
||||
[String]: development/String.md
|
||||
[String]: ../archived/development/String.md
|
||||
|
||||
[string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
|
||||
|
||||
|
||||
@@ -105,6 +105,22 @@ object Config {
|
||||
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
|
||||
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.
|
||||
|
||||
### 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
|
||||
|
||||
### 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_]+)?" }
|
||||
]
|
||||
},
|
||||
"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": {
|
||||
"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]*" } ] },
|
||||
"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" } } } ] },
|
||||
"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" } ] },
|
||||
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this)\\b|π)" } ] },
|
||||
"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|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(?:@[\\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*\\()" } ] },
|
||||
"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": ":" } ] }
|
||||
|
||||
+44
-13
@@ -94,6 +94,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
|
||||
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) {
|
||||
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() }
|
||||
|
||||
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
|
||||
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
|
||||
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
|
||||
}
|
||||
|
||||
// Add annotation coloring using token highlighter (treat @Label as annotation)
|
||||
// Add annotation/label coloring using token highlighter
|
||||
run {
|
||||
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
||||
for (s in tokens) if (s.kind == HighlightKind.Label) {
|
||||
@@ -261,8 +261,29 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
val end = s.range.endExclusive
|
||||
if (start in 0..end && end <= text.length && start < end) {
|
||||
val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null }
|
||||
if (lexeme != null && lexeme.startsWith("@")) {
|
||||
putRange(start, end, LyngHighlighterColors.ANNOTATION)
|
||||
if (lexeme != null) {
|
||||
// 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 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.
|
||||
* Strategy:
|
||||
|
||||
@@ -43,10 +43,15 @@ class LyngColorSettingsPage : ColorSettingsPage {
|
||||
}
|
||||
|
||||
var counter = 0
|
||||
counter = counter + 1
|
||||
outer@ while (counter < 10) {
|
||||
if (counter == 5) return@outer
|
||||
counter = counter + 1
|
||||
}
|
||||
""".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(
|
||||
AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD),
|
||||
@@ -58,6 +63,7 @@ class LyngColorSettingsPage : ColorSettingsPage {
|
||||
AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT),
|
||||
// Semantic
|
||||
AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION),
|
||||
AttributesDescriptor("Label (semantic)", LyngHighlighterColors.LABEL),
|
||||
AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE),
|
||||
AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE),
|
||||
AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION),
|
||||
|
||||
@@ -82,4 +82,9 @@ object LyngHighlighterColors {
|
||||
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
|
||||
"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(
|
||||
"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",
|
||||
"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) {
|
||||
@@ -133,10 +133,24 @@ class LyngLexer : LexerBase() {
|
||||
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()) {
|
||||
i++
|
||||
while (i < endOffset && buffer[i].isIdentifierPart()) i++
|
||||
if (i < endOffset && buffer[i] == '@') {
|
||||
i++
|
||||
myTokenEnd = i
|
||||
myTokenType = LyngTokenTypes.LABEL
|
||||
return
|
||||
}
|
||||
myTokenEnd = i
|
||||
val text = buffer.subSequence(myTokenStart, myTokenEnd).toString()
|
||||
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.isIdentifierStart(): Boolean = this == '_' || this.isLetter()
|
||||
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.PUNCT -> pack(LyngHighlighterColors.PUNCT)
|
||||
LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER)
|
||||
LyngTokenTypes.LABEL -> pack(LyngHighlighterColors.LABEL)
|
||||
else -> emptyArray()
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ object LyngTokenTypes {
|
||||
val NUMBER = LyngTokenType("NUMBER")
|
||||
val KEYWORD = LyngTokenType("KEYWORD")
|
||||
val IDENTIFIER = LyngTokenType("IDENTIFIER")
|
||||
val LABEL = LyngTokenType("LABEL")
|
||||
val PUNCT = LyngTokenType("PUNCT")
|
||||
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");
|
||||
* 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.ExperimentalWasmDsl
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
plugins {
|
||||
@@ -53,11 +52,11 @@ kotlin {
|
||||
browser()
|
||||
nodejs()
|
||||
}
|
||||
@OptIn(ExperimentalWasmDsl::class)
|
||||
wasmJs() {
|
||||
browser()
|
||||
nodejs()
|
||||
}
|
||||
// @OptIn(ExperimentalWasmDsl::class)
|
||||
// wasmJs() {
|
||||
// browser()
|
||||
// nodejs()
|
||||
// }
|
||||
|
||||
// Keep expect/actual warning suppressed consistently with other modules
|
||||
targets.configureEach {
|
||||
@@ -94,13 +93,13 @@ kotlin {
|
||||
implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}")
|
||||
}
|
||||
}
|
||||
// For Wasm we use in-memory VFS for now
|
||||
val wasmJsMain by getting {
|
||||
dependencies {
|
||||
api(libs.okio)
|
||||
implementation(libs.okio.fakefilesystem)
|
||||
}
|
||||
}
|
||||
// // For Wasm we use in-memory VFS for now
|
||||
// val wasmJsMain by getting {
|
||||
// dependencies {
|
||||
// api(libs.okio)
|
||||
// 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
|
||||
* 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
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -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) {
|
||||
val s = stripCommentLexeme(t.value)
|
||||
if (pendingDocStart == null) pendingDocStart = t.pos
|
||||
@@ -170,7 +175,6 @@ class Compiler(
|
||||
// A standalone newline not immediately following a comment resets doc buffer
|
||||
if (!prevWasComment) clearPendingDoc() else prevWasComment = false
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
cc.next()
|
||||
@@ -248,7 +252,7 @@ class Compiler(
|
||||
when (t.type) {
|
||||
Token.Type.RBRACE, Token.Type.EOF, Token.Type.SEMICOLON -> {}
|
||||
else ->
|
||||
throw ScriptError(t.pos, "unexpeced `${t.value}` here")
|
||||
throw ScriptError(t.pos, "unexpected `${t.value}` here")
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -280,9 +284,11 @@ class Compiler(
|
||||
}
|
||||
|
||||
private var lastAnnotation: (suspend (Scope, ObjString, Statement) -> Statement)? = null
|
||||
private var lastLabel: String? = null
|
||||
|
||||
private suspend fun parseStatement(braceMeansLambda: Boolean = false): Statement? {
|
||||
lastAnnotation = null
|
||||
lastLabel = null
|
||||
while (true) {
|
||||
val t = cc.next()
|
||||
return when (t.type) {
|
||||
@@ -300,6 +306,10 @@ class Compiler(
|
||||
}
|
||||
|
||||
Token.Type.ATLABEL -> {
|
||||
val label = t.value
|
||||
if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
lastLabel = label
|
||||
}
|
||||
lastAnnotation = parseAnnotation(t)
|
||||
continue
|
||||
}
|
||||
@@ -648,6 +658,7 @@ class Compiler(
|
||||
private suspend fun parseLambdaExpression(): ObjRef {
|
||||
// lambda args are different:
|
||||
val startPos = cc.currentPos()
|
||||
val label = lastLabel
|
||||
val argsDeclaration = parseArgsDeclaration()
|
||||
if (argsDeclaration != null && argsDeclaration.endTokenType != Token.Type.ARROW)
|
||||
throw ScriptError(
|
||||
@@ -655,7 +666,9 @@ class Compiler(
|
||||
"lambda must have either valid arguments declaration with '->' or no arguments"
|
||||
)
|
||||
|
||||
label?.let { cc.labels.add(it) }
|
||||
val body = parseBlock(skipLeadingBrace = true)
|
||||
label?.let { cc.labels.remove(it) }
|
||||
|
||||
return ValueFnRef { closureScope ->
|
||||
statement {
|
||||
@@ -679,7 +692,12 @@ class Compiler(
|
||||
// assign vars as declared the standard way
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1248,6 +1266,8 @@ class Compiler(
|
||||
}
|
||||
}
|
||||
|
||||
Token.Type.OBJECT -> StatementRef(parseObjectDeclaration())
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -1418,6 +1438,7 @@ class Compiler(
|
||||
"while" -> parseWhileStatement()
|
||||
"do" -> parseDoWhileStatement()
|
||||
"for" -> parseForStatement()
|
||||
"return" -> parseReturnStatement(id.pos)
|
||||
"break" -> parseBreakStatement(id.pos)
|
||||
"continue" -> parseContinueStatement(id.pos)
|
||||
"if" -> parseIfStatement()
|
||||
@@ -1773,6 +1794,10 @@ class Compiler(
|
||||
try {
|
||||
// body is a parsed block, it already has separate context
|
||||
result = body.execute(this)
|
||||
} catch (e: ReturnException) {
|
||||
throw e
|
||||
} catch (e: LoopBreakContinueException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
// convert to appropriate exception
|
||||
val objException = when (e) {
|
||||
@@ -1860,8 +1885,12 @@ class Compiler(
|
||||
}
|
||||
|
||||
private suspend fun parseObjectDeclaration(): Statement {
|
||||
val nameToken = cc.requireToken(Token.Type.ID)
|
||||
val startPos = pendingDeclStart ?: nameToken.pos
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
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()
|
||||
pendingDeclDoc = null
|
||||
pendingDeclStart = null
|
||||
@@ -1887,11 +1916,11 @@ class Compiler(
|
||||
|
||||
// Robust body detection
|
||||
var classBodyRange: MiniRange? = null
|
||||
val bodyInit: Statement? = run {
|
||||
val bodyInit: Statement? = inCodeContext(CodeContext.ClassBody(className)) {
|
||||
val saved = cc.savePos()
|
||||
val next = cc.nextNonWhitespace()
|
||||
if (next.type == Token.Type.LBRACE) {
|
||||
val bodyStart = next.pos
|
||||
val nextBody = cc.nextNonWhitespace()
|
||||
if (nextBody.type == Token.Type.LBRACE) {
|
||||
val bodyStart = nextBody.pos
|
||||
val st = withLocalNames(emptySet()) {
|
||||
parseScript()
|
||||
}
|
||||
@@ -1906,15 +1935,15 @@ class Compiler(
|
||||
}
|
||||
|
||||
val initScope = popInitScope()
|
||||
val className = nameToken.value
|
||||
|
||||
return statement(startPos) { context ->
|
||||
val parentClasses = baseSpecs.map { baseSpec ->
|
||||
val rec = context[baseSpec.name] ?: throw ScriptError(nameToken.pos, "unknown base class: ${baseSpec.name}")
|
||||
(rec.value as? ObjClass) ?: throw ScriptError(nameToken.pos, "${baseSpec.name} is not a class")
|
||||
val rec = context[baseSpec.name] ?: throw ScriptError(startPos, "unknown base class: ${baseSpec.name}")
|
||||
(rec.value as? ObjClass) ?: throw ScriptError(startPos, "${baseSpec.name} is not a class")
|
||||
}
|
||||
|
||||
val newClass = ObjInstanceClass(className, *parentClasses.toTypedArray())
|
||||
newClass.isAnonymous = nameToken == null
|
||||
for (i in parentClasses.indices) {
|
||||
val argsList = baseSpecs[i].args
|
||||
// In object, we evaluate parent args once at creation time
|
||||
@@ -1924,12 +1953,14 @@ class Compiler(
|
||||
val classScope = context.createChildScope(newThisObj = newClass)
|
||||
classScope.currentClassCtx = newClass
|
||||
newClass.classScope = classScope
|
||||
classScope.addConst("object", newClass)
|
||||
|
||||
bodyInit?.execute(classScope)
|
||||
|
||||
// Create instance (singleton)
|
||||
val instance = newClass.callOn(context.createChildScope(Arguments.EMPTY))
|
||||
context.addItem(className, false, instance)
|
||||
if (nameToken != null)
|
||||
context.addItem(className, false, 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 {
|
||||
val t = cc.next()
|
||||
if (t.type != Token.Type.RPAREN)
|
||||
@@ -2588,6 +2651,7 @@ class Compiler(
|
||||
|
||||
// Capture doc locally to reuse even if we need to emit later
|
||||
val declDocLocal = pendingDeclDoc
|
||||
val outerLabel = lastLabel
|
||||
|
||||
// Emit MiniFunDecl before body parsing (body range unknown yet)
|
||||
run {
|
||||
@@ -2614,6 +2678,8 @@ class Compiler(
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
@@ -2629,7 +2695,9 @@ class Compiler(
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.ASSIGN) {
|
||||
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
|
||||
statement(expr.pos) { scope ->
|
||||
expr.execute(scope)
|
||||
@@ -2661,8 +2729,15 @@ class Compiler(
|
||||
if (extTypeName != null) {
|
||||
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
|
||||
val fnCreateStatement = statement(start) { context ->
|
||||
if (isDelegated) {
|
||||
@@ -2675,7 +2750,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
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")
|
||||
context.addExtension(type, name, ObjRecord(ObjUnset, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply {
|
||||
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.instanceInitializers += statement(start) { scp ->
|
||||
val accessType2 = scp.resolveQualifiedIdentifier("DelegateAccess.Callable")
|
||||
val initValue2 = delegateExpression!!.execute(scp)
|
||||
val initValue2 = delegateExpression.execute(scp)
|
||||
val finalDelegate2 = try {
|
||||
initValue2.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType2, scp.thisObj))
|
||||
} catch (e: Exception) {
|
||||
@@ -3053,7 +3128,7 @@ class Compiler(
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected getter expression")
|
||||
(expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
||||
expr
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after get()")
|
||||
}
|
||||
@@ -3074,7 +3149,7 @@ class Compiler(
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
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 ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
@@ -3109,7 +3184,7 @@ class Compiler(
|
||||
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 ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
@@ -3312,7 +3387,7 @@ class Compiler(
|
||||
prop
|
||||
}
|
||||
} else {
|
||||
val isLateInitVal = !isMutable && initialExpression == null && getter == null && setter == null
|
||||
val isLateInitVal = !isMutable && initialExpression == null
|
||||
if (declaringClassName != null && !isStatic) {
|
||||
val storageName = "$declaringClassName::$name"
|
||||
// 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 ->
|
||||
AssignOpRef(BinOp.PERCENT, a, b, pos)
|
||||
},
|
||||
Operator(Token.Type.IFNULLASSIGN, lastPriority) { pos, a, b ->
|
||||
AssignIfNullRef(a, b, pos)
|
||||
},
|
||||
// logical 1
|
||||
Operator(Token.Type.OR, ++lastPriority) { _, a, b ->
|
||||
foldBinary(BinOp.OR, a, b)?.let { return@Operator ConstRef(it.asReadonly) }
|
||||
|
||||
@@ -325,6 +325,7 @@ private class Parser(fromPos: Pos) {
|
||||
|
||||
'?' -> {
|
||||
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.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 {
|
||||
var s: Scope? = this
|
||||
do {
|
||||
val t = s!!.thisObj
|
||||
while (s != null) {
|
||||
val t = s.thisObj
|
||||
if (t is T) return t
|
||||
s = s.parent
|
||||
} while (s != null)
|
||||
}
|
||||
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 {
|
||||
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
|
||||
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
|
||||
object : Statement() {
|
||||
override val pos: Pos = Pos.builtIn
|
||||
@@ -635,14 +642,22 @@ open class Scope(
|
||||
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
|
||||
}
|
||||
}
|
||||
})!!
|
||||
})
|
||||
rec.value = res
|
||||
return res
|
||||
}
|
||||
return rec.value
|
||||
}
|
||||
|
||||
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
|
||||
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
|
||||
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
|
||||
return
|
||||
|
||||
@@ -40,7 +40,7 @@ class Source(val fileName: String, val text: String) {
|
||||
|
||||
fun extractPackageName(): String {
|
||||
for ((n,line) in lines.withIndex()) {
|
||||
if( line.isBlank() || line.isEmpty() )
|
||||
if( line.isBlank() )
|
||||
continue
|
||||
if( line.startsWith("package ") )
|
||||
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,
|
||||
SEMICOLON, COLON,
|
||||
PLUS, MINUS, STAR, SLASH, PERCENT,
|
||||
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN,
|
||||
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN,
|
||||
PLUS2, MINUS2,
|
||||
IN, NOTIN, IS, NOTIS, BY,
|
||||
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 {
|
||||
var s = code
|
||||
// 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)
|
||||
s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
|
||||
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 {")
|
||||
// Ensure space before '(' for catch parameter
|
||||
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)"
|
||||
s = s.replace(Regex("\\(\\s+"), "(")
|
||||
// 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
|
||||
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.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,
|
||||
|
||||
@@ -96,17 +96,15 @@ object CompletionEngineLight {
|
||||
}
|
||||
|
||||
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
|
||||
mini?.let { m ->
|
||||
val decls = m.declarations
|
||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||
funs.forEach { offerDeclAdd(out, prefix, it) }
|
||||
classes.forEach { offerDeclAdd(out, prefix, it) }
|
||||
enums.forEach { offerDeclAdd(out, prefix, it) }
|
||||
vals.forEach { offerDeclAdd(out, prefix, it) }
|
||||
}
|
||||
val decls = mini.declarations
|
||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||
funs.forEach { offerDeclAdd(out, prefix, it) }
|
||||
classes.forEach { offerDeclAdd(out, prefix, it) }
|
||||
enums.forEach { offerDeclAdd(out, prefix, it) }
|
||||
vals.forEach { offerDeclAdd(out, prefix, it) }
|
||||
|
||||
// Imported and builtin
|
||||
val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
|
||||
@@ -192,7 +190,7 @@ object CompletionEngineLight {
|
||||
val chosen = variants.asSequence()
|
||||
.filterIsInstance<MiniMemberValDecl>()
|
||||
.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
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
|
||||
@@ -137,11 +137,20 @@ open class Obj {
|
||||
// methods that to override
|
||||
|
||||
open suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if( other === this) return 0
|
||||
if( other === ObjNull ) return 2
|
||||
if (other === this) return 0
|
||||
if (other === ObjNull || other === ObjUnset || other === ObjVoid) return 2
|
||||
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 {
|
||||
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) {
|
||||
val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
|
||||
return obj.copy(
|
||||
|
||||
@@ -30,6 +30,7 @@ private object ClassIdGen { var c: Long = 1L; fun nextId(): Long = c++ }
|
||||
|
||||
val ObjClassType by lazy {
|
||||
ObjClass("Class").apply {
|
||||
addProperty("className", getter = { thisAs<ObjClass>().classNameObj })
|
||||
addFnDoc(
|
||||
name = "name",
|
||||
doc = "Simple name of this class (without package).",
|
||||
@@ -91,6 +92,8 @@ open class ObjClass(
|
||||
vararg parents: ObjClass,
|
||||
) : Obj() {
|
||||
|
||||
var isAnonymous: Boolean = false
|
||||
|
||||
var isAbstract: Boolean = false
|
||||
|
||||
// Stable identity and simple structural version for PICs
|
||||
@@ -181,11 +184,8 @@ open class ObjClass(
|
||||
val base = c3Linearize(this, mutableMapOf())
|
||||
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
|
||||
else {
|
||||
// During very early bootstrap rootObjectType might not be initialized yet.
|
||||
// We use a safe check here.
|
||||
@Suppress("UNNECESSARY_SAFE_CALL")
|
||||
val root = net.sergeych.lyng.obj.Obj.rootObjectType
|
||||
if (root != null) base + root else base
|
||||
val root = Obj.rootObjectType
|
||||
base + root
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +235,7 @@ open class ObjClass(
|
||||
// 1) members-defined methods
|
||||
for ((k, v) in members) {
|
||||
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
|
||||
@@ -243,7 +243,7 @@ open class ObjClass(
|
||||
if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) {
|
||||
// if not already present, copy reference for dispatch
|
||||
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 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 ->
|
||||
val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this
|
||||
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()
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -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)")
|
||||
return obj.copy(
|
||||
value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))),
|
||||
type = ObjRecord.Type.Other
|
||||
)
|
||||
val res = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
|
||||
obj.value = res
|
||||
return obj
|
||||
}
|
||||
return super.resolveRecord(scope, obj, name, decl)
|
||||
}
|
||||
@@ -197,18 +196,22 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
if (rec.type == ObjRecord.Type.Delegated) {
|
||||
val storageName = "${cls.className}::$name"
|
||||
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()
|
||||
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 caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(
|
||||
ObjIllegalAccessException(
|
||||
scope,
|
||||
"can't invoke method $name (declared in ${decl.className ?: "?"})"
|
||||
"can't invoke method $name (declared in ${decl.className})"
|
||||
)
|
||||
)
|
||||
return rec.value.invoke(
|
||||
@@ -217,6 +220,9 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
args,
|
||||
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 }
|
||||
encoder.encodeAnyList(scope, params)
|
||||
val vars = serializingVars.values.map { it.value }
|
||||
if (vars.isNotEmpty<Obj>()) {
|
||||
if (vars.isNotEmpty()) {
|
||||
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) {
|
||||
if (isIntRange) {
|
||||
val s = (start as ObjInt).value
|
||||
val e = (end as ObjInt).value
|
||||
if (start is ObjInt && end is ObjInt) {
|
||||
val s = start.value
|
||||
val e = end.value
|
||||
if (isEndInclusive) {
|
||||
for (i in s..e) {
|
||||
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
|
||||
}
|
||||
}
|
||||
} else if (isCharRange) {
|
||||
val s = (start as ObjChar).value
|
||||
val e = (end as ObjChar).value
|
||||
} else if (start is ObjChar && end is ObjChar) {
|
||||
val s = start.value
|
||||
val e = end.value
|
||||
if (isEndInclusive) {
|
||||
for (c in s..e) {
|
||||
if (!callback(ObjChar(c))) break
|
||||
|
||||
@@ -29,14 +29,19 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
|
||||
override val objClass: ObjClass = type
|
||||
|
||||
fun Scope.init() {
|
||||
if (self.start == null || self.end == null)
|
||||
raiseError("next is only available for finite ranges")
|
||||
isCharRange = self.isCharRange
|
||||
lastIndex = if (self.isIntRange || self.isCharRange) {
|
||||
if (self.isEndInclusive)
|
||||
self.end.toInt() - self.start.toInt() + 1
|
||||
val s = self.start
|
||||
val e = self.end
|
||||
if (s is ObjInt && e is ObjInt) {
|
||||
lastIndex = if (self.isEndInclusive)
|
||||
(e.value - s.value + 1).toInt()
|
||||
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 {
|
||||
raiseError("not implemented iterator for range of $this")
|
||||
}
|
||||
@@ -46,10 +51,13 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
|
||||
|
||||
fun next(scope: Scope): Obj =
|
||||
if (nextIndex < lastIndex) {
|
||||
val x = if (self.isEndInclusive)
|
||||
self.start!!.toLong() + nextIndex++
|
||||
val start = self.start
|
||||
val x = if (start is ObjInt)
|
||||
start.value + nextIndex++
|
||||
else if (start is ObjChar)
|
||||
start.value.code.toLong() + nextIndex++
|
||||
else
|
||||
self.start!!.toLong() + nextIndex++
|
||||
scope.raiseError("iterator error: unsupported range start")
|
||||
if( isCharRange ) ObjChar(x.toInt().toChar()) else ObjInt(x)
|
||||
}
|
||||
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.AND -> a.logicalAnd(scope, b)
|
||||
BinOp.EQARROW -> ObjMapEntry(a, b)
|
||||
BinOp.EQ -> ObjBool(a.compareTo(scope, b) == 0)
|
||||
BinOp.NEQ -> ObjBool(a.compareTo(scope, b) != 0)
|
||||
BinOp.EQ -> ObjBool(a.equals(scope, b))
|
||||
BinOp.NEQ -> ObjBool(!a.equals(scope, b))
|
||||
BinOp.REF_EQ -> ObjBool(a === b)
|
||||
BinOp.REF_NEQ -> ObjBool(a !== 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. */
|
||||
class QualifiedThisRef(private val typeName: String, private val atPos: Pos) : ObjRef {
|
||||
override suspend fun get(scope: Scope): ObjRecord {
|
||||
val thisObj = scope.thisObj
|
||||
val t = scope[typeName]?.value as? ObjClass
|
||||
?: scope.raiseError("unknown type $typeName")
|
||||
val inst = (thisObj as? ObjInstance)
|
||||
?: scope.raiseClassCastError("this is not an instance")
|
||||
if (!inst.objClass.allParentsSet.contains(t) && inst.objClass !== t)
|
||||
scope.raiseClassCastError(
|
||||
"Qualifier ${'$'}{t.className} is not an ancestor of ${'$'}{inst.objClass.className} (order: ${'$'}{inst.objClass.renderLinearization(true)})"
|
||||
)
|
||||
return ObjQualifiedView(inst, t).asReadonly
|
||||
|
||||
var s: Scope? = scope
|
||||
while (s != null) {
|
||||
val inst = s.thisObj as? ObjInstance
|
||||
if (inst != null) {
|
||||
if (inst.objClass === t || inst.objClass.allParentsSet.contains(t)) {
|
||||
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
|
||||
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
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun size4ReadsEnabled(): Boolean =
|
||||
PerfFlags.FIELD_PIC_SIZE_4 ||
|
||||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && rPromotedTo4)
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun size4WritesEnabled(): Boolean =
|
||||
PerfFlags.FIELD_PIC_SIZE_4 ||
|
||||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && wPromotedTo4)
|
||||
@@ -611,7 +620,7 @@ class FieldRef(
|
||||
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
|
||||
val fieldPic = PerfFlags.FIELD_PIC
|
||||
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
|
||||
val base = target.get(scope).value
|
||||
val base = target.evalValue(scope)
|
||||
if (base == ObjNull && isOptional) {
|
||||
// no-op on null receiver for optional chaining assignment
|
||||
return
|
||||
@@ -714,10 +723,9 @@ class FieldRef(
|
||||
|
||||
override suspend fun evalValue(scope: Scope): Obj {
|
||||
// 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 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 (fieldPic) {
|
||||
val (key, ver) = receiverKeyAndVersion(base)
|
||||
@@ -1058,6 +1066,7 @@ class MethodCallRef(
|
||||
private var mFreezeWindowsLeft: Int = 0
|
||||
private var mWindowAccesses: Int = 0
|
||||
private var mWindowMisses: Int = 0
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun size4MethodsEnabled(): Boolean =
|
||||
PerfFlags.METHOD_PIC_SIZE_4 ||
|
||||
((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) {
|
||||
val rec = actualOwner.getSlotRecord(slot)
|
||||
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
|
||||
@@ -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 */
|
||||
class AssignRef(
|
||||
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,
|
||||
// which matches the language/tests expectation for `$~[i]`.
|
||||
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.
|
||||
*/
|
||||
suspend fun encodeAny(scope: Scope, obj: Obj) {
|
||||
if (obj.objClass.isAnonymous) scope.raiseError("Cannot serialize anonymous object")
|
||||
encodeCached(obj) {
|
||||
val type = putTypeRecord(scope, obj, obj.lynonType())
|
||||
obj.serialize(scope, this, type)
|
||||
|
||||
@@ -47,7 +47,7 @@ class BindingHighlightTest {
|
||||
val mini = sink.build()
|
||||
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)
|
||||
val sym = binding.symbols.firstOrNull { it.name == "counter" }
|
||||
@@ -78,7 +78,7 @@ class BindingHighlightTest {
|
||||
val mini = sink.build()
|
||||
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" }
|
||||
assertNotNull(sym, "Top-level val 'answer' must be registered as a symbol")
|
||||
@@ -114,7 +114,7 @@ class BindingHighlightTest {
|
||||
val mini = sink.build()
|
||||
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`
|
||||
val nameSym = binding.symbols.firstOrNull { it.name == "name" }
|
||||
@@ -163,7 +163,7 @@ class BindingHighlightTest {
|
||||
val mini = sink.build()
|
||||
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) }
|
||||
assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol")
|
||||
|
||||
@@ -36,7 +36,7 @@ class BindingTest {
|
||||
Compiler.compileWithMini(src, sink)
|
||||
val mini = sink.build()
|
||||
assertNotNull(mini, "MiniScript should be built")
|
||||
return Binder.bind(src, mini!!)
|
||||
return Binder.bind(src, mini)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,7 +72,7 @@ class BindingTest {
|
||||
val xSym = snap.symbols.firstOrNull { it.name == "x" }
|
||||
assertNotNull(xSym)
|
||||
// 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)
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ class BindingTest {
|
||||
val fooField = snap.symbols.firstOrNull { it.name == "foo" }
|
||||
assertNotNull(fooField)
|
||||
// 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)
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ class BindingTest {
|
||||
)
|
||||
val xField = snap.symbols.firstOrNull { it.name == "x" }
|
||||
assertNotNull(xField)
|
||||
val refs = snap.references.count { it.symbolId == xField!!.id }
|
||||
val refs = snap.references.count { it.symbolId == xField.id }
|
||||
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 mini = sink.build()
|
||||
assertNotNull(mini)
|
||||
val imps = mini!!.imports
|
||||
val imps = mini.imports
|
||||
assertTrue(imps.isNotEmpty(), "imports should be captured")
|
||||
val first = imps.first()
|
||||
val segNames = first.segments.map { it.name }
|
||||
@@ -68,12 +68,12 @@ class MiniAstTest {
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
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")
|
||||
// Doc
|
||||
assertNotNull(fn.doc)
|
||||
assertEquals("Summary: does foo", fn.doc!!.summary)
|
||||
assertTrue(fn.doc!!.raw.contains("details"))
|
||||
assertEquals("Summary: does foo", fn.doc.summary)
|
||||
assertTrue(fn.doc.raw.contains("details"))
|
||||
// Params
|
||||
assertEquals(2, fn.params.size)
|
||||
val p1 = fn.params[0]
|
||||
@@ -99,10 +99,10 @@ class MiniAstTest {
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
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.doc)
|
||||
assertEquals("docs for x", vd.doc!!.summary)
|
||||
assertEquals("docs for x", vd.doc.summary)
|
||||
val ty = vd.type
|
||||
assertNotNull(ty)
|
||||
val gen = ty as MiniGenericType
|
||||
@@ -126,10 +126,10 @@ class MiniAstTest {
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
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.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
|
||||
@@ -141,10 +141,10 @@ class MiniAstTest {
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
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.doc)
|
||||
assertTrue(cd.doc!!.raw.contains("Class C docs"))
|
||||
assertTrue(cd.doc.raw.contains("Class C docs"))
|
||||
// Bases captured as plain names for now
|
||||
assertEquals(listOf("Base1", "Base2"), cd.bases)
|
||||
}
|
||||
@@ -162,10 +162,10 @@ class MiniAstTest {
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
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.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("E", ed.name)
|
||||
}
|
||||
|
||||
@@ -711,4 +711,24 @@ class OOTest {
|
||||
""".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())
|
||||
}
|
||||
|
||||
@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");
|
||||
* 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 } )
|
||||
""")
|
||||
}
|
||||
|
||||
@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)
|
||||
""")
|
||||
}
|
||||
|
||||
@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)
|
||||
assertNotNull(open)
|
||||
// The char at open must be '{'
|
||||
assertEquals('{', text[open!!])
|
||||
assertEquals('{', text[open])
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -55,7 +55,7 @@ class BlockReindentTest {
|
||||
val range = BraceUtils.findEnclosingBlockRange(text, close, includeTrailingNewline = true)
|
||||
assertNotNull(range)
|
||||
// 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 startLinePrefix = text.substring(BraceUtils.lineStart(text, start), start)
|
||||
// start at column 0 of the line
|
||||
|
||||
@@ -21,6 +21,15 @@ import kotlin.test.assertEquals
|
||||
|
||||
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
|
||||
fun reindent_simpleFunction() {
|
||||
val src = """
|
||||
|
||||
@@ -34,7 +34,7 @@ class CommentEolTest {
|
||||
assertTrue(cmt != null, "Expected a comment span")
|
||||
// It should start at 0 and extend exactly to the end of the line (before \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")
|
||||
// Ensure there is no other span overlapping within the same line
|
||||
spans.filter { it !== cmt }.forEach {
|
||||
@@ -50,7 +50,7 @@ class CommentEolTest {
|
||||
assertTrue(cmt != null, "Expected a block comment span")
|
||||
// The comment should end right after "/* block */"
|
||||
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
|
||||
|
||||
@@ -25,6 +25,16 @@ class HighlightMappingTest {
|
||||
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 }
|
||||
|
||||
@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
|
||||
fun keywordsAndIdentifiers() {
|
||||
val text = "a and b or not c"
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.sergeych.lyng.miniast
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class CompletionEngineLightTest {
|
||||
@@ -89,9 +90,9 @@ class CompletionEngineLightTest {
|
||||
""".trimIndent()
|
||||
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
|
||||
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"
|
||||
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
|
||||
@@ -106,8 +107,8 @@ class CompletionEngineLightTest {
|
||||
val names = items.map { it.name }
|
||||
assertTrue(names.isNotEmpty(), "Expected String members for parenthesized literal, got empty list")
|
||||
val reItem = items.firstOrNull { it.name == "re" }
|
||||
assertTrue(reItem != null, "Expected to find 're' for parenthesized String literal, got: $names")
|
||||
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected ': Regex' for re(), was: ${reItem.typeText}")
|
||||
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}")
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,8 +121,8 @@ class CompletionEngineLightTest {
|
||||
val names = items.map { it.name }
|
||||
assertTrue(names.isNotEmpty(), "Expected String members without explicit imports, got empty list")
|
||||
val reItem = items.firstOrNull { it.name == "re" }
|
||||
assertTrue(reItem != null, "Expected to find 're' without explicit imports, got: $names")
|
||||
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected ': Regex' for re() without imports, was: ${reItem.typeText}")
|
||||
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}")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package lyng.stdlib
|
||||
|
||||
/*
|
||||
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.
|
||||
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.
|
||||
*/
|
||||
fun cached(builder) {
|
||||
var calculated = false
|
||||
@@ -16,7 +16,7 @@ fun cached(builder) {
|
||||
}
|
||||
}
|
||||
/* 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 {
|
||||
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) {
|
||||
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 {
|
||||
var hits = 0
|
||||
@@ -49,8 +49,8 @@ fun Iterable.count(predicate): Int {
|
||||
hits
|
||||
}
|
||||
/*
|
||||
filter out all null elements from this collection (Iterable); flow of
|
||||
non-null elements is returned
|
||||
filter out all null elements from this collection (Iterable); flow of
|
||||
non-null elements is returned
|
||||
*/
|
||||
fun Iterable.filterFlowNotNull(): Flow {
|
||||
filterFlow { it != null }
|
||||
@@ -72,12 +72,12 @@ fun Iterable.drop(n) {
|
||||
val Iterable.first get() {
|
||||
val i = iterator()
|
||||
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
|
||||
NuSuchElementException
|
||||
Return the first element that matches the predicate or throws
|
||||
NuSuchElementException
|
||||
*/
|
||||
fun Iterable.findFirst(predicate) {
|
||||
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) {
|
||||
for( x in this ) {
|
||||
@@ -108,7 +108,7 @@ val Iterable.last get() {
|
||||
found = true
|
||||
}
|
||||
if( !found ) throw NoSuchElementException()
|
||||
element
|
||||
element
|
||||
}
|
||||
|
||||
/* 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.
|
||||
@return List
|
||||
Returns a single list of all elements from all collections in the given collection.
|
||||
@return List
|
||||
*/
|
||||
fun Iterable.flatten() {
|
||||
val result = []
|
||||
@@ -227,8 +227,8 @@ fun Iterable.flatten() {
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a single list of all elements yielded from results of transform function being
|
||||
invoked on each element of original collection.
|
||||
Returns a single list of all elements yielded from results of transform function being
|
||||
invoked on each element of original collection.
|
||||
*/
|
||||
fun Iterable.flatMap(transform): List {
|
||||
map(transform).flatten()
|
||||
@@ -279,8 +279,8 @@ val String.re get() = Regex(this)
|
||||
fun TODO(message=null) = throw NotImplementedException(message ?: "not implemented")
|
||||
|
||||
/*
|
||||
Provides different access types for delegates.
|
||||
Used in the 'bind' hook to validate delegate usage.
|
||||
Provides different access types for delegates.
|
||||
Used in the 'bind' hook to validate delegate usage.
|
||||
*/
|
||||
enum DelegateAccess {
|
||||
Val,
|
||||
@@ -289,9 +289,9 @@ enum DelegateAccess {
|
||||
}
|
||||
|
||||
/*
|
||||
Base interface for all delegates.
|
||||
Implementing this interface is optional as Lyng uses dynamic dispatch,
|
||||
but it is recommended for documentation and clarity.
|
||||
Base interface for all delegates.
|
||||
Implementing this interface is optional as Lyng uses dynamic dispatch,
|
||||
but it is recommended for documentation and clarity.
|
||||
*/
|
||||
interface Delegate {
|
||||
/* 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")
|
||||
|
||||
/*
|
||||
Called once during initialization to configure or validate the delegate.
|
||||
Should return the delegate object to be used (usually 'this').
|
||||
Called once during initialization to configure or validate the delegate.
|
||||
Should return the delegate object to be used (usually 'this').
|
||||
*/
|
||||
fun bind(name, access, thisRef) = this
|
||||
}
|
||||
|
||||
/*
|
||||
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.
|
||||
Executes the block with `this` set to self and
|
||||
returns what the block returns.
|
||||
*/
|
||||
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 {
|
||||
private val creator = creatorParam
|
||||
@@ -325,7 +335,8 @@ class lazy(creatorParam) : Delegate {
|
||||
}
|
||||
|
||||
override fun getValue(thisRef, name) {
|
||||
if (value == Unset) value = creator()
|
||||
if (value == Unset)
|
||||
value = with(thisRef,creator)
|
||||
value
|
||||
}
|
||||
}
|
||||
@@ -530,7 +530,7 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
|
||||
fun isIdentPart(ch: Char) = ch == '_' || ch == '$' || ch == '~' || ch.isLetterOrDigit()
|
||||
// A conservative list of language keywords to avoid misclassifying as function calls
|
||||
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",
|
||||
"throw", "return", "break", "continue", "in", "is", "as", "as?", "not",
|
||||
"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
|
||||
continue
|
||||
}
|
||||
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)
|
||||
if ((text.startsWith("class", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+5 >= n || !isIdentPart(text.getOrNull(i+5) ?: '\u0000'))) ||
|
||||
(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)
|
||||
if (name != null) result[p to name.second] = "hl-class"
|
||||
i = p
|
||||
|
||||
@@ -77,6 +77,14 @@ fun HomePage() {
|
||||
// Create, transform, and verify — the Lyng way
|
||||
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 evens2 = data.filter { it % 2 == 0 }.map { it * it }
|
||||
assertEquals([4, 16], evens2)
|
||||
@@ -86,8 +94,12 @@ val base = { a: 1, b: 2 }
|
||||
val patch = { b: 3, c: }
|
||||
val m = { "a": 0, ...base, ...patch, d: 4 }
|
||||
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
|
||||
""".trimIndent()
|
||||
val mapHtml = "<pre><code>" + htmlEscape(code) + "</code></pre>"
|
||||
|
||||
@@ -329,7 +329,7 @@
|
||||
<!-- Top-left version ribbon -->
|
||||
<div class="corner-ribbon bg-danger text-white">
|
||||
<span style="margin-left: -5em">
|
||||
v1.1.0-rc
|
||||
v1.1.1-SNAPSHOT
|
||||
</span>
|
||||
</div>
|
||||
<!-- Fixed top navbar for the whole site -->
|
||||
|
||||
@@ -20,6 +20,22 @@ import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
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
|
||||
fun highlightsLyngFencedBlock() {
|
||||
val md = """
|
||||
|
||||
Reference in new issue
Block a user