Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecmascript): String fromCodePoints #543

Merged
merged 10 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::ecmascript::abstract_operations::testing_and_comparison::is_integral_number;
use crate::ecmascript::abstract_operations::type_conversion::to_number;
use crate::ecmascript::abstract_operations::type_conversion::to_string;
use crate::ecmascript::builders::builtin_function_builder::BuiltinFunctionBuilder;
use crate::ecmascript::builtins::ordinary::get_prototype_from_constructor;
Expand All @@ -12,6 +14,7 @@ use crate::ecmascript::builtins::ArgumentsList;
use crate::ecmascript::builtins::Behaviour;
use crate::ecmascript::builtins::Builtin;
use crate::ecmascript::builtins::BuiltinIntrinsicConstructor;
use crate::ecmascript::execution::agent::ExceptionType;
use crate::ecmascript::execution::Agent;
use crate::ecmascript::execution::JsResult;
use crate::ecmascript::execution::ProtoIntrinsics;
Expand Down Expand Up @@ -161,26 +164,70 @@ impl StringConstructor {
Ok(String::from_string(agent, result, gc.nogc()).into())
}

/// ### [22.1.2.2 String.fromCodePoint ( ...`codePoints` ) ](https://262.ecma-international.org/15.0/index.html#sec-string.fromcodepoint)
/// ### [22.1.2.2 String.fromCodePoint ( ...`codePoints` )](https://tc39.es/ecma262/multipage/text-processing.html#sec-string.fromcodepoint)
///
/// This function may be called with any number of arguments which form
/// the rest parameter `codePoints`.
fn from_code_point(
_agent: &mut Agent,
agent: &mut Agent,
_this_value: Value,
_arguments: ArgumentsList,
_gc: GcScope,
code_points: ArgumentsList,
mut gc: GcScope,
) -> JsResult<Value> {
// 3. Assert: If codePoints is empty, then result is the empty String.
if code_points.is_empty() {
return Ok(String::EMPTY_STRING.into_value());
}
// fast path: only a single valid code unit
if code_points.len() == 1 && code_points.first().unwrap().is_integer() {
// a. Let nextCP be ? ToNumber(next).
// b. If IsIntegralNumber(nextCP) is false, throw a RangeError exception.
// c. If ℝ(nextCP) < 0 or ℝ(nextCP) > 0x10FFFF, throw a RangeError exception.
let Value::Integer(next_cp) = code_points.first().unwrap() else {
unreachable!();
};
let next_cp = next_cp.into_i64();
if !(0..=0x10FFFF).contains(&next_cp) {
return Err(agent.throw_exception(
ExceptionType::RangeError,
format!("{:?} is not a valid code point", next_cp),
gc.nogc(),
));
}
// d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
if let Some(cu) = char::from_u32(next_cp as u32) {
// 4. Return result.
return Ok(SmallString::from(cu).into());
}
};
// 1. Let result be the empty String.
let mut result = std::string::String::new();
// 2. For each element next of codePoints, do
aapoalas marked this conversation as resolved.
Show resolved Hide resolved
// a. Let nextCP be ? ToNumber(next).
// b. If IsIntegralNumber(nextCP) is false, throw a RangeError exception.
// c. If ℝ(nextCP) < 0 or ℝ(nextCP) > 0x10FFFF, throw a RangeError exception.
// d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
// 3. Assert: If codePoints is empty, then result is the empty String.
for next in code_points.iter() {
// a. Let nextCP be ? ToNumber(next).
aapoalas marked this conversation as resolved.
Show resolved Hide resolved
let next_cp = to_number(agent, *next, gc.reborrow())?.unbind();
// b. If IsIntegralNumber(nextCP) is false, throw a RangeError exception.
if !is_integral_number(agent, next_cp, gc.reborrow()) {
return Err(agent.throw_exception(
ExceptionType::RangeError,
format!("{:?} is not a valid code point", next_cp.to_real(agent)),
gc.nogc(),
));
}
// c. If ℝ(nextCP) < 0 or ℝ(nextCP) > 0x10FFFF, throw a RangeError exception.
let next_cp = next_cp.into_i64(agent);
if !(0..=0x10FFFF).contains(&next_cp) {
aapoalas marked this conversation as resolved.
Show resolved Hide resolved
return Err(agent.throw_exception(
ExceptionType::RangeError,
format!("{:?} is not a valid code point", next_cp),
gc.nogc(),
));
}
// d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
result.push(char::from_u32(next_cp as u32).unwrap());
}
// 4. Return result.

todo!()
Ok(String::from_string(agent, result, gc.nogc()).into())
}

