PHOBOSLAB

Blog Home

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?

Update December 2nd 2012: an up-to-date version can be found on github. The old version and instructions follow.

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:

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:

Building JavaScriptCore from the Source:

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

Monday, June 13th 2011

27 Comments:

#1 – Bob Barnett – Wednesday, June 15th 2011, 01:18

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!

#2Dominic – Sunday, June 19th 2011, 16:18

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

#3Hackerzlife – Monday, June 20th 2011, 22:33

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];
}

#4Gustav – Tuesday, June 21st 2011, 20:34

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!

#5 – Abon – Sunday, June 26th 2011, 18:53

Many thanks Dominic!

#6Johannes Fahrenkrug – Wednesday, July 6th 2011, 08:46

This is fantastic, thank you so much for sharing your work!

#7 – karlozm – Thursday, July 14th 2011, 19:47

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

#8 – ray – Sunday, July 24th 2011, 03:24

Any plans to write anything for Honeycomb / Ice cream sandwich?

#9 – Jim – Friday, August 19th 2011, 13:49

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!

#10FRIV – Wednesday, August 31st 2011, 05:51

#import <JavaScriptCore/JavaScriptCore.h>
unnecessary?

#11 – Josh Wu – Friday, September 23rd 2011, 01:05

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!

#12 – Josh Wu – Monday, September 26th 2011, 22:23

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.

#13 – Chris – Tuesday, September 27th 2011, 19:18

Thanks again for providing the source. Any chance you could throw it up on github so I could fork it?

#14 – Lanka – Saturday, October 29th 2011, 16:36

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.

#15 – Jacky – Saturday, November 5th 2011, 10:39

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

#16 – malick – Wednesday, December 21st 2011, 23:26

good job. is it possible to do the same for webcore? what do you think is needed?

#17 – TomW – Thursday, December 29th 2011, 20:53

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.

#18 – Henry – Monday, January 30th 2012, 21:35

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?

#19 – Minun – Tuesday, March 13th 2012, 10:32

Thanks for your sharing, it is very good.

After some research, I tried to build a new version based on the source code from iOS 5.1, and finally succeeded. I have tested it in my iPhone 4S, at least it told me 2 if I asked it 1+1. Ha ha!

However, I can't build the latest source code downloaded from webkit.org, it seems some low level interpreter support were missing, or the ruby offline asm extraction code has some mistakes if target is chosen as armv7. The version used in iOS 5.1 doesn't support that.

#20 – Tracy E – Friday, July 13th 2012, 03:58

Can the lib support iOS3.X ?

#21 – Ricardo QH. – Wednesday, August 22nd 2012, 21:33

Hello Dominic,

Thanks a lot for this, works great for me.
I'm wondering if you or someone else have figured out how to create a compiling Webcore project as well as this one for JavaScriptCore, I really need it for a project which I'm working on, I'll really appreciate if you have some information you want to share with me. We are looking for somebody with knowledge on this area that can help us, we are willing to pay for it...

Thanks in advance!

#22 – joao – Friday, September 21st 2012, 11:28

any news regarding amv7s?

Can you post a new lib with armv7s and armv7 ? or how to compile it?

Regar,ds

#23 – joao – Tuesday, October 2nd 2012, 10:12

i tried to compile it but without success. I can only compile in DEBUG Mode. I cant compile in production mode.

The problem seems to be with IOS6 SDK. The compiler must be LLVM GCC and not apple one.
I was trying to create a armv7s lib in production mode. But I cant. any ideas?

#24 – ritu – Thursday, October 11th 2012, 16:13

Can you pls share the compilation steps or compied lib with iOS6 for armv7s

#25 – Dhiren – Thursday, November 1st 2012, 12:35

Hi, Thanks for sharing project. Though I am able to compile project from Source, but while using I am getting error during compile as below :

"_WebCoreWebThreadIsLockedOrDisabled", referenced from:

WTF::CrossThreadRefCounted<WTF::OwnFastMallocPtr<unsigned short const> >::isOwnedByCurrentThread() constin libiOSJavaScriptCore.a(StringImpl.o)

Symbol(s) not found for architecture armv7s

Collect2: ld returned 1 exit status


This error also comes when I build for armv7. Can you please tell me what could be wrong in compilation of Library??


#26 – T – Friday, September 20th 2013, 01:23

Thanks Dominic for sharing this project and helping make javascript a possibility for the iphone/ipad! Any chance you can update your javascriptcore-ios git repository with the pull requests issued by the community? There are a few gems in there! Support for arm7s, cocoapods, and the latest javascript-objective-c bridge just being a few.

This would be a huge help! Thanks!

#27 – Martin Scheffer – Sunday, September 22nd 2013, 00:56

it just works !!
FANTASTIC !

five minutes after reading this page i had a scriptable app (ok almost)

yeah i would add some explanation about the headers, or better, make your lib into a "fake framework" 100% allowed, and makes it possible to use the <whatever/thename.h> syntax to import headers.

go here: github.com/kstenerud/iOS-Universal-Framework

use the "fake" framework variant.

it's just a folder called framework, that contains a .a library and some headers, perfect for this, would make it even easier to use your stuff.

thanks !

Post a Comment:

Comment: (Required)

(use <code> tags for preformatted text; URLs are recognized automatically)

Name: (Required)

URL:

Please type phoboslab into the following input field or enable Javascript. This is an anti-spam measure. Sorry for the inconvenience.