1

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

4
  • Please be more clear on the context in which you need to extract. If you are talking about passing content of client-side JS variables to server side C++ you need to send the vars to the server using http (ajax, form) Commented Aug 31, 2011 at 8:50
  • @mplungjan I wasn't very precise. I have embedded SpiderMonkey in my C++ app. Short snippet: char *script = "var foo = 'some string'"; ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, &rval); How can I now get to the value of JavaScript variable foo? Commented Aug 31, 2011 at 9:12
  • @mplungjan Updated. Do You know to solve my problem? Maybe You can give me some directions so I can investigate further? Thank You. Commented Aug 31, 2011 at 11:51
  • I think this is a duplicate stackoverflow.com/questions/639514/… More here: stackoverflow.com/… I have no idea myself Commented Aug 31, 2011 at 14:22

2 Answers 2

0

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));
Sign up to request clarification or add additional context in comments.

1 Comment

The above example NO LONGER WORKS with the latest Mozilla JavaScript version 38 or newer (both js and xpcshell)
-1

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

1 Comment

substring. Not subString - and I doubt that is what he needs.

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.