Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

[BUG FIX]如果更新的文件中新定义了一个function,使用了global变量,会更新错误 #16

@magicsupery

Description

@magicsupery

1.不能正确热更的代码:

local a = 10
local data = {
    a = a,
    c = c,
}

function data.globalTest()
   return c
end

return data

如果原有模块没有定义globalTest函数,因为globalTest在沙盒中被require,其env指向的是沙盒内的global,会有错误。

原始代码如下:
489行
https://github.com/cloudwu/luareload/blob/master/reload.lua#:~:text=end-,for%20_%2C%20item%20in%20ipairs(data.objects)%20do,end,-else

2.修改方案:
假如有原有函数,可以设置原有函数的env;但是因为是新增函数,按照一般理解来看,这里的env应该可以理解成_G,根据项目约束,我们可以增加代码如下:

if type(v) == "function" then
    debug.setfenv(v, _G)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions