Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

反馈一个 HotReload.lua 中 require 函数的问题 #653

Open
GotGimhong opened this issue Oct 8, 2023 · 3 comments
Open

反馈一个 HotReload.lua 中 require 函数的问题 #653

GotGimhong opened this issue Oct 8, 2023 · 3 comments

Comments

@GotGimhong
Copy link
Contributor

HotReload.lua 重写了 require 函数。按照目前的实现,如果在 Lua 代码的末尾没有任何返回,那么被 require 过的 Lua 代码并不会被记录到 loaded_modulepackage.loaded 当中,因为这里的临时变量 new_module 始终是 nil
UnLua反馈

举个例子:

-- Script/MyGlobalTable.lua
MyGlobalTable = MyGlobalTable or {}
-- ...

如果使用重写后的 require 函数,那么每次 require 这个文件时都会重新执行一次里面的代码。但是按照 Lua 原来的 require 函数的实现,require 过一次后,package.loaded["MyGlobalTable"] 会被设置成 true,之后即使重复 require,也不会重复执行文件里的代码。

因此想请问一下这样实现的原因,是出于某种考虑还是单纯写错了。

@jozhn
Copy link
Contributor

jozhn commented Oct 18, 2023

你记错了,Lua本身就是只有文件末尾return table的时候才会记录在package.loaded里面

@GotGimhong
Copy link
Contributor Author

你记错了,Lua本身就是只有文件末尾return table的时候才会记录在package.loaded里面

文件末尾有 return table 的时候当然会把这个 table 记录在 package.loaded 里面。我的意思是,当文件末尾没有 return table 时,package.loaded 里面也会对应记录一个 true,表示加载过这个文件,这个是 Lua 本身的设计,具体可以看 Lua 源码 loadlib.c 里面,ll_require 函数的实现,以下是 Lua 5.4.4 的实现,在旧版本中也有这种设计。
lua544_loadlib_c

@jozhn
Copy link
Contributor

jozhn commented Oct 26, 2023

哦,是我记错了。。。你说得对

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants