refs #22 more docs, fixed EH bug

+shortcut throw "message"
This commit is contained in:
sergeych committed 2025-06-13 01:30:26 +04:00
1 parent 5ed8b2f123
commit c3bf536bab
6 files changed
+167 -9

No files matched your search

@@ -725,7 +725,9 @@ class Compiler(
private fun parseThrowStatement(cc: CompilerContext): Statement {
val throwStatement = parseStatement(cc) ?: throw ScriptError(cc.currentPos(), "throw object expected")
return statement {
val errorObject = throwStatement.execute(this)
var errorObject = throwStatement.execute(this)
if( errorObject is ObjString )
errorObject = ObjException(this, errorObject.value)
if( errorObject is ObjException )
raiseError(errorObject)
else raiseError("this is not an exception object: $errorObject")
@@ -816,11 +818,10 @@ class Compiler(
for (exceptionClassName in cdata.classNames) {
val exObj = ObjException.getErrorClass(exceptionClassName)
?: raiseSymbolNotFound("error clas not exists: $exceptionClassName")
println("exObj: $exObj")
println("objException: ${objException.objClass}")
if( objException.isInstanceOf(exObj) )
if( objException.isInstanceOf(exObj) ) {
exceptionObject = objException
break
}
}
if( exceptionObject != null ) {
val catchContext = this.copy(pos = cdata.catchVar.pos)
@@ -347,13 +347,16 @@ open class ObjException(exceptionClass: ExceptionClass, val context: Context, va
class ExceptionClass(val name: String,vararg parents: ObjClass) : ObjClass(name, *parents) {
override suspend fun callOn(context: Context): Obj {
return ObjException(this, context, name).apply {
println(">>>> "+this)
}
val message = context.args.getOrNull(0)?.toString() ?: name
return ObjException(this, context, message)
}
override fun toString(): String = "ExceptionClass[$name]@${hashCode().encodeToHex()}"
}
val Root = ExceptionClass("Throwable")
val Root = ExceptionClass("Throwable").apply {
addConst("message", statement {
(thisObj as ObjException).message.toObj()
})
}
private val op = ProtectedOp()
private val existingErrorClasses = mutableMapOf<String, ExceptionClass>()
@@ -1813,4 +1813,24 @@ class ScriptTest {
""".trimIndent())
}
@Test
fun testAccessEHData() = runTest {
eval("""
val x = IllegalArgumentException("test")
val m = try {
throw x
null
}
catch(e) {
println(e)
println(e::class)
println(e.message)
println("--------------")
e.message
}
println(m)
assert( m == "test" )
""".trimIndent())
}
}
+5
View File
@@ -268,4 +268,9 @@ class BookTest {
fun testArgumentBooks() = runTest {
runDocTests("../docs/declaring_arguments.md")
}
@Test
fun testExceptionsBooks() = runTest {
runDocTests("../docs/exceptions_handling.md")
}
}