This module provides a few C++ utilities wrapping the Lua API.
#include <fblualib/LuaUtils.h>
The module provides a series of functions named (and organized) as follows: for a type Foo (see below as to what types are allowed), we have:
luaGetFoo(lua_State* L, int index)
returns afolly::Optional<Foo>
, which is set (non-empty) if the element on the stack at the given index can be converted to a Foo and empty otherwiseluaGetFooChecked(lua_State* L, int index)
returns aFoo
if the element on the stack at the given index can be converted to a Foo, and raises a Lua error otherwiseluaGetFieldIfFoo(lua_State* L, int index, const char* field)
behaves similarly toluaGetFoo
, but retrieves the data from a string-keyed field in a Lua table at the given indexluaGetFieldIfFooChecked(lua_State* L, int index, const char* field)
behaves similarly toluaGetFooChecked
, but retrieves a field as above
Possible types (values of Foo
) are:
String
; the value type isfolly::StringPiece
Number<T>
, templated by a numeric type; the value type isT
(soluaGetNumber<float>(L, index)
, etc)Tensor<T>
, templated by a numeric type; the value type isthpp::Tensor<T>
(soluaGetTensor<float>(L, index)
, etc)
luaGetString*
and luaGetNumber*
support an extra bool strict
argument
(default false
) indicating whether the data must be of the exact expected
type (strict = true
) or may be converted (strict = false
); with
strict = false
, we will convert between numbers and strings, and will
round floating-point values if integers are requested; with strict = true
,
we won't.
There is also luaPushTensor(lua_Stte* L, thpp::Tensor<T> tensor)
which
pushes a tensor on the stack (use the lua_*
or luaL_*
functions
from the Lua standard library for other types).