2020-04-04 20:28:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-05-08 15:23:56 +00:00
|
|
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
2020-04-04 20:28:21 +00:00
|
|
|
* 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 <LibJS/Heap/Heap.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
|
|
|
#include <LibJS/Runtime/ArrayConstructor.h>
|
2020-05-08 15:23:56 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 11:18:06 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-04 20:28:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-20 13:40:48 +00:00
|
|
|
ArrayConstructor::ArrayConstructor(GlobalObject& global_object)
|
|
|
|
: NativeFunction("Array", *global_object.function_prototype())
|
2020-04-04 20:28:21 +00:00
|
|
|
{
|
2020-06-20 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ArrayConstructor::~ArrayConstructor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayConstructor::initialize(Interpreter& interpreter, GlobalObject& global_object)
|
|
|
|
{
|
|
|
|
NativeFunction::initialize(interpreter, global_object);
|
|
|
|
|
|
|
|
define_property("prototype", global_object.array_prototype(), 0);
|
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_property("length", Value(1), Attribute::Configurable);
|
2020-05-08 15:28:35 +00:00
|
|
|
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
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_function("isArray", is_array, 1, attr);
|
|
|
|
define_native_function("of", of, 0, attr);
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayConstructor::call(Interpreter& interpreter)
|
|
|
|
{
|
2020-04-17 16:24:01 +00:00
|
|
|
if (interpreter.argument_count() <= 0)
|
2020-06-20 10:47:19 +00:00
|
|
|
return Array::create(global_object());
|
2020-04-04 20:28:21 +00:00
|
|
|
|
2020-05-08 15:23:56 +00:00
|
|
|
if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
|
|
|
|
auto array_length_value = interpreter.argument(0);
|
2020-05-18 07:59:35 +00:00
|
|
|
if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::ArrayInvalidLength);
|
2020-05-08 15:23:56 +00:00
|
|
|
return {};
|
|
|
|
}
|
2020-06-20 10:47:19 +00:00
|
|
|
auto* array = Array::create(global_object());
|
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(array_length_value.as_i32());
|
2020-04-04 20:28:21 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:47:19 +00:00
|
|
|
auto* array = Array::create(global_object());
|
2020-05-08 15:23:56 +00:00
|
|
|
for (size_t i = 0; i < interpreter.argument_count(); ++i)
|
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().append(interpreter.argument(i));
|
2020-05-08 15:23:56 +00:00
|
|
|
return array;
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value ArrayConstructor::construct(Interpreter& interpreter)
|
|
|
|
{
|
|
|
|
return call(interpreter);
|
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
|
2020-05-08 15:28:35 +00:00
|
|
|
{
|
|
|
|
auto value = interpreter.argument(0);
|
2020-06-10 18:01:00 +00:00
|
|
|
return Value(value.is_array());
|
2020-05-08 15:28:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 11:55:34 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
|
2020-05-08 15:32:56 +00:00
|
|
|
{
|
2020-06-20 11:55:34 +00:00
|
|
|
auto* array = Array::create(global_object);
|
2020-05-08 15:32:56 +00:00
|
|
|
for (size_t i = 0; i < interpreter.argument_count(); ++i)
|
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().append(interpreter.argument(i));
|
2020-05-08 15:32:56 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2020-04-04 20:28:21 +00:00
|
|
|
}
|