Skip to content

Commit

Permalink
Changelog: Number() object.
Browse files Browse the repository at this point in the history
Includes some clippy fixes.

Fixes boa-dev#34
  • Loading branch information
pop committed Nov 5, 2019
1 parent 340291d commit 1796146
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Feature enhancements:
Implement Array.prototype.fill() (@bojan88)
- Array tests: Tests implemented for shift, unshift and reverse, pop and push (@muskuloes)
- Demo page has been improved, new font plus change on input. Thanks @WofWca
- [FEATURE #182](https://github.com/jasonwilliams/boa/pull/182):
Implement some Number prototype methods (incl tests) (@pop)

Bug fixes:

Expand Down
16 changes: 9 additions & 7 deletions src/lib/builtins/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ use std::{borrow::Borrow, f64, ops::Deref};
/// Converts a Value to a Number.
fn to_number(value: &Value) -> Value {
match *value.deref().borrow() {
ValueData::Boolean(b) => match b {
true => to_value(1),
false => to_value(0),
},
ValueData::Boolean(b) => {
if b {
to_value(1)
} else {
to_value(0)
}
}
ValueData::Function(_) | ValueData::Undefined => to_value(f64::NAN),
ValueData::Integer(i) => to_value(f64::from(i)),
ValueData::Object(ref o) => (o).deref().borrow().get_internal_slot("NumberData"),
ValueData::Null => to_value(0),
ValueData::Number(n) => to_value(f64::from(n)),
ValueData::Number(n) => to_value(n),
ValueData::String(ref s) => match s.parse::<f64>() {
Ok(n) => to_value(f64::from(n)),
Ok(n) => to_value(n),
Err(_) => to_value(f64::NAN),
},
}
Expand Down Expand Up @@ -191,7 +194,6 @@ mod tests {

forward(&mut engine, init);
let default_zero = forward_val(&mut engine, "default_zero").unwrap();
println!("{:?}", default_zero);
let int_one = forward_val(&mut engine, "int_one").unwrap();
let float_two = forward_val(&mut engine, "float_two").unwrap();
let str_three = forward_val(&mut engine, "str_three").unwrap();
Expand Down
1 change: 0 additions & 1 deletion src/lib/builtins/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,6 @@ fn is_trimmable_whitespace(c: char) -> bool {

pub fn trim(this: &Value, _: &[Value], ctx: &mut Interpreter) -> ResultValue {
let this_str: String = ctx.value_to_rust_string(this);
println!("{:?}", this_str);
Ok(to_value(this_str.trim_matches(is_trimmable_whitespace)))
}

Expand Down
16 changes: 7 additions & 9 deletions src/lib/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,13 @@ impl Interpreter {

/// https://tc39.es/ecma262/#sec-ordinarytoprimitive
fn ordinary_to_primitive(&mut self, o: &Value, hint: &str) -> Value {
println!("{:?}", o.get_type());
debug_assert!(o.get_type() == "object");
println!("{:?}", hint);
debug_assert!(hint == "string" || hint == "number");
let method_names: Vec<&str> = if hint == "string" {
vec!["toString", "valueOf"]
} else {
vec!["valueOf", "toString"]
};
println!("{:?}", method_names);
for name in method_names.iter() {
let method: Value = o.get_field_slice(name);
if method.is_function() {
Expand Down Expand Up @@ -689,17 +686,18 @@ impl Interpreter {
pub fn value_to_rust_number(&mut self, value: &Value) -> f64 {
match *value.deref().borrow() {
ValueData::Null => f64::from(0),
ValueData::Boolean(boolean) => match boolean {
false => f64::from(0),
_ => f64::from(1),
},
ValueData::Boolean(boolean) => {
if boolean {
f64::from(1)
} else {
f64::from(0)
}
}
ValueData::Number(num) => num,
ValueData::Integer(num) => f64::from(num),
ValueData::String(ref string) => string.parse::<f64>().unwrap(),
ValueData::Object(_) => {
println!("It's a object!");
let prim_value = self.to_primitive(value, Some("number"));
println!("{:?}", prim_value);
self.to_string(&prim_value)
.to_string()
.parse::<f64>()
Expand Down

0 comments on commit 1796146

Please sign in to comment.