This example package demonstrates compiling a AssemblyScript program, datastructures.ts
which demonstrates working with the AssemblyScript Map
and Set
data structures.
from the add
directory project root:
npm install
To compile the datastructures.ts
code to a WASM binary:
npm run build
A datastructures.wasm
file will be produced in the build
directory
Test the module outside of the context of the blockchain:
npm test
This will run NodeJS based tests in the test
directory. For example in datastructures.unit.spec.js
test instantiation of the compiled WASM module, and assertions to verify its API and behavior:
describe('Map', function () {
it('Set', function () {
//create a pointer to the string param "hello" we will pass in as the value
let paramPointer0 = module.__retain(module.__allocString("hello"));
//set the key 0 => "hello"
module.mapSet(0, paramPointer0);
module.__release(paramPointer0); //release the memory for the pointer
//get the value from memory
const pointer = module.mapGet(0);
const string = module.__getString(pointer);
module.__release(pointer);
assert.strictEqual(string, 'hello'); // => true!
});
...
});
Add the string at pointer x to the end of the internal Array
Remove the last element from the internal array and return the pointer to the string element
Remove the first element from the internal array and return the pointer to the string element
Get the pointer to the string at index x in the internal Array
Return the length of the internal Array
Add the string at pointer x to the internal Set
Check if the string at pointer x is contained in the internal Set. Return the integer representation of the boolean result (0/1)
Return the number of elements in the internal Set (cardinality)
Set the integer key k in the internal Map<i32, string> to string value at pointer v
Return the pointer to the string value in memory mapped to int k
Return the number of keys in the internal Map<i32, string>