|
@@ -63,21 +63,29 @@ static int l_iriref_new_rel (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
+/** @brief create a new literal.
|
|
|
+ *
|
|
|
+ * Argument 2 (data type) and 3 (lang) are exclusive. If both are specified,
|
|
|
+ * datatype has precedence. If both are nil, datatype is set to the default
|
|
|
+ * (xsd:string).
|
|
|
+ */
|
|
|
static int l_lit_new (lua_State *L)
|
|
|
{
|
|
|
const char
|
|
|
*data = luaL_checkstring (L, 1),
|
|
|
- *dtype_str = luaL_checkstring (L, 2);
|
|
|
- const char *lang = luaL_checkstring (L, 3);
|
|
|
+ *dtype_str = lua_tostring (L, 2);
|
|
|
+ const char *lang = lua_tostring (L, 3);
|
|
|
|
|
|
LSUP_Term **tp = allocate_term (L);
|
|
|
LSUP_Term *dtype;
|
|
|
|
|
|
- if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
|
|
|
- else {
|
|
|
- dtype = (dtype_str) ? LSUP_iriref_new (dtype_str, NULL) : NULL;
|
|
|
+ if (dtype_str) {
|
|
|
+ // TODO check memory leak.
|
|
|
+ dtype = LSUP_iriref_new (dtype_str, LSUP_default_nsm);
|
|
|
*tp = LSUP_literal_new (data, dtype);
|
|
|
}
|
|
|
+ else if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
|
|
|
+ else *tp = LSUP_literal_new (data, NULL);
|
|
|
|
|
|
if (!*tp) return luaL_error (L, "Error while creating a term!");
|
|
|
|
|
@@ -184,14 +192,8 @@ static int get_attr (lua_State *L)
|
|
|
return 1; // Return metamethod to be called as a function.
|
|
|
|
|
|
luaL_getmetafield (L, 1, "getters");
|
|
|
- if (lua_getfield (L, -1, attr) != LUA_TNIL) {
|
|
|
- //printf ("Found getter: %s\n", attr);
|
|
|
- int rc = lua_tocfunction (L, -1)(L);
|
|
|
- lua_pop (L, 2);
|
|
|
-
|
|
|
- return rc;
|
|
|
- }
|
|
|
- lua_pop (L, 1);
|
|
|
+ if (lua_getfield (L, -1, attr) != LUA_TNIL)
|
|
|
+ return lua_tocfunction (L, -1)(L);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -254,8 +256,10 @@ static int get_iriref_frag (lua_State *L)
|
|
|
|
|
|
static int get_lit_datatype (lua_State *L)
|
|
|
{
|
|
|
+ printf ("Getting datatype.\n");
|
|
|
LSUP_Term *t = check_term (L);
|
|
|
- if (!LSUP_IS_LITERAL (t)) return luaL_error (L, "Term is not a literal.");
|
|
|
+ if (!LSUP_IS_LITERAL (t))
|
|
|
+ return luaL_error (L, "Term is not a literal.", 2);
|
|
|
|
|
|
lua_pushstring (L, t->datatype->data);
|
|
|
|
|
@@ -266,7 +270,8 @@ static int get_lit_datatype (lua_State *L)
|
|
|
static int get_lit_lang (lua_State *L)
|
|
|
{
|
|
|
LSUP_Term *t = check_term (L);
|
|
|
- if (!LSUP_IS_LITERAL (t)) return luaL_error (L, "Term is not a literal.");
|
|
|
+ if (!LSUP_IS_LITERAL (t))
|
|
|
+ return luaL_error (L, "Term is not a literal.", 2);
|
|
|
|
|
|
lua_pushstring (L, t->lang);
|
|
|
|
|
@@ -352,6 +357,7 @@ static const luaL_Reg term_lib_meth [] = {
|
|
|
|
|
|
int luaopen_term (lua_State *L)
|
|
|
{
|
|
|
+ LSUP_init(); // This is idempotent: no problem calling it multiple times.
|
|
|
luaL_newmetatable (L, "LSUP.Term");
|
|
|
luaL_setfuncs (L, term_lib_meth, 0);
|
|
|
|