JSContext* context = JS_NewContext(8 * 1024 * 1024); if (!context) { // handle error }
JS::RootedValue result(context); if (!JS_EvaluateScript(context, "2+2", strlen("2+2"), "filename", 1, &result)) { // handle error } if (result.isNumber()) { printf("%f\n", result.toNumber()); }This example evaluates the JavaScript code "2+2" and stores the result in a RootedValue object. If the script evaluation fails, the code handles the error accordingly. If the result is a number, the code prints the result to the console. Package/Library: SpiderMonkey (provided by Mozilla)