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 #34
  • Loading branch information
pop committed Oct 22, 2019
1 parent 46d32ba commit 6d82f34
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 83 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
29 changes: 29 additions & 0 deletions src/lib/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,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 @@ -646,6 +649,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

0 comments on commit 6d82f34

Please sign in to comment.