Skip to content

Commit 7f29b82

Browse files
committed
runtime: Add explanation on offset of ArrayBufferView memory layout (api version 0.0.5)
1 parent cc5ce84 commit 7f29b82

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

runtime/wasm/src/asc_abi/v0_0_5.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,15 @@ impl<T: AscValue> TypedArray<T> {
127127
&self,
128128
heap: &H,
129129
) -> Result<Vec<T>, DeterministicHostError> {
130+
// This subtraction is needed because on the ArrayBufferView memory layout
131+
// there are two pointers to the data.
132+
// - The first (self.buffer) points to the related ArrayBuffer.
133+
// - The second (self.data_start) points to where in this ArrayBuffer the data starts.
134+
// So this is basically getting the offset.
135+
// Related docs: https://www.assemblyscript.org/memory.html#arraybufferview-layout
130136
let data_start_with_offset = self
131137
.data_start
132-
.checked_sub(self.buffer.wasm_ptr()) // needed to get offset
138+
.checked_sub(self.buffer.wasm_ptr())
133139
.ok_or_else(|| {
134140
DeterministicHostError(anyhow::anyhow!(
135141
"Subtract overflow on pointer: {}", // Usually when pointer is zero because of null in AssemblyScript
@@ -266,9 +272,15 @@ impl<T: AscValue> Array<T> {
266272
&self,
267273
heap: &H,
268274
) -> Result<Vec<T>, DeterministicHostError> {
275+
// This subtraction is needed because on the ArrayBufferView memory layout
276+
// there are two pointers to the data.
277+
// - The first (self.buffer) points to the related ArrayBuffer.
278+
// - The second (self.buffer_data_start) points to where in this ArrayBuffer the data starts.
279+
// So this is basically getting the offset.
280+
// Related docs: https://www.assemblyscript.org/memory.html#arraybufferview-layout
269281
let buffer_data_start_with_offset = self
270282
.buffer_data_start
271-
.checked_sub(self.buffer.wasm_ptr()) // needed to get offset
283+
.checked_sub(self.buffer.wasm_ptr())
272284
.ok_or_else(|| {
273285
DeterministicHostError(anyhow::anyhow!(
274286
"Subtract overflow on pointer: {}", // Usually when pointer is zero because of null in AssemblyScript

0 commit comments

Comments
 (0)