123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /*
- * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <AK/Function.h>
- #include <AK/StringBuilder.h>
- #include <LibJS/Heap/Heap.h>
- #include <LibJS/Interpreter.h>
- #include <LibJS/Runtime/Array.h>
- #include <LibJS/Runtime/ArrayPrototype.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/Function.h>
- #include <LibJS/Runtime/GlobalObject.h>
- #include <LibJS/Runtime/MarkedValueList.h>
- #include <LibJS/Runtime/Value.h>
- namespace JS {
- ArrayPrototype::ArrayPrototype()
- : Object(interpreter().global_object().object_prototype())
- {
- put_native_function("filter", filter, 1);
- put_native_function("forEach", for_each, 1);
- put_native_function("map", map, 1);
- put_native_function("pop", pop, 0);
- put_native_function("push", push, 1);
- put_native_function("shift", shift, 0);
- 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_native_function("slice", slice, 2);
- put("length", Value(0));
- }
- ArrayPrototype::~ArrayPrototype()
- {
- }
- static Array* array_from(Interpreter& interpreter)
- {
- auto* this_object = interpreter.this_value().to_object(interpreter.heap());
- if (!this_object)
- return {};
- if (!this_object->is_array()) {
- interpreter.throw_exception<TypeError>("Not an Array");
- return nullptr;
- }
- return static_cast<Array*>(this_object);
- }
- static Function* callback_from_args(Interpreter& interpreter, const String& name)
- {
- if (interpreter.argument_count() < 1) {
- interpreter.throw_exception<TypeError>(String::format("Array.prototype.%s() requires at least one argument", name.characters()));
- return nullptr;
- }
- auto callback = interpreter.argument(0);
- if (!callback.is_object() || !callback.as_object().is_function()) {
- interpreter.throw_exception<TypeError>(String::format("%s is not a function", callback.to_string().characters()));
- return nullptr;
- }
- return static_cast<Function*>(&callback.as_object());
- }
- Value ArrayPrototype::filter(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- auto* callback = callback_from_args(interpreter, "filter");
- if (!callback)
- return {};
- auto this_value = interpreter.argument(1);
- auto initial_array_size = array->elements().size();
- auto* new_array = Array::create(interpreter.global_object());
- for (size_t i = 0; i < initial_array_size; ++i) {
- if (i >= array->elements().size())
- break;
- auto value = array->elements()[i];
- if (value.is_empty())
- continue;
- MarkedValueList arguments(interpreter.heap());
- arguments.append(value);
- arguments.append(Value((i32)i));
- arguments.append(array);
- auto result = interpreter.call(callback, this_value, move(arguments));
- if (interpreter.exception())
- return {};
- if (result.to_boolean())
- new_array->elements().append(value);
- }
- return Value(new_array);
- }
- Value ArrayPrototype::for_each(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- auto* callback = callback_from_args(interpreter, "forEach");
- if (!callback)
- return {};
- auto this_value = interpreter.argument(1);
- auto initial_array_size = array->elements().size();
- for (size_t i = 0; i < initial_array_size; ++i) {
- if (i >= array->elements().size())
- break;
- auto value = array->elements()[i];
- if (value.is_empty())
- continue;
- MarkedValueList arguments(interpreter.heap());
- arguments.append(value);
- arguments.append(Value((i32)i));
- arguments.append(array);
- interpreter.call(callback, this_value, move(arguments));
- if (interpreter.exception())
- return {};
- }
- return js_undefined();
- }
- Value ArrayPrototype::map(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- auto* callback = callback_from_args(interpreter, "map");
- if (!callback)
- return {};
- auto this_value = interpreter.argument(1);
- auto initial_array_size = array->elements().size();
- auto* new_array = Array::create(interpreter.global_object());
- for (size_t i = 0; i < initial_array_size; ++i) {
- if (i >= array->elements().size())
- break;
- auto value = array->elements()[i];
- if (value.is_empty())
- continue;
- MarkedValueList arguments(interpreter.heap());
- arguments.append(value);
- arguments.append(Value((i32)i));
- arguments.append(array);
- auto result = interpreter.call(callback, this_value, move(arguments));
- if (interpreter.exception())
- return {};
- new_array->elements().append(result);
- }
- return Value(new_array);
- }
- Value ArrayPrototype::push(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- for (size_t i = 0; i < interpreter.argument_count(); ++i)
- array->elements().append(interpreter.argument(i));
- return Value(array->length());
- }
- Value ArrayPrototype::unshift(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- for (size_t i = 0; i < interpreter.argument_count(); ++i)
- array->elements().insert(i, interpreter.argument(i));
- return Value(array->length());
- }
- Value ArrayPrototype::pop(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- if (array->elements().is_empty())
- return js_undefined();
- return array->elements().take_last();
- }
- Value ArrayPrototype::shift(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- if (array->elements().is_empty())
- return js_undefined();
- return array->elements().take_first();
- }
- static Value join_array_with_separator(Interpreter& interpreter, const Array& array, StringView separator)
- {
- StringBuilder builder;
- for (size_t i = 0; i < array.elements().size(); ++i) {
- if (i != 0)
- builder.append(separator);
- if (!array.elements()[i].is_empty())
- builder.append(array.elements()[i].to_string());
- }
- return js_string(interpreter, builder.to_string());
- }
- Value ArrayPrototype::to_string(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- return join_array_with_separator(interpreter, *array, ",");
- }
- Value ArrayPrototype::join(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- String separator = ",";
- if (interpreter.argument_count() == 1)
- separator = interpreter.argument(0).to_string();
- return join_array_with_separator(interpreter, *array, separator);
- }
- Value ArrayPrototype::concat(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- auto* new_array = Array::create(interpreter.global_object());
- 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);
- }
- Value ArrayPrototype::slice(Interpreter& interpreter)
- {
- auto* array = array_from(interpreter);
- if (!array)
- return {};
- auto* new_array = Array::create(interpreter.global_object());
- if (interpreter.argument_count() == 0) {
- new_array->elements().append(array->elements());
- return new_array;
- }
- ssize_t array_size = static_cast<ssize_t>(array->elements().size());
- auto start_slice = interpreter.argument(0).to_i32();
- auto end_slice = array_size;
- if (start_slice > array_size)
- return new_array;
- if (start_slice < 0)
- start_slice = end_slice + start_slice;
- if (interpreter.argument_count() >= 2) {
- end_slice = interpreter.argument(1).to_i32();
- if (end_slice < 0)
- end_slice = array_size + end_slice;
- else if (end_slice > array_size)
- end_slice = array_size;
- }
- size_t array_capacity = start_slice + array_size - end_slice;
- new_array->elements().ensure_capacity(array_capacity);
- for (ssize_t i = start_slice; i < end_slice; ++i) {
- new_array->elements().append(array->elements().at(i));
- }
- return new_array;
- }
- }
|