43
MacRuby Objective-C, Cocoa, LLVM, Grand Central and other musings A quick introduction to Olivier Gutknecht - OSDC.fr - Oct. 3 2009

MacRuby, an introduction

Embed Size (px)

Citation preview

Page 1: MacRuby, an introduction

MacRubyObjective-C, Cocoa, LLVM, Grand Central

and other musings

A quick introduction to

Olivier Gutknecht - OSDC.fr - Oct. 3 2009

Page 2: MacRuby, an introduction

Olivier [email protected] : olg

co-founderServer Software

iCal, iSync, [...] Active lurker

MacRuby

Page 3: MacRuby, an introduction

Breaking News !

MacRuby developer attacked by raptors

Page 4: MacRuby, an introduction

TextText

Page 5: MacRuby, an introduction

TextText

Page 6: MacRuby, an introduction

MacRuby

Page 7: MacRuby, an introduction

Mac OS X Stack

Darwin

Core Technologies

Application Frameworks

Applications

Kernel, userland, libdispatch, ...

CoreGraphics, CoreFoundation, ...

Cocoa, WebKit, ...

Page 8: MacRuby, an introduction

2002 Mac OS X 10.2 Ruby 1.6.7

2005 Mac OS X 10.4 Ruby 1.8.2

2007 Mac OS X 10.5 Ruby 1.8.6RubyCocoa, gems, Rails

2009 Mac OS X 10.6 Ruby 1.8.7RubyCocoa, gems, Rails

20xx ? Sky is the limit

Ruby on OS X

Page 9: MacRuby, an introduction

Ruby on OS X

• Ruby, just on another unix platform

• With some small improvements...

e.g. mongrel_rails_persists on OS X Server launchd / bonjour integration

• What about Cocoa ?

Page 10: MacRuby, an introduction

Family business

SmallTalk

Objective-C Ruby

Page 11: MacRuby, an introduction

Sure. Check out Gitnub

A “true” OS X App in Ruby ?

Page 12: MacRuby, an introduction

RubyCocoa

• A ruby-objc bridge (FUJIMOTO Hisakuni, 2001)

• Ruby 1.8

• Green threads, no reentrance

• Two runtimes, two GC

• ... interesting syntax

• Ouch

Page 13: MacRuby, an introduction
Page 14: MacRuby, an introduction

require 'osx/cocoa'; include OSXapp = NSApplication.sharedApplicationwin = NSWindow.alloc.initWithContentRect_styleMask_backing_defer([0, 0, 200, 60], NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask,NSBackingStoreBuffered, false)win.title = 'Hello World'button = NSButton.alloc.initWithFrame(NSZeroRect) win.contentView.addSubview(button)button.bezelStyle = NSRoundedBezelStyle button.title = 'Hello!'button.sizeToFit button.frameOrigin = NSMakePoint((win.contentView.frameSize.width/2.0)-(button.frameSize.width/2.0), (win.contentView.frameSize.height/2.0)-(button.frameSize.height/2.0))button_controller = Object.new def button_controller.sayHello(sender) puts "Hello OSDC!" endbutton.target = button_controller button.action = 'sayHello:'win.display win.orderFrontRegardlessapp.run

Page 15: MacRuby, an introduction

Easy.(when you’re proficient in Objective-C, Cocoa, Ruby and the bridge - here be dragons)

Page 16: MacRuby, an introduction

MacRuby

Page 17: MacRuby, an introduction

MacRuby

• One GC to release them all

Page 18: MacRuby, an introduction

MacRuby

• One GC to release them all

• One runtime to bind them

Page 19: MacRuby, an introduction

MacRuby

• One GC to release them all

• One runtime to bind them

• In the land of Cocoa where Obj-C lie

Page 20: MacRuby, an introduction

MacRuby

Laurent Sansonetti(Apple)

Vincent IsambartKich KilmerEloy DuranBen Stiglitz

Matt Aimonetti...

http://www.macruby.orghttp://twitter.com/macruby

Page 21: MacRuby, an introduction

Modest goals

• The best platform for Ruby developers

• A great platform for Cocoa developers

Page 22: MacRuby, an introduction

Bridge, what bridge ?$ macirb>> s = "osdc"=> "osdc"

>> s.class=> NSMutableString

>> s.class.ancestors=> [NSMutableString, NSString, Comparable, NSObject, Kernel]

