diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index e56fb20e116979b7c50700343590826fbea6c17b..4309074129c89f776706fdd3bd4858f302d1ba2b 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -48,10 +48,6 @@ Interpreter::Interpreter(VM& vm) : m_vm(vm) , m_console(*this) { -#define __JS_ENUMERATE(SymbolName, snake_name) \ - m_well_known_symbol_##snake_name = js_symbol(*this, "Symbol." #SymbolName, false); - JS_ENUMERATE_WELL_KNOWN_SYMBOLS -#undef __JS_ENUMERATE } Interpreter::~Interpreter() @@ -212,17 +208,6 @@ Reference Interpreter::get_reference(const FlyString& name) return { Reference::GlobalVariable, name }; } -Symbol* Interpreter::get_global_symbol(const String& description) -{ - auto result = m_global_symbol_map.get(description); - if (result.has_value()) - return result.value(); - - auto new_global_symbol = js_symbol(*this, description, true); - m_global_symbol_map.set(description, new_global_symbol); - return new_global_symbol; -} - void Interpreter::gather_roots(HashTable& roots) { if (m_last_value.is_cell()) @@ -237,14 +222,6 @@ void Interpreter::gather_roots(HashTable& roots) } roots.set(call_frame.environment); } - -#define __JS_ENUMERATE(SymbolName, snake_name) \ - roots.set(well_known_symbol_##snake_name()); - JS_ENUMERATE_WELL_KNOWN_SYMBOLS -#undef __JS_ENUMERATE - - for (auto& symbol : m_global_symbol_map) - roots.set(symbol.value); } Value Interpreter::call_internal(Function& function, Value this_value, Optional arguments) diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index a27abff4b52c9a65d48b3e7f9891ce8e679b928d..3c8d64b572e0412e7474b8d5329956479be2d076 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -134,8 +134,6 @@ public: Reference get_reference(const FlyString& name); - Symbol* get_global_symbol(const String& description); - void gather_roots(HashTable&); void enter_scope(const ScopeNode&, ArgumentVector, ScopeType, GlobalObject&); @@ -228,11 +226,6 @@ public: const LexicalEnvironment* get_this_environment() const; Value get_new_target() const; -#define __JS_ENUMERATE(SymbolName, snake_name) \ - Symbol* well_known_symbol_##snake_name() const { return m_well_known_symbol_##snake_name; } - JS_ENUMERATE_WELL_KNOWN_SYMBOLS -#undef __JS_ENUMERATE - private: explicit Interpreter(VM&); @@ -253,13 +246,6 @@ private: bool m_underscore_is_last_value { false }; Console m_console; - - HashMap m_global_symbol_map; - -#define __JS_ENUMERATE(SymbolName, snake_name) \ - Symbol* m_well_known_symbol_##snake_name { nullptr }; - JS_ENUMERATE_WELL_KNOWN_SYMBOLS -#undef __JS_ENUMERATE }; template<> diff --git a/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp b/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp index fd10aec49c90afe65cafecb1cb5ee5cc78a9c2f9..7e50923b96fb0237f3953bdc6d0429a21d55fd4e 100644 --- a/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp @@ -43,7 +43,7 @@ void ArrayIteratorPrototype::initialize(GlobalObject& global_object) Object::initialize(global_object); define_native_function("next", next, 0, Attribute::Configurable | Attribute::Writable); - define_property(global_object.interpreter().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Array Iterator"), Attribute::Configurable); + define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Array Iterator"), Attribute::Configurable); } ArrayIteratorPrototype::~ArrayIteratorPrototype() diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 9cd764ea9cfa84df7a258cc7399c01ead5095aba..d30eff31d2c5e5fdd6893d624e6f7ddf461b815c 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -80,7 +80,7 @@ void ArrayPrototype::initialize(GlobalObject& global_object) // Use define_property here instead of define_native_function so that // Object.is(Array.prototype[Symbol.iterator], Array.prototype.values) // evaluates to true - define_property(global_object.interpreter().well_known_symbol_iterator(), get("values"), attr); + define_property(global_object.vm().well_known_symbol_iterator(), get("values"), attr); } ArrayPrototype::~ArrayPrototype() diff --git a/Libraries/LibJS/Runtime/BigIntPrototype.cpp b/Libraries/LibJS/Runtime/BigIntPrototype.cpp index f2fc0f380dcfc721799ebe3c90fd395627c41f7e..eb2ddc0c1703102655838c96964bef91ef991616 100644 --- a/Libraries/LibJS/Runtime/BigIntPrototype.cpp +++ b/Libraries/LibJS/Runtime/BigIntPrototype.cpp @@ -45,7 +45,7 @@ void BigIntPrototype::initialize(GlobalObject& global_object) define_native_function("toLocaleString", to_locale_string, 0, attr); define_native_function("valueOf", value_of, 0, attr); - define_property(global_object.interpreter().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "BigInt"), Attribute::Configurable); + define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "BigInt"), Attribute::Configurable); } BigIntPrototype::~BigIntPrototype() diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index f1bab379d51739a3163c52e763fd4b6867adb6f4..f96fbcece7b8bf241b8b2e65bf1f326bcb04f557 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -51,7 +51,7 @@ void FunctionPrototype::initialize(GlobalObject& global_object) define_native_function("bind", bind, 1, attr); define_native_function("call", call, 1, attr); define_native_function("toString", to_string, 0, attr); - define_native_function(global_object.interpreter().well_known_symbol_has_instance(), symbol_has_instance, 1, 0); + define_native_function(global_object.vm().well_known_symbol_has_instance(), symbol_has_instance, 1, 0); define_property("length", Value(0), Attribute::Configurable); define_property("name", js_string(heap(), ""), Attribute::Configurable); } diff --git a/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Libraries/LibJS/Runtime/IteratorOperations.cpp index 553aaf04fb3747f77b616ad8a9968e9a654afd73..1cb821577e7f975275c28684e3ea04d34ecd4b39 100644 --- a/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -41,7 +41,7 @@ Object* get_iterator(GlobalObject& global_object, Value value, String hint, Valu auto object = value.to_object(interpreter, global_object); if (!object) return {}; - method = object->get(interpreter.well_known_symbol_iterator()); + method = object->get(global_object.vm().well_known_symbol_iterator()); if (interpreter.exception()) return {}; } diff --git a/Libraries/LibJS/Runtime/IteratorPrototype.cpp b/Libraries/LibJS/Runtime/IteratorPrototype.cpp index 32a1ca89b651b81574fee441c594cd7a6e565cfb..56e275aecef4f9d6e59570ac29525ae1a87cb400 100644 --- a/Libraries/LibJS/Runtime/IteratorPrototype.cpp +++ b/Libraries/LibJS/Runtime/IteratorPrototype.cpp @@ -37,7 +37,7 @@ IteratorPrototype::IteratorPrototype(GlobalObject& global_object) void IteratorPrototype::initialize(GlobalObject& global_object) { Object::initialize(global_object); - define_native_function(global_object.interpreter().well_known_symbol_iterator(), symbol_iterator, 0, Attribute::Writable | Attribute::Enumerable); + define_native_function(global_object.vm().well_known_symbol_iterator(), symbol_iterator, 0, Attribute::Writable | Attribute::Enumerable); } IteratorPrototype::~IteratorPrototype() diff --git a/Libraries/LibJS/Runtime/JSONObject.cpp b/Libraries/LibJS/Runtime/JSONObject.cpp index 841565e5da704cfecb6fe7a94f970ad9b51f42f0..183e814bbe288ffa7862a336357fdcf4e6972c28 100644 --- a/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Libraries/LibJS/Runtime/JSONObject.cpp @@ -49,7 +49,7 @@ void JSONObject::initialize(GlobalObject& global_object) define_native_function("stringify", stringify, 3, attr); define_native_function("parse", parse, 2, attr); - define_property(global_object.interpreter().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "JSON"), Attribute::Configurable); + define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "JSON"), Attribute::Configurable); } JSONObject::~JSONObject() diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index c166f49badfa28dd92b0ad64950e9a67e6d664eb..afc9f1ea6f212872822019e8b0ff8b52cfa1a105 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -75,7 +75,7 @@ void MathObject::initialize(GlobalObject& global_object) define_property("SQRT1_2", Value(M_SQRT1_2), 0); define_property("SQRT2", Value(M_SQRT2), 0); - define_property(global_object.interpreter().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Math"), Attribute::Configurable); + define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Math"), Attribute::Configurable); } MathObject::~MathObject() diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp index a1ce65436d4afeb384e0d87d606c7055bc65e45d..1d94f69a9167189c7d2f93d594adfe57e7d0ecea 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -80,7 +80,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_string) return {}; String tag; - auto to_string_tag = this_object->get(global_object.interpreter().well_known_symbol_to_string_tag()); + auto to_string_tag = this_object->get(global_object.vm().well_known_symbol_to_string_tag()); if (to_string_tag.is_string()) { tag = to_string_tag.as_string().string(); diff --git a/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp b/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp index 858601f68dcc3ac1740e6a3338828c802dcb1877..7d3dd4575570303648e42fa72007ab8e5ae94e36 100644 --- a/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringIteratorPrototype.cpp @@ -43,7 +43,7 @@ void StringIteratorPrototype::initialize(GlobalObject& global_object) Object::initialize(global_object); define_native_function("next", next, 0, Attribute::Configurable | Attribute::Writable); - define_property(global_object.interpreter().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "String Iterator"), Attribute::Configurable); + define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "String Iterator"), Attribute::Configurable); } StringIteratorPrototype::~StringIteratorPrototype() diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 2b66771df083267aeab7c6b81f72a3bbabc3c439..6944ec9a61b34196efbb92e622c63b3345c54dd9 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -89,7 +89,7 @@ void StringPrototype::initialize(GlobalObject& global_object) define_native_function("includes", includes, 1, attr); define_native_function("slice", slice, 2, attr); define_native_function("lastIndexOf", last_index_of, 1, attr); - define_native_function(global_object.interpreter().well_known_symbol_iterator(), symbol_iterator, 0, attr); + define_native_function(global_object.vm().well_known_symbol_iterator(), symbol_iterator, 0, attr); } StringPrototype::~StringPrototype() diff --git a/Libraries/LibJS/Runtime/Symbol.cpp b/Libraries/LibJS/Runtime/Symbol.cpp index c4fa0f3b8e555a319d563aedfcf9399e406f2226..2f2bde0e73f772b3e8f84a632a5987579b6c7117 100644 --- a/Libraries/LibJS/Runtime/Symbol.cpp +++ b/Libraries/LibJS/Runtime/Symbol.cpp @@ -45,6 +45,11 @@ Symbol* js_symbol(Heap& heap, String description, bool is_global) return heap.allocate_without_global_object(move(description), is_global); } +Symbol* js_symbol(VM& vm, String description, bool is_global) +{ + return js_symbol(vm.heap(), move(description), is_global); +} + Symbol* js_symbol(Interpreter& interpreter, String description, bool is_global) { return js_symbol(interpreter.heap(), description, is_global); diff --git a/Libraries/LibJS/Runtime/Symbol.h b/Libraries/LibJS/Runtime/Symbol.h index c576793815573a6c94c1065563df9b1bc5b040d0..2b829e4a65209fba4ef1640f4bf7b8a7af45d0f7 100644 --- a/Libraries/LibJS/Runtime/Symbol.h +++ b/Libraries/LibJS/Runtime/Symbol.h @@ -52,5 +52,6 @@ private: Symbol* js_symbol(Heap&, String description, bool is_global); Symbol* js_symbol(Interpreter&, String description, bool is_global); +Symbol* js_symbol(VM&, String description, bool is_global); } diff --git a/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Libraries/LibJS/Runtime/SymbolConstructor.cpp index 7cb4693d9adbc0cf433e47992922e84926c96ae9..47ff1283b246a61aee5b23f66f8cc5724d3558af 100644 --- a/Libraries/LibJS/Runtime/SymbolConstructor.cpp +++ b/Libraries/LibJS/Runtime/SymbolConstructor.cpp @@ -47,7 +47,7 @@ void SymbolConstructor::initialize(GlobalObject& global_object) define_native_function("keyFor", key_for, 1, Attribute::Writable | Attribute::Configurable); #define __JS_ENUMERATE(SymbolName, snake_name) \ - define_property(#SymbolName, global_object.interpreter().well_known_symbol_##snake_name(), 0); + define_property(#SymbolName, global_object.vm().well_known_symbol_##snake_name(), 0); JS_ENUMERATE_WELL_KNOWN_SYMBOLS #undef __JS_ENUMERATE } @@ -78,7 +78,7 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_) description = interpreter.argument(0).to_string(interpreter); } - return interpreter.get_global_symbol(description); + return global_object.vm().get_global_symbol(description); } JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for) diff --git a/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Libraries/LibJS/Runtime/SymbolPrototype.cpp index e28d6c7319f0aa7bf5060dceab78b4dd2eb2b150..78cfff84e1780a04816351960bc9a613663e5218 100644 --- a/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -51,7 +51,7 @@ void SymbolPrototype::initialize(GlobalObject& global_object) define_native_function("toString", to_string, 0, Attribute::Writable | Attribute::Configurable); define_native_function("valueOf", value_of, 0, Attribute::Writable | Attribute::Configurable); - define_property(global_object.interpreter().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Symbol"), Attribute::Configurable); + define_property(global_object.vm().well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Symbol"), Attribute::Configurable); } SymbolPrototype::~SymbolPrototype() diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp index a6c684b8013998092fb25c4c47593ea161ed2794..e35cd5cf98af21f7913904691cf01d3ada154947 100644 --- a/Libraries/LibJS/Runtime/VM.cpp +++ b/Libraries/LibJS/Runtime/VM.cpp @@ -25,6 +25,7 @@ */ #include +#include #include namespace JS { @@ -37,6 +38,10 @@ NonnullRefPtr VM::create() VM::VM() : m_heap(*this) { +#define __JS_ENUMERATE(SymbolName, snake_name) \ + m_well_known_symbol_##snake_name = js_symbol(*this, "Symbol." #SymbolName, false); + JS_ENUMERATE_WELL_KNOWN_SYMBOLS +#undef __JS_ENUMERATE } VM::~VM() @@ -85,6 +90,25 @@ void VM::gather_roots(HashTable& roots) roots.set(m_exception); for (auto* interpreter : m_interpreters) interpreter->gather_roots(roots); + +#define __JS_ENUMERATE(SymbolName, snake_name) \ + roots.set(well_known_symbol_##snake_name()); + JS_ENUMERATE_WELL_KNOWN_SYMBOLS +#undef __JS_ENUMERATE + + for (auto& symbol : m_global_symbol_map) + roots.set(symbol.value); +} + +Symbol* VM::get_global_symbol(const String& description) +{ + auto result = m_global_symbol_map.get(description); + if (result.has_value()) + return result.value(); + + auto new_global_symbol = js_symbol(*this, description, true); + m_global_symbol_map.set(description, new_global_symbol); + return new_global_symbol; } } diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h index 0cef4ead9addd42dee4e8d19edffadd0f72ef17a..26de0457815ae807ffe37b17945d30185453ca9c 100644 --- a/Libraries/LibJS/Runtime/VM.h +++ b/Libraries/LibJS/Runtime/VM.h @@ -26,6 +26,7 @@ #pragma once +#include #include #include @@ -63,6 +64,13 @@ public: void gather_roots(HashTable&); +#define __JS_ENUMERATE(SymbolName, snake_name) \ + Symbol* well_known_symbol_##snake_name() const { return m_well_known_symbol_##snake_name; } + JS_ENUMERATE_WELL_KNOWN_SYMBOLS +#undef __JS_ENUMERATE + + Symbol* get_global_symbol(const String& description); + private: VM(); @@ -70,6 +78,13 @@ private: Heap m_heap; Vector m_interpreters; + + HashMap m_global_symbol_map; + +#define __JS_ENUMERATE(SymbolName, snake_name) \ + Symbol* m_well_known_symbol_##snake_name { nullptr }; + JS_ENUMERATE_WELL_KNOWN_SYMBOLS +#undef __JS_ENUMERATE }; } diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index b0481908455162df99630746f26ca7e9cc99f0fd..e637362a20db4b7b4d871284906c323c9c06add1 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -702,7 +702,7 @@ Value instance_of(Interpreter& interpreter, Value lhs, Value rhs) interpreter.throw_exception(ErrorType::NotAnObject, rhs.to_string_without_side_effects().characters()); return {}; } - auto has_instance_method = rhs.as_object().get(interpreter.well_known_symbol_has_instance()); + auto has_instance_method = rhs.as_object().get(interpreter.vm().well_known_symbol_has_instance()); if (!has_instance_method.is_empty()) { if (!has_instance_method.is_function()) { interpreter.throw_exception(ErrorType::NotAFunction, has_instance_method.to_string_without_side_effects().characters());