- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3
Example: Square
        Craig Minihan edited this page May 7, 2015 
        ·
        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"
int main() {
    rs::jsapi::Runtime rt;
    // define a global function called echo which
    // prints the first argument on stdout
    rs::jsapi::Global::DefineFunction(rt, "echo",
        [](const std::vector<rs::jsapi::Value>& args, rs::jsapi::Value& result) {
            if (args.size() > 0) {
                std::cout << args[0] << std::endl;
            }
        });
    // invoke echo passing a string
    rs::jsapi::FunctionArguments args(rt);
    args.Append("Hello world!!");
    rt.Call("echo", args);
    // call the function from JavaScript
    rt.Evaluate("echo('lorem ipsum...');");
    return 0;
}