From ec33c19dbea10f17aa7a03be97ce8da173965179 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 15 Jun 2021 20:53:49 +0200 Subject: [PATCH] Berry report bug fix #106 --- lib/libesp32/Berry/src/be_func.c | 17 +++++++++++------ lib/libesp32/Berry/tests/closure.be | 13 +++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 lib/libesp32/Berry/tests/closure.be diff --git a/lib/libesp32/Berry/src/be_func.c b/lib/libesp32/Berry/src/be_func.c index a4e210271..d49229954 100644 --- a/lib/libesp32/Berry/src/be_func.c +++ b/lib/libesp32/Berry/src/be_func.c @@ -54,17 +54,22 @@ void be_initupvals(bvm *vm, bclosure *cl) void be_upvals_close(bvm *vm, bvalue *level) { bupval *node = vm->upvalist, *next; - while (node && node->value >= level) { + bupval **prev = &vm->upvalist; + while (node) { next = node->u.next; - if (!node->refcnt) { - be_free(vm, node, sizeof(bupval)); + if (node->value >= level) { + if (!node->refcnt) { + be_free(vm, node, sizeof(bupval)); + } else { + node->u.value = *node->value; /* move value to upvalue slot */ + node->value = &node->u.value; + } + *prev = next; /* remove from linked list */ } else { - node->u.value = *node->value; /* move value to upvalue slot */ - node->value = &node->u.value; + prev = &node->u.next; } node = next; } - vm->upvalist = node; } void be_release_upvalues(bvm *vm, bclosure *cl) diff --git a/lib/libesp32/Berry/tests/closure.be b/lib/libesp32/Berry/tests/closure.be new file mode 100644 index 000000000..2474fede9 --- /dev/null +++ b/lib/libesp32/Berry/tests/closure.be @@ -0,0 +1,13 @@ +#- test for issue #105 -# + +l=[] +def tick() + var start=100 + for i : 1..3 + l.push(def () return [i, start] end) + end +end +tick() +assert(l[0]() == [1, 100]) +assert(l[1]() == [2, 100]) +assert(l[2]() == [3, 100])