You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Craig Minihan edited this page Dec 7, 2016
·
12 revisions
The following example defines a function called square which squares the first parameter and returns the result. The function is then invoked via JSAPI and from JavaScript.
#include<iostream>
#include"libjsapi.h"intmain() {
rs::jsapi::Context cx;
// define a function which squares the first argument and returns the resultrs::jsapi::Global::DefineFunction(cx, "square",
[](const std::vector<rs::jsapi::Value>& args, rs::jsapi::Value& result) {
if (args.size() > 0) {
result = args[0].toNumber() * args[0].toNumber();
}
});
// call the function via JSAPI
rs::jsapi::FunctionArguments args(cx);
args.Append(5);
rs::jsapi::Value result(cx);
cx.Call("square", args, result);
std::cout << result << std::endl;
// call the function from JavaScript and return the result
cx.Evaluate("(function(){return square(9);})();", result);
std::cout << result << std::endl;
}