Skip to content

Commit

Permalink
as: simplify and document getArrayPtr
Browse files Browse the repository at this point in the history
As mentioned in
bytecodealliance#12 (comment),
it is acceptable to use the `dataStart` field.
  • Loading branch information
abrown committed Jul 7, 2021
1 parent 0b6947c commit c950475
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions assemblyscript/assembly/as-wasi-nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
*/
import * as wasi_ephemeral_nn from './wasi_ephemeral_nn';

function getArrayPtr<T>(data: T): u32 {
// Only typed arrays have byteOffset. Cast to typed.
let u8Data = Uint8Array.wrap(data.buffer);
return (changetype<u32>(u8Data.buffer) + u8Data.byteOffset);
}

export class Graph {
private constructor(private pointer: i32) { }

Expand Down Expand Up @@ -140,3 +134,16 @@ export class WasiNnError extends Error {
this.name = "WasiNnError";
}
}

/**
* Helper function to capture the pointer to the beginning of an array.
* @param data an array
* @returns a pointer to the array data
*/
// @ts-ignore: decorator
@inline
function getArrayPtr<T>(data: T[]): usize {
// Use the documented `dataStart` field; see
// https://www.assemblyscript.org/memory.html#arraybufferview-layout.
return data.dataStart;
}

0 comments on commit c950475

Please sign in to comment.