mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:18:11 +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.
This commit is contained in:
parent
59a32f29b0
commit
dd08c992e8
45 changed files with 501 additions and 417 deletions
|
@ -32,19 +32,17 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
Uint8ClampedArray* Uint8ClampedArray::create(GlobalObject& global_object, i32 length)
|
||||
Uint8ClampedArray* Uint8ClampedArray::create(GlobalObject& global_object, u32 length)
|
||||
{
|
||||
ASSERT(length >= 0);
|
||||
auto& interpreter = global_object.interpreter();
|
||||
return interpreter.heap().allocate<Uint8ClampedArray>(length, *global_object.array_prototype());
|
||||
}
|
||||
|
||||
Uint8ClampedArray::Uint8ClampedArray(i32 length, Object& prototype)
|
||||
Uint8ClampedArray::Uint8ClampedArray(u32 length, Object& prototype)
|
||||
: Object(&prototype)
|
||||
, m_length(length)
|
||||
{
|
||||
ASSERT(m_length >= 0);
|
||||
put_native_property("length", length_getter, nullptr);
|
||||
define_native_property("length", length_getter, nullptr);
|
||||
m_data = new u8[m_length];
|
||||
}
|
||||
|
||||
|
@ -65,10 +63,9 @@ Value Uint8ClampedArray::length_getter(Interpreter& interpreter)
|
|||
return Value(static_cast<const Uint8ClampedArray*>(this_object)->length());
|
||||
}
|
||||
|
||||
bool Uint8ClampedArray::put_by_index(i32 property_index, Value value, u8)
|
||||
bool Uint8ClampedArray::put_by_index(u32 property_index, Value value)
|
||||
{
|
||||
// FIXME: Use attributes
|
||||
ASSERT(property_index >= 0);
|
||||
ASSERT(property_index < m_length);
|
||||
auto number = value.to_i32(interpreter());
|
||||
if (interpreter().exception())
|
||||
|
@ -77,9 +74,8 @@ bool Uint8ClampedArray::put_by_index(i32 property_index, Value value, u8)
|
|||
return true;
|
||||
}
|
||||
|
||||
Value Uint8ClampedArray::get_by_index(i32 property_index) const
|
||||
Value Uint8ClampedArray::get_by_index(u32 property_index) const
|
||||
{
|
||||
ASSERT(property_index >= 0);
|
||||
ASSERT(property_index < m_length);
|
||||
return Value((i32)m_data[property_index]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue