Truly independent web browser
LibWasm: Remove the C ABI that only LibWeb used
Shannon Booth5f9fd12f9fcd
diff --git a/Libraries/LibWasm/Engine/src/capi.rs b/Libraries/LibWasm/Engine/src/capi.rsindex 6ba1cb2f914..89f85bdcbb3 100644--- a/Libraries/LibWasm/Engine/src/capi.rs+++ b/Libraries/LibWasm/Engine/src/capi.rs@@ -11,10 +11,10 @@//! belongs to one thread. Entities (functions, tables, memories, globals, tags, and instances) are the addresses of//! their cells.-use crate::module::{ExternalKind, ImportDescription, Module};+use crate::module::{ImportDescription, Module};use crate::runtime::entity::{Entity, Handle};use crate::runtime::heap::{GcInterface, Visitor};-use crate::runtime::{ExternValue, FunctionKind, HostFunction, InstantiationError, MemoryAddress, Store, Trap, Value};+use crate::runtime::{ExternValue, HostFunction, InstantiationError, Store, Trap, Value};use crate::types::{AddressType, FuncType, GlobalType, HeapType, Limits, MemoryType, RefType, TableType, ValType};use core::ffi::c_void;use std::rc::Rc;@@ -240,18 +240,6 @@ pub enum WasmExternKind {Tag,}-impl WasmExternKind {- fn of(kind: ExternalKind) -> Self {- match kind {- ExternalKind::Function => Self::Function,- ExternalKind::Table => Self::Table,- ExternalKind::Memory => Self::Memory,- ExternalKind::Global => Self::Global,- ExternalKind::Tag => Self::Tag,- }- }-}-/// A function, table, memory, global, or tag of a store.#[repr(C)]#[derive(Debug, Clone, Copy)]@@ -448,42 +436,6 @@ pub unsafe extern "C" fn wasm_module_import(}}-/// # Safety-///-/// `module` must be a live module.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_module_export_count(module: *const WasmModule) -> usize {- module_of(module).exports.len()-}--/// The name and type of an export.-///-/// # Safety-///-/// `module` must be a live module with an export at `index`, and the out parameters must be writable.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_module_export(- module: *const WasmModule,- index: usize,- name: *mut WasmStringView,- ty: *mut WasmExternType,-) {- let module = module_of(module);- let export = &module.exports[index];- let description = match export.kind {- ExternalKind::Function => ImportDescription::Function(module.function_space[export.index as usize]),- ExternalKind::Table => ImportDescription::Table(module.table_space[export.index as usize]),- ExternalKind::Memory => ImportDescription::Memory(module.memory_space[export.index as usize]),- ExternalKind::Global => ImportDescription::Global(module.global_space[export.index as usize]),- ExternalKind::Tag => ImportDescription::Tag(module.tag_space[export.index as usize]),- };- unsafe {- *name = WasmStringView::of(&export.name);- *ty = extern_type_of_import(&description);- (*ty).kind = WasmExternKind::of(export.kind);- }-}-/// The parameter and result counts of the function type at `type_index`, or false if it is not a function type.////// # Safety@@ -1007,132 +959,6 @@ pub unsafe extern "C" fn wasm_store_function_from_reference(true}-/// The bytes of a memory, which move when it grows.-///-/// # Safety-///-/// `store` must be a live store with a memory at `memory`, and `size` writable.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_memory_data(- store: *mut WasmStore,- memory: *mut c_void,- size: *mut usize,-) -> *mut u8 {- let memory = store_of(store).memory_mut(entity(memory));- unsafe { *size = memory.size() };- memory.bytes_mut().as_mut_ptr()-}--/// Grow a memory by `delta` pages. Returns the previous size in pages, or -1 if it cannot grow.-///-/// # Safety-///-/// `store` must be a live store with a memory at `memory`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_memory_grow(store: *mut WasmStore, memory: *mut c_void, delta: u64) -> i64 {- store_of(store)- .memory_mut(entity(memory))- .grow(delta)- .map_or(-1, |previous| previous as i64)-}--/// The limits a memory was created with.-///-/// # Safety-///-/// `store` must be a live store with a memory at `memory`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_memory_limits(store: *mut WasmStore, memory: *mut c_void) -> WasmLimits {- WasmLimits::of(&store_of(store).memory(entity(memory)).ty.limits)-}--/// # Safety-///-/// `store` must be a live store with a table at `table`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_table_size(store: *mut WasmStore, table: *mut c_void) -> u64 {- store_of(store).table(entity(table)).elements.len() as u64-}--/// The element type and limits a table was created with.-///-/// # Safety-///-/// `store` must be a live store with a table at `table`, and the out parameters must be writable.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_table_type(- store: *mut WasmStore,- table: *mut c_void,- element: *mut WasmValueType,- limits: *mut WasmLimits,-) {- let table = store_of(store).table(entity(table));- unsafe {- *element = WasmValueType::of(ValType::Ref(table.ty.element));- *limits = WasmLimits::of(&table.ty.limits);- }-}--/// Read a table element. Returns false if `index` is out of bounds.-///-/// # Safety-///-/// `store` must be a live store with a table at `table`, and `value` writable.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_table_get(- store: *mut WasmStore,- table: *mut c_void,- index: u64,- value: *mut u64,-) -> bool {- let Some(&element) = store_of(store).table(entity(table)).elements.get(index as usize) else {- return false;- };- unsafe { *value = element };- true-}--/// Write a table element. Returns false if `index` is out of bounds.-///-/// # Safety-///-/// `store` must be a live store with a table at `table`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_table_set(- store: *mut WasmStore,- table: *mut c_void,- index: u64,- value: u64,-) -> bool {- let Some(element) = store_of(store)- .table_mut(entity(table))- .elements- .get_mut(index as usize)- else {- return false;- };- *element = value;- true-}--/// Grow a table by `delta` elements set to `initial`. Returns the previous size, or -1 if it cannot grow.-///-/// # Safety-///-/// `store` must be a live store with a table at `table`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_table_grow(- store: *mut WasmStore,- table: *mut c_void,- delta: u64,- initial: u64,-) -> i64 {- store_of(store)- .table_mut(entity(table))- .grow(delta, initial)- .map_or(-1, |previous| previous as i64)-}-/// The type of a global.////// # Safety@@ -1157,340 +983,6 @@ pub unsafe extern "C" fn wasm_store_global_get(store: *mut WasmStore, global: *mWasmValue::of(store_of(store).global(entity(global)).get())}-/// # Safety-///-/// `store` must be a live store with a global at `global`, and `value` must have the global's type.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_global_set(store: *mut WasmStore, global: *mut c_void, value: WasmValue) {- store_of(store).global_mut(entity(global)).set(value.to_value());-}--// Arrays.--/// The number of elements of an array.-///-/// # Safety-///-/// `array` must be a live array reference.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_array_length(array: u64) -> u32 {- unsafe { crate::runtime::gc::array_length(array) }-}--/// The bits of the element at `index` of an array whose elements are numbers, with packed elements zero-extended.-///-/// # Safety-///-/// `array` must be a live array reference whose elements are neither references nor v128s, and `index` must be in-/// its bounds.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_array_element(array: u64, index: u32) -> u64 {- unsafe { crate::runtime::gc::array_element(array, index) }-}--/// Set the element at `index` of an array whose elements are numbers to `bits`, which packed elements wrap.-///-/// # Safety-///-/// `array` must be a live array reference whose elements are neither references nor v128s, and `index` must be in-/// its bounds.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_array_set_element(array: u64, index: u32, bits: u64) {- unsafe { crate::runtime::gc::set_array_element(array, index, bits) }-}--// Custom sections.--/// The number of custom sections of a module.-///-/// # Safety-///-/// `module` must be a live module.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_module_custom_section_count(module: *const WasmModule) -> usize {- module_of(module).custom_sections.len()-}--/// The name and contents of a module's custom section, in the order of the binary.-///-/// # Safety-///-/// `module` must be a live module with a custom section at `index`, and the out parameters must be writable.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_module_custom_section(- module: *const WasmModule,- index: usize,- name: *mut WasmStringView,- contents: *mut WasmStringView,-) {- let module = module_of(module);- let section = &module.custom_sections[index];- let bytes = &module.bytes[section.contents.start as usize..section.contents.end as usize];- unsafe {- *name = WasmStringView::of(§ion.name);- *contents = WasmStringView {- data: bytes.as_ptr(),- length: bytes.len(),- };- }-}--// Tags and exceptions.--/// Add a tag for exceptions that carry values of the given types, and return its address.-///-/// # Safety-///-/// `store` must be a live store, and `parameters` must hold `parameter_count` types.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_allocate_tag(- store: *mut WasmStore,- parameters: *const WasmValueType,- parameter_count: usize,-) -> *mut c_void {- let params = unsafe { ak::slice_from_raw_parts(parameters, parameter_count) }- .iter()- .map(|ty| ty.to_value_type())- .collect();- store_of(store)- .allocate_tag(FuncType {- params,- results: Vec::new(),- })- .cell()- .cast()-}--/// `ty` with its concrete heap type, which `module` defines, replaced by the type's store-wide identity. The types the-/// store reports for functions, tables, globals, and tags have identities in place of type indices too.-///-/// # Safety-///-/// `store` must be a live store and `module` a live module.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_canonical_value_type(- store: *mut WasmStore,- module: *const WasmModule,- ty: WasmValueType,-) -> WasmValueType {- if ty.kind != WasmValueKind::Reference || ty.heap != WasmHeapKind::Concrete {- return ty;- }- let identities = store_of(store).module_type_identities_of(&module_arc(module));- WasmValueType {- type_index: identities[ty.type_index as usize],- ..ty- }-}--/// Whether a non-null reference matches the reference type `ty`, whose concrete heap type is an identity. A-/// reference of the any hierarchy must be in its internal form: an i31 reference, a GC object, or a host reference.-///-/// # Safety-///-/// `store` must be a live store, and `reference` a live reference of the hierarchy of `ty`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_reference_matches(- store: *mut WasmStore,- reference: u64,- ty: WasmValueType,-) -> bool {- let ValType::Ref(reference_type) = ty.to_value_type() else {- return false;- };- store_of(store).reference_matches(reference, reference_type.heap)-}--/// Whether the type `identity` is a function type, as opposed to a struct or array type.-///-/// # Safety-///-/// `store` must be a live store with a type `identity`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_is_function_type(store: *mut WasmStore, identity: u32) -> bool {- store_of(store).is_function_type(identity)-}--/// The number of values an exception with the tag carries.-///-/// # Safety-///-/// `store` must be a live store with a tag at `tag`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_tag_parameter_count(store: *mut WasmStore, tag: *mut c_void) -> usize {- store_of(store).tag(entity(tag)).ty.params.len()-}--/// The types of the values an exception with the tag carries.-///-/// # Safety-///-/// `store` must be a live store with a tag at `tag`, and `parameters` must have room for its parameter count.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_tag_parameters(- store: *mut WasmStore,- tag: *mut c_void,- parameters: *mut WasmValueType,-) {- let ty = &store_of(store).tag(entity(tag)).ty;- unsafe { write_function_type(ty, parameters, std::ptr::null_mut()) };-}--/// Create an exception with the tag, carrying `values`, and return its reference. The exception is a cell of the-/// store's heap, which keeps it while something refers to it.-///-/// # Safety-///-/// `store` must be a live store with a tag at `tag`, and `values` must hold one value of each of its parameter types.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_allocate_exception(- store: *mut WasmStore,- tag: *mut c_void,- values: *const WasmValue,- value_count: usize,-) -> u64 {- let values: Vec<Value> = unsafe { ak::slice_from_raw_parts(values, value_count) }- .iter()- .map(|value| value.to_value())- .collect();- store_of(store).allocate_exception_with_values(entity(tag), &values)-}--/// The tag of an exception.-///-/// # Safety-///-/// `store` must be a live store, and `exception` a reference to one of its exceptions.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_exception_tag(store: *mut WasmStore, exception: u64) -> *mut c_void {- store_of(store).exception(exception).tag.cell().cast()-}--/// The values an exception carries.-///-/// # Safety-///-/// `store` must be a live store, `exception` a reference to one of its exceptions, and `values` must have room for-/// the parameter count of its tag.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_exception_values(store: *mut WasmStore, exception: u64, values: *mut WasmValue) {- for (index, value) in store_of(store).exception_values(exception).into_iter().enumerate() {- unsafe { *values.add(index) = WasmValue::of(value) };- }-}--// Functions, memories.--/// The index of a WebAssembly function in the function index space of the module that defines it. Returns false for a-/// host function.-///-/// # Safety-///-/// `store` must be a live store with a function at `function`, and `index` writable.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_function_index(- store: *mut WasmStore,- function: *mut c_void,- index: *mut u32,-) -> bool {- let FunctionKind::Wasm {- index: function_index, ..- } = store_of(store).function(entity(function)).kind- else {- return false;- };- unsafe { *index = function_index };- true-}--/// The user data a host function was allocated with, or null if the function is no host function.-///-/// # Safety-///-/// `store` must be a live store with a function at `function`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_host_function_user_data(- store: *mut WasmStore,- function: *mut c_void,-) -> *mut c_void {- match &store_of(store).function(entity(function)).kind {- FunctionKind::Host(callback) => callback.user_data(),- FunctionKind::Wasm { .. } => core::ptr::null_mut(),- }-}--/// The handle of the LibGC primitive storage a memory lives in, or zero if the store has no heap.-///-/// # Safety-///-/// `store` must be a live store with a memory at `memory`.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_memory_primitive_storage_handle(store: *mut WasmStore, memory: *mut c_void) -> u64 {- store_of(store).memory(entity(memory)).primitive_storage_handle()-}--/// Called with a memory's address after `memory.grow` grows it.-pub type WasmMemoryGrowCallback = unsafe extern "C" fn(user_data: *mut std::ffi::c_void, memory: *mut c_void);--/// Have `callback` run with `user_data` after each `memory.grow` that grows a memory of the store, but not after-/// `wasm_store_memory_grow()`.-///-/// # Safety-///-/// `store` must be a live store, and `user_data` must stay valid until the callback is cleared or the store is-/// destroyed.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_set_memory_grow_callback(- store: *mut WasmStore,- callback: WasmMemoryGrowCallback,- user_data: *mut std::ffi::c_void,-) {- store_of(store).memory_grow_callback = Some(Rc::new(move |memory: MemoryAddress| unsafe {- callback(user_data, memory.cell().cast());- }));-}--/// Remove the callback `wasm_store_set_memory_grow_callback()` set.-///-/// # Safety-///-/// `store` must be a live store.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_clear_memory_grow_callback(store: *mut WasmStore) {- store_of(store).memory_grow_callback = None;-}--/// Sets whether the agent the store runs code for may block, which `memory.atomic.wait` needs.-///-/// # Safety-///-/// `store` must be a live store.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_store_set_can_block(store: *mut WasmStore, can_block: bool) {- store_of(store).can_block = can_block;-}--/// Has `cell`, an entity, or an exception or a GC object (whose references are their cells), keep the embedder's cell-/// `object` alive for as long as it lives itself.-///-/// # Safety-///-/// `cell` must be a live cell of a store with a LibGC heap, and `object` a cell of the same heap.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_set_host_object(cell: *mut c_void, object: *mut c_void) {- unsafe { crate::runtime::heap::set_host_object(cell.cast(), object) };-}--/// The cell `wasm_set_host_object()` gave `cell` to keep alive, or null.-///-/// # Safety-///-/// `cell` must be a live cell of a store with a LibGC heap.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn wasm_host_object(cell: *mut c_void) -> *mut c_void {- unsafe { crate::runtime::heap::host_object(cell) }-}-// WASI./// The state of a WASI command, whose functions a store gets with `wasm_wasi_allocate_function()`.diff --git a/Libraries/LibWasm/Runtime.cpp b/Libraries/LibWasm/Runtime.cppindex 289c6dc220d..8418d9090ca 100644--- a/Libraries/LibWasm/Runtime.cpp+++ b/Libraries/LibWasm/Runtime.cpp@@ -63,7 +63,6 @@ static FFI::WasmValue to_ffi(Value value) { return bit_cast<FFI::WasmValue>(valustatic FFI::WasmLimits to_ffi(Limits limits) { return bit_cast<FFI::WasmLimits>(limits); }static ValueType from_ffi(FFI::WasmValueType type) { return bit_cast<ValueType>(type); }static Value from_ffi(FFI::WasmValue value) { return bit_cast<Value>(value); }-static Limits from_ffi(FFI::WasmLimits limits) { return bit_cast<Limits>(limits); }static ExternType from_ffi(FFI::WasmExternType type) { return bit_cast<ExternType>(type); }static StringView from_ffi(FFI::WasmStringView view) { return { reinterpret_cast<char const*>(view.data), view.length }; }@@ -113,19 +112,6 @@ Import Module::import(size_t index) constreturn { from_ffi(module_name), from_ffi(name), from_ffi(type) };}-size_t Module::export_count() const-{- return FFI::wasm_module_export_count(m_module);-}--ModuleExport Module::module_export(size_t index) const-{- FFI::WasmStringView name {};- FFI::WasmExternType type {};- FFI::wasm_module_export(m_module, index, &name, &type);- return { from_ffi(name), from_ffi(type) };-}-static FunctionType make_function_type(size_t parameter_count, size_t result_count, auto copy){Vector<FFI::WasmValueType> parameters;@@ -154,19 +140,6 @@ Optional<FunctionType> Module::function_type(u32 type_index) const});}-size_t Module::custom_section_count() const-{- return FFI::wasm_module_custom_section_count(m_module);-}--CustomSection Module::custom_section(size_t index) const-{- FFI::WasmStringView name {};- FFI::WasmStringView contents {};- FFI::wasm_module_custom_section(m_module, index, &name, &contents);- return { from_ffi(name), ReadonlyBytes { contents.data, contents.length } };-}-template<typename To, typename From>static To function_cast(From function){@@ -217,12 +190,11 @@ struct HostFunctionData {Store& store;Store::HostFunction function;- Optional<u32> index;};}-static FFI::WasmStatus call_host_function(void* user_data, FFI::WasmStore*, FFI::WasmValue const* arguments, size_t argument_count, FFI::WasmValue* results, size_t result_count, FFI::WasmString* error, u64* exception)+static FFI::WasmStatus call_host_function(void* user_data, FFI::WasmStore*, FFI::WasmValue const* arguments, size_t argument_count, FFI::WasmValue* results, size_t result_count, FFI::WasmString* error, u64*){auto& data = *static_cast<HostFunctionData*>(user_data);ReadonlySpan<Value> argument_span { reinterpret_cast<Value const*>(arguments), argument_count };@@ -230,10 +202,6 @@ static FFI::WasmStatus call_host_function(void* user_data, FFI::WasmStore*, FFI:auto result = data.function(data.store, argument_span, result_span);if (!result.is_error())return FFI::WasmStatus::Ok;- if (auto thrown = result.error().thrown_exception; thrown.has_value()) {- *exception = *thrown;- return FFI::WasmStatus::Exception;- }*error = make_string(result.error().message);return FFI::WasmStatus::Trap;}@@ -250,7 +218,7 @@ static void release_host_function(void* user_data)delete static_cast<HostFunctionData*>(user_data);}-Entity Store::allocate_host_function(FunctionType const& type, HostFunction function, Optional<u32> index)+Entity Store::allocate_host_function(FunctionType const& type, HostFunction function){Vector<FFI::WasmValueType> parameters;Vector<FFI::WasmValueType> results;@@ -258,18 +226,10 @@ Entity Store::allocate_host_function(FunctionType const& type, HostFunction funcparameters.append(to_ffi(parameter));for (auto result : type.results)results.append(to_ffi(result));- auto* data = new HostFunctionData { *this, move(function), index };+ auto* data = new HostFunctionData { *this, move(function) };return entity_from_ffi(FFI::wasm_store_allocate_host_function(m_store, parameters.data(), parameters.size(), results.data(), results.size(), call_host_function, visit_host_function, release_host_function, data));}-Optional<u32> Store::host_function_index(Entity function)-{- auto* data = FFI::wasm_store_host_function_user_data(m_store, to_ffi(function));- if (!data)- return {};- return static_cast<HostFunctionData*>(data)->index;-}-Optional<Entity> Store::allocate_memory(Limits limits){void* memory = nullptr;@@ -380,193 +340,16 @@ Entity Store::function_from_reference(u64 reference)return entity_from_ffi(function);}-Optional<u32> Store::function_index(Entity function)-{- u32 index = 0;- if (!FFI::wasm_store_function_index(m_store, to_ffi(function), &index))- return {};- return index;-}--ValueType Store::canonical_value_type(Module const& module, ValueType type)-{- return from_ffi(FFI::wasm_store_canonical_value_type(m_store, module.handle(), to_ffi(type)));-}--bool Store::reference_matches(u64 reference, ValueType type)-{- return FFI::wasm_store_reference_matches(m_store, reference, to_ffi(type));-}--bool Store::is_function_type(u32 identity)-{- return FFI::wasm_store_is_function_type(m_store, identity);-}--Entity Store::allocate_tag(ReadonlySpan<ValueType> parameters)-{- Vector<FFI::WasmValueType> types;- for (auto parameter : parameters)- types.append(to_ffi(parameter));- return entity_from_ffi(FFI::wasm_store_allocate_tag(m_store, types.data(), types.size()));-}--Vector<ValueType> Store::tag_parameters(Entity tag)-{- Vector<FFI::WasmValueType> types;- types.resize(FFI::wasm_store_tag_parameter_count(m_store, to_ffi(tag)));- FFI::wasm_store_tag_parameters(m_store, to_ffi(tag), types.data());- Vector<ValueType> parameters;- parameters.ensure_capacity(types.size());- for (auto type : types)- parameters.unchecked_append(from_ffi(type));- return parameters;-}--u64 Store::allocate_exception(Entity tag, ReadonlySpan<Value> values)-{- return FFI::wasm_store_allocate_exception(m_store, to_ffi(tag), reinterpret_cast<FFI::WasmValue const*>(values.data()), values.size());-}--Entity Store::exception_tag(u64 exception)-{- return entity_from_ffi(FFI::wasm_store_exception_tag(m_store, exception));-}--Values Store::exception_values(u64 exception)-{- Values values;- values.resize(FFI::wasm_store_tag_parameter_count(m_store, to_ffi(exception_tag(exception))));- FFI::wasm_store_exception_values(m_store, exception, reinterpret_cast<FFI::WasmValue*>(values.data()));- return values;-}--Bytes Store::memory(Entity memory)-{- size_t size = 0;- auto* data = FFI::wasm_store_memory_data(m_store, to_ffi(memory), &size);- return { data, size };-}--i64 Store::grow_memory(Entity memory, u64 delta)-{- return FFI::wasm_store_memory_grow(m_store, to_ffi(memory), delta);-}--Limits Store::memory_limits(Entity memory)-{- return from_ffi(FFI::wasm_store_memory_limits(m_store, to_ffi(memory)));-}--GC::PrimitiveStorageHandle Store::memory_primitive_storage_handle(Entity memory)-{- auto handle = FFI::wasm_store_memory_primitive_storage_handle(m_store, to_ffi(memory));- return { .index = static_cast<u32>(handle), .generation = static_cast<u32>(handle >> GC_PRIMITIVE_STORAGE_HANDLE_GENERATION_SHIFT) };-}--void Store::set_memory_grow_callback(Function<void(Entity)> callback)-{- m_memory_grow_callback = move(callback);- if (!m_memory_grow_callback) {- FFI::wasm_store_clear_memory_grow_callback(m_store);- return;- }- FFI::wasm_store_set_memory_grow_callback(- m_store, [](void* user_data, void* memory) {- static_cast<Store*>(user_data)->m_memory_grow_callback(entity_from_ffi(memory));- },- this);-}--void Store::set_can_block(bool can_block)-{- FFI::wasm_store_set_can_block(m_store, can_block);-}--void Store::set_host_object(GC::ForeignCell* cell, GC::Cell& object)-{- FFI::wasm_set_host_object(cell, &object);-}--void Store::set_host_object(GC::ForeignCell* cell, GC::ForeignCell& object)-{- FFI::wasm_set_host_object(cell, &object);-}--void* Store::host_object(GC::ForeignCell* cell) const-{- return FFI::wasm_host_object(cell);-}--u64 Store::table_size(Entity table)-{- return FFI::wasm_store_table_size(m_store, to_ffi(table));-}--ValueType Store::table_element_type(Entity table)-{- FFI::WasmValueType element {};- FFI::WasmLimits limits {};- FFI::wasm_store_table_type(m_store, to_ffi(table), &element, &limits);- return from_ffi(element);-}--Limits Store::table_limits(Entity table)-{- FFI::WasmValueType element {};- FFI::WasmLimits limits {};- FFI::wasm_store_table_type(m_store, to_ffi(table), &element, &limits);- return from_ffi(limits);-}--Optional<u64> Store::table_get(Entity table, u64 index)-{- u64 value = 0;- if (!FFI::wasm_store_table_get(m_store, to_ffi(table), index, &value))- return {};- return value;-}--bool Store::table_set(Entity table, u64 index, u64 value)-{- return FFI::wasm_store_table_set(m_store, to_ffi(table), index, value);-}--i64 Store::grow_table(Entity table, u64 delta, u64 initial)-{- return FFI::wasm_store_table_grow(m_store, to_ffi(table), delta, initial);-}-ValueType Store::global_type(Entity global, bool& is_mutable){return from_ffi(FFI::wasm_store_global_type(m_store, to_ffi(global), &is_mutable));}-u32 Store::array_length(u64 array)-{- return FFI::wasm_array_length(array);-}--u64 Store::array_element(u64 array, u32 index)-{- return FFI::wasm_array_element(array, index);-}--void Store::set_array_element(u64 array, u32 index, u64 bits)-{- FFI::wasm_array_set_element(array, index, bits);-}-Value Store::global_get(Entity global){return from_ffi(FFI::wasm_store_global_get(m_store, to_ffi(global)));}-void Store::global_set(Entity global, Value value)-{- FFI::wasm_store_global_set(m_store, to_ffi(global), to_ffi(value));-}-static Vector<FFI::WasmStringView> string_views(ReadonlySpan<ByteString> strings){Vector<FFI::WasmStringView> views;diff --git a/Libraries/LibWasm/Runtime.h b/Libraries/LibWasm/Runtime.hindex 32c4a4c344d..cb79ed78c8b 100644--- a/Libraries/LibWasm/Runtime.h+++ b/Libraries/LibWasm/Runtime.h@@ -18,7 +18,6 @@#include <AK/Vector.h>#include <LibGC/ConservativeVector.h>#include <LibGC/Forward.h>-#include <LibGC/PrimitiveStorage.h>#include <LibGC/Ptr.h>#include <LibWasm/Export.h>@@ -123,8 +122,6 @@ struct Limits {bool has_maximum { false };bool address64 { false };bool shared { false };-- Optional<u64> maximum_if_any() const { return has_maximum ? Optional<u64> { maximum } : Optional<u64> {}; }};// What an import or export is: the type index of a function or tag, the limits of a memory or table, the element type@@ -148,21 +145,11 @@ struct Import {ExternType type;};-struct ModuleExport {- StringView name;- ExternType type;-};-struct InstanceExport {StringView name;Extern value;};-struct CustomSection {- StringView name;- ReadonlyBytes contents;-};-// Values that may be references to cells, which the garbage collector finds while the vector lives.using Values = GC::ConservativeVector<Value, 8>;@@ -174,11 +161,7 @@ public:size_t import_count() const;Import import(size_t index) const;- size_t export_count() const;- ModuleExport module_export(size_t index) const;Optional<FunctionType> function_type(u32 type_index) const;- size_t custom_section_count() const;- CustomSection custom_section(size_t index) const;FFI::WasmModule const* handle() const { return m_module; }@@ -205,22 +188,14 @@ struct Failure {u64 exception { 0 };};-// How a host function fails: it traps with a message, or throws an exception, which WebAssembly code can catch.+// How a host function fails: it traps with a message.struct HostError {HostError(ByteString message): message(move(message)){}- static HostError exception(u64 reference)- {- HostError error { ByteString {} };- error.thrown_exception = reference;- return error;- }-ByteString message;- Optional<u64> thrown_exception;};// What the entities of one thread share: their heap, types, and the interpreter's state. One store serves a whole@@ -239,8 +214,7 @@ public:// function visits the cells it captures, so it must not capture roots.using HostFunction = Function<ErrorOr<void, HostError>(Store&, ReadonlySpan<Value> arguments, Span<Value> results)>;- // `index` is the embedder's index of the host function, if it has one, which host_function_index() returns.- Entity allocate_host_function(FunctionType const&, HostFunction, Optional<u32> index = {});+ Entity allocate_host_function(FunctionType const&, HostFunction);Optional<Entity> allocate_memory(Limits);Optional<Entity> allocate_table(ValueType element, Limits, u64 initial);Entity allocate_global(ValueType, bool is_mutable, Value);@@ -255,71 +229,14 @@ public:u64 function_reference(Entity function);// The function a non-null function reference refers to.Entity function_from_reference(u64 reference);- // The index of a WebAssembly function in the function index space of the module that defines it, or nothing for- // a host function.- Optional<u32> function_index(Entity function);- // The index a host function was allocated with, or nothing.- Optional<u32> host_function_index(Entity function);-- // The types of functions, tables, globals, and tags of the store name concrete heap types by store-wide- // identities, where the types of a module name them by their indices in the module. This is a type of `module`- // in the store's terms.- ValueType canonical_value_type(Module const&, ValueType);- // Whether a non-null reference matches a reference type of the store. A reference of the any hierarchy must be in- // its internal form: an i31 reference, a GC object, or a host reference.- bool reference_matches(u64 reference, ValueType);- // Whether the concrete heap type `identity` is a function type, as opposed to a struct or array type.- bool is_function_type(u32 identity);-- Entity allocate_tag(ReadonlySpan<ValueType> parameters);- Vector<ValueType> tag_parameters(Entity tag);- // An exception is a cell of the heap, which keeps it while something refers to it.- u64 allocate_exception(Entity tag, ReadonlySpan<Value> values);- Entity exception_tag(u64 exception);- Values exception_values(u64 exception);-- // The bytes of a memory move when it grows.- Bytes memory(Entity memory);- // The previous size in pages, or -1 if the memory cannot grow.- i64 grow_memory(Entity memory, u64 delta);- Limits memory_limits(Entity memory);- GC::PrimitiveStorageHandle memory_primitive_storage_handle(Entity memory);- // Runs after each memory.grow instruction that grows a memory, but not after grow_memory().- void set_memory_grow_callback(Function<void(Entity memory)>);- // Whether the agent the store runs code for may block ([[CanBlock]]); memory.atomic.wait traps where it may not.- void set_can_block(bool);- // Has an entity, or the cell of an exception or a GC object, keep `object` alive for as long as it lives itself.- // An object that stands for the cell and refers to it then stays the same object while the cell lives.- void set_host_object(GC::ForeignCell* cell, GC::Cell& object);- void set_host_object(GC::ForeignCell* cell, GC::ForeignCell& object);- // The object set_host_object() gave the cell, or null.- void* host_object(GC::ForeignCell* cell) const;-- u64 table_size(Entity table);- ValueType table_element_type(Entity table);- Limits table_limits(Entity table);- Optional<u64> table_get(Entity table, u64 index);- bool table_set(Entity table, u64 index, u64 value);- // The previous size, or -1 if the table cannot grow.- i64 grow_table(Entity table, u64 delta, u64 initial);-- // The number of elements of an array, which is a reference to one.- u32 array_length(u64 array);- // The bits of an element of an array of numbers (whose elements are neither references nor v128s), with packed- // elements zero-extended. The index must be in bounds.- u64 array_element(u64 array, u32 index);- // Set an element of an array of numbers to `bits`, which packed elements wrap. The index must be in bounds.- void set_array_element(u64 array, u32 index, u64 bits);ValueType global_type(Entity global, bool& is_mutable);Value global_get(Entity global);- void global_set(Entity global, Value);FFI::WasmStore* handle() { return m_store; }private:FFI::WasmStore* m_store { nullptr };- Function<void(Entity)> m_memory_grow_callback;};// The state of a WASI (wasi_snapshot_preview1) command. Its functions only work on Unix hosts.