JavaScriptCore Project Files for iOS
Some weeks ago I released a compiled version of the JavaScriptCore library for iOS as part of my game engine. Since then I had many people asking for the project files necessary to build the library - and I promised to release them as well. But before I do so, let me just rant about Apple's politics real quick:
They suck.
You see, JavaScriptCore is an Open Source library. In fact the whole WebKit project is. It's licensed under BSD and LGPL licenses. The latter of which requires that if you modify the software you have to release your modified version – including the complete source – under the LGPL as well. Furthermore, it states: "For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library."
Yet the JavaScriptCore sources that you get from Apple's Open Source page come without a project file. So while you get the source code for the library, it's useless because you can't compile it.
Luckily you can also get the current JavaScriptCore sources directly from the SVN repository – and behold, they even come with an Xcode project file, ready to be build for MacOSX with the touch of a button. But – and here's the kicker – this project file curiously misses the iOS platform target. You can't build it for the iPhone or iPad.
Adding this iOS target to the project file is by no means a trivial endeavor, mind you. Especially not in Xcode. I ended up duplicating the MacOSX target, setting the Base SDK to Latest iOS, changing the Supported Platforms to iphoneos iphonesimulator and poking around in the project file with a text editor to change the productType to library.static, because you're not allowed to build a framework for iOS.
After some more changes that I can't remember (I'll be sure to document this next time I do it), I was finally able to compile the library for iOS. I submitted my game Biolab Disaster to the App Store only to see that it was rejected the next day.
JavaScriptCore utilizes the libicucore library to sort strings in a unicode fashion. Apparently libicucore is a "private" API on iOS – which is curious because I can add this library to my iOS project without any dirty hacks, I'm just not allowed to use any functions of it. So I compiled JavaScriptCore again, this time setting the UCONFIG_NO_COLLATION preprocessor macro to disable unicode sorting.
Did I mention Apple's politics suck?
Anyway, here's the statically compiled libiOSJavaScriptCore.a that you can use in your iOS projects and the source and project file if you want to build it yourself for whatever reason:
- libiOSJavaScriptCore-534.27.zip – compiled library
- iOSJavaScriptCore-534.27.zip – source code and project file
This all is based on the slightly outdated 534.27 version of JavaScriptCore, but at least this version seems to be AppStore compatible.
Using libiOSJavaScriptCore.a in your Project:
- Copy the JavaScriptCore directory with the header files from the ZIP to your project folder and add them to your Xcode project
- Copy the libiOSJavaScriptCore.a into your project folder
- Add the
libstdc++.dylib,libicucore.dylibfrom the list and thelibiOSJavaScriptCore.afrom "Add other..." like so import <JavaScriptCore/JavaScriptCore.h>
Building JavaScriptCore from the Source:
- Select iOSJavaScriptCore iPhone as the active Scheme and build
- Select iOSJavaScriptCore iPhone Simulator as the active Scheme and build
- Navigate your terminal to the build directory: depending on your Xcode settings either
build/Products/directly in your project directory or~/Library/Developer/Xcode/DerivedData/JavaScriptCore-xxx/Build/Products/ - Combine the ARM6/7 and x86 (Simulator) libs into one:
lipo -create Production-iphoneos/libiOSJavaScriptCore.a Production-iphonesimulator/libiOSJavaScriptCore.a -output libiOSJavaScriptCore.a
Btw.: AppCelerator maintains their own version of JavaScriptCore that they use in Titanium, but I have no idea how to build it. Any hints are greatly appreciated!
If you want to support me, please consider buying a license of my game engine. It also makes a great gift ;-)

