1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:57:36 +00:00

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
This commit is contained in:
davidot 2021-07-05 01:55:45 +02:00 committed by Linus Groh
parent 9b7e48c6bd
commit c52d515028
4 changed files with 31 additions and 64 deletions

View file

@ -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")

View file

@ -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<Value> const& arguments);
Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector<FunctionNode::Parameter> const&, Vector<Value> const& arguments, Environment&);
Value canonical_numeric_index_string(GlobalObject&, Value);
Value canonical_numeric_index_string(GlobalObject&, PropertyName const&);
enum class CallerMode {
Strict,

View file

@ -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<PropertyDescriptor> 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<PropertyDescriptor> 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 {};

View file

@ -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.