Implement Iterator.prototype.take (#676)

This commit is contained in:
Saúl Ibarra Corretgé 2024-11-12 08:10:19 +01:00 committed by GitHub
parent 487129055f
commit 0362c0a4eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 71 additions and 59 deletions

View file

@ -40060,9 +40060,10 @@ static JSValue js_iterator_proto_drop(JSContext *ctx, JSValue this_val,
if (JS_ToInt64Free(ctx, &limit, v)) if (JS_ToInt64Free(ctx, &limit, v))
return JS_EXCEPTION; return JS_EXCEPTION;
} }
if (limit < 0) if (limit < 0) {
fail: fail:
return JS_ThrowRangeError(ctx, "must be positive"); return JS_ThrowRangeError(ctx, "must be positive");
}
return js_create_iterator_helper(ctx, this_val, JS_ITERATOR_HELPER_KIND_DROP, JS_UNDEFINED, limit); return js_create_iterator_helper(ctx, this_val, JS_ITERATOR_HELPER_KIND_DROP, JS_UNDEFINED, limit);
} }
@ -40373,7 +40374,41 @@ exception:
static JSValue js_iterator_proto_take(JSContext *ctx, JSValue this_val, static JSValue js_iterator_proto_take(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv) int argc, JSValue *argv)
{ {
return JS_ThrowInternalError(ctx, "TODO implement Iterator.prototype.take"); JSValue v;
double dlimit;
int64_t limit;
if (!JS_IsObject(this_val))
return JS_ThrowTypeError(ctx, "Iterator.prototype.take called on non-object");
v = JS_ToNumber(ctx, argv[0]);
if (JS_IsException(v))
return JS_EXCEPTION;
// Check for Infinity.
if (JS_ToFloat64(ctx, &dlimit, v)) {
JS_FreeValue(ctx, v);
return JS_EXCEPTION;
}
if (isnan(dlimit)) {
JS_FreeValue(ctx, v);
goto fail;
}
if (!isfinite(dlimit)) {
JS_FreeValue(ctx, v);
if (dlimit < 0)
goto fail;
else
limit = MAX_SAFE_INTEGER;
} else {
v = JS_ToIntegerFree(ctx, v);
if (JS_IsException(v))
return JS_EXCEPTION;
if (JS_ToInt64Free(ctx, &limit, v))
return JS_EXCEPTION;
}
if (limit < 0) {
fail:
return JS_ThrowRangeError(ctx, "must be positive");
}
return js_create_iterator_helper(ctx, this_val, JS_ITERATOR_HELPER_KIND_TAKE, JS_UNDEFINED, limit);
} }
static JSValue js_iterator_proto_toArray(JSContext *ctx, JSValue this_val, static JSValue js_iterator_proto_toArray(JSContext *ctx, JSValue this_val,
@ -40490,12 +40525,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValue this_val,
JSIteratorHelperData *it; JSIteratorHelperData *it;
JSValue ret; JSValue ret;
*pdone = FALSE;
it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_HELPER); it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_HELPER);
if (!it) if (!it)
goto fail; goto fail;
if (it->executing) if (it->executing)
return JS_ThrowTypeError(ctx, "cannot invoke a running iterator"); return JS_ThrowTypeError(ctx, "cannot invoke a running iterator");
if (magic == GEN_MAGIC_RETURN && it->done) if (it->done)
return JS_UNDEFINED; return JS_UNDEFINED;
it->executing = TRUE; it->executing = TRUE;
@ -40536,6 +40573,34 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValue this_val,
goto done; goto done;
} }
break; break;
case JS_ITERATOR_HELPER_KIND_TAKE:
{
JSValue item, method;
if (it->limit > 0) {
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;
}
it->limit--;
item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
JS_FreeValue(ctx, method);
if (JS_IsException(item))
goto fail;
ret = item;
goto done;
}
*pdone = TRUE;
if (JS_IteratorClose(ctx, it->obj, FALSE))
ret = JS_EXCEPTION;
else
ret = JS_UNDEFINED;
goto done;
}
break;
default: default:
abort(); abort();
} }
@ -40545,13 +40610,12 @@ done:
it->executing = FALSE; it->executing = FALSE;
return ret; return ret;
fail: fail:
it->executing = FALSE; if (it) {
if (it && JS_IsObject(it->obj)) {
/* close the iterator object, preserving pending exception */ /* close the iterator object, preserving pending exception */
JS_IteratorClose(ctx, it->obj, TRUE); JS_IteratorClose(ctx, it->obj, TRUE);
} }
*pdone = FALSE; ret = JS_EXCEPTION;
return JS_EXCEPTION; goto done;
} }
static JSValue js_create_iterator_helper(JSContext *ctx, JSValue iterator, static JSValue js_create_iterator_helper(JSContext *ctx, JSValue iterator,

View file

@ -216,58 +216,6 @@ test262/test/built-ins/Iterator/prototype/map/underlying-iterator-closed-in-para
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-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: 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/Iterator/prototype/map/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.map
test262/test/built-ins/Iterator/prototype/take/argument-effect-order.js:24: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/argument-effect-order.js:24: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/callable.js:10: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/callable.js:10: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/exhaustion-calls-return.js:35: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/exhaustion-calls-return.js:35: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/get-next-method-only-once.js:38: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/get-next-method-only-once.js:38: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/get-next-method-throws.js:17: Test262Error: Expected a Test262Error but got a InternalError
test262/test/built-ins/Iterator/prototype/take/get-next-method-throws.js:17: strict mode: Test262Error: Expected a Test262Error but got a InternalError
test262/test/built-ins/Iterator/prototype/take/get-return-method-throws.js:25: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/get-return-method-throws.js:25: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-greater-than-or-equal-to-total.js:23: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-greater-than-or-equal-to-total.js:23: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-less-than-total.js:25: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-less-than-total.js:25: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-rangeerror.js:18: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-rangeerror.js:18: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-tonumber-throws.js:16: Test262Error: Expected a Test262Error but got a InternalError
test262/test/built-ins/Iterator/prototype/take/limit-tonumber-throws.js:16: strict mode: Test262Error: Expected a Test262Error but got a InternalError
test262/test/built-ins/Iterator/prototype/take/limit-tonumber.js:23: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/limit-tonumber.js:23: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-non-object.js:21: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-non-object.js:21: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-throwing-done.js:32: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-throwing-done.js:32: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-throwing-value-done.js:30: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-throwing-value-done.js:30: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-throwing-value.js:32: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-returns-throwing-value.js:32: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-throws.js:21: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/next-method-throws.js:21: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/result-is-iterator.js:11: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/result-is-iterator.js:11: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/return-is-forwarded.js:28: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/return-is-forwarded.js:28: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/return-is-not-forwarded-after-exhaustion.js:27: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/return-is-not-forwarded-after-exhaustion.js:27: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/this-non-callable-next.js:15: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/this-non-callable-next.js:15: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/this-non-object.js:22: Test262Error: Expected a TypeError but got a InternalError
test262/test/built-ins/Iterator/prototype/take/this-non-object.js:22: strict mode: Test262Error: Expected a TypeError but got a InternalError
test262/test/built-ins/Iterator/prototype/take/this-plain-iterator.js:25: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/this-plain-iterator.js:25: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/throws-typeerror-when-generator-is-running.js:33: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/throws-typeerror-when-generator-is-running.js:33: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/underlying-iterator-advanced-in-parallel.js:19: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/underlying-iterator-advanced-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed-in-parallel.js:19: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.take
test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.take
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: 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/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 (`᭿`) test262/test/built-ins/RegExp/property-escapes/generated/Assigned.js:16: Test262Error: `\P{Assigned}` should match U+001B7F (`᭿`)