From af27d65a8a0aa57d5258679928aa2220d94e44e0 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:11:04 +0200 Subject: [PATCH] Sync with Berry upstream (#19119) --- lib/libesp32/berry/src/be_api.c | 2 ++ lib/libesp32/berry/src/berry.h | 2 ++ lib/libesp32/berry/tests/class_static.be | 15 +++++++++- lib/libesp32/berry/tests/string.be | 22 +++++++++++++++ lib/libesp32/berry/tests/walrus.be | 35 ++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 lib/libesp32/berry/tests/walrus.be diff --git a/lib/libesp32/berry/src/be_api.c b/lib/libesp32/berry/src/be_api.c index fb2b8e0176a5..ba5b915d2d9d 100644 --- a/lib/libesp32/berry/src/be_api.c +++ b/lib/libesp32/berry/src/be_api.c @@ -1038,7 +1038,9 @@ BERRY_API int be_pcall(bvm *vm, int argc) return be_protectedcall(vm, f, argc); } +#ifdef __GNUC__ __attribute__((noreturn)) +#endif BERRY_API void be_raise(bvm *vm, const char *except, const char *msg) { be_pushstring(vm, except); diff --git a/lib/libesp32/berry/src/berry.h b/lib/libesp32/berry/src/berry.h index f6c6eddb86c7..962a01ebbdbc 100644 --- a/lib/libesp32/berry/src/berry.h +++ b/lib/libesp32/berry/src/berry.h @@ -2065,7 +2065,9 @@ BERRY_API void be_exit(bvm *vm, int status); * @param except * @param msg */ +#ifdef __GNUC__ __attribute__((noreturn)) +#endif BERRY_API void be_raise(bvm *vm, const char *except, const char *msg); /** diff --git a/lib/libesp32/berry/tests/class_static.be b/lib/libesp32/berry/tests/class_static.be index d972f5ef3536..dbe8c222e3f3 100644 --- a/lib/libesp32/berry/tests/class_static.be +++ b/lib/libesp32/berry/tests/class_static.be @@ -140,5 +140,18 @@ assert(classname(a) == 'A') assert(classname(b) == 'B') assert(A.B.f() == 1) assert(b.g() == 2) -assert(super(b) == nil) assert(super(A.B) == nil) + +#- `_class` initializer can now be used in initializer code -# +class A + static var a = 1 + static var b = _class + static var c = [_class.a, _class.b] + static def f(x) + return _class + end +end +assert(A.a == 1) +assert(A.b == A) +assert(A.c == [1, A]) +assert(A.f(1) == A) diff --git a/lib/libesp32/berry/tests/string.be b/lib/libesp32/berry/tests/string.be index 072e5faa7965..82f8244515b8 100644 --- a/lib/libesp32/berry/tests/string.be +++ b/lib/libesp32/berry/tests/string.be @@ -147,3 +147,25 @@ assert(string.format("%s", nil) == 'nil') assert(string.format("%s", true) == 'true') assert(string.format("%s", false) == 'false') +# format is now synonym to string.format +assert(format == string.format) +assert(format("%.1f", 3) == '3.0') + +# f-strings +assert(f"" == '') +assert(f'' == '') +assert(f"abc\n\r\t" == 'abc\n\r\t') +assert(f'{{a}}' == '{a}') +assert(f'\\\\' == '\\\\') + +assert(f"A = {1+1}" == 'A = 2') +assert(f"A = {1+1:s}" == 'A = 2') +assert(f"A = {1+1:i}" == 'A = 2') +assert(f"A = {1+1:04i}" == 'A = 0002') + +assert(f"P = {3.1415:.2f}" == 'P = 3.14') + +var a = 'foobar{0}' +assert(f"S = {a}" == 'S = foobar{0}') +assert(f"S = {a:i}" == 'S = 0') +assert(f"{a=}" == 'a=foobar{0}') diff --git a/lib/libesp32/berry/tests/walrus.be b/lib/libesp32/berry/tests/walrus.be new file mode 100644 index 000000000000..faa92fa15e10 --- /dev/null +++ b/lib/libesp32/berry/tests/walrus.be @@ -0,0 +1,35 @@ +# test for the walrus operator +var a = 1 +assert((a := a + 1) == 2) +assert((a := a + 1) == 3) + +def f() + var defer = 10 + var ret = [] + for i:0..100 + if (defer := defer - 1) == 0 + ret.push(i) + defer = 10 + end + end + return ret +end +assert(f() == [9, 19, 29, 39, 49, 59, 69, 79, 89, 99]) + +# test for expressions with no side effects +def assert_attribute_error(c) + try + compile(c)() + assert(false, 'unexpected execution flow') + except 'syntax_error' as e, m + end +end + +# below the expressions `a` or `a b` have no side effect and do not generate any code, this is an error when in strict mode +import strict +var a, b +assert_attribute_error("var a,b def f() a b end") +assert_attribute_error("var a,b def f() a end") + +# while the following does have side effect +def f() a := b end