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 | |
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')
32 files changed, 421 insertions, 337 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 6dffc8fd21..7f8944c18a 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -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)); } diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index b9c6749043..38d78cb161 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -42,7 +42,7 @@ Array* Array::create(GlobalObject& global_object) Array::Array(Object& prototype) : Object(&prototype) { - put_native_property("length", length_getter, length_setter, Attribute::Writable); + define_native_property("length", length_getter, length_setter, Attribute::Writable); } Array::~Array() diff --git a/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Libraries/LibJS/Runtime/ArrayConstructor.cpp index d1423009ff..3af09146b3 100644 --- a/Libraries/LibJS/Runtime/ArrayConstructor.cpp +++ b/Libraries/LibJS/Runtime/ArrayConstructor.cpp @@ -39,12 +39,12 @@ namespace JS { ArrayConstructor::ArrayConstructor() : NativeFunction("Array", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().array_prototype(), 0); - put("length", Value(1), Attribute::Configurable); + define_property("prototype", interpreter().global_object().array_prototype(), 0); + define_property("length", Value(1), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("isArray", is_array, 1, attr); - put_native_function("of", of, 0, attr); + define_native_function("isArray", is_array, 1, attr); + define_native_function("of", of, 0, attr); } ArrayConstructor::~ArrayConstructor() diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index b6c372ccab..3d269071fc 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -46,30 +46,30 @@ ArrayPrototype::ArrayPrototype() { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("filter", filter, 1, attr); - put_native_function("forEach", for_each, 1, attr); - put_native_function("map", map, 1, attr); - put_native_function("pop", pop, 0, attr); - put_native_function("push", push, 1, attr); - put_native_function("shift", shift, 0, attr); - put_native_function("toString", to_string, 0, attr); - put_native_function("unshift", unshift, 1, attr); - put_native_function("join", join, 1, attr); - put_native_function("concat", concat, 1, attr); - put_native_function("slice", slice, 2, attr); - put_native_function("indexOf", index_of, 1, attr); - put_native_function("reduce", reduce, 1, attr); - put_native_function("reduceRight", reduce_right, 1, attr); - put_native_function("reverse", reverse, 0, attr); - put_native_function("lastIndexOf", last_index_of, 1, attr); - put_native_function("includes", includes, 1, attr); - put_native_function("find", find, 1, attr); - put_native_function("findIndex", find_index, 1, attr); - put_native_function("some", some, 1, attr); - put_native_function("every", every, 1, attr); - put_native_function("splice", splice, 2, attr); - put_native_function("fill", fill, 1, attr); - put("length", Value(0), Attribute::Configurable); + define_native_function("filter", filter, 1, attr); + define_native_function("forEach", for_each, 1, attr); + define_native_function("map", map, 1, attr); + define_native_function("pop", pop, 0, attr); + define_native_function("push", push, 1, attr); + define_native_function("shift", shift, 0, attr); + define_native_function("toString", to_string, 0, attr); + define_native_function("unshift", unshift, 1, attr); + define_native_function("join", join, 1, attr); + define_native_function("concat", concat, 1, attr); + define_native_function("slice", slice, 2, attr); + define_native_function("indexOf", index_of, 1, attr); + define_native_function("reduce", reduce, 1, attr); + define_native_function("reduceRight", reduce_right, 1, attr); + define_native_function("reverse", reverse, 0, attr); + define_native_function("lastIndexOf", last_index_of, 1, attr); + define_native_function("includes", includes, 1, attr); + define_native_function("find", find, 1, attr); + define_native_function("findIndex", find_index, 1, attr); + define_native_function("some", some, 1, attr); + define_native_function("every", every, 1, attr); + define_native_function("splice", splice, 2, attr); + define_native_function("fill", fill, 1, attr); + define_property("length", Value(0), Attribute::Configurable); } ArrayPrototype::~ArrayPrototype() @@ -115,7 +115,7 @@ static void for_each_item(Interpreter& interpreter, const String& name, AK::Func auto this_value = interpreter.argument(1); for (size_t i = 0; i < initial_length; ++i) { - auto value = this_object->get_by_index(i); + auto value = this_object->get(i); if (interpreter.exception()) return; if (value.is_empty()) { @@ -193,7 +193,7 @@ Value ArrayPrototype::push(Interpreter& interpreter) if (new_length > MAX_ARRAY_LIKE_INDEX) return interpreter.throw_exception<TypeError>("Maximum array size exceeded"); for (size_t i = 0; i < argument_count; ++i) - this_object->put_by_index(length + i, interpreter.argument(i)); + this_object->put(length + i, interpreter.argument(i)); auto new_length_value = Value((i32)new_length); this_object->put("length", new_length_value); if (interpreter.exception()) @@ -228,10 +228,10 @@ Value ArrayPrototype::pop(Interpreter& interpreter) return js_undefined(); } auto index = length - 1; - auto element = this_object->get_by_index(index).value_or(js_undefined()); + auto element = this_object->get(index).value_or(js_undefined()); if (interpreter.exception()) return {}; - this_object->delete_property(PropertyName(index)); + this_object->delete_property(index); this_object->put("length", Value((i32)index)); if (interpreter.exception()) return {}; @@ -279,7 +279,7 @@ Value ArrayPrototype::join(Interpreter& interpreter) for (size_t i = 0; i < length; ++i) { if (i > 0) builder.append(separator); - auto value = this_object->get_by_index(i).value_or(js_undefined()); + auto value = this_object->get(i).value_or(js_undefined()); if (interpreter.exception()) return {}; if (value.is_undefined() || value.is_null()) @@ -379,7 +379,7 @@ Value ArrayPrototype::index_of(Interpreter& interpreter) } auto search_element = interpreter.argument(0); for (i32 i = from_index; i < length; ++i) { - auto element = this_object->get_by_index(i); + auto element = this_object->get(i); if (interpreter.exception()) return {}; if (strict_eq(interpreter, element, search_element)) @@ -410,7 +410,7 @@ Value ArrayPrototype::reduce(Interpreter& interpreter) } else { bool start_found = false; while (!start_found && start < initial_length) { - auto value = this_object->get_by_index(start); + auto value = this_object->get(start); if (interpreter.exception()) return {}; start_found = !value.is_empty(); @@ -427,7 +427,7 @@ Value ArrayPrototype::reduce(Interpreter& interpreter) auto this_value = js_undefined(); for (size_t i = start; i < initial_length; ++i) { - auto value = this_object->get_by_index(i); + auto value = this_object->get(i); if (interpreter.exception()) return {}; if (value.is_empty()) @@ -469,7 +469,7 @@ Value ArrayPrototype::reduce_right(Interpreter& interpreter) } else { bool start_found = false; while (!start_found && start >= 0) { - auto value = this_object->get_by_index(start); + auto value = this_object->get(start); if (interpreter.exception()) return {}; start_found = !value.is_empty(); @@ -486,7 +486,7 @@ Value ArrayPrototype::reduce_right(Interpreter& interpreter) auto this_value = js_undefined(); for (int i = start; i >= 0; --i) { - auto value = this_object->get_by_index(i); + auto value = this_object->get(i); if (interpreter.exception()) return {}; if (value.is_empty()) @@ -548,7 +548,7 @@ Value ArrayPrototype::last_index_of(Interpreter& interpreter) } auto search_element = interpreter.argument(0); for (i32 i = from_index; i >= 0; --i) { - auto element = this_object->get_by_index(i); + auto element = this_object->get(i); if (interpreter.exception()) return {}; if (strict_eq(interpreter, element, search_element)) @@ -579,7 +579,7 @@ Value ArrayPrototype::includes(Interpreter& interpreter) } auto value_to_find = interpreter.argument(0); for (i32 i = from_index; i < length; ++i) { - auto element = this_object->get_by_index(i).value_or(js_undefined()); + auto element = this_object->get(i).value_or(js_undefined()); if (interpreter.exception()) return {}; if (same_value_zero(interpreter, element, value_to_find)) @@ -687,7 +687,7 @@ Value ArrayPrototype::splice(Interpreter& interpreter) auto removed_elements = Array::create(interpreter.global_object()); for (size_t i = 0; i < actual_delete_count; ++i) { - auto value = this_object->get_by_index(actual_start + i); + auto value = this_object->get(actual_start + i); if (interpreter.exception()) return {}; @@ -696,43 +696,43 @@ Value ArrayPrototype::splice(Interpreter& interpreter) if (insert_count < actual_delete_count) { for (size_t i = actual_start; i < initial_length - actual_delete_count; ++i) { - auto from = this_object->get_by_index(i + actual_delete_count); + auto from = this_object->get(i + actual_delete_count); if (interpreter.exception()) return {}; auto to = i + insert_count; if (!from.is_empty()) { - this_object->put_by_index(to, from); + this_object->put(to, from); if (interpreter.exception()) return {}; } else - this_object->delete_property(PropertyName(to)); + this_object->delete_property(to); } for (size_t i = initial_length; i > new_length; --i) - this_object->delete_property(PropertyName(i - 1)); + this_object->delete_property(i - 1); } else if (insert_count > actual_delete_count) { for (size_t i = initial_length - actual_delete_count; i > actual_start; --i) { - auto from = this_object->get_by_index(i + actual_delete_count - 1); + auto from = this_object->get(i + actual_delete_count - 1); if (interpreter.exception()) return {}; auto to = i + insert_count - 1; if (!from.is_empty()) { - this_object->put_by_index(to, from); + this_object->put(to, from); if (interpreter.exception()) return {}; } else - this_object->delete_property(PropertyName(to)); + this_object->delete_property(to); } } for (size_t i = 0; i < insert_count; ++i) { - this_object->put_by_index(actual_start + i, interpreter.argument(i + 2)); + this_object->put(actual_start + i, interpreter.argument(i + 2)); if (interpreter.exception()) return {}; } @@ -782,7 +782,7 @@ Value ArrayPrototype::fill(Interpreter& interpreter) to = min(relative_end, length); for (size_t i = from; i < to; i++) { - this_object->put_by_index(i, interpreter.argument(0)); + this_object->put(i, interpreter.argument(0)); if (interpreter.exception()) return {}; } diff --git a/Libraries/LibJS/Runtime/BooleanConstructor.cpp b/Libraries/LibJS/Runtime/BooleanConstructor.cpp index 96a66a2f0b..64910080c1 100644 --- a/Libraries/LibJS/Runtime/BooleanConstructor.cpp +++ b/Libraries/LibJS/Runtime/BooleanConstructor.cpp @@ -36,8 +36,8 @@ namespace JS { BooleanConstructor::BooleanConstructor() : NativeFunction("Boolean", *interpreter().global_object().function_prototype()) { - put("prototype", Value(interpreter().global_object().boolean_prototype()), 0); - put("length", Value(1), Attribute::Configurable); + define_property("prototype", Value(interpreter().global_object().boolean_prototype()), 0); + define_property("length", Value(1), Attribute::Configurable); } BooleanConstructor::~BooleanConstructor() diff --git a/Libraries/LibJS/Runtime/BooleanPrototype.cpp b/Libraries/LibJS/Runtime/BooleanPrototype.cpp index bb0730ee72..6fcf08ed95 100644 --- a/Libraries/LibJS/Runtime/BooleanPrototype.cpp +++ b/Libraries/LibJS/Runtime/BooleanPrototype.cpp @@ -35,8 +35,8 @@ namespace JS { BooleanPrototype::BooleanPrototype() : BooleanObject(false, *interpreter().global_object().object_prototype()) { - put_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable); - put_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable); + define_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable); + define_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable); } BooleanPrototype::~BooleanPrototype() { } diff --git a/Libraries/LibJS/Runtime/BoundFunction.cpp b/Libraries/LibJS/Runtime/BoundFunction.cpp index 4aa82f2c2b..19fb259430 100644 --- a/Libraries/LibJS/Runtime/BoundFunction.cpp +++ b/Libraries/LibJS/Runtime/BoundFunction.cpp @@ -36,7 +36,7 @@ BoundFunction::BoundFunction(Function& target_function, Value bound_this, Vector , m_constructor_prototype(constructor_prototype) , m_name(String::format("bound %s", target_function.name().characters())) { - put("length", Value(length), Attribute::Configurable); + define_property("length", Value(length), Attribute::Configurable); } BoundFunction::~BoundFunction() diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp index 697e0edb38..e3887e6280 100644 --- a/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -38,15 +38,15 @@ namespace JS { ConsoleObject::ConsoleObject() : Object(interpreter().global_object().object_prototype()) { - put_native_function("log", log); - put_native_function("debug", debug); - put_native_function("info", info); - put_native_function("warn", warn); - put_native_function("error", error); - put_native_function("trace", trace); - put_native_function("count", count); - put_native_function("countReset", count_reset); - put_native_function("clear", clear); + define_native_function("log", log); + define_native_function("debug", debug); + define_native_function("info", info); + define_native_function("warn", warn); + define_native_function("error", error); + define_native_function("trace", trace); + define_native_function("count", count); + define_native_function("countReset", count_reset); + define_native_function("clear", clear); } ConsoleObject::~ConsoleObject() diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index a9f573e6be..cff912c2fa 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -37,10 +37,10 @@ namespace JS { DateConstructor::DateConstructor() : NativeFunction("Date", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().date_prototype(), 0); - put("length", Value(7), Attribute::Configurable); + define_property("prototype", interpreter().global_object().date_prototype(), 0); + define_property("length", Value(7), Attribute::Configurable); - put_native_function("now", now, 0, Attribute::Writable | Attribute::Configurable); + define_native_function("now", now, 0, Attribute::Writable | Attribute::Configurable); } DateConstructor::~DateConstructor() diff --git a/Libraries/LibJS/Runtime/DatePrototype.cpp b/Libraries/LibJS/Runtime/DatePrototype.cpp index ee4bc9c095..546cefa78d 100644 --- a/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -52,18 +52,18 @@ DatePrototype::DatePrototype() : Object(interpreter().global_object().object_prototype()) { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("getDate", get_date, 0, attr); - put_native_function("getDay", get_day, 0, attr); - put_native_function("getFullYear", get_full_year, 0, attr); - put_native_function("getHours", get_hours, 0, attr); - put_native_function("getMilliseconds", get_milliseconds, 0, attr); - put_native_function("getMinutes", get_minutes, 0, attr); - put_native_function("getMonth", get_month, 0, attr); - put_native_function("getSeconds", get_seconds, 0, attr); - put_native_function("getTime", get_time, 0, attr); - put_native_function("toDateString", to_date_string, 0, attr); - put_native_function("toTimeString", to_time_string, 0, attr); - put_native_function("toString", to_string, 0, attr); + define_native_function("getDate", get_date, 0, attr); + define_native_function("getDay", get_day, 0, attr); + define_native_function("getFullYear", get_full_year, 0, attr); + define_native_function("getHours", get_hours, 0, attr); + define_native_function("getMilliseconds", get_milliseconds, 0, attr); + define_native_function("getMinutes", get_minutes, 0, attr); + define_native_function("getMonth", get_month, 0, attr); + define_native_function("getSeconds", get_seconds, 0, attr); + define_native_function("getTime", get_time, 0, attr); + define_native_function("toDateString", to_date_string, 0, attr); + define_native_function("toTimeString", to_time_string, 0, attr); + define_native_function("toString", to_string, 0, attr); } DatePrototype::~DatePrototype() diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp index a28ee1a47c..91d96b9bf1 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.cpp +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -34,8 +34,8 @@ namespace JS { ErrorConstructor::ErrorConstructor() : NativeFunction("Error", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().error_prototype(), 0); - put("length", Value(1), Attribute::Configurable); + define_property("prototype", interpreter().global_object().error_prototype(), 0); + define_property("length", Value(1), Attribute::Configurable); } ErrorConstructor::~ErrorConstructor() @@ -62,8 +62,8 @@ Value ErrorConstructor::construct(Interpreter& interpreter) ConstructorName::ConstructorName() \ : NativeFunction(*interpreter().global_object().function_prototype()) \ { \ - put("prototype", interpreter().global_object().snake_name##_prototype(), 0); \ - put("length", Value(1), Attribute::Configurable); \ + define_property("prototype", interpreter().global_object().snake_name##_prototype(), 0); \ + define_property("length", Value(1), Attribute::Configurable); \ } \ ConstructorName::~ConstructorName() { } \ Value ConstructorName::call(Interpreter& interpreter) \ diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.cpp b/Libraries/LibJS/Runtime/ErrorPrototype.cpp index 83bb5bb6f3..cdc34fe58b 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ErrorPrototype.cpp @@ -39,9 +39,9 @@ ErrorPrototype::ErrorPrototype() : Object(interpreter().global_object().object_prototype()) { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_property("name", name_getter, name_setter, attr); - put_native_property("message", message_getter, nullptr, attr); - put_native_function("toString", to_string, 0, attr); + define_native_property("name", name_getter, name_setter, attr); + define_native_property("message", message_getter, nullptr, attr); + define_native_function("toString", to_string, 0, attr); } ErrorPrototype::~ErrorPrototype() diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Libraries/LibJS/Runtime/FunctionConstructor.cpp index 611780e73e..9a5b8e09ef 100644 --- a/Libraries/LibJS/Runtime/FunctionConstructor.cpp +++ b/Libraries/LibJS/Runtime/FunctionConstructor.cpp @@ -38,8 +38,8 @@ namespace JS { FunctionConstructor::FunctionConstructor() : NativeFunction("Function", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().function_prototype(), 0); - put("length", Value(1), Attribute::Configurable); + define_property("prototype", interpreter().global_object().function_prototype(), 0); + define_property("length", Value(1), Attribute::Configurable); } FunctionConstructor::~FunctionConstructor() diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index c3c6af72ae..88a4aca167 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -46,12 +46,12 @@ FunctionPrototype::FunctionPrototype() void FunctionPrototype::initialize() { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("apply", apply, 2, attr); - put_native_function("bind", bind, 1, attr); - put_native_function("call", call, 1, attr); - put_native_function("toString", to_string, 0, attr); - put("length", Value(0), Attribute::Configurable); - put("name", js_string(heap(), ""), Attribute::Configurable); + define_native_function("apply", apply, 2, attr); + define_native_function("bind", bind, 1, attr); + define_native_function("call", call, 1, attr); + define_native_function("toString", to_string, 0, attr); + define_property("length", Value(0), Attribute::Configurable); + define_property("name", js_string(heap(), ""), Attribute::Configurable); } FunctionPrototype::~FunctionPrototype() @@ -80,7 +80,7 @@ Value FunctionPrototype::apply(Interpreter& interpreter) return {}; MarkedValueList arguments(interpreter.heap()); for (size_t i = 0; i < length; ++i) { - auto element = arg_array.as_object().get(String::number(i)); + auto element = arg_array.as_object().get(i); if (interpreter.exception()) return {}; arguments.append(element.value_or(js_undefined())); diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 6cddc07568..17b7d92318 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -82,19 +82,19 @@ void GlobalObject::initialize() #undef __JS_ENUMERATE u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("gc", gc, 0, attr); - put_native_function("isNaN", is_nan, 1, attr); - put_native_function("isFinite", is_finite, 1, attr); - put_native_function("parseFloat", parse_float, 1, attr); - - put("NaN", js_nan(), 0); - put("Infinity", js_infinity(), 0); - put("undefined", js_undefined(), 0); - - put("globalThis", this, attr); - put("console", heap().allocate<ConsoleObject>(), attr); - put("Math", heap().allocate<MathObject>(), attr); - put("Reflect", heap().allocate<ReflectObject>(), attr); + define_native_function("gc", gc, 0, attr); + define_native_function("isNaN", is_nan, 1, attr); + define_native_function("isFinite", is_finite, 1, attr); + define_native_function("parseFloat", parse_float, 1, attr); + + define_property("NaN", js_nan(), 0); + define_property("Infinity", js_infinity(), 0); + define_property("undefined", js_undefined(), 0); + + define_property("globalThis", this, attr); + define_property("console", heap().allocate<ConsoleObject>(), attr); + define_property("Math", heap().allocate<MathObject>(), attr); + define_property("Reflect", heap().allocate<ReflectObject>(), attr); add_constructor("Array", m_array_constructor, *m_array_prototype); add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype); diff --git a/Libraries/LibJS/Runtime/GlobalObject.h b/Libraries/LibJS/Runtime/GlobalObject.h index 42a99b37fe..8db573598b 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Libraries/LibJS/Runtime/GlobalObject.h @@ -73,9 +73,9 @@ template<typename ConstructorType> inline void GlobalObject::add_constructor(const FlyString& property_name, ConstructorType*& constructor, Object& prototype) { constructor = heap().allocate<ConstructorType>(); - constructor->put("name", js_string(heap(), property_name), Attribute::Configurable); - prototype.put("constructor", constructor, Attribute::Writable | Attribute::Configurable); - put(property_name, constructor, Attribute::Writable | Attribute::Configurable); + constructor->define_property("name", js_string(heap(), property_name), Attribute::Configurable); + prototype.define_property("constructor", constructor, Attribute::Writable | Attribute::Configurable); + define_property(property_name, constructor, Attribute::Writable | Attribute::Configurable); } } diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 86ec01a72f..988d091218 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -38,32 +38,32 @@ MathObject::MathObject() : Object(interpreter().global_object().object_prototype()) { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("abs", abs, 1, attr); - put_native_function("random", random, 0, attr); - put_native_function("sqrt", sqrt, 1, attr); - put_native_function("floor", floor, 1, attr); - put_native_function("ceil", ceil, 1, attr); - put_native_function("round", round, 1, attr); - put_native_function("max", max, 2, attr); - put_native_function("min", min, 2, attr); - put_native_function("trunc", trunc, 1, attr); - put_native_function("sin", sin, 1, attr); - put_native_function("cos", cos, 1, attr); - put_native_function("tan", tan, 1, attr); - put_native_function("pow", pow, 2, attr); - put_native_function("exp", exp, 1, attr); - put_native_function("expm1", expm1, 1, attr); - put_native_function("sign", sign, 1, attr); - put_native_function("clz32", clz32, 1, attr); + define_native_function("abs", abs, 1, attr); + define_native_function("random", random, 0, attr); + define_native_function("sqrt", sqrt, 1, attr); + define_native_function("floor", floor, 1, attr); + define_native_function("ceil", ceil, 1, attr); + define_native_function("round", round, 1, attr); + define_native_function("max", max, 2, attr); + define_native_function("min", min, 2, attr); + define_native_function("trunc", trunc, 1, attr); + define_native_function("sin", sin, 1, attr); + define_native_function("cos", cos, 1, attr); + define_native_function("tan", tan, 1, attr); + define_native_function("pow", pow, 2, attr); + define_native_function("exp", exp, 1, attr); + define_native_function("expm1", expm1, 1, attr); + define_native_function("sign", sign, 1, attr); + define_native_function("clz32", clz32, 1, attr); - put("E", Value(M_E), 0); - put("LN2", Value(M_LN2), 0); - put("LN10", Value(M_LN10), 0); - put("LOG2E", Value(log2(M_E)), 0); - put("LOG10E", Value(log10(M_E)), 0); - put("PI", Value(M_PI), 0); - put("SQRT1_2", Value(M_SQRT1_2), 0); - put("SQRT2", Value(M_SQRT2), 0); + define_property("E", Value(M_E), 0); + define_property("LN2", Value(M_LN2), 0); + define_property("LN10", Value(M_LN10), 0); + define_property("LOG2E", Value(log2(M_E)), 0); + define_property("LOG10E", Value(log10(M_E)), 0); + define_property("PI", Value(M_PI), 0); + define_property("SQRT1_2", Value(M_SQRT1_2), 0); + define_property("SQRT2", Value(M_SQRT2), 0); } MathObject::~MathObject() diff --git a/Libraries/LibJS/Runtime/NumberConstructor.cpp b/Libraries/LibJS/Runtime/NumberConstructor.cpp index 221de5af16..a37c913bbf 100644 --- a/Libraries/LibJS/Runtime/NumberConstructor.cpp +++ b/Libraries/LibJS/Runtime/NumberConstructor.cpp @@ -41,19 +41,19 @@ NumberConstructor::NumberConstructor() : NativeFunction("Number", *interpreter().global_object().function_prototype()) { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("isFinite", is_finite, 1, attr); - put_native_function("isInteger", is_integer, 1, attr); - put_native_function("isNaN", is_nan, 1, attr); - put_native_function("isSafeInteger", is_safe_integer, 1, attr); - put("parseFloat", interpreter().global_object().get("parseFloat")); - put("prototype", interpreter().global_object().number_prototype(), 0); - put("length", Value(1), Attribute::Configurable); - put("EPSILON", Value(EPSILON), 0); - put("MAX_SAFE_INTEGER", Value(MAX_SAFE_INTEGER), 0); - put("MIN_SAFE_INTEGER", Value(MIN_SAFE_INTEGER), 0); - put("NEGATIVE_INFINITY", js_negative_infinity(), 0); - put("POSITIVE_INFINITY", js_infinity(), 0); - put("NaN", js_nan(), 0); + define_native_function("isFinite", is_finite, 1, attr); + define_native_function("isInteger", is_integer, 1, attr); + define_native_function("isNaN", is_nan, 1, attr); + define_native_function("isSafeInteger", is_safe_integer, 1, attr); + define_property("parseFloat", interpreter().global_object().get("parseFloat")); + define_property("prototype", interpreter().global_object().number_prototype(), 0); + define_property("length", Value(1), Attribute::Configurable); + define_property("EPSILON", Value(EPSILON), 0); + define_property("MAX_SAFE_INTEGER", Value(MAX_SAFE_INTEGER), 0); + define_property("MIN_SAFE_INTEGER", Value(MIN_SAFE_INTEGER), 0); + define_property("NEGATIVE_INFINITY", js_negative_infinity(), 0); + define_property("POSITIVE_INFINITY", js_infinity(), 0); + define_property("NaN", js_nan(), 0); } NumberConstructor::~NumberConstructor() diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index daffc88a15..8e4043cbb8 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -89,13 +89,21 @@ bool Object::has_prototype(const Object* prototype) const return false; } -Value Object::get_own_property(const Object& this_object, const FlyString& property_name) const +Value Object::get_own_property(const Object& this_object, PropertyName property_name) const { - auto metadata = shape().lookup(property_name); - if (!metadata.has_value()) - return {}; + Value value_here; + + if (property_name.is_number()) { + if (static_cast<size_t>(property_name.as_number()) >= m_elements.size()) + return {}; + value_here = m_elements[property_name.as_number()]; + } else { + auto metadata = shape().lookup(property_name.as_string()); + if (!metadata.has_value()) + return {}; + value_here = m_storage[metadata.value().offset]; + } - auto value_here = m_storage[metadata.value().offset]; ASSERT(!value_here.is_empty()); if (value_here.is_accessor()) { return value_here.as_accessor().call_getter(Value(const_cast<Object*>(this))); @@ -122,14 +130,14 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k for (size_t i = 0; i < str.length(); ++i) { if (kind == GetOwnPropertyMode::Key) { - properties_array->put_by_index(i, js_string(interpreter(), String::number(i))); + properties_array->define_property(i, js_string(interpreter(), String::number(i))); } else if (kind == GetOwnPropertyMode::Value) { - properties_array->put_by_index(i, js_string(interpreter(), String::format("%c", str[i]))); + properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i]))); } else { auto* entry_array = Array::create(interpreter().global_object()); - entry_array->put_by_index(0, js_string(interpreter(), String::number(i))); - entry_array->put_by_index(1, js_string(interpreter(), String::format("%c", str[i]))); - properties_array->put_by_index(i, entry_array); + entry_array->define_property(0, js_string(interpreter(), String::number(i))); + entry_array->define_property(1, js_string(interpreter(), String::format("%c", str[i]))); + properties_array->define_property(i, entry_array); } } @@ -175,24 +183,36 @@ Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode k return properties_array; } -Value Object::get_own_property_descriptor(const FlyString& property_name) const +Value Object::get_own_property_descriptor(PropertyName property_name) const { - auto metadata = shape().lookup(property_name); - if (!metadata.has_value()) - return js_undefined(); - auto value = get(property_name); - if (interpreter().exception()) - return {}; + Value value; + u8 attributes; + + if (property_name.is_number()) { + if (static_cast<size_t>(property_name.as_number()) >= m_elements.size()) + return {}; + value = m_elements[property_name.as_number()]; + attributes = default_attributes; + } else { + auto metadata = shape().lookup(property_name.as_string()); + if (!metadata.has_value()) + return js_undefined(); + value = get(property_name); + if (interpreter().exception()) + return {}; + attributes = metadata.value().attributes; + } + auto* descriptor = Object::create_empty(interpreter(), interpreter().global_object()); - descriptor->put("enumerable", Value((metadata.value().attributes & Attribute::Enumerable) != 0)); - descriptor->put("configurable", Value((metadata.value().attributes & Attribute::Configurable) != 0)); + descriptor->define_property("enumerable", Value((attributes & Attribute::Enumerable) != 0)); + descriptor->define_property("configurable", Value((attributes & Attribute::Configurable) != 0)); if (value.is_accessor()) { auto& pair = value.as_accessor(); - descriptor->put("get", pair.getter()); - descriptor->put("set", pair.setter()); + descriptor->define_property("get", pair.getter()); + descriptor->define_property("set", pair.setter()); } else { - descriptor->put("value", value.value_or(js_undefined())); - descriptor->put("writable", Value((metadata.value().attributes & Attribute::Writable) != 0)); + descriptor->define_property("value", value.value_or(js_undefined())); + descriptor->define_property("writable", Value((attributes & Attribute::Writable) != 0)); } return descriptor; } @@ -249,7 +269,7 @@ bool Object::define_property(const FlyString& property_name, const Object& descr << "getter=" << getter.to_string_without_side_effects() << ", " << "setter=" << setter.to_string_without_side_effects() << "}"; - return put_own_property(*this, property_name, attributes, Accessor::create(interpreter(), getter_function, setter_function), PutOwnPropertyMode::DefineProperty, throw_exceptions); + return define_property(property_name, Accessor::create(interpreter(), getter_function, setter_function), attributes, throw_exceptions); } auto value = descriptor.get("value"); @@ -263,10 +283,21 @@ bool Object::define_property(const FlyString& property_name, const Object& descr dbg() << "Defining new property " << property_name << " with data descriptor { attributes=" << attributes << ", value=" << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects()) << " }"; - return put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::DefineProperty, throw_exceptions); + return define_property(property_name, value, attributes, throw_exceptions); } -bool Object::put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value value, PutOwnPropertyMode mode, bool throw_exceptions) +bool Object::define_property(PropertyName property_name, Value value, u8 attributes, bool throw_exceptions) +{ + if (property_name.is_number()) + return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions); + bool ok; + i32 property_index = property_name.as_string().to_int(ok); + if (ok && property_index >= 0) + return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions); + return put_own_property(*this, property_name.as_string(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions); +} + +bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value, u8 attributes, PutOwnPropertyMode mode, bool throw_exceptions) { ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor())); @@ -338,6 +369,53 @@ bool Object::put_own_property(Object& this_object, const FlyString& property_nam return true; } +bool Object::put_own_property_by_index(Object& this_object, u32 property_index, Value value, u8 attributes, PutOwnPropertyMode mode, bool throw_exceptions) +{ + ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor())); + + if (value.is_accessor()) { + auto& accessor = value.as_accessor(); + if (accessor.getter()) + attributes |= Attribute::HasGet; + if (accessor.setter()) + attributes |= Attribute::HasSet; + } + + auto new_property = property_index >= m_elements.size(); + auto existing_property = new_property ? Value() : m_elements[property_index]; + auto existing_attributes = default_attributes; + + if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(existing_attributes & Attribute::Configurable) && attributes != existing_attributes) { + dbg() << "Disallow reconfig of non-configurable property"; + if (throw_exceptions) + interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property %d", property_index)); + return false; + } + + auto value_here = existing_property; + if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !(existing_attributes & Attribute::Writable)) { + dbg() << "Disallow write to non-writable property"; + return false; + } + + if (value.is_empty()) + return true; + + if (value_here.is_object() && value_here.as_object().is_native_property()) { + auto& native_property = static_cast<NativeProperty&>(value_here.as_object()); + auto& interpreter = const_cast<Object*>(this)->interpreter(); + auto& call_frame = interpreter.push_call_frame(); + call_frame.this_value = &this_object; + native_property.set(interpreter, value); + interpreter.pop_call_frame(); + } else { + if (new_property) + m_elements.resize(property_index + 1); + m_elements[property_index] = value; + } + return true; +} + Value Object::delete_property(PropertyName property_name) { ASSERT(property_name.is_valid()); @@ -371,16 +449,13 @@ void Object::ensure_shape_is_unique() m_shape = m_shape->create_unique_clone(); } -Value Object::get_by_index(i32 property_index) const +Value Object::get_by_index(u32 property_index) const { - if (property_index < 0) - return get(String::number(property_index)); - const Object* object = this; while (object) { if (is_string_object()) { auto& string = static_cast<const StringObject*>(this)->primitive_string().string(); - if (property_index < (i32)string.length()) + if (property_index < string.length()) return js_string(heap(), string.substring(property_index, 1)); return js_undefined(); } @@ -395,10 +470,14 @@ Value Object::get_by_index(i32 property_index) const return {}; } -Value Object::get(const FlyString& property_name) const +Value Object::get(PropertyName property_name) const { + if (property_name.is_number()) + return get_by_index(property_name.as_number()); + + auto property_string = property_name.to_string(); bool ok; - i32 property_index = property_name.to_int(ok); + i32 property_index = property_string.to_int(ok); if (ok && property_index >= 0) return get_by_index(property_index); @@ -412,18 +491,9 @@ Value Object::get(const FlyString& property_name) const return {}; } -Value Object::get(PropertyName property_name) const -{ - if (property_name.is_number()) - return get_by_index(property_name.as_number()); - return get(property_name.as_string()); -} - -bool Object::put_by_index(i32 property_index, Value value, u8 attributes) +bool Object::put_by_index(u32 property_index, Value value) { ASSERT(!value.is_empty()); - if (property_index < 0) - return put(String::number(property_index), value, attributes); // FIXME: Implement some kind of sparse storage for arrays with huge indices. // Also: Take attributes into account here if (static_cast<size_t>(property_index) >= m_elements.size()) @@ -432,19 +502,24 @@ bool Object::put_by_index(i32 property_index, Value value, u8 attributes) return true; } -bool Object::put(const FlyString& property_name, Value value, u8 attributes) +bool Object::put(PropertyName property_name, Value value) { + if (property_name.is_number()) + return put_by_index(property_name.as_number(), value); + ASSERT(!value.is_empty()); + + auto property_string = property_name.to_string(); bool ok; - i32 property_index = property_name.to_int(ok); + i32 property_index = property_string.to_int(ok); if (ok && property_index >= 0) - return put_by_index(property_index, value, attributes); + return put_by_index(property_index, value); // If there's a setter in the prototype chain, we go to the setter. // Otherwise, it goes in the own property storage. Object* object = this; while (object) { - auto metadata = object->shape().lookup(property_name); + auto metadata = object->shape().lookup(property_string); if (metadata.has_value()) { auto value_here = object->m_storage[metadata.value().offset]; if (value_here.is_accessor()) { @@ -463,27 +538,20 @@ bool Object::put(const FlyString& property_name, Value value, u8 attributes) } object = object->prototype(); } - return put_own_property(*this, property_name, attributes, value, PutOwnPropertyMode::Put); -} - -bool Object::put(PropertyName property_name, Value value, u8 attributes) -{ - if (property_name.is_number()) - return put_by_index(property_name.as_number(), value, attributes); - return put(property_name.as_string(), value, attributes); + return put_own_property(*this, property_string, value, default_attributes, PutOwnPropertyMode::Put); } -bool Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attributes) +bool Object::define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attribute) { auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function)); - function->put("length", Value(length), Attribute::Configurable); - function->put("name", js_string(heap(), property_name), Attribute::Configurable); - return put(property_name, function, attributes); + function->define_property("length", Value(length), Attribute::Configurable); + function->define_property("name", js_string(heap(), property_name), Attribute::Configurable); + return define_property(property_name, function, attribute); } -bool Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attributes) +bool Object::define_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute) { - return put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attributes); + return define_property(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attribute); } void Object::visit_children(Cell::Visitor& visitor) @@ -498,7 +566,7 @@ void Object::visit_children(Cell::Visitor& visitor) visitor.visit(value); } -bool Object::has_property(const FlyString& property_name) const +bool Object::has_property(PropertyName property_name) const { const Object* object = this; while (object) { @@ -509,18 +577,25 @@ bool Object::has_property(const FlyString& property_name) const return false; } -bool Object::has_own_property(const FlyString& property_name) const +bool Object::has_own_property(PropertyName property_name) const { - bool ok; - i32 property_index = property_name.to_int(ok); - if (ok && property_index >= 0) { + auto has_indexed_property = [&](u32 index) -> bool { if (is_string_object()) - return property_index < (i32) static_cast<const StringObject*>(this)->primitive_string().string().length(); - if (static_cast<size_t>(property_index) >= m_elements.size()) + return index < static_cast<const StringObject*>(this)->primitive_string().string().length(); + if (static_cast<size_t>(index) >= m_elements.size()) return false; - return !m_elements[property_index].is_empty(); - } - return shape().lookup(property_name).has_value(); + return !m_elements[index].is_empty(); + }; + + if (property_name.is_number()) + return has_indexed_property(property_name.as_number()); + + bool ok; + i32 property_index = property_name.as_string().to_int(ok); + if (ok && property_index >= 0) + return has_indexed_property(property_index); + + return shape().lookup(property_name.as_string()).has_value(); } Value Object::to_primitive(PreferredType preferred_type) const diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index f3e2b2fbe5..752d186c62 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -60,25 +60,24 @@ public: Shape& shape() { return *m_shape; } const Shape& shape() const { return *m_shape; } - Value delete_property(PropertyName); - - virtual Value get_by_index(i32 property_index) const; - Value get(const FlyString& property_name) const; Value get(PropertyName) const; - virtual bool put_by_index(i32 property_index, Value, u8 attributes = default_attributes); - bool put(const FlyString& property_name, Value, u8 attributes = default_attributes); - bool put(PropertyName, Value, u8 attributes = default_attributes); + bool has_property(PropertyName) const; + bool has_own_property(PropertyName) const; - Value get_own_property(const Object& this_object, const FlyString& property_name) const; - Value get_own_properties(const Object& this_object, GetOwnPropertyMode, u8 attributes = Attribute::Configurable | Attribute::Enumerable | Attribute::Writable) const; - Value get_own_property_descriptor(const FlyString& property_name) const; + bool put(PropertyName, Value); + + Value get_own_property(const Object& this_object, PropertyName) const; + Value get_own_properties(const Object& this_object, GetOwnPropertyMode, u8 attributes = default_attributes) const; + Value get_own_property_descriptor(PropertyName) const; bool define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions = true); - bool put_own_property(Object& this_object, const FlyString& property_name, u8 attributes, Value, PutOwnPropertyMode, bool throw_exceptions = true); + bool define_property(PropertyName, Value value, u8 attributes = default_attributes, bool throw_exceptions = true); - bool put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes); - bool put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes); + bool define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0, u8 attribute = default_attributes); + bool define_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute = default_attributes); + + Value delete_property(PropertyName); virtual bool is_array() const { return false; } virtual bool is_boolean() const { return false; } @@ -99,9 +98,6 @@ public: void set_prototype(Object*); bool has_prototype(const Object* prototype) const; - bool has_property(const FlyString& property_name) const; - bool has_own_property(const FlyString& property_name) const; - enum class PreferredType { Default, String, @@ -118,6 +114,11 @@ public: Vector<Value>& elements() { return m_elements; } private: + virtual Value get_by_index(u32 property_index) const; + virtual bool put_by_index(u32 property_index, Value); + bool put_own_property(Object& this_object, const FlyString& property_name, Value, u8 attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true); + bool put_own_property_by_index(Object& this_object, u32 property_index, Value, u8 attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true); + void set_shape(Shape&); void ensure_shape_is_unique(); diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index 5753046be6..e9f9459f5b 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -38,19 +38,19 @@ namespace JS { ObjectConstructor::ObjectConstructor() : NativeFunction("Object", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().object_prototype(), 0); - put("length", Value(1), Attribute::Configurable); + define_property("prototype", interpreter().global_object().object_prototype(), 0); + define_property("length", Value(1), Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("defineProperty", define_property, 3, attr); - put_native_function("is", is, 2, attr); - put_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr); - put_native_function("getOwnPropertyNames", get_own_property_names, 1, attr); - put_native_function("getPrototypeOf", get_prototype_of, 1, attr); - put_native_function("setPrototypeOf", set_prototype_of, 2, attr); - put_native_function("keys", keys, 1, attr); - put_native_function("values", values, 1, attr); - put_native_function("entries", entries, 1, attr); + define_native_function("defineProperty", define_property_, 3, attr); + define_native_function("is", is, 2, attr); + define_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr); + define_native_function("getOwnPropertyNames", get_own_property_names, 1, attr); + define_native_function("getPrototypeOf", get_prototype_of, 1, attr); + define_native_function("setPrototypeOf", set_prototype_of, 2, attr); + define_native_function("keys", keys, 1, attr); + define_native_function("values", values, 1, attr); + define_native_function("entries", entries, 1, attr); } ObjectConstructor::~ObjectConstructor() @@ -118,7 +118,7 @@ Value ObjectConstructor::get_own_property_descriptor(Interpreter& interpreter) return object->get_own_property_descriptor(property_key); } -Value ObjectConstructor::define_property(Interpreter& interpreter) +Value ObjectConstructor::define_property_(Interpreter& interpreter) { if (!interpreter.argument(0).is_object()) return interpreter.throw_exception<TypeError>("Object argument is not an object"); diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.h b/Libraries/LibJS/Runtime/ObjectConstructor.h index 3d80eff929..2a7376b46f 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.h +++ b/Libraries/LibJS/Runtime/ObjectConstructor.h @@ -42,7 +42,7 @@ private: virtual bool has_constructor() const override { return true; } virtual const char* class_name() const override { return "ObjectConstructor"; } - static Value define_property(Interpreter&); + static Value define_property_(Interpreter&); static Value is(Interpreter&); static Value get_own_property_descriptor(Interpreter&); static Value get_own_property_names(Interpreter&); diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp index 966c43241e..2b3624dc56 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -43,9 +43,9 @@ void ObjectPrototype::initialize() // This must be called after the constructor has returned, so that the below code // can find the ObjectPrototype through normal paths. u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("hasOwnProperty", has_own_property, 1, attr); - put_native_function("toString", to_string, 0, attr); - put_native_function("valueOf", value_of, 0, attr); + define_native_function("hasOwnProperty", has_own_property, 1, attr); + define_native_function("toString", to_string, 0, attr); + define_native_function("valueOf", value_of, 0, attr); } ObjectPrototype::~ObjectPrototype() 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 }; }; } diff --git a/Libraries/LibJS/Runtime/ReflectObject.cpp b/Libraries/LibJS/Runtime/ReflectObject.cpp index 004e497182..a36be0520e 100644 --- a/Libraries/LibJS/Runtime/ReflectObject.cpp +++ b/Libraries/LibJS/Runtime/ReflectObject.cpp @@ -79,19 +79,19 @@ ReflectObject::ReflectObject() : Object(interpreter().global_object().object_prototype()) { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_function("apply", apply, 3, attr); - put_native_function("construct", construct, 2, attr); - put_native_function("defineProperty", define_property, 3, attr); - put_native_function("deleteProperty", delete_property, 2, attr); - put_native_function("get", get, 2, attr); - put_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr); - put_native_function("getPrototypeOf", get_prototype_of, 1, attr); - put_native_function("has", has, 2, attr); - put_native_function("isExtensible", is_extensible, 1, attr); - put_native_function("ownKeys", own_keys, 1, attr); - put_native_function("preventExtensions", prevent_extensions, 1, attr); - put_native_function("set", set, 3, attr); - put_native_function("setPrototypeOf", set_prototype_of, 2, attr); + define_native_function("apply", apply, 3, attr); + define_native_function("construct", construct, 2, attr); + define_native_function("defineProperty", define_property, 3, attr); + define_native_function("deleteProperty", delete_property, 2, attr); + define_native_function("get", get, 2, attr); + define_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr); + define_native_function("getPrototypeOf", get_prototype_of, 1, attr); + define_native_function("has", has, 2, attr); + define_native_function("isExtensible", is_extensible, 1, attr); + define_native_function("ownKeys", own_keys, 1, attr); + define_native_function("preventExtensions", prevent_extensions, 1, attr); + define_native_function("set", set, 3, attr); + define_native_function("setPrototypeOf", set_prototype_of, 2, attr); } ReflectObject::~ReflectObject() diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 4eb5f5549b..4d8e0a4597 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -60,9 +60,9 @@ ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vec , m_parent_environment(parent_environment) , m_function_length(m_function_length) { - put("prototype", Object::create_empty(interpreter(), interpreter().global_object()), 0); - put_native_property("length", length_getter, nullptr, Attribute::Configurable); - put_native_property("name", name_getter, nullptr, Attribute::Configurable); + define_property("prototype", Object::create_empty(interpreter(), interpreter().global_object()), 0); + define_native_property("length", length_getter, nullptr, Attribute::Configurable); + define_native_property("name", name_getter, nullptr, Attribute::Configurable); } ScriptFunction::~ScriptFunction() diff --git a/Libraries/LibJS/Runtime/StringConstructor.cpp b/Libraries/LibJS/Runtime/StringConstructor.cpp index 9f51d3831a..a4f5a12ca3 100644 --- a/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -37,10 +37,10 @@ namespace JS { StringConstructor::StringConstructor() : NativeFunction("String", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().string_prototype(), 0); - put("length", Value(1), Attribute::Configurable); + define_property("prototype", interpreter().global_object().string_prototype(), 0); + define_property("length", Value(1), Attribute::Configurable); - put_native_function("raw", raw, 0, Attribute::Writable | Attribute::Configurable); + define_native_function("raw", raw, 0, Attribute::Writable | Attribute::Configurable); } StringConstructor::~StringConstructor() diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 87a5f58b50..b52d2f56bd 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -64,24 +64,24 @@ StringPrototype::StringPrototype() { u8 attr = Attribute::Writable | Attribute::Configurable; - put_native_property("length", length_getter, nullptr, 0); - put_native_function("charAt", char_at, 1, attr); - put_native_function("repeat", repeat, 1, attr); - put_native_function("startsWith", starts_with, 1, attr); - put_native_function("indexOf", index_of, 1, attr); - put_native_function("toLowerCase", to_lowercase, 0, attr); - put_native_function("toUpperCase", to_uppercase, 0, attr); - put_native_function("toString", to_string, 0, attr); - put_native_function("padStart", pad_start, 1, attr); - put_native_function("padEnd", pad_end, 1, attr); - put_native_function("trim", trim, 0, attr); - put_native_function("trimStart", trim_start, 0, attr); - put_native_function("trimEnd", trim_end, 0, attr); - put_native_function("concat", concat, 1, attr); - put_native_function("substring", substring, 2, attr); - put_native_function("includes", includes, 1, attr); - put_native_function("slice", slice, 2, attr); - put_native_function("lastIndexOf", last_index_of, 1, attr); + define_native_property("length", length_getter, nullptr, 0); + define_native_function("charAt", char_at, 1, attr); + define_native_function("repeat", repeat, 1, attr); + define_native_function("startsWith", starts_with, 1, attr); + define_native_function("indexOf", index_of, 1, attr); + define_native_function("toLowerCase", to_lowercase, 0, attr); + define_native_function("toUpperCase", to_uppercase, 0, attr); + define_native_function("toString", to_string, 0, attr); + define_native_function("padStart", pad_start, 1, attr); + define_native_function("padEnd", pad_end, 1, attr); + define_native_function("trim", trim, 0, attr); + define_native_function("trimStart", trim_start, 0, attr); + define_native_function("trimEnd", trim_end, 0, attr); + define_native_function("concat", concat, 1, attr); + define_native_function("substring", substring, 2, attr); + define_native_function("includes", includes, 1, attr); + define_native_function("slice", slice, 2, attr); + define_native_function("lastIndexOf", last_index_of, 1, attr); } StringPrototype::~StringPrototype() diff --git a/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Libraries/LibJS/Runtime/SymbolConstructor.cpp index c8d36823ba..485d90a3ec 100644 --- a/Libraries/LibJS/Runtime/SymbolConstructor.cpp +++ b/Libraries/LibJS/Runtime/SymbolConstructor.cpp @@ -35,27 +35,27 @@ namespace JS { SymbolConstructor::SymbolConstructor() : NativeFunction("Symbol", *interpreter().global_object().function_prototype()) { - put("prototype", interpreter().global_object().symbol_prototype(), 0); - put("length", Value(0), Attribute::Configurable); + define_property("prototype", interpreter().global_object().symbol_prototype(), 0); + define_property("length", Value(0), Attribute::Configurable); - put_native_function("for", for_, 1, Attribute::Writable | Attribute::Configurable); - put_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable); + define_native_function("for", for_, 1, Attribute::Writable | Attribute::Configurable); + define_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable); SymbolObject::initialize_well_known_symbols(interpreter()); - put("iterator", SymbolObject::well_known_iterator(), 0); - put("asyncIterator", SymbolObject::well_known_async_terator(), 0); - put("match", SymbolObject::well_known_match(), 0); - put("matchAll", SymbolObject::well_known_match_all(), 0); - put("replace", SymbolObject::well_known_replace(), 0); - put("search", SymbolObject::well_known_search(), 0); - put("split", SymbolObject::well_known_split(), 0); - put("hasInstance", SymbolObject::well_known_has_instance(), 0); - put("isConcatSpreadable", SymbolObject::well_known_is_concat_spreadable(), 0); - put("unscopables", SymbolObject::well_known_unscopables(), 0); - put("species", SymbolObject::well_known_species(), 0); - put("toPrimitive", SymbolObject::well_known_to_primtive(), 0); - put("toStringTag", SymbolObject::well_known_to_string_tag(), 0); + define_property("iterator", SymbolObject::well_known_iterator(), 0); + define_property("asyncIterator", SymbolObject::well_known_async_terator(), 0); + define_property("match", SymbolObject::well_known_match(), 0); + define_property("matchAll", SymbolObject::well_known_match_all(), 0); + define_property("replace", SymbolObject::well_known_replace(), 0); + define_property("search", SymbolObject::well_known_search(), 0); + define_property("split", SymbolObject::well_known_split(), 0); + define_property("hasInstance", SymbolObject::well_known_has_instance(), 0); + define_property("isConcatSpreadable", SymbolObject::well_known_is_concat_spreadable(), 0); + define_property("unscopables", SymbolObject::well_known_unscopables(), 0); + define_property("species", SymbolObject::well_known_species(), 0); + define_property("toPrimitive", SymbolObject::well_known_to_primtive(), 0); + define_property("toStringTag", SymbolObject::well_known_to_string_tag(), 0); } SymbolConstructor::~SymbolConstructor() diff --git a/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Libraries/LibJS/Runtime/SymbolPrototype.cpp index 7f8a7b923f..f97470c7d4 100644 --- a/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -42,10 +42,10 @@ namespace JS { SymbolPrototype::SymbolPrototype() : Object(interpreter().global_object().object_prototype()) { - put_native_property("description", description_getter, nullptr, Attribute::Configurable); + define_native_property("description", description_getter, nullptr, Attribute::Configurable); - put_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable); - put_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable); + define_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable); + define_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable); } SymbolPrototype::~SymbolPrototype() diff --git a/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp b/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp index 4bf1dfcdad..3553f50a9a 100644 --- a/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp +++ b/Libraries/LibJS/Runtime/Uint8ClampedArray.cpp @@ -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]); } diff --git a/Libraries/LibJS/Runtime/Uint8ClampedArray.h b/Libraries/LibJS/Runtime/Uint8ClampedArray.h index a68a7a44ef..8b2459c009 100644 --- a/Libraries/LibJS/Runtime/Uint8ClampedArray.h +++ b/Libraries/LibJS/Runtime/Uint8ClampedArray.h @@ -32,15 +32,15 @@ namespace JS { class Uint8ClampedArray final : public Object { public: - static Uint8ClampedArray* create(GlobalObject&, i32 length); + static Uint8ClampedArray* create(GlobalObject&, u32 length); - Uint8ClampedArray(i32 length, Object& prototype); + Uint8ClampedArray(u32 length, Object& prototype); virtual ~Uint8ClampedArray() override; i32 length() const { return m_length; } - virtual bool put_by_index(i32 property_index, Value value, u8 attribute = default_attributes) override; - virtual Value get_by_index(i32 property_index) const override; + virtual bool put_by_index(u32 property_index, Value value) override; + virtual Value get_by_index(u32 property_index) const override; u8* data() { return m_data; } const u8* data() const { return m_data; } @@ -52,7 +52,7 @@ private: static Value length_getter(Interpreter&); u8* m_data { nullptr }; - i32 m_length { 0 }; + u32 m_length { 0 }; }; } |