Compare commits

...
14 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
61 changed files with 1796 additions and 86 deletions

No files matched your search

+1
View File
@@ -32,3 +32,4 @@ test_output*.txt
/bugs/
contents.db
/docs/idea_plugin.md.deploy_backup
/.claude/settings.local.json
+1
View File
@@ -1,6 +1,7 @@
# AI Agent Notes
## 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.
- 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.
+4 -1
View File
@@ -9,7 +9,10 @@ History note:
## 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)
+2
View File
@@ -47,6 +47,7 @@ assertEquals(A.E.One, A.One)
## Resources:
- [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
- [Latest release notes (1.5.6)](docs/whats_new.md)
- [What's New in 1.5](docs/whats_new_1_5.md)
@@ -55,6 +56,7 @@ assertEquals(A.E.One, A.One)
- [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)
- [Json support](docs/json_and_kotlin_serialization.md)
+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
target=vps # default: new server; use --old for d.lynglang.com
for arg in "$@"; do
if [[ "$arg" == "-u" || "$arg" == "--upload-only" ]]; then
upload_only=true
elif [[ "$arg" == -P* ]]; then
GRADLE_ARGS+=("$arg")
elif [[ "$arg" == "--old" ]]; then
target=com
fi
@@ -125,7 +129,7 @@ if [[ "$upload_only" == false ]]; then
refreshTextmateZip
updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link"
./gradlew site:jsBrowserDistribution
./gradlew site:jsBrowserDistribution "${GRADLE_ARGS[@]}"
BUILD_RC=$?
# Always restore original doc if backup exists
+2 -1
View File
@@ -113,9 +113,10 @@ process_file() {
# - [text](file.md) -> [text](#file.md)
# - [text](dir/file.md) -> [text](#dir_file.md)
# - [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" | \
perl -pe 's{(!\[[^\]]*\]\()/}{$1site/src/jsMain/resources/}g' | \
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD"
+17
View File
@@ -21,6 +21,23 @@ plugins {
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.
// Usage: ./gradlew runIde
// It simply delegates to :lyng-idea:runIde provided by the Gradle IntelliJ Plugin.
+26 -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
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 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.
@@ -1042,6 +1048,25 @@ Static fields can be accessed from static methods via the class qualifier:
assertEquals("bar", Test.getData() )
>>> 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
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.
@@ -1248,7 +1273,7 @@ scopeB()
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
+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.
+7
View File
@@ -43,6 +43,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
## 3. Literals and Core Expressions
- 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 `...`.
- 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).
@@ -65,6 +67,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- Declaration-side variadic parameters use ellipsis suffix:
- functions: `fun f(head, tail...) { ... }`
- 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:
- positional splat: `f(...[1,2,3])`
- named splat: `f(...{ a: 1, b: 2 })` (map-style)
@@ -122,6 +126,7 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- 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() = ...`.
- 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(...)`.
- delegated callable: `fun f(...) by delegate`.
- Type aliases:
@@ -129,6 +134,8 @@ Primary sources used: `lynglib/src/commonMain/kotlin/net/sergeych/lyng/{Parser,T
- generic: `type Box<T> = List<T>`
- aliases are expanded structurally.
- 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.
- `object` supports named singleton and anonymous object expression forms.
- enums support lifted entries: `enum E* { A, B }`.
+1
View File
@@ -98,6 +98,7 @@ Requires installing `lyngio` into the import manager from host code.
- `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.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:
- `import lyng.io.http.types` (`HttpHeaders`)
- `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.
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:
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.
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
For module-level APIs, the default workflow is:
+2
View File
@@ -1,5 +1,7 @@
# 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.
+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`.
+29
View File
@@ -42,6 +42,21 @@ 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
@@ -179,3 +194,17 @@ val allowLocalOnly = object : HttpAccessPolicy {
- **Windows native:** supported via the Ktor WinHttp client backend
- **Apple native:** supported via the Ktor Darwin client backend
- **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.
+2 -1
View File
@@ -16,7 +16,8 @@
#
#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.configuration-cache=true
#Kotlin
+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.
+17 -1
View File
@@ -29,7 +29,7 @@ plugins {
}
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> {
val overrideDir = providers.gradleProperty("sqlite3.lib.dir").orNull
@@ -141,6 +141,7 @@ kotlin {
api(libs.kotlinx.coroutines.core)
api(libs.mordant.core)
api(libs.ktor.client.core)
implementation("com.fleeksoft.ksoup:ksoup:0.2.6")
implementation(libs.ktor.client.websockets)
}
}
@@ -329,6 +330,15 @@ tasks.matching { it.name.startsWith("lint", ignoreCase = true) }.configureEach {
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 {
val mavenToken by lazy {
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
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(CIO)
actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(CIO, config)
@@ -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
import kotlinx.coroutines.CancellationException
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope
import net.sergeych.lyng.ScopeFacade
@@ -41,6 +43,7 @@ import net.sergeych.lyng.requireNoArgs
import net.sergeych.lyng.requireScope
import net.sergeych.lyngio.http.LyngHttpRequest
import net.sergeych.lyngio.http.LyngHttpResponse
import net.sergeych.lyngio.http.LyngHttpConfig
import net.sergeych.lyngio.http.getSystemHttpEngine
import net.sergeych.lyngio.http.security.HttpAccessDeniedException
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"
internal const val HTTP_TYPES_MODULE_NAME = "lyng.io.http.types"
fun createHttpModule(policy: HttpAccessPolicy, scope: Scope): Boolean =
createHttpModule(policy, scope.importManager)
fun createHttpModule(
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)
if (manager.packageNames.contains(HTTP_MODULE_NAME)) return false
manager.addPackage(HTTP_MODULE_NAME) { module ->
buildHttpModule(module, policy)
buildHttpModule(module, policy, config)
}
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 {
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)
}
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))
val engine = getSystemHttpEngine()
val engine = getSystemHttpEngine(config)
val headersType = ObjHttpHeaders.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 {
return try {
block()
} catch (e: CancellationException) {
throw e
} catch (e: HttpAccessDeniedException) {
raiseIllegalOperation(e.reasonDetail ?: "http access denied")
raiseIllegalOperation("HTTP_ACCESS_DENIED: " + (e.reasonDetail ?: "http access denied"))
} catch (e: Exception) {
raiseIllegalOperation(e.message ?: "http error")
}
@@ -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.HttpClientEngineFactory
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.request
import io.ktor.client.request.setBody
@@ -14,14 +16,21 @@ import io.ktor.http.takeFrom
internal fun createKtorHttpEngine(
engineFactory: HttpClientEngineFactory<HttpClientEngineConfig>,
): LyngHttpEngine = KtorLyngHttpEngine(engineFactory)
config: LyngHttpConfig,
): LyngHttpEngine = KtorLyngHttpEngine(engineFactory, config)
private class KtorLyngHttpEngine(
engineFactory: HttpClientEngineFactory<HttpClientEngineConfig>,
config: LyngHttpConfig,
) : LyngHttpEngine {
private val clientResult = runCatching {
HttpClient(engineFactory) {
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")
}
val response = httpClient.request {
applyRequest(request)
try {
val response = httpClient.request { applyRequest(request) }
return LyngHttpResponse(
status = response.status.value,
statusText = response.status.description,
headers = response.headers.entries().associate { it.key to it.value.toList() },
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)
}
return LyngHttpResponse(
status = response.status.value,
statusText = response.status.description,
headers = response.headers.entries().associate { it.key to it.value.toList() },
bodyBytes = response.body<ByteArray>(),
)
}
private fun HttpRequestBuilder.applyRequest(request: LyngHttpRequest) {
@@ -33,6 +33,18 @@ data class LyngHttpResponse(
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 {
val isSupported: Boolean
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
@@ -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))
}
}
}
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
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
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(Js)
actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(Js, config)
@@ -2,4 +2,4 @@ package net.sergeych.lyngio.http
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,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
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
actual fun getSystemHttpEngine(): LyngHttpEngine = createKtorHttpEngine(WinHttp)
actual fun getSystemHttpEngine(config: LyngHttpConfig): LyngHttpEngine = createKtorHttpEngine(WinHttp, config)
+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
}
/* 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 {
/* True when HTTP client support is available on this runtime. */
fun isSupported(): Bool
+8 -4
View File
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych"
version = "1.5.9"
version = providers.gradleProperty("lyng.version").getOrElse("1.5.10")
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
@@ -47,7 +47,11 @@ buildkonfig {
kotlin {
jvmToolchain(17)
jvm()
jvm {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
}
}
androidTarget {
publishLibraryVariants("release")
@OptIn(ExperimentalKotlinGradlePluginApi::class)
@@ -297,8 +301,8 @@ android {
minSdk = libs.versions.android.minSdk.get().toInt()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
dependencies {
@@ -509,5 +509,15 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
val isTransient: Boolean = false,
val annotationSpecs: List<ParsedDeclAnnotation> = 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)
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))
if (spec.declaredName != null) {
@@ -19,6 +19,7 @@ package net.sergeych.lyng
import net.sergeych.lyng.Compiler.Companion.compile
import net.sergeych.lyng.bytecode.*
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
@@ -2894,10 +2895,34 @@ class Compiler(
private suspend fun parseExpression(): Statement? {
val pos = cc.currentPos()
return parseExpressionLevel()?.let { ref ->
ExpressionStatement(ref, pos)
ExpressionStatement(lowerTypedAssignOperator(ref, pos), pos)
}
}
private fun lowerTypedAssignOperator(ref: ObjRef, pos: Pos): ObjRef {
val assign = ref as? AssignOpRef ?: return ref
val target = assign.target as? FieldRef ?: return ref
val methodName = when (assign.op) {
BinOp.PLUS -> "plusAssign"
BinOp.MINUS -> "minusAssign"
BinOp.STAR -> "mulAssign"
BinOp.SLASH -> "divAssign"
BinOp.PERCENT -> "modAssign"
else -> return ref
}
val targetClass = resolveReceiverTypeDecl(target)?.let { resolveTypeDeclObjClass(it) } ?: return ref
if (targetClass.getInstanceMemberOrNull(methodName, includeAbstract = true, includeStatic = false) == null) {
return ref
}
return MethodCallRef(
target,
methodName,
listOf(ParsedArgument(ExpressionStatement(assign.value, pos), pos)),
tailBlock = false,
isOptional = target.isOptional,
)
}
private suspend fun parseExpressionLevel(level: Int = 0): ObjRef? {
if (level == lastLevel)
return parseTerm()
@@ -3096,7 +3121,7 @@ class Compiler(
val field = FieldRef(left, next.value, isOptional)
operand = CallRef(field, args, tailBlock, isOptional, memberCallTypeArgs)
} else {
operand = MethodCallRef(left, next.value, args, tailBlock, isOptional, memberCallTypeArgs)
operand = MethodCallRef(left, next.value, args, tailBlock, isOptional, memberCallTypeArgs, classScopeCall = true)
}
} else {
// instance method call
@@ -3213,14 +3238,14 @@ class Compiler(
resolutionSink?.referenceReflection(name.value, next.pos)
}
}
MethodCallRef(left, next.value, args, true, isOptional)
MethodCallRef(left, next.value, args, true, isOptional, classScopeCall = shouldTreatAsClassScopeCall(left, next.value))
} else {
val unionCall = buildUnionMethodCall(left, next.value, next.pos, isOptional, args, true)
if (unionCall != null) {
unionCall
} else {
enforceReceiverTypeForMember(left, next.value, next.pos)
MethodCallRef(left, next.value, args, true, isOptional)
MethodCallRef(left, next.value, args, true, isOptional, classScopeCall = shouldTreatAsClassScopeCall(left, next.value))
}
}
is QualifiedThisRef ->
@@ -3240,7 +3265,7 @@ class Compiler(
unionCall
} else {
enforceReceiverTypeForMember(left, next.value, next.pos)
MethodCallRef(left, next.value, args, true, isOptional)
MethodCallRef(left, next.value, args, true, isOptional, classScopeCall = shouldTreatAsClassScopeCall(left, next.value))
}
}
}
@@ -3671,7 +3696,12 @@ class Compiler(
val effectiveType = if (param.isEllipsis) TypeDecl.Ellipsis(rawType) else rawType
lambdaParamTypeDecls += effectiveType
if (effectiveType != TypeDecl.TypeAny && effectiveType != TypeDecl.TypeNullableAny) {
seedLambdaParamType(param.name, rawType)
val localType = if (param.isEllipsis) {
TypeDecl.Generic("List", listOf(rawType), false)
} else {
rawType
}
seedLambdaParamType(param.name, localType)
}
}
} else {
@@ -3753,7 +3783,7 @@ class Compiler(
val returnClass = inferReturnClassFromStatement(body)
val paramKnownClasses = mutableMapOf<String, ObjClass>()
argsDeclaration?.params?.forEach { param ->
val cls = resolveTypeDeclObjClass(param.type) ?: return@forEach
val cls = resolveTypeDeclObjClass(param.localType) ?: return@forEach
paramKnownClasses[param.name] = cls
}
val returnLabels = label?.let { setOf(it) } ?: emptySet()
@@ -3991,6 +4021,7 @@ class Compiler(
while (true) {
val t = cc.next()
when (t.type) {
Token.Type.NEWLINE, Token.Type.SINGLE_LINE_COMMENT, Token.Type.MULTILINE_COMMENT -> continue
Token.Type.COMMA -> {
// todo: check commas sequences like [,] [,,] before, after or instead of expressions
}
@@ -4289,10 +4320,15 @@ class Compiler(
}
var defaultValue: Obj? = null
cc.ifNextIs(Token.Type.ASSIGN) {
var defaultSource: String? = null
cc.ifNextIs(Token.Type.ASSIGN) { assignment ->
val expr = parseExpression()
?: throw ScriptError(cc.current().pos, "Expected default value expression")
if (typeInfo == TypeDecl.TypeAny) {
inferTypeDeclFromInitializer(expr)?.let { typeInfo = it }
}
defaultValue = wrapBytecode(expr)
defaultSource = extractDefaultArgumentSource(assignment.pos)
}
val isEllipsis = cc.skipTokenOfType(Token.Type.ELLIPSIS, isOptional = true)
result += ArgsDeclaration.Item(
@@ -4305,7 +4341,8 @@ class Compiler(
effectiveAccess,
visibility,
isTransient,
annotationSpecs = annotationSpecs
annotationSpecs = annotationSpecs,
defaultSource = defaultSource,
)
// important: valid argument list continues with ',' and ends with '->' or ')'
@@ -4343,6 +4380,48 @@ class Compiler(
return ArgsDeclaration(result, endTokenType)
}
/**
* Extracts a default expression from its original source. Token positions for strings point
* inside their quotes, so reconstructing the range from the first/last expression token loses
* delimiters. Scan the source instead, respecting nested groups and quoted literals.
*/
private fun extractDefaultArgumentSource(assignmentPos: Pos): String {
val source = assignmentPos.source
val text = source.text
val start = source.offsetOf(assignmentPos) + 1
var index = start
var round = 0
var square = 0
var curly = 0
var quote: Char? = null
var escaped = false
while (index < text.length) {
val ch = text[index]
val activeQuote = quote
if (activeQuote != null) {
if (escaped) escaped = false
else if (ch == '\\') escaped = true
else if (ch == activeQuote) quote = null
index++
continue
}
when (ch) {
'"', '\'', '`' -> quote = ch
'(' -> round++
')' -> if (round == 0 && square == 0 && curly == 0) break else round--
'[' -> square++
']' -> square--
'{' -> curly++
'}' -> curly--
',' -> if (round == 0 && square == 0 && curly == 0) break
'.' -> if (round == 0 && square == 0 && curly == 0 &&
text.startsWith("...", index)) break
}
index++
}
return text.substring(start, index).trim()
}
@Suppress("unused")
private fun parseTypeDeclaration(): TypeDecl {
return parseTypeDeclarationWithMini().first
@@ -7379,6 +7458,14 @@ class Compiler(
isOptional: Boolean,
explicitTypeArgs: List<TypeDecl>? = null
): ObjRef {
val thisTypeName = when (left) {
is LocalVarRef -> if (left.name == "this") currentImplicitThisTypeName() else null
is QualifiedThisRef -> left.typeName
else -> null
}
if (thisTypeName != null && codeContexts.any { it is CodeContext.ClassBody && it.name == thisTypeName }) {
throw ScriptError(cc.currentPos(), "constructor delegation through this is not supported; call the class by name")
}
var detectedBlockArgument = blockArgument
val expectedReceiver = tailBlockReceiverType(left)
val expectedTailCallable = expectedCallableArgumentType(left, 0)
@@ -8599,6 +8686,7 @@ class Compiler(
val initScope = popInitScope()
val wrappedBodyInit = bodyInit?.let { wrapClassBodyBytecode(it, "object@$className") }
val wrappedInitScope = initScope.map { wrapClassBodyBytecode(it, "objectInit@$className") }
val spec = ClassDeclSpec(
declaredName = declaredName,
@@ -8614,7 +8702,7 @@ class Compiler(
constructorArgs = null,
constructorFieldIds = null,
bodyInit = wrappedBodyInit,
initScope = emptyList()
initScope = wrappedInitScope
)
return ClassDeclStatement(spec)
}
@@ -8666,7 +8754,7 @@ class Compiler(
constructorArgsDeclaration?.params?.forEach { param ->
if (param.accessType != null) {
val declClass = resolveTypeDeclObjClass(param.type)
val declClass = resolveTypeDeclObjClass(param.localType)
if (declClass != null) {
classFieldTypesByName.getOrPut(qualifiedName) { mutableMapOf() }[param.name] = declClass
}
@@ -8684,8 +8772,8 @@ class Compiler(
val classParamTypeDeclMap = slotTypeDeclByScopeId.getOrPut(classSlotPlan.id) { mutableMapOf() }
for (param in ctorDecl.params) {
val slot = classSlotPlan.slots[param.name]?.index ?: continue
classParamTypeDeclMap[slot] = param.type
resolveTypeDeclObjClass(param.type)?.let { classParamTypeMap[slot] = it }
classParamTypeDeclMap[slot] = param.localType
resolveTypeDeclObjClass(param.localType)?.let { classParamTypeMap[slot] = it }
}
}
val ctorForcedLocalSlots = LinkedHashMap<String, Int>()
@@ -9905,6 +9993,10 @@ class Compiler(
name = t.value
nameStartPos = t.pos
}
if (parentContext is CodeContext.ClassBody && extTypeName == null &&
name == parentContext.name.substringAfterLast('.')) {
throw ScriptError(nameStartPos, "member function cannot have the same name as its enclosing class: $name")
}
val extensionWrapperName = extTypeName?.let { extensionCallableName(it, name) }
val classCtx = codeContexts.asReversed().firstOrNull { it is CodeContext.ClassBody } as? CodeContext.ClassBody
var memberMethodId = if (extTypeName == null) classCtx?.memberMethodIds?.get(name) else null
@@ -9980,11 +10072,11 @@ class Compiler(
pendingTypeParamStack.removeLast()
}
if (parentContext is CodeContext.ClassBody && !isStatic && extTypeName == null) {
if (parentContext is CodeContext.ClassBody && extTypeName == null) {
classMemberTypeDeclByName.getOrPut(parentContext.name) { mutableMapOf() }[name] = TypeDecl.Function(
receiver = receiverTypeDecl,
contextReceivers = contextReceiverTypeDecls,
params = argsDeclaration.params.map { it.type },
params = argsDeclaration.params.map { it.signatureType },
returnType = returnTypeDecl ?: TypeDecl.TypeAny,
nullable = false
)
@@ -10087,19 +10179,19 @@ class Compiler(
argsDeclaration = wrapDefaultArgsBytecode(argsDeclaration, forcedLocalSlots, paramSlotPlan.id)
val capturePlan = CapturePlan(paramSlotPlan, isFunction = true, propagateToParentFunction = false)
val rangeParamNames = argsDeclaration.params
.filter { isRangeType(it.type) }
.filter { isRangeType(it.localType) }
.map { it.name }
.toSet()
val paramTypeMap = slotTypeByScopeId.getOrPut(paramSlotPlan.id) { mutableMapOf() }
val paramTypeDeclMap = slotTypeDeclByScopeId.getOrPut(paramSlotPlan.id) { mutableMapOf() }
for (param in argsDeclaration.params) {
val cls = resolveTypeDeclObjClass(param.type) ?: continue
val cls = resolveTypeDeclObjClass(param.localType) ?: continue
val slot = paramSlotPlan.slots[param.name]?.index ?: continue
paramTypeMap[slot] = cls
}
for (param in argsDeclaration.params) {
val slot = paramSlotPlan.slots[param.name]?.index ?: continue
paramTypeDeclMap[slot] = param.type
paramTypeDeclMap[slot] = param.localType
}
// Parse function body while tracking declared locals to compute precise capacity hints
@@ -10154,13 +10246,13 @@ class Compiler(
val inferredReturnDecl = returnTypeDecl ?: inferReturnTypeDeclFromStatement(rawFnStatements)
val inferredReturnClass = inferredReturnDecl?.let { resolveTypeDeclObjClass(it) }
?: inferReturnClassFromStatement(rawFnStatements)
if (parentContext is CodeContext.ClassBody && !isStatic && extTypeName == null) {
if (parentContext is CodeContext.ClassBody && extTypeName == null) {
val ownerClassName = parentContext.name
run {
val memberTypeDecl = TypeDecl.Function(
receiver = receiverTypeDecl,
contextReceivers = contextReceiverTypeDecls,
params = argsDeclaration.params.map { it.type },
params = argsDeclaration.params.map { it.signatureType },
returnType = inferredReturnDecl ?: TypeDecl.TypeAny,
nullable = false
)
@@ -10194,7 +10286,7 @@ class Compiler(
if (!compileBytecode) return@let stmt
val paramKnownClasses = mutableMapOf<String, ObjClass>()
for (param in argsDeclaration.params) {
val cls = resolveTypeDeclObjClass(param.type) ?: continue
val cls = resolveTypeDeclObjClass(param.localType) ?: continue
paramKnownClasses[param.name] = cls
}
wrapFunctionBytecode(
@@ -10357,7 +10449,7 @@ class Compiler(
typeDecl = if (isDelegated) null else TypeDecl.Function(
receiver = receiverTypeDecl,
contextReceivers = contextReceiverTypeDecls,
params = argsDeclaration.params.map { it.type },
params = argsDeclaration.params.map { it.signatureType },
returnType = inferredReturnDecl ?: TypeDecl.TypeAny,
nullable = false
),
@@ -10366,7 +10458,32 @@ class Compiler(
captureSlots = captureSlots,
slotIndex = declSlotIndex,
scopeId = declScopeId,
startPos = start
startPos = start,
resolvedMetadata = ResolvedFunctionMetadata(
name = name,
namePos = nameStartPos,
declarationPos = start,
visibility = visibility,
annotations = declarationAnnotationSpecs.map { it.name },
typeParams = mergedTypeParamDecls,
parameters = argsDeclaration.params.map { parameter ->
ResolvedFunctionParameter(
name = parameter.name,
type = parameter.type,
isEllipsis = parameter.isEllipsis,
hasDefault = parameter.defaultValue != null,
defaultSource = parameter.defaultSource,
pos = parameter.pos,
)
},
functionType = TypeDecl.Function(
receiver = receiverTypeDecl,
contextReceivers = contextReceiverTypeDecls,
params = argsDeclaration.params.map { it.signatureType },
returnType = inferredReturnDecl ?: TypeDecl.TypeAny,
nullable = false,
),
),
)
val declaredFn = FunctionDeclStatement(spec)
if (isStatic && parentIsClassBody) {
@@ -11725,6 +11842,28 @@ class Compiler(
return script
}
/**
* Resolve semantic metadata for top-level functions in [source] without evaluating it.
*
* Functions are returned in source order. Their metadata includes annotations, visibility,
* generic parameters, parameter declarations and defaults, receiver information, and the
* declared or compiler-inferred return type. Annotation names are collected syntactically;
* annotation functions and default expressions are not executed.
*
* This performs parsing, import resolution, and semantic compilation, but does not generate
* executable bytecode. Use [compile] when the resulting script must also be executed.
*
* @param source named Lyng source whose top-level functions should be inspected
* @param importManager provider used to resolve the source's imports and external types
* @return metadata for each top-level function declaration in source order
*/
suspend fun resolveFunctionMetadata(
source: Source,
importManager: ImportProvider,
): List<ResolvedFunctionMetadata> =
compileWithResolution(source, importManager, compileBytecode = false)
.resolvedFunctionMetadata()
suspend fun dryRun(source: Source, importManager: ImportProvider): ResolutionReport {
return CompileTimeResolver.dryRun(source, importManager)
}
@@ -50,6 +50,7 @@ data class FunctionDeclSpec(
val slotIndex: Int?,
val scopeId: Int?,
val startPos: Pos,
val resolvedMetadata: ResolvedFunctionMetadata,
)
internal suspend fun executeFunctionDecl(
@@ -18,6 +18,7 @@ package net.sergeych.lyng
import net.sergeych.lyng.bytecode.CmdFrame
import net.sergeych.lyng.bytecode.CmdVm
import net.sergeych.lyng.bytecode.seedFrameLocalsFromScope
import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjNull
@@ -34,6 +35,7 @@ class PropertyAccessorStatement(
val bytecodeStmt = requireBytecodeBody(scope, body, "property accessor")
val fn = bytecodeStmt.bytecodeFunction()
val binder: suspend (CmdFrame, Arguments) -> Unit = { frame, arguments ->
seedFrameLocalsFromScope(frame, scope)
val slotPlan = fn.localSlotPlanByName()
val slotIndex = slotPlan[argName]
?: 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
}
@@ -19,17 +19,16 @@ package net.sergeych.lyng
import kotlinx.coroutines.delay
import kotlinx.coroutines.yield
import net.sergeych.lyng.Script.Companion.defaultImportManager
import net.sergeych.lyng.bridge.bind
import net.sergeych.lyng.bridge.bindObject
import net.sergeych.lyng.bytecode.BytecodeLambdaCallable
import net.sergeych.lyng.bytecode.CmdFunction
import net.sergeych.lyng.bytecode.CmdVm
import net.sergeych.lyng.bytecode.BytecodeLambdaCallable
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.serialization.ObjJsonClass
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.decimalLyng
import net.sergeych.lyng.stdlib_included.legacyDigestLyng
@@ -55,6 +54,18 @@ class Script(
) : Statement() {
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.
* This is intended for embedding scenarios where the host owns scope lifecycle and wants
@@ -4876,11 +4876,16 @@ class BytecodeCompiler(
val receiverClass = resolveReceiverClass(ref.receiver) ?: ObjDynamic.type
val receiver = compileRefWithFallback(ref.receiver, null, refPosOrCurrent(ref.receiver)) ?: return null
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 memberSlot = allocSlot()
if (!ref.isOptional) {
builder.emit(Opcode.GET_CLASS_SCOPE, receiver.slot, nameId, memberSlot)
builder.emit(Opcode.GET_CLASS_SCOPE, classSlot, nameId, memberSlot)
} else {
val nullSlot = allocSlot()
builder.emit(Opcode.CONST_NULL, nullSlot)
@@ -4892,7 +4897,7 @@ class BytecodeCompiler(
Opcode.JMP_IF_TRUE,
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.mark(nullLabel)
builder.emit(Opcode.CONST_NULL, memberSlot)
@@ -3261,12 +3261,14 @@ class CmdDeclExtProperty(internal val constId: Int, internal val slot: Int) : Cm
override suspend fun perform(frame: CmdFrame) {
val decl = frame.fn.constants[constId] as? BytecodeConst.ExtensionPropertyDecl
?: error("DECL_EXT_PROPERTY expects ExtensionPropertyDecl at $constId")
val type = frame.ensureScope().resolveExtensionReceiverClass(decl.extTypeName)
frame.ensureScope().addExtension(
val declarationScope = frame.ensureScope()
val property = decl.property.withDeclarationScope(declarationScope)
val type = declarationScope.resolveExtensionReceiverClass(decl.extTypeName)
declarationScope.addExtension(
type,
decl.property.name,
property.name,
ObjRecord(
decl.property,
property,
isMutable = false,
visibility = decl.visibility,
writeVisibility = decl.setterVisibility,
@@ -3274,9 +3276,9 @@ class CmdDeclExtProperty(internal val constId: Int, internal val slot: Int) : Cm
type = ObjRecord.Type.Property
)
)
val getterName = extensionPropertyGetterName(decl.extTypeName, decl.property.name)
val getterWrapper = ObjExtensionPropertyGetterCallable(decl.property.name, decl.property)
frame.ensureScope().addItem(
val getterName = extensionPropertyGetterName(decl.extTypeName, property.name)
val getterWrapper = ObjExtensionPropertyGetterCallable(property.name, property)
declarationScope.addItem(
getterName,
false,
getterWrapper,
@@ -3288,10 +3290,10 @@ class CmdDeclExtProperty(internal val constId: Int, internal val slot: Int) : Cm
if (getterLocal != null) {
frame.setObjUnchecked(frame.fn.scopeSlotCount + getterLocal, getterWrapper)
}
if (decl.property.setter != null) {
val setterName = extensionPropertySetterName(decl.extTypeName, decl.property.name)
val setterWrapper = ObjExtensionPropertySetterCallable(decl.property.name, decl.property)
frame.ensureScope()
if (property.setter != null) {
val setterName = extensionPropertySetterName(decl.extTypeName, property.name)
val setterWrapper = ObjExtensionPropertySetterCallable(property.name, property)
declarationScope
.addItem(
setterName,
false,
@@ -56,9 +56,9 @@ internal fun trySeedFrameLocalsFromScopeFast(frame: CmdFrame, scope: Scope): Boo
record.type == ObjRecord.Type.Property -> return false
record.value is ObjProperty -> return false
else -> when (val direct = record.value) {
is FrameSlotRef -> direct.resolvedCaptureValueOrNull() ?: return false
is RecordSlotRef -> direct.resolvedCaptureValueOrNull() ?: return false
is ScopeSlotRef -> direct.resolvedCaptureValueOrNull() ?: return false
is FrameSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: return false
is RecordSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: return false
is ScopeSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: return false
else -> direct
}
}
@@ -86,9 +86,9 @@ internal suspend fun seedFrameLocalsFromScope(frame: CmdFrame, scope: Scope) {
scope.resolve(record, name)
} else {
when (val direct = record.value) {
is net.sergeych.lyng.FrameSlotRef -> direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.RecordSlotRef -> direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.ScopeSlotRef -> direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.FrameSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.RecordSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: direct
is net.sergeych.lyng.ScopeSlotRef -> if (record.isMutable) direct else direct.resolvedCaptureValueOrNull() ?: direct
else -> direct
}
}
@@ -32,15 +32,21 @@ import net.sergeych.lyng.executeBytecodeWithSeed
class ObjProperty(
val name: String,
val getter: Obj?,
val setter: Obj?
val setter: Obj?,
private val declarationScope: Scope? = null
) : Obj() {
fun withDeclarationScope(scope: Scope): ObjProperty =
ObjProperty(name, getter, setter, scope)
suspend fun callGetter(scope: Scope, instance: Obj, declaringClass: ObjClass? = null): Obj {
val g = getter ?: scope.raiseError("property $name has no getter")
// Execute getter in a child scope of the instance with 'this' properly set
// Match extension function behavior (access to instance scope + call 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
(g as? BytecodeCallable)?.callOnFast(execScope)?.let { return it }
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
// Match extension function behavior (access to instance scope + call 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
(s as? BytecodeCallable)?.callOnFast(execScope)?.let { return }
when (s) {
@@ -484,6 +484,7 @@ class MethodCallRef(
internal val tailBlock: Boolean,
internal val isOptional: Boolean,
internal val explicitTypeArgs: List<TypeDecl>? = null,
internal val classScopeCall: Boolean = false,
) : ObjRef {
// 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
@@ -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())
}
}
@@ -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]") }
}
}
@@ -249,4 +249,39 @@ class ScriptImportPreparationTest {
val result = script.execute(manager.newStdScope()) as ObjString
assertEquals("<html><h3>Imported</h3></html>", result.value)
}
@Test
fun importedExtensionPropertyAccessorsUseDeclaringModuleScope() = runTest {
val manager = Script.defaultImportManager.copy().apply {
addTextPackages(
"""
package imported.extprop
private var prefix = "module"
private fun decorate(value: String): String = prefix + ":" + value
var String.decorated: String
get() = decorate(this)
set(value) { prefix = value }
""".trimIndent()
)
}
val script = Compiler.compile(
Source(
"<extension-property-import>",
"""
import imported.extprop
val before = "ok".decorated
"ignored".decorated = "updated"
before + "|" + "ok".decorated
""".trimIndent()
),
manager
)
val result = script.execute(manager.newStdScope()) as ObjString
assertEquals("module:ok|updated:ok", result.value)
}
}
@@ -0,0 +1,76 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
import kotlin.test.assertEquals
class StaticReturnTypeRegressionTest {
@Test
fun declaredFactoryReturnSupportsInferredVariablesAndChains() = runTest {
assertEquals("hi hi hi", eval("""
class Factory(x) {
static fun make(y): Factory = Factory(y)
fun hi(): String = "hi"
}
val a: Factory = Factory.make(1)
val b = Factory.make(2)
a.hi() + " " + b.hi() + " " + Factory.make(3).hi()
""".trimIndent()).toString())
}
@Test
fun declaredScalarReturnsSupportMembersAndArithmetic() = runTest {
assertEquals("4", eval("""
class Scalars {
static fun text(y): String = "abc"
static fun num(y: Int): Int = y
}
val s = Scalars.text(1)
val n = Scalars.num(1)
s.length + n
""".trimIndent()).toString())
}
@Test
fun singletonStaticReturnSupportsMembers() = runTest {
assertEquals("3", eval("""
object Singleton { static fun text(): String = "abc" }
val text = Singleton.text()
text.length
""".trimIndent()).toString())
}
@Test
fun singletonStaticTailBlockAndInstanceMethodsUseTheirOwnDispatch() = runTest {
assertEquals("6", eval("""
object Singleton {
static fun text(block: ()->String): String = block()
fun instanceText(): String = "abc"
}
Singleton.text { "abc" }.length + Singleton.instanceText().length
""".trimIndent()).toString())
}
@Test
fun inferredStaticReturnMatchesInstanceAndTopLevelFunctions() = runTest {
assertEquals("9", eval("""
class Factory {
static fun text() = "abc"
fun instanceText() = "abc"
}
fun topText() = "abc"
Factory.text().length + Factory().instanceText().length + topText().length
""".trimIndent()).toString())
}
@Test
fun genericStaticReturnSubstitutesArgumentType() = runTest {
assertEquals("7", eval("""
class Box<T>(val value: T) {
static fun of<T>(v: T): Box<T> = Box(v)
}
val box = Box.of("abc")
val explicit = Box.of<String>("abcd")
box.value.length + explicit.value.length
""".trimIndent()).toString())
}
}
@@ -768,6 +768,33 @@ class TypesTest {
assertTrue(e.message?.contains("extern variable value cannot have an initializer or delegate") == true)
}
@Test
fun testInferenceFromDefaults() = runTest {
eval("""
fun foo(i = 42, s = "bar",r = 0.0) {
assert( i is Int )
assert( s is String )
assert( r is Real )
r.toInt()
}
class Foobar(val i = 100,val s = "42", r = 1.0) {
fun test(amount: Int) {
assert( i is Int )
assert( s is String )
assert( r is Real )
(r * amount).toInt()
}
}
foo()
val fb = Foobar()
assert(fb.i is Int)
assert(fb.s is String)
assert(fb.r is Real)
val n = fb.test(15)
assertEquals(n, 15)
""".trimIndent())
}
// @Test fun nonTrivialOperatorsTest() = runTest {
// val s = Script.newScope()
// s.eval("""
@@ -0,0 +1,99 @@
/*
* Copyright 2026 Sergey S. Chernov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.bridge.globalBinder
import net.sergeych.lyng.obj.ObjClass
import net.sergeych.lyng.obj.ObjString
import kotlin.test.Test
import kotlin.test.assertEquals
class ImportedExternExtensionPropertyTest {
@Test
fun importedExtensionGetterCanCallExternFromItsImportedRuntimeModule() = runTest {
val imports = Script.defaultImportManager.copy()
imports.addPackage("test.contract.runtime") { module ->
module.eval(
"""
package test.contract.runtime
class ContractContext(val label: String)
class ContractRegistryBase
class ContractRegistry : ContractRegistryBase
extern val Contracts: ContractRegistry
extern fun contractHandle(nameOrId: String): ContractContext
""".trimIndent()
)
val contextClass = module.resolve(
requireNotNull(module["ContractContext"]),
"ContractContext"
) as ObjClass
val registryClass = module.resolve(
requireNotNull(module["ContractRegistry"]),
"ContractRegistry"
) as ObjClass
val context = contextClass.callOn(
module.createChildScope(args = Arguments(ObjString("bound")))
)
val registry = registryClass.callOn(module.createChildScope())
module.globalBinder().bindGlobalVarRaw("Contracts", get = { registry })
module.globalBinder().bindGlobalFunRaw("contractHandle") { _, args ->
assertEquals("Alpha", (args.firstAndOnly() as ObjString).value)
context
}
}
imports.addTextPackages(
"""
package test.contract.Alpha
import test.contract.runtime
class AlphaContractAbi(val target: ContractContext)
val ContractRegistryBase.AlphaHandle: ContractContext
get() = contractHandle("Alpha")
val ContractRegistryBase.Alpha: AlphaContractAbi
get() = AlphaContractAbi(contractHandle("Alpha"))
""".trimIndent()
)
val scope = imports.newStdScope()
val handleResult = scope.eval(
"""
import test.contract.runtime
import test.contract.Alpha
Contracts.AlphaHandle.label
""".trimIndent()
) as ObjString
assertEquals("bound", handleResult.value)
val result = scope.eval(
"""
import test.contract.runtime
import test.contract.Alpha
Contracts.Alpha.target.label
""".trimIndent()
) as ObjString
assertEquals("bound", result.value)
}
}
@@ -256,6 +256,46 @@ class OperatorOverloadingTest {
""".trimIndent())
}
@Test
fun testAssignOperatorMethodsOnValMember() = runTest {
eval("""
class Counter(var n: Int) {
fun plusAssign(x: Int) { n = n + x }
fun minusAssign(x: Int) { n = n - x }
fun mulAssign(x: Int) { n = n * x }
fun divAssign(x: Int) { n = n / x }
fun modAssign(x: Int) { n = n % x }
}
class Holder(val counter: Counter)
val holder = Holder(Counter(10))
holder.counter += 2
assertEquals(12, holder.counter.n)
holder.counter -= 3
assertEquals(9, holder.counter.n)
holder.counter *= 4
assertEquals(36, holder.counter.n)
holder.counter /= 6
assertEquals(6, holder.counter.n)
holder.counter %= 4
assertEquals(2, holder.counter.n)
""".trimIndent())
}
@Test
fun testAssignOperatorFallbackOnMutableMember() = runTest {
eval("""
class Counter(val n: Int) {
fun minus(x: Int) = Counter(n - x)
}
class Holder(var counter: Counter)
val holder = Holder(Counter(10))
holder.counter -= 3
assertEquals(7, holder.counter.n)
""".trimIndent())
}
@Test
fun testBuiltinListPlusAssignOnVal() = runTest {
eval("""
@@ -0,0 +1,135 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNull
import kotlin.test.assertSame
import kotlin.test.assertTrue
class ResolvedFunctionMetadataTest {
@Test
fun resolvesCompleteExportSignatureWithoutExecutingSource() = runTest {
val source = Source(
"metadata.lyng",
"""
@Export
fun exported<T: Object = String>(
head: T,
rest: T...,
limit: Int = 7,
suffix: String = "!",
): List<T> { [] }
fun helper(value: String): String { value }
""".trimIndent()
)
val functions = Compiler.resolveFunctionMetadata(source, Script.defaultImportManager)
val exported = functions.single { it.name == "exported" }
val helper = functions.single { it.name == "helper" }
assertEquals(listOf("Export"), exported.annotations)
assertTrue(helper.annotations.isEmpty())
assertEquals("metadata.lyng", exported.namePos.source.fileName)
assertSame(source, exported.namePos.source)
assertEquals("exported", exported.namePos.currentLine.substring(exported.namePos.column, exported.namePos.column + 8))
assertSame(source, exported.declarationPos.source)
val typeParameter = exported.typeParams.single()
assertEquals("T", typeParameter.name)
assertEquals(TypeDecl.Variance.Invariant, typeParameter.variance)
assertSimple(typeParameter.bound, "Object")
assertSimple(typeParameter.defaultType, "String")
assertEquals(listOf("head", "rest", "limit", "suffix"), exported.parameters.map { it.name })
assertTypeVariable(exported.parameters[0].type, "T")
assertFalse(exported.parameters[0].isEllipsis)
assertFalse(exported.parameters[0].hasDefault)
assertNull(exported.parameters[0].defaultSource)
assertTypeVariable(exported.parameters[1].type, "T")
assertTrue(exported.parameters[1].isEllipsis)
assertFalse(exported.parameters[1].hasDefault)
assertNull(exported.parameters[1].defaultSource)
assertSimple(exported.parameters[2].type, "Int")
assertFalse(exported.parameters[2].isEllipsis)
assertTrue(exported.parameters[2].hasDefault)
assertEquals("7", exported.parameters[2].defaultSource)
assertTrue(exported.parameters[3].hasDefault)
assertEquals("\"!\"", exported.parameters[3].defaultSource)
val returnType = assertIs<TypeDecl.Generic>(exported.returnType)
assertEquals("List", returnType.name)
assertEquals(1, returnType.args.size)
assertTypeVariable(returnType.args.single(), "T")
val functionType = assertIs<TypeDecl.Function>(exported.functionType)
assertNull(functionType.receiver)
assertTrue(functionType.contextReceivers.isEmpty())
assertEquals(4, functionType.params.size)
assertTypeVariable(functionType.params[0], "T")
val variadicType = assertIs<TypeDecl.Ellipsis>(functionType.params[1])
assertTypeVariable(variadicType.elementType, "T")
assertSimple(functionType.params[2], "Int")
assertGenericListOfTypeVariable(functionType.returnType, "T")
}
@Test
fun reportsCompilerInferredReturnType() = runTest {
val source = Source(
"inferred.lyng",
"""
@Export
fun increment(value: Int) = value + 1
""".trimIndent()
)
val function = Compiler.resolveFunctionMetadata(source, Script.defaultImportManager).single()
assertEquals("increment", function.name)
assertEquals(listOf("Export"), function.annotations)
assertSimple(function.returnType, "Int")
assertSimple(assertIs<TypeDecl.Function>(function.functionType).returnType, "Int")
}
private fun assertSimple(type: TypeDecl?, name: String, nullable: Boolean = false) {
val simple = assertIs<TypeDecl.Simple>(type)
assertEquals(name, simple.name)
assertEquals(nullable, simple.isNullable)
}
private fun assertTypeVariable(type: TypeDecl?, name: String, nullable: Boolean = false) {
val variable = assertIs<TypeDecl.TypeVar>(type)
assertEquals(name, variable.name)
assertEquals(nullable, variable.isNullable)
}
private fun assertGenericListOfTypeVariable(type: TypeDecl?, variableName: String) {
val generic = assertIs<TypeDecl.Generic>(type)
assertEquals("List", generic.name)
assertEquals(1, generic.args.size)
assertTypeVariable(generic.args.single(), variableName)
}
}
@@ -0,0 +1,58 @@
/*
* Copyright 2026 Sergey S. Chernov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.obj.ObjString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
class VariadicParameterTypingTest {
@Test
fun typedVariadicParameterIsAListInsideFunctionBody() = runTest {
val result = Script.newScope().eval(
"""
fun join(prefix: String, values: Int...): String =
prefix + values.joinToString(",")
fun sum(values: Int...): Int {
var result = 0
for (value in values) result += value
return result
}
join("n=", 1, 2, 3) + "|" + sum(4, 5, 6)
""".trimIndent()
) as ObjString
assertEquals("n=1,2,3|15", result.value)
}
@Test
fun variadicMetadataKeepsElementTypeInEllipsisSignature() = runTest {
val metadata = Compiler.resolveFunctionMetadata(
Source("variadic-metadata", "fun join(values: Int...): String = values.joinToString(\",\")"),
Script.defaultImportManager,
).single()
val parameter = metadata.parameters.single()
assertEquals("Int", (parameter.type as TypeDecl.Simple).name)
val signatureType = assertIs<TypeDecl.Ellipsis>(metadata.functionType.params.single())
assertEquals("Int", (signatureType.elementType as TypeDecl.Simple).name)
}
}
+14
View File
@@ -0,0 +1,14 @@
# Lyng
Lyng is a statically resolved scripting language. Read its current references before generating code; do not infer APIs from Kotlin or Java.
## Canonical AI references
- [Language reference](https://lynglang.com/docs/ai_language_reference.md): implemented syntax, types, control flow, modules, and current limitations.
- [Standard-library reference](https://lynglang.com/docs/ai_stdlib_reference.md): default globals, collections, built-in imports, and optional modules.
- [Agent instructions](https://lynglang.com/docs/ai_agents.md): documentation workflow, version checks, and offline use.
## Additional documentation
- [Tutorial](https://lynglang.com/docs/tutorial.md)
- [Source repository](https://gitea.sergeych.net/SergeychWorks/lyng)
The two compact references above take precedence over older specs or proposals. Optional modules must be installed by the host. Verify uncertain constructs with a small read-only script on the actual runtime before editing user data.