2020-04-21 18:46:51 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-21 18:46:51 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
#include <LibJS/Runtime/Uint8ClampedArray.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
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 01:33:37 -03:00
|
|
|
Uint8ClampedArray* Uint8ClampedArray::create(GlobalObject& global_object, u32 length)
|
2020-04-21 18:46:51 -03:00
|
|
|
{
|
2020-09-27 15:07:25 -03:00
|
|
|
return global_object.heap().allocate<Uint8ClampedArray>(global_object, length, *global_object.array_prototype());
|
2020-04-21 18:46:51 -03: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 01:33:37 -03:00
|
|
|
Uint8ClampedArray::Uint8ClampedArray(u32 length, Object& prototype)
|
2020-06-23 12:21:53 -03:00
|
|
|
: Object(prototype)
|
2020-04-21 18:46:51 -03:00
|
|
|
, m_length(length)
|
|
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
auto& vm = this->vm();
|
2021-01-10 20:29:28 -03:00
|
|
|
define_native_property(vm.names.length, length_getter, {});
|
2020-12-01 13:06:48 -03:00
|
|
|
m_data = (u8*)calloc(m_length, 1);
|
2020-04-21 18:46:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uint8ClampedArray::~Uint8ClampedArray()
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(m_data);
|
2020-12-01 13:06:48 -03:00
|
|
|
free(m_data);
|
2020-04-21 18:46:51 -03:00
|
|
|
m_data = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_GETTER(Uint8ClampedArray::length_getter)
|
2020-04-21 18:46:51 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
2020-04-21 18:46:51 -03:00
|
|
|
if (!this_object)
|
|
|
|
|
return {};
|
2020-08-25 07:52:32 -03:00
|
|
|
if (StringView(this_object->class_name()) != "Uint8ClampedArray") {
|
2020-09-27 13:36:49 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Uint8ClampedArray");
|
2020-08-25 07:52:32 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-04-21 18:46:51 -03:00
|
|
|
return Value(static_cast<const Uint8ClampedArray*>(this_object)->length());
|
|
|
|
|
}
|
|
|
|
|
|
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 01:33:37 -03:00
|
|
|
bool Uint8ClampedArray::put_by_index(u32 property_index, Value value)
|
2020-04-21 18:46:51 -03:00
|
|
|
{
|
2020-12-01 13:10:57 -03:00
|
|
|
if (property_index >= m_length)
|
|
|
|
|
return Base::put_by_index(property_index, value);
|
2020-09-27 13:36:49 -03:00
|
|
|
auto number = value.to_i32(global_object());
|
2020-09-21 11:46:45 -03:00
|
|
|
if (vm().exception())
|
2020-05-17 20:28:00 -03:00
|
|
|
return {};
|
|
|
|
|
m_data[property_index] = clamp(number, 0, 255);
|
2020-04-30 16:01:54 -03:00
|
|
|
return true;
|
2020-04-21 18:46:51 -03: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 01:33:37 -03:00
|
|
|
Value Uint8ClampedArray::get_by_index(u32 property_index) const
|
2020-04-21 18:46:51 -03:00
|
|
|
{
|
2020-12-01 13:10:57 -03:00
|
|
|
if (property_index >= m_length)
|
|
|
|
|
return Base::get_by_index(property_index);
|
2020-04-21 18:46:51 -03:00
|
|
|
return Value((i32)m_data[property_index]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|