From 5ae9419a069f8180197df14447b1a23f1cf9411d Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Wed, 27 May 2020 11:35:09 -0700 Subject: LibJS: Object index properties have descriptors; Handle sparse indices This patch adds an IndexedProperties object for storing indexed properties within an Object. This accomplishes two goals: indexed properties now have an associated descriptor, and objects now gracefully handle sparse properties. The IndexedProperties class is a wrapper around two other classes, one for simple indexed properties storage, and one for general indexed property storage. Simple indexed property storage is the common-case, and is simply a vector of properties which all have attributes of default_attributes (writable, enumerable, and configurable). General indexed property storage is for a collection of indexed properties where EITHER one or more properties have attributes other than default_attributes OR there is a property with a large index (in particular, large is '200' or higher). Indexed properties are now treated relatively the same as storage within the various Object methods. Additionally, there is a custom iterator class for IndexedProperties which makes iteration easy. The iterator skips empty values by default, but can be configured otherwise. Likewise, it evaluates getters by default, but can be set not to. --- Userland/js.cpp | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'Userland/js.cpp') diff --git a/Userland/js.cpp b/Userland/js.cpp index 9d853d09ca..ccf6c37117 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -122,31 +122,44 @@ String read_next_piece() static void print_value(JS::Value value, HashTable& seen_objects); -static void print_array(const JS::Array& array, HashTable& seen_objects) +static void print_array(JS::Array& array, HashTable& seen_objects) { + bool first = true; fputs("[ ", stdout); - for (size_t i = 0; i < array.elements().size(); ++i) { - print_value(array.elements()[i], seen_objects); - if (i != array.elements().size() - 1) + for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) { + if (!first) fputs(", ", stdout); + first = false; + auto value = it.value_and_attributes(&array).value; + // The V8 repl doesn't throw an exception here, and instead just + // prints 'undefined'. We may choose to replicate that behavior in + // the future, but for now lets just catch the error + if (array.interpreter().exception()) + return; + print_value(value, seen_objects); } fputs(" ]", stdout); } -static void print_object(const JS::Object& object, HashTable& seen_objects) +static void print_object(JS::Object& object, HashTable& seen_objects) { fputs("{ ", stdout); - - for (size_t i = 0; i < object.elements().size(); ++i) { - if (object.elements()[i].is_empty()) - continue; - printf("\"\033[33;1m%zu\033[0m\": ", i); - print_value(object.elements()[i], seen_objects); - if (i != object.elements().size() - 1) + bool first = true; + for (auto& entry : object.indexed_properties()) { + if (!first) fputs(", ", stdout); + first = false; + printf("\"\033[33;1m%d\033[0m\": ", entry.index()); + auto value = entry.value_and_attributes(&object).value; + // The V8 repl doesn't throw an exception here, and instead just + // prints 'undefined'. We may choose to replicate that behavior in + // the future, but for now lets just catch the error + if (object.interpreter().exception()) + return; + print_value(value, seen_objects); } - if (!object.elements().is_empty() && object.shape().property_count()) + if (!object.indexed_properties().is_empty() && object.shape().property_count()) fputs(", ", stdout); size_t index = 0; @@ -196,7 +209,7 @@ void print_value(JS::Value value, HashTable& seen_objects) } if (value.is_array()) - return print_array(static_cast(value.as_object()), seen_objects); + return print_array(static_cast(value.as_object()), seen_objects); if (value.is_object()) { auto& object = value.as_object(); -- cgit v1.2.3