18 Comments:
Thank you Dominic!!! It works! 2 observations:
1) Typo: shouldn't libstc++.dylib be libstdc++.dylib?
2) I had to change all the import directives in the header files like so:
#import <JavaScriptCore/JavaScriptCore.h>
becomes
#import "JavaScriptCore.h"
Is there some build setting that will make this unnecessary?
Thanks again!
1) Yep, thank you. Fixed it.
2) Mh, you shouldn't need to do this. Try adding ${SOURCE_ROOT} to your projects Header Search Paths
Yes, It actually works. With this sort of simple code.
Great to see Javascript being able to be used in Appstore compatible form && semi-native. :)
- (void)drawRect:(CGRect)rect { JSGlobalContextRef ctx = JSGlobalContextCreate(NULL); //pass a javascript code JSStringRef scriptJS = JSStringCreateWithUTF8CString("return (\"Current time = \" + (new Date()));"); JSObjectRef fn = JSObjectMakeFunction(ctx, NULL, 0, NULL, scriptJS, NULL, 1, NULL); JSValueRef result = JSObjectCallAsFunction(ctx, fn, NULL, 0, NULL, NULL); JSStringRef jstrArg = JSValueToStringCopy(ctx, result, NULL); NSString* hello = (NSString*)JSStringCopyCFString(kCFAllocatorDefault, jstrArg); JSStringRelease(jstrArg); JSStringRelease(scriptJS); CGPoint location = CGPointMake(10, 20); UIFont *font = [UIFont systemFontOfSize:24]; [[UIColor whiteColor] set]; [hello drawAtPoint:location withFont:font]; }Hi Dominic!... i really hope you keep rolling well on the industry... i played Z-type and have seen some videos about your platform-game (one of the first ones testing your impact's power).
But now going to the main thing. I came mostly because i thought you could feel like supporting this Handheld idea dedicated to indie development. the-nd.com/ this is just starting to rush from the the botton of obscurity, to the top of the enlightment.
Many Amateur developers have just paid some attention to it and show some possible project that they could made in the near future to encourage some investors. I was hoping that maybe you or some of your readers could feel attracted by the idea and get motivated to post some of her projects (new ones or old ones who could be ported) to just make this handheld itiative more solid to the public eye.
I hope i don't disrespect you or your readers by using this a medium for some sort of spam. See ya then! and i hope the best for your projects!
Many thanks Dominic!
This is fantastic, thank you so much for sharing your work!
The project works perfect in Xcode 4, just make a small change to Header Search Paths & Library Search Paths, change:
${SOURCE_ROOT}
for:
"$(SRCROOT)/"
and #import <JavaScriptCore/JavaScriptCore.h> will work
Any plans to write anything for Honeycomb / Ice cream sandwich?
Fantastic, thank you!
One small question, this download contains files ./parser/Grammar.h and ./parser/Grammar.cpp which don't appear in the current nightly build (or older, I tried 534.15). Any idea why they are appearing/necessary?
Thanks again for this making this public!
#import <JavaScriptCore/JavaScriptCore.h>
unnecessary?
Hey I'm trying to use this library in my project but I realized you can only compile this with armv7? Is this what is expected? Or do you have a armv6/7 compatible binary that I could use? Thanks!
Disregard that. I took a closer look at my compiler errors and realized that I hadn't set the C/C++ compiler version in my build to LLVM GCC4.2. That fixed it for me.
Thanks again for providing the source. Any chance you could throw it up on github so I could fork it?
Thanks a lot for sharing the source. I tried to build the library from source using Xcode 4.2. But the generated libiOSJavaScriptCore.a file doesn't work properly in my test project.
I got a linker error:
Undefined symbols for architecture armv7: _WebCoreWebThreadIsLockedOrDisabled
I'm wondering if there is any tricks to build the library. The pre-compiled .a file from your website works perfectly.
Thanks in advance.
Could you please tell me where to get the original source codes of JavaScriptCore v534.27?
I tried to checkout a copy from svn.webkit.org/repository/webkit/tags/Safari-534.27/. But it seems different from the one you used.
By the way, Titanium maintains their compilable copy of JavaScriptCore at github.com/appcelerator/tijscore, which is using the same version of JSC as iOS 4.3.3
good job. is it possible to do the same for webcore? what do you think is needed?
One note: my understanding of LGPL is that it isn't permissible to statically link code under another licence (except GPL) to it unless you make it possible for users to update the LGPL library themselves (e.g. to fix a bug or enhance it). You can provide either source code or object code (or a mix) for the rest of the program along with suitable build instructions/scripts/resources.
This might make JavaScriptCore tricky to use in this way in a commercial iOS app.
I was wondering about the icucore library which you describe as being private:
"I'm just not allowed to use any functions of it. So I compiled JavaScriptCore again, this time setting the UCONFIG_NO_COLLATION preprocessor macro to disable unicode sorting."
However I cannot build my project using the libiOSJavaScriptCore.a downloaded from the above link without linking with -licucore option. Also you say to include libicucore.dylib. Why are these steps necessary?
Post a Comment: