Skip to content

Commit 7560495

Browse files
leinlintangzx
authored andcommitted
lua_setglobal
1 parent ab08dd2 commit 7560495

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

emmy_core/api/lua_api.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ void* LoadAPI(const char* name) {
5151
return handler;
5252
}
5353
#endif
54+
int LUA_REGISTRYINDEX = 0;
55+
int LUA_GLOBALSINDEX = 0;
5456

5557
IMP_LUA_API(lua_gettop);
5658
IMP_LUA_API(lua_settop);
@@ -99,6 +101,7 @@ IMP_LUA_API_E(lua_remove);
99101
IMP_LUA_API_E(lua_tointegerx);
100102
IMP_LUA_API_E(lua_tonumberx);
101103
IMP_LUA_API_E(lua_getglobal);
104+
IMP_LUA_API_E(lua_setglobal);
102105
IMP_LUA_API_E(lua_callk);
103106
IMP_LUA_API_E(lua_pcallk);
104107
IMP_LUA_API_E(luaL_setfuncs);
@@ -129,9 +132,20 @@ lua_Number lua_tonumber(lua_State* L, int idx) {
129132

130133
int lua_getglobal(lua_State* L, const char* name) {
131134
if (luaVersion == LuaVersion::LUA_51) {
132-
return lua_getfield(L, -10002, name);
135+
return lua_getfield(L, LUA_GLOBALSINDEX, name);
136+
}
137+
else {
138+
return e_lua_getglobal(L, name);
139+
}
140+
}
141+
142+
void lua_setglobal(lua_State* L, const char* name) {
143+
if (luaVersion == LuaVersion::LUA_51) {
144+
return lua_setfield(L, LUA_GLOBALSINDEX, name);
145+
}
146+
else {
147+
return e_lua_setglobal(L, name);
133148
}
134-
return e_lua_getglobal(L, name);
135149
}
136150

137151
void lua_call(lua_State* L, int nargs, int nresults) {
@@ -188,8 +202,6 @@ void lua_remove(lua_State *L, int idx) {
188202
}
189203
}
190204

191-
int LUA_REGISTRYINDEX = 0;
192-
193205
extern "C" bool SetupLuaAPI() {
194206
LOAD_LUA_API(lua_gettop);
195207
LOAD_LUA_API(lua_settop);
@@ -239,6 +251,7 @@ extern "C" bool SetupLuaAPI() {
239251
LOAD_LUA_API_E(lua_tointegerx);
240252
LOAD_LUA_API_E(lua_tonumberx);
241253
LOAD_LUA_API_E(lua_getglobal);
254+
LOAD_LUA_API_E(lua_setglobal);
242255
LOAD_LUA_API_E(lua_callk);
243256
LOAD_LUA_API_E(lua_pcallk);
244257
LOAD_LUA_API_E(luaL_setfuncs);
@@ -249,15 +262,18 @@ extern "C" bool SetupLuaAPI() {
249262
if (e_lua_rotate) {
250263
luaVersion = LuaVersion::LUA_53;
251264
LUA_REGISTRYINDEX = -1001000;
265+
LUA_GLOBALSINDEX = 2;
252266
}
253267
else if (e_lua_callk) {
254268
luaVersion = LuaVersion::LUA_52;
255269
//todo
256270
LUA_REGISTRYINDEX = -1001000;
271+
LUA_GLOBALSINDEX = 2;
257272
}
258273
else {
259274
luaVersion = LuaVersion::LUA_51;
260275
LUA_REGISTRYINDEX = -10000;
276+
LUA_GLOBALSINDEX = -10002;
261277
}
262278
printf("[EMMY]lua version: %d\n", luaVersion);
263279
return true;

emmy_core/api/lua_api.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ typedef lua_Number(*dll_e_lua_tonumberx) (lua_State *L, int idx, int *isnum);
262262
DEF_LUA_API_E(lua_tonumberx);
263263
typedef int (*dll_e_lua_getglobal)(lua_State* L, const char* name);
264264
DEF_LUA_API_E(lua_getglobal);
265+
typedef void (*dll_e_lua_setglobal)(lua_State* L, const char* name);
266+
DEF_LUA_API_E(lua_setglobal);
265267
typedef void (*dll_e_lua_callk)(lua_State* L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k);
266268
DEF_LUA_API_E(lua_callk);
267269
typedef int (*dll_e_lua_pcallk)(lua_State* L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k);
@@ -278,9 +280,10 @@ lua_Integer lua_tointeger(lua_State* L, int idx);
278280
lua_Number lua_tonumber(lua_State* L, int idx);
279281
int lua_setfenv(lua_State* L, int idx);
280282
int lua_getglobal(lua_State* L, const char* name);
283+
void lua_setglobal(lua_State* L, const char* name);
281284
int lua_pcall(lua_State* L, int nargs, int nresults, int errfunc);
282285
int lua_upvalueindex(int i);
283286
int lua_absindex(lua_State *L, int idx);
284287
void lua_call(lua_State* L, int nargs, int nresults);
285288
void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup);
286-
void lua_remove(lua_State *L, int idx);
289+
void lua_remove(lua_State *L, int idx);

emmy_core/emmy_helper_lib.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,9 @@ int emmyHelperInit(lua_State* L) {
161161
// 1: table
162162
int luaopen_emmy_helper(lua_State* L) {
163163
initialized = false;
164-
lua_getglobal(L, "_G");
165164

166-
lua_pushstring(L, "emmyHelperInit");
167165
lua_pushcfunction(L, emmyHelperInit);
168-
lua_rawset(L, -3);
169-
170-
lua_pop(L, 1);
166+
lua_setglobal(L, "emmyHelperInit");
171167

172168
return 0;
173169
}

0 commit comments

Comments
 (0)