Using embedded SpiderMonkey in my C++ application I would like to extract all the strings from JavaScript code. JavaScript code looks something like this:
var foo = "something";
var space = " ";
var bar = foo + space + "beautiful";
C++ code looks like this:
char *script = "var foo = \"something\"; var space = \" \"; var bar = foo + space + \"beautiful\";";
ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, &rval);
So my questions is, after SpiderMonkey executes JavaScript, how can I extract the string from variable bar (extracted value should be "something beautiful") and use it in my regular C++ code? I guess I have to evaluate the script first and then somehow extract the string from the JavaScript variable. I don't know how to extract the string using SpiderMonkey.
My second question:
http://siliconforks.com/doc/parsing-javascript-with-spidermonkey/
This SpiderMonkey JavaScript parser is written for SpiderMonkey 1.6.
How can this be done with latest SpiderMonkey, because APIs for
parsing have changed?
Thnx in advance,
Goran
Since bar is property of global object, after the JS_EvaluateScript() I can use JS_GetProperty() function, something like this
JS_GetProperty(cx, global, "bar", &rval);
JSString *str = JS_ValueToString(cx, rval);
printf("%s\n", JS_EncodeString(cx, str));