fn raw(
Expand Down
32 changes: 17 additions & 15 deletions tests/expectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -3636,7 +3636,7 @@
"built-ins/RegExp/property-escapes/character-class.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ASCII.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ASCII_Hex_Digit.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Alphabetic.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Alphabetic.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/Any.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Assigned.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Bidi_Control.js": "CRASH",
Expand Down Expand Up @@ -3671,7 +3671,7 @@
"built-ins/RegExp/property-escapes/generated/General_Category_-_Final_Punctuation.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Format.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Initial_Punctuation.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Letter_Number.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Line_Separator.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Lowercase_Letter.js": "CRASH",
Expand All @@ -3696,15 +3696,15 @@
"built-ins/RegExp/property-escapes/generated/General_Category_-_Surrogate.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Symbol.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Titlecase_Letter.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Unassigned.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Unassigned.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Uppercase_Letter.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Grapheme_Base.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Grapheme_Base.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/Grapheme_Extend.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Hex_Digit.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/IDS_Binary_Operator.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/IDS_Trinary_Operator.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ID_Continue.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ID_Start.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/ID_Continue.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/ID_Start.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/Ideographic.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Join_Control.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Logical_Order_Exception.js": "CRASH",
Expand Down Expand Up @@ -4063,8 +4063,8 @@
"built-ins/RegExp/property-escapes/generated/Uppercase.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/Variation_Selector.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/White_Space.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/XID_Continue.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/XID_Start.js": "CRASH",
"built-ins/RegExp/property-escapes/generated/XID_Continue.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/XID_Start.js": "TIMEOUT",
"built-ins/RegExp/property-escapes/generated/strings/Basic_Emoji-negative-CharacterClass.js": "FAIL",
"built-ins/RegExp/property-escapes/generated/strings/Basic_Emoji-negative-P.js": "FAIL",
"built-ins/RegExp/property-escapes/generated/strings/Basic_Emoji-negative-u.js": "FAIL",
Expand Down Expand Up @@ -5195,13 +5195,6 @@
"built-ins/SharedArrayBuffer/zero-length.js": "CRASH",
"built-ins/String/S15.5.5.1_A4_T1.js": "CRASH",
"built-ins/String/S9.8_A5_T1.js": "FAIL",
"built-ins/String/fromCodePoint/argument-is-Symbol.js": "CRASH",
"built-ins/String/fromCodePoint/argument-is-not-integer.js": "CRASH",
"built-ins/String/fromCodePoint/argument-not-coercible.js": "CRASH",
"built-ins/String/fromCodePoint/arguments-is-empty.js": "CRASH",
"built-ins/String/fromCodePoint/number-is-out-of-range.js": "CRASH",
"built-ins/String/fromCodePoint/return-string-value.js": "CRASH",
"built-ins/String/fromCodePoint/to-number-conversions.js": "CRASH",
"built-ins/String/proto-from-ctor-realm.js": "FAIL",
"built-ins/String/prototype/Symbol.iterator/this-val-non-obj-coercible.js": "CRASH",
"built-ins/String/prototype/Symbol.iterator/this-val-to-str-err.js": "CRASH",
Expand Down Expand Up @@ -11363,7 +11356,12 @@
"built-ins/isFinite/tonumber-operations.js": "FAIL",
"built-ins/isNaN/tonumber-operations.js": "FAIL",
"built-ins/parseFloat/S15.1.2.3_A6.js": "TIMEOUT",
"harness/assert-notsamevalue-tostring.js": "CRASH",
"harness/assert-obj.js": "CRASH",
"harness/assert-samevalue-objects.js": "CRASH",
"harness/assert-samevalue-tostring.js": "CRASH",
"harness/assert-throws-same-realm.js": "FAIL",
"harness/assert-tostring.js": "CRASH",
"harness/assertRelativeDateMs.js": "CRASH",
"harness/asyncHelpers-throwsAsync-custom-typeerror.js": "CRASH",
"harness/asyncHelpers-throwsAsync-func-throws-sync.js": "CRASH",
Expand All @@ -11377,11 +11375,15 @@
"harness/asyncHelpers-throwsAsync-same-realm.js": "CRASH",
"harness/asyncHelpers-throwsAsync-single-arg.js": "CRASH",
"harness/deepEqual-array.js": "CRASH",
"harness/deepEqual-deep.js": "CRASH",
"harness/deepEqual-mapset.js": "CRASH",
"harness/deepEqual-object.js": "CRASH",
"harness/deepEqual-primitives-bigint.js": "CRASH",
"harness/deepEqual-primitives.js": "FAIL",
"harness/nativeFunctionMatcher.js": "CRASH",
"harness/testTypedArray-conversions.js": "CRASH",
"harness/verifyProperty-desc-is-not-object.js": "CRASH",
"harness/verifyProperty-undefined-desc.js": "CRASH",
"language/arguments-object/10.6-10-c-ii-1.js": "FAIL",
"language/arguments-object/10.6-10-c-ii-2.js": "FAIL",
"language/arguments-object/10.6-12-1.js": "FAIL",
Expand Down
12 changes: 6 additions & 6 deletions tests/metrics.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"results": {
"crash": 14186,
"fail": 7225,
"pass": 23831,
"skip": 54,
"timeout": 3,
"crash": 14931,
"fail": 7665,
"pass": 24138,
"skip": 55,
"timeout": 12,
"unresolved": 0
},
"total": 45299
"total": 46801
}
1 change: 1 addition & 0 deletions tests/skip.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"built-ins/TypedArray/prototype/copyWithin/coerced-values-end-detached-prototype.js",
"built-ins/TypedArray/prototype/copyWithin/coerced-values-end-detached.js",
"built-ins/TypedArray/prototype/copyWithin/coerced-values-start-detached.js",
"built-ins/RegExp/property-escapes/generated/General_Category_-_Other_Letter.js",
"harness/propertyhelper-verifywritable-array-length.js",
"language/identifiers/part-unicode-15.1.0-escaped.js",
"language/identifiers/part-unicode-15.1.0.js",
Expand Down
2 changes: 1 addition & 1 deletion tests/test262
Submodule test262 updated 2857 files