From dae1001565a1bd8d1090d43c44141bd19bdd57b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 12 Nov 2024 23:15:30 +0100 Subject: [PATCH] Implement Iterator.prototype.map --- quickjs.c | 36 +++++++++++++++++++++++++++- test262_errors.txt | 58 ---------------------------------------------- 2 files changed, 35 insertions(+), 59 deletions(-) diff --git a/quickjs.c b/quickjs.c index 900dec4c..18382b48 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40328,7 +40328,13 @@ static JSValue js_iterator_proto_forEach(JSContext *ctx, JSValue this_val, static JSValue js_iterator_proto_map(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - return JS_ThrowInternalError(ctx, "TODO implement Iterator.prototype.map"); + JSValue func; + if (!JS_IsObject(this_val)) + return JS_ThrowTypeError(ctx, "Iterator.prototype.map called on non-object"); + func = argv[0]; + if (check_function(ctx, func)) + return JS_EXCEPTION; + return js_create_iterator_helper(ctx, this_val, JS_ITERATOR_HELPER_KIND_MAP, func, 0); } static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValue this_val, @@ -40687,6 +40693,34 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValue this_val, goto filter_again; } break; + case JS_ITERATOR_HELPER_KIND_MAP: + { + JSValue item, method, index_val, args[2]; + if (magic == GEN_MAGIC_NEXT) { + method = js_dup(it->next); + } else { + method = JS_GetProperty(ctx, it->obj, JS_ATOM_return); + if (JS_IsException(method)) + goto fail; + } + item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone); + JS_FreeValue(ctx, method); + if (JS_IsException(item)) + goto fail; + if (*pdone || magic == GEN_MAGIC_RETURN) { + ret = item; + goto done; + } + index_val = js_int64(it->count++); + args[0] = item; + args[1] = index_val; + ret = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args); + JS_FreeValue(ctx, index_val); + if (JS_IsException(ret)) + goto fail; + goto done; + } + break; case JS_ITERATOR_HELPER_KIND_TAKE: { JSValue item, method; diff --git a/test262_errors.txt b/test262_errors.txt index 116162be..816dd62a 100644 --- a/test262_errors.txt +++ b/test262_errors.txt @@ -98,64 +98,6 @@ test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed-in- test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.flatMap test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.flatMap test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.flatMap -test262/test/built-ins/Iterator/prototype/map/argument-effect-order.js:21: Test262Error: Expected a TypeError but got a InternalError -test262/test/built-ins/Iterator/prototype/map/argument-effect-order.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError -test262/test/built-ins/Iterator/prototype/map/callable.js:10: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/callable.js:10: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/exhaustion-does-not-call-return.js:31: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/exhaustion-does-not-call-return.js:31: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/get-next-method-only-once.js:38: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/get-next-method-only-once.js:38: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/get-next-method-throws.js:17: Test262Error: Expected a Test262Error but got a InternalError -test262/test/built-ins/Iterator/prototype/map/get-next-method-throws.js:17: strict mode: Test262Error: Expected a Test262Error but got a InternalError -test262/test/built-ins/Iterator/prototype/map/get-return-method-throws.js:25: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/get-return-method-throws.js:25: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/iterator-already-exhausted.js:19: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/iterator-already-exhausted.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/iterator-return-method-throws.js:25: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/iterator-return-method-throws.js:25: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-args.js:24: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-args.js:24: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-this.js:26: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-this.js:26: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-throws-then-closing-iterator-also-throws.js:30: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-throws-then-closing-iterator-also-throws.js:30: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-throws.js:31: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/mapper-throws.js:31: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-non-object.js:19: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-non-object.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-done.js:27: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-done.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value-done.js:27: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value-done.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value.js:27: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-throws.js:19: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/next-method-throws.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/non-callable-mapper.js:18: Test262Error: Expected a TypeError but got a InternalError -test262/test/built-ins/Iterator/prototype/map/non-callable-mapper.js:18: strict mode: Test262Error: Expected a TypeError but got a InternalError -test262/test/built-ins/Iterator/prototype/map/result-is-iterator.js:11: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/result-is-iterator.js:11: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/return-is-forwarded-to-underlying-iterator.js:28: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/return-is-forwarded-to-underlying-iterator.js:28: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/return-is-not-forwarded-after-exhaustion.js:27: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/return-is-not-forwarded-after-exhaustion.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/returned-iterator-yields-mapper-return-values.js:23: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/returned-iterator-yields-mapper-return-values.js:23: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/this-non-callable-next.js:13: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/this-non-callable-next.js:13: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/this-non-object.js:21: Test262Error: Expected a TypeError but got a InternalError -test262/test/built-ins/Iterator/prototype/map/this-non-object.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError -test262/test/built-ins/Iterator/prototype/map/this-plain-iterator.js:24: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/this-plain-iterator.js:24: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/throws-typeerror-when-generator-is-running.js:39: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/throws-typeerror-when-generator-is-running.js:39: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/underlying-iterator-advanced-in-parallel.js:19: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/underlying-iterator-advanced-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-parallel.js:19: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.map -test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.map test262/test/built-ins/RegExp/property-escapes/generated/Alphabetic.js:16: Test262Error: `\P{Alphabetic}` should match U+000363 (`ͣ`) test262/test/built-ins/RegExp/property-escapes/generated/Alphabetic.js:16: strict mode: Test262Error: `\P{Alphabetic}` should match U+000363 (`ͣ`) test262/test/built-ins/RegExp/property-escapes/generated/Assigned.js:16: Test262Error: `\P{Assigned}` should match U+001B7F (`᭿`)