Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry add file.savecode() #21884

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 `file.savecode()`

### Breaking Changed

Expand Down
11 changes: 8 additions & 3 deletions lib/libesp32/berry/src/be_bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,21 @@ 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 *fp = be_fopen(filename, "wb");
if (fp == NULL) {
bytecode_error(vm, be_pushfstring(vm,
"can not open file '%s'.", filename));
} else {
save_header(fp);
save_global_info(vm, fp);
save_proto(vm, fp, proto);
be_bytecode_save_to_fs(vm, fp, proto);
be_fclose(fp);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/libesp32/berry/src/be_bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "be_object.h"

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_from_fs(bvm *vm, void *fp);
bbool be_bytecode_check(const char *path);
Expand Down
22 changes: 22 additions & 0 deletions lib/libesp32/berry/src/be_filelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "be_mem.h"
#include "be_sys.h"
#include "be_gc.h"
#include "be_bytecode.h"
#include <string.h>

#define READLINE_STEP 100
Expand Down Expand Up @@ -181,6 +182,26 @@ static int i_close(bvm *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
static int m_open(bvm *vm)
#else
Expand All @@ -201,6 +222,7 @@ int be_nfunc_open(bvm *vm)
{ "flush", i_flush },
{ "close", i_close },
{ "deinit", i_close },
{ "savecode", i_savecode },
{ NULL, NULL }
};
fname = argc >= 1 && be_isstring(vm, 1) ? be_tostring(vm, 1) : NULL;
Expand Down