Config-UCL
view release on metacpan or search on metacpan
libucl-0.8.1/lua/lua_ucl.c view on Meta::CPAN
}
return 1;
}
/**
* Push a simple object to lua depending on its actual type
*/
static int
ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj,
bool allow_array)
{
struct ucl_lua_funcdata *fd;
if (allow_array && obj->next != NULL) {
/* Actually we need to push this as an array */
return ucl_object_lua_push_array (L, obj);
}
switch (obj->type) {
case UCL_BOOLEAN:
lua_pushboolean (L, ucl_obj_toboolean (obj));
break;
case UCL_STRING:
lua_pushstring (L, ucl_obj_tostring (obj));
break;
case UCL_INT:
#if LUA_VERSION_NUM >= 501
lua_pushinteger (L, ucl_obj_toint (obj));
#else
lua_pushnumber (L, ucl_obj_toint (obj));
#endif
break;
case UCL_FLOAT:
case UCL_TIME:
lua_pushnumber (L, ucl_obj_todouble (obj));
break;
case UCL_NULL:
lua_getfield (L, LUA_REGISTRYINDEX, "ucl.null");
break;
case UCL_USERDATA:
fd = (struct ucl_lua_funcdata *)obj->value.ud;
lua_rawgeti (L, LUA_REGISTRYINDEX, fd->idx);
break;
default:
lua_pushnil (L);
break;
}
return 1;
}
/***
* @function ucl_object_push_lua(L, obj, allow_array)
* This is a `C` function to push `UCL` object as lua variable. This function
* converts `obj` to lua representation using the following conversions:
*
* - *scalar* values are directly presented by lua objects
* - *userdata* values are converted to lua function objects using `LUA_REGISTRYINDEX`,
* this can be used to pass functions from lua to c and vice-versa
* - *arrays* are converted to lua tables with numeric indicies suitable for `ipairs` iterations
* - *objects* are converted to lua tables with string indicies
* @param {lua_State} L lua state pointer
* @param {ucl_object_t} obj object to push
* @param {bool} allow_array expand implicit arrays (should be true for all but partial arrays)
* @return {int} `1` if an object is pushed to lua
*/
int
ucl_object_push_lua (lua_State *L, const ucl_object_t *obj, bool allow_array)
{
switch (obj->type) {
case UCL_OBJECT:
return ucl_object_lua_push_object (L, obj, allow_array);
case UCL_ARRAY:
return ucl_object_lua_push_array (L, obj);
default:
return ucl_object_lua_push_scalar (L, obj, allow_array);
}
}
/**
* Parse lua table into object top
* @param L
* @param top
* @param idx
*/
static ucl_object_t *
ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
{
ucl_object_t *obj, *top = NULL;
size_t keylen;
const char *k;
bool is_array = true, is_implicit = false, found_mt = false;
size_t max = 0, nelts = 0;
if (idx < 0) {
/* For negative indicies we want to invert them */
idx = lua_gettop (L) + idx + 1;
}
/* First, we check from metatable */
if (luaL_getmetafield (L, idx, "class") != 0) {
if (lua_type (L, -1) == LUA_TSTRING) {
const char *classname = lua_tostring (L, -1);
if (strcmp (classname, UCL_OBJECT_TYPE_META) == 0) {
is_array = false;
found_mt = true;
} else if (strcmp (classname, UCL_ARRAY_TYPE_META) == 0) {
is_array = true;
found_mt = true;
#if LUA_VERSION_NUM >= 502
max = lua_rawlen (L, idx);
#else
max = lua_objlen (L, idx);
#endif
nelts = max;
} else if (strcmp (classname, UCL_IMPL_ARRAY_TYPE_META) == 0) {
is_array = true;
is_implicit = true;
( run in 0.560 second using v1.01-cache-2.11-cpan-96521ef73a4 )