Compare commits

..
34 Commits
Author SHA1 Message Date
sergeych 7fa491c0bb Publish AI reference discovery and embedding instructions 2026-10-21 03:57:27 +04:00
sergeych 0d727d64a5 Forward Gradle version properties through site deployment 2026-10-21 00:08:56 +04:00
sergeych c76b10e9fd Document compiler fixes and repair standalone documentation images 2026-10-21 00:04:58 +04:00
sergeych c34371b784 Preserve static return types and initialize singleton static methods 2026-10-21 00:01:11 +04:00
sergeych 04c9343a9d Reject class-named members and unsupported constructor delegation 2026-10-20 23:57:41 +04:00
sergeych 26a1ca9710 Add offline HTML parsing module to LyngIO 2026-10-16 02:01:49 +04:00
sergeych 66ab7db3d8 Fix multiline list parsing and expose actionable HTTP failures 2026-10-13 02:19:56 +04:00
sergeych 8b0d847776 Limit lyng.io HTTP redirect chains 2026-08-18 13:39:25 +04:00
Junie AI 4fa9b80592 Update build configurations for Java 17 compatibility and remove unused Modelfile 2026-08-15 17:08:49 +04:00
sergeych 9451eb574f Fix compatibility issues with Kotlin/Native commonization tasks during build and configuration cache 2026-07-28 21:18:27 +04:00
sergeych 7635664dbc version bump to 1.5.10 2026-07-28 17:22:21 +04:00
sergeych e66f646b25 Improve type inference for default initializers and add support for assign operator methods 2026-07-28 17:20:02 +04:00
sergeych 2f118a1fef docs on new function metadata API 2026-07-28 00:36:12 +04:00
sergeych 898ebfa536 Add resolved export metadata and fix imported accessors 2026-07-27 22:35:49 +04:00
sergeych 1a22b60b7d 1.5.9 deployed 2026-06-28 00:41:00 +03:00
sergeych 14080c3875 upgrade kotlin version to 2.4.0 2026-06-27 04:54:44 +03:00
sergeych 90e46a3fff fix another type inference problem with external structures 2026-06-27 04:35:51 +03:00
sergeych 7af6d3925e release 1.5.6 2026-05-16 11:18:48 +03:00
sergeych 234d1ef02b Add lyng.io.process module for process execution utilities and shell scripting sugar (e.g., sh, exec, CommandRun). 2026-05-10 15:04:32 +03:00
sergeych 3e74019d9d fixed CLI input problem and tetris sample 2026-05-10 13:00:08 +03:00
sergeych 31fac1a73c site: fixed .md/lyng code display 2026-04-30 09:50:10 +03:00
sergeych 53a9d21a19 docs improved 2026-04-29 22:39:43 +03:00
sergeych 739fdfc94b Add HTTP respondHtml sugar 2026-04-29 22:28:37 +03:00
sergeych c8e03d69ad Add Lyng HTML DSL helpers 2026-04-29 22:07:30 +03:00
sergeych b2200e71ff Add context receiver extensions for DSLs 2026-04-29 20:49:50 +03:00
sergeych e107296bca Add receiver-stack function types 2026-04-29 12:59:24 +03:00
sergeych 1bababa058 Normalize site TOC heading hierarchy 2026-04-27 15:21:44 +03:00
sergeych 35f4c968a4 T? ?: break inference fixed 2026-04-26 21:42:36 +03:00
sergeych 79429d5f2d added nullable ?: break support with proper inference ;) 2026-04-26 20:57:46 +03:00
sergeych fae9965bdf fix CLI boostrap of http server 2026-04-26 17:23:21 +03:00
sergeych 2dc4fb8230 more http server docs 2026-04-26 16:16:59 +03:00
sergeych f74ed9afe4 Switch HTTP server API to RequestContext receivers 2026-04-26 15:57:57 +03:00
sergeych ca4a0d4b12 Extend HTTP server routing and JSON exchange helpers 2026-04-26 13:42:14 +03:00
sergeych b969edd30a Fix commonMain cancellation handling in HTTP server loop 2026-04-26 10:21:33 +03:00
118 changed files with 6433 additions and 389 deletions

No files matched your search

