Skip to content

Commit ed2ff1e

Browse files
authored
Make Iterator proto methods conform to spec (#1059)
Close the iterator when argument validation fails.
1 parent f316b3d commit ed2ff1e

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

quickjs.c

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41777,33 +41777,31 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val,
4177741777
double dlimit;
4177841778
v = JS_ToNumber(ctx, argv[0]);
4177941779
if (JS_IsException(v))
41780-
return JS_EXCEPTION;
41780+
goto fail;
4178141781
// Check for Infinity.
4178241782
if (JS_ToFloat64(ctx, &dlimit, v)) {
4178341783
JS_FreeValue(ctx, v);
41784-
return JS_EXCEPTION;
41784+
goto fail;
4178541785
}
4178641786
if (isnan(dlimit)) {
4178741787
JS_FreeValue(ctx, v);
41788-
goto fail;
41788+
goto range_error;
4178941789
}
4179041790
if (!isfinite(dlimit)) {
4179141791
JS_FreeValue(ctx, v);
4179241792
if (dlimit < 0)
41793-
goto fail;
41793+
goto range_error;
4179441794
else
4179541795
count = MAX_SAFE_INTEGER;
4179641796
} else {
4179741797
v = JS_ToIntegerFree(ctx, v);
4179841798
if (JS_IsException(v))
41799-
return JS_EXCEPTION;
41799+
goto fail;
4180041800
if (JS_ToInt64Free(ctx, &count, v))
41801-
return JS_EXCEPTION;
41802-
}
41803-
if (count < 0) {
41804-
fail:
41805-
return JS_ThrowRangeError(ctx, "must be positive");
41801+
goto fail;
4180641802
}
41803+
if (count < 0)
41804+
goto range_error;
4180741805
}
4180841806
break;
4180941807
case JS_ITERATOR_HELPER_KIND_FILTER:
@@ -41812,7 +41810,7 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val,
4181241810
{
4181341811
func = argv[0];
4181441812
if (check_function(ctx, func))
41815-
return JS_EXCEPTION;
41813+
goto fail;
4181641814
}
4181741815
break;
4181841816
default:
@@ -41822,17 +41820,17 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val,
4182241820

4182341821
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
4182441822
if (JS_IsException(method))
41825-
return JS_EXCEPTION;
41823+
goto fail;
4182641824
obj = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_HELPER);
4182741825
if (JS_IsException(obj)) {
4182841826
JS_FreeValue(ctx, method);
41829-
return JS_EXCEPTION;
41827+
goto fail;
4183041828
}
4183141829
it = js_malloc(ctx, sizeof(*it));
4183241830
if (!it) {
4183341831
JS_FreeValue(ctx, obj);
4183441832
JS_FreeValue(ctx, method);
41835-
return JS_EXCEPTION;
41833+
goto fail;
4183641834
}
4183741835
it->kind = magic;
4183841836
it->obj = js_dup(this_val);
@@ -41844,6 +41842,11 @@ static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val,
4184441842
it->done = 0;
4184541843
JS_SetOpaqueInternal(obj, it);
4184641844
return obj;
41845+
range_error:
41846+
JS_ThrowRangeError(ctx, "must be positive");
41847+
fail:
41848+
JS_IteratorClose(ctx, this_val, true);
41849+
return JS_EXCEPTION;
4184741850
}
4184841851

4184941852
static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
@@ -41856,8 +41859,10 @@ static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
4185641859

4185741860
if (check_iterator(ctx, this_val) < 0)
4185841861
return JS_EXCEPTION;
41862+
func = JS_UNDEFINED;
41863+
method = JS_UNDEFINED;
4185941864
if (check_function(ctx, argv[0]))
41860-
return JS_EXCEPTION;
41865+
goto fail;
4186141866
func = js_dup(argv[0]);
4186241867
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
4186341868
if (JS_IsException(method))
@@ -42006,9 +42011,11 @@ static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val,
4200642011

4200742012
if (check_iterator(ctx, this_val) < 0)
4200842013
return JS_EXCEPTION;
42009-
if (check_function(ctx, argv[0]))
42010-
return JS_EXCEPTION;
4201142014
acc = JS_UNDEFINED;
42015+
func = JS_UNDEFINED;
42016+
method = JS_UNDEFINED;
42017+
if (check_function(ctx, argv[0]))
42018+
goto exception;
4201242019
func = js_dup(argv[0]);
4201342020
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
4201442021
if (JS_IsException(method))

test262_errors.txt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ test262/test/built-ins/Iterator/prototype/constructor/prop-desc.js:10: Test262Er
2828
test262/test/built-ins/Iterator/prototype/constructor/prop-desc.js:10: strict mode: Test262Error: Expected SameValue(«"undefined"», «"function"») to be true
2929
test262/test/built-ins/Iterator/prototype/constructor/weird-setter.js:23: TypeError: cannot read property 'call' of undefined
3030
test262/test/built-ins/Iterator/prototype/constructor/weird-setter.js:23: strict mode: TypeError: cannot read property 'call' of undefined
31-
test262/test/built-ins/Iterator/prototype/drop/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
32-
test262/test/built-ins/Iterator/prototype/drop/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
33-
test262/test/built-ins/Iterator/prototype/every/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
34-
test262/test/built-ins/Iterator/prototype/every/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
35-
test262/test/built-ins/Iterator/prototype/filter/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
36-
test262/test/built-ins/Iterator/prototype/filter/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
37-
test262/test/built-ins/Iterator/prototype/find/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
38-
test262/test/built-ins/Iterator/prototype/find/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
39-
test262/test/built-ins/Iterator/prototype/flatMap/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
40-
test262/test/built-ins/Iterator/prototype/flatMap/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
41-
test262/test/built-ins/Iterator/prototype/forEach/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
42-
test262/test/built-ins/Iterator/prototype/forEach/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
43-
test262/test/built-ins/Iterator/prototype/map/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
44-
test262/test/built-ins/Iterator/prototype/map/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
45-
test262/test/built-ins/Iterator/prototype/reduce/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
46-
test262/test/built-ins/Iterator/prototype/reduce/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
47-
test262/test/built-ins/Iterator/prototype/some/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
48-
test262/test/built-ins/Iterator/prototype/some/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
49-
test262/test/built-ins/Iterator/prototype/take/argument-validation-failure-closes-underlying.js:18: Test262Error: Expected SameValue(«false», «true») to be true
50-
test262/test/built-ins/Iterator/prototype/take/argument-validation-failure-closes-underlying.js:18: strict mode: Test262Error: Expected SameValue(«false», «true») to be true
5131
test262/test/built-ins/Object/defineProperties/typedarray-backed-by-resizable-buffer.js:20: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all
5232
test262/test/built-ins/Object/defineProperties/typedarray-backed-by-resizable-buffer.js:20: strict mode: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all
5333
test262/test/built-ins/Object/defineProperty/coerced-P-grow.js:45: TypeError: out-of-bound index in typed array

0 commit comments

Comments
 (0)