Skip to content

Commit

Permalink
Feature: Most of Number() is complete
Browse files Browse the repository at this point in the history
Includes:
- make_number()
- call_number()
- Number().toExponential()
- Number().toFixed()
- Number().toLocaleString() (ish)
- Number().toString()
- Number().valueOf()
- create_constructor()
- init()

Missing:
- Number().toPrecision()

refs boa-dev#34
  • Loading branch information
pop committed Oct 27, 2019
1 parent f2d4ff2 commit c64712e
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ yarn-error.log
.vscode/settings.json

# tests/js/test.js is used for testing changes locally
test/js/test.js
tests/js/test.js
1 change: 1 addition & 0 deletions src/lib/builtins/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ 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
29 changes: 29 additions & 0 deletions src/lib/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,16 @@ 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 @@ -690,6 +693,32 @@ impl Interpreter {
_ => String::from("undefined"),
}
}

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::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>()
.unwrap()
}
_ => {
// TODO: Make undefined?
f64::from(0)
}
}
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit c64712e

Please sign in to comment.