Skip to content

Commit 0d7aaed

Browse files
author
Fabrice Bellard
committed
ensure that JS_IteratorNext() returns JS_UNDEFINED when done = TRUE (#394)
1 parent 2634856 commit 0d7aaed

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

quickjs.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15107,6 +15107,7 @@ static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj,
1510715107
return JS_EXCEPTION;
1510815108
}
1510915109

15110+
/* Note: always return JS_UNDEFINED when *pdone = TRUE. */
1511015111
static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
1511115112
JSValueConst method,
1511215113
int argc, JSValueConst *argv, BOOL *pdone)
@@ -15117,9 +15118,13 @@ static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
1511715118
obj = JS_IteratorNext2(ctx, enum_obj, method, argc, argv, &done);
1511815119
if (JS_IsException(obj))
1511915120
goto fail;
15120-
if (done != 2) {
15121-
*pdone = done;
15121+
if (likely(done == 0)) {
15122+
*pdone = FALSE;
1512215123
return obj;
15124+
} else if (done != 2) {
15125+
JS_FreeValue(ctx, obj);
15126+
*pdone = TRUE;
15127+
return JS_UNDEFINED;
1512315128
} else {
1512415129
done_val = JS_GetProperty(ctx, obj, JS_ATOM_done);
1512515130
if (JS_IsException(done_val))
@@ -37510,10 +37515,8 @@ static JSValue js_object_fromEntries(JSContext *ctx, JSValueConst this_val,
3751037515
item = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
3751137516
if (JS_IsException(item))
3751237517
goto fail;
37513-
if (done) {
37514-
JS_FreeValue(ctx, item);
37518+
if (done)
3751537519
break;
37516-
}
3751737520

3751837521
key = JS_UNDEFINED;
3751937522
value = JS_UNDEFINED;
@@ -46550,10 +46553,8 @@ static JSValue js_map_constructor(JSContext *ctx, JSValueConst new_target,
4655046553
item = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
4655146554
if (JS_IsException(item))
4655246555
goto fail;
46553-
if (done) {
46554-
JS_FreeValue(ctx, item);
46556+
if (done)
4655546557
break;
46556-
}
4655746558
if (is_set) {
4655846559
ret = JS_Call(ctx, adder, obj, 1, (JSValueConst *)&item);
4655946560
if (JS_IsException(ret)) {
@@ -52686,10 +52687,8 @@ static JSValue js_array_from_iterator(JSContext *ctx, uint32_t *plen,
5268652687
val = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
5268752688
if (JS_IsException(val))
5268852689
goto fail;
52689-
if (done) {
52690-
JS_FreeValue(ctx, val);
52690+
if (done)
5269152691
break;
52692-
}
5269352692
if (JS_CreateDataPropertyUint32(ctx, arr, k, val, JS_PROP_THROW) < 0)
5269452693
goto fail;
5269552694
k++;

0 commit comments

Comments
 (0)