Embed a JavaScript Engine in an iOS Application

Embed a JavaScript engine in an iOS application

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 :)

V8 JavaScript Engine and Mac App Store

If you embed your own interpreter engine (any programming language), you will have to disable JIT (or any other dynamic executable code generation), as writing executable code will not work in the app sandbox on stock OS iOS devices. Compiler engines are not allowed. An app with an interpreter also can not have any code download capability, or it will be rejected by Apple. So you will have to embed your complete game with your interpreter for submission to Apple's App store.

But there are many apps approved and in the app store with embedded interpreters (Basic, for instance).

Is Google's V8 JavaScript engine available for iOS?

Apple requires that you use WebKit when rendering web-content in an app, and they may reject your app if you use something else.

That said, you may want JavaScript for something other than rendering a webpage, and in theory that use-case should allow you to use whatever JavaScript engine you want. As the V8 source-code is available in C++, it should be possible to compile it into an iOS project. To do so you'll need to rename any .m files that make use of V8 functionality (either directly or indirectly through transitive dependencies) to .mm so that XCode knows to compile those files as Objective-C++.

This process of getting it to work will likely be a bit finicky, but in theory it should be possible.

Am I allowed to run a javascript runtime (like v8) on the iPhone?

I think your interpretation is correct - You would not be allowed to download and execute JavaScript code in v8.

If there were some way to run the code in an interpreter already on the iPhone (i.e. the javascript engine in MobileSafari) then that would be permitted I think.

Which Javascript engine would you embed in your application?

Mozilla's SpiderMonkey is fairly easy and well-documented. It's a C API, but it's straightforward to wrap it in C++. It can be compiled to be thread-safe, which is useful for games since you'd likely want to have your main logic in one thread and user interface logic in a second thread.

Google's V8 might be a good choice, since you're using C++, but I have no experience with it yet. According to the documentation (thanks to Daniel James), V8 is not thread-safe, although this may change in the future.

There's also WebKit's SquirrelFish, but I couldn't find a standalone version of that when I was looking earlier.

JavaScript engine to enable scripting support / application automation

Whoever told you to use node.js does not understand your use case.

If you want to embed a script engine - you can embed V8. Node JS is a whole platform that brings V8 together with a library for asynchronous io called libuv, and other tools and libraries dedicated to making networking and server building easy.

It's not nearly as easy to embed as V8 itself and it's not as fun. The performance would likely be very similar and maintenance would be a lot harder with node.

Embedding JS Engine into Windows Phone applications

Unfortunately, no. The Chakra APIs are not approved for store apps at this time.

Javascript for Add to Home Screen on iPhone?

Yes. The majority of modern browsers support the Add to Home screen (or A2HS) feature for Progressive Web Apps. To quote the Mozilla Web Docs article:

Add to Home screen is a feature available in
modern browsers that allows a user to "install" a web app, ie. add a
shortcut to their Home screen.

See also: A2HS browser support at caniuse.com



Related Topics



Leave a reply



Submit