2020-03-20 19:29:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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>
|
2020-03-20 20:01:36 +00:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-20 19:29:57 +00:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-04-17 16:24:01 +00:00
|
|
|
#include <LibJS/Runtime/ArrayPrototype.h>
|
2020-03-28 23:37:33 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 08:27:57 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-20 19:29:57 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-04-17 16:24:01 +00:00
|
|
|
Array* Array::create(GlobalObject& global_object)
|
2020-03-20 19:29:57 +00:00
|
|
|
{
|
2020-04-17 16:24:01 +00:00
|
|
|
auto& interpreter = global_object.interpreter();
|
2020-06-20 13:40:48 +00:00
|
|
|
return interpreter.heap().allocate<Array>(global_object, *global_object.array_prototype());
|
2020-04-17 16:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array::Array(Object& prototype)
|
2020-04-18 08:27:57 +00:00
|
|
|
: Object(&prototype)
|
2020-04-17 16:24:01 +00:00
|
|
|
{
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-27 04:33:37 +00:00
|
|
|
define_native_property("length", length_getter, length_setter, Attribute::Writable);
|
2020-03-20 19:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array::~Array()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-20 14:25:12 +00:00
|
|
|
Array* Array::typed_this(Interpreter& interpreter, GlobalObject& global_object)
|
2020-03-28 23:37:33 +00:00
|
|
|
{
|
2020-06-20 14:40:30 +00:00
|
|
|
auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
|
2020-03-28 23:37:33 +00:00
|
|
|
if (!this_object)
|
|
|
|
return {};
|
2020-04-22 23:33:13 +00:00
|
|
|
if (!this_object->is_array()) {
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::NotAn, "Array");
|
2020-04-22 23:33:13 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<Array*>(this_object);
|
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_GETTER(Array::length_getter)
|
2020-04-22 23:33:13 +00:00
|
|
|
{
|
2020-06-20 14:25:12 +00:00
|
|
|
auto* array = typed_this(interpreter, global_object);
|
2020-04-22 23:33:13 +00:00
|
|
|
if (!array)
|
|
|
|
return {};
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 18:35:09 +00:00
|
|
|
return Value(static_cast<i32>(array->indexed_properties().array_like_size()));
|
2020-03-28 23:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_SETTER(Array::length_setter)
|
2020-03-28 23:37:33 +00:00
|
|
|
{
|
2020-06-20 14:25:12 +00:00
|
|
|
auto* array = typed_this(interpreter, global_object);
|
2020-04-22 23:33:13 +00:00
|
|
|
if (!array)
|
|
|
|
return;
|
2020-05-17 23:28:00 +00:00
|
|
|
auto length = value.to_number(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return;
|
2020-04-22 23:33:13 +00:00
|
|
|
if (length.is_nan() || length.is_infinity() || length.as_double() < 0) {
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<RangeError>(ErrorType::ArrayInvalidLength);
|
2020-04-22 23:33:13 +00:00
|
|
|
return;
|
|
|
|
}
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 18:35:09 +00:00
|
|
|
array->indexed_properties().set_array_like_size(length.as_double());
|
2020-03-28 23:37:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
}
|