Reference#

LuaClassLib#

Contents of the header file luaclasslib.h.

Class Registration#

Functions supporting the creation and registration of Lua classes.

struct luaC_Class#

Contains information about a user data class.

Public Members

const char *name#

The name of the class.

const char *parent#

The name of the parent.

int user_ctor#

Whether to allow construction by calling the class object.

luaC_Constructor alloc#

The class allocator.

luaC_Destructor gc#

The class garbage collector.

const luaL_Reg *methods#

The class methods.

int luaC_classfromptr(lua_State *L)#

Obtains the Lua class table associated with the luaC_Class at the top of the stack. If the class table does not exist, it will be created.

Parameters:

L – The Lua state.

Returns:

1 if the class was successfully registered, and 0 otherwise.

void luaC_unregister(lua_State *L, const char *name)#

Removes the class with the given name from the class registry.

Parameters:
  • L – The Lua state.

  • name – The name of the class to unregister.

void luaC_setinheritcb(lua_State *L, int idx, lua_CFunction cb)#

Sets the __inherited callback on a class. When the class is derived from, the function wll be called with the parent class and derived class as its arguments.

Parameters:
  • L – The Lua state.

  • idx – The index of the class.

  • cb – The callback function.

int luaC_newclass(lua_State *L, const char *name, const char *parent, luaL_Reg *methods)#

Helper method for creating and registering a simple luaC_Class as a full userdata. Useful for when you’re using stock classes and don’t want to define your luaC_Class with static linkage.

Parameters:
  • L – The Lua state.

  • name – The class name.

  • module – The module to add the class to. Can be null.

  • parent – The parent class name. Must be in the registry. Can be null.

  • methods – The class methods.

Returns:

1 if the class was successfully created and registered, and 0 otherwise.

typedef void (*luaC_Constructor)(lua_State *L)#

A user data class constructor. Implementations of this function should push one value onto the stack, a userdata with at least one user value.

Param L:

The Lua state.

typedef void (*luaC_Destructor)(lua_State *L, void *p)#

A user data class destructor. Implementations of this function should perform any necessary resource cleanup for the user data object represented by p. Do not free p, that is handled by the Lua garbage collector.

Param L:

The Lua state.

Param p:

A pointer to the user data to destruct.

LUAC_CLASS_HEADER#

Header for luaC_Class objects.

Utility#

Utility functions for Lua classes and objects.

int luaC_construct(lua_State *L, int nargs, const char *name)#

Construct an instance of a class.

Parameters:
  • L – The Lua state.

  • nargs – The number of arguments on the stack to pass to the constructor.

  • name – The name of the class.

Returns:

1 if the object was successfully constructed, and 0 otherwise.

void luaC_super(lua_State *L, const char *name, int nargs, int nresults)#

Calls a parent class method, passing the given number of arguments from the top of the stack. Leaves the stack in its previous state. Should only be used in C class methods, in which the first stack index is the object on which the method was invoked.

Parameters:
  • L – The Lua state.

  • name – The name of the method.

  • nargs – The number of arguments to pass.

  • nresults – The number of results to return.

luaC_superinit(L)#

Calls the init function of the parent class.

Parameters:
  • L – The Lua state.

int luaC_getparentfield(lua_State *L, int idx, int depth, const char *name)#

Pushes onto the stack the value of a field at the given depth up the heirarchy. If depth is greater than the number of parents above the target object, pushes nil.

Parameters:
  • L – The Lua state.

  • idx – The index of the object in the stack.

  • depth – The parent depth.

  • name – The name of the field.

Returns:

The type of the value pushed onto the stack.

static inline void luaC_mcall(lua_State *L, const char *method, int nargs, int nresults)#

Call a method of an object, passing the object as the first argument.

Parameters:
  • L – The Lua state.

  • method – The name of the method to call.

  • nargs – The number of arguments.

  • nresults – The number of results.

static inline int luaC_pmcall(lua_State *L, const char *method, int nargs, int nresults, int msgh)#

Call a method of an object in protected mode, passing the object as the first argument.

Parameters:
  • L – The Lua state.

  • method – The name of the method to call.

  • nargs – The number of arguments.

  • nresults – The number of results.

  • msgh – The stack index of the message handler, or 0 if none is to be used.

Returns:

The pcall status code.

int luaC_pushclass(lua_State *L, const char *name)#

Pushes onto the stack the class registered under the given name.

Parameters:
  • L – The Lua state.

  • name – The fully qualified (with module prefix) class name.

Returns:

The type of the pushed value.

luaC_Class *luaC_uclass(lua_State *L, int idx)#

Returns a pointer to the user data class associated with the class at the given stack index.

Parameters:
  • L – The Lua state.

  • idx – The stack index of the class.

Returns:

A pointer to the user data class, or NULL if none was found.

luaC_getclass(L, i)#

Pushes onto the stack the class table of the object at the given stack index.

Parameters:
  • L – The Lua state.

  • i – The index of the object on the stack.

Returns:

1 if the class was fetched successfully, and 0 otherwise.

