From 8dc6416bbadfadec000ffeac6559caaeda14569b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 15 Mar 2020 15:11:13 +0100 Subject: LibJS: Use the same StringPrototype globally To make sure that everyone has the same instance of StringPrototype, hang a global prototype off of the Interpreter that can be fetched when constructing new StringObjects. --- Libraries/LibJS/Interpreter.cpp | 3 +++ Libraries/LibJS/Interpreter.h | 3 +++ Libraries/LibJS/StringObject.cpp | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'Libraries') diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index f08839d5d2..903f7fbc9f 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include namespace JS { @@ -38,6 +39,7 @@ Interpreter::Interpreter() : m_heap(*this) { m_global_object = heap().allocate(); + m_string_prototype = heap().allocate(); } Interpreter::~Interpreter() @@ -137,6 +139,7 @@ Value Interpreter::get_variable(const String& name) void Interpreter::collect_roots(Badge, HashTable& roots) { roots.set(m_global_object); + roots.set(m_string_prototype); for (auto& scope : m_scope_stack) { for (auto& it : scope.variables) { diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index 79940cac3a..305e6a105e 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -88,6 +88,8 @@ public: return m_this_stack.last(); } + Object* string_prototype() { return m_string_prototype; } + private: Heap m_heap; @@ -95,6 +97,7 @@ private: Vector m_this_stack; Object* m_global_object { nullptr }; + Object* m_string_prototype { nullptr }; }; } diff --git a/Libraries/LibJS/StringObject.cpp b/Libraries/LibJS/StringObject.cpp index fbb1ec32f3..6eb09ff4f2 100644 --- a/Libraries/LibJS/StringObject.cpp +++ b/Libraries/LibJS/StringObject.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include #include @@ -35,7 +36,7 @@ namespace JS { StringObject::StringObject(PrimitiveString* string) : m_string(string) { - set_prototype(heap().allocate()); + set_prototype(interpreter().string_prototype()); put("length", Value(static_cast(m_string->string().length()))); } -- cgit v1.2.3