From c52d5150281e750503610fd01485f47523fe9152 Mon Sep 17 00:00:00 2001 From: davidot Date: Mon, 5 Jul 2021 01:55:45 +0200 Subject: [PATCH] LibJS: Make AbstractOperations::canonical_num... take a PropertyName This allows us to hide the fact that it could be a number and means we no longer need to check for this optimization in string and typedarray --- .../LibJS/Runtime/AbstractOperations.cpp | 13 +++- .../LibJS/Runtime/AbstractOperations.h | 2 +- .../Libraries/LibJS/Runtime/StringObject.cpp | 11 +-- Userland/Libraries/LibJS/Runtime/TypedArray.h | 69 +++++-------------- 4 files changed, 31 insertions(+), 64 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 27e443a825..919752d03c 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -544,10 +544,19 @@ Object* create_mapped_arguments_object(GlobalObject& global_object, FunctionObje } // 7.1.21 CanonicalNumericIndexString ( argument ), https://tc39.es/ecma262/#sec-canonicalnumericindexstring -Value canonical_numeric_index_string(GlobalObject& global_object, Value argument) +Value canonical_numeric_index_string(GlobalObject& global_object, PropertyName const& property_name) { + // NOTE: If the property name is a number type (An implementation-defined optimized + // property key type), it can be treated as a string property that has already been + // converted successfully into a canonical numeric index. + + VERIFY(property_name.is_string() || property_name.is_number()); + + if (property_name.is_number()) + return Value(property_name.as_number()); + // 1. Assert: Type(argument) is String. - VERIFY(argument.is_string()); + auto argument = Value(js_string(global_object.vm(), property_name.as_string())); // 2. If argument is "-0", return -0𝔽. if (argument.as_string().string() == "-0") diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index 68e7c1355e..3c7ecdbb68 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -29,7 +29,7 @@ bool validate_and_apply_property_descriptor(Object*, PropertyName const&, bool e Object* get_prototype_from_constructor(GlobalObject&, FunctionObject const& constructor, Object* (GlobalObject::*intrinsic_default_prototype)()); Object* create_unmapped_arguments_object(GlobalObject&, Vector const& arguments); Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector const&, Vector const& arguments, Environment&); -Value canonical_numeric_index_string(GlobalObject&, Value); +Value canonical_numeric_index_string(GlobalObject&, PropertyName const&); enum class CallerMode { Strict, diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.cpp b/Userland/Libraries/LibJS/Runtime/StringObject.cpp index 046c14335d..f1f028665d 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringObject.cpp @@ -45,8 +45,6 @@ void StringObject::visit_edges(Cell::Visitor& visitor) // 10.4.3.5 StringGetOwnProperty ( S, P ),https://tc39.es/ecma262/#sec-stringgetownproperty static Optional string_get_own_property(GlobalObject& global_object, StringObject const& string, PropertyName const& property_name) { - auto& vm = global_object.vm(); - // 1. Assert: S is an Object that has a [[StringData]] internal slot. // 2. Assert: IsPropertyKey(P) is true. VERIFY(property_name.is_valid()); @@ -58,14 +56,7 @@ static Optional string_get_own_property(GlobalObject& global return {}; // 4. Let index be ! CanonicalNumericIndexString(P). - // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. - Value index; - if (property_name.is_string()) - index = canonical_numeric_index_string(global_object, property_name.to_value(vm)); - else - index = Value(property_name.as_number()); + auto index = canonical_numeric_index_string(global_object, property_name); // 5. If index is undefined, return undefined. if (index.is_undefined()) return {}; diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index d3bdb90690..dcceb3eaa2 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -185,19 +185,14 @@ public: // 2. Assert: O is an Integer-Indexed exotic object. // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. + // property key type), it can be treated as a string property that will transparently be + // converted into a canonical numeric index. // 3. If Type(P) is String, then // NOTE: This includes an implementation-defined optimization, see note above! if (property_name.is_string() || property_name.is_number()) { // a. Let numericIndex be ! CanonicalNumericIndexString(P). - // NOTE: This includes an implementation-defined optimization, see note above! - Value numeric_index; - if (property_name.is_string()) - numeric_index = canonical_numeric_index_string(global_object(), property_name.to_value(vm())); - else - numeric_index = Value(property_name.as_number()); + auto numeric_index = canonical_numeric_index_string(global_object(), property_name); // b. If numericIndex is not undefined, then if (!numeric_index.is_undefined()) { // i. Let value be ! IntegerIndexedElementGet(O, numericIndex). @@ -230,19 +225,14 @@ public: // 2. Assert: O is an Integer-Indexed exotic object. // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. + // property key type), it can be treated as a string property that will transparently be + // converted into a canonical numeric index. // 3. If Type(P) is String, then // NOTE: This includes an implementation-defined optimization, see note above! if (property_name.is_string() || property_name.is_number()) { // a. Let numericIndex be ! CanonicalNumericIndexString(P). - // NOTE: This includes an implementation-defined optimization, see note above! - Value numeric_index; - if (property_name.is_string()) - numeric_index = canonical_numeric_index_string(global_object(), property_name.to_value(vm())); - else - numeric_index = Value(property_name.as_number()); + auto numeric_index = canonical_numeric_index_string(global_object(), property_name); // b. If numericIndex is not undefined, return ! IsValidIntegerIndex(O, numericIndex). if (!numeric_index.is_undefined()) return is_valid_integer_index(*this, numeric_index); @@ -261,19 +251,14 @@ public: // 2. Assert: O is an Integer-Indexed exotic object. // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. + // property key type), it can be treated as a string property that will transparently be + // converted into a canonical numeric index. // 3. If Type(P) is String, then // NOTE: This includes an implementation-defined optimization, see note above! if (property_name.is_string() || property_name.is_number()) { // a. Let numericIndex be ! CanonicalNumericIndexString(P). - // NOTE: This includes an implementation-defined optimization, see note above! - Value numeric_index; - if (property_name.is_string()) - numeric_index = canonical_numeric_index_string(global_object(), property_name.to_value(vm())); - else - numeric_index = Value(property_name.as_number()); + auto numeric_index = canonical_numeric_index_string(global_object(), property_name); // b. If numericIndex is not undefined, then if (!numeric_index.is_undefined()) { // i. If ! IsValidIntegerIndex(O, numericIndex) is false, return false. @@ -319,21 +304,15 @@ public: // 1. Assert: IsPropertyKey(P) is true. VERIFY(property_name.is_valid()); - // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. + // property key type), it can be treated as a string property that will transparently be + // converted into a canonical numeric index. // 2. If Type(P) is String, then // NOTE: This includes an implementation-defined optimization, see note above! if (property_name.is_string() || property_name.is_number()) { // a. Let numericIndex be ! CanonicalNumericIndexString(P). - // NOTE: This includes an implementation-defined optimization, see note above! - Value numeric_index; - if (property_name.is_string()) - numeric_index = canonical_numeric_index_string(global_object(), property_name.to_value(vm())); - else - numeric_index = Value(property_name.as_number()); + auto numeric_index = canonical_numeric_index_string(global_object(), property_name); // b. If numericIndex is not undefined, then if (!numeric_index.is_undefined()) { // i. Return ! IntegerIndexedElementGet(O, numericIndex). @@ -353,21 +332,15 @@ public: // 1. Assert: IsPropertyKey(P) is true. VERIFY(property_name.is_valid()); - // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. + // property key type), it can be treated as a string property that will transparently be + // converted into a canonical numeric index. // 2. If Type(P) is String, then // NOTE: This includes an implementation-defined optimization, see note above! if (property_name.is_string() || property_name.is_number()) { // a. Let numericIndex be ! CanonicalNumericIndexString(P). - // NOTE: This includes an implementation-defined optimization, see note above! - Value numeric_index; - if (property_name.is_string()) - numeric_index = canonical_numeric_index_string(global_object(), property_name.to_value(vm())); - else - numeric_index = Value(property_name.as_number()); + auto numeric_index = canonical_numeric_index_string(global_object(), property_name); // b. If numericIndex is not undefined, then if (!numeric_index.is_undefined()) { // i. Perform ? IntegerIndexedElementSet(O, numericIndex, V). @@ -391,21 +364,15 @@ public: VERIFY(property_name.is_valid()); // 2. Assert: O is an Integer-Indexed exotic object. - // NOTE: If the property name is a number type (An implementation-defined optimized - // property key type), it can be treated as a string property that has already been - // converted successfully into a canonical numeric index. + // property key type), it can be treated as a string property that will transparently be + // converted into a canonical numeric index. // 3. If Type(P) is String, then // NOTE: This includes an implementation-defined optimization, see note above! if (property_name.is_string() || property_name.is_number()) { // a. Let numericIndex be ! CanonicalNumericIndexString(P). - // NOTE: This includes an implementation-defined optimization, see note above! - Value numeric_index; - if (property_name.is_string()) - numeric_index = canonical_numeric_index_string(global_object(), property_name.to_value(vm())); - else - numeric_index = Value(property_name.as_number()); + auto numeric_index = canonical_numeric_index_string(global_object(), property_name); // b. If numericIndex is not undefined, then if (!numeric_index.is_undefined()) { // i. If ! IsValidIntegerIndex(O, numericIndex) is false, return true; else return false.