+2
View File
@@ -31,3 +31,5 @@ test_output*.txt
/bugcontents.db /bugcontents.db
/bugs/ /bugs/
contents.db contents.db
/docs/idea_plugin.md.deploy_backup
/.claude/settings.local.json
+1
View File
@@ -1,6 +1,7 @@
# AI Agent Notes # AI Agent Notes
## Canonical AI References ## Canonical AI References
- Public discovery: `https://lynglang.com/llms.txt`; workflow: `docs/ai_agents.md`. Keep these links current when moving canonical references.
- Use `docs/ai_language_reference.md` as the primary, compiler-verified Lyng language reference for code generation. - Use `docs/ai_language_reference.md` as the primary, compiler-verified Lyng language reference for code generation.
- For generics-heavy code generation, follow `docs/ai_language_reference.md` section `7.1 Generics Runtime Model and Bounds` and `7.2 Differences vs Java / Kotlin / Scala`. - For generics-heavy code generation, follow `docs/ai_language_reference.md` section `7.1 Generics Runtime Model and Bounds` and `7.2 Differences vs Java / Kotlin / Scala`.
- Use `docs/ai_stdlib_reference.md` for default runtime/module APIs and stdlib surface. - Use `docs/ai_stdlib_reference.md` for default runtime/module APIs and stdlib surface.
+29 -1
View File
@@ -9,7 +9,35 @@ History note:
## Unreleased ## Unreleased
- No unreleased entries yet. - Reject member functions named after their enclosing class and diagnose unsupported `this(...)` constructor delegation at compile time.
- Preserve declared, inferred, and generic return types from static method calls.
- Initialize singleton static members and dispatch their calls through class scope, including trailing-lambda calls.
- Update the language reference and OOP guide with constructor naming rules and typed static factory examples.
## 1.5.6 (2026-05-16)
### Process execution and CLI scripting
- Added `lyng.io.process` for coroutine-friendly external process execution.
- Added `sh(...)`, `exec(...)`, and `CommandRun` with captured output, streaming output, `wait()`, `check()`, and process status helpers.
- Documented safe shell-vs-argv usage and common script patterns for CLI automation.
### HTTP server and HTML rendering
- Added the minimal `lyng.io.http.server` API for embedded HTTP/1.1 services, local tools, test fixtures, and lightweight app backends.
- Added exact-path, regex, and path-template routing with named route parameters.
- Added `RequestContext` receiver sugar, JSON helpers (`jsonBody<T>()`, `respondJson(...)`), reusable routers, and WebSocket routes.
- Added `respondHtml { ... }` and the pure Lyng `lyng.io.html` builder DSL with escaped text, attributes, common tag helpers, and generic tag escape hatches.
### Language and serialization
- Added receiver-stack function types and context receiver extensions for DSL-style APIs.
- Added canonical JSON round-trip APIs with `Json.encode(...)` / `Json.decode(...)`.
- Added typed canonical JSON APIs with `Json.encodeAs(Type, value)` / `Json.decodeAs(Type, text)`.
- Extended database serialization support with typed canonical JSON encoding, SQL object expansion, decode annotations, and preserved declaration metadata.
### Runtime, docs, and samples
- Fixed nullable Elvis-with-`break` inference and callable return inference regressions.
- Fixed CLI bootstrap behavior for HTTP server scripts and CLI input handling.
- Expanded docs for HTTP, WebSocket, serialization, process execution, DSL receivers, and the generated site.
- Release metadata and README now point to `1.5.6`.
## 1.5.5 (2026-04-23) ## 1.5.5 (2026-04-23)
+9 -3
View File
@@ -47,13 +47,19 @@ assertEquals(A.E.One, A.One)
## Resources: ## Resources:
- [Language home](https://lynglang.com) - [Language home](https://lynglang.com)
- [AI agent instructions](docs/ai_agents.md) and [machine-readable entry point](https://lynglang.com/llms.txt)
- [introduction and tutorial](docs/tutorial.md) - start here please - [introduction and tutorial](docs/tutorial.md) - start here please
- [Latest release notes (1.5.5)](docs/whats_new.md) - [Latest release notes (1.5.6)](docs/whats_new.md)
- [What's New in 1.5](docs/whats_new_1_5.md) - [What's New in 1.5](docs/whats_new_1_5.md)
- [Testing and Assertions](docs/Testing.md) - [Testing and Assertions](docs/Testing.md)
- [Filesystem and Processes (lyngio)](docs/lyngio.md) - [Filesystem and Processes (lyngio)](docs/lyngio.md)
- [SQL Databases (lyng.io.db)](docs/lyng.io.db.md) - [SQL Databases (lyng.io.db)](docs/lyng.io.db.md)
- [HTTP Server (lyng.io.http.server)](docs/lyng.io.http.server.md)
- [HTML Builder (lyng.io.html)](docs/lyng.io.html.md)
- [HTML Parsing (lyng.io.html.parse)](docs/lyng.io.html.parse.md)
- [Process Execution (lyng.io.process)](docs/lyng.io.process.md)
- [Time and Calendar Types](docs/time.md) - [Time and Calendar Types](docs/time.md)
- [Json support](docs/json_and_kotlin_serialization.md)
- [Return Statement](docs/return_statement.md) - [Return Statement](docs/return_statement.md)
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md) - [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
- [Samples directory](docs/samples) - [Samples directory](docs/samples)
@@ -66,7 +72,7 @@ assertEquals(A.E.One, A.One)
### Add dependency to your project ### Add dependency to your project
```kotlin ```kotlin
val lyngVersion = "1.5.5" val lyngVersion = "1.5.6"
repositories { repositories {
// ... // ...
@@ -186,7 +192,7 @@ Designed to add scripting to kotlin multiplatform application in easy and effici
# Language Roadmap # Language Roadmap
The current stable release is **v1.5.5**: the 1.5 cycle now includes the database/date/concurrency additions as well as the latest compiler/runtime stabilization work, and the language, tooling, and site are aligned around this release. The current stable release is **v1.5.6**: the 1.5 cycle now includes the database/date/concurrency additions, process execution, lightweight HTTP server and HTML DSL work, canonical Json serialization, and the latest compiler/runtime stabilization work.
Ready features: Ready features:
+48 -8
View File
@@ -17,15 +17,55 @@
# #
# #
set -e set -euo pipefail
echo "publishing all artifacts"
echo
./gradlew publishToMavenLocal site:jsBrowserDistribution publish buildInstallablePlugin :lyng:linkReleaseExecutableLinuxX64 :lyng:installJvmDist --parallel --no-configuration-cache
#echo GRADLE_ARGS=(--no-configuration-cache --console=plain)
#echo "Creating plugin"
#echo echo "actualizing Kotlin JS/Wasm lock files"
#./gradlew buildInstallablePlugin echo
./gradlew kotlinUpgradeYarnLock kotlinWasmUpgradeYarnLock "${GRADLE_ARGS[@]}"
echo "building all artifacts"
echo
./gradlew publishToMavenLocal site:jsBrowserDistribution buildInstallablePlugin :lyng:linkReleaseExecutableLinuxX64 :lyng:installJvmDist --parallel "${GRADLE_ARGS[@]}"
echo
echo "publishing remote Maven artifacts"
echo
tasks_log="$(mktemp)"
./gradlew tasks --all "${GRADLE_ARGS[@]}" >"$tasks_log"
mapfile -t publish_tasks < <(
sed -n 's/^\([^[:space:]]*publish.*PublicationToMavenRepository\) - .*$/:\1/p' "$tasks_log" |
sort -u
)
rm -f "$tasks_log"
if [ "${#publish_tasks[@]}" -eq 0 ]; then
echo "No remote Maven publication tasks found."
exit 1
fi
for task in "${publish_tasks[@]}"; do
echo
echo "Publishing $task"
publish_log="$(mktemp)"
if ./gradlew "$task" "${GRADLE_ARGS[@]}" >"$publish_log" 2>&1; then
cat "$publish_log"
rm -f "$publish_log"
continue
fi
if grep -Eiq '(^|[^0-9])409([^0-9]|$)|already exists|already been published|cannot be updated|Package file already exists|Repository does not allow updating assets' "$publish_log"; then
cat "$publish_log"
echo "Skipping $task: artifact already exists in the Maven repository."
rm -f "$publish_log"
continue
fi
cat "$publish_log"
rm -f "$publish_log"
exit 1
done
echo echo
echo "building CLI tools" echo "building CLI tools"
+5 -1
View File
@@ -17,11 +17,15 @@
# #
# #
# Pass snapshot overrides through explicitly, e.g. -Plyng.version=1.5.11-SNAPSHOT.
GRADLE_ARGS=()
upload_only=false upload_only=false
target=vps # default: new server; use --old for d.lynglang.com target=vps # default: new server; use --old for d.lynglang.com
for arg in "$@"; do for arg in "$@"; do
if [[ "$arg" == "-u" || "$arg" == "--upload-only" ]]; then if [[ "$arg" == "-u" || "$arg" == "--upload-only" ]]; then
upload_only=true upload_only=true
elif [[ "$arg" == -P* ]]; then
GRADLE_ARGS+=("$arg")
elif [[ "$arg" == "--old" ]]; then elif [[ "$arg" == "--old" ]]; then
target=com target=com
fi fi
@@ -125,7 +129,7 @@ if [[ "$upload_only" == false ]]; then
refreshTextmateZip refreshTextmateZip
updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link" updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link"
./gradlew site:jsBrowserDistribution ./gradlew site:jsBrowserDistribution "${GRADLE_ARGS[@]}"
BUILD_RC=$? BUILD_RC=$?
# Always restore original doc if backup exists # Always restore original doc if backup exists
+2 -1
View File
@@ -113,9 +113,10 @@ process_file() {
# - [text](file.md) -> [text](#file.md) # - [text](file.md) -> [text](#file.md)
# - [text](dir/file.md) -> [text](#dir_file.md) # - [text](dir/file.md) -> [text](#dir_file.md)
# - [text](file.md#anchor) -> [text](#anchor) # - [text](file.md#anchor) -> [text](#anchor)
# - Fix image links: [alt](../images/...) -> [alt](images/...) if needed, but none found yet. # - Resolve site-root image URLs against the local site resources for embedding.
cat "$full_path" | \ cat "$full_path" | \
perl -pe 's{(!\[[^\]]*\]\()/}{$1site/src/jsMain/resources/}g' | \
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \ perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD" perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD"
+17
View File
@@ -21,6 +21,23 @@ plugins {
alias(libs.plugins.vanniktech.mavenPublish) apply false alias(libs.plugins.vanniktech.mavenPublish) apply false
} }
// Some JVM dependencies are published as Java 21 bytecode. Keep the library's
// Java 17 compilation target, but use a compatible runtime for its JVM tests.
project(":lynglib") {
afterEvaluate {
val toolchains = extensions.getByType<org.gradle.jvm.toolchain.JavaToolchainService>()
tasks.withType<Test>().configureEach {
javaLauncher.set(toolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(21))
})
// An explicit jvmTest invocation should execute the suite rather than
// silently reusing the previous test task outputs or build-cache entry.
outputs.upToDateWhen { false }
outputs.cacheIf { false }
}
}
}
// Convenience alias to run the IntelliJ IDE with the Lyng plugin from the project root. // Convenience alias to run the IntelliJ IDE with the Lyng plugin from the project root.
// Usage: ./gradlew runIde // Usage: ./gradlew runIde
// It simply delegates to :lyng-idea:runIde provided by the Gradle IntelliJ Plugin. // It simply delegates to :lyng-idea:runIde provided by the Gradle IntelliJ Plugin.
+67 -1
View File
@@ -42,6 +42,12 @@ a _constructor_ that requires two parameters for fields. So when creating it wit
Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the
example above. example above.
A member function cannot have the same name as its enclosing class, including a
static member function. Constructors are declared in the class header; use the
class name to create another instance. `this(...)` and `this@Type(...)` do not
declare or delegate to secondary constructors. Local functions may shadow class
names according to the normal lexical scope rules.
## Singleton Objects ## Singleton Objects
Singleton objects are declared using the `object` keyword. An `object` declaration defines both a class and a single instance of that class at the same time. This is perfect for stateless utilities, global configuration, or shared delegates. Singleton objects are declared using the `object` keyword. An `object` declaration defines both a class and a single instance of that class at the same time. This is perfect for stateless utilities, global configuration, or shared delegates.
@@ -454,6 +460,43 @@ Key rules and features:
- For arbitrary receivers, use casts: `(expr as Type).member(...)` or `(expr as? Type)?.member(...)`. - For arbitrary receivers, use casts: `(expr as Type).member(...)` or `(expr as? Type)?.member(...)`.
- Qualified access does not relax visibility. - Qualified access does not relax visibility.
### Receiver-stack lambdas
Qualified `this@Type` is also used outside inheritance when a lambda has multiple visible receivers.
This is common in DSL-style builders.
- `A & B` means one receiver value that implements both types.
- `context(A, B) C.()->R` means a receiver stack:
- primary `this` is `C`
- outer/context receivers are `A`, then `B`
- Unqualified lookup checks the primary receiver first.
- If the primary receiver does not define a member and several outer/context receivers do, Lyng reports a compile-time ambiguity. Use `this@Type` to select one explicitly.
Example:
```lyng
class Html { fun title() = "html" }
class Head { fun title() = "head" }
class Body
val block: context(Html, Head) Body.()->String = {
// title() // compile-time ambiguity: Html vs Head
this@Html.title()
}
```
Context receivers can also constrain extension functions. The extension is visible only when the required receiver is
already in the implicit receiver stack:
```lyng
class Tag { fun addText(text: String) { /* ... */ } }
context(Tag)
fun String.unaryPlus() {
this@Tag.addText(this)
}
```
- Field inheritance (`val`/`var`) and collisions - Field inheritance (`val`/`var`) and collisions
- Instance storage is kept per declaring class, internally disambiguated; unqualified read/write resolves to the first match in the resolution order (leftmost base). - Instance storage is kept per declaring class, internally disambiguated; unqualified read/write resolves to the first match in the resolution order (leftmost base).
- Qualified read/write (via `this@Type` or casts) targets the chosen ancestor’s storage. - Qualified read/write (via `this@Type` or casts) targets the chosen ancestor’s storage.
@@ -625,10 +668,14 @@ Unary operators are overloaded by defining methods with no arguments:
| Operator | Method Name | | Operator | Method Name |
| :--- | :--- | | :--- | :--- |
| `+a` | `unaryPlus()` |
| `-a` | `negate()` | | `-a` | `negate()` |
| `!a` | `logicalNot()` | | `!a` | `logicalNot()` |
| `~a` | `bitNot()` | | `~a` | `bitNot()` |
`unaryPlus()` is useful in DSL-style builders where `+"text"` should append text to
the current receiver. See [samples/html_builder_dsl.lyng](samples/html_builder_dsl.lyng).
### Assignment Operators ### Assignment Operators
Assignment operators like `+=` first attempt to call a specific assignment method. If that method is not defined, they fall back to a combination of the binary operator and a regular assignment (e.g., `a = a + b`). Assignment operators like `+=` first attempt to call a specific assignment method. If that method is not defined, they fall back to a combination of the binary operator and a regular assignment (e.g., `a = a + b`).
@@ -1001,6 +1048,25 @@ Static fields can be accessed from static methods via the class qualifier:
assertEquals("bar", Test.getData() ) assertEquals("bar", Test.getData() )
>>> void >>> void
Static calls preserve their declared or inferred return types, so their results
can be used directly in chained expressions or assigned without a type annotation.
Generic static factories also preserve the type inferred from their arguments:
class StaticBox<T>(val value: T) {
static fun of<T>(value: T): StaticBox<T> = StaticBox(value)
}
val box = StaticBox.of("hello")
assertEquals(5, box.value.length)
>>> void
Named singleton objects also support static methods:
object StaticGreeting {
static fun text(): String = "hello"
}
assertEquals(5, StaticGreeting.text().length)
>>> void
# Extension members # Extension members
Sometimes an existing type or named singleton object is missing some particular functionality that can be _added to it_ without rewriting its inner logic and without using its private state. In this case, _extension members_ can be used. Sometimes an existing type or named singleton object is missing some particular functionality that can be _added to it_ without rewriting its inner logic and without using its private state. In this case, _extension members_ can be used.
@@ -1207,7 +1273,7 @@ scopeB()
assertThrows { 42.description } assertThrows { 42.description }
``` ```
This isolation ensures that libraries can use extensions internally without worrying about name collisions with other libraries or the user's code. When a module is imported using `use`, its top-level extensions become available in the importing scope. This isolation ensures that libraries can use extensions internally without worrying about name collisions with other libraries or the user's code. When a package is imported using `import`, its top-level extensions become available in the importing scope. An imported extension property's getter and setter retain the lexical scope of the package that declared them, so they can safely call that package's private helpers and bound `extern` functions and properties.
## dynamic symbols ## dynamic symbols
+24
View File
@@ -0,0 +1,24 @@
# Lyng instructions for AI agents
Before writing Lyng, read the [current language reference](ai_language_reference.md)
and [compact standard-library reference](ai_stdlib_reference.md). These are the
canonical implementation-oriented references; older specifications and proposals
must not override them. Discover them from <https://lynglang.com/llms.txt>.
Use the language reference for syntax, types, generic bounds, imports, and current
limitations. Use the stdlib reference to distinguish default globals from explicit
imports and optional host modules. Follow links to individual API pages when you
need more detail. Do not guess Kotlin/Java method names or assume optional modules
are installed. Check the host runtime version and validate uncertain constructs
with a small read-only script before changing user data.
For offline embedding, copy both compact references from the same committed Lyng
revision, retain their paths and commit ID, and split long results at section
boundaries to fit the host's tool output limit. Keep host-specific capability and
permission documentation alongside them. In Tetradion, call `agent.docsList()` or
`agent.docsSearch("query")`, then `agent.docs("reference.syntax")`,
`agent.docs("reference.flow")`, `agent.docs("reference.types")`,
`agent.docs("reference.modules")`, or `agent.docs("reference.stdlib")`.
The site publishes these Markdown files directly under `/docs/`, without requiring
JavaScript or browser automation.
+15
View File
@@ -43,6 +43,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
## 3. Literals and Core Expressions ## 3. Literals and Core Expressions
- Scalars: `null`, `true`, `false`, `void`. - Scalars: `null`, `true`, `false`, `void`.
- List literals allow multiline layout, comments between entries and trailing commas.
Newlines before the closing `]` are whitespace, including in nested and empty lists.
- List literal: `[a, b, c]`, spreads with `...`. - List literal: `[a, b, c]`, spreads with `...`.
- Spread positions: beginning, middle, end are all valid: `[...a]`, `[0, ...a, 4]`, `[head, ...mid, tail]`. - Spread positions: beginning, middle, end are all valid: `[...a]`, `[0, ...a, 4]`, `[head, ...mid, tail]`.
- Spread source must be a `List` at runtime (non-list spread raises an error). - Spread source must be a `List` at runtime (non-list spread raises an error).
@@ -65,6 +67,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- Declaration-side variadic parameters use ellipsis suffix: - Declaration-side variadic parameters use ellipsis suffix:
- functions: `fun f(head, tail...) { ... }` - functions: `fun f(head, tail...) { ... }`
- lambdas: `{ x, rest... -> ... }` - lambdas: `{ x, rest... -> ... }`
- a typed parameter `values: T...` has signature type `T...`, but inside the callable `values`
is a `List<T>` containing the collected arguments.
- Call-side splats use `...expr` and are expanded by argument kind: - Call-side splats use `...expr` and are expanded by argument kind:
- positional splat: `f(...[1,2,3])` - positional splat: `f(...[1,2,3])`
- named splat: `f(...{ a: 1, b: 2 })` (map-style) - named splat: `f(...{ a: 1, b: 2 })` (map-style)
@@ -83,6 +87,7 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
## 4. Operators (implemented) ## 4. Operators (implemented)
- Assignment: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `?=`. - Assignment: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `?=`.
- Logical: `||`, `&&`, unary `!`. - Logical: `||`, `&&`, unary `!`.
- Unary arithmetic/bitwise: unary `+`, unary `-`, `~`.
- Bitwise: `|`, `^`, `&`, `~`, shifts `<<`, `>>`. - Bitwise: `|`, `^`, `&`, `~`, shifts `<<`, `>>`.
- Equality/comparison: `==`, `!=`, `===`, `!==`, `<`, `<=`, `>`, `>=`, `<=>`, `=~`, `!~`. - Equality/comparison: `==`, `!=`, `===`, `!==`, `<`, `<=`, `>`, `>=`, `<=>`, `=~`, `!~`.
- Type/containment: `is`, `!is`, `in`, `!in`, `as`, `as?`. - Type/containment: `is`, `!is`, `in`, `!in`, `as`, `as?`.
@@ -119,7 +124,9 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- shorthand: `fun f(x) = expr`. - shorthand: `fun f(x) = expr`.
- generics: `fun f<T>(x: T): T`. - generics: `fun f<T>(x: T): T`.
- extension functions: `fun Type.name(...) { ... }`. - extension functions: `fun Type.name(...) { ... }`.
- context-aware extension functions: `context(Tag) fun String.unaryPlus() { this@Tag.addText(this) }`.
- named singleton `object` declarations can be extension receivers too: `fun Config.describe(...) { ... }`, `val Config.tag get() = ...`. - named singleton `object` declarations can be extension receivers too: `fun Config.describe(...) { ... }`, `val Config.tag get() = ...`.
- Static class/object methods preserve declared or inferable return types at call sites, including chained member access and generic return types inferred from arguments. For example, `static fun of<T>(v: T): Box<T> = Box(v)` lets `Box.of("abc").value.length` retain the `String` member type.
- static extension functions are callable on the type object: `static fun List<T>.fill(...)` -> `List.fill(...)`. - static extension functions are callable on the type object: `static fun List<T>.fill(...)` -> `List.fill(...)`.
- delegated callable: `fun f(...) by delegate`. - delegated callable: `fun f(...) by delegate`.
- Type aliases: - Type aliases:
@@ -127,6 +134,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- generic: `type Box<T> = List<T>` - generic: `type Box<T> = List<T>`
- aliases are expanded structurally. - aliases are expanded structurally.
- Classes/objects/enums/interfaces: - Classes/objects/enums/interfaces:
- Constructors use the class name. A direct member function (including `static`) cannot have the same name as its enclosing class; local functions may still shadow names normally.
- `this(...)` and `this@Type(...)` are not secondary-constructor delegation syntax; call the class by name to construct another instance.
- `interface` is parsed as abstract class synonym. - `interface` is parsed as abstract class synonym.
- `object` supports named singleton and anonymous object expression forms. - `object` supports named singleton and anonymous object expression forms.
- enums support lifted entries: `enum E* { A, B }`. - enums support lifted entries: `enum E* { A, B }`.
@@ -164,7 +173,12 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- unions `A | B` - unions `A | B`
- intersections `A & B` - intersections `A & B`
- function types `(A, B)->R` and receiver form `Receiver.(A)->R` - function types `(A, B)->R` and receiver form `Receiver.(A)->R`
- receiver-stack function types via `context(A, B) Receiver.(P)->R`
- variadics in function type via ellipsis (`T...`) - variadics in function type via ellipsis (`T...`)
- `A & B` means one value implementing both types.
- `context(A, B) Receiver.(P)->R` is different: it declares an ordered implicit-receiver stack where `Receiver` is primary `this`, then `A`, then `B`.
- Nested receiver lambdas keep outer receivers in scope; unqualified lookup prefers the innermost receiver, and `this@Type` can select an outer/context receiver explicitly.
- If the primary receiver does not provide a member and multiple outer/context receivers do, the lookup is a compile-time ambiguity and must be disambiguated with `this@Type`.
- Generics: - Generics:
- type params on classes/functions/type aliases - type params on classes/functions/type aliases
- bounds via `:` with union/intersection expressions - bounds via `:` with union/intersection expressions
@@ -217,6 +231,7 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- Disambiguation helpers are supported: - Disambiguation helpers are supported:
- qualified this: `this@Base.member()` - qualified this: `this@Base.member()`
- cast view: `(obj as Base).member()` - cast view: `(obj as Base).member()`
- In nested receiver lambdas, `this@Type` can target any receiver visible through the receiver stack, not just inheritance ancestors.
- On unknown receiver types, compiler allows only Object-safe members: - On unknown receiver types, compiler allows only Object-safe members:
- `toString`, `toInspectString`, `let`, `also`, `apply`, `run` - `toString`, `toInspectString`, `let`, `also`, `apply`, `run`
- Other members require known receiver type or explicit cast. - Other members require known receiver type or explicit cast.
+14
View File
@@ -0,0 +1,14 @@
# AI notes: publish JVM CLI updates with `bin/local_jrelease`
[//]: # (excludeFromIndex)
When a change affects the JVM CLI launcher used as `jlyng`, refresh the installed local distribution with:
```bash
bin/local_jrelease
```
Why:
- `jlyng` in this repo is installed from `~/bin/jlyng-jvm/lyng-jvm`, not directly from `lyng/build/install`.
- Manual copying from Gradle build output can leave the actual launcher on `PATH` stale.
- `bin/local_jrelease` rebuilds `lyng/build/distributions/lyng-jvm.zip`, reinstalls it under `~/bin/jlyng-jvm`, and recreates the `~/bin/jlyng` symlink.
+10
View File
@@ -0,0 +1,10 @@
# AI notes: heading levels must be consecutive
[//]: # (excludeFromIndex)
When editing repository documentation:
- Use heading levels in order: `#`, then `##`, then `###`, and so on.
- Do not skip levels, for example `#` directly to `###`.
- Keep the heading tree balanced inside each document; sibling sections should use the same level.
- If you add a subsection and the parent is `##`, the child must be `###`.
+7
View File
@@ -88,10 +88,17 @@ Sources: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt`, `lynglib/s
Requires installing `lyngio` into the import manager from host code. Requires installing `lyngio` into the import manager from host code.
- `import lyng.io.fs` (filesystem `Path` API) - `import lyng.io.fs` (filesystem `Path` API)
- `import lyng.io.process` (process execution API) - `import lyng.io.process` (process execution API)
- Shell-script sugar: `sh(command): CommandRun` and `exec(executable, args=[]): CommandRun`.
- Prefer `sh("git status --short").out` for small shell output, `sh("...").lines` for large stdout streams, and `.check()` for commands that must exit with code 0.
- Prefer `exec("git", ["add", file])` when arguments come from data, filenames, or user input; it bypasses shell parsing.
- `CommandRun` is active and owns process pipes; choose one consumption path per stream (`out` or `lines`, `err` or `errorLines`).
- `import lyng.io.console` (console capabilities, geometry, ANSI/output, events) - `import lyng.io.console` (console capabilities, geometry, ANSI/output, events)
- `import lyng.io.http` (HTTP/HTTPS client API) - `import lyng.io.http` (HTTP/HTTPS client API)
- `import lyng.io.http.server` (minimal HTTP/1.1 and WebSocket server API)
- `import lyng.io.ws` (WebSocket client API; currently supported on JVM, capability-gated elsewhere) - `import lyng.io.ws` (WebSocket client API; currently supported on JVM, capability-gated elsewhere)
- `import lyng.io.net` (TCP/UDP transport API; currently supported on JVM, capability-gated elsewhere) - `import lyng.io.net` (TCP/UDP transport API; currently supported on JVM, capability-gated elsewhere)
- `import lyng.io.html` (pure Lyng HTML builder DSL: `html { body { h3 { +"text" } } }`)
- `import lyng.io.html.parse` (offline HTML5 parsing: `Html.parse(source, baseUrl = "")`, document title/text, CSS selectors, element text/attributes/absolute URLs; see [HTML parsing](lyng.io.html.parse.md))
- Shared network value-type packages are also available when installed by host code: - Shared network value-type packages are also available when installed by host code:
- `import lyng.io.http.types` (`HttpHeaders`) - `import lyng.io.http.types` (`HttpHeaders`)
- `import lyng.io.ws.types` (`WsMessage`) - `import lyng.io.ws.types` (`WsMessage`)
+12
View File
@@ -41,6 +41,18 @@ Ellipsis can also appear in **function types** to denote a variadic position:
Ellipsis argument receives what is left from arguments after processing regular one that could be before or after. Ellipsis argument receives what is left from arguments after processing regular one that could be before or after.
For a typed declaration, the ellipsis type describes each argument while the name inside the
function is a list of those arguments. Thus `values: Int...` is `Int...` in the function signature
and `List<Int>` in the function body:
```lyng
fun sum(values: Int...): Int {
var result = 0
for (value in values) result += value
return result
}
```
Ellipsis could be a first argument: Ellipsis could be a first argument:
fun testCountArgs(data...,size) { fun testCountArgs(data...,size) {
+73
View File
@@ -107,6 +107,79 @@ val run2 = script.execute(scope)
`Scope.eval("...")` is the low-level shortcut that compiles and executes on the given scope. `Scope.eval("...")` is the low-level shortcut that compiles and executes on the given scope.
For most embedding use cases, prefer `session.eval("...")`. For most embedding use cases, prefer `session.eval("...")`.
### 2.2) Inspect function signatures without executing source
Declaration generators and other host tools can ask the compiler for semantic metadata about
top-level functions without running the script:
```kotlin
val source = Source(
"service.lyng",
"""
@Export
fun greet<T: Object = String>(
first: T,
rest: T...,
punctuation: String = "!",
): String = first.toString() + punctuation
""".trimIndent()
)
val functions = Compiler.resolveFunctionMetadata(
source,
Script.defaultImportManager,
)
val greet = functions.single()
println(greet.name) // greet
println(greet.annotations) // [Export]
println(greet.parameters[1].isEllipsis) // true
println(greet.parameters[2].defaultSource) // "!"
println(greet.returnType) // String
```
`resolveFunctionMetadata(...)` parses the source and resolves its imports and types, but it does
not execute the script, annotation functions, or default expressions. It returns
`ResolvedFunctionMetadata` entries in source order. Each entry provides:
- the function name, visibility, annotations, and source positions;
- generic parameters with their variance, bounds, and default types;
- parameters, variadic markers, and the original text of default expressions when available;
- the complete function type, including receivers, context receivers, and the declared or inferred
return type.
For a variadic declaration such as `values: Int...`, `ResolvedFunctionParameter.type` is the
element type `Int`, `isEllipsis` is `true`, and the parameter in `functionType` is `Int...`.
Inside Lyng function code, the collected `values` variable has type `List<Int>`.
If you already compiled the source, obtain the same information without resolving it again:
```kotlin
val script = Compiler.compile(source, Script.defaultImportManager)
val functions = script.resolvedFunctionMetadata()
```
Only top-level function declarations are returned. Use the Mini-AST APIs when tooling also needs
nested declarations or a syntax-oriented representation.
### 2.3) Prepare imports separately from execution
Low-level hosts that own a scope can install a compiled script's imports without executing its
statements:
```kotlin
val script = Compiler.compile(source, importManager)
val scope = importManager.newModule()
script.importInto(scope)
val result = script.execute(scope)
```
`importInto(...)` is idempotent for a scope and is useful when import preparation and execution
belong to different host lifecycle phases. To create a fresh raw module with the script's imports
already installed, use `script.instantiateModule(seedScope)`. When supplied, `seedScope` provides
the import provider and any seed-bound imports used by the new module.
### 3) Preferred: bind extern globals from Kotlin ### 3) Preferred: bind extern globals from Kotlin
For module-level APIs, the default workflow is: For module-level APIs, the default workflow is:
+1 -1
View File
@@ -27,6 +27,6 @@ See `docs/lyng_d_files.md` for `.lyng.d` syntax and examples.
- Alternatively, if/when the plugin is published to a marketplace, you will be able to install it - Alternatively, if/when the plugin is published to a marketplace, you will be able to install it
directly from the “Marketplace” tab (not yet available). directly from the “Marketplace” tab (not yet available).
### [Download plugin v0.0.2-SNAPSHOT](https://lynglang.com/distributables/lyng-idea-0.0.2-SNAPSHOT.zip) ### [Download plugin v0.0.5-SNAPSHOT](https://lynglang.com/distributables/lyng-idea-0.0.5-SNAPSHOT.zip)
Your ideas and bugreports are welcome on the [project gitea page](https://gitea.sergeych.net/SergeychWorks/lyng/issues) Your ideas and bugreports are welcome on the [project gitea page](https://gitea.sergeych.net/SergeychWorks/lyng/issues)
+166
View File
@@ -0,0 +1,166 @@
# lyng.io.html
For reading existing HTML, see [lyng.io.html.parse](lyng.io.html.parse.md).
`lyng.io.html` provides a pure Lyng HTML builder DSL. It uses Lyng context
receiver extensions, so text can be appended with `+"text"` inside tag blocks
without global builder state.
Host code installs the package from `lyngio` with `createHtmlModule(...)`:
```kotlin
val scope = Script.newScope()
createHtmlModule(scope.importManager)
```
Lyng code can then import it:
```lyng
import lyng.io.html
val page = html {
head {
title { +"Demo" }
}
body {
nav {
a(href: "/") { +"Home" }
}
h3 { +"Heading 3" }
p {
attr("data-id", 123)
+"Text is escaped: <safe>"
}
img(src: "/logo.png", alt: "Logo")
}
}
```
`html { ... }` returns a `String` beginning with `<!doctype html>`.
## Escaping
Text appended with unary `+` is HTML-escaped:
```lyng
html {
body {
p { +"Text & <more>" }
}
}
```
produces:
```html
<!doctype html><html><body><p>Text &amp; &lt;more&gt;</p></body></html>
```
Attribute values are escaped with HTML attribute rules:
```lyng
p {
attr("data-x", "\"quoted\" & <tag>")
+"content"
}
```
Use `raw(...)` only for trusted markup:
```lyng
div {
raw("<span>already escaped or trusted</span>")
}
```
## Tag Helpers
Current tag helpers cover common structural tags (`head`, `body`, `main`,
`section`, `article`, `header`, `footer`, `nav`, `div`, `span`, `p`), headings
(`h1` through `h6`), lists (`ul`, `ol`, `li`), and text/code tags (`strong`,
`em`, `code`, `pre`, `script`, `style`).
```lyng
body {
main {
section {
h2 { +"News" }
p { +"First item" }
}
}
}
```
Common void tags are also available: `meta`, `link`, `img`, `br`, and `input`.
```lyng
head {
meta { attr("charset", "utf-8") }
link {
attr("rel", "stylesheet")
attr("href", "/site.css")
}
}
```
## Attributes
Use `attr(name, value)` inside a tag block to set an escaped attribute value.
`id(...)` and `classes(...)` are small aliases:
```lyng
div {
id("root")
classes("app shell")
}
```
Use `flag(name)` for boolean attributes:
```lyng
input {
attr("type", "checkbox")
flag("checked")
}
```
## Convenience Helpers
Convenience helpers include `metaCharset()`, `stylesheet(href)`,
`a(href) { ... }`, `img(src, alt)`, and `input(type, name, value)`.
```lyng
head {
metaCharset()
stylesheet("/site.css")
}
body {
nav {
a(href: "/home") { +"Home" }
}
img(src: "/logo.png", alt: "Logo & mark")
input(type: "hidden", name: "token", value: "abc")
}
```
## Generic Elements
Use `tag(name) { ... }` and `voidTag(name) { ... }` for elements that do not
have dedicated helpers yet:
```lyng
body {
tag("custom-element") {
flag("hidden")
+"Secret"
}
voidTag("source") {
attr("srcset", "/image.webp")
attr("type", "image/webp")
}
}
```
These helpers are intentionally simple escape hatches. Prefer a dedicated helper
when one exists because it can encode safer defaults and clearer parameter names.
+41
View File
@@ -0,0 +1,41 @@
# lyng.io.html.parse
Offline HTML5 parsing backed by the core [Ksoup](https://github.com/fleeksoft/ksoup)
parser (no network extensions). Installed from `lyngio`:
```kotlin
createHtmlParseModule(scope.importManager)
```
```lyng
import lyng.io.html.parse
val doc = Html.parse("<title>News</title><main><p>Hello &amp; welcome</p><a href='/next'>Next</a></main>", "https://example.com/")
println(doc.title)
println(doc.text())
for (raw in doc.select("a[href]")) {
val link = raw as HtmlElement
println(link.text() + " => " + link.absUrl("href"))
}
```
`Html.parse(source, baseUrl = "")` accepts documents, fragments and malformed HTML.
It returns `HtmlDocument`, with `title: String`, `text(): String` and
`select(css): List<HtmlElement>`. Elements have `text()`, `attr(name)`,
`absUrl(name)` and `select(css)`. Missing selections return an empty list;
missing attributes or unresolvable absolute URLs return an empty string.
Invalid CSS selectors raise an error. `<base href>` participates in URL resolution.
Use the final fetched URL as the base when available.
Text extraction decodes entities, normalizes whitespace, preserves block and
`br` boundaries as newlines, and excludes `script`, `style`, `template`, `noscript`
and `[hidden]` subtrees. Selection retains the original DOM. This is readable
text extraction, not CSS rendering or article detection; navigation and cookie
banners may remain. It does not preserve preformatted whitespace.
Parsing never fetches resources or executes JavaScript. `absUrl` only resolves
URLs; it does not filter schemes. Fetch through the host's authorized HTTP API,
check status and content type, and choose bounded excerpts to return to an AI.
This API parses HTML only; XML parsing is separate future work.
Canonical declarations: `lyngio/stdlib/lyng/io/html_parse.lyng`.
+40 -11
View File
@@ -1,4 +1,4 @@
### lyng.io.http — HTTP/HTTPS client for Lyng scripts # lyng.io.http — HTTP/HTTPS client for Lyng scripts
This module provides a compact HTTP client API for Lyng scripts. It is implemented in `lyngio` and backed by Ktor on supported runtimes. This module provides a compact HTTP client API for Lyng scripts. It is implemented in `lyngio` and backed by Ktor on supported runtimes.
@@ -8,7 +8,7 @@ This module provides a compact HTTP client API for Lyng scripts. It is implement
--- ---
#### Add the library to your project (Gradle) ## Add the library to your project (Gradle)
If you use this repository as a multi-module project, add a dependency on `:lyngio`: If you use this repository as a multi-module project, add a dependency on `:lyngio`:
@@ -22,7 +22,7 @@ For external projects, ensure you also use the Lyng Maven repository described i
--- ---
#### Install the module into a Lyng session ## Install the module into a Lyng session
The HTTP module is not installed automatically. Install it into the session scope and provide a policy. The HTTP module is not installed automatically. Install it into the session scope and provide a policy.
@@ -42,9 +42,24 @@ suspend fun bootstrapHttp() {
} }
``` ```
Redirects are followed transparently, up to 10 hops by default. Hosts can set a different
limit when installing the module:
```kotlin
createHttpModule(
PermitAllHttpAccessPolicy,
scope,
LyngHttpConfig(maxRedirects = 5),
)
```
Exceeding the limit raises an `HTTP_REDIRECT_LIMIT_EXCEEDED` error containing the configured
limit, initial URL, and last redirect target. HTTPS-to-HTTP redirects remain disabled, and the
`Authorization` header is removed when a redirect changes authority.
--- ---
#### Using from Lyng scripts ## Using from Lyng scripts
Simple GET: Simple GET:
@@ -86,9 +101,9 @@ HTTPS GET:
--- ---
#### API reference ## API reference
##### `Http` (static methods) ### `Http` (static methods)
- `isSupported(): Bool` — Whether HTTP client support is available on the current runtime. - `isSupported(): Bool` — Whether HTTP client support is available on the current runtime.
- `request(req: HttpRequest): HttpResponse` — Execute a request described by a mutable request object. - `request(req: HttpRequest): HttpResponse` — Execute a request described by a mutable request object.
@@ -101,7 +116,7 @@ For convenience methods, `headers...` accepts:
- `MapEntry`, e.g. `"Accept" => "text/plain"` - `MapEntry`, e.g. `"Accept" => "text/plain"`
- 2-item lists, e.g. `["Accept", "text/plain"]` - 2-item lists, e.g. `["Accept", "text/plain"]`
##### `HttpRequest` ### `HttpRequest`
- `method: String` - `method: String`
- `url: String` - `url: String`
@@ -112,7 +127,7 @@ For convenience methods, `headers...` accepts:
Only one of `bodyText` and `bodyBytes` should be set. Only one of `bodyText` and `bodyBytes` should be set.
##### `HttpResponse` ### `HttpResponse`
- `status: Int` - `status: Int`
- `statusText: String` - `statusText: String`
@@ -122,7 +137,7 @@ Only one of `bodyText` and `bodyBytes` should be set.
Response body decoding is cached inside the response object. Response body decoding is cached inside the response object.
##### `HttpHeaders` ### `HttpHeaders`
`HttpHeaders` behaves like `Map<String, String>` for the first value of each header name and additionally exposes: `HttpHeaders` behaves like `Map<String, String>` for the first value of each header name and additionally exposes:
@@ -134,7 +149,7 @@ Header lookup is case-insensitive.
--- ---
#### Security policy ## Security policy
The module uses `HttpAccessPolicy` to authorize requests before they are sent. The module uses `HttpAccessPolicy` to authorize requests before they are sent.
@@ -170,7 +185,7 @@ val allowLocalOnly = object : HttpAccessPolicy {
--- ---
#### Platform support ## Platform support
- **JVM:** supported - **JVM:** supported
- **Android:** supported via the Ktor CIO client backend - **Android:** supported via the Ktor CIO client backend
@@ -179,3 +194,17 @@ val allowLocalOnly = object : HttpAccessPolicy {
- **Windows native:** supported via the Ktor WinHttp client backend - **Windows native:** supported via the Ktor WinHttp client backend
- **Apple native:** supported via the Ktor Darwin client backend - **Apple native:** supported via the Ktor Darwin client backend
- **Other targets:** may report unsupported; use `Http.isSupported()` before relying on it - **Other targets:** may report unsupported; use `Http.isSupported()` before relying on it
## Request failure diagnostics
Transport exceptions report the request method and target, a category, the
underlying exception/cause chain, and recovery advice. Query strings and userinfo
are redacted from the diagnostic target. Categories are `HTTP_TLS_FAILED`,
`HTTP_TIMEOUT`, `HTTP_DNS_FAILED`, `HTTP_CONNECTION_FAILED` and
`HTTP_REQUEST_FAILED`. Permission denials are `HTTP_ACCESS_DENIED`; redirect
limit errors retain `HTTP_REDIRECT_LIMIT_EXCEEDED`.
`HTTP_TLS_FAILED` means the handshake failed before an HTTP response. Do not
retry the same URL unchanged; use another source. A remote TLS fatal alert is
not a Lyng syntax error. Non-2xx responses remain ordinary response objects, and
request cancellation propagates rather than becoming a generic HTTP error.
+446
View File
@@ -0,0 +1,446 @@
# `lyng.io.http.server` - Minimal HTTP/1.1 And WebSocket Server
This module provides a small server-side HTTP API for Lyng scripts. It is implemented in `lyngio` on top of the existing TCP layer and is intended for embedded tools, local services, test fixtures, and lightweight app backends.
It supports:
- HTTP/1.1 request parsing
- keep-alive
- exact-path routing
- regex routing
- path-template routing with named parameters
- websocket upgrade and server-side sessions
It does not aim to replace a full reverse proxy. Typical deployment is behind nginx, Caddy, or another frontend that handles TLS and public-facing edge concerns.
> **Security note:** this module uses the same `NetAccessPolicy` capability model as raw TCP sockets. If scripts are allowed to listen on TCP, they can host an HTTP server.
## Install The Module Into A Lyng Session
Kotlin bootstrap example:
```kotlin
import net.sergeych.lyng.EvalSession
import net.sergeych.lyng.Scope
import net.sergeych.lyng.io.http.server.createHttpServerModule
import net.sergeych.lyngio.net.security.PermitAllNetAccessPolicy
suspend fun bootstrapHttpServer() {
val session = EvalSession()
val scope: Scope = session.getScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
session.eval("import lyng.io.http.server")
}
```
## RequestContext Sugar
Route handlers use `RequestContext` as the receiver, so inside handlers you normally write direct calls such as:
- `jsonBody<T>()`
- `respondJson(...)`
- `respondHtml { ... }`
- `respondText(...)`
- `setHeader(...)`
- `request.path`
- `routeParams["id"]`
This keeps ordinary HTTP endpoints compact and avoids passing an explicit request or exchange parameter through every route lambda.
## HTML Response Sugar
Use `respondHtml { ... }` to render an HTML document with the `lyng.io.html` DSL and send it as `text/html; charset=utf-8`.
```lyng
import lyng.io.http.server
import lyng.io.html
val server = HttpServer()
server.get("/") {
respondHtml {
head {
title { +"Lyng status" }
}
body {
h3 { +"Service is running" }
p { +"Path: ${request.path}" }
}
}
}
server.listen(8080, "127.0.0.1")
```
Pass `code:` when the route should return a non-200 status:
```lyng
server.get("/accepted") {
respondHtml(code: 202) {
body { h3 { +"Accepted" } }
}
}
```
## JSON API Sugar
For ordinary JSON APIs, `RequestContext` includes two primary helpers:
- `jsonBody<T>()` decodes the request body with typed `Json.decodeAs(...)`
- `respondJson(body, status = 200)` sets JSON content type and responds with plain `toJsonString()`
These helpers intentionally use ordinary JSON projection for HTTP interop, not canonical `Json.encode(...)`.
### Typed JSON POST With Route Params
```lyng
import lyng.io.http.server
closed class CreateResultRequest(title: String, score: Int)
closed class CreateResultResponse(id: String, userId: String, title: String, score: Int)
val server = HttpServer()
server.postPath("/api/users/{userId}/results") {
val req = jsonBody<CreateResultRequest>()
if (req.title.isBlank()) {
respondJson({ error: "title must not be empty" }, 400)
return
}
respondJson(
CreateResultResponse("r-101", routeParams["userId"], req.title, req.score),
201
)
}
server.listen(8080, "127.0.0.1")
```
### JSON Response With Route Params
```lyng
import lyng.io.http.server
val server = HttpServer()
server.getPath("/api/users/{id}") {
respondJson({
id: routeParams["id"],
path: request.path,
ok: true
})
}
server.listen(8080, "127.0.0.1")
```
## Request And Route Data
`ServerRequest` exposes parsed HTTP request data:
- `method: String`
- `target: String`
- `path: String`
- `pathParts: List<String>`
- `queryString: String?`
- `query: Map<String, String>`
- `headers: HttpHeaders`
- `body: Buffer`
`RequestContext` exposes routing context and response controls:
- `request: ServerRequest`
- `routeMatch: RegexMatch?`
- `routeParams: Map<String, String>`
- `jsonBody<T>()`
- `respond(...)`
- `respondText(...)`
- `respondJson(body, status = 200)`
- `respondHtml(code: 200) { ... }`
- `setHeader(...)`
- `addHeader(...)`
- `acceptWebSocket(...)`
For exact routes, `routeMatch` is `null` and `routeParams` is empty.
For regex routes, `routeMatch` is set and `routeParams` is empty.
For path-template routes, both `routeMatch` and `routeParams` are set.
## Reusable Routers
`Router` collects the same route kinds as `HttpServer`, but does not listen on sockets by itself.
Mount it into `HttpServer` or another `Router`.
```lyng
import lyng.io.http.server
val api = Router()
api.get("/health") {
respondText(200, "ok")
}
val users = Router()
users.getPath("/users/{id}") {
respondJson({ id: routeParams["id"] })
}
api.mount(users)
val server = HttpServer()
server.mount(api)
server.listen(8080, "127.0.0.1")
```
Mounted routers reuse the built-in server router. They are configuration-time composition, not an extra per-request Lyng dispatch layer.
## WebSocket Routes
You can route websocket upgrades by exact path, regex, or path template.
```lyng
server.ws("/chat") { ws ->
ws.sendText("hello")
ws.close()
}
server.wsPath("/ws/{room}") { ws ->
ws.sendText("room=" + routeParams["room"])
ws.close()
}
```
A websocket handler runs only for requests that actually ask for websocket upgrade. Ordinary HTTP requests to the same path are not treated as websocket sessions.
### Choosing Between `ws(...)` And `acceptWebSocket(...)`
Use `server.ws(...)` or `server.wsPath(...)` when the route is always a websocket endpoint.
Use `acceptWebSocket(...)` inside a normal HTTP handler when the same route may inspect the request first and then decide whether to upgrade.
```lyng
server.get("/maybe-upgrade") {
if (!request.isWebSocketUpgrade()) {
respondText(400, "websocket upgrade required")
return
}
acceptWebSocket { ws ->
ws.sendText("connected")
ws.close()
}
}
```
### Reading Incoming Messages
Inside a websocket handler, call `ws.receive()` to wait for the next application message.
What `receive()` returns:
- `WsMessage` for the next text or binary message.
- `null` after the client sends a close frame.
- `null` after the socket is already closed and no more frames can arrive.
What reaches Lyng code:
- Text frames become `WsMessage(isText = true, text = ...)`.
- Binary frames become `WsMessage(isText = false, data = ...)`.
- Fragmented websocket messages are reassembled before they are returned.
- Ping and pong control frames are handled internally and do not appear in Lyng.
- A client close frame is answered by the server close handshake, then `receive()` returns `null`.
Typical server receive loop:
```lyng
import lyng.buffer
server.ws("/echo") { ws ->
while (true) {
val msg = ws.receive() ?: break
if (msg.isText) {
ws.sendText("echo:" + msg.text)
} else {
ws.sendBytes(msg.data as Buffer)
}
}
}
```
### Sending Outgoing Messages
Use:
- `ws.sendText(text)` for text messages.
- `ws.sendBytes(data)` for binary messages.
Example:
```lyng
import lyng.buffer
server.ws("/push") { ws ->
ws.sendText("ready")
ws.sendBytes(Buffer(1, 2, 3))
ws.close()
}
```
Send behavior:
- Each call sends one websocket message.
- The server API does not expose frame-by-frame streaming.
- Once the session is closed, send calls fail with a websocket error.
### What Happens When The Connection Closes
There are three practical cases:
1. The client closes first.
The runtime replies with a close frame, releases the socket, and `receive()` returns `null`.
2. Your handler closes first with `ws.close(...)`.
The runtime sends a close frame and releases the socket locally.
3. The transport disappears unexpectedly.
The session is released and no more messages can be received; subsequent sends fail.
What Lyng code should do:
- Treat `receive() == null` as end-of-session.
- Exit the handler or break the receive loop at that point.
- Do not keep sending after close has been observed.
The current server-side API does not expose the peer close code or close reason to Lyng.
### Closing The Connection Yourself
Call `ws.close()` when you want to terminate the websocket session.
```lyng
server.ws("/chat") { ws ->
ws.sendText("server shutting down")
ws.close(1000, "done")
}
```
Close semantics:
- `close()` sends a websocket close frame with the given code and reason.
- Defaults are `code = 1000` and `reason = ""`.
- `close()` is idempotent; calling it again after close does nothing.
- After local close, the session should be treated as unusable.
- After close, `isOpen()` becomes false and further sends fail.
### WebSocket Handler Pattern
```lyng
import lyng.io.http.server
val server = HttpServer()
server.wsPath("/rooms/{room}") { ws ->
val room = routeParams["room"] ?: "<unknown>"
ws.sendText("joined:" + room)
while (true) {
val msg = ws.receive() ?: break
if (msg.isText) {
ws.sendText(room + ":" + msg.text)
}
}
ws.close()
}
server.listen(8080, "127.0.0.1")
```
## Path-Template Routes
Path templates are sugar on top of regex routes. Template parameters are exposed as decoded `routeParams`.
```lyng
server.getPath("/users/{userId}/posts/{postId}") {
respondText(
200,
routeParams["userId"] + ":" + routeParams["postId"]
)
}
```
Template rules:
- template must start with `/`
- a segment is either literal text or `{name}`
- parameter names must be valid identifiers
- parameter values match one path segment only
- parameter values use path decoding rules:
- valid percent-encoding is decoded
- `+` stays `+`
- malformed `%` stays literal
## Regex Routes
Regex routes match the whole request path, not a substring.
```lyng
server.get("^/users/([0-9]+)/posts/([0-9]+)$".re) {
val m = routeMatch!!
respondText(200, "user=" + m[1] + ", post=" + m[2])
}
```
## Basic Exact Route
```lyng
import lyng.io.http.server
val server = HttpServer()
server.get("/hello") {
setHeader("Content-Type", "text/plain")
respondText(200, "hello")
}
server.listen(8080, "127.0.0.1")
```
## Route Precedence
Dispatch order is:
1. exact method route
2. exact `any` route
3. regex method route, registration order
4. regex `any` route, registration order
5. fallback
This means exact routes stay fast and always win over template or regex routes for the same path.
## API Surface
### `Router` Route Registration Methods
- `get(path: String|Regex, handler)`
- `getPath(pathTemplate: String, handler)`
- `post(path: String|Regex, handler)`
- `postPath(pathTemplate: String, handler)`
- `put(path: String|Regex, handler)`
- `putPath(pathTemplate: String, handler)`
- `delete(path: String|Regex, handler)`
- `deletePath(pathTemplate: String, handler)`
- `any(path: String|Regex, handler)`
- `anyPath(pathTemplate: String, handler)`
- `ws(path: String|Regex, handler)`
- `wsPath(pathTemplate: String, handler)`
- `fallback(handler)`
- `mount(router)`
### `HttpServer` Route Registration Methods
- `get(path: String|Regex, handler)`
- `getPath(pathTemplate: String, handler)`
- `post(path: String|Regex, handler)`
- `postPath(pathTemplate: String, handler)`
- `put(path: String|Regex, handler)`
- `putPath(pathTemplate: String, handler)`
- `delete(path: String|Regex, handler)`
- `deletePath(pathTemplate: String, handler)`
- `any(path: String|Regex, handler)`
- `anyPath(pathTemplate: String, handler)`
- `ws(path: String|Regex, handler)`
- `wsPath(pathTemplate: String, handler)`
- `fallback(handler)`
- `mount(router)`
- `listen(port, host = null, backlog = 128)`
+267 -6
View File
@@ -3,6 +3,7 @@
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. 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. > **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.
> The reference `lyng`/`jlyng` CLI installs this module in its import manager, so CLI scripts can use `import lyng.io.process` directly.
--- ---
@@ -44,24 +45,241 @@ suspend fun bootstrapProcess() {
--- ---
#### Using from Lyng scripts #### Shell-script shorthand
Most CLI scripts should start with the shorthand API:
```lyng
import lyng.io.process
val branch = sh("git branch --show-current").out.trim()
println("Branch: " + branch)
```
`sh(command)` starts `command` through the platform shell and returns an active `CommandRun`.
The process is already running when the handle is returned. The handle gives you both convenient capture properties and streaming line flows:
```lyng
val run = sh("git status --short")
println(run.out) // capture all stdout as String
println(run.err) // capture all stderr as String
println(run.code) // known after out/err/wait/check, otherwise null
println(run.ok) // true, false, or null
```
Use `.check()` when a non-zero exit code should fail the script:
```lyng
sh("lyng fmt --check examples/*.lyng").check()
sh("git diff --check").check()
```
Use `.wait()` when the exit code is data and should not raise:
```lyng
val code = sh("git diff --quiet").wait()
if (code == 0) {
println("clean")
} else {
println("changed")
}
```
Use `.lines` for output that may be large. It streams stdout instead of buffering the entire output:
```lyng
for (file in sh("git ls-files").lines) {
if (file.endsWith(".lyng")) {
println(file)
}
}
```
You can combine it with normal iterable helpers:
```lyng
val count = sh("git ls-files").lines.count {
it.endsWith(".lyng")
}
println("Lyng files: " + count)
```
Use `exec(executable, args)` when arguments come from data, filenames, or user input:
```lyng
val file = ARGV[0] as String
exec("git", ["add", file]).check()
```
`exec(...)` bypasses the shell, so arguments are passed as argv entries rather than parsed by shell quoting rules.
```lyng
val status = exec("git", ["status", "--short"]).out
println(status)
```
---
#### Capture vs streaming
`CommandRun` is active: it owns a running process. Choose the consumption style intentionally.
- `.out` captures stdout into memory, waits for the process to finish, and also drains stderr concurrently.
- `.err` captures stderr into memory, waits for the process to finish, and also drains stdout concurrently.
- `.check()` captures both streams, waits, and fails if the exit code is non-zero.
- `.lines` streams stdout line by line and does not buffer the whole output.
- `.errorLines` streams stderr line by line and does not buffer the whole error output.
- `.wait()` only waits and returns the exit code.
Treat a `CommandRun` as a one-shot stream owner. Pick one stdout consumption path (`out` or `lines`) and one stderr consumption path (`err` or `errorLines`). Do not collect the same stream twice; it is backed by the process pipe.
For small output, capture is simplest:
```lyng
val version = sh("git describe --tags --always").out.trim()
```
For large output, stream:
```lyng
for (line in sh("find . -type f").lines) {
if (line.endsWith(".lyng")) {
println(line)
}
}
```
For commands that may write a lot to both stdout and stderr, consume both streams. One practical pattern is to collect stderr in a background task while processing stdout:
```lyng
val r = sh("some-command-that-writes-a-lot")
val errors: List<String> = []
val stderrTask = launch {
for (line in r.errorLines) {
errors.add(line)
}
}
for (line in r.lines) {
println("OUT: " + line)
}
val code = r.wait()
stderrTask.await()
if (code != 0) {
println(errors.joinToString("\n"))
exit(code)
}
```
As with ordinary shell programming, avoid using `.out` for unbounded output. It is meant for command results that comfortably fit in memory.
---
#### Common script patterns
Capture one small value:
```lyng
val currentCommit = exec("git", ["rev-parse", "--short", "HEAD"]).out.trim()
```
Run a command for its side effects and fail on error:
```lyng
exec("mkdir", ["-p", "build/reports"]).check()
sh("lyng fmt --check src/**/*.lyng").check()
```
Filter a long output stream:
```lyng
for (path in sh("git ls-files").lines) {
if (path.endsWith(".lyng")) {
println(path)
}
}
```
Branch on a command status:
```lyng
if (exec("git", ["diff", "--quiet"]).wait() != 0) {
println("working tree has changes")
}
```
Keep user-controlled text out of the shell:
```lyng
val file = ARGV[0] as String
exec("git", ["log", "--oneline", "--", file]).check()
```
---
#### `sh` vs `exec`
Use `sh(...)` when you intentionally want shell syntax:
```lyng
sh("git status --short | wc -l").out.trim()
sh("find . -name '*.lyng' | sort").check()
```
Use `exec(...)` when you already have structured arguments:
```lyng
exec("git", ["commit", "-m", message]).check()
exec("cp", [sourcePath, targetPath]).check()
```
This distinction matters for safety. Shell commands are parsed by `/bin/sh -c` on Unix-like systems or the platform shell on supported platforms. If you interpolate untrusted text into a shell string, it can change the command being run. Prefer `exec(...)` for user input and filenames.
---
#### Low-level process API
The shorthand API is built on top of `Process.execute(...)` and `Process.shell(...)`. Use the low-level API when you need direct process control, including explicit signals or forceful termination:
```lyng ```lyng
import lyng.io.process import lyng.io.process
// Execute a process with arguments // Execute a process with arguments
val p = Process.execute("ls", ["-l", "/tmp"]) val p = Process.execute("ls", ["-l", "/tmp"])
for (line in p.stdout) { for (line in p.stdout()) {
println("OUT: " + line) println("OUT: " + line)
} }
val exitCode = p.waitFor() val exitCode = p.waitFor()
println("Process exited with: " + exitCode) println("Process exited with: " + exitCode)
// Run a shell command // Run a shell command
val sh = Process.shell("echo 'Hello from shell' | wc -w") val shellProcess = Process.shell("echo 'Hello from shell' | wc -w")
for (line in sh.stdout) { for (line in shellProcess.stdout()) {
println("Word count: " + line.trim()) println("Word count: " + line.trim())
} }
```
Signals and termination:
```lyng
val server = Process.shell("python3 -m http.server 8080")
delay(1000)
server.signal("SIGTERM")
val code = server.waitFor()
println("server exited: " + code)
```
---
#### Platform information
```lyng
import lyng.io.process
// Platform information // Platform information
val details = Platform.details() val details = Platform.details()
@@ -77,15 +295,57 @@ if (Platform.isSupported()) {
--- ---
#### Executable script example
```lyng
#!/usr/bin/env lyng
import lyng.io.process
if (ARGV.size == 0) {
println("usage: changed-files.lyng <extension>")
exit(2)
}
val ext = ARGV[0] as String
var matches = 0
for (file in sh("git status --short").lines) {
if (file.endsWith(ext)) {
println(file)
matches++
}
}
println("matched: " + matches)
```
---
#### API Reference #### API Reference
##### `Process` (static methods) ##### `Process` (static methods)
- `execute(executable: String, args: List<String>): RunningProcess` — Start an external process. - `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`). - `shell(command: String): RunningProcess` — Run a command through the system shell (e.g., `/bin/sh` or `cmd.exe`).
##### Shorthand functions
- `sh(command: String): CommandRun` — Run a command through the system shell and return an active handle.
- `exec(executable: String, args: List<String> = []): CommandRun` — Run an executable with argv-style arguments and return an active handle.
##### `CommandRun`
- `command: String` — Original command display text.
- `lines: Flow<String>` — Streaming stdout lines. Use for large output.
- `errorLines: Flow<String>` — Streaming stderr lines. Use for large error output.
- `out: String` — Captured stdout. Captures both stdout and stderr concurrently and waits for completion.
- `err: String` — Captured stderr. Captures both stdout and stderr concurrently and waits for completion.
- `wait(): Int` — Wait for the command to exit and return the exit code.
- `check(): CommandRun` — Capture output, wait, and fail if the exit code is non-zero.
- `code: Int?` — Known exit code after `wait`, `check`, `out`, or `err`; otherwise `null`.
- `ok: Bool?` — `true` for exit code 0, `false` for non-zero, or `null` before the exit code is known.
##### `RunningProcess` (instance methods) ##### `RunningProcess` (instance methods)
- `stdout: Flow` — Standard output stream as a Lyng Flow of lines. - `stdout(): Flow` — Standard output stream as a Lyng Flow of lines.
- `stderr: Flow` — Standard error 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. - `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"`). - `signal(name: String)` — Send a signal to the process (e.g., `"SIGINT"`, `"SIGTERM"`, `"SIGKILL"`).
- `destroy()` — Forcefully terminate the process. - `destroy()` — Forcefully terminate the process.
@@ -110,6 +370,7 @@ Example of a restricted policy in Kotlin:
```kotlin ```kotlin
import net.sergeych.lyngio.fs.security.AccessDecision import net.sergeych.lyngio.fs.security.AccessDecision
import net.sergeych.lyngio.fs.security.AccessContext
import net.sergeych.lyngio.fs.security.Decision import net.sergeych.lyngio.fs.security.Decision
import net.sergeych.lyngio.process.security.ProcessAccessOp import net.sergeych.lyngio.process.security.ProcessAccessOp
import net.sergeych.lyngio.process.security.ProcessAccessPolicy import net.sergeych.lyngio.process.security.ProcessAccessPolicy
+189 -60
View File
@@ -1,4 +1,4 @@
### lyng.io.ws — WebSocket client for Lyng scripts # `lyng.io.ws` - WebSocket client for Lyng scripts
This module provides a compact WebSocket client API for Lyng scripts. It is implemented in `lyngio` and currently backed by Ktor WebSockets on the JVM. This module provides a compact WebSocket client API for Lyng scripts. It is implemented in `lyngio` and currently backed by Ktor WebSockets on the JVM.
@@ -6,11 +6,9 @@ This module provides a compact WebSocket client API for Lyng scripts. It is impl
> >
> **Shared type note:** `WsMessage` is also available from `lyng.io.ws.types` when host code wants the reusable message type without depending on the WebSocket client module itself. > **Shared type note:** `WsMessage` is also available from `lyng.io.ws.types` when host code wants the reusable message type without depending on the WebSocket client module itself.
--- ## Install The Module Into A Lyng Session
#### Install the module into a Lyng session Kotlin host bootstrap example:
Kotlin (host) bootstrap example:
```kotlin ```kotlin
import net.sergeych.lyng.EvalSession import net.sergeych.lyng.EvalSession
@@ -26,59 +24,189 @@ suspend fun bootstrapWs() {
} }
``` ```
--- ## Using From Lyng Scripts
#### Using from Lyng scripts ### Text Exchange
Simple text message exchange: ```lyng
import lyng.io.ws
import lyng.io.ws val ws = Ws.connect(WS_TEST_URL)
ws.sendText("ping")
val m: WsMessage = ws.receive()
ws.close()
[ws.url() == WS_TEST_URL, m.isText, m.text]
>>> [true,true,echo:ping]
```
val ws = Ws.connect(WS_TEST_URL) ### Binary Exchange
```lyng
import lyng.buffer
import lyng.io.ws
val ws = Ws.connect(WS_TEST_BINARY_URL)
ws.sendBytes(Buffer(9, 8, 7))
val m: WsMessage = ws.receive()
ws.close()
[m.isText, (m.data as Buffer).hex]
>>> [false,010203090807]
```
### Secure `wss` Exchange
```lyng
import lyng.io.ws
val ws = Ws.connect(WSS_TEST_URL)
ws.sendText("ping")
val m: WsMessage = ws.receive()
ws.close()
[ws.url() == WSS_TEST_URL, m.text]
>>> [true,secure:ping]
```
## Message Flow And Session Lifecycle
### Reading Incoming Messages
Call `ws.receive()` to wait for the next application message.
What `receive()` returns:
- `WsMessage` for the next text or binary message.
- `null` after the peer closes the connection cleanly.
- `null` after the transport has already been closed and no more messages can arrive.
What reaches Lyng code:
- Text frames are exposed as `WsMessage(isText = true, text = ...)`.
- Binary frames are exposed as `WsMessage(isText = false, data = ...)`.
- Fragmented websocket messages are reassembled before they are returned.
- Ping and pong control frames are handled internally and are not returned by `receive()`.
- Incoming close frames are handled internally; after that `receive()` returns `null`.
Typical receive loop:
```lyng
import lyng.buffer
import lyng.io.ws
val ws = Ws.connect(WS_URL)
while (true) {
val msg = ws.receive() ?: break
if (msg.isText) {
println("text=" + msg.text)
} else {
println("bytes=" + ((msg.data as Buffer).size))
}
}
println("peer closed the websocket")
```
### Sending Outgoing Messages
Use:
- `ws.sendText(text)` for UTF-8 text messages.
- `ws.sendBytes(data)` for binary messages.
Example:
```lyng
import lyng.buffer
import lyng.io.ws
val ws = Ws.connect(WS_URL)
ws.sendText("hello")
ws.sendBytes(Buffer(1, 2, 3, 4))
```
Send behavior:
- Each call sends one websocket message.
- The API does not expose partial-frame streaming; send the whole message in one call.
- If the session is already closed, `sendText(...)` and `sendBytes(...)` fail with a websocket error.
- If the transport breaks during send, the session is released and the send call fails.
### Detecting Closed Connections
Use both signals together:
- `ws.isOpen()` tells you whether the session is still considered open right now.
- `ws.receive() == null` tells you the receive side has reached the end of the websocket session.
Practical rule:
- If `receive()` returns `null`, stop reading and treat the session as closed.
- After close has been observed, do not attempt further sends.
The API does not currently expose the peer close code or close reason to Lyng code.
### Closing The Connection Yourself
Call `ws.close()` when you are done.
```lyng
import lyng.io.ws
val ws = Ws.connect(WS_URL)
ws.sendText("bye")
ws.close(1000, "done")
```
Close semantics:
- `close()` sends a websocket close frame with the given code and reason.
- Defaults are `code = 1000` and `reason = ""`.
- After `close()`, the session is released locally and should be treated as closed immediately.
- Calling `close()` on an already closed session is a no-op.
- After local close, `receive()` returns `null` and further sends fail.
### Recommended Usage Pattern
For request-response style exchanges:
```lyng
import lyng.io.ws
val ws = Ws.connect(WS_URL)
try {
ws.sendText("ping") ws.sendText("ping")
val m: WsMessage = ws.receive() val reply = ws.receive() ?: error("socket closed before reply")
println(reply.text)
} finally {
ws.close() ws.close()
[ws.url() == WS_TEST_URL, m.isText, m.text] }
>>> [true,true,echo:ping] ```
Binary message exchange: For long-lived consumers:
import lyng.buffer ```lyng
import lyng.io.ws import lyng.io.ws
val ws = Ws.connect(WS_TEST_BINARY_URL) val ws = Ws.connect(WS_URL)
ws.sendBytes(Buffer(9, 8, 7))
val m: WsMessage = ws.receive() try {
while (true) {
val msg = ws.receive() ?: break
if (msg.isText) {
println(msg.text)
}
}
} finally {
ws.close() ws.close()
[m.isText, (m.data as Buffer).hex] }
>>> [false,010203090807] ```
Secure websocket (`wss`) exchange: ## API Reference
import lyng.io.ws ### `Ws`
val ws = Ws.connect(WSS_TEST_URL) - `isSupported(): Bool` - whether WebSocket client support is available on the current runtime.
ws.sendText("ping") - `connect(url: String, headers...): WsSession` - open a client websocket session.
val m: WsMessage = ws.receive()
ws.close()
[ws.url() == WSS_TEST_URL, m.text]
>>> [true,secure:ping]
---
#### API reference
##### `Ws` (static methods)
- `isSupported(): Bool` — Whether WebSocket client support is available on the current runtime.
- `connect(url: String, headers...): WsSession` — Open a client websocket session.
`headers...` accepts: `headers...` accepts:
- `MapEntry`, for example `"Authorization" => "Bearer x"`
- 2-item lists, for example `["Authorization", "Bearer x"]`
- `MapEntry`, e.g. `"Authorization" => "Bearer x"` ### `WsSession`
- 2-item lists, e.g. `["Authorization", "Bearer x"]`
##### `WsSession`
- `isOpen(): Bool` - `isOpen(): Bool`
- `url(): String` - `url(): String`
@@ -87,24 +215,27 @@ Secure websocket (`wss`) exchange:
- `receive(): WsMessage?` - `receive(): WsMessage?`
- `close(code: Int = 1000, reason: String = ""): void` - `close(code: Int = 1000, reason: String = ""): void`
`receive()` returns `null` after a clean close. Behavior summary:
- `receive()` returns `null` after close.
- `close()` is safe to call more than once.
- send operations require an open session.
##### `WsMessage` ### `WsMessage`
- `isText: Bool` - `isText: Bool`
- `text: String?` - `text: String?`
- `data: Buffer?` - `data: Buffer?`
Text messages populate `text`; binary messages populate `data`. Payload rules:
- Text messages populate `text` and leave `data == null`.
- Binary messages populate `data` and leave `text == null`.
--- ## Security Policy
#### Security policy
The module uses `WsAccessPolicy` to authorize websocket operations. The module uses `WsAccessPolicy` to authorize websocket operations.
- `WsAccessPolicy` — interface for custom policies - `WsAccessPolicy` - interface for custom policies.
- `PermitAllWsAccessPolicy` — allows all websocket operations - `PermitAllWsAccessPolicy` - allows all websocket operations.
- `WsAccessOp.Connect(url)` - `WsAccessOp.Connect(url)`
- `WsAccessOp.Send(url, bytes, isText)` - `WsAccessOp.Send(url, bytes, isText)`
- `WsAccessOp.Receive(url)` - `WsAccessOp.Receive(url)`
@@ -137,14 +268,12 @@ val allowLocalOnly = object : WsAccessPolicy {
} }
``` ```
--- ## Platform Support
#### Platform support - **JVM:** supported.
- **Android:** supported via the Ktor CIO websocket client backend.
- **JVM:** supported - **JS:** supported via the Ktor JS websocket client backend.
- **Android:** supported via the Ktor CIO websocket client backend - **Linux native:** supported via the Ktor Curl websocket client backend.
- **JS:** supported via the Ktor JS websocket client backend - **Windows native:** supported via the Ktor WinHttp websocket client backend.
- **Linux native:** supported via the Ktor Curl websocket client backend - **Apple native:** supported via the Ktor Darwin websocket client backend.
- **Windows native:** supported via the Ktor WinHttp websocket client backend - **Other targets:** may report unsupported; use `Ws.isSupported()` before relying on websocket client access.
- **Apple native:** supported via the Ktor Darwin websocket client backend
- **Other targets:** may report unsupported; use `Ws.isSupported()` before relying on websocket client access
+10
View File
@@ -4,6 +4,7 @@ The Lyng CLI is the reference command-line tool for the Lyng language. It lets y
- Run Lyng scripts from files or inline strings (shebangs accepted) - Run Lyng scripts from files or inline strings (shebangs accepted)
- Use standard argument passing (`ARGV`) to your scripts. - Use standard argument passing (`ARGV`) to your scripts.
- Import `lyng.io.process` for shell-script style process execution (`sh`, `exec`, and `CommandRun`).
- Resolve local file imports from the executed script's directory tree. - Resolve local file imports from the executed script's directory tree.
- Format Lyng source files via the built-in `fmt` subcommand. - Format Lyng source files via the built-in `fmt` subcommand.
- Register synchronous process-exit handlers with `atExit(...)`. - Register synchronous process-exit handlers with `atExit(...)`.
@@ -81,6 +82,15 @@ lyng -- -my-script.lyng arg1 arg2
lyng -x "println(\"Hello\")" more args lyng -x "println(\"Hello\")" more args
``` ```
The CLI installs `lyng.io.process`, so scripts can use the shell/process shorthand after importing it:
```lyng
import lyng.io.process
val branch = sh("git branch --show-current").out.trim()
exec("git", ["status", "--short"]).check()
```
- Print version/help: - Print version/help:
``` ```
+3 -1
View File
@@ -14,9 +14,10 @@
- **[lyng.io.db](lyng.io.db.md):** Portable SQL database access. Provides `Database`, `SqlTransaction`, `ResultSet`, SQLite support through `lyng.io.db.sqlite`, and JVM JDBC support through `lyng.io.db.jdbc`. - **[lyng.io.db](lyng.io.db.md):** Portable SQL database access. Provides `Database`, `SqlTransaction`, `ResultSet`, SQLite support through `lyng.io.db.sqlite`, and JVM JDBC support through `lyng.io.db.jdbc`.
- **[lyng.io.fs](lyng.io.fs.md):** Async filesystem access. Provides the `Path` class for file/directory operations, streaming, and globbing. - **[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. - **[lyng.io.process](lyng.io.process.md):** External process execution and shell commands. Provides shell-script sugar (`sh`, `exec`, `CommandRun`), low-level `Process`/`RunningProcess`, and `Platform` information.
- **[lyng.io.console](lyng.io.console.md):** Rich console/TTY access. Provides `Console` capability detection, geometry, output, and iterable events. - **[lyng.io.console](lyng.io.console.md):** Rich console/TTY access. Provides `Console` capability detection, geometry, output, and iterable events.
- **[lyng.io.http](lyng.io.http.md):** HTTP/HTTPS client access. Provides `Http`, `HttpRequest`, `HttpResponse`, and `HttpHeaders`. - **[lyng.io.http](lyng.io.http.md):** HTTP/HTTPS client access. Provides `Http`, `HttpRequest`, `HttpResponse`, and `HttpHeaders`.
- **[lyng.io.http.server](lyng.io.http.server.md):** Minimal HTTP/1.1 and WebSocket server. Provides `HttpServer`, `Router`, `ServerRequest`, `RequestContext`, and `ServerWebSocket`.
- **[lyng.io.ws](lyng.io.ws.md):** WebSocket client access. Provides `Ws`, `WsSession`, and `WsMessage`. - **[lyng.io.ws](lyng.io.ws.md):** WebSocket client access. Provides `Ws`, `WsSession`, and `WsMessage`.
- **[lyng.io.net](lyng.io.net.md):** Transport networking. Provides `Net`, `TcpSocket`, `TcpServer`, `UdpSocket`, and `SocketAddress`. - **[lyng.io.net](lyng.io.net.md):** Transport networking. Provides `Net`, `TcpSocket`, `TcpServer`, `UdpSocket`, and `SocketAddress`.
- **Shared networking type packages:** `lyng.io.http.types`, `lyng.io.ws.types`, and `lyng.io.net.types` expose reusable value types such as `HttpHeaders`, `WsMessage`, `IpVersion`, `SocketAddress`, and `Datagram` when host code wants type-only imports without installing the corresponding capability object module. - **Shared networking type packages:** `lyng.io.http.types`, `lyng.io.ws.types`, and `lyng.io.net.types` expose reusable value types such as `HttpHeaders`, `WsMessage`, `IpVersion`, `SocketAddress`, and `Datagram` when host code wants type-only imports without installing the corresponding capability object module.
@@ -120,6 +121,7 @@ For more details, see the specific module documentation:
- [Process Security Details](lyng.io.process.md#security-policy) - [Process Security Details](lyng.io.process.md#security-policy)
- [Console Module Details](lyng.io.console.md) - [Console Module Details](lyng.io.console.md)
- [HTTP Module Details](lyng.io.http.md) - [HTTP Module Details](lyng.io.http.md)
- [HTTP Server Module Details](lyng.io.http.server.md)
- [Transport Networking Details](lyng.io.net.md) - [Transport Networking Details](lyng.io.net.md)
- [WebSocket Module Details](lyng.io.ws.md) - [WebSocket Module Details](lyng.io.ws.md)
+50
View File
@@ -0,0 +1,50 @@
class Tag(name: String) {
val name = name
var inner = ""
fun child(tagName: String, block: Tag.()->void) {
val child = Tag(tagName)
with(child) { block(this) }
inner += child.render()
}
fun head(block: Tag.()->void) { child("head", block) }
fun body(block: Tag.()->void) { child("body", block) }
fun title(block: Tag.()->void) { child("title", block) }
fun h1(block: Tag.()->void) { child("h1", block) }
fun addText(text: String) {
inner += text
}
fun render() {
"<" + name + ">" + inner + "</" + name + ">"
}
}
context(Tag)
fun String.unaryPlus() {
this@Tag.addText(this)
}
fun html(block: Tag.()->void) {
val root = Tag("html")
with(root) { block(this) }
root.render()
}
val page = html {
head {
title {
+"Demo"
}
}
body {
h1 {
+"Heading 1"
}
}
}
println(page)
assertEquals("<html><head><title>Demo</title></head><body><h1>Heading 1</h1></body></html>", page)
+8
View File
@@ -1,6 +1,9 @@
// Sample: Operator Overloading in Lyng // Sample: Operator Overloading in Lyng
class Vector<T>(val x: T, val y: T) { class Vector<T>(val x: T, val y: T) {
// Overload unary +
fun unaryPlus() = this
// Overload + // Overload +
fun plus(other: Vector<U>) = Vector(x + other.x, y + other.y) fun plus(other: Vector<U>) = Vector(x + other.x, y + other.y)
@@ -28,6 +31,11 @@ val v2 = Vector(5, 5)
println("v1: " + v1) println("v1: " + v1)
println("v2: " + v2) println("v2: " + v2)
// Test unary +
val v0 = +v1
println("+v1 = " + v0)
assertEquals(Vector(10, 20), v0)
// Test binary + // Test binary +
val v3 = v1 + v2 val v3 = v1 + v2
println("v1 + v2 = " + v3) println("v1 + v2 = " + v3)
+34
View File
@@ -352,6 +352,40 @@ Sets `this` to the first argument and executes the block. Returns the value retu
assertEquals(3, sum) assertEquals(3, sum)
>>> void >>> void
Receiver lambdas can also keep outer receivers in scope. The primary receiver wins for unqualified lookup, and `this@Type`
selects an outer receiver explicitly:
class Html { fun lang() = "en" }
class Body { fun lang() = "body" }
fun html(block: Html.()->String) = with(Html()) { block(this) }
fun body(block: Body.()->String) = with(Body()) { block(this) }
val result = html {
body {
lang() + ":" + this@Html.lang()
}
}
assertEquals("body:en", result)
>>> void
You can declare the same requirement in a function type:
val block: context(Html) Body.()->String = {
lang() + ":" + this@Html.lang()
}
If the primary receiver does not define a member and multiple outer/context receivers do, Lyng reports an ambiguity instead of picking one silently:
class A { fun title() = "a" }
class B { fun title() = "b" }
class C
val block: context(A, B) C.()->String = {
// title() // compile-time ambiguity
this@A.title()
}
## run ## run
Executes a block after it returning the value passed by the block. for example, can be used with elvis operator: Executes a block after it returning the value passed by the block. for example, can be used with elvis operator:
+116 -8
View File
@@ -1,16 +1,17 @@
# What's New in Lyng # What's New in Lyng
This document highlights the current Lyng release, **1.5.5**, and the broader additions from the 1.5 cycle. This document highlights the current Lyng release, **1.5.9**, and the broader additions from the 1.5 cycle.
It is intentionally user-facing: new language features, new modules, new tools, and the practical things you can build with them. It is intentionally user-facing: new language features, new modules, new tools, and the practical things you can build with them.
For a programmer-focused migration summary across 1.5.x, see `docs/whats_new_1_5.md`. For a programmer-focused migration summary across 1.5.x, see `docs/whats_new_1_5.md`.
## Release 1.5.5 Highlights ## Release 1.5.9 Highlights
- `1.5.5` extends the 1.5 line with practical database APIs, first-class calendar dates, and better coroutine building blocks. - `1.5.9` extends the 1.5 line with process execution helpers, a lightweight HTTP server, HTML rendering helpers, and better DSL support.
- The 1.5 line now brings together richer ranges and loops, interpolation, math modules, immutable and observable collections, richer `lyngio`, and much better CLI/IDE support. - The 1.5 line now brings together richer ranges and loops, interpolation, math modules, immutable and observable collections, richer `lyngio`, and much better CLI/IDE support.
- `1.5.5` adds `Channel`, `LaunchPool`, and `joinAll()` so coroutine-heavy scripts can coordinate work more directly. - `1.5.9` adds `lyng.io.process` with `sh(...)`, `exec(...)`, and `CommandRun` for shell scripting and external process control.
- `1.5.5` adds `Date`, the portable `lyng.io.db` layer, SQLite/JDBC providers, and a compatibility `lyng.legacy_digest` module. - `1.5.9` adds the `lyng.io.http.server` module for exact, regex, and path-template routing, JSON helpers, WebSocket routes, and `respondHtml { ... }`.
- `1.5.5` also continues runtime/compiler hardening with better import dispatch, faster exact lambda calls, and correct `val +=`/`-=` behavior for mutating types versus real reassignment. - `1.5.9` adds the pure Lyng `lyng.io.html` builder DSL and language support for receiver-stack function types and context receiver extensions.
- `1.5.9` expands serialization with canonical `Json.encode(...)` / `Json.decode(...)` and typed `Json.encodeAs(...)` / `Json.decodeAs(...)`.
- The docs, homepage samples, and release metadata now point at the current stable version. - The docs, homepage samples, and release metadata now point at the current stable version.
## User Highlights Across 1.5.x ## User Highlights Across 1.5.x
@@ -22,11 +23,118 @@ For a programmer-focused migration summary across 1.5.x, see `docs/whats_new_1_5
- Calendar `Date` support in `lyng.time` - Calendar `Date` support in `lyng.time`
- `Channel`, `LaunchPool`, and `joinAll()` for coroutine workflows - `Channel`, `LaunchPool`, and `joinAll()` for coroutine workflows
- Immutable collections and opt-in `ObservableList` - Immutable collections and opt-in `ObservableList`
- Rich `lyngio` modules for SQLite/JDBC databases, console, HTTP, WebSocket, TCP, and UDP - Rich `lyngio` modules for SQLite/JDBC databases, process execution, console, HTTP, HTTP servers, WebSocket, TCP, UDP, and HTML rendering
- Legacy SHA-1 compatibility helpers in `lyng.legacy_digest` - Legacy SHA-1 compatibility helpers in `lyng.legacy_digest`
- CLI improvements including the built-in formatter `lyng fmt` - CLI improvements including the built-in formatter `lyng fmt`
- Better IDE support and stronger docs around the released feature set - Better IDE support and stronger docs around the released feature set
## New in 1.5.9
### Process Execution (`lyng.io.process`)
Lyng scripts can now run external commands through a coroutine-friendly process API.
Use `sh(...)` for shell commands and `exec(...)` when command arguments come from data and should bypass shell parsing.
```lyng
import lyng.io.process
val branch = sh("git branch --show-current").out.trim()
println("Branch: " + branch)
exec("git", ["status", "--short"]).check()
for (file in sh("git ls-files").lines) {
if (file.endsWith(".lyng")) {
println(file)
}
}
```
`CommandRun` supports captured output (`out`, `err`), streaming line output (`lines`, `errorLines`), `wait()`, `check()`, and status helpers.
See [lyng.io.process](lyng.io.process.md).
### HTTP Server Routes and HTML Responses
`lyng.io.http.server` now provides a compact HTTP/1.1 server API for embedded tools, local services, test fixtures, and lightweight app backends.
Routes can match exact paths, regexes, or path templates with named parameters.
Handlers run with `RequestContext` as the receiver, so request data and response helpers are available directly.
```lyng
import lyng.io.http.server
val server = HttpServer()
server.getPath("/api/users/{id}") {
respondJson({
id: routeParams["id"],
path: request.path,
ok: true
})
}
server.listen(8080, "127.0.0.1")
```
The server also supports reusable routers, JSON body decoding with `jsonBody<T>()`, WebSocket routes, and `respondHtml { ... }`.
See [lyng.io.http.server](lyng.io.http.server.md).
### HTML Builder DSL (`lyng.io.html`)
`lyng.io.html` provides a pure Lyng HTML builder that escapes text and attributes by default.
It is designed for server-side rendering, generated reports, and small embedded UI surfaces.
```lyng
import lyng.io.html
val page = html {
head {
title { +"Demo" }
}
body {
nav {
a(href: "/") { +"Home" }
}
p { +"Text is escaped: <safe>" }
}
}
```
When used with the HTTP server, `respondHtml { ... }` renders the same DSL directly as a `text/html` response.
See [lyng.io.html](lyng.io.html.md).
### Receiver-Stack Function Types and Context Extensions
Lyng now supports receiver-stack function types such as `context(Html) Body.()->String`.
Nested receiver lambdas keep outer receivers in scope, `this@Type` can select a specific receiver, and ambiguous receiver lookup is reported at compile time.
```lyng
class Html { fun htmlOnly() = "html" }
class Body { fun bodyOnly() = "body" }
val block: context(Html) Body.()->String = {
this@Html.htmlOnly() + ":" + bodyOnly()
}
```
Context receiver extensions let APIs expose helpers only when the right implicit receiver is already active.
This is what makes HTML builder code like `+"text"` work inside tag blocks without global builder state.
See [Object Oriented Programming](OOP.md#receiver-stack-lambdas).
### Canonical and Typed JSON Serialization
The serialization layer now has separate APIs for ordinary JSON projection, self-describing canonical JSON, and schema-guided typed canonical JSON.
```lyng
import lyng.serialization
closed class Point(x: Int, y: Int)
closed class Segment(a: Point, b: Point)
val value = Segment(Point(0, 1), Point(2, 3))
val encoded = Json.encodeAs(Segment, value)
assertEquals("{\"a\":{\"x\":0,\"y\":1},\"b\":{\"x\":2,\"y\":3}}", encoded)
assertEquals(value, Json.decodeAs(Segment, encoded))
```
Use `toJson()` / `toJsonString()` for ordinary JSON interop, `Json.encode(...)` / `Json.decode(...)` for Lyng-to-Lyng round trips without a schema, and `Json.encodeAs(...)` / `Json.decodeAs(...)` when both sides already know the type.
See [Json support](json_and_kotlin_serialization.md).
## Language Features ## Language Features
### Descending Ranges and Loops ### Descending Ranges and Loops
@@ -327,7 +435,7 @@ Singleton objects are declared using the `object` keyword. They provide a conven
```lyng ```lyng
object Config { object Config {
val version = "1.5.6-SNAPSHOT" val version = "1.5.9"
fun show() = println("Config version: " + version) fun show() = println("Config version: " + version)
} }
+19
View File
@@ -0,0 +1,19 @@
import lyng.io.http.server
closed class CreateUserRequest(name: String, age: Int)
closed class CreateUserResponse(id: Int, name: String, age: Int)
val server = HttpServer()
server.postPath("/api/users") {
val req = jsonBody<CreateUserRequest>()
if (req.name.isBlank()) {
respondJson({ error: "name must not be empty" }, 400)
return
}
respondJson(CreateUserResponse(101, req.name, req.age), 201)
}
server.listen(8080, "127.0.0.1")
+1 -6
View File
@@ -636,13 +636,10 @@ if (!Console.isSupported()) {
} }
} }
var inputRunning = true
launch { launch {
while (inputRunning) { while (true) {
try { try {
for (ev in Console.events()) { for (ev in Console.events()) {
if (!inputRunning) break
// Isolate per-event failures so one bad event does not unwind the stream. // Isolate per-event failures so one bad event does not unwind the stream.
try { try {
if (ev is ConsoleKeyEvent) { if (ev is ConsoleKeyEvent) {
@@ -675,7 +672,6 @@ if (!Console.isSupported()) {
} }
} catch (err: Object) { } catch (err: Object) {
// Recover stream-level failures by recreating event stream in next loop turn. // Recover stream-level failures by recreating event stream in next loop turn.
if (!inputRunning) break
logError("Input stream recovered after error", err) logError("Input stream recovered after error", err)
Console.setRawMode(true) Console.setRawMode(true)
delay(50) delay(50)
@@ -803,7 +799,6 @@ if (!Console.isSupported()) {
delay(FRAME_DELAY_MS) delay(FRAME_DELAY_MS)
} }
} finally { } finally {
inputRunning = false
Console.setRawMode(false) Console.setRawMode(false)
Console.setCursorVisible(true) Console.setCursorVisible(true)
Console.leaveAltScreen() Console.leaveAltScreen()
+2 -1
View File
@@ -16,7 +16,8 @@
# #
#Gradle #Gradle
org.gradle.jvmargs=-Xmx4096M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M" org.gradle.jvmargs=-Xmx4096M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M" --add-opens=java.base/java.lang=ALL-UNNAMED
org.gradle.java.home=/usr/lib/jvm/java-17-openjdk-amd64
org.gradle.caching=true org.gradle.caching=true
org.gradle.configuration-cache=true org.gradle.configuration-cache=true
#Kotlin #Kotlin
+3 -1
View File
@@ -2,7 +2,7 @@
agp = "8.5.2" agp = "8.5.2"
clikt = "5.0.3" clikt = "5.0.3"
mordant = "3.0.2" mordant = "3.0.2"
kotlin = "2.3.20" kotlin = "2.4.0"
android-minSdk = "24" android-minSdk = "24"
android-compileSdk = "34" android-compileSdk = "34"
kotlinx-coroutines = "1.10.2" kotlinx-coroutines = "1.10.2"
@@ -58,4 +58,6 @@ hikaricp = { module = "com.zaxxer:HikariCP", version.ref = "hikaricp" }
[plugins] [plugins]
androidLibrary = { id = "com.android.library", version.ref = "agp" } androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlinCompose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
vanniktech-mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.29.0" } vanniktech-mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.29.0" }
+74
View File
@@ -0,0 +1,74 @@
### Ticket 1 — Name resolution: a member function named like its class silently shadows the primary constructor inside the class body
**Summary.** `class` bodies accept a member function whose name equals the class name. That member then wins name resolution for **all unqualified calls inside the class body**, including `init` blocks, method bodies and nested lambdas. The primary constructor becomes unreachable from inside the class, with no escape hatch. Compiles cleanly, breaks at runtime.
**Repro**
```lyng
class C1(x) {
fun C1(y): String = "member " + y.toString()
init { println(C1(9)) } // -> "member C1(9)", NOT the constructor
}
class C2(x) {
fun C2(y, z): String = y.toString() + z.toString()
fun probe(): String = C2(7, 8) // member chosen although ctor takes 1 arg
}
class Fact(x) {
fun Fact(y): Fact = Fact(y * 2) // compiles; unqualified Fact(...) = itself
}
Fact(1).Fact(3) // java.lang.StackOverflowError
```
Local functions shadow too:
```lyng
class M3(x) {
fun probe(y): String {
fun M3(z): String = "local " + z.toString()
return M3(y) // local member wins, ctor unreachable
}
}
```
No workaround found:
```lyng
class Q1(x) {
fun Q1(y): String = "member"
fun a(): String = Q1.Q1(3) // IllegalArgumentException: expected 1 arguments, got 0
fun b(): String = (Q1(5) as Q1).Q1(6) // ClassCastException: Cannot cast String to Q1
}
class G1(x) { fun g(): G1 = this(x + 1) } // Exception: operation is not implemented
```
Related symptom: adding `Type::class` in a class that also has a same-name member turns the **declaration** itself into a hard error pointing at the wrong place:
```lyng
class D16(x) {
fun D16(y): String = "member " + y.toString() // Error: expected 1 arguments, got 0
fun lit(): String = D16::class.toString()
}
```
Control cases compile fine: same-name member alone ✔, `::class` alone ✔, the combination ✘.
**Expected.** Either (a) reject a member whose name collides with the class name at compile time (preferred — it is never a constructor in Lyng, there are no secondary constructors), or (b) keep constructor calls resolvable for the primary arity and fall back to members only for non-matching arity, plus a working `this(...)`/`this@Type(...)` delegation. In any case `Type::class` must not be affected by a same-name member, and diagnostics must point at the actual construct.
**Suggested tests.** `fun SameName(...)` declared in a class with the same name → expected compile error; self-recursive factory → must not reach runtime stack overflow; `Type::class` inside such a class → must compile; `this(...)` → either implement or emit a clear compile-time error instead of `operation is not implemented`.
## Resolution
Valid: direct instance/static member functions named after their enclosing class
are now rejected at the function name. This also rejects the collision before
any `::class` expression can produce a misleading downstream diagnostic.
The supported control is `Type::class` in a class without the illegal member;
the suggested test asking the illegal declaration to compile contradicts the
report's preferred resolution.
Direct `this(...)` / `this@Type(...)` calls on the enclosing class instance now
report unsupported constructor delegation during compilation. Use `Type(...)`
to construct another instance; no secondary constructors were introduced.
Local functions shadowing class names are ordinary lexical shadowing, not a bug,
and remain supported. Regression coverage: `ClassNameMemberRegressionTest`.
Before the fix, 2 of its 4 tests failed; the constructor/class-literal and local
shadowing controls passed.
Validation: all 4 focused JVM regression tests passed, followed by the full
`:lynglib:jvmTest` suite (benchmarks excluded by the repository default).
+65
View File
@@ -0,0 +1,65 @@
### Ticket 2 — Static member calls: declared return type is not propagated, receiver becomes `Object`
**Summary.** The **invocation** of a `static` member (class or `object` singleton) loses its declared return type at the call site; the result is treated as a general receiver, so every type-specific member access fails to compile. The runtime value is correct — this is purely a static-typing defect. Constructor calls, instance-method calls and top-level function calls all preserve the declared type correctly, so the inconsistency is specific to static members.
**Repro**
```lyng
class S(x) {
static fun make(y): S = S(y) // return type is explicitly declared
fun hi(): String = "hi"
}
val a: S = S.make(1) // OK — annotation rescues it
println(a.hi())
val b = S.make(2) // b is S == true at runtime
println(b.hi()) // Error: member access requires compile-time receiver type: hi
println(S.make(1).hi())// Error: same, chained call
println((S.make(3) as S).hi()) // OK — explicit cast rescues it
fun need(v: S) = v.hi(); need(S.make(4)) // OK — typed argument position rescues it
```
Not limited to self-returning factories — any declared return type is dropped:
```lyng
class K(x) { static fun text(y): String = "t"; static fun num(y): Int = y }
val s = K.text(1); println(s.length) // Error: ... compile-time receiver type: length
val n = K.num(1); println(n + 1) // same class of failure
object O { static fun text(y): String = "o" }
val o = O.text(1); println(o.length) // Error: ... on O.text result
class B(x) { static fun noRet(y) = B(y) } // untyped static: also general
class Box<T>(val value: T) { static fun of<T>(v: T): Box<T> = Box(v) }
val bx = Box.of(42); println(bx.value) // Error; val bx2: Box<Int> = Box.of(43) works
```
Controls that **do** work correctly: `val d = K8(9); d.hi()` (constructor), `val c = S(3).again(4); c.hi()` (instance method), `val t = topLevelMake(1); t.x` (top-level function).
**Expected.** `StaticClass.member(args)` must produce the declared return type as the static compile-time receiver type, exactly like instance and top-level calls. Only genuinely untyped statics (`fun` without a return type and uninferrable bodies) should fall back to `Object`/`Obj`.
**Suggested tests.** Matrix over `{constructor | instance method | top-level fun | static fun} × {declared ret type | inferred | omitted}` asserting that `.length`, arithmetic, `is T`-then-member, chained calls and generic instantiation behave identically for statics as for the other three; plus `object` singletons and generic statics.
## Resolution
Valid: static method signatures were excluded from the compiler's member type
metadata, both before body compilation and when inferred returns were recorded.
They now use the same callable signature and generic substitution path as
instance methods. Explicit annotations, inferred expression returns, primitive
returns, chained calls, and inferred/explicit generic arguments are covered by
`StaticReturnTypeRegressionTest`.
The singleton example additionally exposed runtime bugs once typing was
corrected: object static initializers were discarded, and a static call on an
object was sent to instance-method dispatch. Object declarations now preserve
and execute their static initialization statements before constructing the
singleton instance.
The compiler now retains its class-scope call decision for bytecode generation;
singleton static calls use the object's class and the existing class-scope
access opcode. Instance methods retain their dispatch path. Tests also cover
static calls with a trailing lambda.
Before the fix, all 5 original regression cases failed with missing compile-time
receiver types. The initial type-only correction passed 4, with the singleton
case then failing at runtime; dispatch is corrected in this same report.
Validation: all 6 focused JVM regression tests passed, followed by all 1,040
`:lynglib:jvmTest` tests (benchmarks excluded by the repository default).
`git diff --check` also passed.
+5
View File
@@ -0,0 +1,5 @@
# Invalid reports
Place reports here when reproduction and the language contract show that they
are not bugs. Append the reason and verification evidence to each moved report.
Valid reports with an invalid subclaim stay with their resolution notes.
+12 -2
View File
@@ -17,8 +17,8 @@
package net.sergeych package net.sergeych
import com.github.ajalt.clikt.core.CoreCliktCommand
import com.github.ajalt.clikt.core.Context import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.CoreCliktCommand
import com.github.ajalt.clikt.core.main import com.github.ajalt.clikt.core.main
import com.github.ajalt.clikt.core.subcommands import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.arguments.argument
@@ -45,16 +45,20 @@ import net.sergeych.lyng.io.db.createDbModule
import net.sergeych.lyng.io.db.jdbc.createJdbcModule import net.sergeych.lyng.io.db.jdbc.createJdbcModule
import net.sergeych.lyng.io.db.sqlite.createSqliteModule import net.sergeych.lyng.io.db.sqlite.createSqliteModule
import net.sergeych.lyng.io.fs.createFs import net.sergeych.lyng.io.fs.createFs
import net.sergeych.lyng.io.html.createHtmlModule
import net.sergeych.lyng.io.http.createHttpModule import net.sergeych.lyng.io.http.createHttpModule
import net.sergeych.lyng.io.http.server.createHttpServerModule
import net.sergeych.lyng.io.net.createNetModule import net.sergeych.lyng.io.net.createNetModule
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyng.io.ws.createWsModule import net.sergeych.lyng.io.ws.createWsModule
import net.sergeych.lyng.obj.* import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyngio.net.shutdownSystemNetEngine
import net.sergeych.lyngio.console.security.PermitAllConsoleAccessPolicy import net.sergeych.lyngio.console.security.PermitAllConsoleAccessPolicy
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
import net.sergeych.lyngio.http.security.PermitAllHttpAccessPolicy import net.sergeych.lyngio.http.security.PermitAllHttpAccessPolicy
import net.sergeych.lyngio.net.security.PermitAllNetAccessPolicy import net.sergeych.lyngio.net.security.PermitAllNetAccessPolicy
import net.sergeych.lyngio.net.shutdownSystemNetEngine
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import net.sergeych.lyngio.ws.security.PermitAllWsAccessPolicy import net.sergeych.lyngio.ws.security.PermitAllWsAccessPolicy
import net.sergeych.mp_tools.globalDefer import net.sergeych.mp_tools.globalDefer
import okio.* import okio.*
@@ -145,7 +149,10 @@ private fun ImportManager.invalidateCliModuleCaches() {
invalidatePackageCache("lyng.io.console") invalidatePackageCache("lyng.io.console")
invalidatePackageCache("lyng.io.db.jdbc") invalidatePackageCache("lyng.io.db.jdbc")
invalidatePackageCache("lyng.io.db.sqlite") invalidatePackageCache("lyng.io.db.sqlite")
invalidatePackageCache("lyng.io.html")
invalidatePackageCache("lyng.io.http") invalidatePackageCache("lyng.io.http")
invalidatePackageCache("lyng.io.http.server")
invalidatePackageCache("lyng.io.process")
invalidatePackageCache("lyng.io.ws") invalidatePackageCache("lyng.io.ws")
invalidatePackageCache("lyng.io.net") invalidatePackageCache("lyng.io.net")
} }
@@ -235,7 +242,10 @@ private fun installCliModules(manager: ImportManager) {
createDbModule(manager) createDbModule(manager)
createJdbcModule(manager) createJdbcModule(manager)
createSqliteModule(manager) createSqliteModule(manager)
createHtmlModule(manager)
createHttpModule(PermitAllHttpAccessPolicy, manager) createHttpModule(PermitAllHttpAccessPolicy, manager)
createHttpServerModule(PermitAllNetAccessPolicy, manager)
createProcessModule(PermitAllProcessAccessPolicy, manager)
createWsModule(PermitAllWsAccessPolicy, manager) createWsModule(PermitAllWsAccessPolicy, manager)
createNetModule(PermitAllNetAccessPolicy, manager) createNetModule(PermitAllNetAccessPolicy, manager)
} }
@@ -73,10 +73,12 @@ class CliNetworkJvmTest {
try { try {
val script = """ val script = """
import lyng.io.http import lyng.io.http
import lyng.io.http.server
import lyng.io.ws import lyng.io.ws
import lyng.io.net import lyng.io.net
assert(Http.isSupported()) assert(Http.isSupported())
assert(HttpServer() is HttpServer)
println("ws=" + Ws.isSupported()) println("ws=" + Ws.isSupported())
println("net=" + Net.isSupported()) println("net=" + Net.isSupported())
+17 -1
View File
@@ -29,7 +29,7 @@ plugins {
} }
group = "net.sergeych" group = "net.sergeych"
version = "0.0.1-SNAPSHOT" version = providers.gradleProperty("lyngio.version").getOrElse("0.0.3-SNAPSHOT")
private fun Project.sqliteLinuxLinkerOpts(vararg defaultDirs: String): List<String> { private fun Project.sqliteLinuxLinkerOpts(vararg defaultDirs: String): List<String> {
val overrideDir = providers.gradleProperty("sqlite3.lib.dir").orNull val overrideDir = providers.gradleProperty("sqlite3.lib.dir").orNull
@@ -141,6 +141,7 @@ kotlin {
api(libs.kotlinx.coroutines.core) api(libs.kotlinx.coroutines.core)
api(libs.mordant.core) api(libs.mordant.core)
api(libs.ktor.client.core) api(libs.ktor.client.core)
implementation("com.fleeksoft.ksoup:ksoup:0.2.6")
implementation(libs.ktor.client.websockets) implementation(libs.ktor.client.websockets)
} }
} }
@@ -329,6 +330,15 @@ tasks.matching { it.name.startsWith("lint", ignoreCase = true) }.configureEach {
this.enabled = false this.enabled = false
} }
// Kotlin/Native commonization replaces intermediate .knm files while Gradle is
// snapshotting this task's outputs, which can make publishing fail with a
// FileNotFoundException. These files are generated intermediates, so tracking
// their state provides no useful incremental-build benefit.
tasks.matching { it.name == "commonizeCInterop" }.configureEach {
doNotTrackState("Kotlin/Native commonizer mutates its output tree during execution")
notCompatibleWithConfigurationCache("Kotlin/Native cinterop commonization accesses task projects during execution")
}
publishing { publishing {
val mavenToken by lazy { val mavenToken by lazy {
File("${System.getProperty("user.home")}/.gitea_token").readText() File("${System.getProperty("user.home")}/.gitea_token").readText()
@@ -346,3 +356,9 @@ publishing {
} }
} }
} }
// Lyng's JVM dependencies include Java 21 bytecode; keep the Java 17 library target.
val testToolchains = extensions.getByType<org.gradle.jvm.toolchain.JavaToolchainService>()
tasks.withType<Test>().configureEach {
javaLauncher.set(testToolchains.launcherFor { languageVersion.set(JavaLanguageVersion.of(21)) })
}
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(CIO) actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(CIO, config)
@@ -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.lyng.io.html
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope
import net.sergeych.lyng.Source
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyngio.stdlib_included.htmlLyng
private const val HTML_MODULE_NAME = "lyng.io.html"
fun createHtmlModule(scope: Scope): Boolean = createHtmlModule(scope.importManager)
fun createHtml(scope: Scope): Boolean = createHtmlModule(scope)
fun createHtmlModule(manager: ImportManager): Boolean {
if (manager.packageNames.contains(HTML_MODULE_NAME)) return false
manager.addPackage(HTML_MODULE_NAME) { module ->
buildHtmlModule(module)
}
return true
}
fun createHtml(manager: ImportManager): Boolean = createHtmlModule(manager)
private suspend fun buildHtmlModule(module: ModuleScope) {
module.eval(Source(HTML_MODULE_NAME, htmlLyng))
}
@@ -0,0 +1,96 @@
package net.sergeych.lyng.io.html
import com.fleeksoft.ksoup.Ksoup
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.nodes.Element
import com.fleeksoft.ksoup.nodes.Node
import com.fleeksoft.ksoup.nodes.TextNode
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope
import net.sergeych.lyng.Source
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyngio.stdlib_included.html_parseLyng
private const val MODULE = "lyng.io.html.parse"
fun createHtmlParseModule(scope: Scope): Boolean = createHtmlParseModule(scope.importManager)
fun createHtmlParseModule(manager: ImportManager): Boolean {
if (MODULE in manager.packageNames) return false
manager.addPackage(MODULE) { module -> buildHtmlParseModule(module) }
return true
}
private suspend fun buildHtmlParseModule(module: ModuleScope) {
module.eval(Source(MODULE, html_parseLyng))
val html = ObjClass("Html").apply {
addClassFn("parse") {
val source = requiredArg<ObjString>(0).value
val baseUrl = (args.list.getOrNull(1) as? ObjString)?.value.orEmpty()
ObjHtmlDocument(Ksoup.parse(source, baseUrl))
}
}
module.addConst("Html", html)
module.addConst("HtmlDocument", ObjHtmlDocument.type)
module.addConst("HtmlElement", ObjHtmlElement.type)
}
private class ObjHtmlDocument(val document: Document) : Obj() {
override val objClass get() = type
companion object {
val type = object : ObjClass("HtmlDocument") {
override suspend fun callOn(scope: Scope): Obj = scope.raiseError("Use Html.parse()")
}.apply {
addProperty("title", getter = { ObjString(thisAs<ObjHtmlDocument>().document.title()) })
addFn("text") { ObjString(readableText(thisAs<ObjHtmlDocument>().document.body())) }
addFn("select") {
val css = requiredArg<ObjString>(0).value
ObjList(thisAs<ObjHtmlDocument>().document.select(css).map { ObjHtmlElement(it) as Obj }.toMutableList())
}
}
}
}
private class ObjHtmlElement(val element: Element) : Obj() {
override val objClass get() = type
companion object {
val type = object : ObjClass("HtmlElement") {
override suspend fun callOn(scope: Scope): Obj = scope.raiseError("Use document.select()")
}.apply {
addFn("text") { ObjString(readableText(thisAs<ObjHtmlElement>().element)) }
addFn("attr") { ObjString(thisAs<ObjHtmlElement>().element.attr(requiredArg<ObjString>(0).value)) }
addFn("absUrl") { ObjString(thisAs<ObjHtmlElement>().element.absUrl(requiredArg<ObjString>(0).value)) }
addFn("select") {
val css = requiredArg<ObjString>(0).value
ObjList(thisAs<ObjHtmlElement>().element.select(css).map { ObjHtmlElement(it) as Obj }.toMutableList())
}
}
}
}
private val excludedTags = setOf("script", "style", "template", "noscript")
private val inlineWhitespace = Regex("[\\s\\u00a0]+")
/** Iterative traversal avoids overflowing the host stack on deeply nested input. */
private fun readableText(root: Element): String {
val text = StringBuilder()
val pending = ArrayDeque<Pair<Node, Boolean>>()
pending.addLast(root to false)
while (pending.isNotEmpty()) {
val (node, exit) = pending.removeLast()
when (node) {
is TextNode -> text.append(node.getWholeText().replace(inlineWhitespace, " "))
is Element -> {
if (node.normalName() in excludedTags || node.hasAttr("hidden")) continue
val block = node.isBlock() || node.normalName() == "br"
if (block) text.append('\n')
if (!exit) {
if (block) pending.addLast(node to true)
for (child in node.childNodes().asReversed()) pending.addLast(child to false)
}
}
}
}
return text.toString().lineSequence().map { it.replace(inlineWhitespace, " ").trim() }.filter { it.isNotEmpty() }.joinToString("\n")
}
@@ -17,6 +17,8 @@
package net.sergeych.lyng.io.http package net.sergeych.lyng.io.http
import kotlinx.coroutines.CancellationException
import net.sergeych.lyng.ModuleScope import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.ScopeFacade import net.sergeych.lyng.ScopeFacade
@@ -41,6 +43,7 @@ import net.sergeych.lyng.requireNoArgs
import net.sergeych.lyng.requireScope import net.sergeych.lyng.requireScope
import net.sergeych.lyngio.http.LyngHttpRequest import net.sergeych.lyngio.http.LyngHttpRequest
import net.sergeych.lyngio.http.LyngHttpResponse import net.sergeych.lyngio.http.LyngHttpResponse
import net.sergeych.lyngio.http.LyngHttpConfig
import net.sergeych.lyngio.http.getSystemHttpEngine import net.sergeych.lyngio.http.getSystemHttpEngine
import net.sergeych.lyngio.http.security.HttpAccessDeniedException import net.sergeych.lyngio.http.security.HttpAccessDeniedException
import net.sergeych.lyngio.http.security.HttpAccessOp import net.sergeych.lyngio.http.security.HttpAccessOp
@@ -51,21 +54,36 @@ import net.sergeych.lyngio.stdlib_included.http_typesLyng
private const val HTTP_MODULE_NAME = "lyng.io.http" private const val HTTP_MODULE_NAME = "lyng.io.http"
internal const val HTTP_TYPES_MODULE_NAME = "lyng.io.http.types" internal const val HTTP_TYPES_MODULE_NAME = "lyng.io.http.types"
fun createHttpModule(policy: HttpAccessPolicy, scope: Scope): Boolean = fun createHttpModule(
createHttpModule(policy, scope.importManager) policy: HttpAccessPolicy,
scope: Scope,
config: LyngHttpConfig = LyngHttpConfig(),
): Boolean = createHttpModule(policy, scope.importManager, config)
fun createHttp(policy: HttpAccessPolicy, scope: Scope): Boolean = createHttpModule(policy, scope) fun createHttp(
policy: HttpAccessPolicy,
scope: Scope,
config: LyngHttpConfig = LyngHttpConfig(),
): Boolean = createHttpModule(policy, scope, config)
fun createHttpModule(policy: HttpAccessPolicy, manager: ImportManager): Boolean { fun createHttpModule(
policy: HttpAccessPolicy,
manager: ImportManager,
config: LyngHttpConfig = LyngHttpConfig(),
): Boolean {
createHttpTypesModule(manager) createHttpTypesModule(manager)
if (manager.packageNames.contains(HTTP_MODULE_NAME)) return false if (manager.packageNames.contains(HTTP_MODULE_NAME)) return false
manager.addPackage(HTTP_MODULE_NAME) { module -> manager.addPackage(HTTP_MODULE_NAME) { module ->
buildHttpModule(module, policy) buildHttpModule(module, policy, config)
} }
return true return true
} }
fun createHttp(policy: HttpAccessPolicy, manager: ImportManager): Boolean = createHttpModule(policy, manager) fun createHttp(
policy: HttpAccessPolicy,
manager: ImportManager,
config: LyngHttpConfig = LyngHttpConfig(),
): Boolean = createHttpModule(policy, manager, config)
internal fun createHttpTypesModule(manager: ImportManager): Boolean { internal fun createHttpTypesModule(manager: ImportManager): Boolean {
if (manager.packageNames.contains(HTTP_TYPES_MODULE_NAME)) return false if (manager.packageNames.contains(HTTP_TYPES_MODULE_NAME)) return false
@@ -80,9 +98,9 @@ private suspend fun buildHttpTypesModule(module: ModuleScope) {
module.addConst("HttpHeaders", ObjHttpHeaders.type) module.addConst("HttpHeaders", ObjHttpHeaders.type)
} }
private suspend fun buildHttpModule(module: ModuleScope, policy: HttpAccessPolicy) { private suspend fun buildHttpModule(module: ModuleScope, policy: HttpAccessPolicy, config: LyngHttpConfig) {
module.eval(Source(HTTP_MODULE_NAME, httpLyng)) module.eval(Source(HTTP_MODULE_NAME, httpLyng))
val engine = getSystemHttpEngine() val engine = getSystemHttpEngine(config)
val headersType = ObjHttpHeaders.type val headersType = ObjHttpHeaders.type
val requestType = ObjHttpRequest.type val requestType = ObjHttpRequest.type
@@ -148,8 +166,10 @@ private suspend fun buildHttpModule(module: ModuleScope, policy: HttpAccessPolic
private suspend inline fun ScopeFacade.httpGuard(crossinline block: suspend () -> Obj): Obj { private suspend inline fun ScopeFacade.httpGuard(crossinline block: suspend () -> Obj): Obj {
return try { return try {
block() block()
} catch (e: CancellationException) {
throw e
} catch (e: HttpAccessDeniedException) { } catch (e: HttpAccessDeniedException) {
raiseIllegalOperation(e.reasonDetail ?: "http access denied") raiseIllegalOperation("HTTP_ACCESS_DENIED: " + (e.reasonDetail ?: "http access denied"))
} catch (e: Exception) { } catch (e: Exception) {
raiseIllegalOperation(e.message ?: "http error") raiseIllegalOperation(e.message ?: "http error")
} }
@@ -1,10 +1,13 @@
package net.sergeych.lyng.io.http.server package net.sergeych.lyng.io.http.server
import kotlinx.serialization.json.Json
import net.sergeych.lyng.ModuleScope import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Pos
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.ScopeFacade import net.sergeych.lyng.ScopeFacade
import net.sergeych.lyng.Source import net.sergeych.lyng.Source
import net.sergeych.lyng.Arguments import net.sergeych.lyng.Arguments
import net.sergeych.lyng.CallSignature
import net.sergeych.lyng.TypeDecl import net.sergeych.lyng.TypeDecl
import net.sergeych.lyng.asFacade import net.sergeych.lyng.asFacade
import net.sergeych.lyng.obj.Obj import net.sergeych.lyng.obj.Obj
@@ -14,16 +17,22 @@ import net.sergeych.lyng.obj.ObjClass
import net.sergeych.lyng.obj.ObjExternCallable import net.sergeych.lyng.obj.ObjExternCallable
import net.sergeych.lyng.obj.ObjInt import net.sergeych.lyng.obj.ObjInt
import net.sergeych.lyng.obj.ObjList import net.sergeych.lyng.obj.ObjList
import net.sergeych.lyng.obj.ObjMap
import net.sergeych.lyng.obj.ObjNull import net.sergeych.lyng.obj.ObjNull
import net.sergeych.lyng.obj.ObjProperty import net.sergeych.lyng.obj.ObjProperty
import net.sergeych.lyng.obj.ObjRegex
import net.sergeych.lyng.obj.ObjRegexMatch
import net.sergeych.lyng.obj.ObjString import net.sergeych.lyng.obj.ObjString
import net.sergeych.lyng.obj.ObjTypeExpr
import net.sergeych.lyng.obj.ObjVoid import net.sergeych.lyng.obj.ObjVoid
import net.sergeych.lyng.obj.requiredArg import net.sergeych.lyng.obj.requiredArg
import net.sergeych.lyng.obj.thisAs import net.sergeych.lyng.obj.thisAs
import net.sergeych.lyng.io.http.ObjHttpHeaders import net.sergeych.lyng.io.http.ObjHttpHeaders
import net.sergeych.lyng.io.http.createHttpTypesModule import net.sergeych.lyng.io.http.createHttpTypesModule
import net.sergeych.lyng.io.html.createHtmlModule
import net.sergeych.lyng.io.ws.ObjWsMessage import net.sergeych.lyng.io.ws.ObjWsMessage
import net.sergeych.lyng.io.ws.createWsTypesModule import net.sergeych.lyng.io.ws.createWsTypesModule
import net.sergeych.lyng.serialization.ObjJsonClass
import net.sergeych.lyng.pacman.ImportManager import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.raiseIllegalOperation import net.sergeych.lyng.raiseIllegalOperation
import net.sergeych.lyng.requireNoArgs import net.sergeych.lyng.requireNoArgs
@@ -34,6 +43,7 @@ import net.sergeych.lyngio.http.server.HttpRequest
import net.sergeych.lyngio.http.server.HttpResponse import net.sergeych.lyngio.http.server.HttpResponse
import net.sergeych.lyngio.http.server.HttpServerConfig import net.sergeych.lyngio.http.server.HttpServerConfig
import net.sergeych.lyngio.http.server.HttpWebSocketSession import net.sergeych.lyngio.http.server.HttpWebSocketSession
import net.sergeych.lyngio.http.server.decodePathSegment
import net.sergeych.lyngio.http.server.defaultReason import net.sergeych.lyngio.http.server.defaultReason
import net.sergeych.lyngio.http.server.startHttpServer import net.sergeych.lyngio.http.server.startHttpServer
import net.sergeych.lyngio.net.security.NetAccessDeniedException import net.sergeych.lyngio.net.security.NetAccessDeniedException
@@ -52,6 +62,7 @@ fun createHttpServer(policy: NetAccessPolicy, scope: Scope): Boolean = createHtt
fun createHttpServerModule(policy: NetAccessPolicy, manager: ImportManager): Boolean { fun createHttpServerModule(policy: NetAccessPolicy, manager: ImportManager): Boolean {
createHttpTypesModule(manager) createHttpTypesModule(manager)
createWsTypesModule(manager) createWsTypesModule(manager)
createHtmlModule(manager)
if (manager.packageNames.contains(HTTP_SERVER_MODULE_NAME)) return false if (manager.packageNames.contains(HTTP_SERVER_MODULE_NAME)) return false
manager.addPackage(HTTP_SERVER_MODULE_NAME) { module -> manager.addPackage(HTTP_SERVER_MODULE_NAME) { module ->
buildHttpServerModule(module, policy) buildHttpServerModule(module, policy)
@@ -63,13 +74,15 @@ fun createHttpServer(policy: NetAccessPolicy, manager: ImportManager): Boolean =
private suspend fun buildHttpServerModule(module: ModuleScope, policy: NetAccessPolicy) { private suspend fun buildHttpServerModule(module: ModuleScope, policy: NetAccessPolicy) {
module.eval(Source(HTTP_SERVER_MODULE_NAME, http_serverLyng)) module.eval(Source(HTTP_SERVER_MODULE_NAME, http_serverLyng))
val requestContextClass = ObjServerExchange.type(module.requireClass("RequestContext"))
module.addConst("HttpHeaders", ObjHttpHeaders.type) module.addConst("HttpHeaders", ObjHttpHeaders.type)
module.addConst("WsMessage", ObjWsMessage.type) module.addConst("WsMessage", ObjWsMessage.type)
module.addConst("ServerRequest", ObjServerRequest.type) module.addConst("ServerRequest", ObjServerRequest.type)
module.addConst("ServerExchange", ObjServerExchange.type) module.addConst("RequestContext", requestContextClass)
module.addConst("ServerWebSocket", ObjServerWebSocket.type) module.addConst("ServerWebSocket", ObjServerWebSocket.type)
module.addConst("HttpServerHandle", ObjHttpServerHandle.type) module.addConst("HttpServerHandle", ObjHttpServerHandle.type)
module.addConst("HttpServer", ObjLyngHttpServer.type(policy)) module.addConst("Router", ObjLyngRouter.type(requestContextClass))
module.addConst("HttpServer", ObjLyngHttpServer.type(policy, requestContextClass))
} }
private suspend inline fun ScopeFacade.httpServerGuard(crossinline block: suspend () -> Obj): Obj { private suspend inline fun ScopeFacade.httpServerGuard(crossinline block: suspend () -> Obj): Obj {
@@ -96,31 +109,47 @@ private fun captureCallable(scope: Scope, rawCallable: Obj): RegisteredCallable
private suspend fun RegisteredCallable.call(vararg args: Obj): Obj = private suspend fun RegisteredCallable.call(vararg args: Obj): Obj =
scope.asFacade().call(callable, Arguments(args.toList())) scope.asFacade().call(callable, Arguments(args.toList()))
private suspend fun RegisteredCallable.callWithReceiver(receiver: Obj, vararg args: Obj): Obj =
scope.asFacade().call(callable, Arguments(args.toList()), receiver)
private val stringType = TypeDecl.Simple("String", false) private val stringType = TypeDecl.Simple("String", false)
private val nullableStringType = TypeDecl.Simple("String", true) private val nullableStringType = TypeDecl.Simple("String", true)
private val boolType = TypeDecl.Simple("Bool", false) private val boolType = TypeDecl.Simple("Bool", false)
private val intType = TypeDecl.Simple("Int", false) private val intType = TypeDecl.Simple("Int", false)
private val bufferType = TypeDecl.Simple("Buffer", false) private val bufferType = TypeDecl.Simple("Buffer", false)
private val nullableBufferType = TypeDecl.Simple("Buffer", true) private val nullableBufferType = TypeDecl.Simple("Buffer", true)
private val regexType = TypeDecl.Simple("Regex", false)
private val nullableRegexMatchType = TypeDecl.Simple("RegexMatch", true)
private val voidType = TypeDecl.Simple("Void", false) private val voidType = TypeDecl.Simple("Void", false)
private val httpHeadersType = TypeDecl.Simple("HttpHeaders", false) private val httpHeadersType = TypeDecl.Simple("HttpHeaders", false)
private val htmlTagType = TypeDecl.Simple("HtmlTag", false)
private val serverRequestType = TypeDecl.Simple("ServerRequest", false) private val serverRequestType = TypeDecl.Simple("ServerRequest", false)
private val serverExchangeType = TypeDecl.Simple("ServerExchange", false) private val requestContextType = TypeDecl.Simple("RequestContext", false)
private val serverWebSocketType = TypeDecl.Simple("ServerWebSocket", false) private val serverWebSocketType = TypeDecl.Simple("ServerWebSocket", false)
private val nullableServerWsMessageType = TypeDecl.Simple("WsMessage", true) private val nullableServerWsMessageType = TypeDecl.Simple("WsMessage", true)
private val httpServerHandleType = TypeDecl.Simple("HttpServerHandle", false) private val httpServerHandleType = TypeDecl.Simple("HttpServerHandle", false)
private val httpServerType = TypeDecl.Simple("HttpServer", false) private val httpServerType = TypeDecl.Simple("HttpServer", false)
private val routerType = TypeDecl.Simple("Router", false)
private val nullableAnyType = TypeDecl.TypeNullableAny private val nullableAnyType = TypeDecl.TypeNullableAny
private fun listType(item: TypeDecl) = TypeDecl.Generic("List", listOf(item), false) private fun listType(item: TypeDecl) = TypeDecl.Generic("List", listOf(item), false)
private fun mapType(key: TypeDecl, value: TypeDecl) = TypeDecl.Generic("Map", listOf(key, value), false)
private fun unionType(vararg options: TypeDecl) = TypeDecl.Union(options.toList(), nullable = false)
private fun fnType(returnType: TypeDecl, vararg params: TypeDecl) = private fun fnType(returnType: TypeDecl, vararg params: TypeDecl) =
TypeDecl.Function(receiver = null, params = params.toList(), returnType = returnType) TypeDecl.Function(receiver = null, params = params.toList(), returnType = returnType)
private fun receiverFnType(receiver: TypeDecl, returnType: TypeDecl, vararg params: TypeDecl) =
TypeDecl.Function(receiver = receiver, params = params.toList(), returnType = returnType)
private fun receiverCallSignature(receiverTypeName: String) =
CallSignature(tailBlockReceiverType = receiverTypeName)
private fun bridgeFn( private fun bridgeFn(
owner: ObjClass, owner: ObjClass,
name: String, name: String,
typeDecl: TypeDecl.Function, typeDecl: TypeDecl.Function,
callSignature: net.sergeych.lyng.CallSignature? = null,
code: suspend ScopeFacade.() -> Obj, code: suspend ScopeFacade.() -> Obj,
) { ) {
owner.createField( owner.createField(
@@ -128,6 +157,7 @@ private fun bridgeFn(
initialValue = ObjExternCallable.fromBridge { code() }, initialValue = ObjExternCallable.fromBridge { code() },
type = net.sergeych.lyng.obj.ObjRecord.Type.Fun, type = net.sergeych.lyng.obj.ObjRecord.Type.Fun,
typeDecl = typeDecl, typeDecl = typeDecl,
callSignature = callSignature,
) )
} }
@@ -145,53 +175,289 @@ private fun bridgeProperty(
) )
} }
private fun requireRoutePattern(scope: ScopeFacade, index: Int): RoutePattern = when (val path = scope.args.list.getOrNull(index)) {
is ObjString -> {
if (!path.value.startsWith('/')) scope.raiseIllegalArgument("path must start with '/'")
RoutePattern.Exact(path.value)
}
is ObjRegex -> RoutePattern.Regex(path)
else -> scope.raiseClassCastError("path must be String or Regex")
}
private fun requirePathTemplate(scope: ScopeFacade, index: Int): String {
val template = scope.requiredArg<ObjString>(index).value
if (!template.startsWith('/')) scope.raiseIllegalArgument("pathTemplate must start with '/'")
return template
}
private class RouteRegistry(
private val exchangeClass: ObjClass,
) {
private val methodRoutes = linkedMapOf<String, LinkedHashMap<String, RegisteredCallable>>()
private val methodRegexRoutes = linkedMapOf<String, MutableList<RegisteredRegexRoute>>()
private val anyRoutes = linkedMapOf<String, RegisteredCallable>()
private val anyRegexRoutes = mutableListOf<RegisteredRegexRoute>()
private val wsRoutes = linkedMapOf<String, RegisteredCallable>()
private val wsRegexRoutes = mutableListOf<RegisteredRegexRoute>()
private var fallback: RegisteredCallable? = null
suspend fun registerRoute(method: String, scope: ScopeFacade) {
val path = requireRoutePattern(scope, 0)
val handler = captureCallable(scope.requireScope(), scope.args.list[1])
when (path) {
is RoutePattern.Exact -> {
val routes = methodRoutes.getOrPut(method) { linkedMapOf() }
if (routes.containsKey(path.path)) scope.raiseIllegalArgument("duplicate route for $method ${path.path}")
routes[path.path] = handler
}
is RoutePattern.Regex -> {
val routes = methodRegexRoutes.getOrPut(method) { mutableListOf() }
if (routes.any { it.pattern.regex.pattern == path.regex.regex.pattern }) {
scope.raiseIllegalArgument("duplicate regex route for $method ${path.regex.regex.pattern}")
}
routes += RegisteredRegexRoute(path.regex, handler)
}
}
}
suspend fun registerTemplateRoute(method: String, scope: ScopeFacade) {
val template = requirePathTemplate(scope, 0)
val handler = captureCallable(scope.requireScope(), scope.args.list[1])
val routes = methodRegexRoutes.getOrPut(method) { mutableListOf() }
val compiled = compilePathTemplate(template, scope)
if (routes.any { it.identity == compiled.identity }) {
scope.raiseIllegalArgument("duplicate path route for $method $template")
}
routes += RegisteredRegexRoute(compiled.pattern, handler, compiled.paramNames, compiled.identity)
}
suspend fun registerAny(scope: ScopeFacade) {
val path = requireRoutePattern(scope, 0)
val handler = captureCallable(scope.requireScope(), scope.args.list[1])
when (path) {
is RoutePattern.Exact -> {
if (anyRoutes.containsKey(path.path)) scope.raiseIllegalArgument("duplicate route for ANY ${path.path}")
anyRoutes[path.path] = handler
}
is RoutePattern.Regex -> {
if (anyRegexRoutes.any { it.pattern.regex.pattern == path.regex.regex.pattern }) {
scope.raiseIllegalArgument("duplicate regex route for ANY ${path.regex.regex.pattern}")
}
anyRegexRoutes += RegisteredRegexRoute(path.regex, handler)
}
}
}
suspend fun registerTemplateAny(scope: ScopeFacade) {
val template = requirePathTemplate(scope, 0)
val handler = captureCallable(scope.requireScope(), scope.args.list[1])
val compiled = compilePathTemplate(template, scope)
if (anyRegexRoutes.any { it.identity == compiled.identity }) {
scope.raiseIllegalArgument("duplicate path route for ANY $template")
}
anyRegexRoutes += RegisteredRegexRoute(compiled.pattern, handler, compiled.paramNames, compiled.identity)
}
suspend fun registerWs(scope: ScopeFacade) {
val path = requireRoutePattern(scope, 0)
val handler = captureCallable(scope.requireScope(), scope.args.list[1])
when (path) {
is RoutePattern.Exact -> {
if (wsRoutes.containsKey(path.path)) scope.raiseIllegalArgument("duplicate websocket route for ${path.path}")
wsRoutes[path.path] = handler
}
is RoutePattern.Regex -> {
if (wsRegexRoutes.any { it.pattern.regex.pattern == path.regex.regex.pattern }) {
scope.raiseIllegalArgument("duplicate websocket regex route for ${path.regex.regex.pattern}")
}
wsRegexRoutes += RegisteredRegexRoute(path.regex, handler)
}
}
}
suspend fun registerTemplateWs(scope: ScopeFacade) {
val template = requirePathTemplate(scope, 0)
val handler = captureCallable(scope.requireScope(), scope.args.list[1])
val compiled = compilePathTemplate(template, scope)
if (wsRegexRoutes.any { it.identity == compiled.identity }) {
scope.raiseIllegalArgument("duplicate websocket path route for $template")
}
wsRegexRoutes += RegisteredRegexRoute(compiled.pattern, handler, compiled.paramNames, compiled.identity)
}
suspend fun registerFallback(scope: ScopeFacade) {
fallback = captureCallable(scope.requireScope(), scope.args.list[0])
}
fun mount(scope: ScopeFacade, other: RouteRegistry) {
other.methodRoutes.forEach { (method, routes) ->
val target = methodRoutes.getOrPut(method) { linkedMapOf() }
routes.forEach { (path, handler) ->
if (target.containsKey(path)) scope.raiseIllegalArgument("duplicate route for $method $path")
target[path] = handler
}
}
other.methodRegexRoutes.forEach { (method, routes) ->
val target = methodRegexRoutes.getOrPut(method) { mutableListOf() }
routes.forEach { route ->
if (target.any { it.identity == route.identity }) {
scope.raiseIllegalArgument("duplicate route for $method ${route.identity.removePrefix("path:")}")
}
target += route
}
}
other.anyRoutes.forEach { (path, handler) ->
if (anyRoutes.containsKey(path)) scope.raiseIllegalArgument("duplicate route for ANY $path")
anyRoutes[path] = handler
}
other.anyRegexRoutes.forEach { route ->
if (anyRegexRoutes.any { it.identity == route.identity }) {
scope.raiseIllegalArgument("duplicate route for ANY ${route.identity.removePrefix("path:")}")
}
anyRegexRoutes += route
}
other.wsRoutes.forEach { (path, handler) ->
if (wsRoutes.containsKey(path)) scope.raiseIllegalArgument("duplicate websocket route for $path")
wsRoutes[path] = handler
}
other.wsRegexRoutes.forEach { route ->
if (wsRegexRoutes.any { it.identity == route.identity }) {
scope.raiseIllegalArgument("duplicate websocket route for ${route.identity.removePrefix("path:")}")
}
wsRegexRoutes += route
}
if (other.fallback != null) {
if (fallback != null) scope.raiseIllegalArgument("fallback is already defined")
fallback = other.fallback
}
}
suspend fun dispatchRequest(request: HttpRequest): HttpHandlerResult {
val path = request.head.path
if (request.head.wantsWebSocketUpgrade) {
wsRoutes[path]?.let { route ->
return HttpHandlerResult.WebSocket { session ->
val exchange = ObjServerExchange(request, null, emptyMap(), exchangeClass)
route.callWithReceiver(exchange, ObjServerWebSocket(session))
}
}
matchRegexRoute(wsRegexRoutes, path)?.let { matched ->
return HttpHandlerResult.WebSocket { session ->
val exchange = ObjServerExchange(request, ObjRegexMatch(matched.match), matched.params, exchangeClass)
matched.handler.callWithReceiver(exchange, ObjServerWebSocket(session))
}
}
}
val method = request.head.method.uppercase()
val exactRoute = methodRoutes[method]?.get(path) ?: anyRoutes[path]
if (exactRoute != null) {
val exchange = ObjServerExchange(request, null, emptyMap(), exchangeClass)
exactRoute.callWithReceiver(exchange)
return exchangeResult(exactRoute === fallback, exchange)
}
matchRegexRoute(methodRegexRoutes[method], path)?.let { matched ->
val exchange = ObjServerExchange(request, ObjRegexMatch(matched.match), matched.params, exchangeClass)
matched.handler.callWithReceiver(exchange)
return exchangeResult(false, exchange)
}
matchRegexRoute(anyRegexRoutes, path)?.let { matched ->
val exchange = ObjServerExchange(request, ObjRegexMatch(matched.match), matched.params, exchangeClass)
matched.handler.callWithReceiver(exchange)
return exchangeResult(false, exchange)
}
val fallbackRoute = fallback ?: return HttpHandlerResult.Response(
HttpResponse(status = 404, body = "not found".encodeToByteArray())
)
val exchange = ObjServerExchange(request, null, emptyMap(), exchangeClass)
fallbackRoute.callWithReceiver(exchange)
return exchangeResult(true, exchange)
}
private fun exchangeResult(isFallback: Boolean, exchange: ObjServerExchange): HttpHandlerResult = when (val result = exchange.result) {
is ExchangeResult.Http -> result.value
is ExchangeResult.WebSocket -> result.value
ExchangeResult.Unhandled -> {
if (isFallback) {
HttpHandlerResult.Response(HttpResponse(status = 404, body = "not found".encodeToByteArray()))
} else {
HttpHandlerResult.Response(HttpResponse(status = 500, body = "route handler did not handle exchange".encodeToByteArray(), close = true))
}
}
}
}
private class ObjLyngHttpServer( private class ObjLyngHttpServer(
private val netPolicy: NetAccessPolicy, private val netPolicy: NetAccessPolicy,
private val requestContextClass: ObjClass,
) : Obj() { ) : Obj() {
private val methodRoutes = linkedMapOf<String, LinkedHashMap<String, RegisteredCallable>>() private val routes = RouteRegistry(requestContextClass)
private val anyRoutes = linkedMapOf<String, RegisteredCallable>()
private val wsRoutes = linkedMapOf<String, RegisteredCallable>()
private var fallback: RegisteredCallable? = null
private var handle: net.sergeych.lyngio.http.server.HttpServer? = null private var handle: net.sergeych.lyngio.http.server.HttpServer? = null
override val objClass: ObjClass override val objClass: ObjClass
get() = type(netPolicy) get() = type(netPolicy, requestContextClass)
companion object { companion object {
private val types = mutableMapOf<NetAccessPolicy, ObjClass>() private val types = mutableMapOf<Pair<NetAccessPolicy, ObjClass>, ObjClass>()
fun type(netPolicy: NetAccessPolicy): ObjClass = fun type(netPolicy: NetAccessPolicy, requestContextClass: ObjClass): ObjClass =
types.getOrPut(netPolicy) { types.getOrPut(netPolicy to requestContextClass) {
object : ObjClass("HttpServer") { object : ObjClass("HttpServer") {
override suspend fun callOn(scope: Scope): Obj { override suspend fun callOn(scope: Scope): Obj {
if (scope.args.list.isNotEmpty()) scope.raiseError("HttpServer() does not accept arguments") if (scope.args.list.isNotEmpty()) scope.raiseError("HttpServer() does not accept arguments")
return ObjLyngHttpServer(netPolicy) return ObjLyngHttpServer(netPolicy, requestContextClass)
} }
}.apply { }.apply {
val exchangeHandlerType = fnType(nullableAnyType, serverExchangeType) val routeArgType = unionType(stringType, regexType)
val webSocketHandlerType = fnType(nullableAnyType, serverWebSocketType, serverExchangeType) val exchangeHandlerType = receiverFnType(requestContextType, nullableAnyType)
val webSocketHandlerType = receiverFnType(requestContextType, nullableAnyType, serverWebSocketType)
val exchangeHandlerSignature = receiverCallSignature("RequestContext")
bridgeFn(this, "get", fnType(httpServerType, stringType, exchangeHandlerType)) { bridgeFn(this, "get", fnType(httpServerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerRoute("GET", this) thisAs<ObjLyngHttpServer>().registerRoute("GET", this)
} }
bridgeFn(this, "post", fnType(httpServerType, stringType, exchangeHandlerType)) { bridgeFn(this, "getPath", fnType(httpServerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerTemplateRoute("GET", this)
}
bridgeFn(this, "post", fnType(httpServerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerRoute("POST", this) thisAs<ObjLyngHttpServer>().registerRoute("POST", this)
} }
bridgeFn(this, "put", fnType(httpServerType, stringType, exchangeHandlerType)) { bridgeFn(this, "postPath", fnType(httpServerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerTemplateRoute("POST", this)
}
bridgeFn(this, "put", fnType(httpServerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerRoute("PUT", this) thisAs<ObjLyngHttpServer>().registerRoute("PUT", this)
} }
bridgeFn(this, "delete", fnType(httpServerType, stringType, exchangeHandlerType)) { bridgeFn(this, "putPath", fnType(httpServerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerTemplateRoute("PUT", this)
}
bridgeFn(this, "delete", fnType(httpServerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerRoute("DELETE", this) thisAs<ObjLyngHttpServer>().registerRoute("DELETE", this)
} }
bridgeFn(this, "any", fnType(httpServerType, stringType, exchangeHandlerType)) { bridgeFn(this, "deletePath", fnType(httpServerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerTemplateRoute("DELETE", this)
}
bridgeFn(this, "any", fnType(httpServerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerAny(this) thisAs<ObjLyngHttpServer>().registerAny(this)
} }
bridgeFn(this, "ws", fnType(httpServerType, stringType, webSocketHandlerType)) { bridgeFn(this, "anyPath", fnType(httpServerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerTemplateAny(this)
}
bridgeFn(this, "ws", fnType(httpServerType, routeArgType, webSocketHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerWs(this) thisAs<ObjLyngHttpServer>().registerWs(this)
} }
bridgeFn(this, "fallback", fnType(httpServerType, exchangeHandlerType)) { bridgeFn(this, "wsPath", fnType(httpServerType, stringType, webSocketHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerTemplateWs(this)
}
bridgeFn(this, "fallback", fnType(httpServerType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngHttpServer>().registerFallback(this) thisAs<ObjLyngHttpServer>().registerFallback(this)
} }
bridgeFn(this, "mount", fnType(httpServerType, routerType)) {
thisAs<ObjLyngHttpServer>().mount(this)
}
bridgeFn(this, "listen", fnType(httpServerHandleType, intType, nullableStringType, intType)) { bridgeFn(this, "listen", fnType(httpServerHandleType, intType, nullableStringType, intType)) {
thisAs<ObjLyngHttpServer>().listen(this) thisAs<ObjLyngHttpServer>().listen(this)
} }
@@ -203,43 +469,51 @@ private class ObjLyngHttpServer(
if (handle != null) scope.raiseIllegalState("HttpServer routes cannot be modified after listen()") if (handle != null) scope.raiseIllegalState("HttpServer routes cannot be modified after listen()")
} }
private fun requirePath(scope: ScopeFacade, index: Int): String {
val path = scope.requiredArg<ObjString>(index).value
if (!path.startsWith('/')) scope.raiseIllegalArgument("path must start with '/'")
return path
}
private suspend fun registerRoute(method: String, scope: ScopeFacade): Obj = scope.httpServerGuard { private suspend fun registerRoute(method: String, scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope) ensureMutable(scope)
val path = requirePath(scope, 0) routes.registerRoute(method, scope)
val handler = captureCallable(scope.requireScope(), scope.args.list[1]) scope.thisObj
val routes = methodRoutes.getOrPut(method) { linkedMapOf() } }
if (routes.containsKey(path)) scope.raiseIllegalArgument("duplicate route for $method $path")
routes[path] = handler private suspend fun registerTemplateRoute(method: String, scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope)
routes.registerTemplateRoute(method, scope)
scope.thisObj scope.thisObj
} }
private suspend fun registerAny(scope: ScopeFacade): Obj = scope.httpServerGuard { private suspend fun registerAny(scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope) ensureMutable(scope)
val path = requirePath(scope, 0) routes.registerAny(scope)
val handler = captureCallable(scope.requireScope(), scope.args.list[1]) scope.thisObj
if (anyRoutes.containsKey(path)) scope.raiseIllegalArgument("duplicate route for ANY $path") }
anyRoutes[path] = handler
private suspend fun registerTemplateAny(scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope)
routes.registerTemplateAny(scope)
scope.thisObj scope.thisObj
} }
private suspend fun registerWs(scope: ScopeFacade): Obj = scope.httpServerGuard { private suspend fun registerWs(scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope) ensureMutable(scope)
val path = requirePath(scope, 0) routes.registerWs(scope)
val handler = captureCallable(scope.requireScope(), scope.args.list[1]) scope.thisObj
if (wsRoutes.containsKey(path)) scope.raiseIllegalArgument("duplicate websocket route for $path") }
wsRoutes[path] = handler
private suspend fun registerTemplateWs(scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope)
routes.registerTemplateWs(scope)
scope.thisObj scope.thisObj
} }
private suspend fun registerFallback(scope: ScopeFacade): Obj = scope.httpServerGuard { private suspend fun registerFallback(scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope) ensureMutable(scope)
fallback = captureCallable(scope.requireScope(), scope.args.list[0]) routes.registerFallback(scope)
scope.thisObj
}
private suspend fun mount(scope: ScopeFacade): Obj = scope.httpServerGuard {
ensureMutable(scope)
routes.mount(scope, scope.requiredArg<ObjLyngRouter>(0).routes)
scope.thisObj scope.thisObj
} }
@@ -254,47 +528,204 @@ private class ObjLyngHttpServer(
val started = startHttpServer( val started = startHttpServer(
config = HttpServerConfig(host = host ?: "127.0.0.1", port = port, backlog = backlog), config = HttpServerConfig(host = host ?: "127.0.0.1", port = port, backlog = backlog),
) { request -> ) { request ->
dispatchRequest(request) routes.dispatchRequest(request)
} }
handle = started handle = started
ObjHttpServerHandle(started) ObjHttpServerHandle(started)
} }
}
private suspend fun dispatchRequest(request: HttpRequest): HttpHandlerResult { private class ObjLyngRouter(
val path = request.head.path private val requestContextClass: ObjClass,
if (request.head.wantsWebSocketUpgrade) { ) : Obj() {
wsRoutes[path]?.let { route -> val routes = RouteRegistry(requestContextClass)
return HttpHandlerResult.WebSocket { session ->
val exchange = ObjServerExchange(request) override val objClass: ObjClass
route.call(ObjServerWebSocket(session), exchange) get() = type(requestContextClass)
companion object {
private val types = mutableMapOf<ObjClass, ObjClass>()
fun type(requestContextClass: ObjClass): ObjClass =
types.getOrPut(requestContextClass) {
object : ObjClass("Router") {
override suspend fun callOn(scope: Scope): Obj {
if (scope.args.list.isNotEmpty()) scope.raiseError("Router() does not accept arguments")
return ObjLyngRouter(requestContextClass)
}
}.apply {
val routeArgType = unionType(stringType, regexType)
val exchangeHandlerType = receiverFnType(requestContextType, nullableAnyType)
val webSocketHandlerType = receiverFnType(requestContextType, nullableAnyType, serverWebSocketType)
val exchangeHandlerSignature = receiverCallSignature("RequestContext")
bridgeFn(this, "get", fnType(routerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerRoute("GET", this)
}
bridgeFn(this, "getPath", fnType(routerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerTemplateRoute("GET", this)
}
bridgeFn(this, "post", fnType(routerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerRoute("POST", this)
}
bridgeFn(this, "postPath", fnType(routerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerTemplateRoute("POST", this)
}
bridgeFn(this, "put", fnType(routerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerRoute("PUT", this)
}
bridgeFn(this, "putPath", fnType(routerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerTemplateRoute("PUT", this)
}
bridgeFn(this, "delete", fnType(routerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerRoute("DELETE", this)
}
bridgeFn(this, "deletePath", fnType(routerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerTemplateRoute("DELETE", this)
}
bridgeFn(this, "any", fnType(routerType, routeArgType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerAny(this)
}
bridgeFn(this, "anyPath", fnType(routerType, stringType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerTemplateAny(this)
}
bridgeFn(this, "ws", fnType(routerType, routeArgType, webSocketHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerWs(this)
}
bridgeFn(this, "wsPath", fnType(routerType, stringType, webSocketHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerTemplateWs(this)
}
bridgeFn(this, "fallback", fnType(routerType, exchangeHandlerType), exchangeHandlerSignature) {
thisAs<ObjLyngRouter>().registerFallback(this)
}
bridgeFn(this, "mount", fnType(routerType, routerType)) {
thisAs<ObjLyngRouter>().mount(this)
}
} }
} }
} }
val route = methodRoutes[request.head.method.uppercase()]?.get(path) private suspend fun registerRoute(method: String, scope: ScopeFacade): Obj = scope.httpServerGuard {
?: anyRoutes[path] routes.registerRoute(method, scope)
?: fallback scope.thisObj
if (route == null) {
return HttpHandlerResult.Response(HttpResponse(status = 404, body = "not found".encodeToByteArray()))
} }
val exchange = ObjServerExchange(request) private suspend fun registerTemplateRoute(method: String, scope: ScopeFacade): Obj = scope.httpServerGuard {
route.call(exchange) routes.registerTemplateRoute(method, scope)
return when (val result = exchange.result) { scope.thisObj
is ExchangeResult.Http -> result.value
is ExchangeResult.WebSocket -> result.value
ExchangeResult.Unhandled -> {
if (route === fallback) {
HttpHandlerResult.Response(HttpResponse(status = 404, body = "not found".encodeToByteArray()))
} else {
HttpHandlerResult.Response(HttpResponse(status = 500, body = "route handler did not handle exchange".encodeToByteArray(), close = true))
} }
private suspend fun registerAny(scope: ScopeFacade): Obj = scope.httpServerGuard {
routes.registerAny(scope)
scope.thisObj
} }
private suspend fun registerTemplateAny(scope: ScopeFacade): Obj = scope.httpServerGuard {
routes.registerTemplateAny(scope)
scope.thisObj
} }
private suspend fun registerWs(scope: ScopeFacade): Obj = scope.httpServerGuard {
routes.registerWs(scope)
scope.thisObj
}
private suspend fun registerTemplateWs(scope: ScopeFacade): Obj = scope.httpServerGuard {
routes.registerTemplateWs(scope)
scope.thisObj
}
private suspend fun registerFallback(scope: ScopeFacade): Obj = scope.httpServerGuard {
routes.registerFallback(scope)
scope.thisObj
}
private suspend fun mount(scope: ScopeFacade): Obj = scope.httpServerGuard {
routes.mount(scope, scope.requiredArg<ObjLyngRouter>(0).routes)
scope.thisObj
} }
} }
private sealed interface RoutePattern {
data class Exact(val path: String) : RoutePattern
data class Regex(val regex: ObjRegex) : RoutePattern
}
private data class RegisteredRegexRoute(
val pattern: ObjRegex,
val handler: RegisteredCallable,
val paramNames: List<String> = emptyList(),
val identity: String = "re:${pattern.regex.pattern}",
)
private data class MatchedRegexRoute(
val handler: RegisteredCallable,
val match: MatchResult,
val params: Map<String, String>,
)
private fun matchRegexRoute(routes: List<RegisteredRegexRoute>?, path: String): MatchedRegexRoute? {
if (routes == null) return null
for (route in routes) {
val match = route.pattern.regex.matchEntire(path) ?: continue
val params = if (route.paramNames.isEmpty()) {
emptyMap()
} else {
route.paramNames.withIndex().associateTo(linkedMapOf()) { (index, name) ->
name to decodePathSegment(match.groupValues[index + 1])
}
}
return MatchedRegexRoute(route.handler, match, params)
}
return null
}
private data class CompiledPathTemplate(
val pattern: ObjRegex,
val paramNames: List<String>,
val identity: String,
)
private fun compilePathTemplate(template: String, scope: ScopeFacade): CompiledPathTemplate {
val segments = if (template == "/") emptyList() else template.removePrefix("/").split('/')
val names = mutableListOf<String>()
val pattern = buildString {
append('^')
if (segments.isEmpty()) {
append('/')
} else {
for (segment in segments) {
append('/')
if (segment.startsWith('{') && segment.endsWith('}')) {
val name = segment.substring(1, segment.length - 1)
if (!isValidPathParamName(name)) {
scope.raiseIllegalArgument("invalid path parameter name: $name")
}
if (!names.add(name)) {
scope.raiseIllegalArgument("duplicate path parameter name: $name")
}
append("([^/]+)")
} else if ('{' in segment || '}' in segment) {
scope.raiseIllegalArgument("path template segments must be literal text or {name}")
} else {
append(Regex.escape(segment))
}
}
}
append('$')
}
return CompiledPathTemplate(
pattern = ObjRegex(Regex(pattern)),
paramNames = names,
identity = "path:$template"
)
}
private fun isValidPathParamName(name: String): Boolean =
name.isNotEmpty() &&
(name.first() == '_' || name.first().isLetter()) &&
name.drop(1).all { it == '_' || it.isLetterOrDigit() }
private class ObjHttpServerHandle( private class ObjHttpServerHandle(
private val handle: net.sergeych.lyngio.http.server.HttpServer, private val handle: net.sergeych.lyngio.http.server.HttpServer,
) : Obj() { ) : Obj() {
@@ -340,8 +771,14 @@ private class ObjServerRequest(
bridgeProperty(this, "path", stringType) { bridgeProperty(this, "path", stringType) {
ObjString(thisAs<ObjServerRequest>().request.head.path) ObjString(thisAs<ObjServerRequest>().request.head.path)
} }
bridgeProperty(this, "query", nullableStringType) { bridgeProperty(this, "pathParts", listType(stringType)) {
thisAs<ObjServerRequest>().request.head.query?.let(::ObjString) ?: ObjNull ObjList(thisAs<ObjServerRequest>().request.head.pathParts.map(::ObjString).toMutableList())
}
bridgeProperty(this, "queryString", nullableStringType) {
thisAs<ObjServerRequest>().request.head.queryString?.let(::ObjString) ?: ObjNull
}
bridgeProperty(this, "query", mapType(stringType, stringType)) {
thisAs<ObjServerRequest>().request.head.query.toObjMap()
} }
bridgeProperty(this, "headers", httpHeadersType) { bridgeProperty(this, "headers", httpHeadersType) {
requestHeadersObj(thisAs<ObjServerRequest>().request.head.headers) requestHeadersObj(thisAs<ObjServerRequest>().request.head.headers)
@@ -367,6 +804,9 @@ private sealed interface ExchangeResult {
private class ObjServerExchange( private class ObjServerExchange(
private val request: HttpRequest, private val request: HttpRequest,
private val routeMatch: ObjRegexMatch?,
private val routeParams: Map<String, String>,
private val type: ObjClass,
) : Obj() { ) : Obj() {
private val responseHeaders = linkedMapOf<String, MutableList<String>>() private val responseHeaders = linkedMapOf<String, MutableList<String>>()
var result: ExchangeResult = ExchangeResult.Unhandled var result: ExchangeResult = ExchangeResult.Unhandled
@@ -376,14 +816,36 @@ private class ObjServerExchange(
get() = type get() = type
companion object { companion object {
val type = object : ObjClass("ServerExchange") { private val types = mutableMapOf<ObjClass, ObjClass>()
fun type(base: ObjClass): ObjClass =
types.getOrPut(base) {
object : ObjClass("RequestContext") {
override suspend fun callOn(scope: Scope): Obj { override suspend fun callOn(scope: Scope): Obj {
scope.raiseError("ServerExchange cannot be created directly") scope.raiseError("RequestContext cannot be created directly")
} }
}.apply { }.apply {
bridgeProperty(this, "request", serverRequestType) { bridgeProperty(this, "request", serverRequestType) {
ObjServerRequest(thisAs<ObjServerExchange>().request) ObjServerRequest(thisAs<ObjServerExchange>().request)
} }
bridgeProperty(this, "routeMatch", nullableRegexMatchType) {
thisAs<ObjServerExchange>().routeMatch ?: ObjNull
}
bridgeProperty(this, "routeParams", mapType(stringType, stringType)) {
thisAs<ObjServerExchange>().routeParams.toObjMap()
}
bridgeFn(
this,
"jsonBody",
base.getInstanceMemberOrNull("jsonBody")?.typeDecl as? TypeDecl.Function
?: fnType(nullableAnyType),
callSignature = base.getInstanceMemberOrNull("jsonBody")?.callSignature
) {
val self = thisAs<ObjServerExchange>()
val targetType = resolveJsonTargetType(requireScope())
val text = self.request.body.decodeToString()
ObjJsonClass.decodeFromJsonElement(requireScope(), Json.parseToJsonElement(text), targetType)
}
bridgeFn(this, "respond", fnType(voidType, intType, nullableBufferType)) { bridgeFn(this, "respond", fnType(voidType, intType, nullableBufferType)) {
val self = thisAs<ObjServerExchange>() val self = thisAs<ObjServerExchange>()
val status = args.list.getOrNull(0)?.let { objToInt(this, it, "status") } ?: 200 val status = args.list.getOrNull(0)?.let { objToInt(this, it, "status") } ?: 200
@@ -398,6 +860,53 @@ private class ObjServerExchange(
self.setHttpResponse(status, bodyText.encodeToByteArray()) self.setHttpResponse(status, bodyText.encodeToByteArray())
ObjVoid ObjVoid
} }
bridgeFn(
this,
"respondJson",
base.getInstanceMemberOrNull("respondJson")?.typeDecl as? TypeDecl.Function
?: fnType(voidType, nullableAnyType, intType),
callSignature = base.getInstanceMemberOrNull("respondJson")?.callSignature
) {
val self = thisAs<ObjServerExchange>()
val body = args.list.getOrNull(0) ?: ObjNull
val status = args.list.getOrNull(1)?.let { objToInt(this, it, "status") } ?: 200
self.ensureMutable(this)
self.responseHeaders["Content-Type"] = mutableListOf("application/json; charset=utf-8")
val bodyText = if (body === ObjNull) {
"null"
} else {
(body.invokeInstanceMethod(requireScope(), "toJsonString") as ObjString).value
}
self.setHttpResponse(status, bodyText.encodeToByteArray())
ObjVoid
}
bridgeFn(
this,
"respondHtml",
base.getInstanceMemberOrNull("respondHtml")?.typeDecl as? TypeDecl.Function
?: fnType(voidType, intType, receiverFnType(htmlTagType, voidType)),
callSignature = receiverCallSignature("HtmlTag")
) {
val self = thisAs<ObjServerExchange>()
val first = args.list.getOrNull(0)
val second = args.list.getOrNull(1)
val status = args.named["code"]?.let { objToInt(this, it, "code") } ?: when {
first is ObjInt && second != null -> first.value.toInt()
second is ObjInt -> second.value.toInt()
else -> 200
}
val builder = when {
first is ObjInt -> second
else -> first
} ?: raiseIllegalArgument("respondHtml requires a builder")
val htmlModule = requireScope().importManager.createModuleScope(Pos.builtIn, "lyng.io.html")
val htmlFn = htmlModule.get("html")?.value ?: raiseIllegalState("lyng.io.html.html is not available")
val bodyText = (call(htmlFn, Arguments(listOf(builder))) as ObjString).value
self.ensureMutable(this)
self.responseHeaders["Content-Type"] = mutableListOf("text/html; charset=utf-8")
self.setHttpResponse(status, bodyText.encodeToByteArray())
ObjVoid
}
bridgeFn(this, "setHeader", fnType(voidType, stringType, stringType)) { bridgeFn(this, "setHeader", fnType(voidType, stringType, stringType)) {
val self = thisAs<ObjServerExchange>() val self = thisAs<ObjServerExchange>()
val name = requiredArg<ObjString>(0).value val name = requiredArg<ObjString>(0).value
@@ -417,14 +926,15 @@ private class ObjServerExchange(
bridgeFn( bridgeFn(
this, this,
"acceptWebSocket", "acceptWebSocket",
fnType(voidType, fnType(nullableAnyType, serverWebSocketType, serverExchangeType)) fnType(voidType, receiverFnType(requestContextType, nullableAnyType, serverWebSocketType)),
receiverCallSignature("RequestContext")
) { ) {
val self = thisAs<ObjServerExchange>() val self = thisAs<ObjServerExchange>()
val registered = captureCallable(requireScope(), args.list[0]) val registered = captureCallable(requireScope(), args.list[0])
self.ensureMutable(this) self.ensureMutable(this)
self.result = ExchangeResult.WebSocket( self.result = ExchangeResult.WebSocket(
HttpHandlerResult.WebSocket { session -> HttpHandlerResult.WebSocket { session ->
registered.call(ObjServerWebSocket(session), self) registered.callWithReceiver(self, ObjServerWebSocket(session))
} }
) )
ObjVoid ObjVoid
@@ -434,6 +944,7 @@ private class ObjServerExchange(
} }
} }
} }
}
private fun ensureMutable(scope: ScopeFacade) { private fun ensureMutable(scope: ScopeFacade) {
if (result !== ExchangeResult.Unhandled) { if (result !== ExchangeResult.Unhandled) {
@@ -512,3 +1023,17 @@ private fun objBufferOrNull(scope: ScopeFacade, value: Obj, name: String): ObjBu
is ObjBuffer -> value is ObjBuffer -> value
else -> scope.raiseClassCastError("$name must be Buffer or null") else -> scope.raiseClassCastError("$name must be Buffer or null")
} }
private fun resolveJsonTargetType(scope: Scope): TypeDecl {
val explicit = scope.args.explicitTypeArgs.singleOrNull()
if (explicit != null) return explicit
val bound = scope["T"]?.value
return when (bound) {
is ObjTypeExpr -> bound.typeDecl
is ObjClass -> TypeDecl.Simple(bound.className, false)
else -> scope.raiseIllegalArgument("jsonBody requires exactly one type argument")
}
}
private fun Map<String, String>.toObjMap(): ObjMap =
ObjMap(entries.associate { ObjString(it.key) to ObjString(it.value) }.toMutableMap())
@@ -17,10 +17,16 @@
package net.sergeych.lyng.io.process package net.sergeych.lyng.io.process
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import net.sergeych.lyng.ModuleScope import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.ScopeFacade import net.sergeych.lyng.ScopeFacade
import net.sergeych.lyng.Source
import net.sergeych.lyng.miniast.* import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.* import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager import net.sergeych.lyng.pacman.ImportManager
@@ -28,6 +34,24 @@ import net.sergeych.lyng.requireScope
import net.sergeych.lyngio.process.* import net.sergeych.lyngio.process.*
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
import net.sergeych.lyngio.process.security.ProcessAccessPolicy import net.sergeych.lyngio.process.security.ProcessAccessPolicy
import net.sergeych.lyngio.stdlib_included.processLyng
import kotlin.Boolean
import kotlin.Exception
import kotlin.Int
import kotlin.String
import kotlin.also
import kotlin.apply
import kotlin.collections.joinToString
import kotlin.collections.listOf
import kotlin.collections.map
import kotlin.collections.mutableListOf
import kotlin.collections.mutableMapOf
import kotlin.collections.toTypedArray
import kotlin.let
import kotlin.takeIf
import kotlin.text.isNotBlank
import kotlin.text.uppercase
import kotlin.to
/** /**
* Install Lyng module `lyng.io.process` into the given scope's ImportManager. * Install Lyng module `lyng.io.process` into the given scope's ImportManager.
@@ -47,6 +71,8 @@ fun createProcessModule(policy: ProcessAccessPolicy, manager: ImportManager): Bo
} }
private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAccessPolicy) { private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAccessPolicy) {
module.eval(Source("lyng.io.process", processLyng))
val runner = try { val runner = try {
SecuredLyngProcessRunner(getSystemProcessRunner(), policy) SecuredLyngProcessRunner(getSystemProcessRunner(), policy)
} catch (e: Exception) { } catch (e: Exception) {
@@ -54,6 +80,7 @@ private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAcces
} }
val runningProcessType = object : ObjClass("RunningProcess") {} val runningProcessType = object : ObjClass("RunningProcess") {}
val commandRunType = object : ObjClass("CommandRun") {}
runningProcessType.apply { runningProcessType.apply {
addFnDoc( addFnDoc(
@@ -149,6 +176,87 @@ private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAcces
} }
} }
commandRunType.apply {
addPropertyDoc(
name = "command",
doc = "Original shell command or argv-style command display text.",
type = type("lyng.String"),
moduleName = module.packageName,
getter = { ObjString(thisAs<ObjCommandRun>().command) }
)
addPropertyDoc(
name = "out",
doc = "Captured standard output as a string. Captures both stdout and stderr concurrently.",
type = type("lyng.String"),
moduleName = module.packageName,
getter = { ObjString(thisAs<ObjCommandRun>().captureAll().stdoutText) }
)
addPropertyDoc(
name = "err",
doc = "Captured standard error as a string. Captures both stdout and stderr concurrently.",
type = type("lyng.String"),
moduleName = module.packageName,
getter = { ObjString(thisAs<ObjCommandRun>().captureAll().stderrText) }
)
addPropertyDoc(
name = "lines",
doc = "Streaming standard output lines. Use this for large output instead of `out`.",
type = type("lyng.Flow"),
moduleName = module.packageName,
getter = { thisAs<ObjCommandRun>().process.stdout.toLyngFlow(this) }
)
addPropertyDoc(
name = "errorLines",
doc = "Streaming standard error lines. Use this for large stderr output instead of `err`.",
type = type("lyng.Flow"),
moduleName = module.packageName,
getter = { thisAs<ObjCommandRun>().process.stderr.toLyngFlow(this) }
)
addPropertyDoc(
name = "code",
doc = "Exit code after `wait`, `check`, `out`, or `err`; otherwise null.",
type = type("lyng.Int?"),
moduleName = module.packageName,
getter = {
val code = thisAs<ObjCommandRun>().knownExitCode()
code?.toObj() ?: ObjNull
}
)
addPropertyDoc(
name = "ok",
doc = "True if the known exit code is zero, false if non-zero, or null before the process is known to have exited.",
type = type("lyng.Bool?"),
moduleName = module.packageName,
getter = {
val code = thisAs<ObjCommandRun>().knownExitCode()
code?.let { (it == 0).toObj() } ?: ObjNull
}
)
addFnDoc(
name = "wait",
doc = "Wait for the process to exit and return its exit code.",
returns = type("lyng.Int"),
moduleName = module.packageName
) {
thisAs<ObjCommandRun>().waitFor().toObj()
}
addFnDoc(
name = "check",
doc = "Capture output, wait for completion, and fail if the exit code is non-zero.",
returns = type("CommandRun"),
moduleName = module.packageName
) {
val command = thisAs<ObjCommandRun>()
val captured = command.captureAll()
if (captured.exitCode != 0) {
val detail = captured.stderrText.takeIf { it.isNotBlank() } ?: captured.stdoutText
val suffix = detail.takeIf { it.isNotBlank() }?.let { ": $it" } ?: ""
raiseError("command failed with exit code ${captured.exitCode}: ${command.command}$suffix")
}
command
}
}
val platformType = object : ObjClass("Platform") {} val platformType = object : ObjClass("Platform") {}
platformType.apply { platformType.apply {
@@ -197,6 +305,45 @@ private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAcces
type = type("RunningProcess"), type = type("RunningProcess"),
moduleName = module.packageName moduleName = module.packageName
) )
module.addConstDoc(
name = "CommandRun",
value = commandRunType,
doc = "Shell-script friendly handle for a running command.",
type = type("CommandRun"),
moduleName = module.packageName
)
module.addFnDoc(
"sh",
doc = "Run a command via the system shell and return an active command handle.",
params = listOf(ParamDoc("command", type("lyng.String"))),
returns = type("CommandRun"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val command = requireOnlyArg<ObjString>().value
ObjCommandRun(commandRunType, command, runner.shell(command))
}
}
module.addFnDoc(
"exec",
doc = "Run an executable with argv-style arguments and return an active command handle.",
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
returns = type("CommandRun"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
if (args.list.size > 2) {
raiseError("Expected at most 2 arguments, got ${args.list.size}")
}
val executable = requiredArg<ObjString>(0).value
val rawArgs = if (args.list.size >= 2) requiredArg<ObjList>(1) else ObjList(mutableListOf())
val argv = rawArgs.list.map { (it as? ObjString)?.value ?: it.toString() }
ObjCommandRun(commandRunType, listOf(executable, *argv.toTypedArray()).joinToString(" "), runner.execute(executable, argv))
}
}
} }
class ObjRunningProcess( class ObjRunningProcess(
@@ -206,6 +353,49 @@ class ObjRunningProcess(
override fun toString(): String = "RunningProcess($process)" override fun toString(): String = "RunningProcess($process)"
} }
private data class CapturedCommandOutput(
val exitCode: Int,
val stdoutText: String,
val stderrText: String
)
private class ObjCommandRun(
override val objClass: ObjClass,
val command: String,
val process: LyngProcess
) : Obj() {
private val captureMutex = Mutex()
private var captured: CapturedCommandOutput? = null
private var exitCode: Int? = null
fun knownExitCode(): Int? = captured?.exitCode ?: exitCode
suspend fun waitFor(): Int {
captured?.let { return it.exitCode }
exitCode?.let { return it }
return process.waitFor().also { exitCode = it }
}
suspend fun captureAll(): CapturedCommandOutput = captureMutex.withLock {
captured?.let { return@withLock it }
coroutineScope {
val stdout = async { process.stdout.toList().joinToString("\n") }
val stderr = async { process.stderr.toList().joinToString("\n") }
val code = async { process.waitFor() }
CapturedCommandOutput(
exitCode = code.await(),
stdoutText = stdout.await(),
stderrText = stderr.await()
).also {
captured = it
exitCode = it.exitCode
}
}
}
override fun toString(): String = "CommandRun($command)"
}
private suspend inline fun ScopeFacade.processGuard(crossinline block: suspend () -> Obj): Obj { private suspend inline fun ScopeFacade.processGuard(crossinline block: suspend () -> Obj): Obj {
return try { return try {
block() block()
@@ -0,0 +1,100 @@
@file:OptIn(io.ktor.utils.io.InternalAPI::class)
package net.sergeych.lyngio.http
import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall
import io.ktor.client.plugins.HttpResponseRedirectEvent
import io.ktor.client.plugins.api.ClientPlugin
import io.ktor.client.plugins.api.Send
import io.ktor.client.plugins.api.createClientPlugin
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.authority
import io.ktor.http.isSecure
import io.ktor.http.takeFrom
private val redirectMethods = setOf(HttpMethod.Get, HttpMethod.Head)
internal class HttpRedirectLimitExceededException(
val limit: Int,
val initialUrl: String,
val lastUrl: String,
) : IllegalStateException(
"HTTP_REDIRECT_LIMIT_EXCEEDED: limit=$limit, initialUrl=$initialUrl, lastUrl=$lastUrl"
)
internal class CappedHttpRedirectConfig {
var maxRedirects: Int = LyngHttpConfig.DEFAULT_MAX_REDIRECTS
var checkHttpMethod: Boolean = true
var allowHttpsDowngrade: Boolean = false
}
internal val CappedHttpRedirect: ClientPlugin<CappedHttpRedirectConfig> = createClientPlugin(
"CappedHttpRedirect",
::CappedHttpRedirectConfig,
) {
val maxRedirects = pluginConfig.maxRedirects.also {
require(it >= 0) { "maxRedirects must not be negative" }
}
val checkHttpMethod = pluginConfig.checkHttpMethod
val allowHttpsDowngrade = pluginConfig.allowHttpsDowngrade
suspend fun Send.Sender.followRedirects(
context: HttpRequestBuilder,
origin: HttpClientCall,
client: HttpClient,
): HttpClientCall {
var call = origin
var requestBuilder = context
val initialUrl = origin.request.url.toString()
var redirectCount = 0
while (call.response.status.isRedirectStatus()) {
val location = call.response.headers[HttpHeaders.Location] ?: return call
val previousProtocol = call.request.url.protocol
val previousAuthority = call.request.url.authority
requestBuilder = HttpRequestBuilder().apply {
takeFrom(requestBuilder)
url.parameters.clear()
url.takeFrom(location)
if (!allowHttpsDowngrade && previousProtocol.isSecure() && !url.protocol.isSecure()) {
return call
}
if (previousAuthority != url.authority) {
headers.remove(HttpHeaders.Authorization)
}
}
if (redirectCount >= maxRedirects) {
throw HttpRedirectLimitExceededException(
maxRedirects,
initialUrl,
requestBuilder.url.buildString(),
)
}
redirectCount += 1
client.monitor.raise(HttpResponseRedirectEvent, call.response)
call = proceed(requestBuilder)
}
return call
}
on(Send) { request ->
val origin = proceed(request)
if (checkHttpMethod && origin.request.method !in redirectMethods) {
return@on origin
}
followRedirects(request, origin, client)
}
}
private fun HttpStatusCode.isRedirectStatus(): Boolean = when (value) {
HttpStatusCode.MovedPermanently.value,
HttpStatusCode.Found.value,
HttpStatusCode.TemporaryRedirect.value,
HttpStatusCode.PermanentRedirect.value,
HttpStatusCode.SeeOther.value -> true
else -> false
}
@@ -0,0 +1,34 @@
package net.sergeych.lyngio.http
/** Transport diagnostics, preserved by the Lyng bridge instead of a generic "http error". */
class LyngHttpRequestException(request: LyngHttpRequest, cause: Throwable) :
IllegalStateException(describeHttpFailure(request, cause), cause)
internal fun describeHttpFailure(request: LyngHttpRequest, cause: Throwable): String {
val causes = mutableListOf<Throwable>()
var current: Throwable? = cause
repeat(8) {
val item = current ?: return@repeat
if (causes.any { it === item }) return@repeat
causes += item
current = item.cause
}
val details = causes.joinToString(" <- ") { "${it::class.simpleName ?: "Exception"}: ${it.message ?: "no detail"}" }
val text = details.lowercase()
val (code, advice) = when {
"http_redirect_limit_exceeded" in text -> "HTTP_REDIRECT_LIMIT_EXCEEDED" to
"Do not retry this URL unchanged; inspect the redirect chain or use another source."
"tls" in text || "ssl" in text || "handshake" in text || "certificate" in text -> "HTTP_TLS_FAILED" to
"The HTTPS handshake failed before an HTTP response. Do not retry this URL unchanged; changing its path/query will not repair TLS. Use another HTTPS host or source."
"timeout" in text || "timed out" in text -> "HTTP_TIMEOUT" to
"The request exceeded a time limit. At most a bounded retry may help; avoid repeating it indefinitely."
"unknownhost" in text || "unresolvedaddress" in text || "name or service not known" in text -> "HTTP_DNS_FAILED" to
"The hostname could not be resolved. Verify the hostname or use another source."
"connectexception" in text || "connection refused" in text || "connection reset" in text -> "HTTP_CONNECTION_FAILED" to
"The connection failed before a usable response. Verify the endpoint; avoid repeated unchanged requests."
else -> "HTTP_REQUEST_FAILED" to "Use the cause details to choose another source or a different approach."
}
// Query strings and userinfo can contain credentials; the script already knows the full URL.
val target = request.url.substringBefore('?').substringBefore('#').replace(Regex("//[^/@]+@"), "//[redacted]@")
return "$code: ${request.method.uppercase()} $target\nCause: $details\n$advice"
}
@@ -5,6 +5,8 @@ import io.ktor.client.call.body
import io.ktor.client.engine.HttpClientEngineConfig import io.ktor.client.engine.HttpClientEngineConfig
import io.ktor.client.engine.HttpClientEngineFactory import io.ktor.client.engine.HttpClientEngineFactory
import io.ktor.client.plugins.timeout import io.ktor.client.plugins.timeout
import io.ktor.client.plugins.HttpTimeout
import kotlinx.coroutines.CancellationException
import io.ktor.client.request.HttpRequestBuilder import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.request import io.ktor.client.request.request
import io.ktor.client.request.setBody import io.ktor.client.request.setBody
@@ -14,14 +16,21 @@ import io.ktor.http.takeFrom
internal fun createKtorHttpEngine( internal fun createKtorHttpEngine(
engineFactory: HttpClientEngineFactory<HttpClientEngineConfig>, engineFactory: HttpClientEngineFactory<HttpClientEngineConfig>,
): LyngHttpEngine = KtorLyngHttpEngine(engineFactory) config: LyngHttpConfig,
): LyngHttpEngine = KtorLyngHttpEngine(engineFactory, config)
private class KtorLyngHttpEngine( private class KtorLyngHttpEngine(
engineFactory: HttpClientEngineFactory<HttpClientEngineConfig>, engineFactory: HttpClientEngineFactory<HttpClientEngineConfig>,
config: LyngHttpConfig,
) : LyngHttpEngine { ) : LyngHttpEngine {
private val clientResult = runCatching { private val clientResult = runCatching {
HttpClient(engineFactory) { HttpClient(engineFactory) {
expectSuccess = false expectSuccess = false
followRedirects = false
install(HttpTimeout)
install(CappedHttpRedirect) {
maxRedirects = config.maxRedirects
}
} }
} }
@@ -33,15 +42,22 @@ private class KtorLyngHttpEngine(
throw UnsupportedOperationException(it.message ?: "HTTP client is not supported") throw UnsupportedOperationException(it.message ?: "HTTP client is not supported")
} }
val response = httpClient.request { try {
applyRequest(request) val response = httpClient.request { applyRequest(request) }
}
return LyngHttpResponse( return LyngHttpResponse(
status = response.status.value, status = response.status.value,
statusText = response.status.description, statusText = response.status.description,
headers = response.headers.entries().associate { it.key to it.value.toList() }, headers = response.headers.entries().associate { it.key to it.value.toList() },
bodyBytes = response.body<ByteArray>(), bodyBytes = response.body<ByteArray>(),
) )
} catch (e: CancellationException) {
throw e
} catch (e: HttpRedirectLimitExceededException) {
// Keep the existing typed redirect-limit contract for host callers.
throw e
} catch (e: Exception) {
throw LyngHttpRequestException(request, e)
}
} }
private fun HttpRequestBuilder.applyRequest(request: LyngHttpRequest) { private fun HttpRequestBuilder.applyRequest(request: LyngHttpRequest) {
@@ -33,6 +33,18 @@ data class LyngHttpResponse(
val bodyBytes: ByteArray, val bodyBytes: ByteArray,
) )
data class LyngHttpConfig(
val maxRedirects: Int = DEFAULT_MAX_REDIRECTS,
) {
init {
require(maxRedirects >= 0) { "maxRedirects must not be negative" }
}
companion object {
const val DEFAULT_MAX_REDIRECTS: Int = 10
}
}
interface LyngHttpEngine { interface LyngHttpEngine {
val isSupported: Boolean val isSupported: Boolean
suspend fun request(request: LyngHttpRequest): LyngHttpResponse suspend fun request(request: LyngHttpRequest): LyngHttpResponse
@@ -46,4 +58,4 @@ internal object UnsupportedHttpEngine : LyngHttpEngine {
} }
} }
expect fun getSystemHttpEngine(): LyngHttpEngine expect fun getSystemHttpEngine(config: LyngHttpConfig = LyngHttpConfig()): LyngHttpEngine
@@ -33,7 +33,7 @@ internal suspend fun parseHttpRequest(
method = requestHead.method, method = requestHead.method,
target = requestHead.target, target = requestHead.target,
path = requestHead.path, path = requestHead.path,
query = requestHead.query, queryString = requestHead.queryString,
version = requestHead.version, version = requestHead.version,
headers = headers, headers = headers,
contentLength = contentLength, contentLength = contentLength,
@@ -48,7 +48,7 @@ private data class ParsedRequestLine(
val method: String, val method: String,
val target: String, val target: String,
val path: String, val path: String,
val query: String?, val queryString: String?,
val version: String, val version: String,
) )
@@ -75,8 +75,8 @@ private fun parseRequestLine(line: String, config: HttpServerConfig): ParsedRequ
} }
val queryAt = target.indexOf('?') val queryAt = target.indexOf('?')
val path = if (queryAt >= 0) target.substring(0, queryAt) else target val path = if (queryAt >= 0) target.substring(0, queryAt) else target
val query = if (queryAt >= 0) target.substring(queryAt + 1) else null val queryString = if (queryAt >= 0) target.substring(queryAt + 1) else null
return ParsedRequestLine(method = method, target = target, path = path, query = query, version = version) return ParsedRequestLine(method = method, target = target, path = path, queryString = queryString, version = version)
} }
private suspend fun parseHeaders( private suspend fun parseHeaders(
@@ -40,13 +40,36 @@ internal data class HttpRequestHead(
val method: String, val method: String,
val target: String, val target: String,
val path: String, val path: String,
val query: String?, val queryString: String?,
val version: String, val version: String,
val headers: HttpHeaders, val headers: HttpHeaders,
val contentLength: Int?, val contentLength: Int?,
val wantsClose: Boolean, val wantsClose: Boolean,
val wantsWebSocketUpgrade: Boolean, val wantsWebSocketUpgrade: Boolean,
) ) {
private var pathPartsParsed = false
private var pathPartsCache: List<String> = emptyList()
private var queryParsed = false
private var queryCache: Map<String, String> = emptyMap()
val pathParts: List<String>
get() {
if (!pathPartsParsed) {
pathPartsCache = parsePathParts(path)
pathPartsParsed = true
}
return pathPartsCache
}
val query: Map<String, String>
get() {
if (!queryParsed) {
queryCache = parseQueryParameters(queryString)
queryParsed = true
}
return queryCache
}
}
internal data class HttpRequest( internal data class HttpRequest(
val head: HttpRequestHead, val head: HttpRequestHead,
@@ -84,6 +107,89 @@ internal interface HttpServer {
fun close() fun close()
} }
internal fun parsePathParts(path: String): List<String> {
if (path.isEmpty() || path == "/") return emptyList()
val raw = if (path.startsWith('/')) path.substring(1) else path
if (raw.isEmpty()) return emptyList()
return raw.split('/').map(::decodePathSegment)
}
internal fun parseQueryParameters(queryString: String?): Map<String, String> {
if (queryString.isNullOrEmpty()) return emptyMap()
val result = linkedMapOf<String, String>()
var start = 0
while (start <= queryString.length) {
val nextAmp = queryString.indexOf('&', start).let { if (it >= 0) it else queryString.length }
if (nextAmp > start) {
val part = queryString.substring(start, nextAmp)
val eqAt = part.indexOf('=')
val rawKey = if (eqAt >= 0) part.substring(0, eqAt) else part
val rawValue = if (eqAt >= 0) part.substring(eqAt + 1) else ""
result[decodeQueryComponent(rawKey, plusAsSpace = true)] = decodeQueryComponent(rawValue, plusAsSpace = true)
}
if (nextAmp == queryString.length) break
start = nextAmp + 1
}
return result
}
internal fun decodePathSegment(value: String): String = decodeQueryComponent(value, plusAsSpace = false)
private fun decodeQueryComponent(value: String, plusAsSpace: Boolean): String {
if (value.isEmpty()) return value
val out = StringBuilder(value.length)
val bytes = ArrayList<Byte>()
fun flushBytes() {
if (bytes.isEmpty()) return
out.append(bytes.toByteArray().decodeToString())
bytes.clear()
}
var i = 0
while (i < value.length) {
when (val ch = value[i]) {
'+' -> {
flushBytes()
out.append(if (plusAsSpace) ' ' else '+')
i += 1
}
'%' -> {
val decoded = decodePercentByte(value, i)
if (decoded != null) {
bytes += decoded.first.toByte()
i = decoded.second
} else {
flushBytes()
out.append('%')
i += 1
}
}
else -> {
flushBytes()
out.append(ch)
i += 1
}
}
}
flushBytes()
return out.toString()
}
private fun decodePercentByte(value: String, offset: Int): Pair<Int, Int>? {
if (offset + 2 >= value.length) return null
val hi = value[offset + 1].hexDigitValueOrNull() ?: return null
val lo = value[offset + 2].hexDigitValueOrNull() ?: return null
return ((hi shl 4) or lo) to (offset + 3)
}
private fun Char.hexDigitValueOrNull(): Int? = when (this) {
in '0'..'9' -> code - '0'.code
in 'a'..'f' -> code - 'a'.code + 10
in 'A'..'F' -> code - 'A'.code + 10
else -> null
}
internal fun defaultReason(status: Int): String = when (status) { internal fun defaultReason(status: Int): String = when (status) {
101 -> "Switching Protocols" 101 -> "Switching Protocols"
200 -> "OK" 200 -> "OK"
@@ -87,8 +87,8 @@ private class StartedHttpServer(
withTimeout(config.keepAliveTimeoutMillis) { withTimeout(config.keepAliveTimeoutMillis) {
parseHttpRequest(reader, config) parseHttpRequest(reader, config)
} }
} catch (_: CancellationException) { } catch (e: CancellationException) {
throw CancellationException() throw e
} catch (e: HttpProtocolException) { } catch (e: HttpProtocolException) {
safeWriteError(socket, e.status, e.message ?: defaultReason(e.status)) safeWriteError(socket, e.status, e.message ?: defaultReason(e.status))
break break
@@ -99,8 +99,8 @@ private class StartedHttpServer(
val result = try { val result = try {
handler.handle(request) handler.handle(request)
} catch (_: CancellationException) { } catch (e: CancellationException) {
throw CancellationException() throw e
} catch (_: Throwable) { } catch (_: Throwable) {
HttpHandlerResult.Response(HttpResponse(status = 500, close = true)) HttpHandlerResult.Response(HttpResponse(status = 500, close = true))
} }
@@ -0,0 +1,196 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.io.html
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.obj.ObjString
import net.sergeych.lyng.pacman.ImportManager
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class LyngHtmlModuleTest {
@Test
fun testModuleRegistrationIsIdempotent() = runTest {
val importManager = ImportManager()
assertTrue(createHtmlModule(importManager))
assertFalse(createHtmlModule(importManager))
}
@Test
fun testModuleCanBeImported() = runTest {
val scope = Script.newScope()
createHtmlModule(scope.importManager)
val result = Compiler.compile(
Source(
"<html-test>",
"""
import lyng.io.html
42
""".trimIndent()
),
scope.importManager
).execute(scope)
assertEquals("42", result.inspect(scope))
}
@Test
fun testHtmlDslBuildsNestedDocument() = runTest {
val scope = Script.newScope()
createHtmlModule(scope.importManager)
val result = Compiler.compile(
Source(
"<html-dsl-test>",
"""
import lyng.io.html
html {
head {
title { +"Demo" }
}
body {
h3 { +"Heading 3" }
p {
attr("data-x", "\"quoted\" & <tag>")
+"Text & <more>"
}
}
}
""".trimIndent()
),
scope.importManager
).execute(scope)
assertEquals(
"<!doctype html><html><head><title>Demo</title></head><body><h3>Heading 3</h3><p data-x=\"&quot;quoted&quot; &amp; &lt;tag&gt;\">Text &amp; &lt;more&gt;</p></body></html>",
(result as ObjString).value
)
}
@Test
fun testHtmlDslSupportsRawAndVoidTags() = runTest {
val scope = Script.newScope()
createHtmlModule(scope.importManager)
val result = Compiler.compile(
Source(
"<html-void-test>",
"""
import lyng.io.html
html {
head {
meta { attr("charset", "utf-8") }
}
body {
div {
id("root")
classes("app shell")
raw("<span>trusted</span>")
br {}
}
}
}
""".trimIndent()
),
scope.importManager
).execute(scope)
assertEquals(
"<!doctype html><html><head><meta charset=\"utf-8\"></head><body><div id=\"root\" class=\"app shell\"><span>trusted</span><br></div></body></html>",
(result as ObjString).value
)
}
@Test
fun testHtmlDslTypedAttributeHelpers() = runTest {
val scope = Script.newScope()
createHtmlModule(scope.importManager)
val result = Compiler.compile(
Source(
"<html-typed-attrs-test>",
"""
import lyng.io.html
html {
head {
metaCharset()
stylesheet("/site.css")
}
body {
nav {
a(href: "/home") { +"Home" }
}
img(src: "/logo.png", alt: "Logo & mark")
input(type: "hidden", name: "token", value: "\"abc\"")
}
}
""".trimIndent()
),
scope.importManager
).execute(scope)
assertEquals(
"<!doctype html><html><head><meta charset=\"utf-8\"><link rel=\"stylesheet\" href=\"/site.css\"></head><body><nav><a href=\"/home\">Home</a></nav><img src=\"/logo.png\" alt=\"Logo &amp; mark\"><input type=\"hidden\" name=\"token\" value=\"&quot;abc&quot;\"></body></html>",
(result as ObjString).value
)
}
@Test
fun testHtmlDslGenericTagsAndFlagAttributes() = runTest {
val scope = Script.newScope()
createHtmlModule(scope.importManager)
val result = Compiler.compile(
Source(
"<html-generic-tag-test>",
"""
import lyng.io.html
html {
body {
tag("custom-element") {
flag("hidden")
+"Secret"
}
voidTag("source") {
attr("srcset", "/image.webp")
attr("type", "image/webp")
}
}
}
""".trimIndent()
),
scope.importManager
).execute(scope)
assertEquals(
"<!doctype html><html><body><custom-element hidden>Secret</custom-element><source srcset=\"/image.webp\" type=\"image/webp\"></body></html>",
(result as ObjString).value
)
}
}
@@ -0,0 +1,59 @@
package net.sergeych.lyng.io.html
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.pacman.ImportManager
import kotlin.test.*
class LyngHtmlParseModuleTest {
private suspend fun execute(source: String) {
val scope = Script.newScope()
createHtmlParseModule(scope)
Compiler.compile(Source("html-parse-test", source.trimIndent()), scope.importManager).execute(scope)
}
@Test fun registrationIsIdempotent() {
val manager = ImportManager()
assertTrue(createHtmlParseModule(manager))
assertFalse(createHtmlParseModule(manager))
}
@Test fun parsesMalformedHtmlAndExtractsTextWithoutChangingDom() = runTest {
execute("""
import lyng.io.html.parse
val doc = Html.parse("<title>A &amp; B</title><style>bad css</style><body><h1>Heading</h1><p>Hello <b>world</b> &amp; &#65;<br>Next<p>Last<script>bad script</script><div hidden>secret</div><template>bad template</template><noscript>bad fallback</noscript>")
assert(doc.title == "A & B")
assert(doc.text() == "Heading\nHello world & A\nNext\nLast")
assert(doc.select("p").size == 2)
assert(doc.select("script").size == 1)
assert(doc.select("#missing").size == 0)
assert(doc.text() == doc.text())
""")
}
@Test fun selectorsAttributesBaseAndNestedSelection() = runTest {
execute("""
import lyng.io.html.parse
val doc = Html.parse("<base href='/docs/'><article><a class='ref' href='intro?a=1&amp;b=2'>Read &amp; learn</a></article>", "https://example.com/start")
val articles = doc.select("article")
val article = articles[0] as HtmlElement
val links = article.select("a.ref[href]")
val link = links[0] as HtmlElement
assert(link.text() == "Read & learn")
assert(link.attr("href") == "intro?a=1&b=2")
assert(link.absUrl("href") == "https://example.com/docs/intro?a=1&b=2")
assert(link.attr("missing") == "")
assert(link.absUrl("missing") == "")
val fragment = Html.parse("<p>One</p><p>Two</p>")
assert(fragment.text() == "One\nTwo")
assert(fragment.title == "")
assert(Html.parse("").text() == "")
""")
}
@Test fun badSelectorFailsClearly() = runTest {
assertFails { execute("import lyng.io.html.parse\nHtml.parse(\"<p>Hi\").select(\"[\")") }
}
}
@@ -0,0 +1,25 @@
package net.sergeych.lyngio.http
import kotlin.test.*
class HttpRequestFailureTest {
private val request = LyngHttpRequest("GET", "https://user:secret@example.com/page?key=private#fragment")
@Test fun savedFatalTlsAlertGetsSpecificCauseAndNoUnchangedRetryAdvice() {
val saved = IllegalStateException("Received alert during handshake. Level: FATAL, code: InternalError")
val message = LyngHttpRequestException(request, saved).message!!
assertTrue(message.startsWith("HTTP_TLS_FAILED: GET https://[redacted]@example.com/page"))
assertTrue(message.contains("FATAL, code: InternalError"))
assertTrue(message.contains("Do not retry this URL unchanged"))
assertTrue(message.contains("changing its path/query will not repair TLS"))
assertFalse(message.contains("secret")); assertFalse(message.contains("private"))
}
@Test fun nestedCausesAndTimeoutDnsAndConnectionFailuresRemainDistinct() {
val nested = IllegalStateException("request failed", IllegalArgumentException("TLS certificate rejected"))
assertTrue(describeHttpFailure(request, nested).contains("HTTP_TLS_FAILED"))
for ((detail, code) in listOf("request timed out" to "HTTP_TIMEOUT",
"UnknownHostException: example.com" to "HTTP_DNS_FAILED",
"Connection refused" to "HTTP_CONNECTION_FAILED")) {
assertTrue(describeHttpFailure(request, IllegalStateException(detail)).startsWith(code))
}
}
}
@@ -71,6 +71,39 @@ class HttpParserTest {
assertEquals("ping", request.body.decodeToString()) assertEquals("ping", request.body.decodeToString())
} }
@Test
fun queryStringAndDecodedQueryMapAreExposed() = kotlinx.coroutines.test.runTest {
val request = parse(
"GET /echo?a=1&b=hello+world&b=last&utf=%D1%82%D0%B5%D1%81%D1%82&bad=%GG%2&flag HTTP/1.1\r\n" +
"Host: localhost\r\n\r\n"
)
assertEquals("a=1&b=hello+world&b=last&utf=%D1%82%D0%B5%D1%81%D1%82&bad=%GG%2&flag", request.head.queryString)
assertEquals("1", request.head.query["a"])
assertEquals("last", request.head.query["b"])
assertEquals("тест", request.head.query["utf"])
assertEquals("%GG%2", request.head.query["bad"])
assertEquals("", request.head.query["flag"])
}
@Test
fun missingQueryProducesEmptyMap() = kotlinx.coroutines.test.runTest {
val request = parse(
"GET /echo HTTP/1.1\r\n" +
"Host: localhost\r\n\r\n"
)
assertEquals(null, request.head.queryString)
assertEquals(emptyMap(), request.head.query)
}
@Test
fun pathPartsAreLazyDecodedWithoutPlusTranslation() = kotlinx.coroutines.test.runTest {
val request = parse(
"GET /one/two%20words/a+b/%GG/%D1%82%D0%B5%D1%81%D1%82 HTTP/1.1\r\n" +
"Host: localhost\r\n\r\n"
)
assertEquals(listOf("one", "two words", "a+b", "%GG", "тест"), request.head.pathParts)
}
private suspend fun parse( private suspend fun parse(
rawRequest: String, rawRequest: String,
config: HttpServerConfig = HttpServerConfig(), config: HttpServerConfig = HttpServerConfig(),
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
import io.ktor.client.engine.darwin.Darwin import io.ktor.client.engine.darwin.Darwin
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(Darwin) actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(Darwin, config)
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
import io.ktor.client.engine.js.Js import io.ktor.client.engine.js.Js
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(Js) actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(Js, config)
@@ -237,6 +237,10 @@ object JvmLyngConsole : LyngConsole {
val attrs = term.attributes val attrs = term.attributes
attrs.setLocalFlag(Attributes.LocalFlag.ICANON, false) attrs.setLocalFlag(Attributes.LocalFlag.ICANON, false)
attrs.setLocalFlag(Attributes.LocalFlag.ECHO, false) attrs.setLocalFlag(Attributes.LocalFlag.ECHO, false)
attrs.setLocalFlag(Attributes.LocalFlag.IEXTEN, false)
attrs.setLocalFlag(Attributes.LocalFlag.ISIG, false)
attrs.setInputFlag(Attributes.InputFlag.ICRNL, false)
attrs.setInputFlag(Attributes.InputFlag.IXON, false)
attrs.setControlChar(Attributes.ControlChar.VMIN, 0) attrs.setControlChar(Attributes.ControlChar.VMIN, 0)
attrs.setControlChar(Attributes.ControlChar.VTIME, 1) attrs.setControlChar(Attributes.ControlChar.VTIME, 1)
term.setAttributes(attrs) term.setAttributes(attrs)
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(CIO) actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(CIO, config)
@@ -51,12 +51,12 @@ class LyngHttpServerModuleTest {
import lyng.io.http.server import lyng.io.http.server
val server = HttpServer() val server = HttpServer()
server.get("/hello") { ex -> server.get("/hello") {
ex.setHeader("Content-Type", "text/plain") setHeader("Content-Type", "text/plain")
ex.respondText(200, "hello from lyng") respondText(200, "hello from lyng")
} }
server.fallback { ex -> server.fallback {
ex.respondText(404, "miss:" + ex.request.path) respondText(404, "miss:" + request.path)
} }
server.listen(0, "127.0.0.1") server.listen(0, "127.0.0.1")
""".trimIndent() """.trimIndent()
@@ -89,6 +89,349 @@ class LyngHttpServerModuleTest {
handle.invokeInstanceMethod(scope, "close") handle.invokeInstanceMethod(scope, "close")
} }
@Test
fun requestQueryStringAndQueryMapAreAvailableToLyng() = runBlocking {
val engine = getSystemNetEngine()
if (!engine.isSupported || !engine.isTcpAvailable) return@runBlocking
val scope = Script.newScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
val code = """
import lyng.io.http.server
val server = HttpServer()
server.get("/query") {
val q = request.query
respondText(
200,
(request.queryString ?: "<null>") +
"|" + q.size +
"|" + (q["a"] ?: "<null>") +
"|" + (q["b"] ?: "<null>") +
"|" + (q["utf"] ?: "<null>") +
"|" + (q["bad"] ?: "<null>") +
"|" + (q["flag"] ?: "<null>")
)
}
server.listen(0, "127.0.0.1")
""".trimIndent()
val handle = Compiler.compile(code).execute(scope)
val port = waitForPort(handle, scope)
val client = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client.writeUtf8(
"GET /query?a=1&b=first&b=last&utf=%D1%82%D0%B5%D1%81%D1%82&bad=%GG%2&flag HTTP/1.1\r\n" +
"Host: localhost\r\nConnection: close\r\n\r\n"
)
client.flush()
val response = readHttpResponse(client)
assertTrue(response.contains("200 OK"), response)
assertTrue(
response.endsWith(
"a=1&b=first&b=last&utf=%D1%82%D0%B5%D1%81%D1%82&bad=%GG%2&flag|5|1|last|тест|%GG%2|"
),
response
)
} finally {
client.close()
}
val client2 = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client2.writeUtf8("GET /query HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client2.flush()
val response = readHttpResponse(client2)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("<null>|0|<null>|<null>|<null>|<null>|<null>"), response)
} finally {
client2.close()
}
handle.invokeInstanceMethod(scope, "close")
}
@Test
fun regexRoutesExposeMatchAndPathPartsToLyng() = runBlocking {
val engine = getSystemNetEngine()
if (!engine.isSupported || !engine.isTcpAvailable) return@runBlocking
val scope = Script.newScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
val code = """
import lyng.io.http.server
val server = HttpServer()
server.get("^/users/([0-9]+)/posts/([0-9]+)$".re) {
val m = routeMatch!!
respondText(
200,
m[1] +
"|" + m[2] +
"|" + request.pathParts[0] +
"," + request.pathParts[1] +
"," + request.pathParts[2] +
"," + request.pathParts[3]
)
}
server.get("/users/fixed/posts/9") {
respondText(200, "fixed|" + (routeMatch == null))
}
server.listen(0, "127.0.0.1")
""".trimIndent()
val handle = Compiler.compile(code).execute(scope)
val port = waitForPort(handle, scope)
val client = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client.writeUtf8("GET /users/42/posts/7 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client.flush()
val response = readHttpResponse(client)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("42|7|users,42,posts,7"), response)
} finally {
client.close()
}
val client2 = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client2.writeUtf8("GET /users/fixed/posts/9 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client2.flush()
val response = readHttpResponse(client2)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("fixed|true"), response)
} finally {
client2.close()
}
handle.invokeInstanceMethod(scope, "close")
}
@Test
fun pathTemplateRoutesExposeDecodedRouteParamsAndKeepExactRoutesFirst() = runBlocking {
val engine = getSystemNetEngine()
if (!engine.isSupported || !engine.isTcpAvailable) return@runBlocking
val scope = Script.newScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
val code = """
import lyng.io.http.server
val server = HttpServer()
server.get("/users/fixed/posts/9") {
respondText(200, "fixed|" + routeParams.size)
}
server.getPath("/users/{userId}/posts/{postId}") {
respondText(
200,
routeParams["userId"] + "|" +
routeParams["postId"] + "|" +
request.pathParts[1] + "|" +
request.pathParts[3] + "|" +
(routeMatch != null)
)
}
server.listen(0, "127.0.0.1")
""".trimIndent()
val handle = Compiler.compile(code).execute(scope)
val port = waitForPort(handle, scope)
val client = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client.writeUtf8("GET /users/alice%20bob/posts/c+d HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client.flush()
val response = readHttpResponse(client)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("alice bob|c+d|alice bob|c+d|true"), response)
} finally {
client.close()
}
val client2 = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client2.writeUtf8("GET /users/fixed/posts/9 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client2.flush()
val response = readHttpResponse(client2)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("fixed|0"), response)
} finally {
client2.close()
}
handle.invokeInstanceMethod(scope, "close")
}
@Test
fun jsonBodyAndRespondJsonSupportTypedJsonPostHandlers() = runBlocking {
val engine = getSystemNetEngine()
if (!engine.isSupported || !engine.isTcpAvailable) return@runBlocking
val scope = Script.newScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
val code = """
import lyng.io.http.server
closed class CreateUserRequest(name: String, age: Int)
closed class CreateUserResponse(id: Int, name: String, age: Int)
val server = HttpServer()
server.postPath("/api/users") {
val req = jsonBody<CreateUserRequest>()
respondJson(CreateUserResponse(101, req.name, req.age), 201)
}
server.listen(0, "127.0.0.1")
""".trimIndent()
val handle = Compiler.compile(code).execute(scope)
val port = waitForPort(handle, scope)
val client = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
val body = """{"name":"alice","age":30}"""
client.writeUtf8(
"POST /api/users HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: ${body.encodeToByteArray().size}\r\n" +
"Connection: close\r\n\r\n" +
body
)
client.flush()
val response = readHttpResponse(client)
assertTrue(response.contains("201"), response)
assertTrue(response.contains("Content-Type: application/json; charset=utf-8"), response)
assertTrue(response.endsWith("""{"id":101,"name":"alice","age":30}"""), response)
} finally {
client.close()
}
handle.invokeInstanceMethod(scope, "close")
}
@Test
fun respondHtmlRendersHtmlDslAndSetsContentType() = runBlocking {
val engine = getSystemNetEngine()
if (!engine.isSupported || !engine.isTcpAvailable) return@runBlocking
val scope = Script.newScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
val code = """
import lyng.io.http.server
import lyng.io.html
val server = HttpServer()
server.getPath("/html/{name}") {
respondHtml(code: 202) {
head { title { +"Greeting" } }
body {
h3 { +("Hello, " + routeParams["name"]) }
}
}
}
server.listen(0, "127.0.0.1")
""".trimIndent()
val handle = Compiler.compile(code).execute(scope)
val port = waitForPort(handle, scope)
val client = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client.writeUtf8("GET /html/alice%26bob HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client.flush()
val response = readHttpResponse(client)
assertTrue(response.contains("202"), response)
assertTrue(response.contains("Content-Type: text/html; charset=utf-8"), response)
assertTrue(
response.endsWith(
"<!doctype html><html><head><title>Greeting</title></head><body><h3>Hello, alice&amp;bob</h3></body></html>"
),
response
)
} finally {
client.close()
}
handle.invokeInstanceMethod(scope, "close")
}
@Test
fun routerMountPreservesBuiltInRoutingSemantics() = runBlocking {
val engine = getSystemNetEngine()
if (!engine.isSupported || !engine.isTcpAvailable) return@runBlocking
val scope = Script.newScope()
createHttpServerModule(PermitAllNetAccessPolicy, scope)
val code = """
import lyng.io.http.server
val api = Router()
api.get("/health") {
respondText(200, "ok")
}
val users = Router()
users.getPath("/users/{id}") {
respondText(200, "user:" + routeParams["id"])
}
users.fallback {
respondText(404, "router-miss:" + request.path)
}
api.mount(users)
val server = HttpServer()
server.mount(api)
server.listen(0, "127.0.0.1")
""".trimIndent()
val handle = Compiler.compile(code).execute(scope)
val port = waitForPort(handle, scope)
val client = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client.writeUtf8("GET /health HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client.flush()
val response = readHttpResponse(client)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("ok"), response)
} finally {
client.close()
}
val client2 = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client2.writeUtf8("GET /users/alice HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client2.flush()
val response = readHttpResponse(client2)
assertTrue(response.contains("200 OK"), response)
assertTrue(response.endsWith("user:alice"), response)
} finally {
client2.close()
}
val client3 = engine.tcpConnect("127.0.0.1", port, 2_000, true)
try {
client3.writeUtf8("GET /missing HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
client3.flush()
val response = readHttpResponse(client3)
assertTrue(response.contains("404"), response)
assertTrue(response.endsWith("router-miss:/missing"), response)
} finally {
client3.close()
}
handle.invokeInstanceMethod(scope, "close")
}
private suspend fun waitForPort(handle: Obj, scope: net.sergeych.lyng.Scope): Int { private suspend fun waitForPort(handle: Obj, scope: net.sergeych.lyng.Scope): Int {
repeat(100) { repeat(100) {
val port = runCatching { val port = runCatching {
@@ -19,9 +19,11 @@ package net.sergeych.lyng.io.process
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler import net.sergeych.lyng.Compiler
import net.sergeych.lyng.ExecutionError
import net.sergeych.lyng.Script import net.sergeych.lyng.Script
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue import kotlin.test.assertTrue
class LyngProcessModuleTest { class LyngProcessModuleTest {
@@ -84,4 +86,78 @@ class LyngProcessModuleTest {
val result = script.execute(scope) val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("name"), "Result should contain 'name', but was: ${result.inspect(scope)}") assertTrue(result.inspect(scope).contains("name"), "Result should contain 'name', but was: ${result.inspect(scope)}")
} }
@Test
fun testShellSugarCapturesOutput() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
val r = sh("echo shell-sugar")
assert(r.out.trim() == "shell-sugar")
assert(r.code == 0)
assert(r.ok == true)
true
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("true"))
}
@Test
fun testShellSugarStreamsLines() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
val lines: List<String> = []
for (line in sh("echo one && echo two").lines) {
lines.add(line)
}
lines.joinToString(",")
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("one,two"))
}
@Test
fun testExecSugarCapturesOutput() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
exec("echo", ["exec-sugar"]).out.trim()
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("exec-sugar"))
}
@Test
fun testShellSugarCheckFailsOnNonZeroExit() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
sh("echo check-failed && exit 7").check()
""".trimIndent()
val script = Compiler.compile(code)
assertFailsWith<ExecutionError> {
script.execute(scope)
}
Unit
}
} }
@@ -0,0 +1,73 @@
package net.sergeych.lyngio.http
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpServer
import io.ktor.client.engine.cio.CIO
import kotlinx.coroutines.runBlocking
import java.net.InetSocketAddress
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class KtorHttpRedirectTest {
@Test
fun redirectsWithinLimitRemainTransparent() = runBlocking {
val server = newServer { exchange ->
when (exchange.requestURI.path) {
"/start" -> redirect(exchange, "/middle")
"/middle" -> redirect(exchange, "/result")
else -> writeResponse(exchange, 200, "redirected")
}
}
try {
val engine = createKtorHttpEngine(CIO, LyngHttpConfig(maxRedirects = 2))
val response = engine.request(LyngHttpRequest("GET", url(server, "/start")))
assertEquals(200, response.status)
assertEquals("redirected", response.bodyBytes.decodeToString())
} finally {
server.stop(0)
}
}
@Test
fun redirectLimitProducesDiagnosticError() = runBlocking {
val server = newServer { exchange ->
val next = exchange.requestURI.path.removePrefix("/").toIntOrNull()?.plus(1) ?: 1
redirect(exchange, "/$next")
}
try {
val engine = createKtorHttpEngine(CIO, LyngHttpConfig(maxRedirects = 2))
val error = assertFailsWith<HttpRedirectLimitExceededException> {
engine.request(LyngHttpRequest("GET", url(server, "/0")))
}
assertEquals(2, error.limit)
assertTrue(error.initialUrl.endsWith("/0"), error.initialUrl)
assertTrue(error.lastUrl.endsWith("/3"), error.lastUrl)
assertTrue(error.message.orEmpty().startsWith("HTTP_REDIRECT_LIMIT_EXCEEDED:"), error.message)
} finally {
server.stop(0)
}
}
private fun newServer(handler: (HttpExchange) -> Unit): HttpServer =
HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0).apply {
createContext("/") { exchange -> handler(exchange) }
start()
}
private fun url(server: HttpServer, path: String) =
"http://127.0.0.1:${server.address.port}$path"
private fun redirect(exchange: HttpExchange, location: String) {
exchange.responseHeaders.add("Location", location)
exchange.sendResponseHeaders(302, -1)
exchange.close()
}
private fun writeResponse(exchange: HttpExchange, status: Int, body: String) {
val bytes = body.encodeToByteArray()
exchange.sendResponseHeaders(status, bytes.size.toLong())
exchange.responseBody.use { it.write(bytes) }
}
}
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
import io.ktor.client.engine.curl.Curl import io.ktor.client.engine.curl.Curl
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(Curl) actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(Curl, config)
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
import io.ktor.client.engine.winhttp.WinHttp import io.ktor.client.engine.winhttp.WinHttp
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(WinHttp) actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(WinHttp, config)
+142
View File
@@ -0,0 +1,142 @@
package lyng.io.html
import lyng.stdlib
fun escapeHtml(text: String): String {
val amp: String = text.replace("&", "&amp;")
val lt: String = amp.replace("<", "&lt;")
lt.replace(">", "&gt;")
}
fun escapeHtmlAttr(text: String): String {
val escaped: String = escapeHtml(text)
val quoted: String = escaped.replace("\"", "&quot;")
quoted.replace("'", "&#39;")
}
class HtmlTag(name: String, isVoid: Bool = false) {
val name = name
val isVoid = isVoid
var attributes = ""
var inner = ""
fun attr(name: String, value: Object): HtmlTag {
attributes += " " + name + "=\"" + escapeHtmlAttr(value.toString()) + "\""
this
}
fun flag(name: String): HtmlTag {
attributes += " " + name
this
}
fun id(value: String): HtmlTag = attr("id", value)
fun classes(value: String): HtmlTag = attr("class", value)
fun addText(text: String): void {
inner += escapeHtml(text)
}
fun raw(html: String): void {
inner += html
}
fun child(tagName: String, block: HtmlTag.()->void): void {
val child = HtmlTag(tagName)
with(child) { block(this) }
inner += child.render()
}
fun voidChild(tagName: String, block: HtmlTag.()->void): void {
val child = HtmlTag(tagName, true)
with(child) { block(this) }
inner += child.render()
}
fun tag(name: String, block: HtmlTag.()->void): void { child(name, block) }
fun voidTag(name: String, block: HtmlTag.()->void): void { voidChild(name, block) }
fun render(): String {
if (isVoid) "<" + name + attributes + ">"
else "<" + name + attributes + ">" + inner + "</" + name + ">"
}
fun head(block: HtmlTag.()->void): void { child("head", block) }
fun body(block: HtmlTag.()->void): void { child("body", block) }
fun title(block: HtmlTag.()->void): void { child("title", block) }
fun main(block: HtmlTag.()->void): void { child("main", block) }
fun section(block: HtmlTag.()->void): void { child("section", block) }
fun article(block: HtmlTag.()->void): void { child("article", block) }
fun header(block: HtmlTag.()->void): void { child("header", block) }
fun footer(block: HtmlTag.()->void): void { child("footer", block) }
fun nav(block: HtmlTag.()->void): void { child("nav", block) }
fun div(block: HtmlTag.()->void): void { child("div", block) }
fun span(block: HtmlTag.()->void): void { child("span", block) }
fun p(block: HtmlTag.()->void): void { child("p", block) }
fun a(href: String, block: HtmlTag.()->void): void {
child("a") {
attr("href", href)
block(this)
}
}
fun ul(block: HtmlTag.()->void): void { child("ul", block) }
fun ol(block: HtmlTag.()->void): void { child("ol", block) }
fun li(block: HtmlTag.()->void): void { child("li", block) }
fun h1(block: HtmlTag.()->void): void { child("h1", block) }
fun h2(block: HtmlTag.()->void): void { child("h2", block) }
fun h3(block: HtmlTag.()->void): void { child("h3", block) }
fun h4(block: HtmlTag.()->void): void { child("h4", block) }
fun h5(block: HtmlTag.()->void): void { child("h5", block) }
fun h6(block: HtmlTag.()->void): void { child("h6", block) }
fun strong(block: HtmlTag.()->void): void { child("strong", block) }
fun em(block: HtmlTag.()->void): void { child("em", block) }
fun code(block: HtmlTag.()->void): void { child("code", block) }
fun pre(block: HtmlTag.()->void): void { child("pre", block) }
fun script(block: HtmlTag.()->void): void { child("script", block) }
fun style(block: HtmlTag.()->void): void { child("style", block) }
fun meta(block: HtmlTag.()->void): void { voidChild("meta", block) }
fun link(block: HtmlTag.()->void): void { voidChild("link", block) }
fun img(block: HtmlTag.()->void): void { voidChild("img", block) }
fun br(block: HtmlTag.()->void): void { voidChild("br", block) }
fun input(block: HtmlTag.()->void): void { voidChild("input", block) }
fun metaCharset(charset: String = "utf-8"): void {
meta { attr("charset", charset) }
}
fun stylesheet(href: String): void {
link {
attr("rel", "stylesheet")
attr("href", href)
}
}
fun img(src: String, alt: String = ""): void {
voidChild("img") {
attr("src", src)
if (alt != "") attr("alt", alt)
}
}
fun input(type: String, name: String = "", value: String = ""): void {
voidChild("input") {
attr("type", type)
if (name != "") attr("name", name)
if (value != "") attr("value", value)
}
}
}
context(HtmlTag)
fun String.unaryPlus(): void {
this@HtmlTag.addText(this)
}
fun html(block: HtmlTag.()->void): String {
val root = HtmlTag("html")
with(root) { block(this) }
"<!doctype html>" + root.render()
}
+31
View File
@@ -0,0 +1,31 @@
package lyng.io.html.parse
/* Offline HTML5 parsing. Never fetches resources or executes JavaScript. */
extern object Html {
/* Parse a document or fragment; baseUrl resolves relative attributes (no request). */
fun parse(source: String, baseUrl: String = ""): HtmlDocument
}
/* Parsed document. Retains the original DOM for selection and attributes. */
extern class HtmlDocument {
/* Decoded document title, or an empty string. */
val title: String
/* Readable body text: entities decoded, block/BR boundaries preserved, whitespace normalized.
Excludes script, style, template, noscript and [hidden]; does not evaluate CSS visibility. */
fun text(): String
/* CSS selector; empty list when nothing matches. Invalid selectors raise an error. */
fun select(css: String): List<HtmlElement>
}
/* An element selected from a document. */
extern class HtmlElement {
/* Same readable-text rules as HtmlDocument.text(), scoped to this element. */
fun text(): String
/* Decoded attribute value; empty string when absent. */
fun attr(name: String): String
/* Attribute resolved against the base URL (including HTML <base>); empty if unresolvable.
Inspect the scheme before using the URL: it is not restricted to HTTP/HTTPS. */
fun absUrl(name: String): String
/* CSS selector scoped to this element. */
fun select(css: String): List<HtmlElement>
}
+9 -1
View File
@@ -32,7 +32,15 @@ extern class HttpResponse {
fun bytes(): Buffer fun bytes(): Buffer
} }
/* HTTP/HTTPS client singleton. */ /*
HTTP/HTTPS client singleton.
Transport failures include a diagnostic code, request method/URL, underlying cause,
and recovery advice: HTTP_TLS_FAILED, HTTP_TIMEOUT, HTTP_DNS_FAILED,
HTTP_CONNECTION_FAILED, HTTP_REQUEST_FAILED. HTTP_ACCESS_DENIED means no grant.
HTTP_TLS_FAILED occurs before any response: do not retry the URL unchanged;
use another source. Query strings/userinfo are redacted from the diagnostic target.
Non-2xx HTTP statuses are returned normally. Cancellation propagates to the host.
*/
extern object Http { extern object Http {
/* True when HTTP client support is available on this runtime. */ /* True when HTTP client support is available on this runtime. */
fun isSupported(): Bool fun isSupported(): Bool
+45 -11
View File
@@ -1,14 +1,18 @@
package lyng.io.http.server package lyng.io.http.server
import lyng.io.http.types import lyng.io.http.types
import lyng.serialization
import lyng.io.ws.types import lyng.io.ws.types
import lyng.io.html
/* Immutable parsed incoming server request. */ /* Immutable parsed incoming server request. */
extern class ServerRequest { extern class ServerRequest {
val method: String val method: String
val target: String val target: String
val path: String val path: String
val query: String? val pathParts: List<String>
val queryString: String?
val query: Map<String, String>
val headers: HttpHeaders val headers: HttpHeaders
val body: Buffer val body: Buffer
fun text(): String fun text(): String
@@ -25,13 +29,18 @@ extern class ServerWebSocket {
} }
/* Mutable exchange object for one incoming request. */ /* Mutable exchange object for one incoming request. */
extern class ServerExchange { extern class RequestContext {
val request: ServerRequest val request: ServerRequest
val routeMatch: RegexMatch?
val routeParams: Map<String, String>
fun jsonBody<T>(): T
fun respond(status: Int = 200, body: Buffer? = null): void fun respond(status: Int = 200, body: Buffer? = null): void
fun respondText(status: Int = 200, bodyText: String = ""): void fun respondText(status: Int = 200, bodyText: String = ""): void
fun respondJson(body: Object?, status: Int = 200): void
fun respondHtml(code: Int = 200, builder: HtmlTag.()->void): void
fun setHeader(name: String, value: String): void fun setHeader(name: String, value: String): void
fun addHeader(name: String, value: String): void fun addHeader(name: String, value: String): void
fun acceptWebSocket(handler: (ServerWebSocket, ServerExchange) -> Object?): void fun acceptWebSocket(handler: RequestContext.(ServerWebSocket) -> Object?): void
fun isHandled(): Bool fun isHandled(): Bool
} }
@@ -41,14 +50,39 @@ extern class HttpServerHandle {
fun close(): void fun close(): void
} }
/* Exact-path HTTP/WebSocket server with built-in router. */ /* Reusable route collection mounted into HttpServer or other Router. */
extern class Router {
fun get(path: String|Regex, handler: RequestContext.() -> Object?): Router
fun getPath(pathTemplate: String, handler: RequestContext.() -> Object?): Router
fun post(path: String|Regex, handler: RequestContext.() -> Object?): Router
fun postPath(pathTemplate: String, handler: RequestContext.() -> Object?): Router
fun put(path: String|Regex, handler: RequestContext.() -> Object?): Router
fun putPath(pathTemplate: String, handler: RequestContext.() -> Object?): Router
fun delete(path: String|Regex, handler: RequestContext.() -> Object?): Router
fun deletePath(pathTemplate: String, handler: RequestContext.() -> Object?): Router
fun any(path: String|Regex, handler: RequestContext.() -> Object?): Router
fun anyPath(pathTemplate: String, handler: RequestContext.() -> Object?): Router
fun ws(path: String|Regex, handler: RequestContext.(ServerWebSocket) -> Object?): Router
fun wsPath(pathTemplate: String, handler: RequestContext.(ServerWebSocket) -> Object?): Router
fun fallback(handler: RequestContext.() -> Object?): Router
fun mount(router: Router): Router
}
/* HTTP/WebSocket server with built-in router. */
extern class HttpServer { extern class HttpServer {
fun get(path: String, handler: (ServerExchange) -> Object?): HttpServer fun get(path: String|Regex, handler: RequestContext.() -> Object?): HttpServer
fun post(path: String, handler: (ServerExchange) -> Object?): HttpServer fun getPath(pathTemplate: String, handler: RequestContext.() -> Object?): HttpServer
fun put(path: String, handler: (ServerExchange) -> Object?): HttpServer fun post(path: String|Regex, handler: RequestContext.() -> Object?): HttpServer
fun delete(path: String, handler: (ServerExchange) -> Object?): HttpServer fun postPath(pathTemplate: String, handler: RequestContext.() -> Object?): HttpServer
fun any(path: String, handler: (ServerExchange) -> Object?): HttpServer fun put(path: String|Regex, handler: RequestContext.() -> Object?): HttpServer
fun ws(path: String, handler: (ServerWebSocket, ServerExchange) -> Object?): HttpServer fun putPath(pathTemplate: String, handler: RequestContext.() -> Object?): HttpServer
fun fallback(handler: (ServerExchange) -> Object?): HttpServer fun delete(path: String|Regex, handler: RequestContext.() -> Object?): HttpServer
fun deletePath(pathTemplate: String, handler: RequestContext.() -> Object?): HttpServer
fun any(path: String|Regex, handler: RequestContext.() -> Object?): HttpServer
fun anyPath(pathTemplate: String, handler: RequestContext.() -> Object?): HttpServer
fun ws(path: String|Regex, handler: RequestContext.(ServerWebSocket) -> Object?): HttpServer
fun wsPath(pathTemplate: String, handler: RequestContext.(ServerWebSocket) -> Object?): HttpServer
fun fallback(handler: RequestContext.() -> Object?): HttpServer
fun mount(router: Router): HttpServer
fun listen(port: Int, host: String? = null, backlog: Int = 128): HttpServerHandle fun listen(port: Int, host: String? = null, backlog: Int = 128): HttpServerHandle
} }
+59
View File
@@ -0,0 +1,59 @@
package lyng.io.process
/* Handle to a running process. This is the low-level process API. */
extern class RunningProcess {
/* Standard output as a flow of process output lines. */
fun stdout(): Flow<String>
/* Standard error as a flow of process error lines. */
fun stderr(): Flow<String>
/* Send a signal such as "SIGINT", "SIGTERM", or "SIGKILL". */
fun signal(signal: String): void
/* Wait for process completion and return its exit code. */
fun waitFor(): Int
/* Forcefully terminate the process. */
fun destroy(): void
}
/* Shell-script friendly active handle for a running command. */
extern class CommandRun {
/* Original shell command or argv-style command display text. */
val command: String
/* Capture standard output as a string, wait for completion, and drain stderr concurrently. Use `lines` for large output. */
val out: String
/* Capture standard error as a string, wait for completion, and drain stdout concurrently. Use `errorLines` for large output. */
val err: String
/* Streaming standard output lines. Treat the process stdout stream as one-shot. */
val lines: Flow<String>
/* Streaming standard error lines. Treat the process stderr stream as one-shot. */
val errorLines: Flow<String>
/* Exit code after `wait`, `check`, `out`, or `err`; otherwise null. */
val code: Int?
/* True for exit code 0, false for non-zero, or null before the exit code is known. */
val ok: Bool?
/* Wait for the command to exit and return the exit code. */
fun wait(): Int
/* Capture output, wait for completion, and fail if the exit code is non-zero. */
fun check(): CommandRun
}
/* Process execution and control. */
extern object Process {
/* Execute a process with arguments without using a shell. */
fun execute(executable: String, args: List<String>): RunningProcess
/* Execute a command via the system shell. */
fun shell(command: String): RunningProcess
}
/* Platform information for process support. */
extern object Platform {
/* Get platform core details. */
fun details(): Map<String,Object?>
/* Check if processes are supported on this platform. */
fun isSupported(): Bool
}
/* Run a command via the system shell and return an active command handle. */
extern fun sh(command: String): CommandRun
/* Run an executable with argv-style arguments and return an active command handle. */
extern fun exec(executable: String, args: List<String> = []): CommandRun
+9 -5
View File
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" group = "net.sergeych"
version = "1.5.6-SNAPSHOT" version = providers.gradleProperty("lyng.version").getOrElse("1.5.10")
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below // Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
@@ -29,7 +29,7 @@ plugins {
alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary) alias(libs.plugins.androidLibrary)
// alias(libs.plugins.vanniktech.mavenPublish) // alias(libs.plugins.vanniktech.mavenPublish)
kotlin("plugin.serialization") version "2.3.20" alias(libs.plugins.kotlinSerialization)
id("com.codingfeline.buildkonfig") version "0.17.1" id("com.codingfeline.buildkonfig") version "0.17.1"
`maven-publish` `maven-publish`
} }
@@ -47,7 +47,11 @@ buildkonfig {
kotlin { kotlin {
jvmToolchain(17) jvmToolchain(17)
jvm() jvm {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
}
}
androidTarget { androidTarget {
publishLibraryVariants("release") publishLibraryVariants("release")
@OptIn(ExperimentalKotlinGradlePluginApi::class) @OptIn(ExperimentalKotlinGradlePluginApi::class)
@@ -297,8 +301,8 @@ android {
minSdk = libs.versions.android.minSdk.get().toInt() minSdk = libs.versions.android.minSdk.get().toInt()
} }
compileOptions { compileOptions {
sourceCompatibility = JavaVersion.VERSION_11 sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_17
} }
} }
dependencies { dependencies {
@@ -509,5 +509,15 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
val isTransient: Boolean = false, val isTransient: Boolean = false,
val annotationSpecs: List<ParsedDeclAnnotation> = emptyList(), val annotationSpecs: List<ParsedDeclAnnotation> = emptyList(),
val annotations: List<DeclAnnotation> = emptyList(), val annotations: List<DeclAnnotation> = emptyList(),
) /** Exact source text of the default expression, when parsed from Lyng source. */
val defaultSource: String? = null,
) {
/** Type used at call sites, where ellipsis describes repeated arguments. */
val signatureType: TypeDecl
get() = if (isEllipsis) TypeDecl.Ellipsis(type) else type
/** Type visible inside the callable after repeated arguments are collected. */
val localType: TypeDecl
get() = if (isEllipsis) TypeDecl.Generic("List", listOf(type), false) else type
}
} }
@@ -88,6 +88,9 @@ internal suspend fun executeClassDecl(
classScope.addConst("object", newClass) classScope.addConst("object", newClass)
spec.bodyInit?.let { executeBytecodeWithSeed(classScope, it, "object body init") } spec.bodyInit?.let { executeBytecodeWithSeed(classScope, it, "object body init") }
for (statement in spec.initScope) {
executeBytecodeWithSeed(classScope, statement, "object static init")
}
val instance = newClass.callOn(scope.createChildScope(Arguments.EMPTY)) val instance = newClass.callOn(scope.createChildScope(Arguments.EMPTY))
if (spec.declaredName != null) { if (spec.declaredName != null) {
@@ -22,13 +22,16 @@ sealed class CodeContext {
class Function( class Function(
val name: String, val name: String,
val implicitThisMembers: Boolean = false, val implicitThisMembers: Boolean = false,
val implicitThisTypeName: String? = null, val implicitReceiverTypeNames: List<String> = emptyList(),
val typeParams: Set<String> = emptySet(), val typeParams: Set<String> = emptySet(),
val typeParamDecls: List<TypeDecl.TypeParam> = emptyList(), val typeParamDecls: List<TypeDecl.TypeParam> = emptyList(),
/** True for static methods and top-level functions: they have no implicit `this`, /** True for static methods and top-level functions: they have no implicit `this`,
* so class-body field initializers inside them should not inherit the class name. */ * so class-body field initializers inside them should not inherit the class name. */
val noImplicitThis: Boolean = false val noImplicitThis: Boolean = false
): CodeContext() ): CodeContext() {
val implicitThisTypeName: String?
get() = implicitReceiverTypeNames.firstOrNull()
}
class ClassBody(val name: String, val isExtern: Boolean = false): CodeContext() { class ClassBody(val name: String, val isExtern: Boolean = false): CodeContext() {
var typeParams: Set<String> = emptySet() var typeParams: Set<String> = emptySet()
var typeParamDecls: List<TypeDecl.TypeParam> = emptyList() var typeParamDecls: List<TypeDecl.TypeParam> = emptyList()
File diff suppressed because it is too large. Load diff
@@ -50,6 +50,7 @@ data class FunctionDeclSpec(
val slotIndex: Int?, val slotIndex: Int?,
val scopeId: Int?, val scopeId: Int?,
val startPos: Pos, val startPos: Pos,
val resolvedMetadata: ResolvedFunctionMetadata,
) )
internal suspend fun executeFunctionDecl( internal suspend fun executeFunctionDecl(
@@ -18,6 +18,7 @@ package net.sergeych.lyng
import net.sergeych.lyng.bytecode.CmdFrame import net.sergeych.lyng.bytecode.CmdFrame
import net.sergeych.lyng.bytecode.CmdVm import net.sergeych.lyng.bytecode.CmdVm
import net.sergeych.lyng.bytecode.seedFrameLocalsFromScope
import net.sergeych.lyng.obj.Obj import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjNull import net.sergeych.lyng.obj.ObjNull
@@ -34,6 +35,7 @@ class PropertyAccessorStatement(
val bytecodeStmt = requireBytecodeBody(scope, body, "property accessor") val bytecodeStmt = requireBytecodeBody(scope, body, "property accessor")
val fn = bytecodeStmt.bytecodeFunction() val fn = bytecodeStmt.bytecodeFunction()
val binder: suspend (CmdFrame, Arguments) -> Unit = { frame, arguments -> val binder: suspend (CmdFrame, Arguments) -> Unit = { frame, arguments ->
seedFrameLocalsFromScope(frame, scope)
val slotPlan = fn.localSlotPlanByName() val slotPlan = fn.localSlotPlanByName()
val slotIndex = slotPlan[argName] val slotIndex = slotPlan[argName]
?: scope.raiseIllegalState("property accessor argument $argName missing from slot plan") ?: scope.raiseIllegalState("property accessor argument $argName missing from slot plan")
@@ -0,0 +1,79 @@
/*
* 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
/**
* Compiler-resolved metadata for a function parameter.
*
* [type] is the declared element type for a variadic parameter, not its collected local type.
* For example, `values: Int...` has a [type] of `Int`, [isEllipsis] is `true`, and the
* corresponding entry in [ResolvedFunctionMetadata.functionType] is `Int...`.
*
* [defaultSource] preserves the source expression for tools that generate declarations. It is
* null for parameters without defaults and may also be null for declarations assembled by a host
* rather than parsed from source; use [hasDefault] to distinguish those cases.
*
* @property name parameter name as written in the declaration
* @property type compiler-resolved declared type, or the variadic element type when [isEllipsis]
* is `true`
* @property isEllipsis whether this parameter collects repeated arguments
* @property hasDefault whether the declaration supplies a default expression
* @property defaultSource exact source text of the default expression, when available
* @property pos source position of the parameter declaration
*/
data class ResolvedFunctionParameter(
val name: String,
val type: TypeDecl,
val isEllipsis: Boolean,
val hasDefault: Boolean,
val defaultSource: String?,
val pos: Pos,
)
/**
* Semantic function declaration information produced by the compiler without evaluating source.
*
* Unlike the editor Mini-AST, this descriptor contains compiler-resolved parameter and return
* types, including an inferred return type when inference succeeds. Annotation names describe the
* declaration syntax; annotation functions are not executed while this metadata is collected.
* The descriptor is intended for declaration generators and other embedding tools that need
* semantic signatures without running the script.
*
* @property name declared function name
* @property namePos source position of [name]
* @property declarationPos source position at the start of the function declaration
* @property visibility resolved declaration visibility
* @property annotations annotation names in declaration order, without evaluating annotations
* @property typeParams resolved generic type parameters, including bounds and defaults
* @property parameters resolved parameters in declaration order
* @property functionType complete callable type, including receiver, context receivers, variadic
* markers, and the declared or inferred return type
*/
data class ResolvedFunctionMetadata(
val name: String,
val namePos: Pos,
val declarationPos: Pos,
val visibility: Visibility,
val annotations: List<String>,
val typeParams: List<TypeDecl.TypeParam>,
val parameters: List<ResolvedFunctionParameter>,
val functionType: TypeDecl.Function,
) {
/** Declared or compiler-inferred return type of this function. */
val returnType: TypeDecl get() = functionType.returnType
}
@@ -99,6 +99,20 @@ open class Scope(
extensions.getOrPut(cls) { mutableMapOf() }[name] = record extensions.getOrPut(cls) { mutableMapOf() }[name] = record
} }
private fun extensionContextReceiversSatisfied(record: ObjRecord): Boolean {
val fnType = record.typeDecl as? TypeDecl.Function ?: return true
if (fnType.contextReceivers.isEmpty()) return true
return fnType.contextReceivers.all { required ->
thisVariants.any { variant ->
when (required) {
is TypeDecl.Simple -> variant.isInstanceOf(required.name.substringAfterLast('.'))
is TypeDecl.Generic -> variant.isInstanceOf(required.name.substringAfterLast('.'))
else -> false
}
}
}
}
internal fun findExtension(receiverClass: ObjClass, name: String): ObjRecord? { internal fun findExtension(receiverClass: ObjClass, name: String): ObjRecord? {
var s: Scope? = this var s: Scope? = this
var hops = 0 var hops = 0
@@ -106,7 +120,9 @@ open class Scope(
// Proximity rule: check all extensions in the current scope before going to parent. // Proximity rule: check all extensions in the current scope before going to parent.
// Priority within scope: more specific class in MRO wins. // Priority within scope: more specific class in MRO wins.
for (cls in receiverClass.mro) { for (cls in receiverClass.mro) {
s.extensions[cls]?.get(name)?.let { return it } s.extensions[cls]?.get(name)?.let {
if (extensionContextReceiversSatisfied(it)) return it
}
} }
if (s is BytecodeClosureScope) { if (s is BytecodeClosureScope) {
s.closureScope.findExtension(receiverClass, name)?.let { return it } s.closureScope.findExtension(receiverClass, name)?.let { return it }
@@ -19,17 +19,16 @@ package net.sergeych.lyng
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.yield import kotlinx.coroutines.yield
import net.sergeych.lyng.Script.Companion.defaultImportManager
import net.sergeych.lyng.bridge.bind import net.sergeych.lyng.bridge.bind
import net.sergeych.lyng.bridge.bindObject import net.sergeych.lyng.bridge.bindObject
import net.sergeych.lyng.bytecode.BytecodeLambdaCallable
import net.sergeych.lyng.bytecode.CmdFunction import net.sergeych.lyng.bytecode.CmdFunction
import net.sergeych.lyng.bytecode.CmdVm import net.sergeych.lyng.bytecode.CmdVm
import net.sergeych.lyng.bytecode.BytecodeLambdaCallable
import net.sergeych.lyng.miniast.* import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.* import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.serialization.ObjJsonClass import net.sergeych.lyng.serialization.ObjJsonClass
import net.sergeych.lyng.serialization.bindSerializationFormat import net.sergeych.lyng.serialization.bindSerializationFormat
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.stdlib_included.complexLyng import net.sergeych.lyng.stdlib_included.complexLyng
import net.sergeych.lyng.stdlib_included.decimalLyng import net.sergeych.lyng.stdlib_included.decimalLyng
import net.sergeych.lyng.stdlib_included.legacyDigestLyng import net.sergeych.lyng.stdlib_included.legacyDigestLyng
@@ -55,6 +54,18 @@ class Script(
) : Statement() { ) : Statement() {
fun statements(): List<Statement> = statements fun statements(): List<Statement> = statements
/**
* Return compiler-resolved metadata for this script's top-level function declarations.
*
* Results retain source order and include declared or inferred signature types. This is a
* read-only view of information produced while compiling the script; calling it does not
* execute the script, its annotations, or parameter default expressions.
*
* @return metadata for every top-level function declaration in source order
*/
fun resolvedFunctionMetadata(): List<ResolvedFunctionMetadata> =
statements.filterIsInstance<FunctionDeclStatement>().map { it.spec.resolvedMetadata }
/** /**
* Explicitly apply this script's import/module bindings to [scope] without executing the script. * Explicitly apply this script's import/module bindings to [scope] without executing the script.
* This is intended for embedding scenarios where the host owns scope lifecycle and wants * This is intended for embedding scenarios where the host owns scope lifecycle and wants
@@ -26,6 +26,7 @@ sealed class TypeDecl(val isNullable:Boolean = false) {
// ?? // ??
data class Function( data class Function(
val receiver: TypeDecl?, val receiver: TypeDecl?,
val contextReceivers: List<TypeDecl> = emptyList(),
val params: List<TypeDecl>, val params: List<TypeDecl>,
val returnType: TypeDecl, val returnType: TypeDecl,
val nullable: Boolean = false val nullable: Boolean = false
@@ -43,6 +43,7 @@ class BytecodeCompiler(
private val callableReturnTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(), private val callableReturnTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(),
private val callableReturnTypeByName: Map<String, ObjClass> = emptyMap(), private val callableReturnTypeByName: Map<String, ObjClass> = emptyMap(),
private val callSignatureByName: Map<String, CallSignature> = emptyMap(), private val callSignatureByName: Map<String, CallSignature> = emptyMap(),
private val extensionContextReceiversByWrapperName: Map<String, List<String>> = emptyMap(),
private val externCallableNames: Set<String> = emptySet(), private val externCallableNames: Set<String> = emptySet(),
private val externBindingNames: Set<String> = emptySet(), private val externBindingNames: Set<String> = emptySet(),
private val preparedModuleBindingNames: Set<String> = emptySet(), private val preparedModuleBindingNames: Set<String> = emptySet(),
@@ -1146,9 +1147,29 @@ class BytecodeCompiler(
} }
private fun compileUnary(ref: UnaryOpRef): CompiledValue? { private fun compileUnary(ref: UnaryOpRef): CompiledValue? {
return when (unaryOp(ref)) {
UnaryOp.POSITIVE -> {
val operandRef = unaryOperand(ref)
if (hasUnaryCallable(operandRef, "unaryPlus")) {
return compileMethodCall(MethodCallRef(operandRef, "unaryPlus", emptyList(), false, false))
}
val a = compileRef(operandRef) ?: return null
return when (a.type) {
SlotType.INT, SlotType.REAL -> a
else -> {
val obj = ensureObjSlot(a)
val out = allocSlot()
builder.emit(Opcode.POS_OBJ, obj.slot, out)
updateSlotType(out, SlotType.OBJ)
slotObjClass[obj.slot]?.let { slotObjClass[out] = it }
CompiledValue(out, SlotType.OBJ)
}
}
}
else -> {
val a = compileRef(unaryOperand(ref)) ?: return null val a = compileRef(unaryOperand(ref)) ?: return null
val out = allocSlot() val out = allocSlot()
return when (unaryOp(ref)) { when (unaryOp(ref)) {
UnaryOp.NEGATE -> when (a.type) { UnaryOp.NEGATE -> when (a.type) {
SlotType.INT -> { SlotType.INT -> {
builder.emit(Opcode.NEG_INT, a.slot, out) builder.emit(Opcode.NEG_INT, a.slot, out)
@@ -1186,16 +1207,35 @@ class BytecodeCompiler(
} }
return compileObjUnaryOp(unaryOperand(ref), a, "bitNot", Pos.builtIn) return compileObjUnaryOp(unaryOperand(ref), a, "bitNot", Pos.builtIn)
} }
UnaryOp.POSITIVE -> error("unreachable")
} }
} }
}
}
private fun hasUnaryCallable(ref: ObjRef, memberName: String): Boolean {
val receiverClass = resolveReceiverClass(ref) ?: return false
if (receiverClass == ObjDynamic.type) return false
if (receiverClass is ObjInstanceClass && !isThisReceiver(ref)) return true
val resolvedMember = receiverClass.resolveInstanceMember(memberName)
if (resolvedMember?.declaringClass?.className == "Obj") return false
val abstractRecord = receiverClass.members[memberName] ?: receiverClass.classScope?.objects?.get(memberName)
if (abstractRecord?.isAbstract == true) return false
val methodId = receiverClass.instanceMethodIdMap(includeAbstract = true)[memberName]
if (methodId != null && resolvedMember?.declaringClass?.className != "Obj") return true
val fieldId = if (resolvedMember != null) receiverClass.instanceFieldIdMap()[memberName] else null
if (fieldId != null) return true
return resolveExtensionCallableSlot(receiverClass, memberName) != null
}
private fun compileObjUnaryOp( private fun compileObjUnaryOp(
ref: ObjRef, ref: ObjRef,
value: CompiledValue, value: CompiledValue,
memberName: String, memberName: String,
pos: Pos pos: Pos,
defaultIdentity: Boolean = false
): CompiledValue? { ): CompiledValue? {
val receiverClass = resolveReceiverClass(ref) val receiverClass = resolveReceiverClass(ref) ?: slotObjClass[value.slot]
val methodId = receiverClass?.instanceMethodIdMap(includeAbstract = true)?.get(memberName) val methodId = receiverClass?.instanceMethodIdMap(includeAbstract = true)?.get(memberName)
if (methodId != null) { if (methodId != null) {
val receiverObj = ensureObjSlot(value) val receiverObj = ensureObjSlot(value)
@@ -1204,6 +1244,19 @@ class BytecodeCompiler(
updateSlotType(dst, SlotType.OBJ) updateSlotType(dst, SlotType.OBJ)
return CompiledValue(dst, SlotType.OBJ) return CompiledValue(dst, SlotType.OBJ)
} }
val extSlot = when {
receiverClass != null -> resolveExtensionCallableSlot(receiverClass, memberName)
else -> resolveUniqueExtensionWrapperSlot(memberName, "__ext__")
}
if (extSlot != null) {
val callee = ensureObjSlot(extSlot)
val args = compileCallArgsWithReceiver(value, emptyList(), false) ?: return null
val encodedCount = encodeCallArgCount(args) ?: return null
val dst = allocSlot()
setPos(pos)
emitCallCompiled(callee, args.base, encodedCount, dst)
return CompiledValue(dst, SlotType.OBJ)
}
if (memberName == "negate" && if (memberName == "negate" &&
(receiverClass == null || isDelegateClass(receiverClass) || receiverClass in setOf(ObjInt.type, ObjReal.type)) (receiverClass == null || isDelegateClass(receiverClass) || receiverClass in setOf(ObjInt.type, ObjReal.type))
) { ) {
@@ -1217,6 +1270,9 @@ class BytecodeCompiler(
updateSlotType(dst, SlotType.OBJ) updateSlotType(dst, SlotType.OBJ)
return CompiledValue(dst, SlotType.OBJ) return CompiledValue(dst, SlotType.OBJ)
} }
if (defaultIdentity) {
return value
}
throw BytecodeCompileException( throw BytecodeCompileException(
"Unknown member $memberName on ${receiverClass?.className ?: "unknown"}", "Unknown member $memberName on ${receiverClass?.className ?: "unknown"}",
pos pos
@@ -4473,6 +4529,20 @@ class BytecodeCompiler(
} }
private fun compileElvis(ref: ElvisRef): CompiledValue? { private fun compileElvis(ref: ElvisRef): CompiledValue? {
fun isAbruptControlRef(candidate: ObjRef): Boolean {
var stmt = (candidate as? StatementRef)?.statement ?: return false
while (stmt is BytecodeStatement) {
stmt = stmt.original
}
return when (stmt) {
is net.sergeych.lyng.BreakStatement,
is net.sergeych.lyng.ContinueStatement,
is net.sergeych.lyng.ReturnStatement,
is net.sergeych.lyng.ThrowStatement -> true
else -> false
}
}
val leftValue = compileRefWithFallback(ref.left, null, Pos.builtIn) ?: return null val leftValue = compileRefWithFallback(ref.left, null, Pos.builtIn) ?: return null
val leftObj = ensureObjSlot(leftValue) val leftObj = ensureObjSlot(leftValue)
val resultSlot = allocSlot() val resultSlot = allocSlot()
@@ -4489,9 +4559,12 @@ class BytecodeCompiler(
emitMove(leftObj, resultSlot) emitMove(leftObj, resultSlot)
builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel))) builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel)))
builder.mark(rightLabel) builder.mark(rightLabel)
val rightIsAbruptControl = isAbruptControlRef(ref.right)
val rightValue = compileRefWithFallback(ref.right, null, Pos.builtIn) ?: return null val rightValue = compileRefWithFallback(ref.right, null, Pos.builtIn) ?: return null
if (!rightIsAbruptControl) {
val rightObj = ensureObjSlot(rightValue) val rightObj = ensureObjSlot(rightValue)
emitMove(rightObj, resultSlot) emitMove(rightObj, resultSlot)
}
builder.mark(endLabel) builder.mark(endLabel)
updateSlotType(resultSlot, SlotType.OBJ) updateSlotType(resultSlot, SlotType.OBJ)
return CompiledValue(resultSlot, SlotType.OBJ) return CompiledValue(resultSlot, SlotType.OBJ)
@@ -4803,11 +4876,16 @@ class BytecodeCompiler(
val receiverClass = resolveReceiverClass(ref.receiver) ?: ObjDynamic.type val receiverClass = resolveReceiverClass(ref.receiver) ?: ObjDynamic.type
val receiver = compileRefWithFallback(ref.receiver, null, refPosOrCurrent(ref.receiver)) ?: return null val receiver = compileRefWithFallback(ref.receiver, null, refPosOrCurrent(ref.receiver)) ?: return null
val dst = allocSlot() val dst = allocSlot()
if (shouldUseClassScopeReceiver(ref.receiver, receiver, receiverClass)) { val classReceiver = shouldUseClassScopeReceiver(ref.receiver, receiver, receiverClass)
if (ref.classScopeCall || classReceiver) {
// Singleton objects carry their static members on the instance's class.
val classSlot = if (classReceiver) receiver.slot else allocSlot().also {
builder.emit(Opcode.GET_OBJ_CLASS, receiver.slot, it)
}
val nameId = builder.addConst(BytecodeConst.StringVal(ref.name)) val nameId = builder.addConst(BytecodeConst.StringVal(ref.name))
val memberSlot = allocSlot() val memberSlot = allocSlot()
if (!ref.isOptional) { if (!ref.isOptional) {
builder.emit(Opcode.GET_CLASS_SCOPE, receiver.slot, nameId, memberSlot) builder.emit(Opcode.GET_CLASS_SCOPE, classSlot, nameId, memberSlot)
} else { } else {
val nullSlot = allocSlot() val nullSlot = allocSlot()
builder.emit(Opcode.CONST_NULL, nullSlot) builder.emit(Opcode.CONST_NULL, nullSlot)
@@ -4819,7 +4897,7 @@ class BytecodeCompiler(
Opcode.JMP_IF_TRUE, Opcode.JMP_IF_TRUE,
listOf(CmdBuilder.Operand.IntVal(cmpSlot), CmdBuilder.Operand.LabelRef(nullLabel)) listOf(CmdBuilder.Operand.IntVal(cmpSlot), CmdBuilder.Operand.LabelRef(nullLabel))
) )
builder.emit(Opcode.GET_CLASS_SCOPE, receiver.slot, nameId, memberSlot) builder.emit(Opcode.GET_CLASS_SCOPE, classSlot, nameId, memberSlot)
builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel))) builder.emit(Opcode.JMP, listOf(CmdBuilder.Operand.LabelRef(endLabel)))
builder.mark(nullLabel) builder.mark(nullLabel)
builder.emit(Opcode.CONST_NULL, memberSlot) builder.emit(Opcode.CONST_NULL, memberSlot)
@@ -5955,6 +6033,7 @@ class BytecodeCompiler(
): String? { ): String? {
for (receiverName in extensionReceiverTypeNames(receiverClass)) { for (receiverName in extensionReceiverTypeNames(receiverClass)) {
val candidate = wrapperName(receiverName, memberName) val candidate = wrapperName(receiverName, memberName)
if (!extensionContextReceiversSatisfied(candidate)) continue
if (allowedScopeNames != null && if (allowedScopeNames != null &&
!allowedScopeNames.contains(candidate) && !allowedScopeNames.contains(candidate) &&
!localSlotIndexByName.containsKey(candidate) !localSlotIndexByName.containsKey(candidate)
@@ -5966,6 +6045,31 @@ class BytecodeCompiler(
return null return null
} }
private fun currentImplicitReceiverTypeNames(): List<String> {
val result = mutableListOf<String>()
inlineThisBindings.asReversed().forEach { binding ->
val typeName = binding.typeName ?: return@forEach
if (!result.contains(typeName)) result += typeName
}
implicitThisTypeName?.let {
if (!result.contains(it)) result += it
}
return result
}
private fun extensionContextReceiversSatisfied(wrapperName: String): Boolean {
val required = extensionContextReceiversByWrapperName[wrapperName].orEmpty()
if (required.isEmpty()) return true
val visible = currentImplicitReceiverTypeNames()
return required.all { req ->
visible.any { visibleName ->
visibleName == req || resolveTypeNameClass(visibleName)?.let { cls ->
cls.className == req || cls.mro.any { it.className == req }
} == true
}
}
}
private fun resolveUniqueExtensionWrapperName( private fun resolveUniqueExtensionWrapperName(
memberName: String, memberName: String,
wrapperPrefix: String wrapperPrefix: String
@@ -5974,12 +6078,12 @@ class BytecodeCompiler(
val candidates = LinkedHashSet<String>() val candidates = LinkedHashSet<String>()
for (name in localSlotIndexByName.keys) { for (name in localSlotIndexByName.keys) {
if (name.startsWith(wrapperPrefix) && name.endsWith(suffix)) { if (name.startsWith(wrapperPrefix) && name.endsWith(suffix)) {
candidates.add(name) if (extensionContextReceiversSatisfied(name)) candidates.add(name)
} }
} }
for (name in scopeSlotIndexByName.keys) { for (name in scopeSlotIndexByName.keys) {
if (name.startsWith(wrapperPrefix) && name.endsWith(suffix)) { if (name.startsWith(wrapperPrefix) && name.endsWith(suffix)) {
candidates.add(name) if (extensionContextReceiversSatisfied(name)) candidates.add(name)
} }
} }
return candidates.singleOrNull() return candidates.singleOrNull()
@@ -8296,6 +8400,19 @@ class BytecodeCompiler(
is ObjChar -> ObjChar.type is ObjChar -> ObjChar.type
else -> null else -> null
} }
is UnaryOpRef -> when (ref.op) {
UnaryOp.NOT -> ObjBool.type
UnaryOp.POSITIVE -> resolveReceiverClass(ref.a)
UnaryOp.NEGATE -> when (val operandClass = resolveReceiverClass(ref.a)) {
ObjInt.type -> ObjInt.type
ObjReal.type -> ObjReal.type
else -> inferMethodCallReturnClass(operandClass, "negate")
}
UnaryOp.BITNOT -> when (val operandClass = resolveReceiverClass(ref.a)) {
ObjInt.type -> ObjInt.type
else -> inferMethodCallReturnClass(operandClass, "bitNot")
}
}
is CastRef -> resolveTypeRefClass(ref.castTypeRef()) is CastRef -> resolveTypeRefClass(ref.castTypeRef())
?: resolveReceiverClass(ref.castValueRef()) ?: resolveReceiverClass(ref.castValueRef())
is FieldRef -> { is FieldRef -> {
@@ -107,6 +107,7 @@ class BytecodeStatement private constructor(
callableReturnTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(), callableReturnTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(),
callableReturnTypeByName: Map<String, ObjClass> = emptyMap(), callableReturnTypeByName: Map<String, ObjClass> = emptyMap(),
callSignatureByName: Map<String, CallSignature> = emptyMap(), callSignatureByName: Map<String, CallSignature> = emptyMap(),
extensionContextReceiversByWrapperName: Map<String, List<String>> = emptyMap(),
externCallableNames: Set<String> = emptySet(), externCallableNames: Set<String> = emptySet(),
externBindingNames: Set<String> = emptySet(), externBindingNames: Set<String> = emptySet(),
preparedModuleBindingNames: Set<String> = emptySet(), preparedModuleBindingNames: Set<String> = emptySet(),
@@ -148,6 +149,7 @@ class BytecodeStatement private constructor(
callableReturnTypeByScopeId = callableReturnTypeByScopeId, callableReturnTypeByScopeId = callableReturnTypeByScopeId,
callableReturnTypeByName = callableReturnTypeByName, callableReturnTypeByName = callableReturnTypeByName,
callSignatureByName = callSignatureByName, callSignatureByName = callSignatureByName,
extensionContextReceiversByWrapperName = extensionContextReceiversByWrapperName,
externCallableNames = externCallableNames, externCallableNames = externCallableNames,
externBindingNames = externBindingNames, externBindingNames = externBindingNames,
preparedModuleBindingNames = preparedModuleBindingNames, preparedModuleBindingNames = preparedModuleBindingNames,
@@ -143,7 +143,7 @@ class CmdBuilder {
Opcode.UNBOX_INT_OBJ, Opcode.UNBOX_REAL_OBJ, Opcode.UNBOX_INT_OBJ, Opcode.UNBOX_REAL_OBJ,
Opcode.INT_TO_REAL, Opcode.REAL_TO_INT, Opcode.BOOL_TO_INT, Opcode.INT_TO_BOOL, Opcode.INT_TO_REAL, Opcode.REAL_TO_INT, Opcode.BOOL_TO_INT, Opcode.INT_TO_BOOL,
Opcode.OBJ_TO_BOOL, Opcode.GET_OBJ_CLASS, Opcode.OBJ_TO_BOOL, Opcode.GET_OBJ_CLASS,
Opcode.NEG_INT, Opcode.NEG_REAL, Opcode.NOT_BOOL, Opcode.INV_INT, Opcode.NEG_INT, Opcode.NEG_REAL, Opcode.NOT_BOOL, Opcode.INV_INT, Opcode.POS_OBJ,
Opcode.ASSERT_IS -> Opcode.ASSERT_IS ->
listOf(OperandKind.SLOT, OperandKind.SLOT) listOf(OperandKind.SLOT, OperandKind.SLOT)
Opcode.CHECK_IS, Opcode.MAKE_QUALIFIED_VIEW -> Opcode.CHECK_IS, Opcode.MAKE_QUALIFIED_VIEW ->
@@ -698,6 +698,7 @@ class CmdBuilder {
} else { } else {
CmdNotBool(operands[0], operands[1]) CmdNotBool(operands[0], operands[1])
} }
Opcode.POS_OBJ -> CmdPosObj(operands[0], operands[1])
Opcode.AND_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) { Opcode.AND_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
CmdAndBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount) CmdAndBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
} else { } else {
@@ -450,6 +450,7 @@ object CmdDisassembler {
is CmdMulObj -> Opcode.MUL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst) is CmdMulObj -> Opcode.MUL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
is CmdDivObj -> Opcode.DIV_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst) is CmdDivObj -> Opcode.DIV_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
is CmdModObj -> Opcode.MOD_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst) is CmdModObj -> Opcode.MOD_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
is CmdPosObj -> Opcode.POS_OBJ to intArrayOf(cmd.a, cmd.dst)
is CmdContainsObj -> Opcode.CONTAINS_OBJ to intArrayOf(cmd.target, cmd.value, cmd.dst) is CmdContainsObj -> Opcode.CONTAINS_OBJ to intArrayOf(cmd.target, cmd.value, cmd.dst)
is CmdAssignOpObj -> Opcode.ASSIGN_OP_OBJ to intArrayOf(cmd.opId, cmd.targetSlot, cmd.valueSlot, cmd.dst, cmd.nameId) is CmdAssignOpObj -> Opcode.ASSIGN_OP_OBJ to intArrayOf(cmd.opId, cmd.targetSlot, cmd.valueSlot, cmd.dst, cmd.nameId)
is CmdJmp -> Opcode.JMP to intArrayOf(cmd.target) is CmdJmp -> Opcode.JMP to intArrayOf(cmd.target)
@@ -593,6 +594,8 @@ object CmdDisassembler {
Opcode.ADD_OBJ, Opcode.SUB_OBJ, Opcode.MUL_OBJ, Opcode.DIV_OBJ, Opcode.MOD_OBJ, Opcode.CONTAINS_OBJ, Opcode.ADD_OBJ, Opcode.SUB_OBJ, Opcode.MUL_OBJ, Opcode.DIV_OBJ, Opcode.MOD_OBJ, Opcode.CONTAINS_OBJ,
Opcode.AND_BOOL, Opcode.OR_BOOL -> Opcode.AND_BOOL, Opcode.OR_BOOL ->
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT) listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
Opcode.POS_OBJ ->
listOf(OperandKind.SLOT, OperandKind.SLOT)
Opcode.ASSIGN_OP_OBJ -> Opcode.ASSIGN_OP_OBJ ->
listOf(OperandKind.ID, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.CONST) listOf(OperandKind.ID, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.CONST)
Opcode.INC_INT, Opcode.DEC_INT, Opcode.RET, Opcode.ITER_PUSH, Opcode.LOAD_THIS -> Opcode.INC_INT, Opcode.DEC_INT, Opcode.RET, Opcode.ITER_PUSH, Opcode.LOAD_THIS ->
@@ -1942,6 +1942,14 @@ class CmdCmpGteObj(internal val a: Int, internal val b: Int, internal val dst: I
} }
} }
class CmdPosObj(internal val a: Int, internal val dst: Int) : Cmd() {
override suspend fun perform(frame: CmdFrame) {
val result = frame.slotToObj(a).unaryPlus(frame.ensureScope())
frame.storeObjResult(dst, result)
return
}
}
class CmdAddObj(internal val a: Int, internal val b: Int, internal val dst: Int) : Cmd() { class CmdAddObj(internal val a: Int, internal val b: Int, internal val dst: Int) : Cmd() {
override suspend fun perform(frame: CmdFrame) { override suspend fun perform(frame: CmdFrame) {
val result = frame.slotToObj(a).plus(frame.ensureScope(), frame.slotToObj(b)) val result = frame.slotToObj(a).plus(frame.ensureScope(), frame.slotToObj(b))
@@ -3253,12 +3261,14 @@ class CmdDeclExtProperty(internal val constId: Int, internal val slot: Int) : Cm
override suspend fun perform(frame: CmdFrame) { override suspend fun perform(frame: CmdFrame) {
val decl = frame.fn.constants[constId] as? BytecodeConst.ExtensionPropertyDecl val decl = frame.fn.constants[constId] as? BytecodeConst.ExtensionPropertyDecl
?: error("DECL_EXT_PROPERTY expects ExtensionPropertyDecl at $constId") ?: error("DECL_EXT_PROPERTY expects ExtensionPropertyDecl at $constId")
val type = frame.ensureScope().resolveExtensionReceiverClass(decl.extTypeName) val declarationScope = frame.ensureScope()
frame.ensureScope().addExtension( val property = decl.property.withDeclarationScope(declarationScope)
val type = declarationScope.resolveExtensionReceiverClass(decl.extTypeName)
declarationScope.addExtension(
type, type,
decl.property.name, property.name,
ObjRecord( ObjRecord(
decl.property, property,
isMutable = false, isMutable = false,
visibility = decl.visibility, visibility = decl.visibility,
writeVisibility = decl.setterVisibility, writeVisibility = decl.setterVisibility,
@@ -3266,9 +3276,9 @@ class CmdDeclExtProperty(internal val constId: Int, internal val slot: Int) : Cm
type = ObjRecord.Type.Property type = ObjRecord.Type.Property
) )
) )
val getterName = extensionPropertyGetterName(decl.extTypeName, decl.property.name) val getterName = extensionPropertyGetterName(decl.extTypeName, property.name)
val getterWrapper = ObjExtensionPropertyGetterCallable(decl.property.name, decl.property) val getterWrapper = ObjExtensionPropertyGetterCallable(property.name, property)
frame.ensureScope().addItem( declarationScope.addItem(
getterName, getterName,
false, false,
getterWrapper, getterWrapper,
@@ -3280,10 +3290,10 @@ class CmdDeclExtProperty(internal val constId: Int, internal val slot: Int) : Cm
if (getterLocal != null) { if (getterLocal != null) {
frame.setObjUnchecked(frame.fn.scopeSlotCount + getterLocal, getterWrapper) frame.setObjUnchecked(frame.fn.scopeSlotCount + getterLocal, getterWrapper)
} }
if (decl.property.setter != null) { if (property.setter != null) {
val setterName = extensionPropertySetterName(decl.extTypeName, decl.property.name) val setterName = extensionPropertySetterName(decl.extTypeName, property.name)
val setterWrapper = ObjExtensionPropertySetterCallable(decl.property.name, decl.property) val setterWrapper = ObjExtensionPropertySetterCallable(property.name, property)
frame.ensureScope() declarationScope
.addItem( .addItem(
setterName, setterName,
false, false,
@@ -4176,6 +4186,15 @@ class BytecodeLambdaCallable(
val context = callScope.applyClosureForBytecode(closureScope, preferredThisType).also { val context = callScope.applyClosureForBytecode(closureScope, preferredThisType).also {
it.args = args it.args = args
} }
preferredThisType?.let { typeName ->
val receiverArg = args.list.firstOrNull { arg ->
arg.isInstanceOf(typeName) ||
((context[typeName]?.value as? ObjClass)?.let { typeClass -> arg.isInstanceOf(typeClass) } == true)
}
if (receiverArg != null && context.thisVariants.firstOrNull() !== receiverArg) {
context.setThisVariants(receiverArg, context.thisVariants)
}
}
if (captureRecords != null) { if (captureRecords != null) {
context.captureRecords = captureRecords context.captureRecords = captureRecords
context.captureNames = captureNames context.captureNames = captureNames
@@ -144,6 +144,7 @@ enum class Opcode(val code: Int) {
MOD_OBJ(0x7B), MOD_OBJ(0x7B),
CONTAINS_OBJ(0x7C), CONTAINS_OBJ(0x7C),
ASSIGN_OP_OBJ(0x7D), ASSIGN_OP_OBJ(0x7D),
POS_OBJ(0x7E),
JMP(0x80), JMP(0x80),
JMP_IF_TRUE(0x81), JMP_IF_TRUE(0x81),
@@ -56,9 +56,9 @@ internal fun trySeedFrameLocalsFromScopeFast(frame: CmdFrame, scope: Scope): Boo
record.type == ObjRecord.Type.Property -> return false record.type == ObjRecord.Type.Property -> return false
record.value is ObjProperty -> return false record.value is ObjProperty -> return false
else -> when (val direct = record.value) { else -> when (val direct = record.value) {
is FrameSlotRef -> direct.resolvedCaptureValueOrNull() ?: return false is FrameSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: return false
is RecordSlotRef -> direct.resolvedCaptureValueOrNull() ?: return false is RecordSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: return false
is ScopeSlotRef -> direct.resolvedCaptureValueOrNull() ?: return false is ScopeSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: return false
else -> direct else -> direct
} }
} }
@@ -86,9 +86,9 @@ internal suspend fun seedFrameLocalsFromScope(frame: CmdFrame, scope: Scope) {
scope.resolve(record, name) scope.resolve(record, name)
} else { } else {
when (val direct = record.value) { when (val direct = record.value) {
is net.sergeych.lyng.FrameSlotRef -> direct.resolvedCaptureValueOrNull() ?: direct is net.sergeych.lyng.FrameSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.RecordSlotRef -> direct.resolvedCaptureValueOrNull() ?: direct is net.sergeych.lyng.RecordSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.ScopeSlotRef -> direct.resolvedCaptureValueOrNull() ?: direct is net.sergeych.lyng.ScopeSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: direct
else -> direct else -> direct
} }
} }
@@ -117,6 +117,28 @@ private fun mergeAdjacent(spans: List<HighlightSpan>): List<HighlightSpan> {
return out return out
} }
/**
* The parser expands interpolated strings into expression-like token streams whose
* synthetic tokens share source positions with the original string. Keep the
* widest source span at each offset and drop nested overlaps so renderers can
* rely on the public non-overlapping span contract.
*/
private fun removeOverlappingSpans(spans: List<HighlightSpan>): List<HighlightSpan> {
if (spans.size < 2) return spans
val sorted = spans.sortedWith(
compareBy<HighlightSpan> { it.range.start }
.thenByDescending { it.range.endExclusive - it.range.start }
)
val out = ArrayList<HighlightSpan>(sorted.size)
var coveredUntil = -1
for (span in sorted) {
if (span.range.start < coveredUntil) continue
out += span
coveredUntil = span.range.endExclusive
}
return out
}
/** Simple highlighter using the existing Lyng lexer (no incremental support yet). */ /** Simple highlighter using the existing Lyng lexer (no incremental support yet). */
class SimpleLyngHighlighter : LyngHighlighter { class SimpleLyngHighlighter : LyngHighlighter {
override fun highlight(text: String): List<HighlightSpan> { override fun highlight(text: String): List<HighlightSpan> {
@@ -170,8 +192,8 @@ class SimpleLyngHighlighter : LyngHighlighter {
val overridden = applyEnumConstantHeuristics(text, src, tokens, raw) val overridden = applyEnumConstantHeuristics(text, src, tokens, raw)
// Adjust single-line comment spans to extend till EOL to compensate for lexer offset/length quirks // Adjust single-line comment spans to extend till EOL to compensate for lexer offset/length quirks
val adjusted = extendSingleLineCommentsToEol(text, overridden) val adjusted = extendSingleLineCommentsToEol(text, overridden)
// Spans are in order; merge adjacent of the same kind for compactness // Normalize spans, then merge adjacent spans of the same kind for compactness.
return mergeAdjacent(adjusted) return mergeAdjacent(removeOverlappingSpans(adjusted))
} }
} }
@@ -36,6 +36,7 @@ data class TypeNameDoc(val segments: List<String>, override val nullable: Boolea
data class TypeGenericDoc(val base: TypeNameDoc, val args: List<TypeDoc>, override val nullable: Boolean = false) : TypeDoc data class TypeGenericDoc(val base: TypeNameDoc, val args: List<TypeDoc>, override val nullable: Boolean = false) : TypeDoc
data class TypeFunctionDoc( data class TypeFunctionDoc(
val receiver: TypeDoc? = null, val receiver: TypeDoc? = null,
val contextReceivers: List<TypeDoc> = emptyList(),
val params: List<TypeDoc>, val params: List<TypeDoc>,
val returns: TypeDoc, val returns: TypeDoc,
override val nullable: Boolean = false override val nullable: Boolean = false
@@ -45,8 +46,13 @@ data class TypeVarDoc(val name: String, override val nullable: Boolean = false)
// Convenience builders // Convenience builders
fun type(name: String, nullable: Boolean = false) = TypeNameDoc(name.split('.'), nullable) fun type(name: String, nullable: Boolean = false) = TypeNameDoc(name.split('.'), nullable)
fun typeVar(name: String, nullable: Boolean = false) = TypeVarDoc(name, nullable) fun typeVar(name: String, nullable: Boolean = false) = TypeVarDoc(name, nullable)
fun funType(params: List<TypeDoc>, returns: TypeDoc, receiver: TypeDoc? = null, nullable: Boolean = false) = fun funType(
TypeFunctionDoc(receiver, params, returns, nullable) params: List<TypeDoc>,
returns: TypeDoc,
receiver: TypeDoc? = null,
contextReceivers: List<TypeDoc> = emptyList(),
nullable: Boolean = false
) = TypeFunctionDoc(receiver, contextReceivers, params, returns, nullable)
// ---------------- Registry ---------------- // ---------------- Registry ----------------
@@ -281,6 +287,7 @@ internal fun TypeDoc.toMiniTypeRef(): MiniTypeRef = when (this) {
is TypeFunctionDoc -> MiniFunctionType( is TypeFunctionDoc -> MiniFunctionType(
range = builtinRange(), range = builtinRange(),
receiver = this.receiver?.toMiniTypeRef(), receiver = this.receiver?.toMiniTypeRef(),
contextReceivers = this.contextReceivers.map { it.toMiniTypeRef() },
params = this.params.map { it.toMiniTypeRef() }, params = this.params.map { it.toMiniTypeRef() },
returnType = this.returns.toMiniTypeRef(), returnType = this.returns.toMiniTypeRef(),
nullable = this.nullable nullable = this.nullable
@@ -139,6 +139,7 @@ data class MiniGenericType(
data class MiniFunctionType( data class MiniFunctionType(
override val range: MiniRange, override val range: MiniRange,
val receiver: MiniTypeRef?, val receiver: MiniTypeRef?,
val contextReceivers: List<MiniTypeRef>,
val params: List<MiniTypeRef>, val params: List<MiniTypeRef>,
val returnType: MiniTypeRef, val returnType: MiniTypeRef,
val nullable: Boolean val nullable: Boolean
@@ -317,6 +317,12 @@ open class Obj {
} }
} }
open suspend fun unaryPlus(scope: Scope): Obj {
return invokeInstanceMethod(scope, "unaryPlus", Arguments.EMPTY) {
this
}
}
open suspend fun mul(scope: Scope, other: Obj): Obj { open suspend fun mul(scope: Scope, other: Obj): Obj {
val otherValue = when (other) { val otherValue = when (other) {
is FrameSlotRef -> other.read() is FrameSlotRef -> other.read()
@@ -32,15 +32,21 @@ import net.sergeych.lyng.executeBytecodeWithSeed
class ObjProperty( class ObjProperty(
val name: String, val name: String,
val getter: Obj?, val getter: Obj?,
val setter: Obj? val setter: Obj?,
private val declarationScope: Scope? = null
) : Obj() { ) : Obj() {
fun withDeclarationScope(scope: Scope): ObjProperty =
ObjProperty(name, getter, setter, scope)
suspend fun callGetter(scope: Scope, instance: Obj, declaringClass: ObjClass? = null): Obj { suspend fun callGetter(scope: Scope, instance: Obj, declaringClass: ObjClass? = null): Obj {
val g = getter ?: scope.raiseError("property $name has no getter") val g = getter ?: scope.raiseError("property $name has no getter")
// Execute getter in a child scope of the instance with 'this' properly set // Execute getter in a child scope of the instance with 'this' properly set
// Match extension function behavior (access to instance scope + call scope). // Match extension function behavior (access to instance scope + call scope).
val instanceScope = (instance as? ObjInstance)?.instanceScope ?: instance.autoInstanceScope(scope) val instanceScope = (instance as? ObjInstance)?.instanceScope ?: instance.autoInstanceScope(scope)
val execScope = scope.applyClosure(instanceScope).createChildScope(newThisObj = instance) val receiverCallScope = scope.applyClosure(instanceScope)
val execBase = declarationScope?.let(receiverCallScope::applyClosure) ?: receiverCallScope
val execScope = execBase.createChildScope(newThisObj = instance)
execScope.currentClassCtx = declaringClass execScope.currentClassCtx = declaringClass
(g as? BytecodeCallable)?.callOnFast(execScope)?.let { return it } (g as? BytecodeCallable)?.callOnFast(execScope)?.let { return it }
return when (g) { return when (g) {
@@ -60,7 +66,9 @@ class ObjProperty(
// Execute setter in a child scope of the instance with 'this' properly set and the value as an argument // Execute setter in a child scope of the instance with 'this' properly set and the value as an argument
// Match extension function behavior (access to instance scope + call scope). // Match extension function behavior (access to instance scope + call scope).
val instanceScope = (instance as? ObjInstance)?.instanceScope ?: instance.autoInstanceScope(scope) val instanceScope = (instance as? ObjInstance)?.instanceScope ?: instance.autoInstanceScope(scope)
val execScope = scope.applyClosure(instanceScope).createChildScope(args = Arguments(value), newThisObj = instance) val receiverCallScope = scope.applyClosure(instanceScope)
val execBase = declarationScope?.let(receiverCallScope::applyClosure) ?: receiverCallScope
val execScope = execBase.createChildScope(args = Arguments(value), newThisObj = instance)
execScope.currentClassCtx = declaringClass execScope.currentClassCtx = declaringClass
(s as? BytecodeCallable)?.callOnFast(execScope)?.let { return } (s as? BytecodeCallable)?.callOnFast(execScope)?.let { return }
when (s) { when (s) {
@@ -73,7 +73,7 @@ class ClassOperatorRef(val target: ObjRef, val pos: Pos) : ObjRef {
} }
/** Unary operations supported by ObjRef. */ /** Unary operations supported by ObjRef. */
enum class UnaryOp { NOT, NEGATE, BITNOT } enum class UnaryOp { NOT, POSITIVE, NEGATE, BITNOT }
/** Binary operations supported by ObjRef. */ /** Binary operations supported by ObjRef. */
enum class BinOp { enum class BinOp {
@@ -484,6 +484,7 @@ class MethodCallRef(
internal val tailBlock: Boolean, internal val tailBlock: Boolean,
internal val isOptional: Boolean, internal val isOptional: Boolean,
internal val explicitTypeArgs: List<TypeDecl>? = null, internal val explicitTypeArgs: List<TypeDecl>? = null,
internal val classScopeCall: Boolean = false,
) : ObjRef { ) : ObjRef {
// 4-entry PIC for method invocations (guarded by PerfFlags.METHOD_PIC) // 4-entry PIC for method invocations (guarded by PerfFlags.METHOD_PIC)
private var mKey1: Long = 0L; private var mVer1: Int = -1; private var mInvoker1: (suspend (Obj, Scope, Arguments) -> Obj)? = null private var mKey1: Long = 0L; private var mVer1: Int = -1; private var mInvoker1: (suspend (Obj, Scope, Arguments) -> Obj)? = null
@@ -206,7 +206,17 @@ private fun typeDeclKey(type: TypeDecl): String = when (type) {
TypeDecl.TypeNullableAny -> "Any?" TypeDecl.TypeNullableAny -> "Any?"
is TypeDecl.Simple -> "S:${type.name}" is TypeDecl.Simple -> "S:${type.name}"
is TypeDecl.Generic -> "G:${type.name}<${type.args.joinToString(",") { typeDeclKey(it) }}>" is TypeDecl.Generic -> "G:${type.name}<${type.args.joinToString(",") { typeDeclKey(it) }}>"
is TypeDecl.Function -> "F:(${type.params.joinToString(",") { typeDeclKey(it) }})->${typeDeclKey(type.returnType)}" is TypeDecl.Function -> buildString {
append("F:")
type.receiver?.let { append("recv=").append(typeDeclKey(it)).append(";") }
if (type.contextReceivers.isNotEmpty()) {
append("ctx=").append(type.contextReceivers.joinToString(",") { typeDeclKey(it) }).append(";")
}
append('(')
append(type.params.joinToString(",") { typeDeclKey(it) })
append(")->")
append(typeDeclKey(type.returnType))
}
is TypeDecl.Ellipsis -> "E:${typeDeclKey(type.elementType)}" is TypeDecl.Ellipsis -> "E:${typeDeclKey(type.elementType)}"
is TypeDecl.TypeVar -> "V:${type.name}" is TypeDecl.TypeVar -> "V:${type.name}"
is TypeDecl.Union -> "U:${type.options.joinToString("|") { typeDeclKey(it) }}" is TypeDecl.Union -> "U:${type.options.joinToString("|") { typeDeclKey(it) }}"
@@ -0,0 +1,62 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class ClassNameMemberRegressionTest {
@Test
fun rejectsClassNamedMembersAtTheirDeclaration() = runTest {
for (member in listOf(
"fun Same(y): String = y.toString()",
"fun Same(y, z): String = y.toString() + z.toString()",
"fun Same(y): Same = Same(y * 2)",
"static fun Same(y): Same = Same(y)",
"fun Same(y): String = \"member\"; fun literal() = Same::class.toString()"
)) {
val error = assertFailsWith<ScriptError> {
eval("class Same(x) {\n $member\n}")
}
assertContains(error.errorMessage, "member function cannot have the same name as its enclosing class")
assertEquals(1, error.pos.line)
}
}
@Test
fun thisIsNotConstructorDelegation() = runTest {
for (receiver in listOf("this", "this@Factory")) {
val error = assertFailsWith<ScriptError> {
eval("class Factory(x) { fun again(): Factory = $receiver(x + 1) }")
}
assertContains(error.errorMessage, "constructor delegation through this is not supported")
}
}
@Test
fun constructorAndClassLiteralRemainAvailable() = runTest {
assertEquals("3", eval("""
class Factory(val x: Int) {
fun again(y: Int): Factory = Factory(y)
fun literal(): String = Factory::class.toString()
}
val f = Factory(1)
f.literal()
f.again(3).x.toString()
""".trimIndent()).toString())
}
@Test
fun localFunctionsCanStillShadowClassNames() = runTest {
assertEquals("local 7", eval("""
class Local(x) {
fun probe(y): String {
fun Local(z): String = "local " + z.toString()
return Local(y)
}
}
Local(1).probe(7)
""".trimIndent()).toString())
}
}
@@ -19,13 +19,10 @@ package net.sergeych.lyng
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.bridge.bind import net.sergeych.lyng.bridge.bind
import net.sergeych.lyng.obj.ObjRecord
import net.sergeych.lyng.bridge.data
import net.sergeych.lyng.bridge.bindGlobalVar import net.sergeych.lyng.bridge.bindGlobalVar
import net.sergeych.lyng.bridge.data
import net.sergeych.lyng.bridge.globalBinder import net.sergeych.lyng.bridge.globalBinder
import net.sergeych.lyng.obj.ObjFalse import net.sergeych.lyng.obj.*
import net.sergeych.lyng.obj.ObjInstance
import net.sergeych.lyng.obj.ObjTrue
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -138,12 +135,65 @@ class GlobalPropertyCaptureRegressionTest {
assertEquals(77.0, x, "bound extern var should stay live after extern class property branch in child scope") assertEquals(77.0, x, "bound extern var should stay live after extern class property branch in child scope")
} }
@Test
fun externPropertyOnExternFunctionResultCanAssignExternGlobalVar() = runTest {
val base = Script.newScope() as ModuleScope
var x = 0L
base.eval(
"""
extern var X: Int
class InputResult {
extern val value: Int?
}
""".trimIndent()
)
base.bind("InputResult") {
addVal("value") {
ObjInt.of(thisObjData<InputPayload>().value)
}
}
base.globalBinder().bindGlobalVar(
name = "X",
get = { x },
set = { x = it }
)
base.globalBinder().bindGlobalFunRaw("input") { _, _ ->
val instance = base.requireClass("InputResult").callOn(base.createChildScope()) as ObjInstance
instance.data = InputPayload(101L)
instance
}
val child = base.createChildScope()
child.eval(
"""
extern fun input(): InputResult
fun main() {
X = input().value
}
""".trimIndent()
)
child.eval("main()")
assertEquals(101L, x, "extern property read directly from extern function result should assign live extern var")
}
} }
private data class ChoicePayload( private data class ChoicePayload(
val isSkip: Boolean, val isSkip: Boolean,
) )
private data class InputPayload(
val value: Long,
)
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private fun <T> ScopeFacade.thisObjData(): T { private fun <T> ScopeFacade.thisObjData(): T {
val instance = thisObj as? ObjInstance ?: raiseClassCastError("Expected result object instance") val instance = thisObj as? ObjInstance ?: raiseClassCastError("Expected result object instance")
+18 -14
View File
@@ -15,17 +15,21 @@
* *
*/ */
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout import kotlinx.coroutines.withTimeout
import kotlin.test.Test import kotlin.test.Test
import net.sergeych.lyng.eval as lyngEval import net.sergeych.lyng.eval as lyngEval
class LaunchPoolTest { class LaunchPoolTest {
private suspend fun eval(code: String) = withTimeout(2_000L) { lyngEval(code) } private suspend fun eval(code: String) = withContext(Dispatchers.Default) {
withTimeout(2_000L) { lyngEval(code) }
}
@Test @Test
fun testBasicExecution() = runBlocking<Unit> { fun testBasicExecution() = runTest {
eval(""" eval("""
val pool = LaunchPool(2) val pool = LaunchPool(2)
val d1 = pool.launch { 1 + 1 } val d1 = pool.launch { 1 + 1 }
@@ -37,7 +41,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testResultsCollected() = runBlocking<Unit> { fun testResultsCollected() = runTest {
eval(""" eval("""
val pool = LaunchPool(4) val pool = LaunchPool(4)
val jobs = (1..10).map { n -> pool.launch { n * n } } val jobs = (1..10).map { n -> pool.launch { n * n } }
@@ -48,7 +52,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testConcurrencyLimit() = runBlocking<Unit> { fun testConcurrencyLimit() = runTest {
eval(""" eval("""
// With maxWorkers=2, at most 2 tasks run at the same time. // With maxWorkers=2, at most 2 tasks run at the same time.
val mu = Mutex() val mu = Mutex()
@@ -70,7 +74,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testExceptionCapturedInDeferred() = runBlocking<Unit> { fun testExceptionCapturedInDeferred() = runTest {
eval(""" eval("""
val pool = LaunchPool(2) val pool = LaunchPool(2)
val good = pool.launch { 42 } val good = pool.launch { 42 }
@@ -83,7 +87,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testPoolContinuesAfterLambdaException() = runBlocking<Unit> { fun testPoolContinuesAfterLambdaException() = runTest {
eval(""" eval("""
val pool = LaunchPool(1) val pool = LaunchPool(1)
val bad = pool.launch { throw IllegalArgumentException("fail") } val bad = pool.launch { throw IllegalArgumentException("fail") }
@@ -96,7 +100,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testLaunchAfterCloseAndJoinThrows() = runBlocking<Unit> { fun testLaunchAfterCloseAndJoinThrows() = runTest {
eval(""" eval("""
val pool = LaunchPool(2) val pool = LaunchPool(2)
pool.launch { 1 } pool.launch { 1 }
@@ -107,7 +111,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testLaunchAfterCancelThrows() = runBlocking<Unit> { fun testLaunchAfterCancelThrows() = runTest {
eval(""" eval("""
val pool = LaunchPool(2) val pool = LaunchPool(2)
pool.cancel() pool.cancel()
@@ -117,7 +121,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testCancelAndJoinWaitsForWorkers() = runBlocking<Unit> { fun testCancelAndJoinWaitsForWorkers() = runTest {
eval(""" eval("""
val pool = LaunchPool(2) val pool = LaunchPool(2)
pool.launch { delay(5) } pool.launch { delay(5) }
@@ -128,7 +132,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testCloseAndJoinDrainsQueue() = runBlocking<Unit> { fun testCloseAndJoinDrainsQueue() = runTest {
eval(""" eval("""
val mu = Mutex() val mu = Mutex()
val results = [] val results = []
@@ -147,7 +151,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testBoundedQueueSuspendsProducer() = runBlocking<Unit> { fun testBoundedQueueSuspendsProducer() = runTest {
eval(""" eval("""
// queue of 2 + 1 worker; producer can only be 1 ahead of what's running // queue of 2 + 1 worker; producer can only be 1 ahead of what's running
val pool = LaunchPool(1, 2) val pool = LaunchPool(1, 2)
@@ -165,7 +169,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testUnlimitedQueueDefault() = runBlocking<Unit> { fun testUnlimitedQueueDefault() = runTest {
eval(""" eval("""
val pool = LaunchPool(4) val pool = LaunchPool(4)
val jobs = (1..50).map { n -> pool.launch { n } } val jobs = (1..50).map { n -> pool.launch { n } }
@@ -177,7 +181,7 @@ class LaunchPoolTest {
} }
@Test @Test
fun testIdempotentClose() = runBlocking<Unit> { fun testIdempotentClose() = runTest {
eval(""" eval("""
val pool = LaunchPool(2) val pool = LaunchPool(2)
pool.closeAndJoin() pool.closeAndJoin()
@@ -473,6 +473,29 @@ class MiniAstTest {
assertEquals("b", fn.params[1].name) assertEquals("b", fn.params[1].name)
} }
@Test
fun miniAst_captures_context_receiver_function_type() = runTest {
val code = """
val block: context(Html, Head) Body.()->String = { "ok" }
""".trimIndent()
val (_, sink) = compileWithMini(code)
val mini = sink.build()
assertNotNull(mini)
val vd = mini.declarations.filterIsInstance<MiniValDecl>().firstOrNull { it.name == "block" }
assertNotNull(vd)
val type = vd.type as MiniFunctionType
val receiver = type.receiver as MiniTypeName
assertEquals(listOf("Body"), receiver.segments.map { it.name })
assertEquals(2, type.contextReceivers.size)
val ctx0 = type.contextReceivers[0] as MiniTypeName
val ctx1 = type.contextReceivers[1] as MiniTypeName
assertEquals(listOf("Html"), ctx0.segments.map { it.name })
assertEquals(listOf("Head"), ctx1.segments.map { it.name })
assertTrue(type.params.isEmpty())
val ret = type.returnType as MiniTypeName
assertEquals(listOf("String"), ret.segments.map { it.name })
}
@Test @Test
fun miniAst_captures_dokka_tags() = runTest { fun miniAst_captures_dokka_tags() = runTest {
val code = """ val code = """
@@ -0,0 +1,21 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.Script
import kotlin.test.*
class MultilineListLiteralTest {
@Test fun finalNewlineCommentsAndTrailingCommaAreWhitespace() = runTest {
val scope = Script.newScope()
for (source in listOf(
"[\n \"first\",\n \"second\"\n]",
"[\n \"first\",\n \"second\",\n]",
"[\n // before\n \"first\", /* between */\n \"second\" // after\n]",
)) assertEquals("[first,second]", scope.eval(source).toString(scope).value)
}
@Test fun nestedAndEmptyMultilineListsAndExpressionsRemainValid() = runTest {
val scope = Script.newScope()
assertEquals("[]", scope.eval("[\n]").toString(scope).value)
assertEquals("[[1,2],[3,4]]", scope.eval("[\n [1,2],\n [3,4]\n]").toString(scope).value)
assertEquals("[3,7]", scope.eval("[\n 1+2,\n 3+4\n]").toString(scope).value)
assertFails { scope.eval("[\n @\n]") }
}
}
Loaded 100 of 118 files, more files were not shown because too many files have changed in this diff. Show more