Apple's new Objective-C to Javascript Bridge
steamclock.com
steamclock.com
The thing is: Why would they want to transform away? As it is now Obj-C is pretty sane. The memory management is now a non-brainer so that even people who come from managed languages can grasp it. GCD is great for concurrency. And using C and C++ based 3rd party libraries is trivial.
The existing developer base is fine with Obj-C.
So maybe Apple wants to attract new developers? But why now? If they really wanted to attract new developers to their platform they would have chosen a different language for the iPhone SDK back when they released it first. But nowadays Apple's platforms have more than enough developers.
Also: Why JavaScript? JS itself is a terrible language with many inconsistencies and is unfit for larger projects - which most native applications are. There are far better and saner languages to base a new SDK upon.
GCD is amazing and I use it more than NSOperation for easy quick off stuff.
That being said, I can see how this would be useful for gamedevelopers who wanna keep their scripts in JS/CoffeeScript instead of LUA :-)
And, if Apple were to switch, why would they pick a language as shitty as Javascript?
I grew up as a programmer through C++ and Java, and the first time I ever worked on a JavaScript project, I hated it, and I said the same thing about JavaScript as you did. That's because I was thinking like a Java programmer, and not just a programmer. As I worked more and more with JavaScript, I've grown to love. It has it's pitfalls, sure, but there's just so much flexibility in JavaScript than languages like Java or C++. Nowadays, I prefer it to Java or C++. Even for server-side programming. I get the feeling that you don't really know enough to know what you're missing.
It is also advisable to taboo projecting your own experience/thoughts onto another, as if that is his/her experience/thoughts, as well.
That you love JavaScript, even for server-side programming, has absolutely zero significance in evaluating whether jfb (or anyone else, for that matter) knows enough to know what s/he is missing.
> It is also advisable to taboo projecting your own experience/thoughts onto another, as if that is his/her experience/thoughts, as well.
I disagree with that take. Personal experiences can lend themselves to evaluating context. Humans do it all the time.
If one is going to make an argument that something is "silly", and provides barley more than nil context to that argument ("enough to know"), then a context vacuum exists. If the OP isn't going to provide it, then someone else will.
C? Buffer overflows, manual memory management, header files, severely lacking standard libraries, crap support for most modern idioms, etc etc.
C++? Tons of edge cases, template madness, unbelievable large language spec, etc etc.
Python? Python 2 - 3 dichotomy, crappy concurrency / parallelism support, , strict on whitespace, standard libs in various stages of cruft and abandonment, no proper closures, CPU bound for lots of interesting problems necessitating bilingual development with C extensions or the use of hacks such as Cython etc.
You can find a similar list for everything, from Lisp to Haskell.
Javascript "insanities" are no big deal. People have been creating large apps (including porting stuff like THE WHOLE BLOODY QT TOOLKIT to it) in it with no problems. Once you know them, you can work around them.
A huge install base, huge number of programmers, easy syntax, powering the internet, lispy-flexibility, and the fastest interpreters this side of the JVM are enough benefits to counter some crappy coercion rules and FP only math bizarreness.
The maddening thing is that Javascript could so easily be vastly better, and it wouldn't take all that much. Remove any kind of implicit binding (and this could easily be optional, like Visual BASIC's option strict, or the similar thing in perl) and you'd be about 75% of the way there, in my view.
It would take a bit more to convince me that this kind of language is suitable for larger-scale projects - this based on my experience with pretty much any language that isn't statically typed - but this would at least remove one obvious type of error without impinging too much on what people appear to see as their freedoms.
I don't recall seeing this mentioned anywhere. Perhaps the still-imperfect browser support puts people off.
Actually the ObjC runtime is amazingly powerful and dynamic, as if it was made for a better language (e.g. can handle dynamism of Ruby http://macruby.org/)
I still haven't found a language that implements events as elegantly as Javascript.
You won't be able to use undocumented APIs because you won't be able to get to them.
Funny. I've sometimes similarly speculated in the past that the set of developers who (a) struggle with JavaScript as a tool for in-browser apps and (b) would actually be genuinely productive with some other dynamic scripting language (Ruby, Python, CoffeeScript) in the same context is pretty small too.
But the truth is probably that whether or not someone can develop non-trivial software with any roughly equivalent language is mostly a function of available time to ramp up vs spend on other things. So, sure, Obj C and JS are close enough that anybody who can develop non-trivial software in one can probably do it in the other... but it's still going to be a time investment while you learn how things are done in the language and various associated APIs.
I don't know if Apple particularly cares if they're pulling in more developers. If they do, though, adding another language that many developers already know makes sense. It reduces the friction involved.
> And, if Apple were to switch, why would they pick a language as shitty as Javascript?
On top of the widespread at-least-shallow familiarity, not everyone shares this opinion.
Also, this is unlikely to be a "switch." It's more likely to be a gateway.
That seems to be a fairly popular dismissal for a language designed in 10 days, and perhaps rightly so. JavaScript probably classifies as mediocre language compared to the wealth of sanely designed choices we have available today.
That said, have you taken a recent look at the next generation of JavaScript aka ES6/Harmony?[1]
- Modules? Yup
- Maps and Sets? You bet.
- Tail call optimizations? Sure
- Classes? Finally!
etc
That's not to suggest that a single iteration will magically fix all of its quirks and shortcomings. However, one possibility could be that Apple picked JS, not on it's checkered history, or the fact that it is popular amongst web developers today, but where it is headed tomorrow and how it could be adopted and adapted.
Because it's popular.
Maybe Apple out of the goodness in their heart decided to make this easier, but I'm skeptical.
Because websites (and thus JavaScript, in some form) are the future of programming at this point in time. JavaScript just happens to be the primary programming language for interacting with HTML5 and a lot of people happen to know it pretty well.
Plus, JavaScript is not a terrible language but it does take a lot of knowledge to work with it cross-browser and use browser APIs (these browser APIs tend to suck).
The main (very welcome) advancement seems to be that they've pushed this capability down from WebKit into JSCore. Today you can basically already do everything that's being shown in OP I think, you just were required to create an invisible WebView to host your JavaScript environment. You can easily expose classes to WebKit/JSCore (using KVC), and interact with JS objects from ObjC, and arrays, strings, etc. are auto-converted. The WebKit requirement is pretty annoying though, and as V8 has shown us, having a drop dead simple way of throwing JS into an app has some huge benefits.
The fact that it's a refactoring (even a very nice one, as it seems to be) rather than a brand new API does make it less likely that Apple has any particular future plans for the API, which in turn make much of the speculation in the post even more speculative. I'm going to make an update to the post with a link to WebScript as well as a bit of additional commentary.
The fact is that after thinking about the iOS developer rules and what is currently available in iOS, it's totally doable to write a true Javascript bridge that respect all the requirement from the iOS rules and still let your JS code access all the ObjC runtime, in a totally transparent way aka I am able to write the following:
var userName = userNameTextfield.getText();
var helloMsg = "Hello " + userName;
messageLabel.setText(helloMsg);
The first line will directly get the text value of a UITextField (no callback used here), and it will end up calling directly the 'text' property on the UITextField. So the JS bridge works transparently where JS code call native ObjC code directly (without using callback) and obviously ObjC can directly access the JS side... there are a few gotcha but the full system works beautifully and surprising enough performance are quite good. It took me a few years to come up with the cleanest solution and implementation.I am currently working on integrating such piece of code into some of my apps and have them released on the AppStore. In fact if anyone good in JS is interested I would really appreciate some help to get that bridge off the ground since I truly believe there is a lot of great opportunities with a JS bridge to ObjC that respect all iOS rules. My contact info are my HN profile.
Edit: minor typo fixed
That said, all of the bridges I've seen were to classical-OO languages. It is possible that Cocoa won't translate as well to prototypal-OO languages like JavaScript. It will be interesting to see how Apple navigates that paradigm shift.
This has already been done with Mozilla's Rhino (JavaScript or Java) and it turns out the be pretty easy to work with Java classes in that context.
On iOS, developers have had to compile their own version of JavaScriptCore to use this API. That's the basis for HTML5 game engines like Impact [2], and some HTML5-to-Objective-C middleware platforms.
Unfortunately, until Apple says otherwise, this version of JSC is still subject to Apple's App Store review guidelines [3]. Thanks to guideline 2.8, you can't have your app, running JSC, execute any JavaScript that doesn't ship within the bundle of your app.
I'd like to see that rule change, someday. Exposing this Objective-C API in a future iOS release isn't going to change the status quo.
[1] - http://developer.apple.com/library/mac/#documentation/Carbon...
[2] - http://impactjs.com
Should make it easier for developers to build their own middleware platform without getting too deep into reams of C boilerplate.
[1] - https://developer.apple.com/library/ios/#documentation/Cocoa...
In Foundation, [NSBundle mainBundle] returns a reference to an object representing your application's root directory, and the code and resources found within. Roughly the same bits that you'd find within an unpacked IAP file.
Speculation is my own, derived from: http://oleb.net/blog/2012/10/remote-view-controllers-in-ios-...
So I wouldn't take this as necessarily the sign of a shift in anything.
They were able to solve this in C by inventing Blocks and GCD.
It's not as good as Hypercritical, but is better than nothing!
That seems like an enormous leap of speculation.
It's only interesting because Apple is officially doing it themselves.
Does this bridge help fix that?
This is how most of the other full feature bridges work, PyObjC, RubyCocoa/MacRuby, LuaCocoa. Otherwise you are hamstrung anytime you need to deal with something like a C struct (e.g. NSPoint).
You don't necessarily need BridgeSupport to call out to Objective-C.
Speaking of BridgeSupport, does anybody know where the most recent source code for that project went? It seems to have been removed from Apple's Mac OS Forge after Laurent Sansonetti left Apple. A shame.
[1] - For example, http://merbist.com/2011/02/19/bridgesupport-build/ . The chapter referenced from O'Reilly's excellent MacRuby book does a better job of explaining the whole process.
While it is possible to workaround the lack of BridgeSupport, if Apple doesn't supply it, most developers are not going to go through the effort of using it. And if Apple doesn't supply it on iOS, they aren't going to make life nice for developers doing this (continue bans on dynamic libraries for loading the .dylib containing missing symbols, on mprotect which is horribly useful for things like subclassing and block generation and JIT).
I don't know where the source went. I'm sad to hear that. If you find a copy, let me know.
https://github.com/mobiruby/BridgeSupport
Which he found from a really neat looking archive for popular open source projects, at that. Bookmarking.
http://code.metager.de/source/
Just made a few fixes to get the BridgeSupport framework to compile on 10.8. Might issue a pull request.
That's so often just about outward signs. Programming is complicated, and the fields of programming and Computer Science are vast. If you're not aware of how miniscule a sliver of the whole thing you command, odds are you're just another unwitting Dunning Krueger victim.
Don't judge a language by what you find strange. Don't judge a language just because it wouldn't suit what you do. Figure out what makes it powerful and why it works. Until you do that, please just shut up and don't add to the noise.
How much longer until we can develop native applications without Objective-C entirely?
A polygot/trans compiler for xcode would make me melt.
You already can --
Lisp: Nu
Ruby: MacRuby/RubyMotion
Basic: Objective-Basic
Lua: LuaCocoa, LuaWax, TLC
JavaScript: JSCocoa
Perl: CamelBones
Here's hoping.
"Javascript is so shitty though!"
"I agree!"
"Javascript isn't so shitty once you get used to it"
"It's like lisp!"
"No it isn't. Javascript is terrible and everyone who likes it is a moron"
"No they aren't!"
"YES THEY ARE"
I don't know, should we expect more from the discourse here than just rehashing the same argument over and over again?Beware of people who are portraying themselves as "savvy" yet don't know how to spell Lua.