Moai Cloud Lua Environment

UPDATED: 8/26/11 More up to date information on the Moai Cloud Lua Environment can be found on our wiki.

The basic Lua runtime environment available to Moai Cloud Services (below) is similar to the standard Lua runtime environment, with some functionality removed for security and stability purposes. Information on how to use each of these functions can be found in the Lua 5.1 Reference Manual, Lua JSON and Lua MD5. In addition to the standard functions defined below, the Moai Cloud Lua environment also provides for some Moai Cloud specific variables and functions used for loading additional code, managing file and image resources and mongoDB database access.

assert=assert,
error=error,
ipairs=ipairs,
loadstring=loadstring,
next=next,
pairs=pairs,
pcall=pcall,
select=select,
tonumber=tonumber,
tostring=tostring,
type=type,
unpack=unpack,
xpcall=xpcall,
coroutine={
       create=coroutine.create,
       resume=coroutine.resume,
       running=coroutine.running,
       status=coroutine.status,
       wrap=coroutine.wrap,
       yield=coroutine.yield,
},
table={
       concat=table.concat,
       insert=table.insert,
       maxn=table.maxn,
       getn=table.getn,
       setn=table.setn,
       remove=table.remove,
       sort=table.sort,
},
string={
       byte=string.byte,
       char=string.char,
       find=string.find,
       format=string.format,
       gmatch=string.gmatch,
       gsub=string.gsub,
       len=string.len,
       lower=string.lower,
       match=string.match,
       rep=string.rep,
       reverse=string.reverse,
       sub=string.sub,
       upper=string.upper,
},
math={
       abs=math.abs,
       acos=math.acos,
       asin=math.asin,
       atan=math.atan,
       atan2=math.atan2,
       ceil=math.ceil,
       cos=math.cos,
       cosh=math.cosh,
       deg=math.deg,
       exp=math.exp,
       floor=math.floor,
       fmod=math.fmod,
       frexp=math.frexp,
       huge=math.huge,
       ldexp=math.ldexp,
       log=math.log,
       log10=math.log10,
       max=math.max,
       min=math.min,
       modf=math.modf,
       pi=math.pi,
       pow=math.pow,
       rad=math.rad,
       random=math.random,
       randomseed=math.randomseed,
       sin=math.sin,
       sinh=math.sinh,
       sqrt=math.sqrt,
       tan=math.tan,
       tanh=math.tanh,
},
os={
       clock=os.clock,
       date=os.date,
       difftime=os.difftime,
       time=os.time,
},
md5={
       crypt=md5.cript,
       decrypt=md5.decrypt,
       exor=md5.exor,
       sum=md5.sum,
       sumhexa=md5.sumhexa,
},
json={
       encode=json.encode,
       isEncodable=json.isEncodable,
       decode=json.decode,
       util=json.util,
},
moai={
       getfile=moai.getfile,
       loadsource=moai.loadsource,
       log=moai.log,
       table_print=moai.table_print,
       userid=moai.userid,
},
mongodb={
       count=mongodb.count,
       insert=mongodb.insert,
       insert_batch=mongodb.insert_batch,
       query=mongodb.query,
       remove=mongodb.remove,
       update=mongodb.update,
},
curl=curl

Moai Cloud Lua Environment Extensions

In addition to the standard Lua environment functions exposed to Moai Cloud applications, some additional cloud-specific items are included in the Lua environment. All additions to the standard Lua environment exist under an environment variable "moai". moai.appid - the UUID of the current service moai.getfile(filename)- Load a file from the GridFS filesystem and return its contents. This is useful for getting the contents of image files stored in the GridFS and returning them to the calling client. Note: moai.getfile can only return files that were originally uploaded with your source ZIP file at service-creation time.

  • filename - (string) the name of a Lua source file included in the ZIP file that was uploaded when you created your serivce. Note: Filenames can include path info but may not include a leading slash '/'. All files are loaded using paths relative to the GridFS root for the current serivce.

moai.loadsource(filename) - Load a Lua source file from the gridfs filesystem and add it to the current Lua environment. Note: moai.getfile can only return files that were originally uploaded with your source ZIP file at service-creation time.

  • filename - (string) the name of a Lua source file included in the ZIP file that was uploaded when you created your service. Note: Filenames can include path info but may not include a leading slash '/'. All files are loaded using paths relative to the GridFS root for the current service.

moai.log(msg, level)- Creates a log file entry for your application. The Moai Cloud internal logger provides for a rolling log of 1000 messages of up to 1000 characters each. You can view the log output from the Moai Cloud dashboard. Note, all log messages across all of your services will be written to a common log file.

  • msg - (string) - the message to log
  • level - (string) - a string indicating the log level (e.g. DEBUG, INFO, WARN, ERROR, FATAL, etc.)

mongodb - An initialized and authenticated mongodb connection object. see: Using mongoDB with Moai Cloud, moai.loadsource, moai.getfile and luamongo. moai.table_print(table)- Returns an HTML formatted string representation of the specified Lua table.

  • table - (Lua table) - the table to stringify.

moai.userid - your zipline id