lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Coda Highland once stated:
> On Sat, Apr 20, 2019 at 5:33 PM Sean Conner <sean@conman.org> wrote:
> > It was thus said that the Great Coda Highland once stated:
> >
> > > the import name always matches the directory containing the module. The
> >
> >   Again, pretty much the same as Lua.
> 
> Pretty much, but not entirely. The fact that some modules don't do this is
> one of the reasons this thread has come up at all.

  That's more an issue for Lua 5.1 than 5.2+.
> 
> > > import scheme also does aggressive namespacing so you have to go out of
> > > your way to do stuff that might screw up some other module's transitive
> > > dependencies.
> >
> >   Okay, so how does *this* work then?
> 
> Mostly, ironclad module scoping. Each file has its own global scope, and

  ...

> Duplicated dependencies don't necessarily even get mapped onto each other.
> If they're not the same module (with compatibility determined by the
> importer's explicitly-requested versioning information) then you can
> actually end up with two copies of the module loaded in, and since they're
> different modules with isolated namespaces they don't interfere with each
> other. It's not the BEST solution to dependency hell because it leads to
> bloating and bugfixes that don't get applied consistently, but it does mean
> that a transitive dependency is a little less likely to screw you over.

  So basically, version numbers are checked when loading modules.  I thought
Lua could do this for C-based modules.  From the manual:

	package.config

		A string describing some compile-time configurations for
		packages. This string is a sequence of lines:

			...
			
			The fifth line is a mark to ignore all text after it
			when building the luaopen_ function name. Default is '-'.

		...

		The name of this C function is the string "luaopen_"
		concatenated with a copy of the module name where each dot
		is replaced by an underscore.  Moreover, if the module name
		has a hyphen, its suffix after (and including) the first
		hyphen is removed. For instance, if the module name is
		a.b.c-v2.1, the function name will be luaopen_a_b_c.

So in trying this, I created the following:

prog.lua

	local a = require "a"
	local b = require "b"

	a.foo()
	b.foo()

a.lua

	local mod = require "mod-1.2.3"

	return {
	  foo = function()
	    print('This is module a')
	    mod.foo()
	  end
	}

b.lua

	local mod = require "mod-2.0.1"
	
	return {
	  foo = function()
	    print('This is module b')
	    mod.foo()
	  end
	}

mod-v1.2.3.c

	#include <lua.h>
	
	static int foo(lua_State *L)
	{
	  lua_getglobal(L,"print");
	  lua_pushliteral(L,"This is module mod-1.2.3");
	  lua_call(L,1,0);
	  return 0;
	}
	
	int luaopen_mod(lua_State *L)
	{
	  lua_createtable(L,0,1);
	  lua_pushcfunction(L,foo);
	  lua_setfield(L,-2,"foo");
	  return 1;
	}

mod-2.0.1.c

	#include <lua.h>
	
	static int foo(lua_State *L)
	{
	  lua_getglobal(L,"print");
	  lua_pushliteral(L,"This is module mod-2.0.1");
	  lua_call(L,1,0);
	  return 0;
	}
	
	int luaopen_mod(lua_State *L)
	{
	  lua_createtable(L,0,1);
	  lua_pushcfunction(L,foo);
	  lua_setfield(L,-2,"foo");
	  return 1;
	}

What I expected to see was:

	This is module a
	This is module mod-1.2.3
	This is module b
	This is module mod-2.0.1

But what I got was:

lua: ./a.lua:2: module 'mod-v1.2.3' not found:
        no field package.preload['mod-v1.2.3']
        no file '/home/spc/.luarocks/share/lua/5.3/mod-v1/2/3.lua'
        no file '/home/spc/.luarocks/share/lua/5.3/mod-v1/2/3/init.lua'
        no file '/home/spc/.luarocks/lib/lua/5.3/mod-v1/2/3.lua'
        no file '/home/spc/.luarocks/lib/lua/5.3/mod-v1/2/3/init.lua'
        no file '/usr/local/share/lua/5.3/mod-v1/2/3.lua'
        no file '/usr/local/share/lua/5.3/mod-v1/2/3/init.lua'
        no file '/usr/local/lib/lua/5.3/mod-v1/2/3.lua'
        no file '/usr/local/lib/lua/5.3/mod-v1/2/3/init.lua'
        no file './mod-v1/2/3.lua'
        no file './mod-v1/2/3/init.lua'
        no file '/home/spc/.luarocks/lib/lua/5.3/mod-v1/2/3.so'
        no file '/usr/local/lib/lua/5.3/mod-v1/2/3.so'
        no file '/usr/local/lib/lua/5.3/loadall.so'
        no file './mod-v1/2/3.so'
        no file '/home/spc/.luarocks/lib/lua/5.3/mod-v1.so'
        no file '/usr/local/lib/lua/5.3/mod-v1.so'
        no file '/usr/local/lib/lua/5.3/loadall.so'
        no file './mod-v1.so'
stack traceback:
        [C]: in function 'require'
        ./a.lua:2: in main chunk
        [C]: in function 'require'
        prog.lua:3: in main chunk
        [C]: in ?

  Am I doing something wrong?  Is Lua doing something wrong?  You can tell I
was confused because one module is "mod-v1.2.3" and the other one is
"mod-2.0.1".  In thinking about this further, even if this did work, it
wouldn't necessarily work on all systems for the same reason the exported
function from C modules isn't just "luaopen()" (because some (IMHO)
braindead systems can't deal with loading dynanic linked object with the
same exports).

  -spc (Confused by all this)