Berry add `file.savecode()` (#21884)

This commit is contained in:
s-hadinger 2024-08-01 15:38:07 +02:00 committed by GitHub
parent 836ce9ef9b
commit 74551c873f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 3 deletions

View File

@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage - Analog GPIO ``ADC Voltage`` with ``AdcParam<x> 11,<start_range>,<end_range>,<lowest_voltage>,<highest_voltage>`` provide energy monitoring with dc voltage
- Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage - Analog GPIO ``ADC Current`` with ``AdcParam<x> 12,<start_range>,<end_range>,<lowest_current>,<highest_current>`` provide energy monitoring with dc voltage
- Berry add new type "addr" to ctypes mapping - Berry add new type "addr" to ctypes mapping
- Berry add `file.savecode()`
### Breaking Changed ### Breaking Changed

View File

@ -297,6 +297,13 @@ static void save_global_info(bvm *vm, void *fp)
} }
} }
void be_bytecode_save_to_fs(bvm *vm, void *fp, bproto *proto)
{
save_header(fp);
save_global_info(vm, fp);
save_proto(vm, fp, proto);
}
void be_bytecode_save(bvm *vm, const char *filename, bproto *proto) void be_bytecode_save(bvm *vm, const char *filename, bproto *proto)
{ {
void *fp = be_fopen(filename, "wb"); void *fp = be_fopen(filename, "wb");
@ -304,9 +311,7 @@ void be_bytecode_save(bvm *vm, const char *filename, bproto *proto)
bytecode_error(vm, be_pushfstring(vm, bytecode_error(vm, be_pushfstring(vm,
"can not open file '%s'.", filename)); "can not open file '%s'.", filename));
} else { } else {
save_header(fp); be_bytecode_save_to_fs(vm, fp, proto);
save_global_info(vm, fp);
save_proto(vm, fp, proto);
be_fclose(fp); be_fclose(fp);
} }
} }

View File

@ -11,6 +11,7 @@
#include "be_object.h" #include "be_object.h"
void be_bytecode_save(bvm *vm, const char *filename, bproto *proto); void be_bytecode_save(bvm *vm, const char *filename, bproto *proto);
void be_bytecode_save_to_fs(bvm *vm, void *fp, bproto *proto);
bclosure* be_bytecode_load(bvm *vm, const char *filename); bclosure* be_bytecode_load(bvm *vm, const char *filename);
bclosure* be_bytecode_load_from_fs(bvm *vm, void *fp); bclosure* be_bytecode_load_from_fs(bvm *vm, void *fp);
bbool be_bytecode_check(const char *path); bbool be_bytecode_check(const char *path);

View File

@ -9,6 +9,7 @@
#include "be_mem.h" #include "be_mem.h"
#include "be_sys.h" #include "be_sys.h"
#include "be_gc.h" #include "be_gc.h"
#include "be_bytecode.h"
#include <string.h> #include <string.h>
#define READLINE_STEP 100 #define READLINE_STEP 100
@ -181,6 +182,26 @@ static int i_close(bvm *vm)
be_return_nil(vm); be_return_nil(vm);
} }
static int i_savecode(bvm *vm)
{
int argc = be_top(vm);
if (argc >= 2 && be_isclosure(vm, 2)) {
be_getmember(vm, 1, ".p");
if (be_iscomptr(vm, -1)) {
void *fh = be_tocomptr(vm, -1);
bvalue *v = be_indexof(vm, 2);
if (var_isclosure(v)) {
bclosure *cl = var_toobj(v);
bproto *pr = cl->proto;
be_bytecode_save_to_fs(vm, fh, pr);
}
}
} else {
be_raise(vm, "type_error", "closure expected");
}
be_return_nil(vm);
}
#if !BE_USE_PRECOMPILED_OBJECT #if !BE_USE_PRECOMPILED_OBJECT
static int m_open(bvm *vm) static int m_open(bvm *vm)
#else #else
@ -201,6 +222,7 @@ int be_nfunc_open(bvm *vm)
{ "flush", i_flush }, { "flush", i_flush },
{ "close", i_close }, { "close", i_close },
{ "deinit", i_close }, { "deinit", i_close },
{ "savecode", i_savecode },
{ NULL, NULL } { NULL, NULL }
}; };
fname = argc >= 1 && be_isstring(vm, 1) ? be_tostring(vm, 1) : NULL; fname = argc >= 1 && be_isstring(vm, 1) ? be_tostring(vm, 1) : NULL;