7

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.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179

3 Answers3

13

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
  • 191
  • 1
  • 5
6

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
  • 60,510
  • 8
  • 78
  • 126
  • 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 May 22 '09 at 07:24
  • 2
    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 May 22 '09 at 13:43
  • 3
    "vastly faster than any js"? [citation needed]! Link please. – Sean McMillan Jul 12 '11 at 17:19
  • @Sean McMillan, unfortunately, good benchmarks are few and far between. Also, see the date: JS has evolved _a lot_ in the last couple of years. Still, Lua hasn't stayed still, and LuaJIT 2.0 has already surpassed Java for many high-performance tasks, in some (real) cases it's even better than most C compilers. – Javier Jul 12 '11 at 17:28
  • Actually, Lua isn't always faster. I believe that google v8 is much faster than lua. – Sam Bloomberg Oct 28 '11 at 13:18
  • V8 is usually somewhat faster than plain Lua; but nowhere near LuaJIT2 – Javier Oct 28 '11 at 15:44
  • 3
    LuaJIT2 beats V8 at about 4x. LuaJIT2 is almost near C level for most things. – Steven Mar 26 '13 at 20:59
3

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
  • 35,755
  • 9
  • 58
  • 55