<tfoot id='kmtyw'></tfoot>

    1. <legend id='kmtyw'><style id='kmtyw'><dir id='kmtyw'><q id='kmtyw'></q></dir></style></legend>
      • <bdo id='kmtyw'></bdo><ul id='kmtyw'></ul>
    2. <i id='kmtyw'><tr id='kmtyw'><dt id='kmtyw'><q id='kmtyw'><span id='kmtyw'><b id='kmtyw'><form id='kmtyw'><ins id='kmtyw'></ins><ul id='kmtyw'></ul><sub id='kmtyw'></sub></form><legend id='kmtyw'></legend><bdo id='kmtyw'><pre id='kmtyw'><center id='kmtyw'></center></pre></bdo></b><th id='kmtyw'></th></span></q></dt></tr></i><div id='kmtyw'><tfoot id='kmtyw'></tfoot><dl id='kmtyw'><fieldset id='kmtyw'></fieldset></dl></div>

      <small id='kmtyw'></small><noframes id='kmtyw'>

      通过 require 在不同 Lua 状态之间共享全局变量

      Sharing global variables between different Lua states through require(通过 require 在不同 Lua 状态之间共享全局变量)

        <bdo id='EDvc6'></bdo><ul id='EDvc6'></ul>
          <legend id='EDvc6'><style id='EDvc6'><dir id='EDvc6'><q id='EDvc6'></q></dir></style></legend>

            <tfoot id='EDvc6'></tfoot>

                  <tbody id='EDvc6'></tbody>

                <small id='EDvc6'></small><noframes id='EDvc6'>

              1. <i id='EDvc6'><tr id='EDvc6'><dt id='EDvc6'><q id='EDvc6'><span id='EDvc6'><b id='EDvc6'><form id='EDvc6'><ins id='EDvc6'></ins><ul id='EDvc6'></ul><sub id='EDvc6'></sub></form><legend id='EDvc6'></legend><bdo id='EDvc6'><pre id='EDvc6'><center id='EDvc6'></center></pre></bdo></b><th id='EDvc6'></th></span></q></dt></tr></i><div id='EDvc6'><tfoot id='EDvc6'></tfoot><dl id='EDvc6'><fieldset id='EDvc6'></fieldset></dl></div>
                本文介绍了通过 require 在不同 Lua 状态之间共享全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我试图找到一种方法来在不同的 Lua 状态之间共享特定 Lua 脚本(在示例中为test.lua)的全局变量.

                I'm trying to find a way to share global variables of a specific Lua script(test.lua in the example) between different Lua states.

                这是我的简单示例代码:

                Here's my simple example code:

                test.lua

                num = 2
                

                main.cpp

                #include <iostream>
                #include <lua.hpp>
                int main() 
                {    
                    lua_State *L1 = luaL_newstate(); //script A
                    luaL_openlibs(L1);
                    lua_settop(L1, 0);
                    luaL_dostring(L1, "require('test') num = 5");
                
                    lua_State *L2 = luaL_newstate(); //script B
                    luaL_openlibs(L2);
                    lua_settop(L2, 0);
                    luaL_dostring(L2, "require('test') print(num)");
                
                    lua_close(L1);
                    lua_close(L2);
                }
                

                我希望得到 5 但我得到了 2.

                I expect to get 5 but I get 2.

                不能通过require在不同的lua_State*之间共享全局变量吗?

                Is not possible to share global variables between different lua_State* through require?

                添加:

                如果不可能,使用luaL_loadfile打开test.lua,然后在C++中创建getter/setter方法来共享变量是个好主意吗?num 介于脚本 AB 之间?

                If it's not possible, would it be a good idea to open test.lua using luaL_loadfile and then create getter/setter methods in C++ to share variable num between script A and B?

                例如像这样,

                脚本A:

                script = my.Script("test")
                script:setVar("num", 5)
                

                脚本 B:

                script = my.Script("test")
                print(script:getVar("num"))
                

                我想知道您如何看待这种设计作为 require 的替代方案.

                I wonder what you think about this design as an alternative to require.

                推荐答案

                您可以将指向 C++ 值的指针作为元表的上值推送到包含这些全局变量的表中,而不是在 Lua 模块中使用全局值.然后将具有相同元表的 globals 表推送到两个 VM.当您现在访问 globals.num 时,会触发 getglobalsetglobal 元方法(取决于您是读还是写).这些将更新 C++ 端的值,以便在两个 VM 之间共享.

                Rather than having the global value in a Lua module, you could push a pointer to a C++ value as an upvalue for a metatable to a table which contains those globals. Then you push the globals table with the same metatable to both VMs. When you now access globals.num the getglobal and setglobal metamethods are triggered (depending on whether you read or write). These will update the value on the C++ side, such that it is shared between the two VMs.

                注意: 从冗长的样板文件可以看出,这不是一个好的解决方案.您应该避免同时拥有多个 VM.如果出于并发目的需要多个 VM,请考虑使用像 Lua Lanes 这样的成熟库,而不是使用自己的库(正确执行此操作需要数千行代码).

                N.B.: As you can judge from the lengthy boilerplate this is not a good solution. You should avoid having multiple VMs at the same time. If you require multiple VMs for concurrency purposes, consider using a mature library like Lua Lanes rather than rolling your own (doing this right requires several thousands of lines of code).

                #include <string>
                
                #include <lua.hpp>
                
                int setglobal(lua_State *L) {
                    void *p = luaL_checkudata(L, 1, "globals_meta");
                    luaL_argcheck(L, p != nullptr, 1, "invalid userdata");
                
                    std::string key = lua_tostring(L, 2);
                    luaL_argcheck(L, key == "num", 2, "unknown global");
                
                    int value = luaL_checkinteger(L, 3);
                    luaL_argcheck(L, lua_isnumber(L, 3), 3, "not a number");
                
                    int *num = static_cast<int *>(lua_touserdata(L, lua_upvalueindex(1)));
                    *num = value;
                    lua_pop(L, 1);
                
                    return 0;
                }
                
                int getglobal(lua_State *L) {
                    void *p = luaL_checkudata(L, 1, "globals_meta");
                    luaL_argcheck(L, p != nullptr, 1, "invalid userdata");
                
                    std::string key = lua_tostring(L, 2);
                    luaL_argcheck(L, key == "num", 2, "unknown global");
                
                    int num = *static_cast<int *>(lua_touserdata(L, lua_upvalueindex(1)));
                    lua_pop(L, 1);
                
                    lua_pushinteger(L, num);
                    return 1;
                }
                
                static const struct luaL_Reg globals_meta[] = {
                    {"__newindex", setglobal},
                    {"__index", getglobal},
                    {nullptr, nullptr} // sentinel
                };
                
                int main() {
                    int num = 2;
                
                    // script A
                
                    lua_State *L1 = luaL_newstate();
                    luaL_openlibs(L1);
                
                    luaL_newmetatable(L1, "globals_meta");
                    lua_pushlightuserdata(L1, &num);
                    luaL_setfuncs(L1, globals_meta, 1);
                
                    lua_newuserdata(L1, 0);
                    luaL_getmetatable(L1, "globals_meta");
                    lua_setmetatable(L1, -2);
                    lua_setglobal(L1, "globals");
                
                    luaL_dostring(L1, "print('Script A: ' .. globals.num) globals.num = 5");
                
                    // script B
                
                    lua_State *L2 = luaL_newstate();
                    luaL_openlibs(L2);
                
                    luaL_newmetatable(L2, "globals_meta");
                    lua_pushlightuserdata(L2, &num);
                    luaL_setfuncs(L2, globals_meta, 1);
                
                    lua_newuserdata(L2, 0);
                    luaL_getmetatable(L2, "globals_meta");
                    lua_setmetatable(L2, -2);
                    lua_setglobal(L2, "globals");
                
                    luaL_dostring(L2, "print('Script B: ' .. globals.num)");
                
                    lua_close(L1);
                    lua_close(L2);
                }
                

                <小时>

                作为对自己的挑战,我实现了一个完整的全局表,它可以传递 nilboolint 类型的值doublestring 在两个 Lua 状态之间.它们可以使用具有字符串表示形式的所有内容命名.


                As a challange to myself I implemented a complete global table which can communicate values of type nil, bool, int, double, and string between two Lua states. They can be named with everything that has a string representation.

                -- To be on the safe side, just use numbers and strings as keys
                globals[1] = "x"
                globals.num = 5
                
                -- Be careful when using table or function literals as keys
                -- Two empty tables don't have the same representation
                globals[{}] = 2 -- "table: 0x10d55a0" = 2
                globals[{}] = 1 -- "table: 0x10ce2c0" = 1
                

                我没有详尽地检查各种异常情况,所以不退款!

                I haven't checked all sorts of exceptional situations exhaustively, so no refunds!

                #include <iostream>
                #include <string>
                #include <unordered_map>
                
                #include <boost/variant.hpp>
                
                #include <lua.hpp>
                
                enum class nil {};
                using Variant = boost::variant<nil, bool, int, double, std::string>;
                
                int setglobal(lua_State *L) {
                    void *p = luaL_checkudata(L, 1, "globals_meta");
                    luaL_argcheck(L, p != nullptr, 1, "invalid userdata");
                
                    std::string key = luaL_tolstring(L, 2, nullptr);
                
                    auto &globals = *static_cast<std::unordered_map<std::string, Variant> *>(
                        lua_touserdata(L, lua_upvalueindex(1)));
                    Variant &v = globals[key];
                
                    switch (lua_type(L, 3)) {
                    case LUA_TNIL:
                        v = nil{};
                        break;
                    case LUA_TBOOLEAN:
                        v = static_cast<bool>(lua_toboolean(L, 3));
                        lua_pop(L, 1);
                        break;
                    case LUA_TNUMBER:
                        if (lua_isinteger(L, 3)) {
                            v = static_cast<int>(luaL_checkinteger(L, 3));
                        } else {
                            v = static_cast<double>(luaL_checknumber(L, 3));
                        }
                        lua_pop(L, 1);
                        break;
                    case LUA_TSTRING:
                        v = std::string(lua_tostring(L, 3));
                        lua_pop(L, 1);
                        break;
                    default:
                        std::string error = "Unsupported global type: ";
                        error.append(lua_typename(L, lua_type(L, 3)));
                        lua_pushstring(L, error.c_str());
                        lua_error(L);
                        break;
                    }
                    return 0;
                }
                
                int getglobal(lua_State *L) {
                    void *p = luaL_checkudata(L, 1, "globals_meta");
                    luaL_argcheck(L, p != nullptr, 1, "invalid userdata");
                
                    std::string key = luaL_tolstring(L, 2, nullptr);
                
                    auto globals = *static_cast<std::unordered_map<std::string, Variant> *>(
                        lua_touserdata(L, lua_upvalueindex(1)));
                    lua_pop(L, 1);
                    auto search = globals.find(key);
                    if (search == globals.end()) {
                        lua_pushstring(L, ("unknown global: " + key).c_str());
                        lua_error(L);
                        return 0;
                    }
                    Variant const &v = search->second;
                
                    switch (v.which()) {
                    case 0:
                        lua_pushnil(L);
                        break;
                    case 1:
                        lua_pushboolean(L, boost::get<bool>(v));
                        break;
                    case 2:
                        lua_pushinteger(L, boost::get<int>(v));
                        break;
                    case 3:
                        lua_pushnumber(L, boost::get<double>(v));
                        break;
                    case 4:
                        lua_pushstring(L, boost::get<std::string>(v).c_str());
                        break;
                    default: // Can't happen
                        std::abort();
                        break;
                    }
                
                    return 1;
                }
                
                static const struct luaL_Reg globals_meta[] = {
                    {"__newindex", setglobal},
                    {"__index", getglobal},
                    {nullptr, nullptr} // sentinel
                };
                
                int main() {
                    std::unordered_map<std::string, Variant> globals;
                    globals["num"] = 2;
                
                    // script A
                
                    lua_State *L1 = luaL_newstate();
                    luaL_openlibs(L1);
                
                    luaL_newmetatable(L1, "globals_meta");
                    lua_pushlightuserdata(L1, &globals);
                    luaL_setfuncs(L1, globals_meta, 1);
                
                    lua_newuserdata(L1, 0);
                    luaL_getmetatable(L1, "globals_meta");
                    lua_setmetatable(L1, -2);
                    lua_setglobal(L1, "globals");
                
                    if (luaL_dostring(L1, "print('Script A: ' .. globals.num)
                "
                                          "globals.num = 5") != 0) {
                        std::cerr << "L1:" << lua_tostring(L1, -1) << '
                ';
                        lua_pop(L1, 1);
                    }
                
                    // script B
                
                    lua_State *L2 = luaL_newstate();
                    luaL_openlibs(L2);
                
                    luaL_newmetatable(L2, "globals_meta");
                    lua_pushlightuserdata(L2, &globals);
                    luaL_setfuncs(L2, globals_meta, 1);
                
                    lua_newuserdata(L2, 0);
                    luaL_getmetatable(L2, "globals_meta");
                    lua_setmetatable(L2, -2);
                    lua_setglobal(L2, "globals");
                
                    if (luaL_dostring(L2, "print('Script B: ' .. globals.num)") != 0) {
                        std::cerr << "L1:" << lua_tostring(L2, -1) << '
                ';
                        lua_pop(L2, 1);
                    }
                
                    lua_close(L1);
                    lua_close(L2);
                }
                

                这篇关于通过 require 在不同 Lua 状态之间共享全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                  <tfoot id='SxW5B'></tfoot>

                  <i id='SxW5B'><tr id='SxW5B'><dt id='SxW5B'><q id='SxW5B'><span id='SxW5B'><b id='SxW5B'><form id='SxW5B'><ins id='SxW5B'></ins><ul id='SxW5B'></ul><sub id='SxW5B'></sub></form><legend id='SxW5B'></legend><bdo id='SxW5B'><pre id='SxW5B'><center id='SxW5B'></center></pre></bdo></b><th id='SxW5B'></th></span></q></dt></tr></i><div id='SxW5B'><tfoot id='SxW5B'></tfoot><dl id='SxW5B'><fieldset id='SxW5B'></fieldset></dl></div>
                    <tbody id='SxW5B'></tbody>

                      <bdo id='SxW5B'></bdo><ul id='SxW5B'></ul>

                    • <small id='SxW5B'></small><noframes id='SxW5B'>

                        1. <legend id='SxW5B'><style id='SxW5B'><dir id='SxW5B'><q id='SxW5B'></q></dir></style></legend>