From 3ad3214323ebb345aaffcaaf46e19ad9f07d6563 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 1 Aug 2024 15:27:56 +0200 Subject: [PATCH] Berry add `file.savecode()` --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_bytecode.c | 11 ++++++++--- lib/libesp32/berry/src/be_bytecode.h | 1 + lib/libesp32/berry/src/be_filelib.c | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d50c983a622c..e05a5b14a07f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - Analog GPIO ``ADC Voltage`` with ``AdcParam 11,,,,`` provide energy monitoring with dc voltage - Analog GPIO ``ADC Current`` with ``AdcParam 12,,,,`` provide energy monitoring with dc voltage - Berry add new type "addr" to ctypes mapping +- Berry add `file.savecode()` ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_bytecode.c b/lib/libesp32/berry/src/be_bytecode.c index a636567facf1..79ed503bd95d 100644 --- a/lib/libesp32/berry/src/be_bytecode.c +++ b/lib/libesp32/berry/src/be_bytecode.c @@ -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 *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, "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); } } diff --git a/lib/libesp32/berry/src/be_bytecode.h b/lib/libesp32/berry/src/be_bytecode.h index 61223952c6ba..4e1d54a3b58c 100644 --- a/lib/libesp32/berry/src/be_bytecode.h +++ b/lib/libesp32/berry/src/be_bytecode.h @@ -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); diff --git a/lib/libesp32/berry/src/be_filelib.c b/lib/libesp32/berry/src/be_filelib.c index 5bd38bad802b..cfb882b11549 100644 --- a/lib/libesp32/berry/src/be_filelib.c +++ b/lib/libesp32/berry/src/be_filelib.c @@ -9,6 +9,7 @@ #include "be_mem.h" #include "be_sys.h" #include "be_gc.h" +#include "be_bytecode.h" #include #define READLINE_STEP 100 @@ -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 @@ -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;