Forráskód Böngészése

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.
Andreas Kling 5 éve
szülő
commit
8dc6416bba

+ 3 - 0
Libraries/LibJS/Interpreter.cpp

@@ -30,6 +30,7 @@
 #include <LibJS/Interpreter.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/NativeFunction.h>
 #include <LibJS/NativeFunction.h>
 #include <LibJS/Object.h>
 #include <LibJS/Object.h>
+#include <LibJS/StringPrototype.h>
 #include <LibJS/Value.h>
 #include <LibJS/Value.h>
 
 
 namespace JS {
 namespace JS {
@@ -38,6 +39,7 @@ Interpreter::Interpreter()
     : m_heap(*this)
     : m_heap(*this)
 {
 {
     m_global_object = heap().allocate<GlobalObject>();
     m_global_object = heap().allocate<GlobalObject>();
+    m_string_prototype = heap().allocate<StringPrototype>();
 }
 }
 
 
 Interpreter::~Interpreter()
 Interpreter::~Interpreter()
@@ -137,6 +139,7 @@ Value Interpreter::get_variable(const String& name)
 void Interpreter::collect_roots(Badge<Heap>, HashTable<Cell*>& roots)
 void Interpreter::collect_roots(Badge<Heap>, HashTable<Cell*>& roots)
 {
 {
     roots.set(m_global_object);
     roots.set(m_global_object);
+    roots.set(m_string_prototype);
 
 
     for (auto& scope : m_scope_stack) {
     for (auto& scope : m_scope_stack) {
         for (auto& it : scope.variables) {
         for (auto& it : scope.variables) {

+ 3 - 0
Libraries/LibJS/Interpreter.h

@@ -88,6 +88,8 @@ public:
         return m_this_stack.last();
         return m_this_stack.last();
     }
     }
 
 
+    Object* string_prototype() { return m_string_prototype; }
+
 private:
 private:
     Heap m_heap;
     Heap m_heap;
 
 
@@ -95,6 +97,7 @@ private:
     Vector<Value> m_this_stack;
     Vector<Value> m_this_stack;
 
 
     Object* m_global_object { nullptr };
     Object* m_global_object { nullptr };
+    Object* m_string_prototype { nullptr };
 };
 };
 
 
 }
 }

+ 2 - 1
Libraries/LibJS/StringObject.cpp

@@ -25,6 +25,7 @@
  */
  */
 
 
 #include <LibJS/Heap.h>
 #include <LibJS/Heap.h>
+#include <LibJS/Interpreter.h>
 #include <LibJS/PrimitiveString.h>
 #include <LibJS/PrimitiveString.h>
 #include <LibJS/StringObject.h>
 #include <LibJS/StringObject.h>
 #include <LibJS/StringPrototype.h>
 #include <LibJS/StringPrototype.h>
@@ -35,7 +36,7 @@ namespace JS {
 StringObject::StringObject(PrimitiveString* string)
 StringObject::StringObject(PrimitiveString* string)
     : m_string(string)
     : m_string(string)
 {
 {
-    set_prototype(heap().allocate<StringPrototype>());
+    set_prototype(interpreter().string_prototype());
     put("length", Value(static_cast<i32>(m_string->string().length())));
     put("length", Value(static_cast<i32>(m_string->string().length())));
 }
 }