Remove interpreter paths and enforce bytecode-only execution
This commit is contained in:
1 parent
e73fe0d3c5
commit
34678068ac
45 files changed
+434
-1878
No files matched your search
@@ -23,6 +23,8 @@ package net.sergeych.lyng.io.fs
|
||||
|
||||
import net.sergeych.lyng.ModuleScope
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.ScopeFacade
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lyng.pacman.ImportManager
|
||||
@@ -437,7 +439,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
moduleName = module.packageName
|
||||
) {
|
||||
fsGuard {
|
||||
val chunkIt = thisObj.invokeInstanceMethod(this, "readUtf8Chunks")
|
||||
val chunkIt = thisObj.invokeInstanceMethod(requireScope(), "readUtf8Chunks")
|
||||
ObjFsLinesIterator(chunkIt)
|
||||
}
|
||||
}
|
||||
@@ -463,7 +465,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
|
||||
// --- Helper classes and utilities ---
|
||||
|
||||
private fun parsePathArg(scope: Scope, self: ObjPath, arg: Obj): LyngPath {
|
||||
private fun parsePathArg(scope: ScopeFacade, self: ObjPath, arg: Obj): LyngPath {
|
||||
return when (arg) {
|
||||
is ObjString -> arg.value.toPath()
|
||||
is ObjPath -> arg.path
|
||||
@@ -472,11 +474,11 @@ private fun parsePathArg(scope: Scope, self: ObjPath, arg: Obj): LyngPath {
|
||||
}
|
||||
|
||||
// Map Fs access denials to Lyng runtime exceptions for script-friendly errors
|
||||
private suspend inline fun Scope.fsGuard(crossinline block: suspend () -> Obj): Obj {
|
||||
private suspend inline fun ScopeFacade.fsGuard(crossinline block: suspend () -> Obj): Obj {
|
||||
return try {
|
||||
block()
|
||||
} catch (e: AccessDeniedException) {
|
||||
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "access denied"))
|
||||
raiseError(ObjIllegalOperationException(requireScope(), e.reasonDetail ?: "access denied"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -668,16 +670,17 @@ class ObjFsLinesIterator(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun ensureBufferFilled(scope: Scope) {
|
||||
private suspend fun ensureBufferFilled(scope: ScopeFacade) {
|
||||
if (buffer.contains('\n') || exhausted) return
|
||||
val actualScope = scope.requireScope()
|
||||
// Pull next chunk from the underlying iterator
|
||||
val it = chunksIterator.invokeInstanceMethod(scope, "iterator")
|
||||
val hasNext = it.invokeInstanceMethod(scope, "hasNext").toBool()
|
||||
val it = chunksIterator.invokeInstanceMethod(actualScope, "iterator")
|
||||
val hasNext = it.invokeInstanceMethod(actualScope, "hasNext").toBool()
|
||||
if (!hasNext) {
|
||||
exhausted = true
|
||||
return
|
||||
}
|
||||
val next = it.invokeInstanceMethod(scope, "next")
|
||||
val next = it.invokeInstanceMethod(actualScope, "next")
|
||||
buffer += next.toString()
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ package net.sergeych.lyng.io.process
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import net.sergeych.lyng.ModuleScope
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.ScopeFacade
|
||||
import net.sergeych.lyng.requireScope
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lyng.pacman.ImportManager
|
||||
@@ -204,20 +206,21 @@ class ObjRunningProcess(
|
||||
override fun toString(): String = "RunningProcess($process)"
|
||||
}
|
||||
|
||||
private suspend inline fun Scope.processGuard(crossinline block: suspend () -> Obj): Obj {
|
||||
private suspend inline fun ScopeFacade.processGuard(crossinline block: suspend () -> Obj): Obj {
|
||||
return try {
|
||||
block()
|
||||
} catch (e: ProcessAccessDeniedException) {
|
||||
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "process access denied"))
|
||||
raiseError(ObjIllegalOperationException(requireScope(), e.reasonDetail ?: "process access denied"))
|
||||
} catch (e: Exception) {
|
||||
raiseError(ObjIllegalOperationException(this, e.message ?: "process error"))
|
||||
raiseError(ObjIllegalOperationException(requireScope(), e.message ?: "process error"))
|
||||
}
|
||||
}
|
||||
|
||||
private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
|
||||
val producer = ObjNativeCallable {
|
||||
val builder = (this as? net.sergeych.lyng.BytecodeClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
|
||||
?: this.thisObj as? ObjFlowBuilder
|
||||
private fun Flow<String>.toLyngFlow(flowScope: ScopeFacade): ObjFlow {
|
||||
val producer = net.sergeych.lyng.obj.ObjExternCallable.fromBridge {
|
||||
val scope = requireScope()
|
||||
val builder = (scope as? net.sergeych.lyng.BytecodeClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
|
||||
?: scope.thisObj as? ObjFlowBuilder
|
||||
|
||||
this@toLyngFlow.collect {
|
||||
try {
|
||||
@@ -229,5 +232,5 @@ private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
return ObjFlow(producer, flowScope)
|
||||
return ObjFlow(producer, flowScope.requireScope())
|
||||
}
|
||||
Reference in new issue
Block a user