PDA

View Full Version : Pre-compiling and saving chunks for later


Mark F
February 11th, 2005, 02:56 PM
I can't seem to wrap my head around how to pre-compile some LUA code (assume it is in a memory buffer) and save it for later. I used to use the lua_bytecode() to do this. I think I want to use luaL_loadbuffer() but doesn't this leave the result on the stack?

Color me confused. :(

Promixis
February 13th, 2005, 04:59 PM
Thats what I understand....

Just need a way to save the compiled code off the stack...

Ron
February 13th, 2005, 08:27 PM
Okay here is what I do. (it's pascal)


TChunkWriteInfo = Record
Data : PCHAR;
Size : Integer;
end;
PChunkWriteInfo = ^TChunkWriteInfo;



function Chunkwriter(L: Plua_State; const p: Pointer; sz: Integer; ud: Pointer): Integer; cdecl;
var
cwi : PChunkWriteInfo;
d : PCHAR;
i : integer;
p2 : PCHAR;
begin

cwi := PChunkWriteInfo(ud);
p2 := PCHAR(p);

if sz = 0 then
begin
result:=0;
exit;
end;

d := cwi.Data;

cwi.Data := Malloc(sz + cwi.Size); // this could also be a realloc.

for i:=0 to cwi.Size - 1 do
cwi.Data[i] := d[i];

for i:=cwi.Size to cwi.Size + sz -1 do
cwi.Data[i] := p2[i-cwi.Size];

cwi.Size := cwi.Size + sz;
SafeFree(d);

result:=sz;
end;



lua_dump(l, Chunkwriter, @cwi);

Mark F
February 14th, 2005, 09:07 AM
I translated to C++ and it works great. Thanks. :D

Ron
February 14th, 2005, 10:48 AM
You're welcome.