-
Notifications
You must be signed in to change notification settings - Fork 2
WASM API
Internally, the library will create 2 buffers when initialized. One for input values and one for output results, after the library finishes the calculations. The library reserves some space for its own use in both input and output buffers. As a result, if you want to write some input data into the input buffer, you should not write from the zero index, but ask the library for the offset. The library provides an offset that points you to the correct index in the buffer, from which you can start writing your data.
The workflow is simple - you input the raw input values, like speed, or position on X/Y/Z into predefined positions in the input buffer. Then call a synchronous function of the library that does the calculation, and finally read the result values from the output buffer.
Before you write into input buffer, or read from output one, you should first get the buffer offset value from the library that will point you to the correct place in the buffer. For this purpose the library exports two constants, one for each buffer:
export const INPUT_BUFFER_OFFSET: u8;
export const OUTPUT_BUFFER_OFFSET: u8;
And here is how you can grab and use them from Javascript:
// grab the pointers to input and output buffers
const inputPtr = ballistics.getInputBufferPtr();
const outputPtr = ballistics.getOutputBufferPtr();
// get the offsets from where we can read/write our data
const inputOffset = ballistics.INPUT_BUFFER_OFFSET.valueOf();
const outputOffset = ballistics.OUTPUT_BUFFER_OFFSET.valueOf();
// ask AssemblyScript loader to get array buffer views, so we can read/write into buffers
const input = __getArrayView(inputPtr);
const output = __getArrayView(outputPtr);
// now we can write data directly into buffers in WASM
let offset = inputOffset;
input[offset++] = 10;
input[offset++] = 4;
input[offset++] = 5;
// or read from
offset = outputOffset;
const a = output[offset++];
const b = output[offset++];
Once you are done with the buffers, you should ask AssemblyScript loader to release the pointers:
__release(inputPtr);
__release(outputPtr);
The zero indexes of input and output buffers below are assumed starting from INPUT_BUFFER_OFFSET
and OUTPUT_BUFFER_OFFSET
. In other words:
0 == INPUT_BUFFER_OFFSET
1 == INPUT_BUFFER_OFFSET + 1
2 == INPUT_BUFFER_OFFSET + 2
// etc
All input and output values in buffers are of 64 bit float
. If you use Javascript, then it is a default type for Number
, so you don't need to do any special type conversions.
Use sidebar on the right to navigate through API.
Ballistics (c) 2020