Skip to content

Commit

Permalink
chore(ecmascript): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yossydev committed Jan 30, 2025
1 parent 87500da commit 8e63517
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// 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::abstract_operations::type_conversion::to_uint32_number;
use crate::ecmascript::builders::builtin_function_builder::BuiltinFunctionBuilder;
use crate::ecmascript::builtins::ordinary::get_prototype_from_constructor;
use crate::ecmascript::builtins::ordinary::ordinary_object_create_with_intrinsics;
Expand Down Expand Up @@ -173,24 +170,27 @@ impl StringConstructor {
agent: &mut Agent,
_this_value: Value,
code_points: ArgumentsList,
mut gc: GcScope,
gc: GcScope,
) -> JsResult<Value> {
let gc = gc.nogc();
// 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).
let next_cp = to_number(agent, code_points[0].into_value(), gc.reborrow())?.unbind();
// b. If IsIntegralNumber(nextCP) is false, throw a RangeError exception.
// c. If ℝ(nextCP) < 0 or ℝ(nextCP) > 0x10FFFF, throw a RangeError exception.
let next_cp = next_cp.into_i64(agent);
if next_cp > 0x10FFFF {
let Value::Integer(next_cp) = code_points.first().unwrap() else {
unreachable!();
};
let next_cp = next_cp.into_i64();
if next_cp < 0 || next_cp > 0x10FFFF {
return Err(agent.throw_exception(
ExceptionType::RangeError,
format!("{:?} is not a valid code point", next_cp),
gc.nogc(),
gc,
));
}
// d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
Expand All @@ -204,29 +204,24 @@ impl StringConstructor {
// 2. For each element next of codePoints, do
for next in code_points.iter() {
// a. Let nextCP be ? ToNumber(next).
let next_cp = to_number(agent, next.into_value(), 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 = to_uint32_number(agent, next_cp);
if next_cp > 0x10FFFF {
let Value::Integer(next_cp) = next else {
unreachable!();
};
let next_cp = next_cp.into_i64();
if next_cp < 0 || next_cp > 0x10FFFF {
return Err(agent.throw_exception(
ExceptionType::RangeError,
format!("{:?} is not a valid code point", next_cp),
gc.nogc(),
gc,
));
}
// d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
result.push(char::from_u32(next_cp).unwrap());
result.push(char::from_u32(next_cp as u32).unwrap());
}
// 4. Return result.
Ok(String::from_string(agent, result, gc.nogc()).into())
Ok(String::from_string(agent, result, gc).into())
}

fn raw(
Expand Down
5 changes: 5 additions & 0 deletions tests/expectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -5195,6 +5195,11 @@
"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/number-is-out-of-range.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
6 changes: 3 additions & 3 deletions tests/metrics.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"results": {
"crash": 14944,
"fail": 7663,
"pass": 24127,
"crash": 14950,
"fail": 7662,
"pass": 24122,
"skip": 55,
"timeout": 12,
"unresolved": 0
Expand Down

0 comments on commit 8e63517

Please sign in to comment.