GithubHelp home page GithubHelp logo

Save/Load Bytecode? about cgsc HOT 2 CLOSED

jarroddavis68 avatar jarroddavis68 commented on June 24, 2024 1
Save/Load Bytecode?

from cgsc.

Comments (2)

jarroddavis68 avatar jarroddavis68 commented on June 24, 2024 1

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;

from cgsc.

riicchhaarrd avatar riicchhaarrd commented on June 24, 2024 1

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.

from cgsc.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.