user exceptions to kotlin fixes

This commit is contained in:
sergeych committed 2026-01-08 09:21:25 +01:00
1 parent 1d089db9ff
commit c12804a806
9 files changed
+262 -13

No files matched your search

@@ -20,6 +20,7 @@ package net.sergeych.lyng
import net.sergeych.lyng.Compiler.Companion.compile
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.pacman.ImportProvider
/**
@@ -3840,4 +3841,6 @@ class Compiler(
}
suspend fun eval(code: String) = compile(code).execute()
suspend fun evalNamed(name: String, code: String, importManager: ImportManager = Script.defaultImportManager) =
compile(Source(name,code), importManager).execute()
@@ -67,6 +67,10 @@ open class Obj {
someClass == rootObjectType ||
(someClass is ObjClass && objClass.allImplementingNames.contains(someClass.className))
fun isInstanceOf(className: String) =
objClass.mro.any { it.className == className } ||
objClass.allImplementingNames.contains(className)
suspend fun invokeInstanceMethod(scope: Scope, name: String, vararg args: Obj): Obj =
invokeInstanceMethod(scope, name, Arguments(args.toList()))
@@ -159,20 +159,17 @@ open class ObjException(
}
val Root = ExceptionClass("Exception").apply {
instanceConstructor = statement {
val msg = args.getOrNull(0) ?: ObjString("Exception")
if (thisObj is ObjInstance) {
(thisObj as ObjInstance).instanceScope.addItem("Exception::message", false, msg)
}
ObjVoid
}
instanceInitializers.add(statement {
if (thisObj is ObjInstance) {
val msg = get("message")?.value ?: ObjString("Exception")
(thisObj as ObjInstance).instanceScope.addItem("Exception::message", false, msg)
val stack = captureStackTrace(this)
(thisObj as ObjInstance).instanceScope.addItem("Exception::stackTrace", false, stack)
}
ObjVoid
})
instanceConstructor = statement { ObjVoid }
addConstDoc(
name = "message",
value = statement {
@@ -331,3 +328,43 @@ class ObjUnsetException(scope: Scope, message: String = "property is unset (not
class ObjNotImplementedException(scope: Scope, message: String = "not implemented") :
ObjException("NotImplementedException", scope, message)
/**
* Check if the object is an instance of Lyng Exception class.
*/
fun Obj.isLyngException(): Boolean = isInstanceOf("Exception")
/**
* Get the exception message.
*/
suspend fun Obj.getLyngExceptionMessage(scope: Scope): String =
invokeInstanceMethod(scope, "message").toString(scope).value
/**
* Get the exception stack trace.
*/
suspend fun Obj.getLyngExceptionStackTrace(scope: Scope): ObjList =
invokeInstanceMethod(scope, "stackTrace").cast(scope)
/**
* Get the exception extra data.
*/
suspend fun Obj.getLyngExceptionExtraData(scope: Scope): Obj =
invokeInstanceMethod(scope, "extraData")
/**
* Get the exception as a formatted string with the primary throw site.
*/
suspend fun Obj.getLyngExceptionString(scope: Scope): String =
invokeInstanceMethod(scope, "toString").toString(scope).value
/**
* Rethrow this object as a Kotlin [ExecutionError] if it's an exception.
*/
suspend fun Obj.raiseAsExecutionError(scope: Scope?=null): Nothing {
if (this is ObjException) raise()
val sc = scope ?: Script.newScope()
val msg = getLyngExceptionMessage(sc)
val pos = (this as? ObjInstance)?.instanceScope?.pos ?: Pos.builtIn
throw ExecutionError(this, pos, msg)
}
@@ -294,10 +294,12 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
// }
val serializingVars: Map<String, ObjRecord> by lazy {
val metaParams = objClass.constructorMeta?.params?.map { it.name }?.toSet() ?: emptySet()
instanceScope.objects.filter {
it.value.type.serializable &&
it.value.type == ObjRecord.Type.Field &&
it.value.isMutable
it.value.isMutable &&
!metaParams.contains(it.key)
}
}