diff options
author | P. J. McDermott <pj@pehjota.net> | 2023-02-24 22:53:53 (EST) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2023-02-24 22:53:53 (EST) |
commit | 4a07d2676fd06b20180b7bbac2aff06d7a0fbcde (patch) | |
tree | 979f85650cec97437c5a488ee2fde7cc366d9a01 | |
parent | a9d7345e8ea6a9cabad8d8e421a5e2cf358005ed (diff) | |
download | overworld-rpg-4a07d2676fd06b20180b7bbac2aff06d7a0fbcde.zip overworld-rpg-4a07d2676fd06b20180b7bbac2aff06d7a0fbcde.tar.gz overworld-rpg-4a07d2676fd06b20180b7bbac2aff06d7a0fbcde.tar.bz2 |
ffi: Eliminate call_arg_index static global var
-rw-r--r-- | src/resources/script.h | 1 | ||||
-rw-r--r-- | src/scripting/ffi.c | 62 | ||||
-rw-r--r-- | src/scripting/ffi.h | 1 |
3 files changed, 32 insertions, 32 deletions
diff --git a/src/resources/script.h b/src/resources/script.h index 12dac85..01a88f6 100644 --- a/src/resources/script.h +++ b/src/resources/script.h @@ -25,6 +25,7 @@ struct script { struct resource res; lua_State *lua_state; + int call_arg_index; }; struct script *script_get(const char *path); diff --git a/src/scripting/ffi.c b/src/scripting/ffi.c index 35ba6f2..16d33ef 100644 --- a/src/scripting/ffi.c +++ b/src/scripting/ffi.c @@ -39,7 +39,6 @@ static struct ffi_namespace root = { NULL, NULL, }; -static int call_arg_index; static void ffi_register_functions_in_namespace(lua_State *l, struct ffi_namespace *parent); @@ -149,9 +148,8 @@ ffi_handle_function(lua_State *l) { struct ffi_function *fn; - call_arg_index = 0; - fn = (struct ffi_function *) lua_touserdata(l, lua_upvalueindex(1)); + fn->call_arg_index = 0; fn->func(fn); return 0; @@ -165,97 +163,97 @@ void ffi_context_switch(struct script *script) int ffi_stack_get_bool(struct ffi_function *fn) { - if (lua_gettop(ctx->lua_state) < ++call_arg_index) { + if (lua_gettop(ctx->lua_state) < ++fn->call_arg_index) { warn("Bad argument #%d to %s:%s() (no value)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - if (!lua_isboolean(ctx->lua_state, call_arg_index)) { + if (!lua_isboolean(ctx->lua_state, fn->call_arg_index)) { warn("Bad argument #%d to %s:%s() (boolean expected)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - return lua_toboolean(ctx->lua_state, call_arg_index); + return lua_toboolean(ctx->lua_state, fn->call_arg_index); } int ffi_stack_get_int(struct ffi_function *fn) { #if LUA_VERSION_NUM <= 501 - if (lua_gettop(ctx->lua_state) < ++call_arg_index) { + if (lua_gettop(ctx->lua_state) < ++fn->call_arg_index) { warn("Bad argument #%d to %s:%s() (no value)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - return lua_tointeger(ctx->lua_state, call_arg_index); + return lua_tointeger(ctx->lua_state, fn->call_arg_index); #elif LUA_VERSION_NUM == 502 int v; int is_int; - if (lua_gettop(ctx->lua_state) < ++call_arg_index) { + if (lua_gettop(ctx->lua_state) < ++fn->call_arg_index) { warn("Bad argument #%d to %s:%s() (no value)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - v = lua_tointegerx(ctx->lua_state, call_arg_index, &is_int); + v = lua_tointegerx(ctx->lua_state, fn->call_arg_index, &is_int); if (!is_int) { warn("Bad argument #%d to %s:%s() (integer expected)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } return v; #elif LUA_VERSION_NUM >= 503 - if (lua_gettop(ctx->lua_state) < ++call_arg_index) { + if (lua_gettop(ctx->lua_state) < ++fn->call_arg_index) { warn("Bad argument #%d to %s:%s() (no value)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - if (!lua_isinteger(ctx->lua_state, call_arg_index)) { + if (!lua_isinteger(ctx->lua_state, fn->call_arg_index)) { warn("Bad argument #%d to %s:%s() (integer expected)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - return lua_tointeger(ctx->lua_state, call_arg_index); + return lua_tointeger(ctx->lua_state, fn->call_arg_index); #endif } double ffi_stack_get_float(struct ffi_function *fn) { - if (lua_gettop(ctx->lua_state) < ++call_arg_index) { + if (lua_gettop(ctx->lua_state) < ++fn->call_arg_index) { warn("Bad argument #%d to %s:%s() (no value)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - if (!lua_isnumber(ctx->lua_state, call_arg_index)) { + if (!lua_isnumber(ctx->lua_state, fn->call_arg_index)) { warn("Bad argument #%d to %s:%s() (number expected)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - return lua_tonumber(ctx->lua_state, call_arg_index); + return lua_tonumber(ctx->lua_state, fn->call_arg_index); } const char * ffi_stack_get_string(struct ffi_function *fn) { - if (lua_gettop(ctx->lua_state) < ++call_arg_index) { + if (lua_gettop(ctx->lua_state) < ++fn->call_arg_index) { warn("Bad argument #%d to %s:%s() (no value)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - if (!lua_isstring(ctx->lua_state, call_arg_index)) { + if (!lua_isstring(ctx->lua_state, fn->call_arg_index)) { warn("Bad argument #%d to %s:%s() (string expected)", - call_arg_index, fn->ns->full_name, fn->name); + fn->call_arg_index, fn->ns->full_name, fn->name); return 0; } - return lua_tostring(ctx->lua_state, call_arg_index); + return lua_tostring(ctx->lua_state, fn->call_arg_index); } void ffi_prepare_call(const char *func) { lua_getglobal(ctx->lua_state, func); - call_arg_index = 0; + ctx->call_arg_index = 0; } void @@ -285,7 +283,7 @@ ffi_stack_set_string(const char *v) void ffi_call(void) { - if (lua_pcall(ctx->lua_state, call_arg_index, 0, 0) != LUA_OK) { + if (lua_pcall(ctx->lua_state, ctx->call_arg_index, 0, 0) != LUA_OK) { err(1, "Error calling function: %s\n", lua_tostring(ctx->lua_state, -1)); } diff --git a/src/scripting/ffi.h b/src/scripting/ffi.h index fa3eb56..8749039 100644 --- a/src/scripting/ffi.h +++ b/src/scripting/ffi.h @@ -24,6 +24,7 @@ struct ffi_function { const char *name; struct ffi_namespace *ns; + int call_arg_index; void (*func)(struct ffi_function *); struct ffi_function *next; }; |