소스 검색

LibWeb: Move main thread JavaScript VM to its own file

Instead of being a weird little global function in DOM/Document.cpp,
you can now get the main thread JS VM via Bindings::main_thread_vm().
Andreas Kling 4 년 전
부모
커밋
342b787d1c

+ 42 - 0
Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp

@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <LibJS/Runtime/VM.h>
+#include <LibWeb/Bindings/MainThreadVM.h>
+
+namespace Web::Bindings {
+
+JS::VM& main_thread_vm()
+{
+    static RefPtr<JS::VM> vm;
+    if (!vm) {
+        vm = JS::VM::create();
+        vm->set_should_log_exceptions(true);
+    }
+    return *vm;
+}
+
+}

+ 35 - 0
Userland/Libraries/LibWeb/Bindings/MainThreadVM.h

@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <LibJS/Forward.h>
+
+namespace Web::Bindings {
+
+JS::VM& main_thread_vm();
+
+}

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -3,6 +3,7 @@ set(SOURCES
     Bindings/EventWrapperFactory.cpp
     Bindings/EventWrapperFactory.cpp
     Bindings/EventTargetWrapperFactory.cpp
     Bindings/EventTargetWrapperFactory.cpp
     Bindings/LocationObject.cpp
     Bindings/LocationObject.cpp
+    Bindings/MainThreadVM.cpp
     Bindings/NavigatorObject.cpp
     Bindings/NavigatorObject.cpp
     Bindings/NodeWrapperFactory.cpp
     Bindings/NodeWrapperFactory.cpp
     Bindings/ScriptExecutionContext.cpp
     Bindings/ScriptExecutionContext.cpp

+ 3 - 14
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * All rights reserved.
  * All rights reserved.
  *
  *
  * Redistribution and use in source and binary forms, with or without
  * Redistribution and use in source and binary forms, with or without
@@ -33,7 +33,7 @@
 #include <LibJS/Interpreter.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/Parser.h>
 #include <LibJS/Parser.h>
 #include <LibJS/Runtime/Function.h>
 #include <LibJS/Runtime/Function.h>
-#include <LibWeb/Bindings/DocumentWrapper.h>
+#include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/Bindings/WindowObject.h>
 #include <LibWeb/Bindings/WindowObject.h>
 #include <LibWeb/CSS/StyleResolver.h>
 #include <LibWeb/CSS/StyleResolver.h>
 #include <LibWeb/DOM/Comment.h>
 #include <LibWeb/DOM/Comment.h>
@@ -534,21 +534,10 @@ Color Document::visited_link_color() const
     return page()->palette().visited_link();
     return page()->palette().visited_link();
 }
 }
 
 
-JS::VM& main_thread_vm();
-JS::VM& main_thread_vm()
-{
-    static RefPtr<JS::VM> vm;
-    if (!vm) {
-        vm = JS::VM::create();
-        vm->set_should_log_exceptions(true);
-    }
-    return *vm;
-}
-
 JS::Interpreter& Document::interpreter()
 JS::Interpreter& Document::interpreter()
 {
 {
     if (!m_interpreter)
     if (!m_interpreter)
-        m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(main_thread_vm(), *m_window);
+        m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(Bindings::main_thread_vm(), *m_window);
     return *m_interpreter;
     return *m_interpreter;
 }
 }
 
 

+ 2 - 5
Userland/Services/WebContent/ClientConnection.cpp

@@ -30,6 +30,7 @@
 #include <LibGfx/SystemTheme.h>
 #include <LibGfx/SystemTheme.h>
 #include <LibJS/Heap/Heap.h>
 #include <LibJS/Heap/Heap.h>
 #include <LibJS/Runtime/VM.h>
 #include <LibJS/Runtime/VM.h>
+#include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/Dump.h>
 #include <LibWeb/Dump.h>
 #include <LibWeb/Layout/InitialContainingBlockBox.h>
 #include <LibWeb/Layout/InitialContainingBlockBox.h>
@@ -39,10 +40,6 @@
 #include <WebContent/WebContentClientEndpoint.h>
 #include <WebContent/WebContentClientEndpoint.h>
 #include <pthread.h>
 #include <pthread.h>
 
 
-namespace Web::DOM {
-extern JS::VM& main_thread_vm();
-}
-
 namespace WebContent {
 namespace WebContent {
 
 
 static HashMap<int, RefPtr<ClientConnection>> s_connections;
 static HashMap<int, RefPtr<ClientConnection>> s_connections;
@@ -197,7 +194,7 @@ void ClientConnection::handle(const Messages::WebContentServer::DebugRequest& me
     }
     }
 
 
     if (message.request() == "collect-garbage") {
     if (message.request() == "collect-garbage") {
-        ::Web::DOM::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
+        Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
     }
     }
 
 
     if (message.request() == "set-line-box-borders") {
     if (message.request() == "set-line-box-borders") {