Config-UCL
view release on metacpan or search on metacpan
libucl-0.8.1/lua/lua_ucl.c view on Meta::CPAN
if not res then
print('parser error: ' .. err)
else
local obj = parser:get_object()
local got = ucl.to_format(obj, 'json')
endif
local table = {
str = 'value',
num = 100500,
null = ucl.null,
func = function ()
return 'huh'
end
}
print(ucl.to_format(table, 'ucl'))
-- Output:
--[[
num = 100500;
str = "value";
null = null;
func = "huh";
--]]
*/
#define PARSER_META "ucl.parser.meta"
#define EMITTER_META "ucl.emitter.meta"
#define NULL_META "ucl.null.meta"
#define OBJECT_META "ucl.object.meta"
#define UCL_OBJECT_TYPE_META "ucl.type.object"
#define UCL_ARRAY_TYPE_META "ucl.type.array"
#define UCL_IMPL_ARRAY_TYPE_META "ucl.type.impl_array"
static int ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj);
static int ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, bool allow_array);
static ucl_object_t* ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags);
static ucl_object_t* ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags);
static void *ucl_null;
/**
* Push a single element of an object to lua
* @param L
* @param key
* @param obj
*/
static void
ucl_object_lua_push_element (lua_State *L, const char *key,
const ucl_object_t *obj)
{
lua_pushstring (L, key);
ucl_object_push_lua (L, obj, true);
lua_settable (L, -3);
}
static void
lua_ucl_userdata_dtor (void *ud)
{
struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
luaL_unref (fd->L, LUA_REGISTRYINDEX, fd->idx);
if (fd->ret != NULL) {
free (fd->ret);
}
free (fd);
}
static const char *
lua_ucl_userdata_emitter (void *ud)
{
struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
const char *out = "";
lua_rawgeti (fd->L, LUA_REGISTRYINDEX, fd->idx);
lua_pcall (fd->L, 0, 1, 0);
out = lua_tostring (fd->L, -1);
if (out != NULL) {
/* We need to store temporary string in a more appropriate place */
if (fd->ret) {
free (fd->ret);
}
fd->ret = strdup (out);
}
lua_settop (fd->L, 0);
return fd->ret;
}
/**
* Push a single object to lua
* @param L
* @param obj
* @return
*/
static int
ucl_object_lua_push_object (lua_State *L, const ucl_object_t *obj,
bool allow_array)
{
const ucl_object_t *cur;
ucl_object_iter_t it = NULL;
if (allow_array && obj->next != NULL) {
/* Actually we need to push this as an array */
return ucl_object_lua_push_array (L, obj);
}
lua_createtable (L, 0, obj->len);
it = NULL;
while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) {
ucl_object_lua_push_element (L, ucl_object_key (cur), cur);
}
luaL_getmetatable (L, UCL_OBJECT_TYPE_META);
lua_setmetatable (L, -2);
return 1;
}
/**
* Push an array to lua as table indexed by integers
* @param L
* @param obj
* @return
*/
static int
ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj)
{
const ucl_object_t *cur;
ucl_object_iter_t it;
int i = 1, nelt = 0;
if (obj->type == UCL_ARRAY) {
nelt = obj->len;
it = ucl_object_iterate_new (obj);
lua_createtable (L, nelt, 0);
while ((cur = ucl_object_iterate_safe (it, true))) {
ucl_object_push_lua (L, cur, false);
lua_rawseti (L, -2, i);
i ++;
}
luaL_getmetatable (L, UCL_ARRAY_TYPE_META);
lua_setmetatable (L, -2);
ucl_object_iterate_free (it);
}
else {
/* Optimize allocation by preallocation of table */
LL_FOREACH (obj, cur) {
nelt ++;
}
lua_createtable (L, nelt, 0);
LL_FOREACH (obj, cur) {
ucl_object_push_lua (L, cur, false);
lua_rawseti (L, -2, i);
i ++;
}
luaL_getmetatable (L, UCL_IMPL_ARRAY_TYPE_META);
lua_setmetatable (L, -2);
}
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) {
libucl-0.8.1/lua/lua_ucl.c view on Meta::CPAN
unsigned int i;
if (!is_implicit) {
top = ucl_object_typed_new (UCL_ARRAY);
ucl_object_reserve (top, nelts);
}
else {
top = NULL;
}
for (i = 1; i <= max; i ++) {
lua_pushinteger (L, i);
lua_gettable (L, idx);
obj = ucl_object_lua_fromelt (L, lua_gettop (L), flags);
if (obj != NULL) {
if (is_implicit) {
DL_APPEND (top, obj);
}
else {
ucl_array_append (top, obj);
}
}
lua_pop (L, 1);
}
}
else {
lua_pushnil (L);
top = ucl_object_typed_new (UCL_OBJECT);
ucl_object_reserve (top, nelts);
while (lua_next (L, idx) != 0) {
/* copy key to avoid modifications */
lua_pushvalue (L, -2);
k = lua_tolstring (L, -1, &keylen);
obj = ucl_object_lua_fromelt (L, lua_gettop (L) - 1, flags);
if (obj != NULL) {
ucl_object_insert_key (top, obj, k, keylen, true);
}
lua_pop (L, 2);
}
}
return top;
}
/**
* Get a single element from lua to object obj
* @param L
* @param obj
* @param idx
*/
static ucl_object_t *
ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags)
{
int type;
double num;
ucl_object_t *obj = NULL;
struct ucl_lua_funcdata *fd;
const char *str;
size_t sz;
type = lua_type (L, idx);
switch (type) {
case LUA_TSTRING:
str = lua_tolstring (L, idx, &sz);
if (str) {
obj = ucl_object_fromstring_common (str, sz, flags);
}
else {
obj = ucl_object_typed_new (UCL_NULL);
}
break;
case LUA_TNUMBER:
num = lua_tonumber (L, idx);
if (num == (int64_t)num) {
obj = ucl_object_fromint (num);
}
else {
obj = ucl_object_fromdouble (num);
}
break;
case LUA_TBOOLEAN:
obj = ucl_object_frombool (lua_toboolean (L, idx));
break;
case LUA_TUSERDATA:
if (lua_topointer (L, idx) == ucl_null) {
obj = ucl_object_typed_new (UCL_NULL);
}
break;
case LUA_TTABLE:
case LUA_TFUNCTION:
case LUA_TTHREAD:
if (luaL_getmetafield (L, idx, "__gen_ucl")) {
if (lua_isfunction (L, -1)) {
lua_settop (L, 3); /* gen, obj, func */
lua_insert (L, 1); /* func, gen, obj */
lua_insert (L, 2); /* func, obj, gen */
lua_call(L, 2, 1);
obj = ucl_object_lua_fromelt (L, 1, flags);
}
lua_pop (L, 2);
}
else {
if (type == LUA_TTABLE) {
obj = ucl_object_lua_fromtable (L, idx, flags);
}
else if (type == LUA_TFUNCTION) {
fd = malloc (sizeof (*fd));
if (fd != NULL) {
lua_pushvalue (L, idx);
fd->L = L;
fd->ret = NULL;
fd->idx = luaL_ref (L, LUA_REGISTRYINDEX);
obj = ucl_object_new_userdata (lua_ucl_userdata_dtor,
lua_ucl_userdata_emitter, (void *)fd);
libucl-0.8.1/lua/lua_ucl.c view on Meta::CPAN
static int
lua_ucl_null_tostring (lua_State* L)
{
lua_pushstring (L, "null");
return 1;
}
static void
lua_ucl_null_mt (lua_State *L)
{
luaL_newmetatable (L, NULL_META);
lua_pushcfunction (L, lua_ucl_null_tostring);
lua_setfield (L, -2, "__tostring");
lua_pop (L, 1);
}
int
luaopen_ucl (lua_State *L)
{
lua_ucl_parser_mt (L);
lua_ucl_null_mt (L);
lua_ucl_object_mt (L);
lua_ucl_types_mt (L);
/* Create the refs weak table: */
lua_createtable (L, 0, 2);
lua_pushliteral (L, "v"); /* tbl, "v" */
lua_setfield (L, -2, "__mode");
lua_pushvalue (L, -1); /* tbl, tbl */
lua_setmetatable (L, -2); /* tbl */
lua_setfield (L, LUA_REGISTRYINDEX, "ucl.refs");
lua_newtable (L);
lua_pushcfunction (L, lua_ucl_parser_init);
lua_setfield (L, -2, "parser");
lua_pushcfunction (L, lua_ucl_to_json);
lua_setfield (L, -2, "to_json");
lua_pushcfunction (L, lua_ucl_to_config);
lua_setfield (L, -2, "to_config");
lua_pushcfunction (L, lua_ucl_to_format);
lua_setfield (L, -2, "to_format");
ucl_null = lua_newuserdata (L, 0);
luaL_getmetatable (L, NULL_META);
lua_setmetatable (L, -2);
lua_pushvalue (L, -1);
lua_setfield (L, LUA_REGISTRYINDEX, "ucl.null");
lua_setfield (L, -2, "null");
return 1;
}
struct ucl_lua_funcdata*
ucl_object_toclosure (const ucl_object_t *obj)
{
if (obj == NULL || obj->type != UCL_USERDATA) {
return NULL;
}
return (struct ucl_lua_funcdata*)obj->value.ud;
}
( run in 1.558 second using v1.01-cache-2.11-cpan-39bf76dae61 )