diff --git a/quickjs.c b/quickjs.c index 41907fc50..bbcc94bda 100644 --- a/quickjs.c +++ b/quickjs.c @@ -45183,6 +45183,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val, } break; } + JS_FreeValue(ctx, item); index_val = JS_UNDEFINED; ret = JS_UNDEFINED; item = JS_UNDEFINED; diff --git a/tests/bug1572.js b/tests/bug1572.js new file mode 100644 index 000000000..e1b418c6e --- /dev/null +++ b/tests/bug1572.js @@ -0,0 +1,18 @@ +import { assert } from "./assert.js"; + +// Every element takes the non-matching path (the predicate is never true). +{ + const o = {}; + assert([o, o, o].values().find(() => false), undefined); + assert(new Array(1000).fill(o).values().find(() => false), undefined); +} + +// The match path still returns the first match and forwards (value, index). +{ + const seen = []; + const r = [10, 20, 30, 40].values().find((v, i) => { seen.push(v, i); return v === 30; }); + assert(r, 30); + assert(seen.length, 6); // stopped at the third element + assert(seen[0], 10); assert(seen[1], 0); + assert(seen[4], 30); assert(seen[5], 2); +}