Skip to content

Commit 17a8772

Browse files
committed
Implement Iterator.prototype.map
1 parent 4933e17 commit 17a8772

File tree

2 files changed

+35
-59
lines changed

2 files changed

+35
-59
lines changed

quickjs.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40328,7 +40328,13 @@ static JSValue js_iterator_proto_forEach(JSContext *ctx, JSValue this_val,
4032840328
static JSValue js_iterator_proto_map(JSContext *ctx, JSValue this_val,
4032940329
int argc, JSValue *argv)
4033040330
{
40331-
return JS_ThrowInternalError(ctx, "TODO implement Iterator.prototype.map");
40331+
JSValue func;
40332+
if (!JS_IsObject(this_val))
40333+
return JS_ThrowTypeError(ctx, "Iterator.prototype.map called on non-object");
40334+
func = argv[0];
40335+
if (check_function(ctx, func))
40336+
return JS_EXCEPTION;
40337+
return js_create_iterator_helper(ctx, this_val, JS_ITERATOR_HELPER_KIND_MAP, func, 0);
4033240338
}
4033340339

4033440340
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,
4068740693
goto filter_again;
4068840694
}
4068940695
break;
40696+
case JS_ITERATOR_HELPER_KIND_MAP:
40697+
{
40698+
JSValue item, method, index_val, args[2];
40699+
if (magic == GEN_MAGIC_NEXT) {
40700+
method = js_dup(it->next);
40701+
} else {
40702+
method = JS_GetProperty(ctx, it->obj, JS_ATOM_return);
40703+
if (JS_IsException(method))
40704+
goto fail;
40705+
}
40706+
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
40707+
JS_FreeValue(ctx, method);
40708+
if (JS_IsException(item))
40709+
goto fail;
40710+
if (*pdone || magic == GEN_MAGIC_RETURN) {
40711+
ret = item;
40712+
goto done;
40713+
}
40714+
index_val = js_int64(it->count++);
40715+
args[0] = item;
40716+
args[1] = index_val;
40717+
ret = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args);
40718+
JS_FreeValue(ctx, index_val);
40719+
if (JS_IsException(ret))
40720+
goto fail;
40721+
goto done;
40722+
}
40723+
break;
4069040724
case JS_ITERATOR_HELPER_KIND_TAKE:
4069140725
{
4069240726
JSValue item, method;

test262_errors.txt

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -98,64 +98,6 @@ test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed-in-
9898
test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.flatMap
9999
test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.flatMap
100100
test262/test/built-ins/Iterator/prototype/flatMap/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.flatMap
101-
test262/test/built-ins/Iterator/prototype/map/argument-effect-order.js:21: Test262Error: Expected a TypeError but got a InternalError
102-
test262/test/built-ins/Iterator/prototype/map/argument-effect-order.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError
103-
test262/test/built-ins/Iterator/prototype/map/callable.js:10: InternalError: TODO implement Iterator.prototype.map
104-
test262/test/built-ins/Iterator/prototype/map/callable.js:10: strict mode: InternalError: TODO implement Iterator.prototype.map
105-
test262/test/built-ins/Iterator/prototype/map/exhaustion-does-not-call-return.js:31: InternalError: TODO implement Iterator.prototype.map
106-
test262/test/built-ins/Iterator/prototype/map/exhaustion-does-not-call-return.js:31: strict mode: InternalError: TODO implement Iterator.prototype.map
107-
test262/test/built-ins/Iterator/prototype/map/get-next-method-only-once.js:38: InternalError: TODO implement Iterator.prototype.map
108-
test262/test/built-ins/Iterator/prototype/map/get-next-method-only-once.js:38: strict mode: InternalError: TODO implement Iterator.prototype.map
109-
test262/test/built-ins/Iterator/prototype/map/get-next-method-throws.js:17: Test262Error: Expected a Test262Error but got a InternalError
110-
test262/test/built-ins/Iterator/prototype/map/get-next-method-throws.js:17: strict mode: Test262Error: Expected a Test262Error but got a InternalError
111-
test262/test/built-ins/Iterator/prototype/map/get-return-method-throws.js:25: InternalError: TODO implement Iterator.prototype.map
112-
test262/test/built-ins/Iterator/prototype/map/get-return-method-throws.js:25: strict mode: InternalError: TODO implement Iterator.prototype.map
113-
test262/test/built-ins/Iterator/prototype/map/iterator-already-exhausted.js:19: InternalError: TODO implement Iterator.prototype.map
114-
test262/test/built-ins/Iterator/prototype/map/iterator-already-exhausted.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map
115-
test262/test/built-ins/Iterator/prototype/map/iterator-return-method-throws.js:25: InternalError: TODO implement Iterator.prototype.map
116-
test262/test/built-ins/Iterator/prototype/map/iterator-return-method-throws.js:25: strict mode: InternalError: TODO implement Iterator.prototype.map
117-
test262/test/built-ins/Iterator/prototype/map/mapper-args.js:24: InternalError: TODO implement Iterator.prototype.map
118-
test262/test/built-ins/Iterator/prototype/map/mapper-args.js:24: strict mode: InternalError: TODO implement Iterator.prototype.map
119-
test262/test/built-ins/Iterator/prototype/map/mapper-this.js:26: InternalError: TODO implement Iterator.prototype.map
120-
test262/test/built-ins/Iterator/prototype/map/mapper-this.js:26: strict mode: InternalError: TODO implement Iterator.prototype.map
121-
test262/test/built-ins/Iterator/prototype/map/mapper-throws-then-closing-iterator-also-throws.js:30: InternalError: TODO implement Iterator.prototype.map
122-
test262/test/built-ins/Iterator/prototype/map/mapper-throws-then-closing-iterator-also-throws.js:30: strict mode: InternalError: TODO implement Iterator.prototype.map
123-
test262/test/built-ins/Iterator/prototype/map/mapper-throws.js:31: InternalError: TODO implement Iterator.prototype.map
124-
test262/test/built-ins/Iterator/prototype/map/mapper-throws.js:31: strict mode: InternalError: TODO implement Iterator.prototype.map
125-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-non-object.js:19: InternalError: TODO implement Iterator.prototype.map
126-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-non-object.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map
127-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-done.js:27: InternalError: TODO implement Iterator.prototype.map
128-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-done.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map
129-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value-done.js:27: InternalError: TODO implement Iterator.prototype.map
130-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value-done.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map
131-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value.js:27: InternalError: TODO implement Iterator.prototype.map
132-
test262/test/built-ins/Iterator/prototype/map/next-method-returns-throwing-value.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map
133-
test262/test/built-ins/Iterator/prototype/map/next-method-throws.js:19: InternalError: TODO implement Iterator.prototype.map
134-
test262/test/built-ins/Iterator/prototype/map/next-method-throws.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map
135-
test262/test/built-ins/Iterator/prototype/map/non-callable-mapper.js:18: Test262Error: Expected a TypeError but got a InternalError
136-
test262/test/built-ins/Iterator/prototype/map/non-callable-mapper.js:18: strict mode: Test262Error: Expected a TypeError but got a InternalError
137-
test262/test/built-ins/Iterator/prototype/map/result-is-iterator.js:11: InternalError: TODO implement Iterator.prototype.map
138-
test262/test/built-ins/Iterator/prototype/map/result-is-iterator.js:11: strict mode: InternalError: TODO implement Iterator.prototype.map
139-
test262/test/built-ins/Iterator/prototype/map/return-is-forwarded-to-underlying-iterator.js:28: InternalError: TODO implement Iterator.prototype.map
140-
test262/test/built-ins/Iterator/prototype/map/return-is-forwarded-to-underlying-iterator.js:28: strict mode: InternalError: TODO implement Iterator.prototype.map
141-
test262/test/built-ins/Iterator/prototype/map/return-is-not-forwarded-after-exhaustion.js:27: InternalError: TODO implement Iterator.prototype.map
142-
test262/test/built-ins/Iterator/prototype/map/return-is-not-forwarded-after-exhaustion.js:27: strict mode: InternalError: TODO implement Iterator.prototype.map
143-
test262/test/built-ins/Iterator/prototype/map/returned-iterator-yields-mapper-return-values.js:23: InternalError: TODO implement Iterator.prototype.map
144-
test262/test/built-ins/Iterator/prototype/map/returned-iterator-yields-mapper-return-values.js:23: strict mode: InternalError: TODO implement Iterator.prototype.map
145-
test262/test/built-ins/Iterator/prototype/map/this-non-callable-next.js:13: InternalError: TODO implement Iterator.prototype.map
146-
test262/test/built-ins/Iterator/prototype/map/this-non-callable-next.js:13: strict mode: InternalError: TODO implement Iterator.prototype.map
147-
test262/test/built-ins/Iterator/prototype/map/this-non-object.js:21: Test262Error: Expected a TypeError but got a InternalError
148-
test262/test/built-ins/Iterator/prototype/map/this-non-object.js:21: strict mode: Test262Error: Expected a TypeError but got a InternalError
149-
test262/test/built-ins/Iterator/prototype/map/this-plain-iterator.js:24: InternalError: TODO implement Iterator.prototype.map
150-
test262/test/built-ins/Iterator/prototype/map/this-plain-iterator.js:24: strict mode: InternalError: TODO implement Iterator.prototype.map
151-
test262/test/built-ins/Iterator/prototype/map/throws-typeerror-when-generator-is-running.js:39: InternalError: TODO implement Iterator.prototype.map
152-
test262/test/built-ins/Iterator/prototype/map/throws-typeerror-when-generator-is-running.js:39: strict mode: InternalError: TODO implement Iterator.prototype.map
153-
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-advanced-in-parallel.js:19: InternalError: TODO implement Iterator.prototype.map
154-
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-advanced-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map
155-
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-parallel.js:19: InternalError: TODO implement Iterator.prototype.map
156-
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.map
157-
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.map
158-
test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.map
159101
test262/test/built-ins/RegExp/property-escapes/generated/Alphabetic.js:16: Test262Error: `\P{Alphabetic}` should match U+000363 (`ͣ`)
160102
test262/test/built-ins/RegExp/property-escapes/generated/Alphabetic.js:16: strict mode: Test262Error: `\P{Alphabetic}` should match U+000363 (`ͣ`)
161103
test262/test/built-ins/RegExp/property-escapes/generated/Assigned.js:16: Test262Error: `\P{Assigned}` should match U+001B7F (`᭿`)

0 commit comments

Comments
 (0)