aboutsummaryrefslogtreecommitdiff
path: root/tests/Module-exports/main.js
blob: 20f362125bd6256087b0ee0c0142c9a762cdd510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var Module = require("./test.js");

console.log("\nTesting main.js");

var length = 20;
var ptr = Module._malloc(length); // Get buffer from emscripten.
var buffer= new Uint8Array(Module.HEAPU8.buffer, ptr, length); // Get a bytes view on the newly allocated buffer.

// Populate the buffer in JavaScript land.
console.log("buffer length = " + length + "\n");
for (var i = 0; i < length; i++) {
    buffer[i] = i + 20; // Add 20 just for a bit of interest.
    console.log("setting buffer[" + i + "] = " + buffer[i]);
}

// Export bufferTest function.
var bufferTest = Module.cwrap('bufferTest', 'number', ['number', 'number']);
console.log("\ncalling bufferTest\n");

bufferTest(ptr, length); // Call our exported C function to prove the buffer was passed correctly.

console.log("\nbufferTest finished\n");

// free the heap buffer
Module._free(ptr);