This example package demonstrates compiling a simple AssemblyScript program, types.ts
which adds two numbers to WASM, executing it in a host environment, and accessing it's methods.
from the add
directory project root:
npm install
To compile the types.ts
code to a WASM binary:
npm run build
A types.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 types.unit.spec.js
test instantiation of the compiled WASM module, and assertions to verify its API and behavior:
describe('Strings', function () {
it('Get String', function () {
//use the __getString AssemblyScript wrapper to get the value from the pointer returned
const pointer = module.getString();
const string = module.__getString(pointer);
//release the result pointer
module.__release(pointer);
assert.strictEqual(string, "It's Alive!!!");
});
...
});
Add two integers, return the result
Return the double representation of 1.001
Returns the float result of adding two floating point numbers together
Return the integer representation of true (1)
Return the integer representation of false (0)
Return the integer representation of i32 x
Return the boolean negation of integer representation of boolean x
Return the integer representation of boolean x or'd with boolean y
Returns a pointer in memory to a string "It's Alive!!!"
Appends an exclamation point on the end of string pointer x
Appends string pointer x to string pointer y and returns a pointer to the result
Returns a pointer in memory the integer array [1, 1, 3, 5, 8]
Returns the length of the array at pointer x