Browse Source

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

* feat: Implement Array.prototype.toString

* fix: fix the missing arguments for Array.prototype.toString's inner join

* refactor: use fmt to beautify the code

* refactor: Array.prototype.toString——smplify error formating

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

* fix: fix the error message

* refactor: Array.prototype.toString remove the duplicated logic
pull/238/head
cisen 4 years ago committed by GitHub
parent
commit
d8f33abe06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      src/lib/builtins/array.rs

20
src/lib/builtins/array.rs

@ -205,7 +205,25 @@ 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 mut arguments = vec![to_value(",")];
// 2.
let mut method: Value =
from_value(this.get_field_slice(method_name)).expect("failed to get Array.prototype.join");
// 3.
if !method.is_function() {
method = _ctx
.realm
.global_obj
.get_field_slice("Object")
.get_field_slice(PROTOTYPE)
.get_field_slice("toString");
method = from_value(method).expect("failed to get Object.prototype.toString");
arguments = vec![];
}
// 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(),

Loading…
Cancel
Save