lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Feb 19, 2012, Jay Carlso wrote:

> On Feb 19, 2012, at 6:50 AM, Xavier Wang wrote:
>
>> hi list, Does anyone knows Scheme?
>
> One of the VLISP authors has released a couple of Lua apps, so there is an existence proof. I forget if he's paying attention to lua-l.

I was off the list, but I resubscribed.  As for knowledgeable
Schemers, you should include the Lua authors.  They openly state they
were influenced by Scheme.

As for me, my latest effort is to make my Datalog interpreter
available as a Redhat package.  The guts of the interpreter is
implemented in Lua and there is a pure Lua package that makes Datalog
available to other Lua applications.

https://bugzilla.redhat.com/show_bug.cgi?id=720857

I also updated the package so that it works with Lua 5.2.  The
interesting changes I had to make follow.

John

--- a/datalog.c
+++ b/datalog.c
@@ -72,7 +72,11 @@ dl_lua(dl_db_t L)
 }

 static const luaL_Reg lualibs[] = {
+#if LUA_VERSION_NUM < 502
   {"", luaopen_base},
+#else
+  {"_G", luaopen_base},
+#endif
   {LUA_TABLIBNAME, luaopen_table},
   {LUA_STRLIBNAME, luaopen_string},
   {NULL, NULL}
@@ -82,11 +86,16 @@ DATALOG_API dl_db_t
 dl_open(void)
 {
   lua_State *L = luaL_newstate();
-  const luaL_Reg *lib = lualibs;
-  for (; lib->func; lib++) {	/* Load libraries used by the */
-    lua_pushcfunction(L, lib->func); /* Lua datalog program. */
+  const luaL_Reg *lib = lualibs; /* Load libraries used by the */
+  for (; lib->func; lib++) {	 /* Lua datalog program. */
+#if LUA_VERSION_NUM < 502
+    lua_pushcfunction(L, lib->func);
     lua_pushstring(L, lib->name);
     lua_call(L, 1, 0);
+#else
+    luaL_requiref(L, lib->name, lib->func, 1);
+    lua_pop(L, 1);		/* remove lib */
+#endif
   }
   if (dl_lua(L))		/* Load the Lua program. */
     return NULL;
@@ -330,6 +339,7 @@ dl_ask(dl_db_t L, dl_answers_t *a)
   lua_Integer size;	      /* Size of the character array block. */
   size_t len;		     /* Used to compute lengths of strings. */
   char *s;		   /* Stores the location to insert a char. */
+  const char *ls;	   /* A string from lua. */
   char **p;		 /* Stores the location to insert a char *. */
   dl_answers_t b;
   *a = NULL;
@@ -402,8 +412,9 @@ dl_ask(dl_db_t L, dl_answers_t *a)
     dl_free(b);
     return 1;
   }
-  len = lua_strlen(L, -1) + 1;
-  memcpy(s, lua_tostring(L, -1), len); /* Copy string. */
+  ls = lua_tolstring(L, -1, &len); /* Get string and length. */
+  len++;			   /* Make room for null byte. */
+  memcpy(s, ls, len);		   /* Copy string. */
   s += len;		/* Update s to be the next unused location. */
   *++p = s;    /* Update p to be the next location and set it to s. */
   lua_pop(L, 1);
@@ -423,8 +434,9 @@ dl_ask(dl_db_t L, dl_answers_t *a)
 	dl_free(b);
 	return 1;
       }
-      len = lua_strlen(L, -1) + 1;
-      memcpy(s, lua_tostring(L, -1), len); /* Copy string. */
+      ls = lua_tolstring(L, -1, &len); /* Get string and length. */
+      len++;			       /* Make room for null byte. */
+      memcpy(s, ls, len);	       /* Copy string. */
       s += len;
       *++p = s;
       lua_pop(L, 1);