Skip to content

Commit

Permalink
Use lua_error return value less;
Browse files Browse the repository at this point in the history
If a hypothetical Lua implementation declared these functions as
noreturn for better codegen, it might also want to change the return
type to void for better compiler compatibility.
  • Loading branch information
bjornbytes committed May 12, 2024
1 parent a7d9df1 commit d9a2166
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
14 changes: 9 additions & 5 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ int luax_typeerror(lua_State* L, int index, const char* expected) {
name = luaL_typename(L, index);
}
const char* message = lua_pushfstring(L, "%s expected, got %s", expected, name);
return luaL_argerror(L, index, message);
luaL_argerror(L, index, message);
return 0;
}

// Registers the userdata on the top of the stack in the registry.
Expand Down Expand Up @@ -273,10 +274,12 @@ int _luax_checkenum(lua_State* L, int index, const StringEntry* map, const char*
}

if (index > 0) {
return luaL_argerror(L, index, lua_pushfstring(L, "invalid %s '%s'", label, string));
luaL_argerror(L, index, lua_pushfstring(L, "invalid %s '%s'", label, string));
} else {
return luaL_error(L, "invalid %s '%s'", label, string);
luaL_error(L, "invalid %s '%s'", label, string);
}

return 0;
}

void luax_registerloader(lua_State* L, lua_CFunction loader, int index) {
Expand Down Expand Up @@ -604,8 +607,9 @@ int luax_readmesh(lua_State* L, int index, float** vertices, uint32_t* vertexCou
return index + 1;
}

return luaL_argerror(L, index, "table, ModelData, Model, or Mesh expected");
luaL_argerror(L, index, "table, ModelData, Model, or Mesh expected");
#else
return luaL_argerror(L, index, "table or ModelData expected");
luaL_argerror(L, index, "table or ModelData expected");
#endif
return 0;
}
6 changes: 3 additions & 3 deletions src/api/l_filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ static int luax_loadfile(lua_State* L, const char* path, const char* debug, cons
int status = luax_loadbufferx(L, buffer, size, debug, mode);
lovrFree(buffer);
switch (status) {
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1));
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1));
case LUA_ERRMEM: return luaL_error(L, "Memory allocation error: %s", lua_tostring(L, -1)), 0;
case LUA_ERRSYNTAX: return luaL_error(L, "Syntax error: %s", lua_tostring(L, -1)), 0;
default: return 1;
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ static int l_lovrFilesystemGetSize(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
uint64_t size = lovrFilesystemGetSize(path);
if (size == ~0ull) {
return luaL_error(L, "File does not exist");
return luaL_error(L, "File does not exist"), 0;
}
lua_pushinteger(L, size);
return 1;
Expand Down
8 changes: 5 additions & 3 deletions src/api/l_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ static uint32_t luax_checkdatatype(lua_State* L, int index) {
const char* string = luaL_checklstring(L, index, &length);

if (length < 3) {
return luaL_error(L, "invalid DataType '%s'", string), 0;
luaL_error(L, "invalid DataType '%s'", string);
return 0;
}

// Deprecated: plurals are allowed and ignored
Expand Down Expand Up @@ -262,7 +263,8 @@ static uint32_t luax_checkdatatype(lua_State* L, int index) {
}
}

return luaL_error(L, "invalid DataType '%s'", string), 0;
luaL_error(L, "invalid DataType '%s'", string);
return 0;
}

uint32_t luax_checkcomparemode(lua_State* L, int index) {
Expand Down Expand Up @@ -854,7 +856,7 @@ static int l_lovrGraphicsNewTexture(lua_State* L) {
}
break;
case LUA_TNIL: break;
default: return luaL_error(L, "Expected Texture usage to be a string, table, or nil");
default: luaL_error(L, "Expected Texture usage to be a string, table, or nil"); return 0;
}
lua_pop(L, 1);

Expand Down
3 changes: 2 additions & 1 deletion src/api/l_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ static int l_lovrLightUserdataOp(lua_State* L) {
return 1;
}
lua_pop(L, 1);
return luaL_error(L, "Unsupported lightuserdata operator %q", lua_tostring(L, lua_upvalueindex(1)));
luaL_error(L, "Unsupported lightuserdata operator %q", lua_tostring(L, lua_upvalueindex(1)));
return 0;
}

lua_rawgeti(L, LUA_REGISTRYINDEX, metaref[type]);
Expand Down
2 changes: 1 addition & 1 deletion src/api/l_math_randomGenerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int l_lovrRandomGeneratorSetState(lua_State* L) {
RandomGenerator* generator = luax_checktype(L, 1, RandomGenerator);
const char* state = luaL_checklstring(L, 2, NULL);
if (lovrRandomGeneratorSetState(generator, state)) {
return luaL_error(L, "invalid random state %s", state);
luaL_error(L, "invalid random state %s", state);
}
return 0;
}
Expand Down
30 changes: 20 additions & 10 deletions src/api/l_math_vectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ static int l_lovrVec2__newindex(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to assign property %s of vec2 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to assign property %s of vec2 (invalid property)", lua_tostring(L, -1));
return 0;
}

static int l_lovrVec2__index(lua_State* L) {
Expand Down Expand Up @@ -473,7 +474,8 @@ static int l_lovrVec2__index(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to index field %s of vec2 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to index field %s of vec2 (invalid property)", lua_tostring(L, -1));
return 0;
}

int l_lovrVec2__metaindex(lua_State* L) {
Expand Down Expand Up @@ -811,7 +813,8 @@ static int l_lovrVec3__newindex(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to assign property %s of vec3 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to assign property %s of vec3 (invalid property)", lua_tostring(L, -1));
return 0;
}

static int l_lovrVec3__index(lua_State* L) {
Expand Down Expand Up @@ -865,7 +868,8 @@ static int l_lovrVec3__index(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to index field %s of vec3 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to index field %s of vec3 (invalid property)", lua_tostring(L, -1));
return 0;
}

int l_lovrVec3__metaindex(lua_State* L) {
Expand Down Expand Up @@ -1207,7 +1211,8 @@ static int l_lovrVec4__newindex(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to assign property %s of vec4 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to assign property %s of vec4 (invalid property)", lua_tostring(L, -1));
return 0;
}

static int l_lovrVec4__index(lua_State* L) {
Expand Down Expand Up @@ -1261,7 +1266,8 @@ static int l_lovrVec4__index(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to index field %s of vec4 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to index field %s of vec4 (invalid property)", lua_tostring(L, -1));
return 0;
}

int l_lovrVec4__metaindex(lua_State* L) {
Expand Down Expand Up @@ -1522,7 +1528,8 @@ static int l_lovrQuat__newindex(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to assign property %s of quat (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to assign property %s of quat (invalid property)", lua_tostring(L, -1));
return 0;
}

static int l_lovrQuat__index(lua_State* L) {
Expand Down Expand Up @@ -1557,7 +1564,8 @@ static int l_lovrQuat__index(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to index field %s of quat (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to index field %s of quat (invalid property)", lua_tostring(L, -1));
return 0;
}

int l_lovrQuat__metaindex(lua_State* L) {
Expand Down Expand Up @@ -1967,7 +1975,8 @@ static int l_lovrMat4__newindex(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to assign property %s of mat4 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to assign property %s of mat4 (invalid property)", lua_tostring(L, -1));
return 0;
}

static int l_lovrMat4__index(lua_State* L) {
Expand All @@ -1993,7 +2002,8 @@ static int l_lovrMat4__index(lua_State* L) {
lua_getglobal(L, "tostring");
lua_pushvalue(L, 2);
lua_call(L, 1, 1);
return luaL_error(L, "attempt to index field %s of mat4 (invalid property)", lua_tostring(L, -1));
luaL_error(L, "attempt to index field %s of mat4 (invalid property)", lua_tostring(L, -1));
return 0;
}

int l_lovrMat4__metaindex(lua_State* L) {
Expand Down
6 changes: 4 additions & 2 deletions src/api/l_physics.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ static int l_lovrPhysicsNewWorld(lua_State* L) {
if (lua_isstring(L, -1)) {
info.tags[i] = lua_tostring(L, -1);
} else {
return luaL_error(L, "World tags must be a table of strings");
luaL_error(L, "World tags must be a table of strings");
return 0;
}
lua_pop(L, 1);
}
Expand Down Expand Up @@ -137,7 +138,8 @@ static int l_lovrPhysicsNewWorld(lua_State* L) {
if (lua_isstring(L, -1)) {
info.tags[i] = lua_tostring(L, -1);
} else {
return luaL_error(L, "World tags must be a table of strings");
luaL_error(L, "World tags must be a table of strings");
return 0;
}
lua_pop(L, 1);
}
Expand Down

0 comments on commit d9a2166

Please sign in to comment.