Skip to content

Commit

Permalink
Implement Number.parseInt and Number.parseFloat
Browse files Browse the repository at this point in the history
- Fix length of `AggregateError()`
- Fix length of `Reflect.setPrototypeOf()`
  • Loading branch information
HalidOdat committed Mar 4, 2022
1 parent 3ec6f63 commit 135d4f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/error/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
38 changes: 31 additions & 7 deletions boa_engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand All @@ -65,20 +78,31 @@ 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)
.static_property("parseFloat", parse_float.clone(), attribute)
.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);

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/reflect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 135d4f7

Please sign in to comment.