luaC_getbase(L, i)#

Pushes onto the stack the base table of the class at the given stack index.

Parameters:
  • L – The Lua state.

  • i – The index of the class on the stack.

Returns:

1 if the base was fetched successfully, and 0 otherwise.

luaC_getparent(L, i)#

Pushes onto the stack the parent class table of the class at the given stack index.

Parameters:
  • L – The Lua state.

  • i – The index of the class on the stack.

Returns:

1 if the parent was fetched successfully, and 0 otherwise.

luaC_getname(L, i)#

Pushes onto the stack the name of the class at the given stack index.

Parameters:
  • L – The Lua state.

  • i – The index of the class on the stack.

Returns:

1 if the parent was fetched successfully, and 0 otherwise.

static inline const char *luaC_typename(lua_State *L, int idx)#

Improved typename function.

Parameters:
  • L – The Lua state.

  • idx – The index of the object on the stack.

Returns:

If the object belongs to a class, returns the classname. Otherwise, returns the regular typename.

static inline int luaC_rawget(lua_State *L, int idx)#

Improved rawget. Works with user data classes.

Parameters:
  • L – The Lua state.

  • idx – The index of the object on the stack.

Returns:

The type of the value pushed onto the stack.

static inline void luaC_rawset(lua_State *L, int idx)#

Improved rawset. Works with user data classes.

Parameters:
  • L – The Lua state.

  • idx – The index of the object on the stack.

static inline int luaC_getpackageloaded(lua_State *L, const char *key)#

Pushes onto the stack the value package.loaded[key].

Parameters:
  • L – The Lua state.

  • key – The key to get.

Returns:

The type of the value pushed onto the stack.

static inline void luaC_setpackageloaded(lua_State *L, const char *key)#

Does the equivalent of package.loaded[key] = v, where v is the value at the top of the stack.

Parameters:
  • L – The Lua state.

  • key – The key to set.

Introspection#

Functions providing introspection into Lua classes and objects.

int luaC_isclass(lua_State *L, int idx)#

Checks if the value at the given index is a class.

Parameters:
  • L – The Lua state.

  • idx – The stack index to check.

Returns:

1 if the value is a class, and 0 otherwise.

int luaC_isobject(lua_State *L, int idx)#

Checks if the value at the given index is an instance of a class.

Parameters:
  • L – The Lua state.

  • idx – The stack index to check.

Returns:

1 if the value is an instance of a class, and 0 otherwise.

int luaC_isinstance(lua_State *L, int arg, const char *name)#

Checks if the value at the given index is an instance of the class named name.

Parameters:
  • L – The Lua state.

  • arg – The stack index to check.

  • name – The name of the class.

Returns:

1 if the value is an instance of name, and 0 otherwise.

void *luaC_checkuclass(lua_State *L, int arg, const char *name)#

Checks if the function argument arg is an instance of the userdata class named name and returns the userdata’s memory-block address.

Parameters:
  • L – The Lua state.

  • arg – The arg to check.

  • name – The name of the class.

Returns:

A pointer to the userdata.

Method Injection#

Functions for overriding class methods.

int luaC_injectmethod(lua_State *L, int idx, const char *method, lua_CFunction f)#

Replaces a class method with a closure of the given C function f, with the previous method as its only upvalue.

Parameters:
  • L – The Lua state.

  • idx – The index of the class object.

  • method – The method to replace.

  • f – The C function to replace the method with.

Returns:

1 if the operation was successful, and 0 otherwise.

luaC_injectindex(L, i, f)#

Replaces the index method of a class with a closure of the given C function f, with the previous index as its only upvalue.

Parameters:
  • L – The Lua state.

  • i – The index of the class object.

  • f – The C function to replace the method with.

Returns:

1 if the operation was successful, and 0 otherwise.

luaC_injectnewindex(L, i, f)#

Replaces the newindex method of a class with a closure of the given C function f, with the previous newindex as its only upvalue.

Parameters:
  • L – The Lua state.

  • i – The index of the class object.

  • f – The C function to replace the method with.

Returns:

1 if the operation was successful, and 0 otherwise.

int luaC_deferindex(lua_State *L)#

When called from an injected index function, calls (or indexes) the original index and pushes the result onto the stack.

Parameters:

L – The Lua state.

Returns:

The type of the value pushed onto the stack.

void luaC_defernewindex(lua_State *L)#

When called from an injected newindex function, calls the original newindex if it exists.

Parameters:

L – The Lua state.

User Value Access#

Functions allowing access to tables stored in the user values of a userdata.

static inline int luaC_uvget(lua_State *L, int idx, int uv)#

Pushes onto the stack the value t[k] where t is the table stored in the given user value of the userdata at the given index, and k is the value at the top of the stack.

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

Returns:

The type of the pushed value.

static inline int luaC_uvset(lua_State *L, int idx, int uv)#

Does the equivalent of t[k] = v, where t is the table stored in the given user value of the userdata at the given index, v is the value on top of the stack, and k is the value just below the top.

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

Returns:

1 if the operation was successful, and 0 otherwise.

