-
Notifications
You must be signed in to change notification settings - Fork 8
Save/Load Bytecode? #19
Comments
I use Delphi (Object Pascal) so I first had to get a procedure compiler_save_bytecode(aCompiler: compiler_t; aFilename: string);
var
fs: TFileStream;
begin
fs := TFile.Create(aFilename);
fs.Write(aCompiler.&program^, aCompiler.program_size);
FreeAndNil(fs);
end;
function vm_load_bytecode(aVM: Pvm_t; aFilename: string): Boolean;
var
fs: TFileStream;
code: array of Byte;
size: integer;
tag: AnsiString;
begin
tag := AnsiString(TPath.GetFileName(aFilename));
fs := TFile.OpenRead(aFilename);
size := fs.Size;
SetLength(code, size);
fs.ReadData(code, size);
Result := Boolean(vm_add_program(aVM, PByte(code), size, PAnsiChar(tag)) = 0);
FreeAndNil(fs);
code := nil;
end; Save out bytecode: const
filename = 'sdl2.gsc';
fps_delta = (1000 div 20);
procedure Test01;
var
c: compiler_t;
r: Integer;
vm: Pvm_t;
begin
r := compiler_init(@c, nil);
r := compiler_add_source_file(@c, filename);
r := compiler_execute(@c);
if r = 0 then
begin
compiler_save_bytecode(c, 'code.bin');
vm := vm_create;
vm_add_program(vm, PByte(c.&program), c.program_size, filename);
compiler_cleanup(@c);
se_addfloat(vm, 1.5);
vm_exec_thread(vm, 'game_update', 1);
vm_exec_thread(vm, 'main', 0);
while (vm_get_num_active_threadrunners(vm) > 0) do
begin
vm_run_active_threads(vm, fps_delta);
sleep(fps_delta);
end;
vm_free(vm);
end
else
writeln('Compiler error');
end; Load and run bytecode: procedure test02;
var
vm: Pvm_t;
begin
vm := vm_create;
if vm_load_bytecode(vm, 'code.bin') then
begin
vm_exec_thread(vm, 'main', 0);
while (vm_get_num_active_threadrunners(vm) > 0) do
begin
vm_run_active_threads(vm, fps_delta);
sleep(fps_delta);
end;
end;
vm_free(vm);
end; |
In your second comment, yes that is the way to save and load bytecode. At the moment it also includes the full program with function names and metadata and such.
Performance is nowhere near LuaJIT, since this doesn't have a JIT compiler.
They're not real threads, they're coroutines. Just mentioning it incase you weren't aware of this. |
Possible?
I want to be able to compile some sources, bind the byte code to a runner and that runner will check on startup if there a "payload" attached (as a win32 resource), load back in and then execute it.
I looked over the src and example scripts, am I right in assuming
is the only way to include modules of code in script?
my gamelib will load the main script file, it needs to then load in all the different parts that make up the game script code, I can then compile and save out the bytecode to the runner is what I'm going for. On execution, the lib will then call into the game_xxx functions as needed.
The ability to
#pragma comment(lib, "gamelib.dll")
is so sweet and the thread support, ahh marvelous. How does gsc compare to LuaJit in terms of performance. My current gamelib is using LuaJit and I'm looking to switch to gsc.
Thanks
Jarrod
The text was updated successfully, but these errors were encountered: