lua - Requiring a LuaJIT module in a subdir is overwriting a module of the same name in the parent dir -
i have file setup this:
main.lua (requires 'mydir.b' , 'b') b.lua mydir/ b.so (luajit c module) from main, this:
function print_loaded() k, v in pairs(package.loaded) print(k, v) end end print_loaded() require 'mydir.b' print_loaded() -- include 'mydir.b' instead of 'b': local b = require 'b' the outputs of prints show call require 'mydir.b' setting return value value of package.loaded['b'] expected package.loaded['mydir.b']. wanted have package.loaded['b'] left unset can later require 'b' , not end (in opinion incorrectly) cached value mydir.b.
my question is: what's way deal this?
in case, want able copy around mydir subdir of of luajit projects, , not have worry mydir.whatever polluting module namespace destroying later requires of whatever @ parent directory level.
in anticipation of people saying, "just rename modules!" yes. can that. i'd love know if there's better solution allows me not have worry name collisions @ all.
the problem was calling lual_register incorrectly within b.so's source file (b.c).
here bad code caused problem:
static const struct lual_reg b[] = { /* set 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 stack. } the problem highlighted line set package.loaded['b'] have return value of module when it's loaded. can fixed replacing line this:
lual_register(l, "mydir.b", b); which set package.loaded['mydir.b'] instead, , leave room later use of module same name (without mydir prefix).
i didn't realize until long after asked question, when got around reading official docs lual_register lua 5.1, version luajit complies with.
Comments
Post a Comment