LibJS: Add Array.prototype.concat

This commit is contained in:
Kesse Jones 2020-04-17 08:51:45 -03:00 committed by Andreas Kling
parent 63e1ea7819
commit 4931c0feda
3 changed files with 61 additions and 0 deletions

View file

@ -48,6 +48,7 @@ ArrayPrototype::ArrayPrototype()
put_native_function("toString", to_string, 0);
put_native_function("unshift", unshift, 1);
put_native_function("join", join, 1);
put_native_function("concat", concat, 1);
put("length", Value(0));
}
@ -229,4 +230,26 @@ Value ArrayPrototype::join(Interpreter& interpreter)
return join_array_with_separator(interpreter, *array, separator);
}
Value ArrayPrototype::concat(Interpreter& interpreter)
{
auto* array = array_from(interpreter);
if (!array)
return {};
auto* new_array = interpreter.heap().allocate<Array>();
new_array->elements().append(array->elements());
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
auto argument = interpreter.argument(i);
if (argument.is_array()) {
auto& argument_object = argument.as_object();
new_array->elements().append(argument_object.elements());
} else {
new_array->elements().append(argument);
}
}
return Value(new_array);
}
}

View file

@ -48,6 +48,7 @@ private:
static Value to_string(Interpreter&);
static Value unshift(Interpreter&);
static Value join(Interpreter&);
static Value concat(Interpreter&);
};
}

View file

@ -0,0 +1,37 @@
load("test-common.js");
try {
assert(Array.prototype.concat.length === 1);
var array = ["hello", "friends"];
var array_concat = array.concat();
assert(array_concat.length === array.length);
array_concat = array.concat(1)
assert(array_concat.length === 3);
assert(array_concat[2] === 1);
array_concat = array.concat([1, 2, 3])
assert(array_concat.length === 5);
assert(array_concat[2] === 1);
assert(array_concat[3] === 2);
assert(array_concat[4] === 3);
array_concat = array.concat(false, "serenity");
assert(array_concat.length === 4);
assert(array_concat[2] === false);
assert(array_concat[3] === "serenity");
array_concat = array.concat({ name: "libjs" }, [1, [2, 3]]);
assert(array_concat.length === 5);
assert(array_concat[2].name === "libjs");
assert(array_concat[3] === 1);
assert(array_concat[4][0] === 2);
assert(array_concat[4][1] === 3);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}