You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_ucl.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  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.
  11. *
  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.
  22. */
  23. /**
  24. * @file lua ucl bindings
  25. */
  26. #include "ucl.h"
  27. #include "ucl_internal.h"
  28. #include "lua_ucl.h"
  29. #include <strings.h>
  30. /***
  31. * @module ucl
  32. * This lua module allows to parse objects from strings and to store data into
  33. * ucl objects. It uses `libucl` C library to parse and manipulate with ucl objects.
  34. * @example
  35. local ucl = require("ucl")
  36. local parser = ucl.parser()
  37. local res,err = parser:parse_string('{key=value}')
  38. if not res then
  39. print('parser error: ' .. err)
  40. else
  41. local obj = parser:get_object()
  42. local got = ucl.to_format(obj, 'json')
  43. endif
  44. local table = {
  45. str = 'value',
  46. num = 100500,
  47. null = ucl.null,
  48. func = function ()
  49. return 'huh'
  50. end
  51. }
  52. print(ucl.to_format(table, 'ucl'))
  53. -- Output:
  54. --[[
  55. num = 100500;
  56. str = "value";
  57. null = null;
  58. func = "huh";
  59. --]]
  60. */
  61. #define PARSER_META "ucl.parser.meta"
  62. #define EMITTER_META "ucl.emitter.meta"
  63. #define NULL_META "null.emitter.meta"
  64. static int ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj);
  65. static int ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, bool allow_array);
  66. static ucl_object_t* ucl_object_lua_fromtable (lua_State *L, int idx);
  67. static ucl_object_t* ucl_object_lua_fromelt (lua_State *L, int idx);
  68. static void *ucl_null;
  69. /**
  70. * Push a single element of an object to lua
  71. * @param L
  72. * @param key
  73. * @param obj
  74. */
  75. static void
  76. ucl_object_lua_push_element (lua_State *L, const char *key,
  77. const ucl_object_t *obj)
  78. {
  79. lua_pushstring (L, key);
  80. ucl_object_push_lua (L, obj, true);
  81. lua_settable (L, -3);
  82. }
  83. static void
  84. lua_ucl_userdata_dtor (void *ud)
  85. {
  86. struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
  87. luaL_unref (fd->L, LUA_REGISTRYINDEX, fd->idx);
  88. if (fd->ret != NULL) {
  89. free (fd->ret);
  90. }
  91. free (fd);
  92. }
  93. static const char *
  94. lua_ucl_userdata_emitter (void *ud)
  95. {
  96. struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud;
  97. const char *out = "";
  98. lua_rawgeti (fd->L, LUA_REGISTRYINDEX, fd->idx);
  99. lua_pcall (fd->L, 0, 1, 0);
  100. out = lua_tostring (fd->L, -1);
  101. if (out != NULL) {
  102. /* We need to store temporary string in a more appropriate place */
  103. if (fd->ret) {
  104. free (fd->ret);
  105. }
  106. fd->ret = strdup (out);
  107. }
  108. lua_settop (fd->L, 0);
  109. return fd->ret;
  110. }
  111. /**
  112. * Push a single object to lua
  113. * @param L
  114. * @param obj
  115. * @return
  116. */
  117. static int
  118. ucl_object_lua_push_object (lua_State *L, const ucl_object_t *obj,
  119. bool allow_array)
  120. {
  121. const ucl_object_t *cur;
  122. ucl_object_iter_t it = NULL;
  123. int nelt = 0;
  124. if (allow_array && obj->next != NULL) {
  125. /* Actually we need to push this as an array */
  126. return ucl_object_lua_push_array (L, obj);
  127. }
  128. /* Optimize allocation by preallocation of table */
  129. while (ucl_iterate_object (obj, &it, true) != NULL) {
  130. nelt ++;
  131. }
  132. lua_createtable (L, 0, nelt);
  133. it = NULL;
  134. while ((cur = ucl_iterate_object (obj, &it, true)) != NULL) {
  135. ucl_object_lua_push_element (L, ucl_object_key (cur), cur);
  136. }
  137. return 1;
  138. }
  139. /**
  140. * Push an array to lua as table indexed by integers
  141. * @param L
  142. * @param obj
  143. * @return
  144. */
  145. static int
  146. ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj)
  147. {
  148. const ucl_object_t *cur;
  149. int i = 1, nelt = 0;
  150. /* Optimize allocation by preallocation of table */
  151. LL_FOREACH (obj, cur) {
  152. nelt ++;
  153. }
  154. lua_createtable (L, nelt, 0);
  155. LL_FOREACH (obj, cur) {
  156. ucl_object_push_lua (L, cur, false);
  157. lua_rawseti (L, -2, i);
  158. i ++;
  159. }
  160. return 1;
  161. }
  162. /**
  163. * Push a simple object to lua depending on its actual type
  164. */
  165. static int
  166. ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj,
  167. bool allow_array)
  168. {
  169. struct ucl_lua_funcdata *fd;
  170. if (allow_array && obj->next != NULL) {
  171. /* Actually we need to push this as an array */
  172. return ucl_object_lua_push_array (L, obj);
  173. }
  174. switch (obj->type) {
  175. case UCL_BOOLEAN:
  176. lua_pushboolean (L, ucl_obj_toboolean (obj));
  177. break;
  178. case UCL_STRING:
  179. lua_pushstring (L, ucl_obj_tostring (obj));
  180. break;
  181. case UCL_INT:
  182. #if LUA_VERSION_NUM >= 501
  183. lua_pushinteger (L, ucl_obj_toint (obj));
  184. #else
  185. lua_pushnumber (L, ucl_obj_toint (obj));
  186. #endif
  187. break;
  188. case UCL_FLOAT:
  189. case UCL_TIME:
  190. lua_pushnumber (L, ucl_obj_todouble (obj));
  191. break;
  192. case UCL_NULL:
  193. lua_getfield (L, LUA_REGISTRYINDEX, "ucl.null");
  194. break;
  195. case UCL_USERDATA:
  196. fd = (struct ucl_lua_funcdata *)obj->value.ud;
  197. lua_rawgeti (L, LUA_REGISTRYINDEX, fd->idx);
  198. break;
  199. default:
  200. lua_pushnil (L);
  201. break;
  202. }
  203. return 1;
  204. }
  205. /***
  206. * @function ucl_object_push_lua(L, obj, allow_array)
  207. * This is a `C` function to push `UCL` object as lua variable. This function
  208. * converts `obj` to lua representation using the following conversions:
  209. *
  210. * - *scalar* values are directly presented by lua objects
  211. * - *userdata* values are converted to lua function objects using `LUA_REGISTRYINDEX`,
  212. * this can be used to pass functions from lua to c and vice-versa
  213. * - *arrays* are converted to lua tables with numeric indicies suitable for `ipairs` iterations
  214. * - *objects* are converted to lua tables with string indicies
  215. * @param {lua_State} L lua state pointer
  216. * @param {ucl_object_t} obj object to push
  217. * @param {bool} allow_array expand implicit arrays (should be true for all but partial arrays)
  218. * @return {int} `1` if an object is pushed to lua
  219. */
  220. int
  221. ucl_object_push_lua (lua_State *L, const ucl_object_t *obj, bool allow_array)
  222. {
  223. switch (obj->type) {
  224. case UCL_OBJECT:
  225. return ucl_object_lua_push_object (L, obj, allow_array);
  226. case UCL_ARRAY:
  227. return ucl_object_lua_push_array (L, obj->value.av);
  228. default:
  229. return ucl_object_lua_push_scalar (L, obj, allow_array);
  230. }
  231. }
  232. /**
  233. * Parse lua table into object top
  234. * @param L
  235. * @param top
  236. * @param idx
  237. */
  238. static ucl_object_t *
  239. ucl_object_lua_fromtable (lua_State *L, int idx)
  240. {
  241. ucl_object_t *obj, *top = NULL;
  242. size_t keylen;
  243. const char *k;
  244. bool is_array = true;
  245. int max = INT_MIN;
  246. if (idx < 0) {
  247. /* For negative indicies we want to invert them */
  248. idx = lua_gettop (L) + idx + 1;
  249. }
  250. /* Check for array */
  251. lua_pushnil (L);
  252. while (lua_next (L, idx) != 0) {
  253. if (lua_type (L, -2) == LUA_TNUMBER) {
  254. double num = lua_tonumber (L, -2);
  255. if (num == (int)num) {
  256. if (num > max) {
  257. max = num;
  258. }
  259. }
  260. else {
  261. /* Keys are not integer */
  262. lua_pop (L, 2);
  263. is_array = false;
  264. break;
  265. }
  266. }
  267. else {
  268. /* Keys are not numeric */
  269. lua_pop (L, 2);
  270. is_array = false;
  271. break;
  272. }
  273. lua_pop (L, 1);
  274. }
  275. /* Table iterate */
  276. if (is_array) {
  277. int i;
  278. top = ucl_object_typed_new (UCL_ARRAY);
  279. for (i = 1; i <= max; i ++) {
  280. lua_pushinteger (L, i);
  281. lua_gettable (L, idx);
  282. obj = ucl_object_lua_fromelt (L, lua_gettop (L));
  283. if (obj != NULL) {
  284. ucl_array_append (top, obj);
  285. }
  286. }
  287. }
  288. else {
  289. lua_pushnil (L);
  290. top = ucl_object_typed_new (UCL_OBJECT);
  291. while (lua_next (L, idx) != 0) {
  292. /* copy key to avoid modifications */
  293. k = lua_tolstring (L, -2, &keylen);
  294. obj = ucl_object_lua_fromelt (L, lua_gettop (L));
  295. if (obj != NULL) {
  296. ucl_object_insert_key (top, obj, k, keylen, true);
  297. }
  298. lua_pop (L, 1);
  299. }
  300. }
  301. return top;
  302. }
  303. /**
  304. * Get a single element from lua to object obj
  305. * @param L
  306. * @param obj
  307. * @param idx
  308. */
  309. static ucl_object_t *
  310. ucl_object_lua_fromelt (lua_State *L, int idx)
  311. {
  312. int type;
  313. double num;
  314. ucl_object_t *obj = NULL;
  315. struct ucl_lua_funcdata *fd;
  316. type = lua_type (L, idx);
  317. switch (type) {
  318. case LUA_TSTRING:
  319. obj = ucl_object_fromstring_common (lua_tostring (L, idx), 0, 0);
  320. break;
  321. case LUA_TNUMBER:
  322. num = lua_tonumber (L, idx);
  323. if (num == (int64_t)num) {
  324. obj = ucl_object_fromint (num);
  325. }
  326. else {
  327. obj = ucl_object_fromdouble (num);
  328. }
  329. break;
  330. case LUA_TBOOLEAN:
  331. obj = ucl_object_frombool (lua_toboolean (L, idx));
  332. break;
  333. case LUA_TUSERDATA:
  334. if (lua_topointer (L, idx) == ucl_null) {
  335. obj = ucl_object_typed_new (UCL_NULL);
  336. }
  337. break;
  338. case LUA_TTABLE:
  339. case LUA_TFUNCTION:
  340. case LUA_TTHREAD:
  341. if (luaL_getmetafield (L, idx, "__gen_ucl")) {
  342. if (lua_isfunction (L, -1)) {
  343. lua_settop (L, 3); /* gen, obj, func */
  344. lua_insert (L, 1); /* func, gen, obj */
  345. lua_insert (L, 2); /* func, obj, gen */
  346. lua_call(L, 2, 1);
  347. obj = ucl_object_lua_fromelt (L, 1);
  348. }
  349. lua_pop (L, 2);
  350. }
  351. else {
  352. if (type == LUA_TTABLE) {
  353. obj = ucl_object_lua_fromtable (L, idx);
  354. }
  355. else if (type == LUA_TFUNCTION) {
  356. fd = malloc (sizeof (*fd));
  357. if (fd != NULL) {
  358. lua_pushvalue (L, idx);
  359. fd->L = L;
  360. fd->ret = NULL;
  361. fd->idx = luaL_ref (L, LUA_REGISTRYINDEX);
  362. obj = ucl_object_new_userdata (lua_ucl_userdata_dtor,
  363. lua_ucl_userdata_emitter);
  364. obj->type = UCL_USERDATA;
  365. obj->value.ud = (void *)fd;
  366. }
  367. }
  368. }
  369. break;
  370. }
  371. return obj;
  372. }
  373. /**
  374. * @function ucl_object_lua_import(L, idx)
  375. * Extracts ucl object from lua variable at `idx` position,
  376. * @see ucl_object_push_lua for conversion definitions
  377. * @param {lua_state} L lua state machine pointer
  378. * @param {int} idx index where the source variable is placed
  379. * @return {ucl_object_t} new ucl object extracted from lua variable. Reference count of this object is 1,
  380. * this object thus needs to be unref'ed after usage.
  381. */
  382. ucl_object_t *
  383. ucl_object_lua_import (lua_State *L, int idx)
  384. {
  385. ucl_object_t *obj;
  386. int t;
  387. t = lua_type (L, idx);
  388. switch (t) {
  389. case LUA_TTABLE:
  390. obj = ucl_object_lua_fromtable (L, idx);
  391. break;
  392. default:
  393. obj = ucl_object_lua_fromelt (L, idx);
  394. break;
  395. }
  396. return obj;
  397. }
  398. static int
  399. lua_ucl_parser_init (lua_State *L)
  400. {
  401. struct ucl_parser *parser, **pparser;
  402. int flags = 0;
  403. if (lua_gettop (L) >= 1) {
  404. flags = lua_tonumber (L, 1);
  405. }
  406. parser = ucl_parser_new (flags);
  407. if (parser == NULL) {
  408. lua_pushnil (L);
  409. }
  410. pparser = lua_newuserdata (L, sizeof (parser));
  411. *pparser = parser;
  412. luaL_getmetatable (L, PARSER_META);
  413. lua_setmetatable (L, -2);
  414. return 1;
  415. }
  416. static struct ucl_parser *
  417. lua_ucl_parser_get (lua_State *L, int index)
  418. {
  419. return *((struct ucl_parser **) luaL_checkudata(L, index, PARSER_META));
  420. }
  421. /***
  422. * @method parser:parse_file(name)
  423. * Parse UCL object from file.
  424. * @param {string} name filename to parse
  425. * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
  426. @example
  427. local parser = ucl.parser()
  428. local res,err = parser:parse_file('/some/file.conf')
  429. if not res then
  430. print('parser error: ' .. err)
  431. else
  432. -- Do something with object
  433. end
  434. */
  435. static int
  436. lua_ucl_parser_parse_file (lua_State *L)
  437. {
  438. struct ucl_parser *parser;
  439. const char *file;
  440. int ret = 2;
  441. parser = lua_ucl_parser_get (L, 1);
  442. file = luaL_checkstring (L, 2);
  443. if (parser != NULL && file != NULL) {
  444. if (ucl_parser_add_file (parser, file)) {
  445. lua_pushboolean (L, true);
  446. ret = 1;
  447. }
  448. else {
  449. lua_pushboolean (L, false);
  450. lua_pushstring (L, ucl_parser_get_error (parser));
  451. }
  452. }
  453. else {
  454. lua_pushboolean (L, false);
  455. lua_pushstring (L, "invalid arguments");
  456. }
  457. return ret;
  458. }
  459. /***
  460. * @method parser:parse_string(input)
  461. * Parse UCL object from file.
  462. * @param {string} input string to parse
  463. * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
  464. */
  465. static int
  466. lua_ucl_parser_parse_string (lua_State *L)
  467. {
  468. struct ucl_parser *parser;
  469. const char *string;
  470. size_t llen;
  471. int ret = 2;
  472. parser = lua_ucl_parser_get (L, 1);
  473. string = luaL_checklstring (L, 2, &llen);
  474. if (parser != NULL && string != NULL) {
  475. if (ucl_parser_add_chunk (parser, (const unsigned char *)string, llen)) {
  476. lua_pushboolean (L, true);
  477. ret = 1;
  478. }
  479. else {
  480. lua_pushboolean (L, false);
  481. lua_pushstring (L, ucl_parser_get_error (parser));
  482. }
  483. }
  484. else {
  485. lua_pushboolean (L, false);
  486. lua_pushstring (L, "invalid arguments");
  487. }
  488. return ret;
  489. }
  490. /***
  491. * @method parser:get_object()
  492. * Get top object from parser and export it to lua representation.
  493. * @return {variant or nil} ucl object as lua native variable
  494. */
  495. static int
  496. lua_ucl_parser_get_object (lua_State *L)
  497. {
  498. struct ucl_parser *parser;
  499. ucl_object_t *obj;
  500. int ret = 1;
  501. parser = lua_ucl_parser_get (L, 1);
  502. obj = ucl_parser_get_object (parser);
  503. if (obj != NULL) {
  504. ret = ucl_object_push_lua (L, obj, false);
  505. /* no need to keep reference */
  506. ucl_object_unref (obj);
  507. }
  508. else {
  509. lua_pushnil (L);
  510. }
  511. return ret;
  512. }
  513. static int
  514. lua_ucl_parser_gc (lua_State *L)
  515. {
  516. struct ucl_parser *parser;
  517. parser = lua_ucl_parser_get (L, 1);
  518. ucl_parser_free (parser);
  519. return 0;
  520. }
  521. static void
  522. lua_ucl_parser_mt (lua_State *L)
  523. {
  524. luaL_newmetatable (L, PARSER_META);
  525. lua_pushvalue(L, -1);
  526. lua_setfield(L, -2, "__index");
  527. lua_pushcfunction (L, lua_ucl_parser_gc);
  528. lua_setfield (L, -2, "__gc");
  529. lua_pushcfunction (L, lua_ucl_parser_parse_file);
  530. lua_setfield (L, -2, "parse_file");
  531. lua_pushcfunction (L, lua_ucl_parser_parse_string);
  532. lua_setfield (L, -2, "parse_string");
  533. lua_pushcfunction (L, lua_ucl_parser_get_object);
  534. lua_setfield (L, -2, "get_object");
  535. lua_pop (L, 1);
  536. }
  537. static int
  538. lua_ucl_to_string (lua_State *L, const ucl_object_t *obj, enum ucl_emitter type)
  539. {
  540. unsigned char *result;
  541. result = ucl_object_emit (obj, type);
  542. if (result != NULL) {
  543. lua_pushstring (L, (const char *)result);
  544. free (result);
  545. }
  546. else {
  547. lua_pushnil (L);
  548. }
  549. return 1;
  550. }
  551. static int
  552. lua_ucl_to_json (lua_State *L)
  553. {
  554. ucl_object_t *obj;
  555. int format = UCL_EMIT_JSON;
  556. if (lua_gettop (L) > 1) {
  557. if (lua_toboolean (L, 2)) {
  558. format = UCL_EMIT_JSON_COMPACT;
  559. }
  560. }
  561. obj = ucl_object_lua_import (L, 1);
  562. if (obj != NULL) {
  563. lua_ucl_to_string (L, obj, format);
  564. ucl_object_unref (obj);
  565. }
  566. else {
  567. lua_pushnil (L);
  568. }
  569. return 1;
  570. }
  571. static int
  572. lua_ucl_to_config (lua_State *L)
  573. {
  574. ucl_object_t *obj;
  575. obj = ucl_object_lua_import (L, 1);
  576. if (obj != NULL) {
  577. lua_ucl_to_string (L, obj, UCL_EMIT_CONFIG);
  578. ucl_object_unref (obj);
  579. }
  580. else {
  581. lua_pushnil (L);
  582. }
  583. return 1;
  584. }
  585. /***
  586. * @function ucl.to_format(var, format)
  587. * Converts lua variable `var` to the specified `format`. Formats supported are:
  588. *
  589. * - `json` - fine printed json
  590. * - `json-compact` - compacted json
  591. * - `config` - fine printed configuration
  592. * - `ucl` - same as `config`
  593. * - `yaml` - embedded yaml
  594. *
  595. * If `var` contains function, they are called during output formatting and if
  596. * they return string value, then this value is used for ouptut.
  597. * @param {variant} var any sort of lua variable (if userdata then metafield `__to_ucl` is searched for output)
  598. * @param {string} format any available format
  599. * @return {string} string representation of `var` in the specific `format`.
  600. * @example
  601. local table = {
  602. str = 'value',
  603. num = 100500,
  604. null = ucl.null,
  605. func = function ()
  606. return 'huh'
  607. end
  608. }
  609. print(ucl.to_format(table, 'ucl'))
  610. -- Output:
  611. --[[
  612. num = 100500;
  613. str = "value";
  614. null = null;
  615. func = "huh";
  616. --]]
  617. */
  618. static int
  619. lua_ucl_to_format (lua_State *L)
  620. {
  621. ucl_object_t *obj;
  622. int format = UCL_EMIT_JSON;
  623. if (lua_gettop (L) > 1) {
  624. if (lua_type (L, 2) == LUA_TNUMBER) {
  625. format = lua_tonumber (L, 2);
  626. if (format < 0 || format >= UCL_EMIT_YAML) {
  627. lua_pushnil (L);
  628. return 1;
  629. }
  630. }
  631. else if (lua_type (L, 2) == LUA_TSTRING) {
  632. const char *strtype = lua_tostring (L, 2);
  633. if (strcasecmp (strtype, "json") == 0) {
  634. format = UCL_EMIT_JSON;
  635. }
  636. else if (strcasecmp (strtype, "json-compact") == 0) {
  637. format = UCL_EMIT_JSON_COMPACT;
  638. }
  639. else if (strcasecmp (strtype, "yaml") == 0) {
  640. format = UCL_EMIT_YAML;
  641. }
  642. else if (strcasecmp (strtype, "config") == 0 ||
  643. strcasecmp (strtype, "ucl") == 0) {
  644. format = UCL_EMIT_CONFIG;
  645. }
  646. }
  647. }
  648. obj = ucl_object_lua_import (L, 1);
  649. if (obj != NULL) {
  650. lua_ucl_to_string (L, obj, format);
  651. ucl_object_unref (obj);
  652. }
  653. else {
  654. lua_pushnil (L);
  655. }
  656. return 1;
  657. }
  658. static int
  659. lua_ucl_null_tostring (lua_State* L)
  660. {
  661. lua_pushstring (L, "null");
  662. return 1;
  663. }
  664. static void
  665. lua_ucl_null_mt (lua_State *L)
  666. {
  667. luaL_newmetatable (L, NULL_META);
  668. lua_pushcfunction (L, lua_ucl_null_tostring);
  669. lua_setfield (L, -2, "__tostring");
  670. lua_pop (L, 1);
  671. }
  672. int
  673. luaopen_ucl (lua_State *L)
  674. {
  675. lua_ucl_parser_mt (L);
  676. lua_ucl_null_mt (L);
  677. /* Create the refs weak table: */
  678. lua_createtable (L, 0, 2);
  679. lua_pushliteral (L, "v"); /* tbl, "v" */
  680. lua_setfield (L, -2, "__mode");
  681. lua_pushvalue (L, -1); /* tbl, tbl */
  682. lua_setmetatable (L, -2); /* tbl */
  683. lua_setfield (L, LUA_REGISTRYINDEX, "ucl.refs");
  684. lua_newtable (L);
  685. lua_pushcfunction (L, lua_ucl_parser_init);
  686. lua_setfield (L, -2, "parser");
  687. lua_pushcfunction (L, lua_ucl_to_json);
  688. lua_setfield (L, -2, "to_json");
  689. lua_pushcfunction (L, lua_ucl_to_config);
  690. lua_setfield (L, -2, "to_config");
  691. lua_pushcfunction (L, lua_ucl_to_format);
  692. lua_setfield (L, -2, "to_format");
  693. ucl_null = lua_newuserdata (L, 0);
  694. luaL_getmetatable (L, NULL_META);
  695. lua_setmetatable (L, -2);
  696. lua_pushvalue (L, -1);
  697. lua_setfield (L, LUA_REGISTRYINDEX, "ucl.null");
  698. lua_setfield (L, -2, "null");
  699. return 1;
  700. }
  701. struct ucl_lua_funcdata*
  702. ucl_object_toclosure (const ucl_object_t *obj)
  703. {
  704. if (obj == NULL || obj->type != UCL_USERDATA) {
  705. return NULL;
  706. }
  707. return (struct ucl_lua_funcdata*)obj->value.ud;
  708. }