From 71010423eb8a9f5024d91032b8e8ddbd77ae7d5c Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Wed, 20 Apr 2022 22:57:07 +0200 Subject: [PATCH] Sync with latest Berry PRs --- lib/libesp32/berry/src/be_byteslib.c | 2 ++ lib/libesp32/berry/src/be_object.h | 1 - lib/libesp32/berry/src/be_var.c | 2 +- lib/libesp32/berry/src/be_vm.h | 1 + lib/libesp32/berry/tests/bytes.be | 41 ++++---------------------- lib/libesp32/berry/tests/class.be | 13 ++++++++ lib/libesp32/berry/tests/int.be | 8 +++++ lib/libesp32/berry/tests/introspect.be | 5 ++++ 8 files changed, 35 insertions(+), 38 deletions(-) create mode 100644 lib/libesp32/berry/tests/int.be diff --git a/lib/libesp32/berry/src/be_byteslib.c b/lib/libesp32/berry/src/be_byteslib.c index b9b4ad3b9..aab229839 100644 --- a/lib/libesp32/berry/src/be_byteslib.c +++ b/lib/libesp32/berry/src/be_byteslib.c @@ -1502,6 +1502,8 @@ void be_load_byteslib(bvm *vm) { "geti", m_geti }, { "set", m_set }, { "seti", m_set }, // setters for signed and unsigned are identical + { "getfloat", m_getfloat }, + { "setfloat", m_setfloat }, { "item", m_item }, { "setitem", m_setitem }, { "size", m_size }, diff --git a/lib/libesp32/berry/src/be_object.h b/lib/libesp32/berry/src/be_object.h index 67e79bcdf..56bf03a9b 100644 --- a/lib/libesp32/berry/src/be_object.h +++ b/lib/libesp32/berry/src/be_object.h @@ -44,7 +44,6 @@ /* values for bproto.varg */ #define BE_VA_VARARG (1 << 0) /* function has variable number of arguments */ #define BE_VA_METHOD (1 << 1) /* function is a method (this is only a hint) */ - #define array_count(a) (sizeof(a) / sizeof((a)[0])) #define bcommon_header \ diff --git a/lib/libesp32/berry/src/be_var.c b/lib/libesp32/berry/src/be_var.c index 59f50f28b..9960e7cc3 100644 --- a/lib/libesp32/berry/src/be_var.c +++ b/lib/libesp32/berry/src/be_var.c @@ -55,7 +55,7 @@ static int global_native_class_find(bvm *vm, bstring *name) /* class name matches */ int idx = be_global_new(vm, name); bvalue *v = be_global_var(vm, idx); - var_setclass(v, cl); + var_setclass(v, (void*) cl); return idx; } } diff --git a/lib/libesp32/berry/src/be_vm.h b/lib/libesp32/berry/src/be_vm.h index 97d5a04d1..a2427277a 100644 --- a/lib/libesp32/berry/src/be_vm.h +++ b/lib/libesp32/berry/src/be_vm.h @@ -84,6 +84,7 @@ struct bupval { } u; int refcnt; }; + struct bvm { bglobaldesc gbldesc; /* global description */ bvalue *stack; /* stack space */ diff --git a/lib/libesp32/berry/tests/bytes.be b/lib/libesp32/berry/tests/bytes.be index 9ca9bda13..c0cef873a 100644 --- a/lib/libesp32/berry/tests/bytes.be +++ b/lib/libesp32/berry/tests/bytes.be @@ -184,40 +184,9 @@ assert(b[-10..-5] == bytes("66778899AABB")) assert(b[5..-10] == bytes("5566")) assert(b[7..-12] == bytes()) -#- floats little endian -# +#- float -# b = bytes("00000000") -b.setfloat(0, 0) -assert(b == bytes("00000000")) -b.setfloat(0, 1) -assert(b == bytes("0000803F")) -b.setfloat(0, -1) -assert(b == bytes("000080BF")) -b.setfloat(0, 3.5) -assert(b == bytes("00006040")) -import math -b.setfloat(0, math.nan) -assert(b == bytes("0000C07F")) - -assert(bytes("00000000").getfloat(0) == 0) -assert(bytes("0000803F").getfloat(0) == 1) -assert(bytes("000080BF").getfloat(0) == -1) -assert(bytes("00006040").getfloat(0) == 3.5) - -#- floats big endian -# -b = bytes("00000000") -b.setfloat(0, 0, true) -assert(b == bytes("00000000")) -b.setfloat(0, 1, true) -assert(b == bytes("3F800000")) -b.setfloat(0, -1, true) -assert(b == bytes("BF800000")) -b.setfloat(0, 3.5, true) -assert(b == bytes("40600000")) -import math -b.setfloat(0, math.nan, true) -assert(b == bytes("7FC00000")) - -assert(bytes("00000000").getfloat(0, true) == 0) -assert(bytes("3F800000").getfloat(0, true) == 1) -assert(bytes("BF800000").getfloat(0, true) == -1) -assert(bytes("40600000").getfloat(0, true) == 3.5) +b.setfloat(0, 0.33) +assert(b == bytes('C3F5A83E')) +b = bytes("0000C03F") +assert(b.getfloat(0) == 1.5) diff --git a/lib/libesp32/berry/tests/class.be b/lib/libesp32/berry/tests/class.be index 8c194a175..e175fcdca 100644 --- a/lib/libesp32/berry/tests/class.be +++ b/lib/libesp32/berry/tests/class.be @@ -45,3 +45,16 @@ assert(c2.C1 == C) c3 = m.C2(m.C()) assert(type(c3.C1) == 'instance') assert(classname(c3.C1) == 'C') + +#- an instance member can be a class and called directly -# +class Test_class + var c + def init() + self.c = map + end +end +c4 = Test_class() +assert(type(c4.c) == 'class') +c5 = c4.c() +assert(type(c5) == 'instance') +assert(classname(c5) == 'map') \ No newline at end of file diff --git a/lib/libesp32/berry/tests/int.be b/lib/libesp32/berry/tests/int.be new file mode 100644 index 000000000..d5a7c79e9 --- /dev/null +++ b/lib/libesp32/berry/tests/int.be @@ -0,0 +1,8 @@ +#- toint() converts any instance to int -# +class Test_int + def toint() + return 42 + end +end +t=Test_int() +assert(int(t) == 42) diff --git a/lib/libesp32/berry/tests/introspect.be b/lib/libesp32/berry/tests/introspect.be index 91531f57a..cb4a612d8 100644 --- a/lib/libesp32/berry/tests/introspect.be +++ b/lib/libesp32/berry/tests/introspect.be @@ -26,3 +26,8 @@ assert(introspect.get(a, 'a') == nil) introspect.set(a, 'a', 3) assert(a.a == 3) + +#- load module dynamically -# +import introspect +m = introspect.module("math") # load module `math`, assign to `m` and don't create a global variable +assert(type(m.pi) == 'real')