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_object_iterate (obj, &it, true) != NULL) {
156 lua_createtable (L, 0, nelt);
159 while ((cur = ucl_object_iterate (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);
190 ucl_object_iterate_free (it);
193 /* Optimize allocation by preallocation of table */
194 LL_FOREACH (obj, cur) {
198 lua_createtable (L, nelt, 0);
200 LL_FOREACH (obj, cur) {
201 ucl_object_push_lua (L, cur, false);
202 lua_rawseti (L, -2, i);
211 * Push a simple object to lua depending on its actual type
214 ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj,
217 struct ucl_lua_funcdata *fd;
219 if (allow_array && obj->next != NULL) {
220 /* Actually we need to push this as an array */
221 return ucl_object_lua_push_array (L, obj);
226 lua_pushboolean (L, ucl_obj_toboolean (obj));
229 lua_pushstring (L, ucl_obj_tostring (obj));
232 #if LUA_VERSION_NUM >= 501
233 lua_pushinteger (L, ucl_obj_toint (obj));
235 lua_pushnumber (L, ucl_obj_toint (obj));
240 lua_pushnumber (L, ucl_obj_todouble (obj));
243 lua_getfield (L, LUA_REGISTRYINDEX, "ucl.null");
246 fd = (struct ucl_lua_funcdata *)obj->value.ud;
247 lua_rawgeti (L, LUA_REGISTRYINDEX, fd->idx);
258 * @function ucl_object_push_lua(L, obj, allow_array)
259 * This is a `C` function to push `UCL` object as lua variable. This function
260 * converts `obj` to lua representation using the following conversions:
262 * - *scalar* values are directly presented by lua objects
263 * - *userdata* values are converted to lua function objects using `LUA_REGISTRYINDEX`,
264 * this can be used to pass functions from lua to c and vice-versa
265 * - *arrays* are converted to lua tables with numeric indicies suitable for `ipairs` iterations
266 * - *objects* are converted to lua tables with string indicies
267 * @param {lua_State} L lua state pointer
268 * @param {ucl_object_t} obj object to push
269 * @param {bool} allow_array expand implicit arrays (should be true for all but partial arrays)
270 * @return {int} `1` if an object is pushed to lua
273 ucl_object_push_lua (lua_State *L, const ucl_object_t *obj, bool allow_array)
277 return ucl_object_lua_push_object (L, obj, allow_array);
279 return ucl_object_lua_push_array (L, obj);
281 return ucl_object_lua_push_scalar (L, obj, allow_array);
286 * Parse lua table into object top
291 static ucl_object_t *
292 ucl_object_lua_fromtable (lua_State *L, int idx)
294 ucl_object_t *obj, *top = NULL;
297 bool is_array = true;
301 /* For negative indicies we want to invert them */
302 idx = lua_gettop (L) + idx + 1;
304 /* Check for array */
306 while (lua_next (L, idx) != 0) {
307 if (lua_type (L, -2) == LUA_TNUMBER) {
308 double num = lua_tonumber (L, -2);
309 if (num == (int)num) {
315 /* Keys are not integer */
322 /* Keys are not numeric */
334 top = ucl_object_typed_new (UCL_ARRAY);
335 for (i = 1; i <= max; i ++) {
336 lua_pushinteger (L, i);
337 lua_gettable (L, idx);
338 obj = ucl_object_lua_fromelt (L, lua_gettop (L));
340 ucl_array_append (top, obj);
347 top = ucl_object_typed_new (UCL_OBJECT);
348 while (lua_next (L, idx) != 0) {
349 /* copy key to avoid modifications */
350 k = lua_tolstring (L, -2, &keylen);
351 obj = ucl_object_lua_fromelt (L, lua_gettop (L));
354 ucl_object_insert_key (top, obj, k, keylen, true);
364 * Get a single element from lua to object obj
369 static ucl_object_t *
370 ucl_object_lua_fromelt (lua_State *L, int idx)
374 ucl_object_t *obj = NULL;
375 struct ucl_lua_funcdata *fd;
377 type = lua_type (L, idx);
381 obj = ucl_object_fromstring_common (lua_tostring (L, idx), 0, 0);
384 num = lua_tonumber (L, idx);
385 if (num == (int64_t)num) {
386 obj = ucl_object_fromint (num);
389 obj = ucl_object_fromdouble (num);
393 obj = ucl_object_frombool (lua_toboolean (L, idx));
396 if (lua_topointer (L, idx) == ucl_null) {
397 obj = ucl_object_typed_new (UCL_NULL);
403 if (luaL_getmetafield (L, idx, "__gen_ucl")) {
404 if (lua_isfunction (L, -1)) {
405 lua_settop (L, 3); /* gen, obj, func */
406 lua_insert (L, 1); /* func, gen, obj */
407 lua_insert (L, 2); /* func, obj, gen */
409 obj = ucl_object_lua_fromelt (L, 1);
414 if (type == LUA_TTABLE) {
415 obj = ucl_object_lua_fromtable (L, idx);
417 else if (type == LUA_TFUNCTION) {
418 fd = malloc (sizeof (*fd));
420 lua_pushvalue (L, idx);
423 fd->idx = luaL_ref (L, LUA_REGISTRYINDEX);
425 obj = ucl_object_new_userdata (lua_ucl_userdata_dtor,
426 lua_ucl_userdata_emitter, (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;
486 int flags = UCL_PARSER_NO_FILEVARS;
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 lua_ucl_push_opaque (lua_State *L, ucl_object_t *obj)
522 pobj = lua_newuserdata (L, sizeof (*pobj));
524 luaL_getmetatable (L, OBJECT_META);
525 lua_setmetatable (L, -2);
528 static inline enum ucl_parse_type
529 lua_ucl_str_to_parse_type (const char *str)
531 enum ucl_parse_type type = UCL_PARSE_UCL;
534 if (strcasecmp (str, "msgpack") == 0) {
535 type = UCL_PARSE_MSGPACK;
537 else if (strcasecmp (str, "sexp") == 0 ||
538 strcasecmp (str, "csexp") == 0) {
539 type = UCL_PARSE_CSEXP;
541 else if (strcasecmp (str, "auto") == 0) {
542 type = UCL_PARSE_AUTO;
550 * @method parser:parse_file(name)
551 * Parse UCL object from file.
552 * @param {string} name filename to parse
553 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
555 local parser = ucl.parser()
556 local res,err = parser:parse_file('/some/file.conf')
559 print('parser error: ' .. err)
561 -- Do something with object
565 lua_ucl_parser_parse_file (lua_State *L)
567 struct ucl_parser *parser;
571 parser = lua_ucl_parser_get (L, 1);
572 file = luaL_checkstring (L, 2);
574 if (parser != NULL && file != NULL) {
575 if (ucl_parser_add_file (parser, file)) {
576 lua_pushboolean (L, true);
580 lua_pushboolean (L, false);
581 lua_pushstring (L, ucl_parser_get_error (parser));
585 lua_pushboolean (L, false);
586 lua_pushstring (L, "invalid arguments");
593 * @method parser:parse_string(input)
594 * Parse UCL object from file.
595 * @param {string} input string to parse
596 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
599 lua_ucl_parser_parse_string (lua_State *L)
601 struct ucl_parser *parser;
604 enum ucl_parse_type type = UCL_PARSE_UCL;
607 parser = lua_ucl_parser_get (L, 1);
608 string = luaL_checklstring (L, 2, &llen);
610 if (lua_type (L, 3) == LUA_TSTRING) {
611 type = lua_ucl_str_to_parse_type (lua_tostring (L, 3));
614 if (parser != NULL && string != NULL) {
615 if (ucl_parser_add_chunk_full (parser, (const unsigned char *)string,
616 llen, 0, UCL_DUPLICATE_APPEND, type)) {
617 lua_pushboolean (L, true);
621 lua_pushboolean (L, false);
622 lua_pushstring (L, ucl_parser_get_error (parser));
626 lua_pushboolean (L, false);
627 lua_pushstring (L, "invalid arguments");
634 * @method parser:get_object()
635 * Get top object from parser and export it to lua representation.
636 * @return {variant or nil} ucl object as lua native variable
639 lua_ucl_parser_get_object (lua_State *L)
641 struct ucl_parser *parser;
645 parser = lua_ucl_parser_get (L, 1);
646 obj = ucl_parser_get_object (parser);
649 ret = ucl_object_push_lua (L, obj, false);
650 /* no need to keep reference */
651 ucl_object_unref (obj);
661 * @method parser:get_object_wrapped()
662 * Get top object from parser and export it to userdata object without
664 * @return {ucl.object or nil} ucl object wrapped variable
667 lua_ucl_parser_get_object_wrapped (lua_State *L)
669 struct ucl_parser *parser;
673 parser = lua_ucl_parser_get (L, 1);
674 obj = ucl_parser_get_object (parser);
677 lua_ucl_push_opaque (L, obj);
687 * @method parser:validate(schema)
688 * Validates the top object in the parser against schema. Schema might be
689 * another object or a string that represents file to load schema from.
691 * @param {string/table} schema input schema
692 * @return {result,err} two values: boolean result and the corresponding error
696 lua_ucl_parser_validate (lua_State *L)
698 struct ucl_parser *parser, *schema_parser;
699 ucl_object_t *schema;
700 const char *schema_file;
701 struct ucl_schema_error err;
703 parser = lua_ucl_parser_get (L, 1);
705 if (parser && parser->top_obj) {
706 if (lua_type (L, 2) == LUA_TTABLE) {
707 schema = ucl_object_lua_import (L, 2);
709 if (schema == NULL) {
710 lua_pushboolean (L, false);
711 lua_pushstring (L, "cannot load schema from lua table");
716 else if (lua_type (L, 2) == LUA_TSTRING) {
717 schema_parser = ucl_parser_new (0);
718 schema_file = luaL_checkstring (L, 2);
720 if (!ucl_parser_add_file (schema_parser, schema_file)) {
721 lua_pushboolean (L, false);
722 lua_pushfstring (L, "cannot parse schema file \"%s\": "
723 "%s", schema_file, ucl_parser_get_error (parser));
724 ucl_parser_free (schema_parser);
729 schema = ucl_parser_get_object (schema_parser);
730 ucl_parser_free (schema_parser);
733 lua_pushboolean (L, false);
734 lua_pushstring (L, "invalid schema argument");
739 if (!ucl_object_validate (schema, parser->top_obj, &err)) {
740 lua_pushboolean (L, false);
741 lua_pushfstring (L, "validation error: "
745 lua_pushboolean (L, true);
749 ucl_object_unref (schema);
752 lua_pushboolean (L, false);
753 lua_pushstring (L, "invalid parser or empty top object");
760 lua_ucl_parser_gc (lua_State *L)
762 struct ucl_parser *parser;
764 parser = lua_ucl_parser_get (L, 1);
765 ucl_parser_free (parser);
771 * @method object:unwrap()
772 * Unwraps opaque ucl object to the native lua object (performing copying)
773 * @return {variant} any lua object
776 lua_ucl_object_unwrap (lua_State *L)
780 obj = lua_ucl_object_get (L, 1);
783 ucl_object_push_lua (L, obj, true);
792 static inline enum ucl_emitter
793 lua_ucl_str_to_emit_type (const char *strtype)
795 enum ucl_emitter format = UCL_EMIT_JSON_COMPACT;
797 if (strcasecmp (strtype, "json") == 0) {
798 format = UCL_EMIT_JSON;
800 else if (strcasecmp (strtype, "json-compact") == 0) {
801 format = UCL_EMIT_JSON_COMPACT;
803 else if (strcasecmp (strtype, "yaml") == 0) {
804 format = UCL_EMIT_YAML;
806 else if (strcasecmp (strtype, "config") == 0 ||
807 strcasecmp (strtype, "ucl") == 0) {
808 format = UCL_EMIT_CONFIG;
815 * @method object:tostring(type)
816 * Unwraps opaque ucl object to string (json by default). Optionally you can
817 * specify output format:
819 * - `json` - fine printed json
820 * - `json-compact` - compacted json
821 * - `config` - fine printed configuration
822 * - `ucl` - same as `config`
823 * - `yaml` - embedded yaml
824 * @param {string} type optional
825 * @return {string} string representation of the opaque ucl object
828 lua_ucl_object_tostring (lua_State *L)
831 enum ucl_emitter format = UCL_EMIT_JSON_COMPACT;
833 obj = lua_ucl_object_get (L, 1);
836 if (lua_gettop (L) > 1) {
837 if (lua_type (L, 2) == LUA_TSTRING) {
838 const char *strtype = lua_tostring (L, 2);
840 format = lua_ucl_str_to_emit_type (strtype);
844 return lua_ucl_to_string (L, obj, format);
854 * @method object:validate(schema[, path[, ext_refs]])
855 * Validates the given ucl object using schema object represented as another
856 * opaque ucl object. You can also specify path in the form `#/path/def` to
857 * specify the specific schema element to perform validation.
859 * @param {ucl.object} schema schema object
860 * @param {string} path optional path for validation procedure
861 * @return {result,err} two values: boolean result and the corresponding
862 * error, if `ext_refs` are also specified, then they are returned as opaque
863 * ucl object as {result,err,ext_refs}
866 lua_ucl_object_validate (lua_State *L)
868 ucl_object_t *obj, *schema, *ext_refs = NULL;
869 const ucl_object_t *schema_elt;
871 struct ucl_schema_error err;
872 const char *path = NULL;
874 obj = lua_ucl_object_get (L, 1);
875 schema = lua_ucl_object_get (L, 2);
877 if (schema && obj && ucl_object_type (schema) == UCL_OBJECT) {
878 if (lua_gettop (L) > 2) {
879 if (lua_type (L, 3) == LUA_TSTRING) {
880 path = lua_tostring (L, 3);
881 if (path[0] == '#') {
885 else if (lua_type (L, 3) == LUA_TUSERDATA || lua_type (L, 3) ==
888 ext_refs = lua_ucl_object_get (L, 3);
891 if (lua_gettop (L) > 3) {
892 if (lua_type (L, 4) == LUA_TUSERDATA || lua_type (L, 4) ==
895 ext_refs = lua_ucl_object_get (L, 4);
901 schema_elt = ucl_object_lookup_path_char (schema, path, '/');
904 /* Use the top object */
909 res = ucl_object_validate_root_ext (schema_elt, obj, schema,
913 lua_pushboolean (L, res);
917 lua_ucl_push_opaque (L, ext_refs);
921 lua_pushboolean (L, res);
922 lua_pushfstring (L, "validation error: %s", err.msg);
925 lua_ucl_push_opaque (L, ext_refs);
930 lua_pushboolean (L, res);
932 lua_pushfstring (L, "cannot find the requested path: %s", path);
935 lua_ucl_push_opaque (L, ext_refs);
940 lua_pushboolean (L, res);
941 lua_pushstring (L, "invalid object or schema");
952 lua_ucl_object_gc (lua_State *L)
956 obj = lua_ucl_object_get (L, 1);
958 ucl_object_unref (obj);
964 lua_ucl_parser_mt (lua_State *L)
966 luaL_newmetatable (L, PARSER_META);
968 lua_pushvalue(L, -1);
969 lua_setfield(L, -2, "__index");
971 lua_pushcfunction (L, lua_ucl_parser_gc);
972 lua_setfield (L, -2, "__gc");
974 lua_pushcfunction (L, lua_ucl_parser_parse_file);
975 lua_setfield (L, -2, "parse_file");
977 lua_pushcfunction (L, lua_ucl_parser_parse_string);
978 lua_setfield (L, -2, "parse_string");
980 lua_pushcfunction (L, lua_ucl_parser_get_object);
981 lua_setfield (L, -2, "get_object");
983 lua_pushcfunction (L, lua_ucl_parser_get_object_wrapped);
984 lua_setfield (L, -2, "get_object_wrapped");
986 lua_pushcfunction (L, lua_ucl_parser_validate);
987 lua_setfield (L, -2, "validate");
993 lua_ucl_object_mt (lua_State *L)
995 luaL_newmetatable (L, OBJECT_META);
997 lua_pushvalue(L, -1);
998 lua_setfield(L, -2, "__index");
1000 lua_pushcfunction (L, lua_ucl_object_gc);
1001 lua_setfield (L, -2, "__gc");
1003 lua_pushcfunction (L, lua_ucl_object_tostring);
1004 lua_setfield (L, -2, "__tostring");
1006 lua_pushcfunction (L, lua_ucl_object_tostring);
1007 lua_setfield (L, -2, "tostring");
1009 lua_pushcfunction (L, lua_ucl_object_unwrap);
1010 lua_setfield (L, -2, "unwrap");
1012 lua_pushcfunction (L, lua_ucl_object_unwrap);
1013 lua_setfield (L, -2, "tolua");
1015 lua_pushcfunction (L, lua_ucl_object_validate);
1016 lua_setfield (L, -2, "validate");
1018 lua_pushstring (L, OBJECT_META);
1019 lua_setfield (L, -2, "class");
1025 lua_ucl_to_json (lua_State *L)
1028 int format = UCL_EMIT_JSON;
1030 if (lua_gettop (L) > 1) {
1031 if (lua_toboolean (L, 2)) {
1032 format = UCL_EMIT_JSON_COMPACT;
1036 obj = ucl_object_lua_import (L, 1);
1038 lua_ucl_to_string (L, obj, format);
1039 ucl_object_unref (obj);
1049 lua_ucl_to_config (lua_State *L)
1053 obj = ucl_object_lua_import (L, 1);
1055 lua_ucl_to_string (L, obj, UCL_EMIT_CONFIG);
1056 ucl_object_unref (obj);
1066 * @function ucl.to_format(var, format)
1067 * Converts lua variable `var` to the specified `format`. Formats supported are:
1069 * - `json` - fine printed json
1070 * - `json-compact` - compacted json
1071 * - `config` - fine printed configuration
1072 * - `ucl` - same as `config`
1073 * - `yaml` - embedded yaml
1075 * If `var` contains function, they are called during output formatting and if
1076 * they return string value, then this value is used for ouptut.
1077 * @param {variant} var any sort of lua variable (if userdata then metafield `__to_ucl` is searched for output)
1078 * @param {string} format any available format
1079 * @return {string} string representation of `var` in the specific `format`.
1090 print(ucl.to_format(table, 'ucl'))
1100 lua_ucl_to_format (lua_State *L)
1103 int format = UCL_EMIT_JSON;
1105 if (lua_gettop (L) > 1) {
1106 if (lua_type (L, 2) == LUA_TNUMBER) {
1107 format = lua_tonumber (L, 2);
1108 if (format < 0 || format >= UCL_EMIT_YAML) {
1113 else if (lua_type (L, 2) == LUA_TSTRING) {
1114 const char *strtype = lua_tostring (L, 2);
1116 if (strcasecmp (strtype, "json") == 0) {
1117 format = UCL_EMIT_JSON;
1119 else if (strcasecmp (strtype, "json-compact") == 0) {
1120 format = UCL_EMIT_JSON_COMPACT;
1122 else if (strcasecmp (strtype, "yaml") == 0) {
1123 format = UCL_EMIT_YAML;
1125 else if (strcasecmp (strtype, "config") == 0 ||
1126 strcasecmp (strtype, "ucl") == 0) {
1127 format = UCL_EMIT_CONFIG;
1129 else if (strcasecmp (strtype, "msgpack") == 0) {
1130 format = UCL_EMIT_MSGPACK;
1135 obj = ucl_object_lua_import (L, 1);
1137 lua_ucl_to_string (L, obj, format);
1138 ucl_object_unref (obj);
1148 lua_ucl_null_tostring (lua_State* L)
1150 lua_pushstring (L, "null");
1155 lua_ucl_null_mt (lua_State *L)
1157 luaL_newmetatable (L, NULL_META);
1159 lua_pushcfunction (L, lua_ucl_null_tostring);
1160 lua_setfield (L, -2, "__tostring");
1166 luaopen_ucl (lua_State *L)
1168 lua_ucl_parser_mt (L);
1169 lua_ucl_null_mt (L);
1170 lua_ucl_object_mt (L);
1172 /* Create the refs weak table: */
1173 lua_createtable (L, 0, 2);
1174 lua_pushliteral (L, "v"); /* tbl, "v" */
1175 lua_setfield (L, -2, "__mode");
1176 lua_pushvalue (L, -1); /* tbl, tbl */
1177 lua_setmetatable (L, -2); /* tbl */
1178 lua_setfield (L, LUA_REGISTRYINDEX, "ucl.refs");
1182 lua_pushcfunction (L, lua_ucl_parser_init);
1183 lua_setfield (L, -2, "parser");
1185 lua_pushcfunction (L, lua_ucl_to_json);
1186 lua_setfield (L, -2, "to_json");
1188 lua_pushcfunction (L, lua_ucl_to_config);
1189 lua_setfield (L, -2, "to_config");
1191 lua_pushcfunction (L, lua_ucl_to_format);
1192 lua_setfield (L, -2, "to_format");
1194 ucl_null = lua_newuserdata (L, 0);
1195 luaL_getmetatable (L, NULL_META);
1196 lua_setmetatable (L, -2);
1198 lua_pushvalue (L, -1);
1199 lua_setfield (L, LUA_REGISTRYINDEX, "ucl.null");
1201 lua_setfield (L, -2, "null");
1206 struct ucl_lua_funcdata*
1207 ucl_object_toclosure (const ucl_object_t *obj)
1209 if (obj == NULL || obj->type != UCL_USERDATA) {
1213 return (struct ucl_lua_funcdata*)obj->value.ud;