Skip to content

Commit de58faa

Browse files
committed
Implement Iterator.prototype.reduce
1 parent f78d1e6 commit de58faa

File tree

2 files changed

+61
-47
lines changed

2 files changed

+61
-47
lines changed

quickjs.c

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40108,7 +40108,67 @@ static JSValue js_iterator_proto_map(JSContext *ctx, JSValue this_val,
4010840108
static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValue this_val,
4010940109
int argc, JSValue *argv)
4011040110
{
40111-
return JS_ThrowInternalError(ctx, "TODO implement Iterator.prototype.reduce");
40111+
JSValue item, method, ret, func, index_val, acc;
40112+
JSValue args[3];
40113+
int64_t idx;
40114+
BOOL done;
40115+
40116+
if (!JS_IsObject(this_val))
40117+
return JS_ThrowTypeError(ctx, "Iterator.prototype.reduce called on non-object");
40118+
if (check_function(ctx, argv[0]))
40119+
return JS_EXCEPTION;
40120+
acc = JS_UNDEFINED;
40121+
func = js_dup(argv[0]);
40122+
method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
40123+
if (JS_IsException(method))
40124+
goto exception;
40125+
if (argc > 1) {
40126+
acc = js_dup(argv[1]);
40127+
idx = 0;
40128+
} else {
40129+
acc = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
40130+
if (JS_IsException(acc))
40131+
goto exception;
40132+
if (done) {
40133+
JS_ThrowTypeError(ctx, "empty iterator");
40134+
goto exception;
40135+
}
40136+
idx = 1;
40137+
}
40138+
for (/* empty */; /*empty*/; idx++) {
40139+
item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
40140+
if (JS_IsException(item))
40141+
goto exception;
40142+
if (done)
40143+
break;
40144+
index_val = JS_NewInt64(ctx, idx);
40145+
if (JS_IsException(index_val)) {
40146+
JS_FreeValue(ctx, item);
40147+
goto exception;
40148+
}
40149+
args[0] = acc;
40150+
args[1] = item;
40151+
args[2] = index_val;
40152+
ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
40153+
JS_FreeValue(ctx, item);
40154+
JS_FreeValue(ctx, index_val);
40155+
if (JS_IsException(ret))
40156+
goto exception;
40157+
JS_FreeValue(ctx, acc);
40158+
acc = ret;
40159+
index_val = JS_UNDEFINED;
40160+
ret = JS_UNDEFINED;
40161+
item = JS_UNDEFINED;
40162+
}
40163+
JS_FreeValue(ctx, func);
40164+
JS_FreeValue(ctx, method);
40165+
return acc;
40166+
exception:
40167+
JS_IteratorClose(ctx, this_val, TRUE);
40168+
JS_FreeValue(ctx, acc);
40169+
JS_FreeValue(ctx, func);
40170+
JS_FreeValue(ctx, method);
40171+
return JS_EXCEPTION;
4011240172
}
4011340173

