mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 10:52:44 +00:00 
			
		
		
		
	 40a70461a0
			
		
	
	
		40a70461a0
		
	
	
	
	
		
			
			Similar to create() in LibJS, wrap() et al. are on a low enough level to warrant passing a Realm directly instead of relying on the current realm from the VM, as a wrapper may need to be allocated while no JS is being executed.
		
			
				
	
	
		
			37 lines
		
	
	
	
		
			829 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
	
		
			829 B
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/WeakPtr.h>
 | |
| #include <LibJS/Heap/Heap.h>
 | |
| #include <LibJS/Runtime/GlobalObject.h>
 | |
| #include <LibWeb/Forward.h>
 | |
| 
 | |
| namespace Web::Bindings {
 | |
| 
 | |
| class Wrappable {
 | |
| public:
 | |
|     virtual ~Wrappable() = default;
 | |
| 
 | |
|     void set_wrapper(Wrapper&);
 | |
|     Wrapper* wrapper() { return m_wrapper; }
 | |
|     Wrapper const* wrapper() const { return m_wrapper; }
 | |
| 
 | |
| private:
 | |
|     WeakPtr<Wrapper> m_wrapper;
 | |
| };
 | |
| 
 | |
| template<class NativeObject>
 | |
| inline Wrapper* wrap_impl(JS::Realm& realm, NativeObject& native_object)
 | |
| {
 | |
|     if (!native_object.wrapper()) {
 | |
|         native_object.set_wrapper(*realm.heap().allocate<typename NativeObject::WrapperType>(realm, realm, native_object));
 | |
|     }
 | |
|     return native_object.wrapper();
 | |
| }
 | |
| 
 | |
| }
 |