|
@@ -15,6 +15,16 @@ LSUP_Term **allocate_term (lua_State *L)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+LSUP_TermSet **allocate_tset (lua_State *L)
|
|
|
|
+{
|
|
|
|
+ LSUP_TermSet **ts_p = lua_newuserdatauv (L, sizeof (*ts_p), 1);
|
|
|
|
+ luaL_getmetatable (L, "LSUP.TermSet");
|
|
|
|
+ lua_setmetatable (L, -2);
|
|
|
|
+
|
|
|
|
+ return ts_p;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
/*
|
|
/*
|
|
* Factory methods.
|
|
* Factory methods.
|
|
*/
|
|
*/
|
|
@@ -310,7 +320,66 @@ static int l_term_set_attr (lua_State *L)
|
|
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
- * Ancillary types not directly created from Lua.
|
|
|
|
|
|
+ * Ancillary types
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Term set.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+static int term_set_iter_next (lua_State *L)
|
|
|
|
+{
|
|
|
|
+ LSUP_TermSet *ts =
|
|
|
|
+ *(LSUP_TermSet **)lua_touserdata (L, lua_upvalueindex (1));
|
|
|
|
+ size_t *ip = lua_touserdata (L, lua_upvalueindex (2));
|
|
|
|
+
|
|
|
|
+ LSUP_Term **tp = allocate_term (L);
|
|
|
|
+ LSUP_Term *tmp = NULL;
|
|
|
|
+ LSUP_rc rc = LSUP_term_set_next (ts, ip, &tmp);
|
|
|
|
+
|
|
|
|
+ LUA_PCHECK (rc, "Error iterating over term set");
|
|
|
|
+
|
|
|
|
+ if (rc == LSUP_END) {
|
|
|
|
+ free (ip);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ *tp = LSUP_term_copy (tmp);
|
|
|
|
+ if (!*tp) {
|
|
|
|
+ free (ip);
|
|
|
|
+ luaL_error (L, "Error allocating term.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int l_term_set_iter_init (lua_State *L)
|
|
|
|
+{
|
|
|
|
+ size_t *ip = malloc (sizeof (*ip));
|
|
|
|
+ LUA_NLCHECK (ip, "Error allocating tset iterator.");
|
|
|
|
+ *ip = 0;
|
|
|
|
+
|
|
|
|
+ lua_pushlightuserdata (L, ip);
|
|
|
|
+ STACK_DUMP(L, "Before pushing tset next closure");
|
|
|
|
+ lua_pushcclosure (L, term_set_iter_next, 2);
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int term_set_gc (lua_State *L)
|
|
|
|
+{
|
|
|
|
+ LSUP_TermSet **ts_p = lua_touserdata (L, 1);
|
|
|
|
+ LSUP_term_set_free (*ts_p);
|
|
|
|
+ *ts_p = NULL;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Link map.
|
|
*/
|
|
*/
|
|
|
|
|
|
static int lmap_iter_next (lua_State *L)
|
|
static int lmap_iter_next (lua_State *L)
|
|
@@ -328,14 +397,11 @@ static int lmap_iter_next (lua_State *L)
|
|
LSUP_rc rc = LSUP_link_map_next (it, &tmp, &ts);
|
|
LSUP_rc rc = LSUP_link_map_next (it, &tmp, &ts);
|
|
|
|
|
|
if (rc != LSUP_OK) {
|
|
if (rc != LSUP_OK) {
|
|
- if (rc == LSUP_END) {
|
|
|
|
- lua_pushnil (L);
|
|
|
|
- lua_pushstring (L, "End of the loop.");
|
|
|
|
- return 2;
|
|
|
|
- }
|
|
|
|
|
|
+ if (rc == LSUP_END) return 0;
|
|
else LUA_PCHECK (rc, "Error iterating over link map");
|
|
else LUA_PCHECK (rc, "Error iterating over link map");
|
|
}
|
|
}
|
|
*link_p = LSUP_term_copy (tmp);
|
|
*link_p = LSUP_term_copy (tmp);
|
|
|
|
+ LUA_NLCHECK (*link_p, "Error allocating term.");
|
|
|
|
|
|
size_t i = 0;
|
|
size_t i = 0;
|
|
tmp = NULL;
|
|
tmp = NULL;
|
|
@@ -500,15 +566,28 @@ int luaopen_lsup_term (lua_State *L)
|
|
// Set getters table as a value for the Term metatable.
|
|
// Set getters table as a value for the Term metatable.
|
|
lua_setfield (L, -2, "getters");
|
|
lua_setfield (L, -2, "getters");
|
|
|
|
|
|
- // Metatables for ancillary types.
|
|
|
|
|
|
+ /*
|
|
|
|
+ * Metatables for ancillary types.
|
|
|
|
+ */
|
|
|
|
+ // Term set.
|
|
|
|
+ luaL_newmetatable (L, "LSUP.TermSet");
|
|
|
|
+ lua_pushvalue (L, -1);
|
|
|
|
+ lua_setfield (L, -2, "__index");
|
|
|
|
+ lua_pushcfunction (L, term_set_gc);
|
|
|
|
+ lua_setfield (L, -2, "__gc");
|
|
|
|
+ lua_pushcfunction (L, l_term_set_iter_init);
|
|
|
|
+ lua_setfield (L, -2, "__pairs");
|
|
|
|
+
|
|
|
|
+ // Link map.
|
|
luaL_newmetatable (L, "LSUP.LinkMap");
|
|
luaL_newmetatable (L, "LSUP.LinkMap");
|
|
lua_pushvalue (L, -1);
|
|
lua_pushvalue (L, -1);
|
|
lua_setfield (L, -2, "__index");
|
|
lua_setfield (L, -2, "__index");
|
|
lua_pushcfunction (L, link_map_gc);
|
|
lua_pushcfunction (L, link_map_gc);
|
|
lua_setfield (L, -2, "__gc");
|
|
lua_setfield (L, -2, "__gc");
|
|
lua_pushcfunction (L, l_lmap_iter_init);
|
|
lua_pushcfunction (L, l_lmap_iter_init);
|
|
- lua_setfield (L, -2, "iter");
|
|
|
|
|
|
+ lua_setfield (L, -2, "__pairs");
|
|
|
|
|
|
|
|
+ // Link map iterator.
|
|
luaL_newmetatable (L, "LSUP.LMapIterator");
|
|
luaL_newmetatable (L, "LSUP.LMapIterator");
|
|
lua_pushcfunction (L, lmap_iter_gc);
|
|
lua_pushcfunction (L, lmap_iter_gc);
|
|
lua_setfield (L, -2, "__gc");
|
|
lua_setfield (L, -2, "__gc");
|