10

Please tell me, how to include a javascript header file or javascript function in C++ code. The C++ code is written in Linux(UBUNTU)?

Although i need to perform the above action only, but let me tell u my purpose of doing it, as i am intending to implement CTI (Computer Telephony Integration) operation.

(Help will be appreciated) Thanks a lot in advance

ratty
  • 13,216
  • 29
  • 75
  • 108
  • javascript depend on the browser.. doesn't it? – Sungguk Lim Apr 26 '10 at 12:13
  • 2
    @sunglim, JavaScript is a general-purpose programming language that happens to be prevalent in browsers. There is no dependency, as such. – Marcelo Cantos Apr 26 '10 at 12:15
  • 1
    @sunglim: Javascript is a dialect of ECMAscript. You will find many flavors of ECMAscript like Javascript (interpreted by different engines for browsers), Jscript.NET, Actionscript, etc. – Alerty Apr 26 '10 at 22:27

4 Answers4

15

Calling Scripting functions from C++

http://clipp.sourceforge.net/Tutorial/back_calling.html

JavaScript Calls from C++ - CodeGuru

http://www.codeguru.com/cpp/i-n/ieprogram/article.php/c4399/JavaScript-Calls-from-C.htm

JavaScript call from C++ - CodeProject

http://www.codeproject.com/KB/COM/jscalls.aspx

calling javascript from c++ code - JavaScript / Ajax / DHTML answers

http://bytes.com/topic/javascript/answers/759793-calling-javascript-c-code

Try All of above this.

schanq
  • 864
  • 1
  • 15
  • 25
ratty
  • 13,216
  • 29
  • 75
  • 108
6

You might want to port your JS to C++; this should be a fairly simple task, as the two languages are moderately alike.

Simply porting the functionality is likely to be far simpler than actually trying to use a JS parsing library, and likely less error prone.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • 1
    That's a good suggestion... also C# 4.0 is scarily close to JavaScript - just make all functions return a 'dynamic' and you're almost there.. see http://channel9.msdn.com/pdc2008/TL16/ – JBRWilkinson May 11 '10 at 09:53
  • It is probably feasible to automatically port JavaScript to C++, since there are already several [JavaScript compilers that target C](https://stackoverflow.com/a/31307856/975097). – Anderson Green Feb 09 '22 at 20:52
5

JavaScript is not a compiled language and it is not, by any stretch of the imagination, compatible with C++, so #include doesn't stand a chance of importing JavaScript code. In fact, the notion of a header file doesn't even exist in JavaScript.

There are several JavaScript engines that can be integrated into a compiled language, including:

  1. The Mozilla project's SpiderMonkey.
  2. Google Chrome's V8.
  3. A whole bunch of others.
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
2

A detailed tutorial for embedding JS in C++ via Mozilla's SpiderMonkey engine can be found here Basically you need to include jsapi.h, create/configure/cleanup the JS engine as the tutorial describes (populating the char* script with your string literal JS source code and passing the resulting character array to JS_EvaluateScript), and then link against the SpiderMonkey library when you build the executable for your system. Note that this tutorial goes on to explain how to call C functions from JS and how to call specific JS functions from C, which is also interesting and possibly more appropriate for the OP's situation.

CCJ
  • 1,619
  • 26
  • 41