diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-05-26 21:33:37 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-27 13:17:35 +0200 |
commit | dd08c992e8d884f646bdf9e9e75b2236ad5d38c8 (patch) | |
tree | 695ac51fb004e0a41d9374c1f1646dc51dbefeaf /Libraries/LibJS/Runtime/PropertyName.h | |
parent | 59a32f29b05857d7928789f0374930b8f5db4c62 (diff) | |
download | serenity-dd08c992e8d884f646bdf9e9e75b2236ad5d38c8.zip |
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.
Diffstat (limited to 'Libraries/LibJS/Runtime/PropertyName.h')
-rw-r--r-- | Libraries/LibJS/Runtime/PropertyName.h | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/Libraries/LibJS/Runtime/PropertyName.h b/Libraries/LibJS/Runtime/PropertyName.h index 4ab31e7284..eff04666bd 100644 --- a/Libraries/LibJS/Runtime/PropertyName.h +++ b/Libraries/LibJS/Runtime/PropertyName.h @@ -40,14 +40,26 @@ public: PropertyName() {} - explicit PropertyName(i32 index) + PropertyName(i32 index) : m_type(Type::Number) , m_number(index) { - ASSERT(m_number >= 0); + ASSERT(index >= 0); } - explicit PropertyName(const FlyString& string) + PropertyName(const char* chars) + : m_type(Type::String) + , m_string(FlyString(chars)) + { + } + + PropertyName(const String& string) + : m_type(Type::String) + , m_string(FlyString(string)) + { + } + + PropertyName(const FlyString& string) : m_type(Type::String) , m_string(string) { @@ -70,7 +82,7 @@ public: private: Type m_type { Type::Invalid }; FlyString m_string; - i32 m_number { 0 }; + u32 m_number { 0 }; }; } |