Search code examples
luazerobrane

Why Lua package.loaded can't cache global table returned by module?


From the Lua 5.1 reference manual:

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]

In my opinion, it meas when you use require and success, package.loaded[modname] must have value no matter true or table which depend on loader function has return

so there is my test below

enter image description here

Where is my package.loaded["test"]? But when I change a line in test.lua

enter image description here

Here is my Lua interperter:

ZeroBraneStudioEduPack-1.40-win32/Project/Lua Interperter/Lua

Sorry my poor English

P.S.
Screenshots above are available at full resolution: 1, 2


Solution

  • ZeroBrane Studio limits the size of the content retrieved and shown in the Watch panel (as controlled by several debugger.* settings). In your case, referencing _G returns a large payload that pushes out some of the other content (it just happens to push different content out as the global variable is already listed in _G, that's why you see slightly different results).

    Try adding the following as the second line in your script package.loaded._G = nil and you should see more elements shown and the results will be consistent between the two cases.