Skip to content

Commit

Permalink
Bug 1874373 - Part 2: Call String.p.codePointAt in StringIteratorNext…
Browse files Browse the repository at this point in the history
…. r=jandem

Directly call `String.p.codePointAt` instead of manually computing the code
point in `StringIteratorNext`.

Depends on D198360

Differential Revision: https://phabricator.services.mozilla.com/D199350
  • Loading branch information
anba committed Jan 24, 2024
1 parent 4936e43 commit 1d625e0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion js/src/builtin/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ bool js::str_charCodeAt(JSContext* cx, unsigned argc, Value* vp) {
*
* ES2024 draft rev 7d2644968bd56d54d2886c012d18698ff3f72c35
*/
static bool str_codePointAt(JSContext* cx, unsigned argc, Value* vp) {
bool js::str_codePointAt(JSContext* cx, unsigned argc, Value* vp) {
AutoJSMethodProfilerEntry pseudoFrame(cx, "String.prototype", "codePointAt");
CallArgs args = CallArgsFromVp(argc, vp);

Expand Down
2 changes: 2 additions & 0 deletions js/src/builtin/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern bool str_toString(JSContext* cx, unsigned argc, Value* vp);

extern bool str_charCodeAt(JSContext* cx, unsigned argc, Value* vp);

extern bool str_codePointAt(JSContext* cx, unsigned argc, Value* vp);

extern bool str_endsWith(JSContext* cx, unsigned argc, Value* vp);

#if JS_HAS_INTL_API
Expand Down
14 changes: 3 additions & 11 deletions js/src/builtin/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,20 +819,12 @@ function StringIteratorNext() {
return result;
}

var charCount = 1;
var first = callFunction(std_String_charCodeAt, S, index);
if (first >= 0xd800 && first <= 0xdbff && index + 1 < size) {
var second = callFunction(std_String_charCodeAt, S, index + 1);
if (second >= 0xdc00 && second <= 0xdfff) {
first = (first - 0xd800) * 0x400 + (second - 0xdc00) + 0x10000;
charCount = 2;
}
}
var codePoint = callFunction(std_String_codePointAt, S, index);
var charCount = 1 + (codePoint > 0xffff);

UnsafeSetReservedSlot(obj, ITERATOR_SLOT_NEXT_INDEX, index + charCount);

// Communicate |first|'s possible range to the compiler.
result.value = callFunction(std_String_fromCodePoint, null, first & 0x1fffff);
result.value = callFunction(std_String_fromCodePoint, null, codePoint);

return result;
}
Expand Down
2 changes: 2 additions & 0 deletions js/src/vm/SelfHosting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,8 @@ static const JSFunctionSpec intrinsic_functions[] = {
JS_FN("std_Set_values", SetObject::values, 0, 0),
JS_INLINABLE_FN("std_String_charCodeAt", str_charCodeAt, 1, 0,
StringCharCodeAt),
JS_INLINABLE_FN("std_String_codePointAt", str_codePointAt, 1, 0,
StringCodePointAt),
JS_INLINABLE_FN("std_String_endsWith", str_endsWith, 1, 0, StringEndsWith),
JS_INLINABLE_FN("std_String_fromCharCode", str_fromCharCode, 1, 0,
StringFromCharCode),
Expand Down

0 comments on commit 1d625e0

Please sign in to comment.