具有本地链路擦除的Lua全局表

具有本地链路擦除的Lua全局表,lua,Lua,我有一个由几个文件组成的模块,其中声明了一个全局表,用于在函数之间传递消息。 当我试图清理一个全局表而不是main文件时,我遇到了冲突,据我所知,这是因为我正在创建一个链接localvalues=\u G.values但我这样做是为了在访问此变量时提高性能 如何清除另一个文件中的全局(使用本地链接)表? 也许还有其他方法可以在模块文件之间传输共享数据,而无需访问 模块: main.lua utils/a.lua utils/b.lua 梅因·卢阿 os.setlocale("C&qu

我有一个由几个文件组成的模块,其中声明了一个全局表,用于在函数之间传递消息。 当我试图清理一个全局表而不是main文件时,我遇到了冲突,据我所知,这是因为我正在创建一个链接localvalues=\u G.values但我这样做是为了在访问此变量时提高性能 如何清除另一个文件中的全局(使用本地链接)表? 也许还有其他方法可以在模块文件之间传输共享数据,而无需访问

模块:

main.lua
utils/a.lua
utils/b.lua
梅因·卢阿

os.setlocale("C")
package.path   = package.path .. ";./utils/?.lua";  -- luacheck: ignore
local Timer = require("timer")

_G.run = true;
_G.values = {};

local counter = 0;
local values = _G.values;
local timer_main = Timer:new()
timer_main:setInterval(1)

local timer_work = Timer:new()
timer_work:setInterval(10)


local a = require("utils.a");
local b = require("utils.b");

local function UpdateTable()
    counter = counter + 1
    print("Update table from main:       "..tostring(values))
    values[counter] = {Apple=counter+1, Banana=counter+2, Orange=counter+3}
end;


local function main()
    print("Global_main_start:            "..tostring(_G.values))
    print("Local_main_start:             "..tostring(values))
    while _G.run do
        if timer_main:isTimeout() then
            UpdateTable()
            print("Global_main_loop:             "..tostring(_G.values))
            print("Local_main_loop:              "..tostring(values))
            timer_main:setInterval(1)
        end
        b.TaskExecute()
        if timer_work:isTimeout() then
            _G.run = false
        end
    end
    print("Global_main_stop:             "..tostring(_G.values))
    print("Local_main_stop:              "..tostring(values))
end;


main()
a、 卢阿

b、 卢阿

timer.lua(仅用于同时执行func)

输出:

Local_after_require_a:        table: 0x7ff50ac09230
Local_after_require_a:        table: 0x7ff50ac09230
Local_after_require_b:        table: 0x7ff50ac09230
Global_main_start:            table: 0x7ff50ac09230
Local_main_start:             table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50ac09230
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
Global EraseData a before:    table: 0x7ff50ac09230
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50af05510   <--- Erased Global var in a.lua 
Local EraseData a after:      table: 0x7ff50ac09230   <--- Local var in a.lua not changed 
Global Execute b after:       table: 0x7ff50af05510   <--- Global var in b.lua changed 
Local Execute b after:        table: 0x7ff50ac09230   <--- Local var in b.lua not changed 
Update table from main:       table: 0x7ff50ac09230   <--- Add new values in old local var in main.lua
Global_main_loop:             table: 0x7ff50af05510  <--- Global var in main.lua changed 
Local_main_loop:              table: 0x7ff50ac09230  <--- Local var in main.lua not changed
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50af05510
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50af05510
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50af05510
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
         Values item: 4       table: 0x7ff50c004280
         Values item: 5       table: 0x7ff50ac08bb0
         Values item: 6       table: 0x7ff50c004410
Global EraseData a before:    table: 0x7ff50af05510
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50c004940
Local EraseData a after:      table: 0x7ff50ac09230
Global Execute b after:       table: 0x7ff50c004940
Local Execute b after:        table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50c004940
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
         Values item: 4       table: 0x7ff50c004280
         Values item: 5       table: 0x7ff50ac08bb0
         Values item: 6       table: 0x7ff50c004410
         Values item: 7       table: 0x7ff50ac09380
         Values item: 8       table: 0x7ff50ac09510
         Values item: 9       table: 0x7ff50c004350
Global EraseData a before:    table: 0x7ff50c004940
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50c004b90
Local EraseData a after:      table: 0x7ff50ac09230
Global Execute b after:       table: 0x7ff50c004b90
Local Execute b after:        table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004b90
Local_main_loop:              table: 0x7ff50ac09230
Global_main_stop:             table: 0x7ff50c004b90
Local_main_stop:              table: 0x7ff50ac09230
Local\u-after\u-require\u a:table:0x7ff50ac09230
本地请求后的请求:表:0x7ff50ac09230
需求后的本地需求:表:0x7ff50ac09230
全局主启动:表:0x7ff50ac09230
本地主启动:表:0x7ff50ac09230
从main更新表:表:0x7ff50ac09230
全局主循环:表:0x7ff50ac09230
本地主回路:表:0x7ff50ac09230
从main更新表:表:0x7ff50ac09230
全局主循环:表:0x7ff50ac09230
本地主回路:表:0x7ff50ac09230
从main更新表:表:0x7ff50ac09230
全局主循环:表:0x7ff50ac09230
本地主回路:表:0x7ff50ac09230
表0x7ff50ac09230之前的全局执行b
本地执行b之前:表:0x7ff50ac09230
值项:1表:0x7ff50ac07be0
值项:2表:0x7ff50c0040d0
值项:3表:0x7ff50af049d0
之前的全局擦除数据a:表:0x7ff50ac09230
之前的本地数据a:表:0x7ff50ac09230

全局擦除:表:0x7ff50af05510之后的数据a执行
\u G.values={}
不会“擦除”表。它仅将
\u G.values
的引用更改为新创建的表。前面指向的表
\u G.values
完全不变,任何其他引用(如
a
的本地
)将保持不变。您可以通过print语句中显示的值看到这一点

表格:0x7ff50ac09230
这是原始表格

表格:0x7ff50af05510
这是新表格

因为您没有更改
表:0x7ff50ac09230
,所以您不会看到
a
的本地
值发生任何更改


我建议您将要清除的表值向下推一个级别,使其位于所引用的表中:

主要

a


我还发现没有必要在您提供的代码中使用
\u G
,只需执行
局部值=值
如果需要访问“原始值”,正确的值将是全局值值现在被局部着色,那么
\u G
就有意义了。

您可以通过删除表中的所有元素来清理表:
对于成对的k(值)do values[k]=nil end
local Timer = require("timer")

local values = _G.values;
local a = a or require("a");
local timer_b = Timer:new()
timer_b:setInterval(3)
print("Local_after_require_b:        "..tostring(values))

local function TaskExecute()
    if timer_b:isTimeout() then
        print("Global Execute b before:      "..tostring(_G.values))
        print("Local Execute b before:       "..tostring(values))
        if #values > 0 then
            for i = 1, #values do
                print("         Values item: "..i.."       "..tostring(values[i]))
            end
        end
        a.EraseData()
        print("Global Execute b after:       "..tostring(_G.values))
        print("Local Execute b after:        "..tostring(values))
        timer_b:setInterval(3)
    end
end;

return {
    TaskExecute = TaskExecute
}
local base   = _G;
local math   = math;
local os     = os;

local Timer = {}


local function new(self)
    local timer = {
        deadline = os.time()
    }
    base.setmetatable(timer, self)
    self.__index = self
    return timer
end

Timer.new = new

local function setInterval(self, duration)
    self.deadline = os.time() + duration
end

Timer.setInterval = setInterval

local function setDate(self, date)
    self.deadline = os.time(date)
end

Timer.setDate = setDate

local function getSecondsLeft(self)
    return math.max(0, self.deadline - os.time())
end

Timer.getSecondsLeft = getSecondsLeft


local function isTimeout(self)
    return os.time() >= self.deadline
end

Timer.isTimeout = isTimeout

return Timer
Local_after_require_a:        table: 0x7ff50ac09230
Local_after_require_a:        table: 0x7ff50ac09230
Local_after_require_b:        table: 0x7ff50ac09230
Global_main_start:            table: 0x7ff50ac09230
Local_main_start:             table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50ac09230
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50ac09230
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
Global EraseData a before:    table: 0x7ff50ac09230
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50af05510   <--- Erased Global var in a.lua 
Local EraseData a after:      table: 0x7ff50ac09230   <--- Local var in a.lua not changed 
Global Execute b after:       table: 0x7ff50af05510   <--- Global var in b.lua changed 
Local Execute b after:        table: 0x7ff50ac09230   <--- Local var in b.lua not changed 
Update table from main:       table: 0x7ff50ac09230   <--- Add new values in old local var in main.lua
Global_main_loop:             table: 0x7ff50af05510  <--- Global var in main.lua changed 
Local_main_loop:              table: 0x7ff50ac09230  <--- Local var in main.lua not changed
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50af05510
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50af05510
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50af05510
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
         Values item: 4       table: 0x7ff50c004280
         Values item: 5       table: 0x7ff50ac08bb0
         Values item: 6       table: 0x7ff50c004410
Global EraseData a before:    table: 0x7ff50af05510
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50c004940
Local EraseData a after:      table: 0x7ff50ac09230
Global Execute b after:       table: 0x7ff50c004940
Local Execute b after:        table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004940
Local_main_loop:              table: 0x7ff50ac09230
Global Execute b before:      table: 0x7ff50c004940
Local Execute b before:       table: 0x7ff50ac09230
         Values item: 1       table: 0x7ff50ac07be0
         Values item: 2       table: 0x7ff50c0040d0
         Values item: 3       table: 0x7ff50af049d0
         Values item: 4       table: 0x7ff50c004280
         Values item: 5       table: 0x7ff50ac08bb0
         Values item: 6       table: 0x7ff50c004410
         Values item: 7       table: 0x7ff50ac09380
         Values item: 8       table: 0x7ff50ac09510
         Values item: 9       table: 0x7ff50c004350
Global EraseData a before:    table: 0x7ff50c004940
Local EraseData a before:     table: 0x7ff50ac09230
Global EraseData a after:     table: 0x7ff50c004b90
Local EraseData a after:      table: 0x7ff50ac09230
Global Execute b after:       table: 0x7ff50c004b90
Local Execute b after:        table: 0x7ff50ac09230
Update table from main:       table: 0x7ff50ac09230
Global_main_loop:             table: 0x7ff50c004b90
Local_main_loop:              table: 0x7ff50ac09230
Global_main_stop:             table: 0x7ff50c004b90
Local_main_stop:              table: 0x7ff50ac09230
messages = {}
messages.values = {}
local messages = _G.messages
...

messages.values = {}