From 4a07d2676fd06b20180b7bbac2aff06d7a0fbcde Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 24 Feb 2023 22:53:53 -0500 Subject: ffi: Eliminate call_arg_index static global var --- (limited to 'src/scripting') 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; }; -- cgit v0.9.1