在 iOS 应用程序中嵌入 JavaScript 引擎

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5317776/
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 16:38:08  来源:igfitidea点击:

Embed a JavaScript engine in an iOS application

javascriptiosiphonev8

提问by dennycd

I wonder if anyone has successfully ported a javascript engine/interpreter to iOS. I'm writing an iPhone game that I would like to use Javascript as the high-level scripting language (AI, gameplay, etc.), but to do that, I need to compile the JS engine into a static library and link it against my objectiveC program for iPhone OS. There are some candidate JS engine I'm looking at but I couldn't find any successful cases for doing that.

我想知道是否有人成功地将 javascript 引擎/解释器移植到 iOS。我正在编写一个 iPhone 游戏,我想使用 Javascript 作为高级脚本语言(AI、游戏玩法等),但要做到这一点,我需要将 JS 引擎编译成一个静态库并将其链接到我的 iPhone 操作系统的objectiveC 程序。我正在寻找一些候选 JS 引擎,但我找不到任何成功的案例。

Here're the js engine I was hoping to use for iOS

这是我希望用于 iOS 的 js 引擎

  • google's V8 javascript engine
  • mozilla's SpiderMonkey
  • 谷歌的 V8 javascript 引擎
  • mozilla 的 SpiderMonkey

The alternative is to use UIWebView'sJavascript callback interface, but that requires loading the entire UIWebViewinto memory and the experiences I heard is that it is usually slow in performance.

另一种方法是使用UIWebView'sJavascript 回调接口,但这需要将整个加载UIWebView到内存中,我听到的经验是它通常性能很慢。

Appreciate if anyone had similar experiences of do this or know any references for that!

不胜感激,如果有人有类似的经验这样做或知道任何参考!

[UPDATED] as Kostis mentioned, Apple introduced JavascriptCorein WWDC 2013

[更新] 正如 Kostis 提到的,Apple在 WWDC 2013 中引入了JavascriptCore

采纳答案by Kostis

For future viewers, now there is the JavaScriptCore framework, introduced in the new iOS 7. It does magic! Wrapping, unwrapping values from/to JS/Objective-C, calling functions, callbacks, everything!

对于未来的观众,现在有了 JavaScriptCore 框架,它在新的 iOS 7 中引入。它真的很神奇!包装,从/向 JS/Objective-C 解包值,调用函数,回调,一切!

Unfortunately, documentation is really poor at the moment. You can find a presentation from the WWDC 2013 eventand some more info in the header files (cmd+click on the header file name in Xcode). There are also some tutorial around the internets which just copy what the guy in the WWDC presentation does.

不幸的是,目前文档确实很差。您可以在WWDC 2013 活动中找到演示文稿,并在头文件中找到更多信息(cmd+单击 Xcode 中的头文件名)。互联网上也有一些教程,它们只是复制 WWDC 演示文稿中的人所做的。

I've used it for one of my projects, it's really powerful. The only think I didn't like is that it passed objects from JS to ObjC by value, i.e. reference was lost. There might be a workaround for this, but I couldn't find anything without proper documentation.

我已经将它用于我的一个项目,它真的很强大。我唯一不喜欢的想法是它通过值将对象从 JS 传递到 ObjC,即引用丢失了。可能有一个解决方法,但如果没有适当的文档,我找不到任何东西。

Hope this will help someone :)

希望这会帮助某人:)

回答by Johannes Fahrenkrug

There are two projects you might be interested in:

您可能感兴趣的两个项目:

  1. JavaScriptCore-iOS
  2. iMonkey
  1. JavaScriptCore-iOS
  2. 猴子

It is absolutely possible to build and ship a JavaScript engine with your iOS app, see http://www.phoboslab.org/log/2011/04/ios-and-javascript-for-real-this-time. Titanium does that, too: https://github.com/appcelerator/webkit_titanium/tree/master/Source/JavaScriptCore. Also see http://www.phoboslab.org/log/2011/06/javascriptcore-project-files-for-ios.

绝对有可能使用您的 iOS 应用程序构建和发布 JavaScript 引擎,请参阅http://www.phoboslab.org/log/2011/04/ios-and-javascript-for-real-this-time。Titanium 也这样做:https: //github.com/appcelerator/webkit_titanium/tree/master/Source/JavaScriptCore。另请参阅http://www.phoboslab.org/log/2011/06/javascriptcore-project-files-for-ios

I've also made a small app that shows how to use JSC on iOS: https://github.com/jfahrenkrug/AddressBookSpy

我还制作了一个小应用程序,展示了如何在 iOS 上使用 JSC:https: //github.com/jfahrenkrug/AddressBookSpy

Enjoy.

享受。

回答by Nik

Take a look at the open source project Ejecta, which embeds JavaScriptCore. It provides the HTML5 canvas API, and uses OpenGL & OpenAL.

看看嵌入 JavaScriptCore 的开源项目Ejecta。它提供 HTML5 画布 API,并使用 OpenGL 和 OpenAL。

回答by ccxvii

If you eventually want to port your game to something other than iOS (or you have other reasons to avoid Apple's proprietary interfaces) you can use MuJS. It's a small library providing a JavaScript interpreter that is easy to embed and extend.

如果您最终想将您的游戏移植到 iOS 以外的其他平台(或者您有其他理由避免使用 Apple 的专有接口),您可以使用MuJS。它是一个小型库,提供易于嵌入和扩展的 JavaScript 解释器。