From e995cb29c210d8d946c889a6a65dd0e90403e767 Mon Sep 17 00:00:00 2001 From: pixel Date: Thu, 20 Nov 2003 09:07:24 +0000 Subject: Added LUA "compiler" to the code. --- include/BLua.h | 3 +- lib/BLua.cc | 28 ++++++++++++++++-- lib/lua/src/luacomp.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 lib/lua/src/luacomp.c diff --git a/include/BLua.h b/include/BLua.h index fb62b14..19c65c4 100644 --- a/include/BLua.h +++ b/include/BLua.h @@ -48,7 +48,8 @@ class Lua : public Base { lua_CFunction tocfunction(int = -1); void * touserdata(int = -1); Lua * tothread(int = -1); - void load(Handle *) throw (GeneralException) ; + void load(Handle *, bool = true) throw (GeneralException); + void dump(Handle *, bool = true); Lua * thread(); static Lua * find(lua_State *) throw (GeneralException); void showerror(); diff --git a/lib/BLua.cc b/lib/BLua.cc index 18789f7..c15e6d9 100644 --- a/lib/BLua.cc +++ b/lib/BLua.cc @@ -1,4 +1,5 @@ #include + #include "BLua.h" #ifndef BUFFERSIZE @@ -10,6 +11,7 @@ typedef GeneralException LuaException; class LuaStatics : public Base { public: static const char * getF(lua_State *, void *, size_t *); + static int putF(lua_State *, const void *, size_t, void *); static int luapanic(lua_State *) throw(GeneralException); static int destructor(lua_State *); }; @@ -219,7 +221,18 @@ const char * LuaStatics::getF(lua_State * L, void * ud, size_t * size) { return (*size > 0) ? lf->buff : NULL; } -void Lua::load(Handle * h) throw (GeneralException) { +struct DumpF { + Handle * f; +}; + +int LuaStatics::putF(lua_State * L, const void * p, size_t size, void * ud) { + DumpF *lf = (DumpF *)ud; + (void)L; + + return lf->f->write(p, size) == size; +} + +void Lua::load(Handle * h, bool docall) throw (GeneralException) { LoadF lf; int status; @@ -232,7 +245,18 @@ void Lua::load(Handle * h) throw (GeneralException) { throw LuaException("Error loading lua chunk from Handle `" + h->GetName() + "'"); } - call(); + if (docall) + call(); +} + +extern "C" void luacmain(lua_State * L, int stripping, lua_Chunkwriter w, void * uD); + +void Lua::dump(Handle * h, bool strip) { + DumpF lf; + + lf.f = h; + + luacmain(L, strip, LuaStatics::putF, &lf); } Lua * Lua::thread() { diff --git a/lib/lua/src/luacomp.c b/lib/lua/src/luacomp.c new file mode 100644 index 0000000..8d13ee5 --- /dev/null +++ b/lib/lua/src/luacomp.c @@ -0,0 +1,78 @@ +/* +** $Id: luacomp.c,v 1.1 2003-11-20 09:07:25 pixel Exp $ +** Lua compiler (saves bytecodes to files; also list bytecodes) +** See Copyright Notice in lua.h +*/ + +#include +#include +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "lfunc.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lstring.h" +#include "lundump.h" + +#ifndef PROGNAME +#define PROGNAME "luacomp" /* program name */ +#endif + +static const char* progname=PROGNAME; /* actual program name */ + +static Proto* toproto(lua_State* L, int i) +{ + const Closure* c=(const Closure*)lua_topointer(L,i); + return c->l.p; +} + +static Proto* combine(lua_State* L, int n) +{ + if (n==1) + return toproto(L,-1); + else + { + int i,pc=0; + Proto* f=luaF_newproto(L); + f->source=luaS_newliteral(L,"=(" PROGNAME ")"); + f->maxstacksize=1; + f->p=luaM_newvector(L,n,Proto*); + f->sizep=n; + f->sizecode=2*n+1; + f->code=luaM_newvector(L,f->sizecode,Instruction); + for (i=0; ip[i]=toproto(L,i-n); + f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i); + f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1); + } + f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0); + return f; + } +} + +static void strip(lua_State* L, Proto* f) +{ + int i,n=f->sizep; + luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); + luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); + luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); + f->lineinfo=NULL; f->sizelineinfo=0; + f->locvars=NULL; f->sizelocvars=0; + f->upvalues=NULL; f->sizeupvalues=0; + f->source=luaS_newliteral(L,"=(none)"); + for (i=0; ip[i]); +} + +void luacmain(lua_State * L, int stripping, lua_Chunkwriter w, void * uD) { + Proto * f; + int i; + f = combine(L, 1); + if (stripping) + strip(L, f); + luaU_dump(L, f, w, uD); +} -- cgit v1.2.3