11

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

3
  • javascript depend on the browser.. doesn't it? Commented Apr 26, 2010 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. Commented Apr 26, 2010 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. Commented Apr 26, 2010 at 22:27

4 Answers 4

16

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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 channel9.msdn.com/pdc2008/TL16
It is probably feasible to automatically port JavaScript to C++, since there are already several JavaScript compilers that target C.
4

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.