WebAssembly.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/MemoryStream.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibJS/Runtime/Array.h>
  11. #include <LibJS/Runtime/ArrayBuffer.h>
  12. #include <LibJS/Runtime/BigInt.h>
  13. #include <LibJS/Runtime/DataView.h>
  14. #include <LibJS/Runtime/Iterator.h>
  15. #include <LibJS/Runtime/NativeFunction.h>
  16. #include <LibJS/Runtime/Object.h>
  17. #include <LibJS/Runtime/Promise.h>
  18. #include <LibJS/Runtime/TypedArray.h>
  19. #include <LibJS/Runtime/VM.h>
  20. #include <LibWasm/AbstractMachine/Validator.h>
  21. #include <LibWeb/WebAssembly/Instance.h>
  22. #include <LibWeb/WebAssembly/Memory.h>
  23. #include <LibWeb/WebAssembly/Module.h>
  24. #include <LibWeb/WebAssembly/Table.h>
  25. #include <LibWeb/WebAssembly/WebAssembly.h>
  26. #include <LibWeb/WebIDL/Buffers.h>
  27. namespace Web::WebAssembly {
  28. namespace Detail {
  29. HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
  30. WebAssemblyCache& get_cache(JS::Realm& realm)
  31. {
  32. return s_caches.ensure(realm.global_object());
  33. }
  34. }
  35. void visit_edges(JS::Object& object, JS::Cell::Visitor& visitor)
  36. {
  37. auto& global_object = HTML::relevant_global_object(object);
  38. if (auto maybe_cache = Detail::s_caches.get(global_object); maybe_cache.has_value()) {
  39. auto& cache = maybe_cache.release_value();
  40. visitor.visit(cache.function_instances());
  41. visitor.visit(cache.imported_objects());
  42. visitor.visit(cache.extern_values());
  43. }
  44. }
  45. void finalize(JS::Object& object)
  46. {
  47. auto& global_object = HTML::relevant_global_object(object);
  48. Detail::s_caches.remove(global_object);
  49. }
  50. // https://webassembly.github.io/spec/js-api/#dom-webassembly-validate
  51. bool validate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
  52. {
  53. // 1. Let stableBytes be a copy of the bytes held by the buffer bytes.
  54. // Note: There's no need to copy the bytes here as the buffer data cannot change while we're compiling the module.
  55. // 2. Compile stableBytes as a WebAssembly module and store the results as module.
  56. auto module_or_error = Detail::parse_module(vm, bytes->raw_object());
  57. // 3. If module is error, return false.
  58. if (module_or_error.is_error())
  59. return false;
  60. // 3 continued - our "compile" step is lazy with validation, explicitly do the validation.
  61. auto compiled_module = module_or_error.release_value();
  62. auto& cache = Detail::get_cache(*vm.current_realm());
  63. if (cache.abstract_machine().validate(compiled_module->module).is_error())
  64. return false;
  65. // 4. Return true.
  66. return true;
  67. }
  68. // https://webassembly.github.io/spec/js-api/#dom-webassembly-compile
  69. WebIDL::ExceptionOr<JS::Value> compile(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes)
  70. {
  71. auto& realm = *vm.current_realm();
  72. // FIXME: This shouldn't block!
  73. auto compiled_module_or_error = Detail::parse_module(vm, bytes->raw_object());
  74. auto promise = JS::Promise::create(realm);
  75. if (compiled_module_or_error.is_error()) {
  76. promise->reject(*compiled_module_or_error.release_error().value());
  77. } else {
  78. auto module_object = vm.heap().allocate<Module>(realm, realm, compiled_module_or_error.release_value());
  79. promise->fulfill(module_object);
  80. }
  81. return promise;
  82. }
  83. // https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate
  84. WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object)
  85. {
  86. // FIXME: Implement the importObject parameter.
  87. (void)import_object;
  88. auto& realm = *vm.current_realm();
  89. // FIXME: This shouldn't block!
  90. auto compiled_module_or_error = Detail::parse_module(vm, bytes->raw_object());
  91. auto promise = JS::Promise::create(realm);
  92. if (compiled_module_or_error.is_error()) {
  93. promise->reject(*compiled_module_or_error.release_error().value());
  94. return promise;
  95. }
  96. auto compiled_module = compiled_module_or_error.release_value();
  97. auto result = Detail::instantiate_module(vm, compiled_module->module);
  98. if (result.is_error()) {
  99. promise->reject(*result.release_error().value());
  100. } else {
  101. auto module_object = vm.heap().allocate<Module>(realm, realm, move(compiled_module));
  102. auto instance_object = vm.heap().allocate<Instance>(realm, realm, result.release_value());
  103. auto object = JS::Object::create(realm, nullptr);
  104. object->define_direct_property("module", module_object, JS::default_attributes);
  105. object->define_direct_property("instance", instance_object, JS::default_attributes);
  106. promise->fulfill(object);
  107. }
  108. return promise;
  109. }
  110. // https://webassembly.github.io/spec/js-api/#dom-webassembly-instantiate-moduleobject-importobject
  111. WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM& vm, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object)
  112. {
  113. // FIXME: Implement the importObject parameter.
  114. (void)import_object;
  115. auto& realm = *vm.current_realm();
  116. auto promise = JS::Promise::create(realm);
  117. auto const& compiled_module = module_object.compiled_module();
  118. auto result = Detail::instantiate_module(vm, compiled_module->module);
  119. if (result.is_error()) {
  120. promise->reject(*result.release_error().value());
  121. } else {
  122. auto instance_object = vm.heap().allocate<Instance>(realm, realm, result.release_value());
  123. promise->fulfill(instance_object);
  124. }
  125. return promise;
  126. }
  127. namespace Detail {
  128. JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM& vm, Wasm::Module const& module)
  129. {
  130. Wasm::Linker linker { module };
  131. HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
  132. auto import_argument = vm.argument(1);
  133. auto& cache = get_cache(*vm.current_realm());
  134. if (!import_argument.is_undefined()) {
  135. auto import_object = TRY(import_argument.to_object(vm));
  136. dbgln("Trying to resolve stuff because import object was specified");
  137. for (Wasm::Linker::Name const& import_name : linker.unresolved_imports()) {
  138. dbgln("Trying to resolve {}::{}", import_name.module, import_name.name);
  139. auto value_or_error = import_object->get(import_name.module);
  140. if (value_or_error.is_error())
  141. break;
  142. auto value = value_or_error.release_value();
  143. auto object_or_error = value.to_object(vm);
  144. if (object_or_error.is_error())
  145. break;
  146. auto object = object_or_error.release_value();
  147. auto import_or_error = object->get(import_name.name);
  148. if (import_or_error.is_error())
  149. break;
  150. auto import_ = import_or_error.release_value();
  151. TRY(import_name.type.visit(
  152. [&](Wasm::TypeIndex index) -> JS::ThrowCompletionOr<void> {
  153. dbgln("Trying to resolve a function {}::{}, type index {}", import_name.module, import_name.name, index.value());
  154. auto& type = module.type_section().types()[index.value()];
  155. // FIXME: IsCallable()
  156. if (!import_.is_function())
  157. return {};
  158. auto& function = import_.as_function();
  159. cache.add_imported_object(function);
  160. // FIXME: If this is a function created by create_native_function(),
  161. // just extract its address and resolve to that.
  162. Wasm::HostFunction host_function {
  163. [&](auto&, auto& arguments) -> Wasm::Result {
  164. JS::MarkedVector<JS::Value> argument_values { vm.heap() };
  165. size_t index = 0;
  166. for (auto& entry : arguments) {
  167. argument_values.append(to_js_value(vm, entry, type.parameters()[index]));
  168. ++index;
  169. }
  170. auto result = TRY(JS::call(vm, function, JS::js_undefined(), argument_values.span()));
  171. if (type.results().is_empty())
  172. return Wasm::Result { Vector<Wasm::Value> {} };
  173. if (type.results().size() == 1)
  174. return Wasm::Result { Vector<Wasm::Value> { TRY(to_webassembly_value(vm, result, type.results().first())) } };
  175. auto method = TRY(result.get_method(vm, vm.names.iterator));
  176. if (method == JS::js_undefined())
  177. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotIterable, result.to_string_without_side_effects());
  178. auto values = TRY(JS::iterator_to_list(vm, TRY(JS::get_iterator_from_method(vm, result, *method))));
  179. if (values.size() != type.results().size())
  180. return vm.throw_completion<JS::TypeError>(ByteString::formatted("Invalid number of return values for multi-value wasm return of {} objects", type.results().size()));
  181. Vector<Wasm::Value> wasm_values;
  182. TRY_OR_THROW_OOM(vm, wasm_values.try_ensure_capacity(values.size()));
  183. size_t i = 0;
  184. for (auto& value : values)
  185. wasm_values.append(TRY(to_webassembly_value(vm, value, type.results()[i++])));
  186. return Wasm::Result { move(wasm_values) };
  187. },
  188. type,
  189. ByteString::formatted("func{}", resolved_imports.size()),
  190. };
  191. auto address = cache.abstract_machine().store().allocate(move(host_function));
  192. dbgln("Resolved to {}", address->value());
  193. // FIXME: LinkError instead.
  194. VERIFY(address.has_value());
  195. resolved_imports.set(import_name, Wasm::ExternValue { Wasm::FunctionAddress { *address } });
  196. return {};
  197. },
  198. [&](Wasm::GlobalType const& type) -> JS::ThrowCompletionOr<void> {
  199. Optional<Wasm::GlobalAddress> address;
  200. // https://webassembly.github.io/spec/js-api/#read-the-imports step 5.1
  201. if (import_.is_number() || import_.is_bigint()) {
  202. if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) {
  203. // FIXME: Throw a LinkError instead.
  204. return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a Number to a BigInteger"sv);
  205. }
  206. if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) {
  207. // FIXME: Throw a LinkError instead.
  208. return vm.throw_completion<JS::TypeError>("LinkError: Import resolution attempted to cast a BigInteger to a Number"sv);
  209. }
  210. auto cast_value = TRY(to_webassembly_value(vm, import_, type.type()));
  211. address = cache.abstract_machine().store().allocate({ type.type(), false }, cast_value);
  212. } else {
  213. // FIXME: https://webassembly.github.io/spec/js-api/#read-the-imports step 5.2
  214. // if v implements Global
  215. // let globaladdr be v.[[Global]]
  216. // FIXME: Throw a LinkError instead
  217. return vm.throw_completion<JS::TypeError>("LinkError: Invalid value for global type"sv);
  218. }
  219. resolved_imports.set(import_name, Wasm::ExternValue { *address });
  220. return {};
  221. },
  222. [&](Wasm::MemoryType const&) -> JS::ThrowCompletionOr<void> {
  223. if (!import_.is_object() || !is<WebAssembly::Memory>(import_.as_object())) {
  224. // FIXME: Throw a LinkError instead
  225. return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Memory for a memory import"sv);
  226. }
  227. auto address = static_cast<WebAssembly::Memory const&>(import_.as_object()).address();
  228. resolved_imports.set(import_name, Wasm::ExternValue { address });
  229. return {};
  230. },
  231. [&](Wasm::TableType const&) -> JS::ThrowCompletionOr<void> {
  232. if (!import_.is_object() || !is<WebAssembly::Table>(import_.as_object())) {
  233. // FIXME: Throw a LinkError instead
  234. return vm.throw_completion<JS::TypeError>("LinkError: Expected an instance of WebAssembly.Table for a table import"sv);
  235. }
  236. auto address = static_cast<WebAssembly::Table const&>(import_.as_object()).address();
  237. resolved_imports.set(import_name, Wasm::ExternValue { address });
  238. return {};
  239. },
  240. [&](auto const&) -> JS::ThrowCompletionOr<void> {
  241. // FIXME: Implement these.
  242. dbgln("Unimplemented import of non-function attempted");
  243. return vm.throw_completion<JS::TypeError>("LinkError: Not Implemented"sv);
  244. }));
  245. }
  246. }
  247. linker.link(resolved_imports);
  248. auto link_result = linker.finish();
  249. if (link_result.is_error()) {
  250. // FIXME: Throw a LinkError.
  251. StringBuilder builder;
  252. builder.append("LinkError: Missing "sv);
  253. builder.join(' ', link_result.error().missing_imports);
  254. return vm.throw_completion<JS::TypeError>(MUST(builder.to_string()));
  255. }
  256. auto instance_result = cache.abstract_machine().instantiate(module, link_result.release_value());
  257. if (instance_result.is_error()) {
  258. // FIXME: Throw a LinkError instead.
  259. return vm.throw_completion<JS::TypeError>(instance_result.error().error);
  260. }
  261. return instance_result.release_value();
  262. }
  263. JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM& vm, JS::Object* buffer_object)
  264. {
  265. ReadonlyBytes data;
  266. if (is<JS::ArrayBuffer>(buffer_object)) {
  267. auto& buffer = static_cast<JS::ArrayBuffer&>(*buffer_object);
  268. data = buffer.buffer();
  269. } else if (is<JS::TypedArrayBase>(buffer_object)) {
  270. auto& buffer = static_cast<JS::TypedArrayBase&>(*buffer_object);
  271. auto typed_array_record = JS::make_typed_array_with_buffer_witness_record(buffer, JS::ArrayBuffer::Order::SeqCst);
  272. if (JS::is_typed_array_out_of_bounds(typed_array_record))
  273. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BufferOutOfBounds, "TypedArray"sv);
  274. data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), JS::typed_array_byte_length(typed_array_record));
  275. } else if (is<JS::DataView>(buffer_object)) {
  276. auto& buffer = static_cast<JS::DataView&>(*buffer_object);
  277. auto view_record = JS::make_data_view_with_buffer_witness_record(buffer, JS::ArrayBuffer::Order::SeqCst);
  278. if (JS::is_view_out_of_bounds(view_record))
  279. return vm.throw_completion<JS::TypeError>(JS::ErrorType::BufferOutOfBounds, "DataView"sv);
  280. data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), JS::get_view_byte_length(view_record));
  281. } else {
  282. return vm.throw_completion<JS::TypeError>("Not a BufferSource"sv);
  283. }
  284. FixedMemoryStream stream { data };
  285. auto module_result = Wasm::Module::parse(stream);
  286. if (module_result.is_error()) {
  287. // FIXME: Throw CompileError instead.
  288. return vm.throw_completion<JS::TypeError>(Wasm::parse_error_to_byte_string(module_result.error()));
  289. }
  290. auto& cache = get_cache(*vm.current_realm());
  291. if (auto validation_result = cache.abstract_machine().validate(module_result.value()); validation_result.is_error()) {
  292. // FIXME: Throw CompileError instead.
  293. return vm.throw_completion<JS::TypeError>(validation_result.error().error_string);
  294. }
  295. auto compiled_module = make_ref_counted<CompiledWebAssemblyModule>(module_result.release_value());
  296. cache.add_compiled_module(compiled_module);
  297. return compiled_module;
  298. }
  299. JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress address, ByteString const& name, Instance* instance)
  300. {
  301. auto& realm = *vm.current_realm();
  302. Optional<Wasm::FunctionType> type;
  303. auto& cache = get_cache(realm);
  304. cache.abstract_machine().store().get(address)->visit([&](auto const& value) { type = value.type(); });
  305. if (auto entry = cache.get_function_instance(address); entry.has_value())
  306. return *entry;
  307. auto function = JS::NativeFunction::create(
  308. realm,
  309. name,
  310. [address, type = type.release_value(), instance](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> {
  311. (void)instance;
  312. auto& realm = *vm.current_realm();
  313. Vector<Wasm::Value> values;
  314. values.ensure_capacity(type.parameters().size());
  315. // Grab as many values as needed and convert them.
  316. size_t index = 0;
  317. for (auto& type : type.parameters())
  318. values.append(TRY(to_webassembly_value(vm, vm.argument(index++), type)));
  319. auto& cache = get_cache(realm);
  320. auto result = cache.abstract_machine().invoke(address, move(values));
  321. // FIXME: Use the convoluted mapping of errors defined in the spec.
  322. if (result.is_trap())
  323. return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("Wasm execution trapped (WIP): {}", result.trap().reason)));
  324. if (result.values().is_empty())
  325. return JS::js_undefined();
  326. if (result.values().size() == 1)
  327. return to_js_value(vm, result.values().first(), type.results().first());
  328. VERIFY_NOT_REACHED();
  329. // TODO
  330. /*return JS::Value(JS::Array::create_from<Wasm::Value>(realm, result.values(), [&](Wasm::Value value) {*/
  331. /* return to_js_value(vm, value);*/
  332. /*}));*/
  333. });
  334. cache.add_function_instance(address, function);
  335. return function;
  336. }
  337. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM& vm, JS::Value value, Wasm::ValueType const& type)
  338. {
  339. static ::Crypto::SignedBigInteger two_64 = "1"_sbigint.shift_left(64);
  340. switch (type.kind()) {
  341. case Wasm::ValueType::I64: {
  342. auto bigint = TRY(value.to_bigint(vm));
  343. auto value = bigint->big_integer().divided_by(two_64).remainder;
  344. VERIFY(value.unsigned_value().trimmed_length() <= 2);
  345. i64 integer = static_cast<i64>(value.unsigned_value().to_u64());
  346. if (value.is_negative())
  347. integer = -integer;
  348. return Wasm::Value { integer };
  349. }
  350. case Wasm::ValueType::I32: {
  351. auto _i32 = TRY(value.to_i32(vm));
  352. return Wasm::Value { static_cast<i32>(_i32) };
  353. }
  354. case Wasm::ValueType::F64: {
  355. auto number = TRY(value.to_double(vm));
  356. return Wasm::Value { static_cast<double>(number) };
  357. }
  358. case Wasm::ValueType::F32: {
  359. auto number = TRY(value.to_double(vm));
  360. return Wasm::Value { static_cast<float>(number) };
  361. }
  362. case Wasm::ValueType::FunctionReference: {
  363. if (value.is_null())
  364. return Wasm::Value(Wasm::ValueType { Wasm::ValueType::Kind::FunctionReference });
  365. if (value.is_function()) {
  366. auto& function = value.as_function();
  367. auto& cache = get_cache(*vm.current_realm());
  368. for (auto& entry : cache.function_instances()) {
  369. if (entry.value == &function)
  370. return Wasm::Value { Wasm::Reference { Wasm::Reference::Func { entry.key, cache.abstract_machine().store().get_module_for(entry.key) } } };
  371. }
  372. }
  373. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Exported function");
  374. }
  375. case Wasm::ValueType::ExternReference: {
  376. if (value.is_null())
  377. return Wasm::Value(Wasm::ValueType { Wasm::ValueType::Kind::ExternReference });
  378. auto& cache = get_cache(*vm.current_realm());
  379. for (auto& entry : cache.extern_values()) {
  380. if (entry.value == value)
  381. return Wasm::Value { Wasm::Reference { Wasm::Reference::Extern { entry.key } } };
  382. }
  383. Wasm::ExternAddress extern_addr = cache.extern_values().size();
  384. cache.add_extern_value(extern_addr, value);
  385. return Wasm::Value { Wasm::Reference { Wasm::Reference::Extern { extern_addr } } };
  386. }
  387. case Wasm::ValueType::V128:
  388. return vm.throw_completion<JS::TypeError>("Cannot convert a vector value to a javascript value"sv);
  389. }
  390. VERIFY_NOT_REACHED();
  391. }
  392. Wasm::Value default_webassembly_value(JS::VM& vm, Wasm::ValueType type)
  393. {
  394. switch (type.kind()) {
  395. case Wasm::ValueType::I32:
  396. case Wasm::ValueType::I64:
  397. case Wasm::ValueType::F32:
  398. case Wasm::ValueType::F64:
  399. case Wasm::ValueType::V128:
  400. case Wasm::ValueType::FunctionReference:
  401. return Wasm::Value(type);
  402. case Wasm::ValueType::ExternReference:
  403. return MUST(to_webassembly_value(vm, JS::js_undefined(), type));
  404. }
  405. VERIFY_NOT_REACHED();
  406. }
  407. // https://webassembly.github.io/spec/js-api/#tojsvalue
  408. JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value, Wasm::ValueType type)
  409. {
  410. auto& realm = *vm.current_realm();
  411. switch (type.kind()) {
  412. case Wasm::ValueType::I64:
  413. return realm.heap().allocate<JS::BigInt>(realm, ::Crypto::SignedBigInteger { wasm_value.to<i64>() });
  414. case Wasm::ValueType::I32:
  415. return JS::Value(wasm_value.to<i32>());
  416. case Wasm::ValueType::F64:
  417. return JS::Value(wasm_value.to<double>());
  418. case Wasm::ValueType::F32:
  419. return JS::Value(static_cast<double>(wasm_value.to<float>()));
  420. case Wasm::ValueType::FunctionReference: {
  421. auto ref_ = wasm_value.to<Wasm::Reference>();
  422. if (ref_.ref().has<Wasm::Reference::Null>())
  423. return JS::js_null();
  424. auto address = ref_.ref().get<Wasm::Reference::Func>().address;
  425. auto& cache = get_cache(realm);
  426. auto* function = cache.abstract_machine().store().get(address);
  427. auto name = function->visit(
  428. [&](Wasm::WasmFunction& wasm_function) {
  429. auto index = *wasm_function.module().functions().find_first_index(address);
  430. return ByteString::formatted("func{}", index);
  431. },
  432. [](Wasm::HostFunction& host_function) {
  433. return host_function.name();
  434. });
  435. return create_native_function(vm, address, name);
  436. }
  437. case Wasm::ValueType::ExternReference: {
  438. auto ref_ = wasm_value.to<Wasm::Reference>();
  439. if (ref_.ref().has<Wasm::Reference::Null>())
  440. return JS::js_null();
  441. auto address = ref_.ref().get<Wasm::Reference::Extern>().address;
  442. auto& cache = get_cache(realm);
  443. auto value = cache.get_extern_value(address);
  444. return value.release_value();
  445. }
  446. case Wasm::ValueType::V128:
  447. VERIFY_NOT_REACHED();
  448. }
  449. VERIFY_NOT_REACHED();
  450. }
  451. }
  452. }