1

I have the following javascript code as string

std::string script = " { function execute() { var x=10; return x; } } ";

I want to evaluate this script and return the value , is there any way to do this?

Thanks in advance.

1 Answer 1

3

To evaluate a JS script you will need

JSBool JS_EvaluateScript(JSContext *cx, JSObject *obj, const char *src, uintN length, const char *filename, uintN lineno, jsval *rval);

As described here : JS_EvaluateScript - SpiderMonkey | MDN

Then if you want to get the returned value you have to call the exectue function :

"function execute(){ var x = 10; return x;} execute();"

Then to evaluate the script have to be a const char* type not a std::string

After evaluating the script you can get the returned value using rval

if(JSVAL_IS_INT(rval))
  printf("The returned value is : %d\n", JSVAL_TO_INT(rval));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.