mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 05:51:51 +00:00
fix reload_module ignoring pack environment
reload_module loaded the new module code with the global environment, while require runs modules in their pack environment. After reload, PACK_ID and other pack globals were nil in the module.
This commit is contained in:
parent
e8a83423d8
commit
d8017ff995
2 changed files with 43 additions and 6 deletions
33
dev/tests/reload_module.lua
Normal file
33
dev/tests/reload_module.lua
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
-- reload_module must run the module in its pack environment, as require does
|
||||
local util = require "core:tests_util"
|
||||
util.create_demo_world()
|
||||
|
||||
-- a pack with a module that uses pack environment (PACK_ID)
|
||||
local PACK = "world:content/reload_test"
|
||||
file.mkdirs(PACK .. "/modules")
|
||||
file.write(PACK .. "/package.json", json.tostring({id="reload_test", title="reload_test"}))
|
||||
|
||||
local function write_module(version)
|
||||
file.write(PACK .. "/modules/mod.lua", string.format([[
|
||||
local this = {}
|
||||
function this.info() return PACK_ID, %d end
|
||||
return this
|
||||
]], version))
|
||||
end
|
||||
|
||||
write_module(1)
|
||||
app.reconfig_packs({"reload_test"}, {})
|
||||
app.tick()
|
||||
|
||||
local mod = require "reload_test:mod"
|
||||
local packid, version = mod.info()
|
||||
assert(packid == "reload_test" and version == 1)
|
||||
|
||||
write_module(2)
|
||||
reload_module("reload_test:mod")
|
||||
packid, version = mod.info()
|
||||
assert(version == 2, "module is not reloaded")
|
||||
assert(packid == "reload_test", "reloaded module has no pack environment")
|
||||
|
||||
app.close_world(true)
|
||||
app.delete_world("demo")
|
||||
|
|
@ -183,6 +183,12 @@ function on_deprecated_call(name, alternatives)
|
|||
end
|
||||
end
|
||||
|
||||
local __pack_envs = __vc__pack_envs
|
||||
function __vc_internals.get_pack_env(packid)
|
||||
return __pack_envs[packid]
|
||||
end
|
||||
__vc__pack_envs = nil
|
||||
|
||||
function reload_module(name)
|
||||
local prefix, name = parse_path(name)
|
||||
local path = prefix..":modules/"..name..".lua"
|
||||
|
|
@ -196,6 +202,10 @@ function reload_module(name)
|
|||
if script == nil then
|
||||
error(err)
|
||||
end
|
||||
local env = __pack_envs[prefix]
|
||||
if env then
|
||||
script = setfenv(script, env)
|
||||
end
|
||||
local result = script()
|
||||
if not result then
|
||||
return
|
||||
|
|
@ -270,12 +280,6 @@ function __vc_lock_internal_modules()
|
|||
__internal_locked = true
|
||||
end
|
||||
|
||||
local __pack_envs = __vc__pack_envs
|
||||
function __vc_internals.get_pack_env(packid)
|
||||
return __pack_envs[packid]
|
||||
end
|
||||
__vc__pack_envs = nil
|
||||
|
||||
function require(path)
|
||||
if not string.find(path, ':') then
|
||||
local prefix, _ = parse_path(_debug_getinfo(2).source)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue