mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 18:38:12 +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
|
@ -1282,20 +1282,20 @@ Value ObjectExpression::execute(Interpreter& interpreter) const
|
|||
for (size_t i = 0; i < elements.size(); ++i) {
|
||||
auto element = elements.at(i);
|
||||
if (!element.is_empty())
|
||||
object->put_by_index(i, element);
|
||||
object->define_property(i, element);
|
||||
}
|
||||
} else if (key_result.is_object()) {
|
||||
auto& obj_to_spread = key_result.as_object();
|
||||
|
||||
for (auto& it : obj_to_spread.shape().property_table_ordered()) {
|
||||
if (it.value.attributes & Attribute::Enumerable)
|
||||
object->put(it.key, obj_to_spread.get(it.key));
|
||||
object->define_property(it.key, obj_to_spread.get(it.key));
|
||||
}
|
||||
} else if (key_result.is_string()) {
|
||||
auto& str_to_spread = key_result.as_string().string();
|
||||
|
||||
for (size_t i = 0; i < str_to_spread.length(); i++) {
|
||||
object->put_by_index(i, js_string(interpreter, str_to_spread.substring(i, 1)));
|
||||
object->define_property(i, js_string(interpreter, str_to_spread.substring(i, 1)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1329,14 +1329,14 @@ Value ObjectExpression::execute(Interpreter& interpreter) const
|
|||
}
|
||||
if (!accessor) {
|
||||
accessor = Accessor::create(interpreter, nullptr, nullptr);
|
||||
object->put_own_property(*object, key, Attribute::Configurable | Attribute::Enumerable, accessor, Object::PutOwnPropertyMode::DefineProperty);
|
||||
object->define_property(key, accessor, Attribute::Configurable | Attribute::Enumerable);
|
||||
}
|
||||
if (property.type() == ObjectProperty::Type::Getter)
|
||||
accessor->set_getter(&value.as_function());
|
||||
else
|
||||
accessor->set_setter(&value.as_function());
|
||||
} else {
|
||||
object->put(key, value);
|
||||
object->define_property(key, value);
|
||||
}
|
||||
}
|
||||
return object;
|
||||
|
@ -1354,7 +1354,7 @@ PropertyName MemberExpression::computed_property_name(Interpreter& interpreter)
|
|||
{
|
||||
if (!is_computed()) {
|
||||
ASSERT(m_property->is_identifier());
|
||||
return PropertyName(static_cast<const Identifier&>(*m_property).string());
|
||||
return static_cast<const Identifier&>(*m_property).string();
|
||||
}
|
||||
auto index = m_property->execute(interpreter);
|
||||
if (interpreter.exception())
|
||||
|
@ -1363,12 +1363,12 @@ PropertyName MemberExpression::computed_property_name(Interpreter& interpreter)
|
|||
ASSERT(!index.is_empty());
|
||||
|
||||
if (index.is_integer() && index.as_i32() >= 0)
|
||||
return PropertyName(index.as_i32());
|
||||
return index.as_i32();
|
||||
|
||||
auto index_string = index.to_string(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
return PropertyName(index_string);
|
||||
return index_string;
|
||||
}
|
||||
|
||||
String MemberExpression::to_string_approximation() const
|
||||
|
@ -1537,7 +1537,7 @@ Value TaggedTemplateLiteral::execute(Interpreter& interpreter) const
|
|||
return {};
|
||||
raw_strings->elements().append(value);
|
||||
}
|
||||
strings->put("raw", raw_strings, 0);
|
||||
strings->define_property("raw", raw_strings, 0);
|
||||
|
||||
return interpreter.call(tag_function, js_undefined(), move(arguments));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue