From 66a5d77663e23af3e0251e3c0661f5a0264bff6d Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Tue, 30 Jul 2024 02:03:28 +0200 Subject: [PATCH] support multiple arguments in `String.prototype.concat` --- crates/dash_vm/src/js_std/string.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/dash_vm/src/js_std/string.rs b/crates/dash_vm/src/js_std/string.rs index 49cd3dfe..6b5c97c6 100644 --- a/crates/dash_vm/src/js_std/string.rs +++ b/crates/dash_vm/src/js_std/string.rs @@ -121,8 +121,19 @@ pub fn char_code_at(cx: CallContext) -> Result { pub fn concat(cx: CallContext) -> Result { let this = cx.this.to_js_string(cx.scope)?; - let other = cx.args.first().unwrap_or_undefined().to_js_string(cx.scope)?; - let concat = String::from(this.res(cx.scope)) + other.res(cx.scope); + + let concat = if cx.args.len() > 1 { + // avoid interning every concatenation + let mut concat = String::from(this.res(cx.scope)); + for value in &cx.args { + concat += value.to_js_string(cx.scope)?.res(cx.scope); + } + concat + } else { + let other = cx.args.first().unwrap_or_undefined().to_js_string(cx.scope)?; + String::from(this.res(cx.scope)) + other.res(cx.scope) + }; + Ok(Value::String(cx.scope.intern(concat.as_ref()).into())) }