4011440174
static JSValue js_iterator_proto_some(JSContext *ctx, JSValue this_val,

test262_errors.txt

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -270,52 +270,6 @@ test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-para
270270
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map
271271
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.map
272272
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.map
273-
test262/test/built-ins/Iterator/prototype/reduce/argument-effect-order.js:16: Test262Error: Expected a TypeError but got a InternalError
274-
test262/test/built-ins/Iterator/prototype/reduce/argument-effect-order.js:16: strict mode: Test262Error: Expected a TypeError but got a InternalError
275-
test262/test/built-ins/Iterator/prototype/reduce/callable.js:10: InternalError: TODO implement Iterator.prototype.reduce
276-
test262/test/built-ins/Iterator/prototype/reduce/callable.js:10: strict mode: InternalError: TODO implement Iterator.prototype.reduce
277-
test262/test/built-ins/Iterator/prototype/reduce/get-next-method-only-once.js:33: InternalError: TODO implement Iterator.prototype.reduce
278-
test262/test/built-ins/Iterator/prototype/reduce/get-next-method-only-once.js:33: strict mode: InternalError: TODO implement Iterator.prototype.reduce
279-
test262/test/built-ins/Iterator/prototype/reduce/get-next-method-throws.js:15: Test262Error: Expected a Test262Error but got a InternalError
280-
test262/test/built-ins/Iterator/prototype/reduce/get-next-method-throws.js:15: strict mode: Test262Error: Expected a Test262Error but got a InternalError
281-
test262/test/built-ins/Iterator/prototype/reduce/iterator-already-exhausted-initial-value.js:20: InternalError: TODO implement Iterator.prototype.reduce
282-
test262/test/built-ins/Iterator/prototype/reduce/iterator-already-exhausted-initial-value.js:20: strict mode: InternalError: TODO implement Iterator.prototype.reduce
283-
test262/test/built-ins/Iterator/prototype/reduce/iterator-already-exhausted-no-initial-value.js:19: Test262Error: Expected a TypeError but got a InternalError
284-
test262/test/built-ins/Iterator/prototype/reduce/iterator-already-exhausted-no-initial-value.js:19: strict mode: Test262Error: Expected a TypeError but got a InternalError
285-
test262/test/built-ins/Iterator/prototype/reduce/iterator-yields-once-initial-value.js:32: InternalError: TODO implement Iterator.prototype.reduce
286-
test262/test/built-ins/Iterator/prototype/reduce/iterator-yields-once-initial-value.js:32: strict mode: InternalError: TODO implement Iterator.prototype.reduce
287-
test262/test/built-ins/Iterator/prototype/reduce/iterator-yields-once-no-initial-value.js:23: InternalError: TODO implement Iterator.prototype.reduce
288-
test262/test/built-ins/Iterator/prototype/reduce/iterator-yields-once-no-initial-value.js:23: strict mode: InternalError: TODO implement Iterator.prototype.reduce
289-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-non-object.js:21: Test262Error: Expected a TypeError but got a InternalError
290-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-non-object.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError
291-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-done.js:17: Test262Error: Expected a Test262Error but got a InternalError
292-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-done.js:17: strict mode: Test262Error: Expected a Test262Error but got a InternalError
293-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-value-done.js:28: InternalError: TODO implement Iterator.prototype.reduce
294-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-value-done.js:28: strict mode: InternalError: TODO implement Iterator.prototype.reduce
295-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-value.js:18: Test262Error: Expected a Test262Error but got a InternalError
296-
test262/test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-value.js:18: strict mode: Test262Error: Expected a Test262Error but got a InternalError
297-
test262/test/built-ins/Iterator/prototype/reduce/next-method-throws.js:15: Test262Error: Expected a Test262Error but got a InternalError
298-
test262/test/built-ins/Iterator/prototype/reduce/next-method-throws.js:15: strict mode: Test262Error: Expected a Test262Error but got a InternalError
299-
test262/test/built-ins/Iterator/prototype/reduce/non-callable-reducer.js:18: Test262Error: Expected a TypeError but got a InternalError
300-
test262/test/built-ins/Iterator/prototype/reduce/non-callable-reducer.js:18: strict mode: Test262Error: Expected a TypeError but got a InternalError
301-
test262/test/built-ins/Iterator/prototype/reduce/reducer-args-initial-value.js:42: InternalError: TODO implement Iterator.prototype.reduce
302-
test262/test/built-ins/Iterator/prototype/reduce/reducer-args-initial-value.js:42: strict mode: InternalError: TODO implement Iterator.prototype.reduce
303-
test262/test/built-ins/Iterator/prototype/reduce/reducer-args-no-initial-value.js:38: InternalError: TODO implement Iterator.prototype.reduce
304-
test262/test/built-ins/Iterator/prototype/reduce/reducer-args-no-initial-value.js:38: strict mode: InternalError: TODO implement Iterator.prototype.reduce
305-
test262/test/built-ins/Iterator/prototype/reduce/reducer-memo-can-be-any-type.js:28: InternalError: TODO implement Iterator.prototype.reduce
306-
test262/test/built-ins/Iterator/prototype/reduce/reducer-memo-can-be-any-type.js:28: strict mode: InternalError: TODO implement Iterator.prototype.reduce
307-
test262/test/built-ins/Iterator/prototype/reduce/reducer-this.js:29: InternalError: TODO implement Iterator.prototype.reduce
308-
test262/test/built-ins/Iterator/prototype/reduce/reducer-this.js:29: strict mode: InternalError: TODO implement Iterator.prototype.reduce
309-
test262/test/built-ins/Iterator/prototype/reduce/reducer-throws-then-closing-iterator-also-throws.js:30: Test262Error: Expected a Test262Error but got a InternalError
310-
test262/test/built-ins/Iterator/prototype/reduce/reducer-throws-then-closing-iterator-also-throws.js:30: strict mode: Test262Error: Expected a Test262Error but got a InternalError
311-
test262/test/built-ins/Iterator/prototype/reduce/reducer-throws.js:32: Test262Error: Expected a Test262Error but got a InternalError
312-
test262/test/built-ins/Iterator/prototype/reduce/reducer-throws.js:32: strict mode: Test262Error: Expected a Test262Error but got a InternalError
313-
test262/test/built-ins/Iterator/prototype/reduce/this-non-callable-next.js:13: Test262Error: Expected a TypeError but got a InternalError
314-
test262/test/built-ins/Iterator/prototype/reduce/this-non-callable-next.js:13: strict mode: Test262Error: Expected a TypeError but got a InternalError
315-
test262/test/built-ins/Iterator/prototype/reduce/this-non-object.js:23: Test262Error: Expected a TypeError but got a InternalError
316-
test262/test/built-ins/Iterator/prototype/reduce/this-non-object.js:23: strict mode: Test262Error: Expected a TypeError but got a InternalError
317-
test262/test/built-ins/Iterator/prototype/reduce/this-plain-iterator.js:29: InternalError: TODO implement Iterator.prototype.reduce
318-
test262/test/built-ins/Iterator/prototype/reduce/this-plain-iterator.js:29: strict mode: InternalError: TODO implement Iterator.prototype.reduce
319273
test262/test/built-ins/Iterator/prototype/some/argument-effect-order.js:16: Test262Error: Expected a TypeError but got a InternalError
320274
test262/test/built-ins/Iterator/prototype/some/argument-effect-order.js:16: strict mode: Test262Error: Expected a TypeError but got a InternalError
321275
test262/test/built-ins/Iterator/prototype/some/callable.js:10: InternalError: TODO implement Iterator.prototype.some

0 commit comments

Comments
 (0)