static inline int luaC_getuvfield(lua_State *L, int idx, int uv, const char *k)#

Pushes onto the stack the value t[k] where t is the table stored in the given user value of the userdata at the given index.

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

  • k – The name of the field.

Returns:

The type of the pushed value.

static inline int luaC_setuvfield(lua_State *L, int idx, int uv, const char *k)#

Does the equivalent of t[k] = v, where t is the table stored in the given user value of the userdata at the given index, and v is the value on top of the stack.

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

  • k – The name of the field.

Returns:

1 if the operation was successful, and 0 otherwise.

static inline int luaC_uvrawget(lua_State *L, int idx, int uv)#

Pushes onto the stack the value t[k] where t is the table stored in the given user value of the userdata at the given index, and k is the value at the top of the stack. The access is raw (does not use the __index metavalue).

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

Returns:

The type of the pushed value.

static inline int luaC_uvrawset(lua_State *L, int idx, int uv)#

Does the equivalent of t[k] = v, where t is the table stored in the given user value of the userdata at the given index, v is the value on top of the stack, and k is the value just below the top. The access is raw (does not use the __index metavalue).

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

Returns:

1 if the operation was successful, and 0 otherwise.

static inline int luaC_uvrawgetp(lua_State *L, int idx, int uv, const void *p)#

Pushes onto the stack the value t[k] where t is the table stored in the given user value of the userdata at the given index, and k is the pointer p represented as a light userdata. The access is raw (does not use the __index metavalue).

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

  • p – The pointer to use as a key.

Returns:

The type of the pushed value.

static inline int luaC_uvrawsetp(lua_State *L, int idx, int uv, const void *p)#

Does the equivalent of t[k] = v, where t is the table stored in the given user value of the userdata at the given index, v is the value on top of the stack, and k is the pointer p represented as a light userdata. The access is raw (does not use the __index metavalue).

Parameters:
  • L – The Lua state.

  • idx – The index of the userdata.

  • uv – The user value to access.

  • p – The pointer to use as a key.

Returns:

1 if the operation was successful, and 0 otherwise.

MoonAuxLib#

Contents of the header file moonauxlib.h.

Compiler API#

Functions from the Moonscript Compiler API.

static inline int moonL_loadstring(lua_State *L, const char *str)#

Loads a Moonscript string as a Lua chunk.

Parameters:
  • L – The Lua state.

  • str – The Moonscript string to load.

Returns:

The Lua pcall status code.

static inline int moonL_loadfile(lua_State *L, const char *name)#

Loads a Moonscript file as a Lua chunk.

Parameters:
  • L – The Lua state.

  • name – The name of the file to load.

Returns:

The Lua pcall status code.

static inline int moonL_dofile(lua_State *L, const char *name)#

Loads a Moonscript file as a Lua chunk, then runs the returned chunk.

Parameters:
  • L – The Lua state.

  • name – The name of the file to load.

Returns:

The Lua pcall status code.

Moonscript Library#

Functions from the Moonscript Standard Library.

static inline int moonL_print(lua_State *L, int index)#

Prints a formatted version of an object.

Parameters:
  • L – The Lua state.

  • index – The index of the object to print.

Returns:

The Lua pcall status code.

Lua Library#

Functions provided by LCL to Lua code.

void luaC_overrideglobals(lua_State *L)#

Overrides the rawget, rawset, and type functions with additional functionality.

Parameters:

L – The Lua state.

lcl.uvget(obj, [uv, ]idx)#

Gets the value of t[idx], where t is the table stored in the user value uv of obj. If no user value is specified, uses the first user value. Uses the __index metamethod.

Parameters:
  • obj – The object.

  • uv[optional] The user value to access.

  • idx – The index to get.

lcl.uvset(obj, [uv, ]idx, value)#

Sets the value of t[idx] to value, where t is the table stored in the user value uv of obj. If no user value is specified, uses the first user value. Uses the __newindex metamethod.

Parameters:
  • obj – The object.

  • uv[optional] The user value to access.

  • idx – The index to set.

  • value – The value.

lcl.rawget(obj, idx)#

If obj is a table, gets the value of obj[idx]. If obj is a userdata, gets the value of t[idx], where t is the table stored in the first user value of obj. Does not use the __index metamethod.

Replaces the default version of this function when luaC_overrideglobals() is called.

Parameters:
  • obj – The object.

  • idx – The index to get.

lcl.rawset(obj, idx, value)#

If obj is a table, sets the value of obj[idx] to value. If obj is a userdata, sets the value of t[idx] to value, where t is the table stored in the first user value of obj. Does not use the __newindex metamethod.

Replaces the default version of this function when luaC_overrideglobals() is called.

Parameters:
  • obj – The object.

  • idx – The index to set.

  • value – The value.

lcl.type(obj)#

If obj is an instance of a named class, returns the name of the class it belongs to. If obj is a class, returns “class”. Otherwise, returns the standard Lua typename.

Replaces the default version of this function when luaC_overrideglobals() is called.

Parameters:

obj – The object.