简体   繁体   中英

extracting strings from JavaScript code

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));

Check String operation functions like subString. It can be used to resolve your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM