Rewrite some of lcl.c so it is branchless

This commit is contained in:
Jeremy Baxter 2023-07-09 21:21:35 +12:00
parent 07164e0cf2
commit 1696efaca1

32
lcl.c
View file

@ -134,19 +134,16 @@ end
static int static int
cl_panic(lua_State *L) cl_panic(lua_State *L)
{ {
int code; ubyte code;
ubyte isinteger;
if (lua_isinteger(L, 1)) { /* if an exit code was given... */ isinteger = lua_isinteger(L, 1);
code = lua_tointeger(L, 1); /* get code */
luaL_checktype(L, 2, LUA_TTABLE); /* get exit code */
luaL_checkstring(L, 3); code = lua_tointeger(L, 1) * isinteger + 1 * !isinteger;
fmesg(L, stderr, 1); luaL_checktype(L, 1 + isinteger, LUA_TTABLE);
} else { luaL_checkstring(L, 2 + isinteger);
code = 1; fmesg(L, stderr, isinteger);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkstring(L, 2);
fmesg(L, stderr, 0);
}
/* format using string.format */ /* format using string.format */
lua_getglobal(L, "os"); lua_getglobal(L, "os");
@ -255,11 +252,8 @@ cl_parseopts(lua_State *L)
lua_pushvalue(L, 3); lua_pushvalue(L, 3);
/* first function parameter: opt */ /* first function parameter: opt */
if (ch == '?') (ch == '?') ? lua_pushnil(L) : /* in case of unknown option */
lua_pushnil(L); /* in case of unknown option */ (ch == ':') ? lua_pushliteral(L, "*") : /* in case of missing option argument */
else if (ch == ':')
lua_pushliteral(L, "*"); /* in case of missing option argument */
else
lua_pushstring(L, s); lua_pushstring(L, s);
/* second function parameter: optarg */ /* second function parameter: optarg */
@ -268,9 +262,9 @@ cl_parseopts(lua_State *L)
lua_pushinteger(L, optind); lua_pushinteger(L, optind);
/* fourth function parameter: opterror /* fourth function parameter: opterror
* (only non-nil on error) */ * (only non-nil on error) */
if (optopt == '?') /* error was not encountered */ if (optopt == '?') { /* error was not encountered */
lua_pushnil(L); lua_pushnil(L);
else { } else {
loptopt[0] = optopt; loptopt[0] = optopt;
loptopt[1] = 0; loptopt[1] = 0;
lua_pushstring(L, loptopt); lua_pushstring(L, loptopt);