/* * Copyright (c) 2021, Ali Mohammad Pur * Copyright (c) 2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include namespace Web::WebAssembly { GC_DEFINE_ALLOCATOR(Table); static Wasm::ValueType table_kind_to_value_type(Bindings::TableKind kind) { switch (kind) { case Bindings::TableKind::Externref: return Wasm::ValueType { Wasm::ValueType::ExternReference }; case Bindings::TableKind::Anyfunc: return Wasm::ValueType { Wasm::ValueType::FunctionReference }; } VERIFY_NOT_REACHED(); } WebIDL::ExceptionOr> Table::construct_impl(JS::Realm& realm, TableDescriptor& descriptor, JS::Value value) { auto& vm = realm.vm(); auto reference_type = table_kind_to_value_type(descriptor.element); auto reference_value = vm.argument_count() == 1 ? Detail::default_webassembly_value(vm, reference_type) : TRY(Detail::to_webassembly_value(vm, value, reference_type)); if (descriptor.maximum.has_value() && descriptor.maximum.value() < descriptor.initial) return vm.throw_completion("Maximum should not be less than initial in table type"sv); Wasm::Limits limits { descriptor.initial, move(descriptor.maximum) }; Wasm::TableType table_type { reference_type, move(limits) }; auto& cache = Detail::get_cache(realm); auto address = cache.abstract_machine().store().allocate(table_type); if (!address.has_value()) return vm.throw_completion("Wasm Table allocation failed"sv); auto const& reference = reference_value.to(); auto& table = *cache.abstract_machine().store().get(*address); for (auto& element : table.elements()) element = reference; return realm.create(realm, *address); } Table::Table(JS::Realm& realm, Wasm::TableAddress address) : Bindings::PlatformObject(realm) , m_address(address) { } void Table::initialize(JS::Realm& realm) { Base::initialize(realm); WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(Table, WebAssembly.Table); } // https://webassembly.github.io/spec/js-api/#dom-table-grow WebIDL::ExceptionOr Table::grow(u32 delta, JS::Value value) { auto& vm = this->vm(); auto& cache = Detail::get_cache(realm()); auto* table = cache.abstract_machine().store().get(address()); if (!table) return vm.throw_completion("Could not find the memory table to grow"sv); auto initial_size = table->elements().size(); auto reference_value = vm.argument_count() == 1 ? Detail::default_webassembly_value(vm, table->type().element_type()) : TRY(Detail::to_webassembly_value(vm, value, table->type().element_type())); auto const& reference = reference_value.to(); if (!table->grow(delta, reference)) return vm.throw_completion("Failed to grow table"sv); return initial_size; } // https://webassembly.github.io/spec/js-api/#dom-table-get WebIDL::ExceptionOr Table::get(u32 index) const { auto& vm = this->vm(); auto& cache = Detail::get_cache(realm()); auto* table = cache.abstract_machine().store().get(address()); if (!table) return vm.throw_completion("Could not find the memory table"sv); if (table->elements().size() <= index) return vm.throw_completion("Table element index out of range"sv); auto& ref = table->elements()[index]; Wasm::Value wasm_value { ref }; return Detail::to_js_value(vm, wasm_value, table->type().element_type()); } // https://webassembly.github.io/spec/js-api/#dom-table-set WebIDL::ExceptionOr Table::set(u32 index, JS::Value value) { auto& vm = this->vm(); auto& cache = Detail::get_cache(realm()); auto* table = cache.abstract_machine().store().get(address()); if (!table) return vm.throw_completion("Could not find the memory table"sv); if (table->elements().size() <= index) return vm.throw_completion("Table element index out of range"sv); auto reference_value = vm.argument_count() == 1 ? Detail::default_webassembly_value(vm, table->type().element_type()) : TRY(Detail::to_webassembly_value(vm, value, table->type().element_type())); auto const& reference = reference_value.to(); table->elements()[index] = reference; return {}; } // https://webassembly.github.io/spec/js-api/#dom-table-length WebIDL::ExceptionOr Table::length() const { auto& vm = this->vm(); auto& cache = Detail::get_cache(realm()); auto* table = cache.abstract_machine().store().get(address()); if (!table) return vm.throw_completion("Could not find the memory table"sv); return table->elements().size(); } }