1 /* Copyright (c) 2014, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * @file lua ucl bindings
29 #include "ucl_internal.h"
35 * This lua module allows to parse objects from strings and to store data into
36 * ucl objects. It uses `libucl` C library to parse and manipulate with ucl objects.
38 local ucl = require("ucl")
40 local parser = ucl.parser()
41 local res,err = parser:parse_string('{key=value}')
44 print('parser error: ' .. err)
46 local obj = parser:get_object()
47 local got = ucl.to_format(obj, 'json')
59 print(ucl.to_format(table, 'ucl'))
69 #define PARSER_META "ucl.parser.meta"
70 #define EMITTER_META "ucl.emitter.meta"
71 #define NULL_META "null.emitter.meta"
72 #define OBJECT_META "ucl.object.meta"
74 static int ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj);
75 static int ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, bool allow_array);
76 static ucl_object_t* ucl_object_lua_fromtable (lua_State *L, int idx);
77 static ucl_object_t* ucl_object_lua_fromelt (lua_State *L, int idx);
79 static void *ucl_null;
82 * Push a single element of an object to lua
88 ucl_object_lua_push_element (lua_State *L, const char *key,
89 const ucl_object_t *obj)
91 lua_pushstring (L, key);
92 ucl_object_push_lua (L, obj, true);
97 lua_ucl_userdata_dtor (void *ud)
99 struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
101 luaL_unref (fd->L, LUA_REGISTRYINDEX, fd->idx);
102 if (fd->ret != NULL) {
109 lua_ucl_userdata_emitter (void *ud)
111 struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
112 const char *out = "";
114 lua_rawgeti (fd->L, LUA_REGISTRYINDEX, fd->idx);
116 lua_pcall (fd->L, 0, 1, 0);
117 out = lua_tostring (fd->L, -1);
120 /* We need to store temporary string in a more appropriate place */
124 fd->ret = strdup (out);
127 lua_settop (fd->L, 0);
133 * Push a single object to lua
139 ucl_object_lua_push_object (lua_State *L, const ucl_object_t *obj,
142 const ucl_object_t *cur;
143 ucl_object_iter_t it = NULL;
146 if (allow_array && obj->next != NULL) {
147 /* Actually we need to push this as an array */
148 return ucl_object_lua_push_array (L, obj);
151 /* Optimize allocation by preallocation of table */
152 while (ucl_iterate_object (obj, &it, true) != NULL) {
156 lua_createtable (L, 0, nelt);
159 while ((cur = ucl_iterate_object (obj, &it, true)) != NULL) {
160 ucl_object_lua_push_element (L, ucl_object_key (cur), cur);
167 * Push an array to lua as table indexed by integers
173 ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj)
175 const ucl_object_t *cur;
176 ucl_object_iter_t it;
179 if (obj->type == UCL_ARRAY) {
181 it = ucl_object_iterate_new (obj);
182 lua_createtable (L, nelt, 0);
184 while ((cur = ucl_object_iterate_safe (it, true))) {
185 ucl_object_push_lua (L, cur, false);
186 lua_rawseti (L, -2, i);
191 /* Optimize allocation by preallocation of table */
192 LL_FOREACH (obj, cur) {
196 lua_createtable (L, nelt, 0);
198 LL_FOREACH (obj, cur) {
199 ucl_object_push_lua (L, cur, false);
200 lua_rawseti (L, -2, i);
209 * Push a simple object to lua depending on its actual type
212 ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj,
215 struct ucl_lua_funcdata *fd;
217 if (allow_array && obj->next != NULL) {
218 /* Actually we need to push this as an array */
219 return ucl_object_lua_push_array (L, obj);
224 lua_pushboolean (L, ucl_obj_toboolean (obj));
227 lua_pushstring (L, ucl_obj_tostring (obj));
230 #if LUA_VERSION_NUM >= 501
231 lua_pushinteger (L, ucl_obj_toint (obj));
233 lua_pushnumber (L, ucl_obj_toint (obj));
238 lua_pushnumber (L, ucl_obj_todouble (obj));
241 lua_getfield (L, LUA_REGISTRYINDEX, "ucl.null");
244 fd = (struct ucl_lua_funcdata *)obj->value.ud;
245 lua_rawgeti (L, LUA_REGISTRYINDEX, fd->idx);
256 * @function ucl_object_push_lua(L, obj, allow_array)
257 * This is a `C` function to push `UCL` object as lua variable. This function
258 * converts `obj` to lua representation using the following conversions:
260 * - *scalar* values are directly presented by lua objects
261 * - *userdata* values are converted to lua function objects using `LUA_REGISTRYINDEX`,
262 * this can be used to pass functions from lua to c and vice-versa
263 * - *arrays* are converted to lua tables with numeric indicies suitable for `ipairs` iterations
264 * - *objects* are converted to lua tables with string indicies
265 * @param {lua_State} L lua state pointer
266 * @param {ucl_object_t} obj object to push
267 * @param {bool} allow_array expand implicit arrays (should be true for all but partial arrays)
268 * @return {int} `1` if an object is pushed to lua
271 ucl_object_push_lua (lua_State *L, const ucl_object_t *obj, bool allow_array)
275 return ucl_object_lua_push_object (L, obj, allow_array);
277 return ucl_object_lua_push_array (L, obj);
279 return ucl_object_lua_push_scalar (L, obj, allow_array);
284 * Parse lua table into object top
289 static ucl_object_t *
290 ucl_object_lua_fromtable (lua_State *L, int idx)
292 ucl_object_t *obj, *top = NULL;
295 bool is_array = true;
299 /* For negative indicies we want to invert them */
300 idx = lua_gettop (L) + idx + 1;
302 /* Check for array */
304 while (lua_next (L, idx) != 0) {
305 if (lua_type (L, -2) == LUA_TNUMBER) {
306 double num = lua_tonumber (L, -2);
307 if (num == (int)num) {
313 /* Keys are not integer */
320 /* Keys are not numeric */
332 top = ucl_object_typed_new (UCL_ARRAY);
333 for (i = 1; i <= max; i ++) {
334 lua_pushinteger (L, i);
335 lua_gettable (L, idx);
336 obj = ucl_object_lua_fromelt (L, lua_gettop (L));
338 ucl_array_append (top, obj);
345 top = ucl_object_typed_new (UCL_OBJECT);
346 while (lua_next (L, idx) != 0) {
347 /* copy key to avoid modifications */
348 k = lua_tolstring (L, -2, &keylen);
349 obj = ucl_object_lua_fromelt (L, lua_gettop (L));
352 ucl_object_insert_key (top, obj, k, keylen, true);
362 * Get a single element from lua to object obj
367 static ucl_object_t *
368 ucl_object_lua_fromelt (lua_State *L, int idx)
372 ucl_object_t *obj = NULL;
373 struct ucl_lua_funcdata *fd;
375 type = lua_type (L, idx);
379 obj = ucl_object_fromstring_common (lua_tostring (L, idx), 0, 0);
382 num = lua_tonumber (L, idx);
383 if (num == (int64_t)num) {
384 obj = ucl_object_fromint (num);
387 obj = ucl_object_fromdouble (num);
391 obj = ucl_object_frombool (lua_toboolean (L, idx));
394 if (lua_topointer (L, idx) == ucl_null) {
395 obj = ucl_object_typed_new (UCL_NULL);
401 if (luaL_getmetafield (L, idx, "__gen_ucl")) {
402 if (lua_isfunction (L, -1)) {
403 lua_settop (L, 3); /* gen, obj, func */
404 lua_insert (L, 1); /* func, gen, obj */
405 lua_insert (L, 2); /* func, obj, gen */
407 obj = ucl_object_lua_fromelt (L, 1);
412 if (type == LUA_TTABLE) {
413 obj = ucl_object_lua_fromtable (L, idx);
415 else if (type == LUA_TFUNCTION) {
416 fd = malloc (sizeof (*fd));
418 lua_pushvalue (L, idx);
421 fd->idx = luaL_ref (L, LUA_REGISTRYINDEX);
423 obj = ucl_object_new_userdata (lua_ucl_userdata_dtor,
424 lua_ucl_userdata_emitter);
425 obj->type = UCL_USERDATA;
426 obj->value.ud = (void *)fd;
437 * @function ucl_object_lua_import(L, idx)
438 * Extracts ucl object from lua variable at `idx` position,
439 * @see ucl_object_push_lua for conversion definitions
440 * @param {lua_state} L lua state machine pointer
441 * @param {int} idx index where the source variable is placed
442 * @return {ucl_object_t} new ucl object extracted from lua variable. Reference count of this object is 1,
443 * this object thus needs to be unref'ed after usage.
446 ucl_object_lua_import (lua_State *L, int idx)
451 t = lua_type (L, idx);
454 obj = ucl_object_lua_fromtable (L, idx);
457 obj = ucl_object_lua_fromelt (L, idx);
465 lua_ucl_to_string (lua_State *L, const ucl_object_t *obj, enum ucl_emitter type)
467 unsigned char *result;
469 result = ucl_object_emit (obj, type);
471 if (result != NULL) {
472 lua_pushstring (L, (const char *)result);
483 lua_ucl_parser_init (lua_State *L)
485 struct ucl_parser *parser, **pparser;
488 if (lua_gettop (L) >= 1) {
489 flags = lua_tonumber (L, 1);
492 parser = ucl_parser_new (flags);
493 if (parser == NULL) {
497 pparser = lua_newuserdata (L, sizeof (parser));
499 luaL_getmetatable (L, PARSER_META);
500 lua_setmetatable (L, -2);
505 static struct ucl_parser *
506 lua_ucl_parser_get (lua_State *L, int index)
508 return *((struct ucl_parser **) luaL_checkudata(L, index, PARSER_META));
511 static ucl_object_t *
512 lua_ucl_object_get (lua_State *L, int index)
514 return *((ucl_object_t **) luaL_checkudata(L, index, OBJECT_META));
518 * @method parser:parse_file(name)
519 * Parse UCL object from file.
520 * @param {string} name filename to parse
521 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
523 local parser = ucl.parser()
524 local res,err = parser:parse_file('/some/file.conf')
527 print('parser error: ' .. err)
529 -- Do something with object
533 lua_ucl_parser_parse_file (lua_State *L)
535 struct ucl_parser *parser;
539 parser = lua_ucl_parser_get (L, 1);
540 file = luaL_checkstring (L, 2);
542 if (parser != NULL && file != NULL) {
543 if (ucl_parser_add_file (parser, file)) {
544 lua_pushboolean (L, true);
548 lua_pushboolean (L, false);
549 lua_pushstring (L, ucl_parser_get_error (parser));
553 lua_pushboolean (L, false);
554 lua_pushstring (L, "invalid arguments");
561 * @method parser:parse_string(input)
562 * Parse UCL object from file.
563 * @param {string} input string to parse
564 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
567 lua_ucl_parser_parse_string (lua_State *L)
569 struct ucl_parser *parser;
574 parser = lua_ucl_parser_get (L, 1);
575 string = luaL_checklstring (L, 2, &llen);
577 if (parser != NULL && string != NULL) {
578 if (ucl_parser_add_chunk (parser, (const unsigned char *)string, llen)) {
579 lua_pushboolean (L, true);
583 lua_pushboolean (L, false);
584 lua_pushstring (L, ucl_parser_get_error (parser));
588 lua_pushboolean (L, false);
589 lua_pushstring (L, "invalid arguments");
596 * @method parser:get_object()
597 * Get top object from parser and export it to lua representation.
598 * @return {variant or nil} ucl object as lua native variable
601 lua_ucl_parser_get_object (lua_State *L)
603 struct ucl_parser *parser;
607 parser = lua_ucl_parser_get (L, 1);
608 obj = ucl_parser_get_object (parser);
611 ret = ucl_object_push_lua (L, obj, false);
612 /* no need to keep reference */
613 ucl_object_unref (obj);
623 * @method parser:get_object_wrapped()
624 * Get top object from parser and export it to userdata object without
626 * @return {ucl.object or nil} ucl object wrapped variable
629 lua_ucl_parser_get_object_wrapped (lua_State *L)
631 struct ucl_parser *parser;
632 ucl_object_t *obj, **pobj;
635 parser = lua_ucl_parser_get (L, 1);
636 obj = ucl_parser_get_object (parser);
639 pobj = lua_newuserdata (L, sizeof (*pobj));
641 luaL_getmetatable (L, OBJECT_META);
642 lua_setmetatable (L, -2);
652 * @method parser:validate(schema)
653 * Validates the top object in the parser against schema. Schema might be
654 * another object or a string that represents file to load schema from.
656 * @param {string/table} schema input schema
657 * @return {result,err} two values: boolean result and the corresponding error
661 lua_ucl_parser_validate (lua_State *L)
663 struct ucl_parser *parser, *schema_parser;
664 ucl_object_t *schema;
665 const char *schema_file;
666 struct ucl_schema_error err;
668 parser = lua_ucl_parser_get (L, 1);
670 if (parser && parser->top_obj) {
671 if (lua_type (L, 2) == LUA_TTABLE) {
672 schema = ucl_object_lua_import (L, 2);
674 if (schema == NULL) {
675 lua_pushboolean (L, false);
676 lua_pushstring (L, "cannot load schema from lua table");
681 else if (lua_type (L, 2) == LUA_TSTRING) {
682 schema_parser = ucl_parser_new (0);
683 schema_file = luaL_checkstring (L, 2);
685 if (!ucl_parser_add_file (schema_parser, schema_file)) {
686 lua_pushboolean (L, false);
687 lua_pushfstring (L, "cannot parse schema file \"%s\": "
688 "%s", schema_file, ucl_parser_get_error (parser));
689 ucl_parser_free (schema_parser);
694 schema = ucl_parser_get_object (schema_parser);
695 ucl_parser_free (schema_parser);
698 lua_pushboolean (L, false);
699 lua_pushstring (L, "invalid schema argument");
704 if (!ucl_object_validate (schema, parser->top_obj, &err)) {
705 lua_pushboolean (L, false);
706 lua_pushfstring (L, "validation error: "
710 lua_pushboolean (L, true);
714 ucl_object_unref (schema);
717 lua_pushboolean (L, false);
718 lua_pushstring (L, "invalid parser or empty top object");
725 lua_ucl_parser_gc (lua_State *L)
727 struct ucl_parser *parser;
729 parser = lua_ucl_parser_get (L, 1);
730 ucl_parser_free (parser);
736 * @method object:unwrap()
737 * Unwraps opaque ucl object to the native lua object (performing copying)
738 * @return {variant} any lua object
741 lua_ucl_object_unwrap (lua_State *L)
745 obj = lua_ucl_object_get (L, 1);
748 ucl_object_push_lua (L, obj, true);
758 * @method object:tostring(type)
759 * Unwraps opaque ucl object to string (json by default). Optionally you can
760 * specify output format:
762 * - `json` - fine printed json
763 * - `json-compact` - compacted json
764 * - `config` - fine printed configuration
765 * - `ucl` - same as `config`
766 * - `yaml` - embedded yaml
767 * @param {string} type optional
768 * @return {string} string representation of the opaque ucl object
771 lua_ucl_object_tostring (lua_State *L)
774 enum ucl_emitter format = UCL_EMIT_JSON_COMPACT;
776 obj = lua_ucl_object_get (L, 1);
779 if (lua_gettop (L) > 1) {
780 if (lua_type (L, 2) == LUA_TSTRING) {
781 const char *strtype = lua_tostring (L, 2);
783 if (strcasecmp (strtype, "json") == 0) {
784 format = UCL_EMIT_JSON;
786 else if (strcasecmp (strtype, "json-compact") == 0) {
787 format = UCL_EMIT_JSON_COMPACT;
789 else if (strcasecmp (strtype, "yaml") == 0) {
790 format = UCL_EMIT_YAML;
792 else if (strcasecmp (strtype, "config") == 0 ||
793 strcasecmp (strtype, "ucl") == 0) {
794 format = UCL_EMIT_CONFIG;
799 return lua_ucl_to_string (L, obj, format);
809 * @method object:validate(schema, path)
810 * Validates the given ucl object using schema object represented as another
811 * opaque ucl object. You can also specify path in the form `#/path/def` to
812 * specify the specific schema element to perform validation.
814 * @param {ucl.object} schema schema object
815 * @param {string} path optional path for validation procedure
816 * @return {result,err} two values: boolean result and the corresponding error
819 lua_ucl_object_validate (lua_State *L)
821 ucl_object_t *obj, *schema;
822 const ucl_object_t *schema_elt;
824 struct ucl_schema_error err;
825 const char *path = NULL;
827 obj = lua_ucl_object_get (L, 1);
828 schema = lua_ucl_object_get (L, 2);
830 if (schema && obj && ucl_object_type (schema) == UCL_OBJECT) {
831 if (lua_gettop (L) > 2 && lua_type (L, 3) == LUA_TSTRING) {
832 path = lua_tostring (L, 3);
833 if (path[0] == '#') {
839 schema_elt = ucl_lookup_path_char (schema, path, '/');
842 /* Use the top object */
847 res = ucl_object_validate (schema_elt, obj, &err);
850 lua_pushboolean (L, res);
854 lua_pushboolean (L, res);
855 lua_pushfstring (L, "validation error: %s", err.msg);
859 lua_pushboolean (L, res);
862 lua_pushfstring (L, "cannot find the requested path: %s", path);
865 /* Should not be reached */
866 lua_pushstring (L, "unknown error");
871 lua_pushboolean (L, res);
872 lua_pushstring (L, "invalid object or schema");
879 lua_ucl_object_gc (lua_State *L)
883 obj = lua_ucl_object_get (L, 1);
885 ucl_object_unref (obj);
891 lua_ucl_parser_mt (lua_State *L)
893 luaL_newmetatable (L, PARSER_META);
895 lua_pushvalue(L, -1);
896 lua_setfield(L, -2, "__index");
898 lua_pushcfunction (L, lua_ucl_parser_gc);
899 lua_setfield (L, -2, "__gc");
901 lua_pushcfunction (L, lua_ucl_parser_parse_file);
902 lua_setfield (L, -2, "parse_file");
904 lua_pushcfunction (L, lua_ucl_parser_parse_string);
905 lua_setfield (L, -2, "parse_string");
907 lua_pushcfunction (L, lua_ucl_parser_get_object);
908 lua_setfield (L, -2, "get_object");
910 lua_pushcfunction (L, lua_ucl_parser_get_object_wrapped);
911 lua_setfield (L, -2, "get_object_wrapped");
913 lua_pushcfunction (L, lua_ucl_parser_validate);
914 lua_setfield (L, -2, "validate");
920 lua_ucl_object_mt (lua_State *L)
922 luaL_newmetatable (L, OBJECT_META);
924 lua_pushvalue(L, -1);
925 lua_setfield(L, -2, "__index");
927 lua_pushcfunction (L, lua_ucl_object_gc);
928 lua_setfield (L, -2, "__gc");
930 lua_pushcfunction (L, lua_ucl_object_tostring);
931 lua_setfield (L, -2, "__tostring");
933 lua_pushcfunction (L, lua_ucl_object_tostring);
934 lua_setfield (L, -2, "tostring");
936 lua_pushcfunction (L, lua_ucl_object_unwrap);
937 lua_setfield (L, -2, "unwrap");
939 lua_pushcfunction (L, lua_ucl_object_unwrap);
940 lua_setfield (L, -2, "tolua");
942 lua_pushcfunction (L, lua_ucl_object_validate);
943 lua_setfield (L, -2, "validate");
945 lua_pushstring (L, OBJECT_META);
946 lua_setfield (L, -2, "class");
952 lua_ucl_to_json (lua_State *L)
955 int format = UCL_EMIT_JSON;
957 if (lua_gettop (L) > 1) {
958 if (lua_toboolean (L, 2)) {
959 format = UCL_EMIT_JSON_COMPACT;
963 obj = ucl_object_lua_import (L, 1);
965 lua_ucl_to_string (L, obj, format);
966 ucl_object_unref (obj);
976 lua_ucl_to_config (lua_State *L)
980 obj = ucl_object_lua_import (L, 1);
982 lua_ucl_to_string (L, obj, UCL_EMIT_CONFIG);
983 ucl_object_unref (obj);
993 * @function ucl.to_format(var, format)
994 * Converts lua variable `var` to the specified `format`. Formats supported are:
996 * - `json` - fine printed json
997 * - `json-compact` - compacted json
998 * - `config` - fine printed configuration
999 * - `ucl` - same as `config`
1000 * - `yaml` - embedded yaml
1002 * If `var` contains function, they are called during output formatting and if
1003 * they return string value, then this value is used for ouptut.
1004 * @param {variant} var any sort of lua variable (if userdata then metafield `__to_ucl` is searched for output)
1005 * @param {string} format any available format
1006 * @return {string} string representation of `var` in the specific `format`.
1017 print(ucl.to_format(table, 'ucl'))
1027 lua_ucl_to_format (lua_State *L)
1030 int format = UCL_EMIT_JSON;
1032 if (lua_gettop (L) > 1) {
1033 if (lua_type (L, 2) == LUA_TNUMBER) {
1034 format = lua_tonumber (L, 2);
1035 if (format < 0 || format >= UCL_EMIT_YAML) {
1040 else if (lua_type (L, 2) == LUA_TSTRING) {
1041 const char *strtype = lua_tostring (L, 2);
1043 if (strcasecmp (strtype, "json") == 0) {
1044 format = UCL_EMIT_JSON;
1046 else if (strcasecmp (strtype, "json-compact") == 0) {
1047 format = UCL_EMIT_JSON_COMPACT;
1049 else if (strcasecmp (strtype, "yaml") == 0) {
1050 format = UCL_EMIT_YAML;
1052 else if (strcasecmp (strtype, "config") == 0 ||
1053 strcasecmp (strtype, "ucl") == 0) {
1054 format = UCL_EMIT_CONFIG;
1059 obj = ucl_object_lua_import (L, 1);
1061 lua_ucl_to_string (L, obj, format);
1062 ucl_object_unref (obj);
1072 lua_ucl_null_tostring (lua_State* L)
1074 lua_pushstring (L, "null");
1079 lua_ucl_null_mt (lua_State *L)
1081 luaL_newmetatable (L, NULL_META);
1083 lua_pushcfunction (L, lua_ucl_null_tostring);
1084 lua_setfield (L, -2, "__tostring");
1090 luaopen_ucl (lua_State *L)
1092 lua_ucl_parser_mt (L);
1093 lua_ucl_null_mt (L);
1094 lua_ucl_object_mt (L);
1096 /* Create the refs weak table: */
1097 lua_createtable (L, 0, 2);
1098 lua_pushliteral (L, "v"); /* tbl, "v" */
1099 lua_setfield (L, -2, "__mode");
1100 lua_pushvalue (L, -1); /* tbl, tbl */
1101 lua_setmetatable (L, -2); /* tbl */
1102 lua_setfield (L, LUA_REGISTRYINDEX, "ucl.refs");
1106 lua_pushcfunction (L, lua_ucl_parser_init);
1107 lua_setfield (L, -2, "parser");
1109 lua_pushcfunction (L, lua_ucl_to_json);
1110 lua_setfield (L, -2, "to_json");
1112 lua_pushcfunction (L, lua_ucl_to_config);
1113 lua_setfield (L, -2, "to_config");
1115 lua_pushcfunction (L, lua_ucl_to_format);
1116 lua_setfield (L, -2, "to_format");
1118 ucl_null = lua_newuserdata (L, 0);
1119 luaL_getmetatable (L, NULL_META);
1120 lua_setmetatable (L, -2);
1122 lua_pushvalue (L, -1);
1123 lua_setfield (L, LUA_REGISTRYINDEX, "ucl.null");
1125 lua_setfield (L, -2, "null");
1130 struct ucl_lua_funcdata*
1131 ucl_object_toclosure (const ucl_object_t *obj)
1133 if (obj == NULL || obj->type != UCL_USERDATA) {
1137 return (struct ucl_lua_funcdata*)obj->value.ud;