LuaD

This internal module, with the help of the luad.conversions package, takes care of converting between D and Lua types.

The conversion rules are as follows, where conversion goes both ways:

boolean
bool
number
lua_Integer (default int)
lua_Number (default double)
string
string, const(char)[], char[]
const(char)*
char
immutable(void)[], const(void)[], void[] (binary data)
table
associative arrays (see luad.conversions.assocarrays)
arrays (see luad.conversions.arrays)
structs (see luad.conversions.structs)
luad.table.LuaTable
function (see luad.conversions.functions)
function pointers
delegates
luad.lfunction.LuaFunction
userdata
classes (see luad.conversions.classes)
nil
the special identifier nil
null class references
any of the above
luad.base.LuaObject
luad.dynamic.LuaDynamic
Algebraic, when given a compatible value (see luad.conversions.variant)


The conversions are checked in the specified order. For example, even though bool is implicitly convertible to lua_Integer, it will be converted to a boolean because boolean has precedence.

wchar and dchar are explicitly disallowed. Lua strings consist of 8-bit characters, if you want to push UTF-16 or UTF-32 strings, convert to UTF-8 first.

Additionally, the following types are pushable to Lua, but can't be retrieved back:
function
lua_CFunction


void  pushValue(T)(lua_State* L, T value);

Push a value of any type to the stack.

Parameters
lua_State* L stack to push to
T value value to push

template  luaTypeOf(T)

Get the associated Lua type for T.

Returns
Lua type for T

T  getValue(T, alias typeMismatchHandler = defaultTypeMismatch)(lua_State* L, int idx);

Get a value of any type from the stack.

Parameters
T type of value
typeMismatchHandler function called to produce an error in case of an invalid conversion.
lua_State* L stack to get from
int idx value stack index

T  popValue(T, alias typeMismatchHandler = defaultTypeMismatch)(lua_State* L);

Same as calling getValue!(T, typeMismatchHandler)(L, -1), then popping one value from the stack.

See Also
getValue

T[]  popStack(T = LuaObject)(lua_State* L, size_t n);

Pop a number of elements from the stack.

Parameters
T element type
lua_State* L stack to pop from
size_t n number of elements to pop
Returns
array of popped elements, or a null array if n = 0

auto  getArgument(T, int narg)(lua_State* L, int idx);

Get a function argument from the stack.


template  returnTypeSize(T)

Used for getting a suitable nresults argument to lua_call or lua_pcall.


T  popReturnValues(T)(lua_State* L, size_t nret);

Pop return values from stack. Defaults to popValue, but has special handling for luad.conversions.functions.LuaVariableReturn, std.typecons.Tuple, static arrays and void.

Parameters
size_t nret number of return values
Returns
Return value, collection of return values, or nothing

int  pushReturnValues(T)(lua_State* L, T value);

Push return values to the stack. Defaults to pushValue, but has special handling for luad.conversions.functions.LuaVariableReturn, std.typecons.Tuple and static arrays.


T  popTuple(T)(lua_State* L) if (isTuple!T);

Pops a std.typecons.Tuple from the values at the top of the stack.


void  pushTuple(T)(lua_State* L, ref T tup) if (isTuple!T);

Pushes all the values in a std.typecons.Tuple to the stack.


T  callWithRet(T)(lua_State* L, int nargs);

Call a Lua function and handle its return values.

Parameters
T type of return value or container of return values
int nargs number of arguments
Returns
Zero, one or all return values as T, taking into account void, luad.conversions.functions.LuaVariableReturn and std.typecons.Tuple returns

void  printStack(lua_State* L);

Print the Lua stack to stdout.