@@ -42176,7 +42176,7 @@ static JSValue js_array_iterator_next(JSContext *ctx, JSValueConst this_val,
42176
42176
}
42177
42177
}
42178
42178
42179
- /* IteratorWrap */
42179
+ /* Iterator Wrap */
42180
42180
42181
42181
typedef struct JSIteratorWrapData {
42182
42182
JSValue wrapped_iter;
@@ -42214,18 +42214,20 @@ static JSValue js_iterator_wrap_next(JSContext *ctx, JSValueConst this_val,
42214
42214
it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_WRAP);
42215
42215
if (!it)
42216
42216
return JS_EXCEPTION;
42217
- if (magic == GEN_MAGIC_NEXT)
42218
- return JS_IteratorNext(ctx, it->wrapped_iter, it->wrapped_next, argc, argv, pdone);
42219
- method = JS_GetProperty(ctx, it->wrapped_iter, JS_ATOM_return);
42220
- if (JS_IsException(method))
42221
- return JS_EXCEPTION;
42222
- if (JS_IsNull(method) || JS_IsUndefined(method)) {
42223
- *pdone = TRUE;
42224
- return JS_UNDEFINED;
42217
+ if (magic == GEN_MAGIC_NEXT) {
42218
+ return JS_IteratorNext(ctx, it->wrapped_iter, it->wrapped_next, 0, NULL, pdone);
42219
+ } else {
42220
+ method = JS_GetProperty(ctx, it->wrapped_iter, JS_ATOM_return);
42221
+ if (JS_IsException(method))
42222
+ return JS_EXCEPTION;
42223
+ if (JS_IsNull(method) || JS_IsUndefined(method)) {
42224
+ *pdone = TRUE;
42225
+ return JS_UNDEFINED;
42226
+ }
42227
+ ret = JS_IteratorNext2(ctx, it->wrapped_iter, method, 0, NULL, pdone);
42228
+ JS_FreeValue(ctx, method);
42229
+ return ret;
42225
42230
}
42226
- ret = JS_IteratorNext2(ctx, it->wrapped_iter, method, argc, argv, pdone);
42227
- JS_FreeValue(ctx, method);
42228
- return ret;
42229
42231
}
42230
42232
42231
42233
static const JSCFunctionListEntry js_iterator_wrap_proto_funcs[] = {
@@ -42275,50 +42277,55 @@ static JSValue js_iterator_constructor(JSContext *ctx, JSValueConst new_target,
42275
42277
static JSValue js_iterator_from(JSContext *ctx, JSValueConst this_val,
42276
42278
int argc, JSValueConst *argv)
42277
42279
{
42278
- JSValue method, iter;
42280
+ JSValueConst obj = argv[0];
42281
+ JSValue method, iter, wrapper;
42279
42282
JSIteratorWrapData *it;
42280
42283
int ret;
42281
42284
42282
- JSValueConst obj = argv[0];
42283
- if (JS_IsString(obj)) {
42284
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
42285
- if (JS_IsException(method))
42286
- return JS_EXCEPTION;
42287
- return JS_CallFree(ctx, method, obj, 0, NULL);
42285
+ if (!JS_IsObject(obj)) {
42286
+ if (!JS_IsString(obj))
42287
+ return JS_ThrowTypeError(ctx, "Iterator.from called on non-object");
42288
42288
}
42289
- if (!JS_IsObject(obj))
42290
- return JS_ThrowTypeError(ctx, "Iterator.from called on non-object");
42291
- ret = JS_OrdinaryIsInstanceOf(ctx, obj, ctx->iterator_ctor);
42292
- if (ret < 0)
42293
- return JS_EXCEPTION;
42294
- if (ret)
42295
- return JS_DupValue(ctx, obj);
42296
42289
method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
42297
42290
if (JS_IsException(method))
42298
42291
return JS_EXCEPTION;
42299
42292
if (JS_IsNull(method) || JS_IsUndefined(method)) {
42300
- method = JS_GetProperty(ctx, obj, JS_ATOM_next);
42301
- if (JS_IsException(method))
42302
- return JS_EXCEPTION;
42303
- iter = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_WRAP);
42304
- if (JS_IsException(iter))
42305
- goto fail;
42306
- it = js_malloc(ctx, sizeof(*it));
42307
- if (!it)
42308
- goto fail;
42309
- it->wrapped_iter = JS_DupValue(ctx, obj);
42310
- it->wrapped_next = method;
42311
- JS_SetOpaque(iter, it);
42293
+ iter = JS_DupValue(ctx, obj);
42312
42294
} else {
42313
42295
iter = JS_GetIterator2(ctx, obj, method);
42314
42296
JS_FreeValue(ctx, method);
42315
42297
if (JS_IsException(iter))
42316
42298
return JS_EXCEPTION;
42317
42299
}
42318
- return iter;
42319
- fail:
42300
+
42301
+ wrapper = JS_UNDEFINED;
42302
+ method = JS_GetProperty(ctx, iter, JS_ATOM_next);
42303
+ if (JS_IsException(method))
42304
+ goto fail;
42305
+
42306
+ ret = JS_OrdinaryIsInstanceOf(ctx, iter, ctx->iterator_ctor);
42307
+ if (ret < 0)
42308
+ goto fail;
42309
+ if (ret) {
42310
+ JS_FreeValue(ctx, method);
42311
+ return iter;
42312
+ }
42313
+
42314
+ wrapper = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_WRAP);
42315
+ if (JS_IsException(wrapper))
42316
+ goto fail;
42317
+ it = js_malloc(ctx, sizeof(*it));
42318
+ if (!it)
42319
+ goto fail;
42320
+ it->wrapped_iter = iter;
42321
+ it->wrapped_next = method;
42322
+ JS_SetOpaque(wrapper, it);
42323
+ return wrapper;
42324
+
42325
+ fail:
42320
42326
JS_FreeValue(ctx, method);
42321
42327
JS_FreeValue(ctx, iter);
42328
+ JS_FreeValue(ctx, wrapper);
42322
42329
return JS_EXCEPTION;
42323
42330
}
42324
42331
@@ -42450,12 +42457,13 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42450
42457
return JS_ThrowTypeErrorNotAnObject(ctx);
42451
42458
func = JS_UNDEFINED;
42452
42459
method = JS_UNDEFINED;
42460
+
42453
42461
if (check_function(ctx, argv[0]))
42454
42462
goto fail;
42455
42463
func = JS_DupValue(ctx, argv[0]);
42456
42464
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
42457
42465
if (JS_IsException(method))
42458
- goto fail ;
42466
+ goto fail_no_close ;
42459
42467
42460
42468
r = JS_UNDEFINED;
42461
42469
@@ -42466,7 +42474,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42466
42474
for (idx = 0; /*empty*/; idx++) {
42467
42475
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
42468
42476
if (JS_IsException(item))
42469
- goto fail ;
42477
+ goto fail_no_close ;
42470
42478
if (done)
42471
42479
break;
42472
42480
index_val = JS_NewInt64(ctx, idx);
@@ -42495,7 +42503,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42495
42503
for (idx = 0; /*empty*/; idx++) {
42496
42504
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
42497
42505
if (JS_IsException(item))
42498
- goto fail ;
42506
+ goto fail_no_close ;
42499
42507
if (done)
42500
42508
break;
42501
42509
index_val = JS_NewInt64(ctx, idx);
@@ -42516,6 +42524,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42516
42524
}
42517
42525
break;
42518
42526
}
42527
+ JS_FreeValue(ctx, item);
42519
42528
index_val = JS_UNDEFINED;
42520
42529
ret = JS_UNDEFINED;
42521
42530
item = JS_UNDEFINED;
@@ -42527,7 +42536,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42527
42536
for (idx = 0; /*empty*/; idx++) {
42528
42537
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
42529
42538
if (JS_IsException(item))
42530
- goto fail ;
42539
+ goto fail_no_close ;
42531
42540
if (done)
42532
42541
break;
42533
42542
index_val = JS_NewInt64(ctx, idx);
@@ -42551,7 +42560,7 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42551
42560
for (idx = 0; /*empty*/; idx++) {
42552
42561
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
42553
42562
if (JS_IsException(item))
42554
- goto fail ;
42563
+ goto fail_no_close ;
42555
42564
if (done)
42556
42565
break;
42557
42566
index_val = JS_NewInt64(ctx, idx);
@@ -42583,8 +42592,9 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
42583
42592
JS_FreeValue(ctx, func);
42584
42593
JS_FreeValue(ctx, method);
42585
42594
return r;
42586
- fail:
42595
+ fail:
42587
42596
JS_IteratorClose(ctx, this_val, TRUE);
42597
+ fail_no_close:
42588
42598
JS_FreeValue(ctx, func);
42589
42599
JS_FreeValue(ctx, method);
42590
42600
return JS_EXCEPTION;
@@ -42615,7 +42625,7 @@ static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val,
42615
42625
} else {
42616
42626
acc = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
42617
42627
if (JS_IsException(acc))
42618
- goto exception ;
42628
+ goto exception_no_close ;
42619
42629
if (done) {
42620
42630
JS_ThrowTypeError(ctx, "empty iterator");
42621
42631
goto exception;
@@ -42625,7 +42635,7 @@ static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val,
42625
42635
for (/* empty */; /*empty*/; idx++) {
42626
42636
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
42627
42637
if (JS_IsException(item))
42628
- goto exception ;
42638
+ goto exception_no_close ;
42629
42639
if (done)
42630
42640
break;
42631
42641
index_val = JS_NewInt64(ctx, idx);
@@ -42646,8 +42656,9 @@ static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val,
42646
42656
JS_FreeValue(ctx, func);
42647
42657
JS_FreeValue(ctx, method);
42648
42658
return acc;
42649
- exception:
42659
+ exception:
42650
42660
JS_IteratorClose(ctx, this_val, TRUE);
42661
+ exception_no_close:
42651
42662
JS_FreeValue(ctx, acc);
42652
42663
JS_FreeValue(ctx, func);
42653
42664
JS_FreeValue(ctx, method);
@@ -42787,7 +42798,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42787
42798
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
42788
42799
if (JS_IsException(item)) {
42789
42800
JS_FreeValue(ctx, method);
42790
- goto fail ;
42801
+ goto fail_no_close ;
42791
42802
}
42792
42803
JS_FreeValue(ctx, item);
42793
42804
if (magic == GEN_MAGIC_RETURN)
@@ -42802,7 +42813,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42802
42813
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
42803
42814
JS_FreeValue(ctx, method);
42804
42815
if (JS_IsException(item))
42805
- goto fail ;
42816
+ goto fail_no_close ;
42806
42817
ret = item;
42807
42818
goto done;
42808
42819
}
@@ -42822,7 +42833,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42822
42833
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
42823
42834
if (JS_IsException(item)) {
42824
42835
JS_FreeValue(ctx, method);
42825
- goto fail ;
42836
+ goto fail_no_close ;
42826
42837
}
42827
42838
if (*pdone || magic == GEN_MAGIC_RETURN) {
42828
42839
JS_FreeValue(ctx, method);
@@ -42835,6 +42846,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42835
42846
selected = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args);
42836
42847
JS_FreeValue(ctx, index_val);
42837
42848
if (JS_IsException(selected)) {
42849
+ JS_FreeValue(ctx, item);
42838
42850
JS_FreeValue(ctx, method);
42839
42851
goto fail;
42840
42852
}
@@ -42843,6 +42855,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42843
42855
ret = item;
42844
42856
goto done;
42845
42857
}
42858
+ JS_FreeValue(ctx, item);
42846
42859
goto filter_again;
42847
42860
}
42848
42861
break;
@@ -42862,7 +42875,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42862
42875
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
42863
42876
JS_FreeValue(ctx, method);
42864
42877
if (JS_IsException(item))
42865
- goto fail ;
42878
+ goto fail_no_close ;
42866
42879
if (*pdone || magic == GEN_MAGIC_RETURN) {
42867
42880
ret = item;
42868
42881
goto done;
@@ -42944,7 +42957,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42944
42957
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
42945
42958
JS_FreeValue(ctx, method);
42946
42959
if (JS_IsException(item))
42947
- goto fail ;
42960
+ goto fail_no_close ;
42948
42961
if (*pdone || magic == GEN_MAGIC_RETURN) {
42949
42962
ret = item;
42950
42963
goto done;
@@ -42974,7 +42987,7 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42974
42987
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
42975
42988
JS_FreeValue(ctx, method);
42976
42989
if (JS_IsException(item))
42977
- goto fail ;
42990
+ goto fail_no_close ;
42978
42991
ret = item;
42979
42992
goto done;
42980
42993
}
@@ -42991,13 +43004,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
42991
43004
abort();
42992
43005
}
42993
43006
42994
- done:
43007
+ done:
42995
43008
it->done = magic == GEN_MAGIC_NEXT ? *pdone : 1;
42996
43009
it->executing = 0;
42997
43010
return ret;
42998
- fail:
43011
+ fail:
42999
43012
/* close the iterator object, preserving pending exception */
43000
43013
JS_IteratorClose(ctx, it->obj, TRUE);
43014
+ fail_no_close:
43001
43015
ret = JS_EXCEPTION;
43002
43016
goto done;
43003
43017
}
0 commit comments