mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:32:44 +00:00 
			
		
		
		
	 3deb8e322f
			
		
	
	
		3deb8e322f
		
	
	
	
	
		
			
			It's now unused, and won't be safe to use once Web constructors and prototypes are lazily created.
		
			
				
	
	
		
			76 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/Forward.h>
 | |
| #include <AK/HashMap.h>
 | |
| #include <LibJS/Forward.h>
 | |
| #include <LibJS/Heap/Cell.h>
 | |
| #include <LibJS/Heap/Heap.h>
 | |
| #include <LibJS/Runtime/VM.h>
 | |
| #include <LibWeb/Bindings/HostDefined.h>
 | |
| 
 | |
| namespace Web::Bindings {
 | |
| 
 | |
| class Intrinsics final : public JS::Cell {
 | |
|     JS_CELL(Intrinsics, JS::Cell);
 | |
| 
 | |
| public:
 | |
|     Intrinsics(JS::Realm& realm)
 | |
|         : m_realm(realm)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     template<typename PrototypeType>
 | |
|     JS::Object& ensure_web_prototype(DeprecatedString const& class_name)
 | |
|     {
 | |
|         if (auto it = m_prototypes.find(class_name); it != m_prototypes.end())
 | |
|             return *it->value;
 | |
| 
 | |
|         create_web_prototype_and_constructor<PrototypeType>(*m_realm);
 | |
|         return *m_prototypes.find(class_name)->value;
 | |
|     }
 | |
| 
 | |
|     template<typename PrototypeType>
 | |
|     JS::NativeFunction& ensure_web_constructor(DeprecatedString const& class_name)
 | |
|     {
 | |
|         if (auto it = m_constructors.find(class_name); it != m_constructors.end())
 | |
|             return *it->value;
 | |
| 
 | |
|         create_web_prototype_and_constructor<PrototypeType>(*m_realm);
 | |
|         return *m_constructors.find(class_name)->value;
 | |
|     }
 | |
| 
 | |
| private:
 | |
|     virtual void visit_edges(JS::Cell::Visitor&) override;
 | |
| 
 | |
|     template<typename PrototypeType>
 | |
|     void create_web_prototype_and_constructor(JS::Realm& realm);
 | |
| 
 | |
|     HashMap<DeprecatedString, JS::NonnullGCPtr<JS::Object>> m_prototypes;
 | |
|     HashMap<DeprecatedString, JS::GCPtr<JS::NativeFunction>> m_constructors;
 | |
|     JS::NonnullGCPtr<JS::Realm> m_realm;
 | |
| };
 | |
| 
 | |
| [[nodiscard]] inline Intrinsics& host_defined_intrinsics(JS::Realm& realm)
 | |
| {
 | |
|     return *verify_cast<HostDefined>(realm.host_defined())->intrinsics;
 | |
| }
 | |
| 
 | |
| template<typename T>
 | |
| [[nodiscard]] JS::Object& ensure_web_prototype(JS::Realm& realm, DeprecatedString const& class_name)
 | |
| {
 | |
|     return host_defined_intrinsics(realm).ensure_web_prototype<T>(class_name);
 | |
| }
 | |
| 
 | |
| template<typename T>
 | |
| [[nodiscard]] JS::NativeFunction& ensure_web_constructor(JS::Realm& realm, DeprecatedString const& class_name)
 | |
| {
 | |
|     return host_defined_intrinsics(realm).ensure_web_constructor<T>(class_name);
 | |
| }
 | |
| 
 | |
| }
 |