Skip to content

Commit

Permalink
support multiple arguments in String.prototype.concat
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jul 30, 2024
1 parent fd17acd commit 66a5d77
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crates/dash_vm/src/js_std/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,19 @@ pub fn char_code_at(cx: CallContext) -> Result<Value, Value> {

pub fn concat(cx: CallContext) -> Result<Value, Value> {
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()))
}

Expand Down

0 comments on commit 66a5d77

Please sign in to comment.