Use of pushglobaltable and setfenv in Lua5.3
2016-11-17 18:04
459 查看
Use
of pushglobaltable and setfenv in Lua5.3
![](http://static.adzerk.net/Advertisers/3ce23fa868ea401dac7e2fcfda5ec312.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjo0NzE0OTMsImNoIjoxMTc4LCJjayI6e30sImNyIjoxNjI3NzY4LCJkaSI6ImEwNmY0OWVmYzgwYTRiZGZhMmNiMjA2ODliNDlmMmY3IiwiZG0iOjEsImZjIjoxOTI0NDQ3LCJmbCI6MjE0MjMxMiwiaXAiOiIxMTMuMTA2LjEwNi45OCIsImt3IjoiYysrLGx1YSIsIm53IjoyMiwicGMiOjAsImVjIjowLCJwciI6MTYwNCwicnQiOjEsInJmIjoiaHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS8iLCJzdCI6ODI3NywidWsiOiJ1ZTEtMWYyZjI3NWY5MTlkNDE0MDg0MTZlN2IyMTFmY2FmYTciLCJ6biI6NDMsInRzIjoxNDc5Mzc2OTc4MzA0LCJiZiI6dHJ1ZSwicG4iOiJhZHplcmsxOTk3MjI3MzM5IiwiZnEiOjB9&s=LcA_5LSwTLkPqoN24zJVLp0fyO4)
up vote1down votefavorite | how to get luathread function with same function name in diffent luathreads in Lua 5.3? use getglobal in 5.1 work sucess, but under 5.3 work error, this code is main run call luathread functions const char * gLua0 = "function Test() print(12) end"; const char * gLua1 = "function Test() print(23) end"; lua_State * sL0 = newLuaThread(); luaL_loadbuffer(sL0, gLua0, strlen(gLua0), nullptr); lua_State * sL1 = newLuaThread(); luaL_loadbuffer(sL1, gLua1, strlen(gLua1), nullptr); lua_getglobal(sL0, "Test"); lua_pcall(sL0, 0, 0, 0); lua_getglobal(sL1, "Test"); lua_pcall(sL1, 0, 0, 0); //---- result of lua 5.1 12 23 //----- result of lua 5.3 23 23 why result 5.3 is diffent from 5.1? this code is create lua thread in lua 5.1 lua_State * newLuaThread() { lua_State * sL = lua_newthread(L); lua_pushvalue(L, -1); int nRef = luaL_ref(L, LUA_REGISTRYINDEX); lua_newtable(L); lua_pushvalue(L, -1); lua_setmetatable(L, -2); lua_pushvalue(L, LUA_GLOBALSINDEX); lua_setfield(L, -2, "__index"); lua_setfenv(L, -2); lua_pop(L, 1); return sL; } this code is create lua thread in lua 5.3 lua_State * newLuaThread() { lua_State * sL = lua_newthread(L); lua_pushvalue(L, -1); int nRef = luaL_ref(L, LUA_REGISTRYINDEX); lua_newtable(nLuaState); lua_pushvalue(nLuaState, -1); lua_setmetatable(nLuaState, -2); lua_getglobal(nLuaState, "_G"); lua_setfield(nLuaState, -2, "__index"); lua_setupvalue(nLuaState, 1, 1); lua_pop(L, 1); return sL; } c++ lua
| ||||||||
a comment |
1 Answer
activeoldestvotesup vote1down vote | spend 5 days find the correct answer is:luaL_loadbuffer(nLuaState, nBuffer, strlen(nBuffer), nullptr); lua_newtable(nLuaState); lua_pushvalue(nLuaState, -1); lua_setmetatable(nLuaState, -2); lua_getglobal(nLuaState, "_G"); lua_setfield(nLuaState, -2, "__index"); lua_pushvalue(nLuaState, -1); lua_setglobal(nLuaState, nChunk); lua_setupvalue(nLuaState, -2, 1); lua_getglobal(nLuaState, nChunk); lua_getfield(nLuaState, -1, nFun); lua_pcall(nLuaState, 0, 0, 0); |
相关文章推荐
- Implementing setfenv in Lua 5.2, 5.3, and above
- lua常见异常 USE " cc.exports. = value " INSTEAD OF SET GLOBAL VARIABLE"
- Set Font Properties On Mouse Hover Of Push Button And Text Items At Run time In Oracle Forms
- How to use outline levels to create a table of contents (TOC) in Word 2003 and in Word 2002
- Effective C++ Item 16 Use the same form in corresponding uses of new and delete
- 【ask】Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
- Demo of use SQL (Insert logs in system resource usage table)使用sql 的一个示例(插入系统使用日志数据)
- 海洋工作室——网站建设专家:The version of SQL Server in use does not support datatype datetime2 and the Entity Framework.
- TabError: Inconsistent use of tabs and spaces in indentation
- TabError: inconsistent use of tabs and spaces in indentation
- Kernel Page Global Directory (PGD) of Page table of Process created in Linux Kernel
- Item 16: Use the same form in corresponding uses of new and delete.(Effective C++)
- Use the unsinged type as operands of shift operator in C language
- [常见错误 及 解决方案系列]USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
- Use Of Cursors And Parallel Cursors In SAP ABAP
- Use Module and Function instead of Class in Python
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- What is Local Index and Global Index in Partition Table? What is Prefix Index and Nonprefix Index in table?
- python inconsistent use of tabs and spaces in indentation
- [EffectiveC++]item16:Use the same form in corresponding uses of new and delete