Lua Power Patches

Lua power patches are patches for the Lua interpreter that modify its behavior in some way. Here are some patches that I've written.

C Function Source

This patch allows you to retrieve the source (as in which dynamic module a function is defined in) of a C function with the debug library instead of always using '=[C]'.

c-func-source.patch
diff -Naur lua-5.1.4/src/Makefile my-lua-changes/src/Makefile
--- lua-5.1.4/src/Makefile	2008-01-19 13:37:58.000000000 -0600
+++ my-lua-changes/src/Makefile	2009-05-06 11:25:10.310376320 -0500
@@ -96,7 +96,7 @@
 	$(MAKE) all MYCFLAGS=
 
 linux:
-	$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
+	$(MAKE) all MYCFLAGS="-DLUA_USE_LINUX -DHAS_DLADDR=1" MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
 
 macosx:
 	$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
diff -Naur lua-5.1.4/src/ldebug.c my-lua-changes/src/ldebug.c
--- lua-5.1.4/src/ldebug.c	2008-05-08 11:56:26.000000000 -0500
+++ my-lua-changes/src/ldebug.c	2009-05-06 11:30:46.990003983 -0500
@@ -28,6 +28,11 @@
 #include "ltm.h"
 #include "lvm.h"
 
+#if HAS_DLADDR
+#  define __USE_GNU
+#  define _GNU_SOURCE
+#  include <dlfcn.h>
+#endif
 
 
 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
@@ -149,7 +154,16 @@
 
 static void funcinfo (lua_Debug *ar, Closure *cl) {
   if (cl->c.isC) {
+#if HAS_DLADDR
+      Dl_info info;
+      if(dladdr(cl->c.f, &info)) {
+          ar->source = info.dli_fname;
+      } else {
+          ar->source = "=[C]";
+      }
+#else
     ar->source = "=[C]";
+#endif
     ar->linedefined = -1;
     ar->lastlinedefined = -1;
     ar->what = "C";
@@ -543,7 +557,20 @@
 
 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
   Instruction i;
-  if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
+  if(!isLua(ci - 1)) {
+#if HAS_DLADDR
+      Dl_info info;
+      if(dladdr(ci_func(ci)->c.f, &info)) {
+          *name = info.dli_sname;
+          return "cfunction"; /* for now */
+      } else {
+          return NULL;
+      }
+#else
+      return NULL;
+#endif
+  }
+  if (isLua(ci) && ci->tailcalls > 0)
     return NULL;  /* calling function is not Lua (or is unknown) */
   ci--;  /* calling function */
   i = ci_func(ci)->l.p->code[currentpc(L, ci)];

Perl Numbers

This patch allows you to insert underscores arbitrarily into numbers, like you can in Perl. This allows you to type 1_000_000 for 1 million.

perl-numbers.patch
diff -Naur lua-5.1.4/src/llex.c my-lua-changes/src/llex.c
--- lua-5.1.4/src/llex.c	2007-12-27 07:02:25.000000000 -0600
+++ my-lua-changes/src/llex.c	2009-04-06 12:37:07.496444467 -0500
@@ -190,10 +190,14 @@
 
 /* LUA_NUMBER */
 static void read_numeral (LexState *ls, SemInfo *seminfo) {
-  lua_assert(isdigit(ls->current));
+  lua_assert(isdigit(ls->current) || ls->current == '_');
   do {
-    save_and_next(ls);
-  } while (isdigit(ls->current) || ls->current == '.');
+    if(ls->current == '_') {
+      next(ls);
+    } else {
+      save_and_next(ls);
+    }
+  } while (isdigit(ls->current) || ls->current == '.' || ls->current == '_');
   if (check_next(ls, "Ee"))  /* `E'? */
     check_next(ls, "+-");  /* optional exponent sign */
   while (isalnum(ls->current) || ls->current == '_')