Browse Source

Implement `Number.parseInt` and `Number.parseFloat` (#1894)

This PR add `Number.parseInt` and `Number.parseFloat` which according to spec are clones of the global objects `parseInt` and `parseFloat`.

It also fixes the last failing test of  the `NativeError` feature with this we get 100% spec complaint `NativeError`s 🎉 

It changes the following:
- Add `Number.parseInt()`
- Add `Number.parseFloat()`
- Fix length of `AggregateError`
- Fix length of `Reflect.setPrototypeOf`
pull/1901/head
Halid Odat 3 years ago
parent
commit
9c2b1114c4
  1. 2
      boa_engine/src/builtins/error/aggregate.rs
  2. 46
      boa_engine/src/builtins/number/mod.rs
  3. 2
      boa_engine/src/builtins/reflect/mod.rs

2
boa_engine/src/builtins/error/aggregate.rs

@ -56,7 +56,7 @@ impl BuiltIn for AggregateError {
impl AggregateError {
/// The amount of arguments this function object takes.
pub(crate) const LENGTH: usize = 1;
pub(crate) const LENGTH: usize = 2;
/// Create a new aggregate error object.
pub(crate) fn constructor(

46
boa_engine/src/builtins/number/mod.rs

@ -17,7 +17,8 @@ use crate::{
builtins::{string::is_trimmable_whitespace, BuiltIn, JsArgs},
context::StandardObjects,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, JsObject, ObjectData,
internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder,
JsObject, ObjectData,
},
property::Attribute,
value::{AbstractRelation, IntegerOrInfinity, JsValue},
@ -49,6 +50,18 @@ impl BuiltIn for Number {
fn init(context: &mut Context) -> JsValue {
let _timer = Profiler::global().start_event(Self::NAME, "init");
let parse_int = FunctionBuilder::native(context, Self::parse_int)
.name("parseInt")
.length(2)
.constructor(false)
.build();
let parse_float = FunctionBuilder::native(context, Self::parse_float)
.name("parseFloat")
.length(1)
.constructor(false)
.build();
let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT;
let number_object = ConstructorBuilder::with_standard_object(
context,
@ -65,20 +78,39 @@ impl BuiltIn for Number {
.static_property("NEGATIVE_INFINITY", f64::NEG_INFINITY, attribute)
.static_property("POSITIVE_INFINITY", f64::INFINITY, attribute)
.static_property("NaN", f64::NAN, attribute)
.static_property(
"parseInt",
parse_int.clone(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.static_property(
"parseFloat",
parse_float.clone(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.static_method(Self::number_is_finite, "isFinite", 1)
.static_method(Self::number_is_nan, "isNaN", 1)
.static_method(Self::is_safe_integer, "isSafeInteger", 1)
.static_method(Self::number_is_integer, "isInteger", 1)
.method(Self::to_exponential, "toExponential", 1)
.method(Self::to_fixed, "toFixed", 1)
.method(Self::to_locale_string, "toLocaleString", 0)
.method(Self::to_precision, "toPrecision", 1)
.method(Self::to_string, "toString", 1)
.method(Self::value_of, "valueOf", 0)
.static_method(Self::number_is_finite, "isFinite", 1)
.static_method(Self::number_is_nan, "isNaN", 1)
.static_method(Self::is_safe_integer, "isSafeInteger", 1)
.static_method(Self::number_is_integer, "isInteger", 1)
.build();
context.register_global_builtin_function("parseInt", 2, Self::parse_int);
context.register_global_builtin_function("parseFloat", 1, Self::parse_float);
context.register_global_property(
"parseInt",
parse_int,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
);
context.register_global_property(
"parseFloat",
parse_float,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
);
context.register_global_builtin_function("isFinite", 1, Self::global_is_finite);
context.register_global_builtin_function("isNaN", 1, Self::global_is_nan);

2
boa_engine/src/builtins/reflect/mod.rs

@ -56,7 +56,7 @@ impl BuiltIn for Reflect {
.function(Self::own_keys, "ownKeys", 1)
.function(Self::prevent_extensions, "preventExtensions", 1)
.function(Self::set, "set", 3)
.function(Self::set_prototype_of, "setPrototypeOf", 3)
.function(Self::set_prototype_of, "setPrototypeOf", 2)
.property(
to_string_tag,
Self::NAME,

Loading…
Cancel
Save