Skip to content

Commit 3339ef7

Browse files
committed
Implement Iterator.prototype.forEach
1 parent b9a22f9 commit 3339ef7

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

quickjs.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39990,7 +39990,50 @@ static JSValue js_iterator_proto_flatMap(JSContext *ctx, JSValue this_val,
3999039990
static JSValue js_iterator_proto_forEach(JSContext *ctx, JSValue this_val,
3999139991
int argc, JSValue *argv)
3999239992
{
39993-
return JS_ThrowInternalError(ctx, "TODO implement Iterator.prototype.forEach");
39993+
JSValue item, method, ret, func, index_val;
39994+
JSValue args[2];
39995+
int64_t idx;
39996+
BOOL done;
39997+
39998+
if (!JS_IsObject(this_val))
39999+
return JS_ThrowTypeError(ctx, "Iterator.prototype.forEach called on non-object");
40000+
if (check_function(ctx, argv[0]))
40001+
return JS_EXCEPTION;
40002+
func = js_dup(argv[0]);
40003+
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
40004+
if (JS_IsException(method))
40005+
goto exception;
40006+
for (idx = 0; /*empty*/; idx++) {
40007+
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
40008+
if (JS_IsException(item))
40009+
goto exception;
40010+
if (done)
40011+
break;
40012+
index_val = JS_NewInt64(ctx, idx);
40013+
if (JS_IsException(index_val)) {
40014+
JS_FreeValue(ctx, item);
40015+
goto exception;
40016+
}
40017+
args[0] = item;
40018+
args[1] = index_val;
40019+
ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
40020+
JS_FreeValue(ctx, item);
40021+
JS_FreeValue(ctx, index_val);
40022+
if (JS_IsException(ret))
40023+
goto exception;
40024+
JS_FreeValue(ctx, ret);
40025+
index_val = JS_UNDEFINED;
40026+
ret = JS_UNDEFINED;
40027+
item = JS_UNDEFINED;
40028+
}
40029+
JS_FreeValue(ctx, func);
40030+
JS_FreeValue(ctx, method);
40031+
return JS_UNDEFINED;
40032+
exception:
40033+
JS_IteratorClose(ctx, this_val, TRUE);
40034+
JS_FreeValue(ctx, func);
40035+
JS_FreeValue(ctx, method);
40036+
return JS_EXCEPTION;
3999440037
}
3999540038

3999640039
static JSValue js_iterator_proto_map(JSContext *ctx, JSValue this_val,

test262_errors.txt

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -262,46 +262,6 @@ test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed-in-
262262
test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.flatMap
263263
test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.flatMap
264264
test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.flatMap
265-
test262/test/built-ins/Iterator/prototype/forEach/argument-effect-order.js:21: Test262Error: Expected a TypeError but got a InternalError
266-
test262/test/built-ins/Iterator/prototype/forEach/argument-effect-order.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError
267-
test262/test/built-ins/Iterator/prototype/forEach/callable.js:10: InternalError: TODO implement Iterator.prototype.forEach
268-
test262/test/built-ins/Iterator/prototype/forEach/callable.js:10: strict mode: InternalError: TODO implement Iterator.prototype.forEach
269-
test262/test/built-ins/Iterator/prototype/forEach/fn-args.js:37: InternalError: TODO implement Iterator.prototype.forEach
270-
test262/test/built-ins/Iterator/prototype/forEach/fn-args.js:37: strict mode: InternalError: TODO implement Iterator.prototype.forEach
271-
test262/test/built-ins/Iterator/prototype/forEach/fn-called-for-each-yielded-value.js:26: InternalError: TODO implement Iterator.prototype.forEach
272-
test262/test/built-ins/Iterator/prototype/forEach/fn-called-for-each-yielded-value.js:26: strict mode: InternalError: TODO implement Iterator.prototype.forEach
273-
test262/test/built-ins/Iterator/prototype/forEach/fn-this.js:27: InternalError: TODO implement Iterator.prototype.forEach
274-
test262/test/built-ins/Iterator/prototype/forEach/fn-this.js:27: strict mode: InternalError: TODO implement Iterator.prototype.forEach
275-
test262/test/built-ins/Iterator/prototype/forEach/fn-throws-then-closing-iterator-also-throws.js:30: Test262Error: Expected a Test262Error but got a InternalError
276-
test262/test/built-ins/Iterator/prototype/forEach/fn-throws-then-closing-iterator-also-throws.js:30: strict mode: Test262Error: Expected a Test262Error but got a InternalError
277-
test262/test/built-ins/Iterator/prototype/forEach/fn-throws.js:32: Test262Error: Expected a Test262Error but got a InternalError
278-
test262/test/built-ins/Iterator/prototype/forEach/fn-throws.js:32: strict mode: Test262Error: Expected a Test262Error but got a InternalError
279-
test262/test/built-ins/Iterator/prototype/forEach/get-next-method-only-once.js:33: InternalError: TODO implement Iterator.prototype.forEach
280-
test262/test/built-ins/Iterator/prototype/forEach/get-next-method-only-once.js:33: strict mode: InternalError: TODO implement Iterator.prototype.forEach
281-
test262/test/built-ins/Iterator/prototype/forEach/get-next-method-throws.js:15: Test262Error: Expected a Test262Error but got a InternalError
282-
test262/test/built-ins/Iterator/prototype/forEach/get-next-method-throws.js:15: strict mode: Test262Error: Expected a Test262Error but got a InternalError
283-
test262/test/built-ins/Iterator/prototype/forEach/iterator-already-exhausted.js:21: InternalError: TODO implement Iterator.prototype.forEach
284-
test262/test/built-ins/Iterator/prototype/forEach/iterator-already-exhausted.js:21: strict mode: InternalError: TODO implement Iterator.prototype.forEach
285-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-non-object.js:21: Test262Error: Expected a TypeError but got a InternalError
286-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-non-object.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError
287-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-throwing-done.js:17: Test262Error: Expected a Test262Error but got a InternalError
288-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-throwing-done.js:17: strict mode: Test262Error: Expected a Test262Error but got a InternalError
289-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-throwing-value-done.js:28: InternalError: TODO implement Iterator.prototype.forEach
290-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-throwing-value-done.js:28: strict mode: InternalError: TODO implement Iterator.prototype.forEach
291-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-throwing-value.js:18: Test262Error: Expected a Test262Error but got a InternalError
292-
test262/test/built-ins/Iterator/prototype/forEach/next-method-returns-throwing-value.js:18: strict mode: Test262Error: Expected a Test262Error but got a InternalError
293-
test262/test/built-ins/Iterator/prototype/forEach/next-method-throws.js:15: Test262Error: Expected a Test262Error but got a InternalError
294-
test262/test/built-ins/Iterator/prototype/forEach/next-method-throws.js:15: strict mode: Test262Error: Expected a Test262Error but got a InternalError
295-
test262/test/built-ins/Iterator/prototype/forEach/non-callable-predicate.js:18: Test262Error: Expected a TypeError but got a InternalError
296-
test262/test/built-ins/Iterator/prototype/forEach/non-callable-predicate.js:18: strict mode: Test262Error: Expected a TypeError but got a InternalError
297-
test262/test/built-ins/Iterator/prototype/forEach/result-is-undefined.js:12: InternalError: TODO implement Iterator.prototype.forEach
298-
test262/test/built-ins/Iterator/prototype/forEach/result-is-undefined.js:12: strict mode: InternalError: TODO implement Iterator.prototype.forEach
299-
test262/test/built-ins/Iterator/prototype/forEach/this-non-callable-next.js:13: Test262Error: Expected a TypeError but got a InternalError
300-
test262/test/built-ins/Iterator/prototype/forEach/this-non-callable-next.js:13: strict mode: Test262Error: Expected a TypeError but got a InternalError
301-
test262/test/built-ins/Iterator/prototype/forEach/this-non-object.js:19: Test262Error: Expected a TypeError but got a InternalError
302-
test262/test/built-ins/Iterator/prototype/forEach/this-non-object.js:19: strict mode: Test262Error: Expected a TypeError but got a InternalError
303-
test262/test/built-ins/Iterator/prototype/forEach/this-plain-iterator.js:26: InternalError: TODO implement Iterator.prototype.forEach
304-
test262/test/built-ins/Iterator/prototype/forEach/this-plain-iterator.js:26: strict mode: InternalError: TODO implement Iterator.prototype.forEach
305265
test262/test/built-ins/Iterator/prototype/map/argument-effect-order.js:21: Test262Error: Expected a TypeError but got a InternalError
306266
test262/test/built-ins/Iterator/prototype/map/argument-effect-order.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError
307267
test262/test/built-ins/Iterator/prototype/map/callable.js:10: InternalError: TODO implement Iterator.prototype.map

0 commit comments

Comments
 (0)