Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/net/hollowcube/luau/LuaState.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,12 @@ static LuaState wrap(MemorySegment L) {
void setMemCat(int category);
long totalBytes(int category);

/// Throws, assumes that there is a value on the stack which becomes the thrown object.
@Contract("-> fail")
/// Creates a LuaError for the caller to throw
@CheckReturnValue
LuaError error();
@CheckReturnValue
LuaError error(String message);
@Contract("_, _ -> fail")
@CheckReturnValue
LuaError error(@PrintFormat String message, @Nullable Object... args);
@Contract("_, _ -> fail")
void typeError(int narg, String tname);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/hollowcube/luau/LuaStateImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ public long totalBytes(int category) {

@Override
public LuaError error() {
throw new LuaError(null);
return new LuaError(null);
}

@Override
Expand Down Expand Up @@ -994,7 +994,7 @@ public LuaCallbacks callbacks() {
@Override
public void checkAny(int argNum) {
if (lua_type(L, argNum) == LuaType.NONE.id()) return;
error("missing argument #%d", argNum);
throw error("missing argument #%d", argNum);
}

@Override
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/hollowcube/luau/require/RequireImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void pushRequireClosure(LuaState state, RequireResolver lrc) {

public static void registerModule(LuaState state, String path) {
if (path.isEmpty() || path.charAt(0) != '@')
state.error("path must begin with '@'");
throw state.error("path must begin with '@'");

state.findTable(REGISTRY_INDEX, REGISTERED_CACHE_TABLE_KEY, 1);

Expand Down Expand Up @@ -76,7 +76,7 @@ private static int requireImpl(LuaState state) {
int level = 0;
do {
if (lua_getinfo(state.L(), level++, DEBUG_WHAT, debug) == 0)
state.error("require is not supported in this context");
throw state.error("require is not supported in this context");
} while (lua_Debug.what(debug).get(ValueLayout.JAVA_BYTE, 0) != 'L');

final String source = lua_Debug.source(debug).getString(0);
Expand All @@ -90,7 +90,7 @@ private static int requireContinuationImpl(LuaState state, LuaStatus ignored) {
final String cacheKey = state.checkString(2);

if (numResults < 1)
state.error("module must return a single value");
throw state.error("module must return a single value");

// Cache the results
if (numResults == 1) {
Expand All @@ -117,7 +117,7 @@ private static int requireInternal(LuaState state, String requirerChunkName) {
state.top(1); // Discard extra arguments, we only use path

final RequireResolver lrc = (RequireResolver) state.toUserData(upvalueIndex(1));
if (lrc == null) state.error("unable to find require configuration");
if (lrc == null) throw state.error("unable to find require configuration");

final String path = state.checkString(1);
if (checkRegisteredModules(state, path))
Expand All @@ -128,7 +128,7 @@ private static int requireInternal(LuaState state, String requirerChunkName) {
case ResolvedRequire.Cached _ -> {
return 1;
}
case ResolvedRequire.ErrorReported(String error) -> state.error(error);
case ResolvedRequire.ErrorReported(String error) -> throw state.error(error);
case ResolvedRequire.ModuleRead(String chunkName, String loadName, String cacheKey) -> {
// (1) path, ..., cacheKey, chunkname, loadname
state.pushString(cacheKey);
Expand All @@ -147,7 +147,7 @@ private static int requireInternal(LuaState state, String requirerChunkName) {
int numResults = lrc.load(state, path, chunkName, loadName);
if (numResults == -1) {
if (state.top() != stackValues)
state.error("stack cannot be modified when require yields");
throw state.error("stack cannot be modified when require yields");
return state.yield(0);
}

Expand All @@ -165,7 +165,7 @@ record ErrorReported(String error) implements ResolvedRequire {}

private static ResolvedRequire resolveRequire(RequireResolver lrc, LuaState state, String requirerChunkName, String path) {
if (!lrc.isRequireAllowed(state, requirerChunkName))
state.error("require is not supported in this context");
throw state.error("require is not supported in this context");

final Navigator navigator = new Navigator(lrc, state, requirerChunkName);

Expand Down
7 changes: 4 additions & 3 deletions src/test/java/net/hollowcube/luau/TestLuaError.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ void stripDefaultErrorText() {

@Test
void errorShouldThrowLuaError(LuaState state) {
assertThrows(LuaError.class, state::error);
assertThrows(LuaError.class, () -> {
throw state.error();
});
}

@Test
Expand Down Expand Up @@ -98,8 +100,7 @@ void throwInUpcall(LuaState state, Arena arena) {
void throwInUpcallNoMessage(LuaState state, Arena arena) {
var func = LuaFunc.wrap(
L -> {
L.error();
return 0;
throw L.error();
},
"errfunc",
arena
Expand Down