| 
									
										
										
										
											2021-07-01 11:19:45 +04:30
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ScopeGuard.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Array.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/ArrayBuffer.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/BigInt.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/TypedArray.h>
 | 
					
						
							|  |  |  | #include <LibWasm/AbstractMachine/Interpreter.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/WindowObject.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebAssembly/WebAssemblyMemoryPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebAssembly/WebAssemblyObject.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Bindings { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WebAssemblyInstanceObject::WebAssemblyInstanceObject(JS::GlobalObject& global_object, size_t index) | 
					
						
							| 
									
										
										
										
											2021-07-01 13:41:15 +04:30
										 |  |  |     : Object(static_cast<Web::Bindings::WindowObject&>(global_object).ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype")) | 
					
						
							| 
									
										
										
										
											2021-07-01 11:19:45 +04:30
										 |  |  |     , m_index(index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Object::initialize(global_object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VERIFY(!m_exports_object); | 
					
						
							|  |  |  |     m_exports_object = create(global_object, nullptr); | 
					
						
							|  |  |  |     auto& instance = this->instance(); | 
					
						
							|  |  |  |     auto& cache = this->cache(); | 
					
						
							|  |  |  |     for (auto& export_ : instance.exports()) { | 
					
						
							|  |  |  |         export_.value().visit( | 
					
						
							|  |  |  |             [&](const Wasm::FunctionAddress& address) { | 
					
						
							|  |  |  |                 auto object = cache.function_instances.get(address); | 
					
						
							|  |  |  |                 if (!object.has_value()) { | 
					
						
							|  |  |  |                     object = create_native_function(address, export_.name(), global_object); | 
					
						
							|  |  |  |                     cache.function_instances.set(address, *object); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 m_exports_object->define_property(export_.name(), *object); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             [&](const Wasm::MemoryAddress& address) { | 
					
						
							|  |  |  |                 auto object = cache.memory_instances.get(address); | 
					
						
							|  |  |  |                 if (!object.has_value()) { | 
					
						
							|  |  |  |                     object = heap().allocate<Web::Bindings::WebAssemblyMemoryObject>(global_object, global_object, address); | 
					
						
							|  |  |  |                     cache.memory_instances.set(address, *object); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 m_exports_object->define_property(export_.name(), *object); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             [&](const auto&) { | 
					
						
							|  |  |  |                 // FIXME: Implement other exports!
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_exports_object->set_integrity_level(IntegrityLevel::Frozen); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WebAssemblyInstanceObject::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Object::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_exports_object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |