Hi,

> Sorry for the delay responding.  I am not saying to do this (pseudocode):
>
> package.loaded = clone_table(originalPackageLoaded)
>
> Rather, I am suggesting this (pseudocode):
>
> clear_all_subkeys(package.loaded)
> merge_into_from(package.loaded, originalPackageLoaded)
>
> The first way would break identity with registry value _LOADED. The second
> would not.*
>
> Referencing it through package.loaded is the right way because it is the
> documented way.  The use of _LOADED in the registry is an undocumented
> implementation detail which might change.

Line 652 of lib/loadlib.c:

  /* set field `loaded' */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
  lua_setfield(L, -2, "loaded");

package.loaded just references to the _LOADED in LUA_REGISTRYINDEX. We could change package.loaded to whatever the hell we want, but internally Lua just gets the _LOADED table in its REGISTRY (poke around loadlib.c if you don't believe me). I ran a grep in the Lua source directory and nowhere does Lua pull package data from package.loaded, it's ALWAYS from _LOADED. Believe me, I've tried what you are suggesting; a long time ago. My package.loaded would read correctly when I print it, but I always get a "package already loaded" message because of the values in _LOADED -- even if I reload the Lua startup script. I see no other way to work around this other than changing Lua source code. That is why I implemented it the way I did in the extension code, modifying _LOADED directly. If you could do better, by all means please prove me wrong. I am a novice C++ programmer after all ;) Maybe the way the extension code re-inits the global scope could be done differently to preserve the package table.

Here's my thoughts on what is happening.

1. When SciTE starts up, a new Lua state is created and _LOADED is created in the REGISTRY. Then the extension saves the populated global namespace for re-merging later when necessary and SciTE does its thing. 2. When different Lua scripts are loaded for different file extensions, everything is still preserved. The memory addresses (as I have checked) of the library tables (string, math, package, etc.) are intact. The "shallow" copy of the global namespace functions as it should when it is re-merged into the global namespace. 3. When the extension is *reloaded*, the global namespace is cleared and *reinitialized*. Therefore all memory addresses are different (I have checked and confirmed this) with the exception of _G naturally. 4. However, _LOADED knows nothing about this *reinitialization* because the same Lua state is being used. Therefore it must be modified directly in order to accurately reflect the status of loaded packages.

I will try to experiment more with different things, however I believe I have already exhausted every conceivable thought as to getting it to work without modifying _LOADED directly.

Take care,
-Mitchell;

>
> Cheers,
> Bruce
>
> * This assumes we're only making a shallow copy of the global namespace, so
> the original package.loaded and the new package.loaded refer to the same
> instance. I believe that is the case; it was my intention at least. If it
> is not working that way, I would call it a bug or at least a misfeature.
>

_______________________________________________
Scite-interest mailing list
[email protected]
http://mailman.lyra.org/mailman/listinfo/scite-interest

Reply via email to