From d8f33abe06e49ad937c0b4cc203eefee7803cb63 Mon Sep 17 00:00:00 2001 From: cisen Date: Fri, 31 Jan 2020 12:55:52 +0800 Subject: [PATCH] fix: Array.prototype.toString should be called by ES value (#227) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- src/lib/builtins/array.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/builtins/array.rs b/src/lib/builtins/array.rs index 516a977627c..b46753e240b 100644 --- a/src/lib/builtins/array.rs +++ b/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. /// 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(),