Truly independent web browser
LibWasm: Let embedders read and write the elements of arrays
Andreas Kling6cfd0593bd6e
diff --git a/Libraries/LibWasm/Engine/src/capi.rs b/Libraries/LibWasm/Engine/src/capi.rsindex 9253383773c..6ba1cb2f914 100644--- a/Libraries/LibWasm/Engine/src/capi.rs+++ b/Libraries/LibWasm/Engine/src/capi.rs@@ -1165,6 +1165,40 @@ pub unsafe extern "C" fn wasm_store_global_set(store: *mut WasmStore, global: *mstore_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.diff --git a/Libraries/LibWasm/Engine/src/runtime/gc.rs b/Libraries/LibWasm/Engine/src/runtime/gc.rsindex ec42f93577a..a6c2261f6df 100644--- a/Libraries/LibWasm/Engine/src/runtime/gc.rs+++ b/Libraries/LibWasm/Engine/src/runtime/gc.rs@@ -265,6 +265,63 @@ pub(super) unsafe extern "C" fn object_external_memory_size(cell: *const c_void)unsafe { out_of_line_data(cell).map_or(0, |(_, words)| words * 8) }}+/// The number of elements of an array.+///+/// # Safety+///+/// `array` must be a live array reference.+pub unsafe fn array_length(array: u64) -> u32 {+ unsafe { read::<u64>(array as *const c_void, OBJECT_LENGTH_OFFSET) as u32 }+}++/// The address and storage kind of the element at `index` of an array of numbers (whose elements are neither+/// references nor v128s).+unsafe fn numeric_array_element(array: u64, index: u32) -> (*mut u8, u8) {+ unsafe {+ let cell = array as *const c_void;+ let rtt = &*read::<*const Rtt>(cell, 0);+ assert_eq!(rtt.kind, RTT_KIND_ARRAY, "the object is an array");+ let kind = *rtt.field_kinds;+ assert!(+ matches!(kind, STORAGE_I8 | STORAGE_I16 | STORAGE_32 | STORAGE_64),+ "the array's elements are numbers"+ );+ assert!(index < array_length(array), "the index is in bounds");+ let data: *mut u8 = read(cell, OBJECT_DATA_OFFSET);+ (data.add(index as usize * rtt.size as usize), kind)+ }+}++/// The bits of the element at `index` of an array of numbers, with packed elements zero-extended.+///+/// # Safety+///+/// `array` must be a live array reference.+pub unsafe fn array_element(array: u64, index: u32) -> u64 {+ unsafe {+ let (address, _) = numeric_array_element(array, index);+ address.cast::<u64>().read()+ }+}++/// Set the element at `index` of an array of numbers to `bits`, which packed elements wrap.+///+/// # Safety+///+/// `array` must be a live array reference.+pub unsafe fn set_array_element(array: u64, index: u32, bits: u64) {+ unsafe {+ let (address, kind) = numeric_array_element(array, index);+ let mask = match kind {+ STORAGE_I8 => 0xff,+ STORAGE_I16 => 0xffff,+ STORAGE_32 => 0xffff_ffff,+ _ => u64::MAX,+ };+ address.cast::<u64>().write(bits & mask);+ }+}+/// An exception: a tag and the values thrown with it, in operand stack order (the last value in the lowest slot).pub struct Exception {pub tag: TagAddress,diff --git a/Libraries/LibWasm/Runtime.cpp b/Libraries/LibWasm/Runtime.cppindex 170ec69e71a..289c6dc220d 100644--- a/Libraries/LibWasm/Runtime.cpp+++ b/Libraries/LibWasm/Runtime.cpp@@ -542,6 +542,21 @@ 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)));diff --git a/Libraries/LibWasm/Runtime.h b/Libraries/LibWasm/Runtime.hindex f95b537f252..32c4a4c344d 100644--- a/Libraries/LibWasm/Runtime.h+++ b/Libraries/LibWasm/Runtime.h@@ -303,6 +303,14 @@ public:// 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);