Requirement that a LuaJIT module in a sub-directory rewrite a module with the same name in the parent directory

I have a file setting like this:

main.lua  (requires 'mydir.b' and then 'b')
b.lua
mydir/
  b.so    (LuaJIT C module)

Basically, I do this:

function print_loaded()
  for k, v in pairs(package.loaded) do print(k, v) end
end

print_loaded()
require 'mydir.b'
print_loaded()

-- This would now include 'mydir.b' instead of 'b':
local b = require 'b'

The outputs printshow that my call require 'mydir.b'sets the return value as a value package.loaded['b']as well as expected package.loaded['mydir.b']. I wanted to package.loaded['b']leave it unchanged so that I could later require 'b', and not end up with (in my opinion, wrong) cached value from mydir.b.

My question is: what is a good way to handle this?

, mydir LuaJIT , mydir.whatever , require whatever .

, : " !" . . , , .

+4
1

, luaL_register b.so (b.c).

, :

static const struct luaL_reg b[] = {
  /* set up a list of function pointers here */
};

int luaopen_mydir_b(lua_State *L) {
  luaL_register(L, "b", b);  // <-- PROBLEM HERE (see below)
  return 1;                  // 1 = # Lua-visible return values on the stack.
}

, package.loaded['b'] . , :

luaL_register(L, "mydir.b", b);

package.loaded['mydir.b'] , , ( mydir).

, , docs luaL_register Lua 5.1, LuaJIT.

+1

Source: https://habr.com/ru/post/1584670/


All Articles