RubyMotion: Under the Hood

65
RubyMotion: Under the Hood Inspired by Click and Clack Monday, October 14, 2013

description

Slides from the talk "RubyMotion: Under the Hood" presented at the Wicked Good Ruby Conference in 2013.

Transcript of RubyMotion: Under the Hood

Page 1: RubyMotion: Under the Hood

RubyMotion:Under the Hood

Inspired by Click and Clack

Monday, October 14, 2013

Page 2: RubyMotion: Under the Hood

A brief introduction

Monday, October 14, 2013

Page 3: RubyMotion: Under the Hood

Name:

About Me

Joshua BallancoMonday, October 14, 2013

Page 4: RubyMotion: Under the Hood

Employer: Burnside Digital

About Me

Monday, October 14, 2013

Page 5: RubyMotion: Under the Hood

Ankara, Turkey

About Me

Location:Monday, October 14, 2013

Page 6: RubyMotion: Under the Hood

A slightly longer introduction

Monday, October 14, 2013

Page 7: RubyMotion: Under the Hood

Before...

Monday, October 14, 2013

Page 8: RubyMotion: Under the Hood

Employer: Patch

About Me

Monday, October 14, 2013

Page 9: RubyMotion: Under the Hood

New York City

About Me

Location:Monday, October 14, 2013

Page 10: RubyMotion: Under the Hood

Before that...

Monday, October 14, 2013

Page 11: RubyMotion: Under the Hood

Employer: Apple

About Me

Monday, October 14, 2013

Page 12: RubyMotion: Under the Hood

Cupertino

About Me

Location:Monday, October 14, 2013

Page 13: RubyMotion: Under the Hood

Before that...

Monday, October 14, 2013

Page 14: RubyMotion: Under the Hood

School: University of Miami

About Me

Monday, October 14, 2013

Page 15: RubyMotion: Under the Hood

Miami

About Me

Location:Monday, October 14, 2013

Page 16: RubyMotion: Under the Hood

A bit of history

Monday, October 14, 2013

Page 17: RubyMotion: Under the Hood

In graduate school

Monday, October 14, 2013

Page 18: RubyMotion: Under the Hood

In graduate school

Monday, October 14, 2013

Page 19: RubyMotion: Under the Hood

At Apple... Retail

Monday, October 14, 2013

Page 20: RubyMotion: Under the Hood

At Apple

Laurent Sansonetti

Monday, October 14, 2013

Page 21: RubyMotion: Under the Hood

After Apple...

Monday, October 14, 2013

Page 22: RubyMotion: Under the Hood

Episode VI - The Return of The RubyMotion

Monday, October 14, 2013

Page 23: RubyMotion: Under the Hood

What is RubyMotion?

• Use Ruby to build apps for iOS and OS X

• Native apps

• Interface directly with Obj-C libraries

• CLI-based build system

Monday, October 14, 2013

Page 24: RubyMotion: Under the Hood

What is RubyMotion?

• RubyMotion: http://www.rubymotion.com/

• MotionCasts: http://motioncasts.tv/

• RubyMotion Wrappers:

! http://rubymotion-wrappers.com/

• ...and lot’s more

Monday, October 14, 2013

Page 25: RubyMotion: Under the Hood

What is MacRuby?

• Intended to be the implementation of Ruby 2.0 for OS X

• Target RubySpec compliance

• JIT or AOT Compiled

• Uses libauto for Garbage Collection

Monday, October 14, 2013

Page 26: RubyMotion: Under the Hood

What is MacRuby?Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM (with JIT)

Objective-C Runtime

Monday, October 14, 2013

Page 27: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM (with JIT)

Objective-C Runtime

Running a “something.rb” file

Monday, October 14, 2013

Page 28: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM (with JIT)

Objective-C Runtime

Running a “something.rb” file

Monday, October 14, 2013

Page 29: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM (with JIT)

Objective-C Runtime

Running a “something.rb” file

Monday, October 14, 2013

Page 30: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM

Objective-C Runtime

AOT compiling “something.rb”

something.o

Monday, October 14, 2013

Page 31: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM

Objective-C Runtime

AOT compiling “something.rb”

something.o

Monday, October 14, 2013

Page 32: RubyMotion: Under the Hood

Parser

Compiler

something.oVM (sans GVL)

Objective-C Runtime

Running an AOT compiled “something.rb”

Monday, October 14, 2013

Page 33: RubyMotion: Under the Hood

Parser

Compiler

something.oVM (sans GVL)

Objective-C Runtime

Running an AOT compiled “something.rb”

Monday, October 14, 2013

Page 34: RubyMotion: Under the Hood

Parser

Compiler

LLVM

something.oVM (sans GVL)

Objective-C Runtime

Running an AOT compiled “something.rb”

Monday, October 14, 2013

Page 35: RubyMotion: Under the Hood

Parser

Compiler

