Javascript C++ 绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12005610/
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
Javascript C++ binding?
提问by Homunculus Reticulli
I have some C++ code that I want to expose to client side of a web app. Ideally, I want to write Javascript wrapper objects for my C++ classes so that I can use them clientside.
我有一些 C++ 代码,我想向 Web 应用程序的客户端公开。理想情况下,我想为我的 C++ 类编写 Javascript 包装器对象,以便我可以在客户端使用它们。
Has this been done before?. Does anyone have a link to show how this may be achieved?
以前做过吗?。有没有人有一个链接来展示如何实现这一目标?
采纳答案by Mansuro
There is a library to convert C++ code to javascript, it might help: emscripten
有一个库可以将 C++ 代码转换为 javascript,它可能会有所帮助: emscripten
回答by Anand Rathi
Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey 1 . 8 . 5 and more
Libjspp 基于 C++ 模板的包装器,用于嵌入和扩展 Javascript 引擎 spidermonkey 1 。8 . 5个以上
SpiderMonkey? is Mozilla Project's Javascript/ECMAScript engine.
蜘蛛猴?是 Mozilla 项目的 Javascript/ECMAScript 引擎。
Libjspp allows C++ developers to embed SpiderMonkey? simply and easily into their applications. Libjspp allows to run multiple Javascript Engines within same process which suits one engine per thread para dime which is helpful in achieving true parallisim. Also Libjspp no way stops user from running multiple threads within engine.
Libjspp 允许 C++ 开发人员嵌入 SpiderMonkey?简单轻松地进入他们的应用程序。Libjspp 允许在同一进程中运行多个 Javascript 引擎,这适合每个线程的一个引擎,这有助于实现真正的并行化。此外,Libjspp 无法阻止用户在引擎内运行多个线程。
回答by OzB
This is an old topi, however, I was in the exact situation right now, and all of the solutions I found on the net complicated or outdated.
这是一个古老的主题,但是,我现在处于确切的情况,并且我在网上找到的所有解决方案都很复杂或过时。
Recently, I ran across a library which supports V8 engine (including the new isolation API, which makes 90% of the libraries I found outdated) and provides great exposure and interaction API.
最近碰到一个支持V8引擎的库(包括新的isolation API,让我发现90%的库都过时了),并且提供了很好的曝光和交互API。
https://github.com/QuartzTechnologies/v8bridge
https://github.com/QuartzTechnologies/v8bridge
I hope that my solution will help anybody.
我希望我的解决方案可以帮助任何人。
回答by Alan Mimms
回答by rightaway717
回答by John Watts
I think you want a C++ JSON parser. You should be able to find one here http://www.json.org/. It may not do all you want because it just serializes and deserializes C++ objects without any behavior, but it should be good enough. See https://stackoverflow.com/questions/245973/whats-the-best-c-json-parserfor some discussion.
我想你想要一个 C++ JSON 解析器。您应该可以在这里找到一个http://www.json.org/。它可能无法满足您的所有需求,因为它只是序列化和反序列化 C++ 对象而没有任何行为,但它应该足够好。有关一些讨论,请参阅https://stackoverflow.com/questions/245973/whats-the-best-c-json-parser。
回答by Lyubomir Vasilev
If the C++ code has to be on the client, then there is no simple way to do this for a web app. A solution may involve coding plugins for the browsers you want to support, which may then be accessed from javascript code.
如果 C++ 代码必须在客户端上,则没有简单的方法可以为 Web 应用程序执行此操作。一个解决方案可能涉及您想要支持的浏览器的编码插件,然后可以从 javascript 代码访问这些插件。
If, for example, you need this for a client application, that is another case. Such a thing has been done and involves linking your application to (or running from outside) with for example chromium library, or any other javascript execution engine. That way you can create bindings to C++ classes and use such objects from javascript and vice-versa. Note that this is also not a trivial solution and may be a big effort to implement (also requires additional resources).
例如,如果您需要将其用于客户端应用程序,那就是另一种情况。这样的事情已经完成,涉及将您的应用程序链接到(或从外部运行)与例如铬库或任何其他 javascript 执行引擎。这样您就可以创建到 C++ 类的绑定并使用来自 javascript 的此类对象,反之亦然。请注意,这也不是一个简单的解决方案,实施起来可能需要付出很大的努力(还需要额外的资源)。
回答by nutrina
You could for example wrap the C++ classes in PHP or Python, and then implement an API over HTTP to access the required functions.
例如,您可以将 C++ 类封装在 PHP 或 Python 中,然后通过 HTTP 实现 API 以访问所需的函数。
Or if you insist on exposing the functions as JavaScript you could try using Node.js, and create an C++ add-on to wrap you classes. See the Node.js documentation here: http://nodejs.org/api/addons.html#addons_wrapping_c_objects
或者,如果您坚持将函数公开为 JavaScript,您可以尝试使用 Node.js,并创建一个 C++ 附加组件来包装您的类。请参阅此处的 Node.js 文档:http: //nodejs.org/api/addons.html#addons_wrapping_c_objects
But either way, I don't think avoid creating some sort of API (HTTP SOAP, XML RPC) to access the functions on your server.
但无论哪种方式,我都不认为要避免创建某种 API(HTTP SOAP、XML RPC)来访问服务器上的功能。
回答by Constantinius
I guess that RPCis what you want. You'll need to wrap your functions on the server side using some sort of framework. I've not yet used it, but this onelooks promising.
我想RPC就是你想要的。您需要使用某种框架将您的功能包装在服务器端。我还没有使用它,但这个看起来很有希望。
On the client side you use proxy objects to dispatch the function calls. The communication is handled usually either via XML-RPC or JSON-RPC. I used this client side frameworkand was quite content but I'm sure you'll find many others.
在客户端,您使用代理对象来调度函数调用。通常通过 XML-RPC 或 JSON-RPC 处理通信。我使用了这个客户端框架并且很满意,但我相信你会发现很多其他的。