ref: 57ac5ca96be7288ce98a096c250ec7cb8ad31c48
parent: 411fe6f19e2116238b392f24ea94d77dd45d9e32
author: kvik <kvik@a-b.xyz>
date: Tue Apr 27 10:33:05 EDT 2021
lu9: implement bytecode dump option
--- a/lu9.c
+++ b/lu9.c
@@ -18,6 +18,7 @@
};
char flag[] = {
+ ['c'] = 0, /* bytecode dump */
['i'] = 0, /* interactive */
['v'] = 0, /* print version */
['w'] = 0, /* enable warnings */
@@ -134,6 +135,33 @@
}
int
+dumpwriter(lua_State *, const void *p, size_t sz, void *data)
+{
+ Biobuf *w;
+
+ w = data;
+ if(sz != 0 && Bwrite(w, p, sz) != sz)
+ return -1;
+ return 0;
+}
+
+int
+dumpfile(lua_State *L, char *file)
+{
+ int r;
+ Biobuf w;
+
+ if((r = luaL_loadfile(L, file)) != LUA_OK)
+ luaerror(L);
+ if(Binit(&w, 1, OWRITE) == -1)
+ sysfatal("Binit: %r");
+ if(lua_dump(L, dumpwriter, &w, 0) == -1)
+ sysfatal("dump: %r");
+ Bterm(&w);
+ return r;
+}
+
+int
luamain(lua_State *L)
{
int argc, i;
@@ -143,11 +171,17 @@
argv = lua_touserdata(L, 2);
file = argv[0];
- if(flag['w'])
- lua_warning(L, "@on", 0);
-
/* GC in generational mode */
lua_gc(L, LUA_GCGEN, 0, 0);
+
+ if(flag['c']){
+ dumpfile(L, file);
+ lua_pushboolean(L, 1);
+ return 1;
+ }
+
+ if(flag['w'])
+ lua_warning(L, "@on", 0);
/* Signal for libraries to ignore LUA_* env. vars */
lua_pushboolean(L, 1);
@@ -201,6 +235,7 @@
lua_State *L;
ARGBEGIN{
+ case 'c': flag['c'] = 1; break;
case 'i': flag['i'] = 1; break;
case 'v': flag['v'] += 1; break;
case 'w': flag['w'] = 1; break;