如何在 C++ 中执行 Javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2713289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:35:55  来源:igfitidea点击:

How to execute Javascript function in C++

javascriptc++linuxubuntu

提问by Williham Totland

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

请告诉我,如何在 C++ 代码中包含 javascript 头文件或 javascript 函数。C++代码是用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.

虽然我只需要执行上述操作,但让我告诉你我这样做的目的,因为我打算实施 CTI(计算机电话集成)操作。

(Help will be appreciated) Thanks a lot in advance

(帮助将不胜感激)非常感谢

回答by ratty

Calling Scripting functions from C++

从 C++ 调用脚本函数

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

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

JavaScript Calls from C++ - CodeGuru

来自 C++ 的 JavaScript 调用 - CodeGuru

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

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

JavaScript call from C++ - CodeProject

来自 C++ 的 JavaScript 调用 - CodeProject

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

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

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

从 C++ 代码调用 javascript - JavaScript/Ajax/DHTML 答案

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

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

Try All of above this.

尝试以上所有这些。

回答by Williham Totland

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

可能想将你的 JS 移植到 C++;这应该是一项相当简单的任务,因为这两种语言有一定的相似性。

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

简单地移植功能可能比实际尝试使用 JS 解析库简单得多,而且可能更不容易出错。

回答by Marcelo Cantos

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

JavaScript 不是一种编译语言,而且无论如何它都不与 C++ 兼容,因此#include没有机会导入 JavaScript 代码。事实上,在 JavaScript 中甚至不存在头文件的概念。

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

有几种 JavaScript 引擎可以集成到编译语言中,包括:

  1. The Mozilla project's SpiderMonkey.
  2. Google Chrome's V8.
  3. A whole bunch of others.
  1. Mozilla 项目的SpiderMonkey
  2. 谷歌浏览器的V8.
  3. 一大群

回答by CCJ

A detailed tutorial for embedding JS in C++ via Mozilla's SpiderMonkey engine can be found hereBasically 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.

可以在这里找到通过 Mozilla 的 SpiderMonkey 引擎在 C++ 中嵌入 JS 的详细教程基本上你需要包含 jsapi.h,按照教程的描述创建/配置/清理 JS 引擎(用你的字符串文字 JS 源代码填充 char* 脚本并将结果字符数组传递给 JS_EvaluateScript),然后在为系统构建可执行文件时链接到 SpiderMonkey 库。请注意,本教程继续解释如何从 JS 调用 C 函数以及如何从 C 调用特定的 JS 函数,这也很有趣,可能更适合 OP 的情况。