>> s.upcase=> "OSDC"

>> s.uppercaseString=> "OSDC"

>> s.respondsToSelector(:upcase)=> 1

>> s.respond_to?(:upcase)=> true

Page 23: MacRuby, an introduction

Bridge, what bridge ?

• A Ruby class is an Objective-C class

• A Ruby method is an Objective-C method

• A Ruby instance is an Objective-C instance

Page 24: MacRuby, an introduction

Syntax

Person *person = [Person new];

[person name];[person setName:name];[person setFirstName:first lastName:last];

person = Person.new

person.nameperson.name = myNameperson.setFirstName(first, lastName:last)

Objective-C

MacRuby

Page 25: MacRuby, an introduction

require ‘hotcocoa’include HotCocoa

application do win = window :title => ‘hello OSDC’, :frame => [0, 0, 200, 60] b = button :title => ‘Hello!’, :layout => {:align => :center} win << b b.on_action { puts “Hello OSDC!” } end

HotCocoa

A thin layer by Rich Kilmer, providing a natural ruby experience when coding Cocoa apps

Page 26: MacRuby, an introduction

Ruby 1.9

YARV

Parser

GC

Runtime

Stdlib

Built-in classes

Page 27: MacRuby, an introduction

MacRuby

LLVM/Roxor

Parser Stdlib

AOT JIT

Runtime

libobjc

libauto

Built-in Classes

CoreFoundation

Page 28: MacRuby, an introduction

MacRuby 0.4 - 04/09

• XCode integration

• Embedding / Runtime Control

• HotCocoa

• Threaded GC

Page 29: MacRuby, an introduction

MacRuby 0.5 - xx/09

• YARV ? LLVM !

• RubySpec

• AOT

• GrandCentral

• ...

Page 30: MacRuby, an introduction

Why LLVM ?

Page 31: MacRuby, an introduction

Coolest Logo Ever

Page 32: MacRuby, an introduction

Everybody loves microbenchmarks

(lies, damn lies and benchmarks)

From a bench by Patrick Thomson @ C4

Page 33: MacRuby, an introduction

static int fib(int n){ if (n < 3) { return 1; } else { return fib(n - 1) + fib(n - 2); }}

C

Page 34: MacRuby, an introduction

Objective-C

@implementation Fib- (int)fib:(int)n{ if (n < 3) { return 1; } else { return [self fib:n - 1] + [self fib:n - 2]; }}@end

Page 35: MacRuby, an introduction

0

1

2

3

4

fib(40)

exec

utio

n tim

e (s

)

C Objective-C

Page 36: MacRuby, an introduction

Ruby

def fib(n) if n < 3 1 else fib(n-1) + fib(n-2) endend

p fib(ARGV.join("").to_i)

Page 37: MacRuby, an introduction

0

1

2

3

4

fib(40)

exec

utio

n tim

e (s

)

C Objective-CMacRuby

Page 38: MacRuby, an introduction

$ macrubyc fibo.rb -o fibo$ ./fibo 40102334155

$ macruby fibo.rb 40102334155

$ ruby fibo.rb 40102334155

MRI Ruby 1.8

MacRuby

MacRuby AOT

Page 39: MacRuby, an introduction

Grand Central# A GCD-based implementation of the sleeping barber problem:# http://en.wikipedia.org/wiki/Sleeping_barber_problem# http://www.madebysofa.com/#blog/the_sleeping_barber

waiting_chairs = Dispatch::Queue.new('com.apple.waiting_chairs')semaphore = Dispatch::Semaphore.new(3)index = -1while true index += 1 success = semaphore.wait(Dispatch::TIME_NOW) if success != 0 puts "Customer turned away #{index}" next end waiting_chairs.dispatch do semaphore.signal puts "Shave and a haircut #{index}" endend

Page 40: MacRuby, an introduction

Tools Galore

?

Page 41: MacRuby, an introduction

Why MacRuby ?

• Ruby for “mac-like” desktop applications

• A wonderful experimentation playground

• ... Interesting perspectives

Page 42: MacRuby, an introduction

Q&A

• Ruby 1.9 compatibility

• Right now, ≈ 80% on rubyspec

• Other platforms, portability

• No closed-source dependancies, no definitive technical blocker

• ... Any takers ?

Page 43: MacRuby, an introduction

Thanks!