Browse Source

Fix term access.

scossu 2 days ago
parent
commit
73b284df33
3 changed files with 25 additions and 16 deletions
  1. 21 15
      lua/src/term.c
  2. 2 0
      lua/src/triple.c
  3. 2 1
      lua/test.lua

+ 21 - 15
lua/src/term.c

@@ -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);
 

+ 2 - 0
lua/src/triple.c

@@ -47,6 +47,8 @@ static int gc (lua_State *L)
  * Note that this is only a visual representation, as terms cannot be modified
  * outside of their containing triple (unlike in the C library).
  *
+ * @FIXME this doesn't allow chaining access methods, e.g. trp.s.data
+ *
  * To get an independent copy of a triple term, use triple.copy_term().
  */
 static int get_term (lua_State *L)

+ 2 - 1
lua/test.lua

@@ -3,9 +3,10 @@ triple = require "triple"
 trp = triple.new (
     term.iriref_new("urn:s:1"),
     term.iriref_new("urn:p:1"),
-    term.iriref_new("urn:o:1"))
+    term.lit_new("hello", nil, "us-EN"))
 
 t1 = term.iriref_new("urn:s:11")
+t2 = term.lit_new("123", "xsd:int")
 mt = {z =99}
 mt["__index"] = mt
 a = {x = 1, y = 2}