Skip to content

Write back every element in Array.prototype.sort - #1843

Open
theaddonn wants to merge 3 commits into
quickjs-ng:masterfrom
theaddonn:fix-sort-write-back
Open

theaddonn wants to merge 3 commits into
quickjs-ng:masterfrom
theaddonn:fix-sort-write-back

Conversation

@theaddonn

Copy link
Copy Markdown
Contributor

Fixes #1736

sort skipped writing elements that were already in place, so Array.prototype.sort.call("aa") and sorting a frozen, already sorted array didn't throw. Now every element is written back like the spec says. No measurable slowdown on a 200k element presorted array.

sort skipped the write for elements that were already in place. The
spec writes every sorted value back with Set(obj, j, v, true), so a
read-only receiver such as a String or a frozen array must throw, and
setters must see each write.

Always write the value back. This made no measurable difference when
sorting a large already-sorted array.

Fixes: quickjs-ng#1736
Comment thread quickjs.c Outdated
goto exception;
}
if (JS_SetPropertyInt64(ctx, obj, n, array[n].val) < 0) {
n++;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we could rewrite the loop to unconditionally increment n on every run now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, done in 76de57b.

Comment thread tests/array-sort-write-back.js Outdated
/* Array.prototype.sort writes every sorted value back with Set(..., true),
even ones that are already in place. */

function throwsTypeError(f) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have assertThrows, please use that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed that, switched to it in 1431684.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array.prototype.sort does not write back to a String receiver

2 participants