/**
* Node.js test action to check Emscripten compiler to WebAssembly
*
* Checked with VMware vRealize Automation 8.5.1 with Node.js 12.22.1
* and VMware Aria Automation 8.18.0 with Node.js 20.11.0
*
* @param sqrtFrom {number}
* @returns CompositeType(
* status {string},
* result {number}
* ):test
*/
exports.handler = (context, inputs, callback) => {
let sqrtFrom = inputs.sqrtFrom;
const _Module = require("./helloWorld.js");
_Module.onRuntimeInitialized = () => {
// Try C function helloPrint
// Using vdirect call
_Module._helloPrint();
// Using ccall: calls a compiled C function with specified
// parameters and returns the result.
_Module.ccall(
"helloPrint", null, ["string"], ["Gabi"]
);
// Using cwrap: wraps a compiled C function and returns a JavaScript
// function you can call normally. This is therefore more useful if
// you plan to call a compiled function a number of times.
const helloPrint = _Module.cwrap(
"helloPrint", null, ["string"]
);
helloPrint("Stefan");
// Try C function helloReturn
let ret = 0;
const retLength = 64;
// Using ccall
ret = _Module.ccall(
"calloc", "number", ["number"], retLength
);
_Module.ccall(
"helloReturn",
null,
["string", "number", "number"],
["", ret, retLength]
);
console.log(_Module.UTF8ToString(ret));
_Module.ccall(
"free", null, ["number"], ret
);
ret = _Module.ccall(
"calloc", "number", ["number"], retLength
);
_Module.ccall(
"helloReturn",
null,
["string", "number", "number"],
["Hugo", ret, retLength]
);
console.log(_Module.UTF8ToString(ret));
_Module.ccall(
"free", null, ["number"], ret
);
// Using cwrap
const calloc = _Module.cwrap(
"calloc", "number", ["number"]
);
const free = _Module.cwrap(
"free", null, ["number"]
);
const helloReturn = _Module.cwrap(
"helloReturn", null, ["string", "number", "number"]
);
ret = calloc(retLength);
helloReturn("", ret, retLength);
console.log(_Module.UTF8ToString(ret));
free(ret);
ret = calloc(retLength);
helloReturn("Fritz", ret, retLength);
console.log(_Module.UTF8ToString(ret));
free(ret);
// Try C function int_sqrt
let result = _Module._int_sqrt(4);
console.log(result);
console.log(
_Module.ccall(
"int_sqrt", "number", ["number"], [25]
)
);
const intSqrt = _Module.cwrap(
"int_sqrt", "number", ["number"]
);
console.log(intSqrt(36));
let fromSqrt = intSqrt(sqrtFrom);
console.log(fromSqrt);
callback(undefined, {status: "done", result: fromSqrt});
}
}
|