@@ -127,9 +127,15 @@ impl<T: AscValue> TypedArray<T> {
127
127
& self ,
128
128
heap : & H ,
129
129
) -> 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
130
136
let data_start_with_offset = self
131
137
. data_start
132
- . checked_sub ( self . buffer . wasm_ptr ( ) ) // needed to get offset
138
+ . checked_sub ( self . buffer . wasm_ptr ( ) )
133
139
. ok_or_else ( || {
134
140
DeterministicHostError ( anyhow:: anyhow!(
135
141
"Subtract overflow on pointer: {}" , // Usually when pointer is zero because of null in AssemblyScript
@@ -266,9 +272,15 @@ impl<T: AscValue> Array<T> {
266
272
& self ,
267
273
heap : & H ,
268
274
) -> 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
269
281
let buffer_data_start_with_offset = self
270
282
. buffer_data_start
271
- . checked_sub ( self . buffer . wasm_ptr ( ) ) // needed to get offset
283
+ . checked_sub ( self . buffer . wasm_ptr ( ) )
272
284
. ok_or_else ( || {
273
285
DeterministicHostError ( anyhow:: anyhow!(
274
286
"Subtract overflow on pointer: {}" , // Usually when pointer is zero because of null in AssemblyScript
0 commit comments