hoelz.ro - Lua Power Patches

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
  1. diff -Naur lua-5.1.4/src/Makefile my-lua-changes/src/Makefile
  2. --- lua-5.1.4/src/Makefile 2008-01-19 13:37:58.000000000 -0600
  3. +++ my-lua-changes/src/Makefile 2009-05-06 11:25:10.310376320 -0500
  4. @@ -96,7 +96,7 @@
  5. $(MAKE) all MYCFLAGS=
  6.  
  7. linux:
  8. - $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
  9. + $(MAKE) all MYCFLAGS="-DLUA_USE_LINUX -DHAS_DLADDR=1" MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
  10.  
  11. macosx:
  12. $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
  13. diff -Naur lua-5.1.4/src/ldebug.c my-lua-changes/src/ldebug.c
  14. --- lua-5.1.4/src/ldebug.c 2008-05-08 11:56:26.000000000 -0500
  15. +++ my-lua-changes/src/ldebug.c 2009-05-06 11:30:46.990003983 -0500
  16. @@ -28,6 +28,11 @@
  17. #include "ltm.h"
  18. #include "lvm.h"
  19.  
  20. +#if HAS_DLADDR
  21. +# define __USE_GNU
  22. +# define _GNU_SOURCE
  23. +# include <dlfcn.h>
  24. +#endif
  25.  
  26.  
  27. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
  28. @@ -149,7 +154,16 @@
  29.  
  30. static void funcinfo (lua_Debug *ar, Closure *cl) {
  31. if (cl->c.isC) {
  32. +#if HAS_DLADDR
  33. + Dl_info info;
  34. + if(dladdr(cl->c.f, &info)) {
  35. + ar->source = info.dli_fname;
  36. + } else {
  37. + ar->source = "=[C]";
  38. + }
  39. +#else
  40. ar->source = "=[C]";
  41. +#endif
  42. ar->linedefined = -1;
  43. ar->lastlinedefined = -1;
  44. ar->what = "C";
  45. @@ -543,7 +557,20 @@
  46.  
  47. static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
  48. Instruction i;
  49. - if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
  50. + if(!isLua(ci - 1)) {
  51. +#if HAS_DLADDR
  52. + Dl_info info;
  53. + if(dladdr(ci_func(ci)->c.f, &info)) {
  54. + *name = info.dli_sname;
  55. + return "cfunction"; /* for now */
  56. + } else {
  57. + return NULL;
  58. + }
  59. +#else
  60. + return NULL;
  61. +#endif
  62. + }
  63. + if (isLua(ci) && ci->tailcalls > 0)
  64. return NULL; /* calling function is not Lua (or is unknown) */
  65. ci--; /* calling function */
  66. 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
  1. diff -Naur lua-5.1.4/src/llex.c my-lua-changes/src/llex.c
  2. --- lua-5.1.4/src/llex.c 2007-12-27 07:02:25.000000000 -0600
  3. +++ my-lua-changes/src/llex.c 2009-04-06 12:37:07.496444467 -0500
  4. @@ -190,10 +190,14 @@
  5.  
  6. /* LUA_NUMBER */
  7. static void read_numeral (LexState *ls, SemInfo *seminfo) {
  8. - lua_assert(isdigit(ls->current));
  9. + lua_assert(isdigit(ls->current) || ls->current == '_');
  10. do {
  11. - save_and_next(ls);
  12. - } while (isdigit(ls->current) || ls->current == '.');
  13. + if(ls->current == '_') {
  14. + next(ls);
  15. + } else {
  16. + save_and_next(ls);
  17. + }
  18. + } while (isdigit(ls->current) || ls->current == '.' || ls->current == '_');
  19. if (check_next(ls, "Ee")) /* `E'? */
  20. check_next(ls, "+-"); /* optional exponent sign */
  21. while (isalnum(ls->current) || ls->current == '_')

Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki ipv6 ready