From 7725b1970e5b7a9ee1e23331f3e19989f28fd03a Mon Sep 17 00:00:00 2001 From: Kesse Jones Date: Thu, 16 Apr 2020 11:35:13 -0300 Subject: [PATCH] LibJS: Add String.prototype.concat --- Libraries/LibJS/Runtime/StringPrototype.cpp | 19 ++++++++++++++++ Libraries/LibJS/Runtime/StringPrototype.h | 1 + .../LibJS/Tests/String.prototype.concat.js | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 Libraries/LibJS/Tests/String.prototype.concat.js diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 453e10230d..560cbb3084 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -55,6 +55,7 @@ StringPrototype::StringPrototype() put_native_function("trim", trim, 0); put_native_function("trimStart", trim_start, 0); put_native_function("trimEnd", trim_end, 0); + put_native_function("concat", concat, 1); } StringPrototype::~StringPrototype() @@ -307,4 +308,22 @@ Value StringPrototype::trim_end(Interpreter& interpreter) return trim_string(interpreter, *this_object, TrimMode::Right); } +Value StringPrototype::concat(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + auto& string = this_object->to_string().as_string()->string(); + + StringBuilder builder; + builder.append(string); + + for (size_t i = 0; i < interpreter.argument_count(); ++i) { + auto string_argument = interpreter.argument(i).to_string(); + builder.append(string_argument); + } + + return js_string(interpreter, builder.to_string()); +} + } diff --git a/Libraries/LibJS/Runtime/StringPrototype.h b/Libraries/LibJS/Runtime/StringPrototype.h index f9e5b12b43..76f5a0c70c 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Libraries/LibJS/Runtime/StringPrototype.h @@ -53,6 +53,7 @@ private: static Value trim(Interpreter&); static Value trim_start(Interpreter&); static Value trim_end(Interpreter&); + static Value concat(Interpreter&); }; } diff --git a/Libraries/LibJS/Tests/String.prototype.concat.js b/Libraries/LibJS/Tests/String.prototype.concat.js new file mode 100644 index 0000000000..aee254ba71 --- /dev/null +++ b/Libraries/LibJS/Tests/String.prototype.concat.js @@ -0,0 +1,22 @@ +load("test-common.js"); + +try { + assert(String.prototype.concat.length === 1); + assert("".concat(1) === "1"); + assert("".concat(3,2,1) === "321"); + assert("hello".concat(" ", "friends") === "hello friends"); + assert("".concat(null) === "null"); + assert("".concat(false) === "false"); + assert("".concat(true) === "true"); + assert("".concat([]) === ""); + assert("".concat([1, 2, 3, 'hello']) === "1,2,3,hello"); + assert("".concat(true, []) === "true"); + assert("".concat(true, false) === "truefalse"); + assert("".concat({}) === "[object Object]"); + assert("".concat(1, {}) === "1[object Object]"); + assert("".concat(1, {}, false) === "1[object Object]false"); + + console.log("PASS"); +} catch (err) { + console.log("FAIL: " + err); +}