3

I'd like to find a way to call Javascript functions from C. Are there any language bindings available for this purpose? I'm trying to make a library of Javascript functions accessible from C. (Something like a C -> Javascript foreign function interface would be suitable for this purpose, but I haven't been able to find one so far.)

Anderson Green
  • 563
  • 6
  • 13
  • There are a few Javascript-to-C++ bindings available, but I need my Javascript functions to be invoked from C, not C++. – Anderson Green Aug 02 '12 at 17:13
  • 1
    Javascript doesn't make much sense without an engine. What exactly is the problem you're trying to solve here? Code reuse, or browser automation? – Jordan Aug 02 '12 at 17:19
  • I'm w/ @Jordan, what is the problem you are trying to solve? What platforms? Why C and not C++? – DevSolo Aug 02 '12 at 18:07
  • I'm aiming for code reuse, not browser automation. – Anderson Green Aug 02 '12 at 18:10
  • Also, I'd like it to be cross-platform. – Anderson Green Aug 02 '12 at 18:11
  • Can you please tell us what you're trying to do, and why these core functions are written in JS? If you really want JS reuse, then Node.js may be an option for you. – Jordan Aug 02 '12 at 19:09
  • I'm trying to make the Javascript API accessible from as many programming languages as possible for the purpose of code reuse. One method of doing this is by writing a C API, as described here: http://programmers.stackexchange.com/a/157605/57752 – Anderson Green Aug 02 '12 at 20:35
  • What you're looking for isn't really a foreign function interface (you'd have to link your C code against javascript sources compiled into machine code), but a C library that can run javascript (basically, a C function that you can pass a chunk of javascript to run it). – tdammers Aug 02 '12 at 21:10

1 Answers1

6

There's quite a few Javascript engines you can use. Which one makes most sense will largely depend on your specific platform/target environments/needs.

There's the V8 javascript engine that powers Chrome, and Firefox's SpiderMonkey.

If Windows-only would suffice, you can use Active Scripting (IActiveScript site and other interfaces). A number of my Windows applications do this.

If QT, I believe they have their own JavaScript classes that can be accessed.

Thats not an exhaustive list, I'm sure there are others. But that should get your search started.

GrandmasterB
  • 37,990
  • 7
  • 78
  • 131
  • +1 for V8. I think that it's written in C++, you you could probably pretty easily wrap it in a C interface (if it doesn't have already). – Linuxios Aug 03 '12 at 13:17