Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Array.prototype.toString should be called by ES value #227

Merged
merged 12 commits into from
Jan 31, 2020
29 changes: 28 additions & 1 deletion src/lib/builtins/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,34 @@ pub fn join(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
/// other kinds of objects for use as a method.
/// <https://tc39.es/ecma262/#sec-array.prototype.tostring>
pub fn to_string(this: &Value, _args: &[Value], _ctx: &mut Interpreter) -> ResultValue {
let join_result = join(this, &[to_value(",")], _ctx);
let method_name = "join";
let arguments = vec![to_value(",")];
// 2.
let method: Value =
from_value(this.get_field_slice(method_name)).expect("failed to get Array.prototype.join");
// 3.
if !method.is_function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better if you changed the value of method and remove the duplicated logic from the if statement?

let mut object_to_string = _ctx
.realm
.global_obj
.get_field_slice("Object")
.get_field_slice(PROTOTYPE)
.get_field_slice("toString");

object_to_string =
from_value(object_to_string).expect("failed to get Object.prototype.toString");
let fun_result = _ctx.call(&object_to_string, this, vec![]);
let match_string = match fun_result {
Ok(v) => match *v {
ValueData::String(ref s) => (*s).clone(),
_ => "".to_string(),
},
Err(v) => format!("error: {}", v),
};
return Ok(to_value(match_string));
}
// 4.
let join_result = _ctx.call(&method, this, arguments);
let match_string = match join_result {
Ok(v) => match *v {
ValueData::String(ref s) => (*s).clone(),
Expand Down