1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:07:34 +00:00

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&);
};
}