Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Save/Load Bytecode? #19

Closed
jarroddavis68 opened this issue Mar 19, 2021 · 2 comments
Closed

Save/Load Bytecode? #19

jarroddavis68 opened this issue Mar 19, 2021 · 2 comments

Comments

@jarroddavis68
Copy link

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

#include <file>

is the only way to include modules of code in script?

[script]
#include <module1>
#include <module2>
#include <module3>

game_startup() {
}

game_shutdown() {
}

game_update(deltaTime) {
}

game_render() {
}

game_show() {
}

main {
  ...
}

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

@jarroddavis68
Copy link
Author

jarroddavis68 commented Mar 20, 2021

I use Delphi (Object Pascal) so I first had to get a gsc.dll made in VS2019, then create an gsc.pas import unit to access the exported functions. The code below is my implementation, seem to work ok. I was able to compile the sdl2 example, save to code.bin and then load it back into a fresh vm and execute it.

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;

@riicchhaarrd
Copy link
Owner

riicchhaarrd commented Mar 20, 2021

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.

How does gsc compare to LuaJit in terms of performance.

Performance is nowhere near LuaJIT, since this doesn't have a JIT compiler.

is so sweet and the thread support, ahh marvelous.

They're not real threads, they're coroutines. Just mentioning it incase you weren't aware of this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants