This example package demonstrates compiling a C program, host.c
which demonstrates calling imported functions residing on the host from C code.
This example also demonstrates returning dynamic values via reference from the host. For example, from a JS host:
- The host defines a reference to an integer:
let hostInteger = 123;
- The host defines an imported function for the WASM environment:
getInteger(){return hostInteger;}
, which returns the integer reference - The host calls a function in the WASM contract that echoes the result of
getInteger()
called from C code. The result is 123 - The host changes the host integer:
hostInteger = 999;
- Repeating step 3 results in 999
emcc is required to compile the host.c
code to a WASM binary:
npm run build
A host.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 host.unit.spec.js
test instantiation of the compiled WASM module, and assertions to verify its API and behavior:
describe('Integers', function () {
it('Echo Host Integer', async function () {
const integer = wasm.instance.exports._echoHostInt();
assert.strictEqual(integer, 99);
});
...
});
Return an integer value from the host function _getInteger()
Return a the pointer to a string value from the host function _getString()