views:

1272

answers:

4

I have a C++ desktop application (written in wxWidgets) and I want to add support for some scripting language.

Scripting would mostly be used for run-time conversions of strings, numbers and dates by user supplied JavaScript code.

I'd like to use JavaScript because it is widely used and everyone is familiar with the syntax.

Googling around, it seems I have two options:

  • SpiderMonkey from Mozilla
  • JavaScriptCore from WebKit

Has anyone tried those? Which one would be easier to set up? Do you know of some other implementation that is better for my needs?

BTW, I target Windows and Linux platforms.

+1  A: 

JavaScriptCore has a stable C API (and ABI), and has been available (and used as) a standard system framework on macos.

[edit: oh, and it works on linux and windows as a standalone library, although i believe only debian distributes it as such]

olliej
A: 

Of course, you could also use Lua, which not only is designed specifically for this, it's vastly faster than any JS.

Also, it's has well-designed semantics, a very minimal core, simple C API, great portability, a very mature JIT, the most helpful online community I've seen, etc...

Javier
I did consider Lua, but I would like to save users of my application from learning a new language. The other potential problem is lack of functions to deal with date/time. AFAICT, you can only get the system time, and that's all. However, my application needs to be able to parse string input as date, and then do stuff like "add 3 days" or "find difference in days between 2 dates" etc.
Milan Babuškov
regardless of which language you embed into your app, you have to expose some special API. adding some date managing capabilities can be done in less than 100 lines of Lua. The 'no new language' is a real issue, OTOH.
Javier
A: 

Is thee a crossplatform API for JavaScriptCore. How can I embed the JavaScriptCore engine in a Windows C++ applicaton

Yes, JavaScriptCore has a stable C API (both API and ABI is stable as it's actually used by other applications). You can pull the JavaScriptCore directory from the repository at webkit.org (you probably want to use one of the stable safari branches). It should "Just Work"
olliej
+3  A: 

There is also Google's V8 JavaScript engine, builds nicely on Linux, embedding API seems quite straightforward too: (Compared to SpiderMonkey's, never looked at the JavaScriptCore API) http://code.google.com/apis/v8/get_started.html

nextdayflight