Fixed issue: #94

This commit is contained in:
UnlegitDqrk
2026-03-01 19:36:19 +01:00
parent 364dbecb17
commit 4c2add3832
3 changed files with 25 additions and 8 deletions

View File

@@ -591,19 +591,19 @@ public class LuaClosure extends LuaFunction {
* Run the error hook if there is one * Run the error hook if there is one
* @param msg the message to use in error hook processing. * @param msg the message to use in error hook processing.
* */ * */
String errorHook(String msg, int level) { LuaValue errorHook(LuaValue msgobj, String msg, int level) {
if (globals == null ) return msg; if (globals == null ) return LuaValue.valueOf(msg);
final LuaThread r = globals.running; final LuaThread r = globals.running;
if (r.errorfunc == null) if (r.errorfunc == null)
return globals.debuglib != null? return LuaValue.valueOf(globals.debuglib != null?
msg + "\n" + globals.debuglib.traceback(level): msg + "\n" + globals.debuglib.traceback(level):
msg; msg);
final LuaValue e = r.errorfunc; final LuaValue e = r.errorfunc;
r.errorfunc = null; r.errorfunc = null;
try { try {
return e.call( LuaValue.valueOf(msg) ).tojstring(); return e.call(msgobj != null ? msgobj : LuaValue.NIL);
} catch ( Throwable t ) { } catch ( Throwable t ) {
return "error in error handling"; return LuaValue.valueOf("error in error handling");
} finally { } finally {
r.errorfunc = e; r.errorfunc = e;
} }
@@ -628,7 +628,9 @@ public class LuaClosure extends LuaFunction {
} }
} }
le.fileline = file + ":" + line; le.fileline = file + ":" + line;
le.traceback = errorHook(le.getMessage(), le.level); LuaValue error = errorHook(le.getMessageObject(), le.getMessage(), le.level);
le.setMessageObject(error);
le.traceback = error != null ? error.tojstring() : null;
} }
private UpValue findupval(LuaValue[] stack, short idx, UpValue[] openups) { private UpValue findupval(LuaValue[] stack, short idx, UpValue[] openups) {

View File

@@ -76,6 +76,10 @@ public class LuaError extends RuntimeException {
return m != null ? LuaValue.valueOf(m): null; return m != null ? LuaValue.valueOf(m): null;
} }
public void setMessageObject(LuaValue messageObject) {
this.object = messageObject;
}
/** Construct LuaError when a program exception occurs. /** Construct LuaError when a program exception occurs.
* <p> * <p>
* All errors generated from lua code should throw LuaError(String) instead. * All errors generated from lua code should throw LuaError(String) instead.

View File

@@ -591,6 +591,17 @@ public class FragmentsTest extends TestSuite {
runFragment(LuaValue.varargsOf(LuaValue.valueOf("boolean"), LuaValue.TRUE), runFragment(LuaValue.varargsOf(LuaValue.valueOf("boolean"), LuaValue.TRUE),
"a,b = pcall(error, true); return type(b), b\n"); "a,b = pcall(error, true); return type(b), b\n");
} }
public void testXpcallHandlerResultReplacesStringError() {
runFragment(LuaValue.varargsOf(LuaValue.valueOf("table"), LuaValue.valueOf(1)),
"local ok, err = xpcall(function() error('oh no') end, function(e) return { marker = 1, original = e } end)\n" +
"return type(err), err.marker\n");
}
public void testXpcallHandlerGetsOriginalErrorObject() {
runFragment(LuaValue.varargsOf(LuaValue.valueOf("table"), LuaValue.valueOf(1)),
"local source = { marker = 1 }\n" +
"local ok, err = xpcall(function() error(source) end, function(e) return e end)\n" +
"return type(err), err.marker\n");
}
public void testBalancedMatchOnEmptyString() { public void testBalancedMatchOnEmptyString() {
runFragment(LuaValue.NIL, "return (\"\"):match(\"%b''\")\n"); runFragment(LuaValue.NIL, "return (\"\"):match(\"%b''\")\n");
} }