Remove interpreter fallbacks and enforce bytecode execution

This commit is contained in:
sergeych committed 2026-02-14 00:41:33 +03:00
1 parent 26564438e2
commit e73fe0d3c5
16 files changed
+700 -701

No files matched your search

+7 -34
View File
@@ -29,7 +29,6 @@ import com.github.ajalt.clikt.parameters.options.option
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.LyngVersion
import net.sergeych.lyng.Pos
import net.sergeych.lyng.Script
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.Source
@@ -158,7 +157,6 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
val version by option("-v", "--version", help = "Print version and exit").flag()
val benchmark by option("--benchmark", help = "Run JVM microbenchmarks and exit").flag()
val bytecodeFallbacks by option("--bytecode-fallbacks", help = "Report lambdas that fall back to interpreter").flag()
val script by argument(help = "one or more scripts to execute").optional()
val execute: String? by option(
"-x", "--execute", help = """
@@ -170,7 +168,7 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
override fun help(context: Context): String =
"""
The Lyng script language interpreter, language version is $LyngVersion.
The Lyng script language runtime, language version is $LyngVersion.
Please refer form more information to the project site:
https://gitea.sergeych.net/SergeychWorks/lyng
@@ -201,15 +199,12 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
launcher {
// there is no script name, it is a first argument instead:
processErrors {
val reporter = bytecodeFallbackReporter(bytecodeFallbacks)
val script = Compiler.compileWithResolution(
Source("<eval>", execute!!),
baseScope.currentImportProvider,
seedScope = baseScope,
bytecodeFallbackReporter = reporter
seedScope = baseScope
)
script.execute(baseScope)
flushBytecodeFallbacks(reporter)
}
}
}
@@ -220,7 +215,7 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
echoFormattedHelp()
} else {
baseScope.addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
launcher { executeFile(script!!, bytecodeFallbacks) }
launcher { executeFile(script!!) }
}
}
}
@@ -228,14 +223,14 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
}
}
fun executeFileWithArgs(fileName: String, args: List<String>, reportBytecodeFallbacks: Boolean = false) {
fun executeFileWithArgs(fileName: String, args: List<String>) {
runBlocking {
baseScopeDefer.await().addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
executeFile(fileName, reportBytecodeFallbacks)
executeFile(fileName)
}
}
suspend fun executeFile(fileName: String, reportBytecodeFallbacks: Boolean = false) {
suspend fun executeFile(fileName: String) {
var text = FileSystem.SYSTEM.source(fileName.toPath()).use { fileSource ->
fileSource.buffer().use { bs ->
bs.readUtf8()
@@ -248,15 +243,12 @@ suspend fun executeFile(fileName: String, reportBytecodeFallbacks: Boolean = fal
}
processErrors {
val scope = baseScopeDefer.await()
val reporter = bytecodeFallbackReporter(reportBytecodeFallbacks)
val script = Compiler.compileWithResolution(
Source(fileName, text),
scope.currentImportProvider,
seedScope = scope,
bytecodeFallbackReporter = reporter
seedScope = scope
)
script.execute(scope)
flushBytecodeFallbacks(reporter)
}
}
@@ -268,22 +260,3 @@ suspend fun processErrors(block: suspend () -> Unit) {
println("\nError executing the script:\n$e\n")
}
}
private fun bytecodeFallbackReporter(enabled: Boolean): ((Pos, String) -> Unit)? {
if (!enabled) return null
val reports = ArrayList<String>()
val reporter: (Pos, String) -> Unit = { pos, msg ->
reports.add("$pos: $msg")
}
return object : (Pos, String) -> Unit by reporter {
override fun invoke(pos: Pos, msg: String) = reporter(pos, msg)
override fun toString(): String = reports.joinToString("\n")
}
}
private fun flushBytecodeFallbacks(reporter: ((Pos, String) -> Unit)?) {
val text = reporter?.toString().orEmpty()
if (text.isBlank()) return
println("Bytecode lambda fallbacks:")
println(text)
}