xref: /freebsd/contrib/libucl/include/lua_ucl.h (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
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 #ifndef LUA_UCL_H_
24 #define LUA_UCL_H_
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include <lua.h>
31 #include <lauxlib.h>
32 #include <lualib.h>
33 #include "ucl.h"
34 
35 /**
36  * Closure structure for lua function storing inside UCL
37  */
38 struct ucl_lua_funcdata {
39 	lua_State *L;
40 	int idx;
41 	char *ret;
42 };
43 
44 /**
45  * Initialize lua UCL API
46  */
47 UCL_EXTERN int luaopen_ucl (lua_State *L);
48 
49 /**
50  * Import UCL object from lua state
51  * @param L lua state
52  * @param idx index of object at the lua stack to convert to UCL
53  * @return new UCL object or NULL, the caller should unref object after using
54  */
55 UCL_EXTERN ucl_object_t* ucl_object_lua_import (lua_State *L, int idx);
56 
57 /**
58  * Import UCL object from lua state, escaping JSON strings
59  * @param L lua state
60  * @param idx index of object at the lua stack to convert to UCL
61  * @return new UCL object or NULL, the caller should unref object after using
62  */
63 UCL_EXTERN ucl_object_t* ucl_object_lua_import_escape (lua_State *L, int idx);
64 
65 /**
66  * Push an object to lua
67  * @param L lua state
68  * @param obj object to push
69  * @param allow_array traverse over implicit arrays
70  */
71 UCL_EXTERN int ucl_object_push_lua (lua_State *L,
72 		const ucl_object_t *obj, bool allow_array);
73 /**
74  * Push an object to lua replacing all ucl.null with `false`
75  * @param L lua state
76  * @param obj object to push
77  * @param allow_array traverse over implicit arrays
78  */
79 UCL_EXTERN int ucl_object_push_lua_filter_nil (lua_State *L,
80 											   const ucl_object_t *obj,
81 											   bool allow_array);
82 
83 UCL_EXTERN struct ucl_lua_funcdata* ucl_object_toclosure (const ucl_object_t *obj);
84 
85 #endif /* LUA_UCL_H_ */
86