* If there is any error loading or running the module, or if it cannot find any loader for
* the module, then require signals an error.
*/
public LuaValue require( LuaValue arg ) {
LuaString name = arg.checkstring();
LuaValue loaded = LOADED.get(name);
if ( loaded.toboolean() ) {
if ( loaded == _SENTINEL )
error("loop or previous error loading module '"+name+"'");
return loaded;
}
/* else must load it; iterate over available loaders */
LuaTable tbl = PACKAGE.get(_LOADERS).checktable();
StringBuffer sb = new StringBuffer();
LuaValue chunk = null;
for ( int i=1; true; i++ ) {
LuaValue loader = tbl.get(i);
if ( loader.isnil() ) {
error( "module '"+name+"' not found: "+name+sb );
}
/* call loader with module name as argument */
chunk = loader.call(name);
if ( chunk.isfunction() )
break;
if ( chunk.isstring() )
sb.append( chunk.tojstring() );
}
// load the module using the loader
LOADED.set(name, _SENTINEL);
LuaValue result = chunk.call(name);
if ( ! result.isnil() )
LOADED.set( name, result );
else if ( (result = LOADED.get(name)) == _SENTINEL )
LOADED.set( name, result = LuaValue.TRUE );
return result;
}