WebAssemblyObject.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "WebAssemblyInstanceObject.h"
  7. #include "WebAssemblyMemoryPrototype.h"
  8. #include "WebAssemblyModuleConstructor.h"
  9. #include "WebAssemblyModuleObject.h"
  10. #include "WebAssemblyModulePrototype.h"
  11. #include "WebAssemblyTableObject.h"
  12. #include "WebAssemblyTablePrototype.h"
  13. #include <AK/ScopeGuard.h>
  14. #include <LibJS/Runtime/Array.h>
  15. #include <LibJS/Runtime/ArrayBuffer.h>
  16. #include <LibJS/Runtime/BigInt.h>
  17. #include <LibJS/Runtime/DataView.h>
  18. #include <LibJS/Runtime/TypedArray.h>
  19. #include <LibWasm/AbstractMachine/Interpreter.h>
  20. #include <LibWasm/AbstractMachine/Validator.h>
  21. #include <LibWeb/Bindings/WindowObject.h>
  22. #include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
  23. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  24. namespace Web::Bindings {
  25. WebAssemblyObject::WebAssemblyObject(JS::GlobalObject& global_object)
  26. : Object(*global_object.object_prototype())
  27. {
  28. s_abstract_machine.enable_instruction_count_limit();
  29. }
  30. void WebAssemblyObject::initialize(JS::GlobalObject& global_object)
  31. {
  32. Object::initialize(global_object);
  33. u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
  34. define_native_function("validate", validate, 1, attr);
  35. define_native_function("compile", compile, 1, attr);
  36. define_native_function("instantiate", instantiate, 1, attr);
  37. auto& vm = global_object.vm();
  38. auto& window = static_cast<WindowObject&>(global_object);
  39. auto& memory_constructor = window.ensure_web_constructor<WebAssemblyMemoryConstructor>("WebAssembly.Memory");
  40. memory_constructor.define_direct_property(vm.names.name, js_string(vm, "WebAssembly.Memory"), JS::Attribute::Configurable);
  41. auto& memory_prototype = window.ensure_web_prototype<WebAssemblyMemoryPrototype>("WebAssemblyMemoryPrototype");
  42. memory_prototype.define_direct_property(vm.names.constructor, &memory_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  43. define_direct_property("Memory", &memory_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  44. auto& instance_constructor = window.ensure_web_constructor<WebAssemblyInstanceConstructor>("WebAssembly.Instance");
  45. instance_constructor.define_direct_property(vm.names.name, js_string(vm, "WebAssembly.Instance"), JS::Attribute::Configurable);
  46. auto& instance_prototype = window.ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype");
  47. instance_prototype.define_direct_property(vm.names.constructor, &instance_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  48. define_direct_property("Instance", &instance_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  49. auto& module_constructor = window.ensure_web_constructor<WebAssemblyModuleConstructor>("WebAssembly.Module");
  50. module_constructor.define_direct_property(vm.names.name, js_string(vm, "WebAssembly.Module"), JS::Attribute::Configurable);
  51. auto& module_prototype = window.ensure_web_prototype<WebAssemblyModulePrototype>("WebAssemblyModulePrototype");
  52. module_prototype.define_direct_property(vm.names.constructor, &module_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  53. define_direct_property("Module", &module_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  54. auto& table_constructor = window.ensure_web_constructor<WebAssemblyTableConstructor>("WebAssembly.Table");
  55. table_constructor.define_direct_property(vm.names.name, js_string(vm, "WebAssembly.Table"), JS::Attribute::Configurable);
  56. auto& table_prototype = window.ensure_web_prototype<WebAssemblyTablePrototype>("WebAssemblyTablePrototype");
  57. table_prototype.define_direct_property(vm.names.constructor, &table_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  58. define_direct_property("Table", &table_constructor, JS::Attribute::Writable | JS::Attribute::Configurable);
  59. }
  60. NonnullOwnPtrVector<WebAssemblyObject::CompiledWebAssemblyModule> WebAssemblyObject::s_compiled_modules;
  61. NonnullOwnPtrVector<Wasm::ModuleInstance> WebAssemblyObject::s_instantiated_modules;
  62. Vector<WebAssemblyObject::ModuleCache> WebAssemblyObject::s_module_caches;
  63. WebAssemblyObject::GlobalModuleCache WebAssemblyObject::s_global_cache;
  64. Wasm::AbstractMachine WebAssemblyObject::s_abstract_machine;
  65. void WebAssemblyObject::visit_edges(Visitor& visitor)
  66. {
  67. Base::visit_edges(visitor);
  68. for (auto& entry : s_global_cache.function_instances)
  69. visitor.visit(entry.value);
  70. for (auto& module_cache : s_module_caches) {
  71. for (auto& entry : module_cache.function_instances)
  72. visitor.visit(entry.value);
  73. for (auto& entry : module_cache.memory_instances)
  74. visitor.visit(entry.value);
  75. }
  76. }
  77. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::validate)
  78. {
  79. // 1. Let stableBytes be a copy of the bytes held by the buffer bytes.
  80. // Note: There's no need to copy the bytes here as the buffer data cannot change while we're compiling the module.
  81. auto buffer = TRY(vm.argument(0).to_object(global_object));
  82. // 2. Compile stableBytes as a WebAssembly module and store the results as module.
  83. auto maybe_module = parse_module(global_object, buffer);
  84. // 3. If module is error, return false.
  85. if (maybe_module.is_error())
  86. return JS::Value(false);
  87. // Drop the module from the cache, we're never going to refer to it.
  88. ScopeGuard drop_from_cache {
  89. [&] {
  90. (void)s_compiled_modules.take_last();
  91. }
  92. };
  93. // 3 continued - our "compile" step is lazy with validation, explicitly do the validation.
  94. if (s_abstract_machine.validate(s_compiled_modules[maybe_module.value()].module).is_error())
  95. return JS::Value(false);
  96. // 4. Return true.
  97. return JS::Value(true);
  98. }
  99. JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::Object* buffer_object)
  100. {
  101. auto& vm = global_object.vm();
  102. ReadonlyBytes data;
  103. if (is<JS::ArrayBuffer>(buffer_object)) {
  104. auto& buffer = static_cast<JS::ArrayBuffer&>(*buffer_object);
  105. data = buffer.buffer();
  106. } else if (is<JS::TypedArrayBase>(buffer_object)) {
  107. auto& buffer = static_cast<JS::TypedArrayBase&>(*buffer_object);
  108. data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), buffer.byte_length());
  109. } else if (is<JS::DataView>(buffer_object)) {
  110. auto& buffer = static_cast<JS::DataView&>(*buffer_object);
  111. data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), buffer.byte_length());
  112. } else {
  113. return vm.throw_completion<JS::TypeError>(global_object, "Not a BufferSource");
  114. }
  115. InputMemoryStream stream { data };
  116. auto module_result = Wasm::Module::parse(stream);
  117. ScopeGuard drain_errors {
  118. [&] {
  119. stream.handle_any_error();
  120. }
  121. };
  122. if (module_result.is_error()) {
  123. // FIXME: Throw CompileError instead.
  124. return vm.throw_completion<JS::TypeError>(global_object, Wasm::parse_error_to_string(module_result.error()));
  125. }
  126. if (auto validation_result = WebAssemblyObject::s_abstract_machine.validate(module_result.value()); validation_result.is_error()) {
  127. // FIXME: Throw CompileError instead.
  128. return vm.throw_completion<JS::TypeError>(global_object, validation_result.error().error_string);
  129. }
  130. WebAssemblyObject::s_compiled_modules.append(make<WebAssemblyObject::CompiledWebAssemblyModule>(module_result.release_value()));
  131. return WebAssemblyObject::s_compiled_modules.size() - 1;
  132. }
  133. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::compile)
  134. {
  135. // FIXME: This shouldn't block!
  136. auto buffer_or_error = vm.argument(0).to_object(global_object);
  137. JS::Value rejection_value;
  138. if (buffer_or_error.is_error()) {
  139. rejection_value = *buffer_or_error.throw_completion().value();
  140. vm.clear_exception();
  141. }
  142. auto promise = JS::Promise::create(global_object);
  143. if (!rejection_value.is_empty()) {
  144. promise->reject(rejection_value);
  145. return promise;
  146. }
  147. auto* buffer = buffer_or_error.release_value();
  148. auto result = parse_module(global_object, buffer);
  149. if (result.is_error())
  150. promise->reject(*result.release_error().value());
  151. else
  152. promise->fulfill(vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value()));
  153. return promise;
  154. }
  155. JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module const& module, JS::VM& vm, JS::GlobalObject& global_object)
  156. {
  157. Wasm::Linker linker { module };
  158. HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
  159. auto import_argument = vm.argument(1);
  160. if (!import_argument.is_undefined()) {
  161. auto* import_object = TRY(import_argument.to_object(global_object));
  162. dbgln("Trying to resolve stuff because import object was specified");
  163. for (const Wasm::Linker::Name& import_name : linker.unresolved_imports()) {
  164. dbgln("Trying to resolve {}::{}", import_name.module, import_name.name);
  165. auto value_or_error = import_object->get(import_name.module);
  166. if (value_or_error.is_error())
  167. break;
  168. auto value = value_or_error.release_value();
  169. auto object_or_error = value.to_object(global_object);
  170. if (object_or_error.is_error())
  171. break;
  172. auto* object = object_or_error.release_value();
  173. auto import_or_error = object->get(import_name.name);
  174. if (import_or_error.is_error())
  175. break;
  176. auto import_ = import_or_error.release_value();
  177. TRY(import_name.type.visit(
  178. [&](Wasm::TypeIndex index) -> JS::ThrowCompletionOr<void> {
  179. dbgln("Trying to resolve a function {}::{}, type index {}", import_name.module, import_name.name, index.value());
  180. auto& type = module.type(index);
  181. // FIXME: IsCallable()
  182. if (!import_.is_function())
  183. return {};
  184. auto& function = import_.as_function();
  185. // FIXME: If this is a function created by create_native_function(),
  186. // just extract its address and resolve to that.
  187. Wasm::HostFunction host_function {
  188. [&](auto&, auto& arguments) -> Wasm::Result {
  189. JS::MarkedValueList argument_values { vm.heap() };
  190. for (auto& entry : arguments)
  191. argument_values.append(to_js_value(global_object, entry));
  192. auto result_or_error = vm.call(function, JS::js_undefined(), move(argument_values));
  193. if (result_or_error.is_error()) {
  194. vm.clear_exception();
  195. return Wasm::Trap();
  196. }
  197. if (type.results().is_empty())
  198. return Wasm::Result { Vector<Wasm::Value> {} };
  199. if (type.results().size() == 1) {
  200. auto value_or_error = to_webassembly_value(global_object, result_or_error.release_value(), type.results().first());
  201. if (value_or_error.is_error())
  202. return Wasm::Trap {};
  203. return Wasm::Result { Vector<Wasm::Value> { value_or_error.release_value() } };
  204. }
  205. // FIXME: Multiple returns
  206. TODO();
  207. },
  208. type
  209. };
  210. auto address = s_abstract_machine.store().allocate(move(host_function));
  211. dbgln("Resolved to {}", address->value());
  212. // FIXME: LinkError instead.
  213. VERIFY(address.has_value());
  214. resolved_imports.set(import_name, Wasm::ExternValue { Wasm::FunctionAddress { *address } });
  215. return {};
  216. },
  217. [&](Wasm::GlobalType const& type) -> JS::ThrowCompletionOr<void> {
  218. Optional<Wasm::GlobalAddress> address;
  219. // https://webassembly.github.io/spec/js-api/#read-the-imports step 5.1
  220. if (import_.is_number() || import_.is_bigint()) {
  221. if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) {
  222. // FIXME: Throw a LinkError instead.
  223. return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a Number to a BigInteger");
  224. }
  225. if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) {
  226. // FIXME: Throw a LinkError instead.
  227. return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a BigInteger to a Number");
  228. }
  229. auto cast_value = TRY(to_webassembly_value(global_object, import_, type.type()));
  230. address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value);
  231. } else {
  232. // FIXME: https://webassembly.github.io/spec/js-api/#read-the-imports step 5.2
  233. // if v implements Global
  234. // let globaladdr be v.[[Global]]
  235. // FIXME: Throw a LinkError instead
  236. return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Invalid value for global type");
  237. }
  238. resolved_imports.set(import_name, Wasm::ExternValue { *address });
  239. return {};
  240. },
  241. [&](Wasm::MemoryType const&) -> JS::ThrowCompletionOr<void> {
  242. if (!import_.is_object() || !is<WebAssemblyMemoryObject>(import_.as_object())) {
  243. // FIXME: Throw a LinkError instead
  244. return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Memory for a memory import");
  245. }
  246. auto address = static_cast<WebAssemblyMemoryObject const&>(import_.as_object()).address();
  247. resolved_imports.set(import_name, Wasm::ExternValue { address });
  248. return {};
  249. },
  250. [&](Wasm::TableType const&) -> JS::ThrowCompletionOr<void> {
  251. if (!import_.is_object() || !is<WebAssemblyTableObject>(import_.as_object())) {
  252. // FIXME: Throw a LinkError instead
  253. return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Table for a table import");
  254. }
  255. auto address = static_cast<WebAssemblyTableObject const&>(import_.as_object()).address();
  256. resolved_imports.set(import_name, Wasm::ExternValue { address });
  257. return {};
  258. },
  259. [&](const auto&) -> JS::ThrowCompletionOr<void> {
  260. // FIXME: Implement these.
  261. dbgln("Unimplemented import of non-function attempted");
  262. return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Not Implemented");
  263. }));
  264. }
  265. }
  266. linker.link(resolved_imports);
  267. auto link_result = linker.finish();
  268. if (link_result.is_error()) {
  269. // FIXME: Throw a LinkError.
  270. StringBuilder builder;
  271. builder.append("LinkError: Missing ");
  272. builder.join(' ', link_result.error().missing_imports);
  273. return vm.throw_completion<JS::TypeError>(global_object, builder.build());
  274. }
  275. auto instance_result = s_abstract_machine.instantiate(module, link_result.release_value());
  276. if (instance_result.is_error()) {
  277. // FIXME: Throw a LinkError instead.
  278. return vm.throw_completion<JS::TypeError>(global_object, instance_result.error().error);
  279. }
  280. s_instantiated_modules.append(instance_result.release_value());
  281. s_module_caches.empend();
  282. return s_instantiated_modules.size() - 1;
  283. }
  284. JS_DEFINE_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
  285. {
  286. // FIXME: This shouldn't block!
  287. auto buffer_or_error = vm.argument(0).to_object(global_object);
  288. auto promise = JS::Promise::create(global_object);
  289. bool should_return_module = false;
  290. if (buffer_or_error.is_error()) {
  291. auto rejection_value = *buffer_or_error.throw_completion().value();
  292. vm.clear_exception();
  293. promise->reject(rejection_value);
  294. return promise;
  295. }
  296. auto* buffer = buffer_or_error.release_value();
  297. const Wasm::Module* module { nullptr };
  298. if (is<JS::ArrayBuffer>(buffer) || is<JS::TypedArrayBase>(buffer)) {
  299. auto result = parse_module(global_object, buffer);
  300. if (result.is_error()) {
  301. promise->reject(*result.release_error().value());
  302. return promise;
  303. }
  304. module = &WebAssemblyObject::s_compiled_modules.at(result.release_value()).module;
  305. should_return_module = true;
  306. } else if (is<WebAssemblyModuleObject>(buffer)) {
  307. module = &static_cast<WebAssemblyModuleObject*>(buffer)->module();
  308. } else {
  309. auto error = JS::TypeError::create(global_object, String::formatted("{} is not an ArrayBuffer or a Module", buffer->class_name()));
  310. promise->reject(error);
  311. return promise;
  312. }
  313. VERIFY(module);
  314. auto result = instantiate_module(*module, vm, global_object);
  315. if (result.is_error()) {
  316. promise->reject(*result.release_error().value());
  317. } else {
  318. auto instance_object = vm.heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.release_value());
  319. if (should_return_module) {
  320. auto object = JS::Object::create(global_object, nullptr);
  321. object->define_direct_property("module", vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, s_compiled_modules.size() - 1), JS::default_attributes);
  322. object->define_direct_property("instance", instance_object, JS::default_attributes);
  323. promise->fulfill(object);
  324. } else {
  325. promise->fulfill(instance_object);
  326. }
  327. }
  328. return promise;
  329. }
  330. JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value)
  331. {
  332. switch (wasm_value.type().kind()) {
  333. case Wasm::ValueType::I64:
  334. return global_object.heap().allocate<JS::BigInt>(global_object, ::Crypto::SignedBigInteger::create_from(wasm_value.to<i64>().value()));
  335. case Wasm::ValueType::I32:
  336. return JS::Value(wasm_value.to<i32>().value());
  337. case Wasm::ValueType::F64:
  338. return JS::Value(wasm_value.to<double>().value());
  339. case Wasm::ValueType::F32:
  340. return JS::Value(static_cast<double>(wasm_value.to<float>().value()));
  341. case Wasm::ValueType::FunctionReference:
  342. // FIXME: What's the name of a function reference that isn't exported?
  343. return create_native_function(global_object, wasm_value.to<Wasm::Reference::Func>().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled");
  344. case Wasm::ValueType::NullFunctionReference:
  345. return JS::js_null();
  346. case Wasm::ValueType::ExternReference:
  347. case Wasm::ValueType::NullExternReference:
  348. TODO();
  349. }
  350. VERIFY_NOT_REACHED();
  351. }
  352. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, const Wasm::ValueType& type)
  353. {
  354. static ::Crypto::SignedBigInteger two_64 = "1"_sbigint.shift_left(64);
  355. auto& vm = global_object.vm();
  356. switch (type.kind()) {
  357. case Wasm::ValueType::I64: {
  358. auto bigint = TRY(value.to_bigint(global_object));
  359. auto value = bigint->big_integer().divided_by(two_64).remainder;
  360. VERIFY(value.unsigned_value().trimmed_length() <= 2);
  361. i64 integer = static_cast<i64>(value.unsigned_value().to_u64());
  362. if (value.is_negative())
  363. integer = -integer;
  364. return Wasm::Value { integer };
  365. }
  366. case Wasm::ValueType::I32: {
  367. auto _i32 = TRY(value.to_i32(global_object));
  368. return Wasm::Value { static_cast<i32>(_i32) };
  369. }
  370. case Wasm::ValueType::F64: {
  371. auto number = TRY(value.to_double(global_object));
  372. return Wasm::Value { static_cast<double>(number) };
  373. }
  374. case Wasm::ValueType::F32: {
  375. auto number = TRY(value.to_double(global_object));
  376. return Wasm::Value { static_cast<float>(number) };
  377. }
  378. case Wasm::ValueType::FunctionReference:
  379. case Wasm::ValueType::NullFunctionReference: {
  380. if (value.is_null())
  381. return Wasm::Value { Wasm::ValueType(Wasm::ValueType::NullExternReference), 0ull };
  382. if (value.is_function()) {
  383. auto& function = value.as_function();
  384. for (auto& entry : WebAssemblyObject::s_global_cache.function_instances) {
  385. if (entry.value == &function)
  386. return Wasm::Value { Wasm::Reference { Wasm::Reference::Func { entry.key } } };
  387. }
  388. }
  389. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Exported function");
  390. }
  391. case Wasm::ValueType::ExternReference:
  392. case Wasm::ValueType::NullExternReference:
  393. TODO();
  394. }
  395. VERIFY_NOT_REACHED();
  396. }
  397. JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name)
  398. {
  399. Optional<Wasm::FunctionType> type;
  400. WebAssemblyObject::s_abstract_machine.store().get(address)->visit([&](const auto& value) { type = value.type(); });
  401. if (auto entry = WebAssemblyObject::s_global_cache.function_instances.get(address); entry.has_value())
  402. return *entry;
  403. auto function = JS::NativeFunction::create(
  404. global_object,
  405. name,
  406. [address, type = type.release_value()](JS::VM& vm, JS::GlobalObject& global_object) -> JS::ThrowCompletionOr<JS::Value> {
  407. Vector<Wasm::Value> values;
  408. values.ensure_capacity(type.parameters().size());
  409. // Grab as many values as needed and convert them.
  410. size_t index = 0;
  411. for (auto& type : type.parameters())
  412. values.append(TRY(to_webassembly_value(global_object, vm.argument(index++), type)));
  413. auto result = WebAssemblyObject::s_abstract_machine.invoke(address, move(values));
  414. // FIXME: Use the convoluted mapping of errors defined in the spec.
  415. if (result.is_trap())
  416. return vm.throw_completion<JS::TypeError>(global_object, String::formatted("Wasm execution trapped (WIP): {}", result.trap().reason));
  417. if (result.values().is_empty())
  418. return JS::js_undefined();
  419. if (result.values().size() == 1)
  420. return to_js_value(global_object, result.values().first());
  421. Vector<JS::Value> result_values;
  422. for (auto& entry : result.values())
  423. result_values.append(to_js_value(global_object, entry));
  424. return JS::Value(JS::Array::create_from(global_object, result_values));
  425. });
  426. WebAssemblyObject::s_global_cache.function_instances.set(address, function);
  427. return function;
  428. }
  429. WebAssemblyMemoryObject::WebAssemblyMemoryObject(JS::GlobalObject& global_object, Wasm::MemoryAddress address)
  430. : Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<WebAssemblyMemoryPrototype>("WebAssemblyMemoryPrototype"))
  431. , m_address(address)
  432. {
  433. }
  434. }