-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Implement Iterator.prototype.take #676
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40370,7 +40370,40 @@ static JSValue js_iterator_proto_some(JSContext *ctx, JSValue this_val, | |
static JSValue js_iterator_proto_take(JSContext *ctx, JSValue this_val, | ||
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, | ||
|
@@ -40487,12 +40520,14 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValue this_val, | |
JSIteratorHelperData *it; | ||
JSValue ret; | ||
|
||
*pdone = FALSE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should fix the unitialized jump valgrind caught: https://github.com/quickjs-ng/quickjs/actions/runs/11786411039/job/32829640312 |
||
|
||
it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_HELPER); | ||
if (!it) | ||
goto fail; | ||
if (it->executing) | ||
return JS_ThrowTypeError(ctx, "cannot invoke a running iterator"); | ||
if (magic == GEN_MAGIC_RETURN && it->done) | ||
if (it->done) | ||
return JS_UNDEFINED; | ||
|
||
it->executing = TRUE; | ||
|
@@ -40533,6 +40568,34 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValue this_val, | |
goto done; | ||
} | ||
break; | ||
case JS_ITERATOR_HELPER_KIND_TAKE: | ||
{ | ||
JSValue item, method; | ||
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; | ||
} | ||
if (it->limit > 0) { | ||
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: | ||
abort(); | ||
} | ||
|
@@ -40542,12 +40605,12 @@ static JSValue js_iterator_helper_next(JSContext *ctx, JSValue this_val, | |
it->executing = FALSE; | ||
return ret; | ||
fail: | ||
it->done = magic == GEN_MAGIC_NEXT ? *pdone : TRUE; | ||
it->executing = FALSE; | ||
if (it && JS_IsObject(it->obj)) { | ||
if (it) { | ||
/* close the iterator object, preserving pending exception */ | ||
JS_IteratorClose(ctx, it->obj, TRUE); | ||
} | ||
*pdone = FALSE; | ||
return JS_EXCEPTION; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(dominant style for this kind of thing)