Skip to content

Commit

Permalink
load bytecode from file pointer (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger committed Mar 24, 2024
1 parent 7d3fe77 commit f1e89b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/be_bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,29 +596,34 @@ void load_global_info(bvm *vm, void *fp)
be_global_release_space(vm);
}

bclosure* be_bytecode_load_from_fs(bvm *vm, void *fp)
{
int version = load_head(fp);
if (version == BYTECODE_VERSION) {
bclosure *cl = be_newclosure(vm, 0);
var_setclosure(vm->top, cl);
be_stackpush(vm);
load_global_info(vm, fp);
load_proto(vm, fp, &cl->proto, -1, version);
be_stackpop(vm, 2); /* pop the closure and list */
be_fclose(fp);
return cl;
}
bytecode_error(vm, be_pushfstring(vm,
"invalid bytecode version."));
return NULL;
}

bclosure* be_bytecode_load(bvm *vm, const char *filename)
{
void *fp = be_fopen(filename, "rb");
if (fp == NULL) {
bytecode_error(vm, be_pushfstring(vm,
"can not open file '%s'.", filename));
} else {
int version = load_head(fp);
if (version == BYTECODE_VERSION) {
bclosure *cl = be_newclosure(vm, 0);
var_setclosure(vm->top, cl);
be_stackpush(vm);
load_global_info(vm, fp);
load_proto(vm, fp, &cl->proto, -1, version);
be_stackpop(vm, 2); /* pop the closure and list */
be_fclose(fp);
return cl;
}
bytecode_error(vm, be_pushfstring(vm,
"invalid bytecode version '%s'.", filename));
return be_bytecode_load_from_fs(vm, fp);
}
bytecode_error(vm, be_pushfstring(vm,
"invalid bytecode file '%s'.", filename));
return NULL;
}

#endif /* BE_USE_BYTECODE_LOADER */
1 change: 1 addition & 0 deletions src/be_bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

void be_bytecode_save(bvm *vm, const char *filename, bproto *proto);
bclosure* be_bytecode_load(bvm *vm, const char *filename);
bclosure* be_bytecode_load_from_fs(bvm *vm, void *fp);
bbool be_bytecode_check(const char *path);

#endif

0 comments on commit f1e89b4

Please sign in to comment.