2 * Hic Est Leo -- Bindings for Lua
4 * (c) 2015 Martin Mares <mj@ucw.cz>
20 /*** Proxy for OSM object attributes ***/
28 static void push_proxy_tag(lua_State *L, struct proxy_tag *t)
30 SWIG_NewPointerObj(L, t, SWIGTYPE_p_proxy_tag, 0);
33 static const char *proxy_tag_index(struct proxy_tag *t, const char *key)
35 osm_key_t k = osm_key_encode(key);
36 osm_val_t v = osm_obj_find_tag(t->o, k);
37 return osm_val_decode(v);
45 const char *index(const char *key);
49 // This overrides default index function generated by SWIG.
50 // The tag proxy therefore cannot have any methods.
51 SWIG_Lua_get_class_metatable(L, "proxy_tag");
52 SWIG_Lua_add_function(L, "__index", _wrap_proxy_tag_index);
56 /*** Proxy for style properties ***/
61 struct style_results *sr;
65 static void push_proxy_style(lua_State *L, struct proxy_style *s)
67 SWIG_NewPointerObj(L, s, SWIGTYPE_p_proxy_style, 0);
70 static const char *proxy_style_index(struct proxy_style *s, const char *key)
77 struct proxy_style { };
80 const char *index(const char *key);
84 // This overrides default index function generated by SWIG.
85 SWIG_Lua_get_class_metatable(L, "proxy_style");
86 SWIG_Lua_add_function(L, "__index", _wrap_proxy_style_index);
90 /*** Compilation and running of expressions ***/
94 static lua_State *lua;
98 lua = luaL_newstate();
104 // SWIG init code leaves data in Lua stack. Get rid of it.
105 lua_pop(L, lua_gettop(L));
107 lua_checkstack(L, 30);
109 lua_pushliteral(L, "Leo");
111 lua_settable(L, LUA_REGISTRYINDEX);
115 // Not used at the moment (FIXME)
117 static char *current_src;
119 static const char *expr_reader(lua_State *L UNUSED, void *data UNUSED, size_t *sizep)
123 char *res = current_src;
124 *sizep = strlen(res);
137 void expr_compile(void *key, char *source)
141 lua_pushliteral(L, "Leo"); // stack: "Leo"
142 lua_gettable(L, LUA_REGISTRYINDEX); // stack: leo-table
143 ASSERT(lua_istable(L, -1));
144 lua_pushlightuserdata(L, key); // stack: leo-table, key
147 current_src = source;
148 int err = lua_load(L, expr_reader, NULL, "@(expr)", NULL);
150 int err = luaL_loadbuffer(L, source, strlen(source), "@expression");
153 css_error(lua_tostring(L, -1));
154 // stack: leo-table, key, function
156 lua_settable(L, -3); // stack: leo-table
157 lua_pop(L, 1); // stack: --
160 void expr_execute(void *key, struct style_results *r, layer_t layer)
164 struct proxy_tag pt = { .o = r->obj };
165 push_proxy_tag(L, &pt); // stack: proxy
166 lua_setglobal(L, "t"); // stack: --
168 struct proxy_style ps = { .sr = r, .layer = layer };
169 push_proxy_style(L, &ps); // stack: proxy
170 lua_setglobal(L, "s"); // stack: --
172 lua_pushliteral(L, "Leo"); // stack: "Leo"
173 lua_gettable(L, LUA_REGISTRYINDEX); // stack: leo-table
174 ASSERT(lua_istable(L, -1));
175 lua_pushlightuserdata(L, key); // stack: leo-table, key
176 lua_gettable(L, -2); // stack: leo-table, function
177 lua_remove(L, -2); // stack: function
178 ASSERT(lua_isfunction(L, -1));
180 lua_call(L, 0, 0); // stack: --