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 Patrick Mc(avery once stated:
> Hi Sean
> 
> I think I understand your approach now but it is still challenging for 
> me. Do you have any alternative approaches to C modules?

  I'm not really sure what you are asking here.  The scheme I have is pretty
simple and it isn't hard to do in either Lua or C modules.  For Lua,
depending on how lazy I am, I'll either do:

	local pairs    = pairs
	local math     = math
	local tostring = tostring
	local string   = string
	local type     = type
	local print    = print

	module("org.conman.table")

	function show(l) ... 

which means I can do the metatable trick I showed earlier, or 

	module("org.conman.string",package.seeall)

which makes the metatable trick fail (since the table org.conman.string will
already have a metatable pointing to the global environment).  If anything,
it's less of an issue with C modules---here, for instance is the exported
function for a simple POSIX-like interface I wrote (while learning how to
extend Lua):

int luaopen_org_conman_fsys(lua_State *L)
{
  luaL_register(L,"org.conman.fsys",reg_fsys);
  
  lua_pushliteral(L,"_COPYRIGHT");
  lua_pushliteral(L,"Copyright 2010 by Sean Conner.  All Rights Reserved.");
  lua_settable(L,-3);
  
  lua_pushliteral(L,"_DESCRIPTION");
  lua_pushliteral(L,"Useful file manipulation functions available under Unix.");
  lua_settable(L,-3);
  
  lua_pushliteral(L,"_VERSION");
  lua_pushliteral(L,"0.2.5");
  lua_settable(L,-3);
  return 1;
}

It also doesn't hurt that I made sure that the directory
"/usr/local/lib/lua/5.1/org/conman/" exists on my system and that I make
sure to copy my own extensions there as I make them.

> I reread several threads today concerning require and dofile. I am more 
> confused then ever though. Is it safe to say that running code from 
> multiple source files is one of the more controversial parts of this 
> language?

  No, but it does require a bit of playing around with require and dofile to
learn the differences.  

  -spc