为什么 Lua package.loaded 不能缓存模块返回的全局 table?
Why Lua package.loaded can't cache global table returned by module?
来自Lua 5.1 参考手册:
Once a loader is found, require calls the loader with a single
argument, modname. If the loader returns any value, require assigns
the returned value to package.loaded[modname]. If the loader returns
no value and has not assigned any value to package.loaded[modname],
then require assigns true to this entry. In any case, require returns
the final value of package.loaded[modname]
在我看来,这意味着当你使用 require 和 success 时,package.loaded[modname]
必须有值,无论是 true 还是 table 这取决于加载程序函数有 return
下面是我的测试
我的 package.loaded["test"]
在哪里?但是当我在 test.lua
中更改一行时
这是我的 Lua 解说员:
ZeroBraneStudioEduPack-1.40-win32/Project/Lua Interperter/Lua
对不起我糟糕的英语
P.S.
上面的屏幕截图以全分辨率提供:1, 2
ZeroBrane Studio 限制了在监视面板中检索和显示的内容的大小(由多个 debugger.*
settings 控制)。在你的例子中,引用 _G
returns 一个大的有效载荷,它推出了一些其他内容(它恰好推出了不同的内容,因为全局变量已经在 _G
中列出,那是为什么你看到的结果略有不同)。
尝试将以下内容添加为脚本中的第二行 package.loaded._G = nil
,您应该会看到显示的更多元素,并且两种情况下的结果将是一致的。
来自Lua 5.1 参考手册:
Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname]
在我看来,这意味着当你使用 require 和 success 时,package.loaded[modname]
必须有值,无论是 true 还是 table 这取决于加载程序函数有 return
下面是我的测试
我的 package.loaded["test"]
在哪里?但是当我在 test.lua
这是我的 Lua 解说员:
ZeroBraneStudioEduPack-1.40-win32/Project/Lua Interperter/Lua
对不起我糟糕的英语
P.S.
上面的屏幕截图以全分辨率提供:1, 2
ZeroBrane Studio 限制了在监视面板中检索和显示的内容的大小(由多个 debugger.*
settings 控制)。在你的例子中,引用 _G
returns 一个大的有效载荷,它推出了一些其他内容(它恰好推出了不同的内容,因为全局变量已经在 _G
中列出,那是为什么你看到的结果略有不同)。
尝试将以下内容添加为脚本中的第二行 package.loaded._G = nil
,您应该会看到显示的更多元素,并且两种情况下的结果将是一致的。