41

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.

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

  • google's V8 javascript engine
  • mozilla's SpiderMonkey

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

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

[UPDATED] as Kostis mentioned, Apple introduced JavascriptCore in WWDC 2013

dennycd
  • 415
  • 1
  • 7
  • 11
  • Any reason you don't want to use JavaScriptCore (http://www.webkit.org/projects/javascript/index.html). It sounds like it can be built as a static library and provides Obj-C bindings. – Justin Niessner Mar 15 '11 at 20:47
  • Hi Justin, the JavaScriptCore source code in WebKit can be built on Mac OS X, however, it seems non-trivial to build it for iOS, but I will give it a try later – dennycd Mar 16 '11 at 06:01
  • Hi Denny, did you manage get anything to work for iOS?? – phi Apr 14 '11 at 08:10

4 Answers4

25

There are two projects you might be interested in:

  1. JavaScriptCore-iOS
  2. iMonkey

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.

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

Enjoy.

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
  • Note that JavaScriptCore appears to be LGPL v2 which makes it questionable for closed source iOS apps unless you provide the .o files so someone can relink your app with a different version of the JavaScriptCore library (ask your lawyer, this is not legal advice). "If you link a program with the library, you must provide complete object files to the recipients so that they can relink them with the library, after making changes to the library and recompiling it." – Dad Aug 08 '11 at 18:15
  • 1
    thanks Johannes, I never know there's a button for that :-0 . Thanks everyone! I'm quite surprised that there's a lot of good answers here. As to the progress of doing the embedding, I followed your suggestion of the JavaScriotCore-iOS project and yes I am able to compile and run the native javascript interpreter on iOS . – dennycd Nov 29 '11 at 22:35
  • re the licensing, it might be worth pinging the developer to see if that's the model they really intended. since it's intended for iOS, distributing the .o's seems like a weird restriction. – orion elenzil Oct 08 '12 at 15:24
14

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!

Unfortunately, documentation is really poor at the moment. You can find a presentation from the WWDC 2013 event and 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.

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.

Hope this will help someone :)

Kostis
  • 1,593
  • 15
  • 16
4

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

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Nik
  • 9,063
  • 7
  • 66
  • 81
1

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.

ccxvii
  • 1,873
  • 1
  • 13
  • 11