-------------------------------------------------------------------------------- -- complain[ln] writes the given objects onto stderr -- -------------------------------------------------------------------------------- function complain (...) for i = 1,arg.n do io.stderr:write(tostring(arg[i])); end; end; function complainln (...) for i = 1,arg.n do io.stderr:write(tostring(arg[i])); end; io.stderr:write("\n"); end; -------------------------------------------------------------------------------- -- cry[ln] synonyms for complain[ln] (for historical reasons) -- -------------------------------------------------------------------------------- cry = complain; cryln = complainln; -------------------------------------------------------------------------------- -- exit leaves the script and it's (Java) environment -- -------------------------------------------------------------------------------- local SystemClass = luajava.bindClass("java.lang.System"); function exit (ExitCode) if (ExitCode == nil) then SystemClass:exit(0); end; local realExitCode = tonumber(ExitCode); if (realExitCode == nil) then error("non-numeric \"ExitCode\" given: \""..ExitCode.."\""); -- not fool-proof! else SystemClass:exit(realExitCode); end; end; -------------------------------------------------------------------------------- -- print[ln] writes the given objects onto stdout -- -------------------------------------------------------------------------------- function print (...) for i = 1,arg.n do io.stdout:write(tostring(arg[i])); end; end; function println (...) for i = 1,arg.n do io.stdout:write(tostring(arg[i])); end; io.stdout:write("\n"); end; -------------------------------------------------------------------------------- -- require an updated version of the intrinsic "require" function -- -------------------------------------------------------------------------------- local _require = require; -- preserve the original function function require (PackageName) -- and define a better one if (PackageName == nil) then error("missingArgument: no \"PackageName\" given"); end; if (type(PackageName) ~= "string") then error("illegalArgument: given \"PackageName\" is not of type \"string\""); end; PackageName = string._trim(PackageName); -- normalize the package name a bit if (PackageName == "") then error("emptyArgument: given \"PackageName\" is empty"); end; if (io.FolderSeparator ~= "/") then PackageName = string.gsub(PackageName, "/", io.FolderSeparator); end; --**** check if that package is currently being loaded **** if (_LOADING ~= nil) and (type(_LOADING) == "table") and _LOADING[PackageName] then return true; -- avoid recursive loading of the requested package else if (_LOADING == nil) or (type(_LOADING) ~= "table") then _LOADING = {[PackageName] = true}; -- mark package as being loaded else _LOADING[PackageName] = true; end; _require(PackageName); if (_LOADING ~= nil) and (type(_LOADING) == "table") then _LOADING[PackageName] = nil; -- unmark package end; end; end; -------------------------------------------------------------------------------- -- say[ln] synonyms for print[ln] (for historical reasons) -- -------------------------------------------------------------------------------- say = print; sayln = println; -------------------------------------------------------------------------------- -- io.getFolderSeparator determines the platform-specific separator character -- -------------------------------------------------------------------------------- local FileClass = luajava.bindClass("java.io.File"); function io.getFolderSeparator () return FileClass.separator; end; io.FolderSeparator = io.getFolderSeparator(); -- shortcut for direct access -------------------------------------------------------------------------------- -- string._trim yields a copy of "Argument" without leading/trailing blanks -- -------------------------------------------------------------------------------- function string._trim (Argument) return string.gsub(string.gsub(Argument, "^%s+", ""), "%s+$", ""); end;