LLVM

something.oVM (sans GVL)

Objective-C Runtime

Running an AOT compiled “something.rb”

Monday, October 14, 2013

Page 36: RubyMotion: Under the Hood

Parser

Compiler

LLVM

something.oVM (sans GVL)

Objective-C Runtime

Running an AOT compiled “something.rb”

Monday, October 14, 2013

Page 37: RubyMotion: Under the Hood

What is RubyMotion?

• Descendent of MacRuby

• “Ruby, the Good Parts”

• Static Compiled

• Retain/release reference counting

Monday, October 14, 2013

Page 38: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM

Objective-C Runtime

Static compiling “something.rb”

something.o

Monday, October 14, 2013

Page 39: RubyMotion: Under the Hood

Ruby Syntax

Parser

Compiler

VM (sans GVL) LLVM

Objective-C Runtime

Static compiling “something.rb”

something.o

Monday, October 14, 2013

Page 40: RubyMotion: Under the Hood

VM (sans GVL)

Objective-C Runtime

Running a static compiled “something.rb”

something.o

Monday, October 14, 2013

Page 41: RubyMotion: Under the Hood

VM (sans GVL)

Objective-C Runtime

Running a static compiled “something.rb”

something.o

Monday, October 14, 2013

Page 42: RubyMotion: Under the Hood

Why must we statically compile?

• On OS X

• Compile writes code to a memory page

• Runtime runs the code from that memory page

Monday, October 14, 2013

Page 43: RubyMotion: Under the Hood

Why must we statically compile?

• On iOS

• Memory pages must be writable or executable, NOT BOTH!

• OS prohibits runtime compilation

• Apple prohibits interpreting arbitrary scripts

• ...but you wouldn’t want an interpreter anyway

Monday, October 14, 2013

Page 44: RubyMotion: Under the Hood

Garbage Collection

Monday, October 14, 2013

Page 45: RubyMotion: Under the Hood

What happened to the Garbage Collector?

It required extra threads, so we had to kill it...

Monday, October 14, 2013

Page 46: RubyMotion: Under the Hood

So RubyMotion Uses ARC?

Yes...

Uh...no

...sorta?

Monday, October 14, 2013

Page 47: RubyMotion: Under the Hood

ARC vs “ARC”

• Objective-C’s ARC modifies your code before compilation

• RubyMotion VM knows when retain and/or release should be called...your code is not touched

Monday, October 14, 2013

Page 48: RubyMotion: Under the Hood

ARC vs “ARC”

Isn’t the distinction rather academic?

Probably...

Monday, October 14, 2013

Page 49: RubyMotion: Under the Hood

“ARC” Caveats• Collection happens when the autorelease

pool drains

• Need to be careful with tight loops that generate many objects

• Use “autorelease do...end”

• Detects almost all cycles

• Use WeakRefs if cycles become problematic

Monday, October 14, 2013

Page 50: RubyMotion: Under the Hood

Debugging RubyMotion

• Remember, RubyMotion objects are Objective-C objects...

• All the usual tricks are valid!

Monday, October 14, 2013

Page 51: RubyMotion: Under the Hood

Let’s Play!

Monday, October 14, 2013

Page 52: RubyMotion: Under the Hood

The Victimapp/app_delegate.rb

Monday, October 14, 2013

Page 53: RubyMotion: Under the Hood

The Victimapp/app_delegate.rb

Monday, October 14, 2013

Page 54: RubyMotion: Under the Hood

REPL Magic

Monday, October 14, 2013

Page 55: RubyMotion: Under the Hood

REPL Magic

Monday, October 14, 2013

Page 56: RubyMotion: Under the Hood

REPL Magic

Monday, October 14, 2013

Page 57: RubyMotion: Under the Hood

REPL Magic

Monday, October 14, 2013

Page 58: RubyMotion: Under the Hood

That’s cool...but can you do it in a debugger???

Monday, October 14, 2013

Page 59: RubyMotion: Under the Hood

Debugger Wizardry

Monday, October 14, 2013

Page 60: RubyMotion: Under the Hood

Debugger Wizardry

Monday, October 14, 2013

Page 61: RubyMotion: Under the Hood

Debugger Wizardry

Monday, October 14, 2013

Page 62: RubyMotion: Under the Hood

Debugger Wizardry

Monday, October 14, 2013

Page 63: RubyMotion: Under the Hood

That’s CRAZY!

I know...but it’s fun!

Monday, October 14, 2013

Page 64: RubyMotion: Under the Hood

Debugging RubyMotion

• Helper methods “pro” and “pri”

• http://www.rubymotion.com/developer-center/articles/debugging/

• Watch for more/better tooling to come...

Monday, October 14, 2013

Page 65: RubyMotion: Under the Hood

Questions?

@manhattanmetric

Joshua Ballanco

https://github.com/jballanc

Monday, October 14, 2013