Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
The Ruby Racer   Under the Hood     Charles Lowell      @cowboyd
The Ruby RacerEmbeds the V8 JavaScript interpreter into your Ruby                     process
Buzz!
Buzz!   Rails 3.1
Buzz!   Rails 3.1             by default
Buzz!     Rails 3.1                 by default        ExecJS    (compile)
Buzz!     Rails 3.1                    by default        ExecJS       (compile)            The Ruby Racer
But that’s just the cool          kids
But that’s just the cool          kids         why else?
Headless JavaScript   Unit Testing
Headless JavaScript   Unit Testing   Our first use at the FrontSide
Tap into Node.js     utilities
Tap into Node.js        utilitiesless.js, handlebars.js, asciimo.js, jsdom.js, etc....
Client/Server code-      sharing
Client/Server code-      sharing  templating, validation, business logic
Performance
PerformanceRe-implementing hotspots with V8 can be just as fast as                      with C.
PerformanceRe-implementing hotspots with V8 can be just as fast as                      with C.                   (but wit...
PerformanceRe-implementing hotspots with V8 can be just as fast as                      with C.                   (but wit...
The Ruby RacerWhat does embedding a JavaScript interpreter into           your Ruby process mean?
Evaluate JavaScript from          Ruby
Evaluate JavaScript from          Ruby
Access JavaScriptObjects from Ruby
V8::Object
V8::ObjectEvery JS Object looks kinda like a Ruby hash
V8::ObjectEvery JS Object looks kinda like a Ruby hash
V8::Objectsymbol/string agnostic
V8::Objectsymbol/string agnostic
V8::Objectsteez: attr_reader
V8::Objectsteez: attr_reader
V8::Object writeable? yes.
V8::Object writeable? yes.
V8::ObjectEnumerable? totally!
V8::ObjectEnumerable? totally!
V8::ObjectEnumerable? totally!
V8::ObjectMethods? no, but yes.
V8::Object            Methods? no, but yes.In JavaScript, methods are just properties that            happen to be functions
V8::Object            Methods? no, but yes.In JavaScript, methods are just properties that            happen to be functio...
V8::Function  call it like a proc
V8::Function  call it like a proc
V8::Function call it like a method
V8::Function call it like a method
V8::Functioncall it like a constructor
V8::Functioncall it like a constructor
V8::Object            Methods? no, but yes.In JavaScript, methods are just properties that            happen to be functions
Methodish
Call JavaScript from        Ruby
Call JavaScript from           Ruby• eval() js code directly
Call JavaScript from           Ruby• eval() js code directly• fiddle directly with js objects
Call JavaScript from           Ruby• eval() js code directly• fiddle directly with js objects• fiddle directly with js funct...
Call Ruby code from      JavaScript
Call Ruby code from      JavaScript
Call Rubybound methods
Call Rubybound methods
Call Ruby procs/lambdas
Call Ruby procs/lambdas
Call RubyEmbed Classes
Call RubyExtend Classes
Ruby Properties
Properties  attr_reader
Properties  attr_reader
Properties attr_accessor
Properties attr_accessor
PropertiesDynamic (hashish)
PropertiesDynamic (hashish)
PropertiesDynamic (hashish)
PropertiesDynamic (hashish)method_missing() !
Access Ruby Code• call ruby methods• fiddle with ruby properties• dynamically fiddle with ruby properties
The Ruby Racer   In the year 3000
Safe eval()
Safe eval()
Safe eval()
Safe eval()
Other Stuff• Multi threaded• Custom Heap Snapshots• Multiple V8 virtual machines per Ruby  Process
Challenges
Challenges• garbage collection
Challenges• garbage collection• garbage collection
Challenges• garbage collection• garbage collection• garbage collection
The Ruby Racer               in the year 3000• Robust, Secure, Efficient• JRuby, Rbx, MRI• Cross platform (wherever ruby+v8...
The Ruby Racer     in the year 3000Where do you want     to go?
The Ruby Racer• github.com/cowboyd/therubyracer• irc://irc.freenode.net/therubyracer• therubyracer@googlegroups.com
Upcoming SlideShare
Loading in …5
×

The Ruby Racer: under the hood

8,027 views

Published on

Have you ever had to implement the same validation logic twice: once in JavaScript for the browser and once in Ruby for the server? Has there ever been a JavaScript library like handlebars.js that you'd love to use server side, but can't because well... it's in JavaScript and not Ruby? Or perhaps a time or two you've been tempted to eval() some anonymous Ruby code, but you didn't dare because it's an unspeakably dangerous thing to do?

The solutions to these and many other problems are suddenly and elegantly within your grasp when you've got the power of a JavaScript interpreter right there with you in your ruby process.

Sound crazy? difficult? It's easier than you might think. This talk will focus on The Ruby Racer: a gem that brings the superb V8 interpteter to Ruby. We'll see how to call JavaScript functions directly from Ruby; how to call Ruby methods directly from JavaScript; how to extend Ruby classes with JavaScript; how to extend your JavaScript objects with Ruby, and a slew of other ways of managing their interaction that will bend your mind

Published in: Technology
  • Be the first to comment

The Ruby Racer: under the hood

  1. 1. The Ruby Racer Under the Hood Charles Lowell @cowboyd
  2. 2. The Ruby RacerEmbeds the V8 JavaScript interpreter into your Ruby process
  3. 3. Buzz!
  4. 4. Buzz! Rails 3.1
  5. 5. Buzz! Rails 3.1 by default
  6. 6. Buzz! Rails 3.1 by default ExecJS (compile)
  7. 7. Buzz! Rails 3.1 by default ExecJS (compile) The Ruby Racer
  8. 8. But that’s just the cool kids
  9. 9. But that’s just the cool kids why else?
  10. 10. Headless JavaScript Unit Testing
  11. 11. Headless JavaScript Unit Testing Our first use at the FrontSide
  12. 12. Tap into Node.js utilities
  13. 13. Tap into Node.js utilitiesless.js, handlebars.js, asciimo.js, jsdom.js, etc....
  14. 14. Client/Server code- sharing
  15. 15. Client/Server code- sharing templating, validation, business logic
  16. 16. Performance
  17. 17. PerformanceRe-implementing hotspots with V8 can be just as fast as with C.
  18. 18. PerformanceRe-implementing hotspots with V8 can be just as fast as with C. (but without C)
  19. 19. PerformanceRe-implementing hotspots with V8 can be just as fast as with C. (but without C) Yay!
  20. 20. The Ruby RacerWhat does embedding a JavaScript interpreter into your Ruby process mean?
  21. 21. Evaluate JavaScript from Ruby
  22. 22. Evaluate JavaScript from Ruby
  23. 23. Access JavaScriptObjects from Ruby
  24. 24. V8::Object
  25. 25. V8::ObjectEvery JS Object looks kinda like a Ruby hash
  26. 26. V8::ObjectEvery JS Object looks kinda like a Ruby hash
  27. 27. V8::Objectsymbol/string agnostic
  28. 28. V8::Objectsymbol/string agnostic
  29. 29. V8::Objectsteez: attr_reader
  30. 30. V8::Objectsteez: attr_reader
  31. 31. V8::Object writeable? yes.
  32. 32. V8::Object writeable? yes.
  33. 33. V8::ObjectEnumerable? totally!
  34. 34. V8::ObjectEnumerable? totally!
  35. 35. V8::ObjectEnumerable? totally!
  36. 36. V8::ObjectMethods? no, but yes.
  37. 37. V8::Object Methods? no, but yes.In JavaScript, methods are just properties that happen to be functions
  38. 38. V8::Object Methods? no, but yes.In JavaScript, methods are just properties that happen to be functions JavaScript function calls are “richer”
  39. 39. V8::Function call it like a proc
  40. 40. V8::Function call it like a proc
  41. 41. V8::Function call it like a method
  42. 42. V8::Function call it like a method
  43. 43. V8::Functioncall it like a constructor
  44. 44. V8::Functioncall it like a constructor
  45. 45. V8::Object Methods? no, but yes.In JavaScript, methods are just properties that happen to be functions
  46. 46. Methodish
  47. 47. Call JavaScript from Ruby
  48. 48. Call JavaScript from Ruby• eval() js code directly
  49. 49. Call JavaScript from Ruby• eval() js code directly• fiddle directly with js objects
  50. 50. Call JavaScript from Ruby• eval() js code directly• fiddle directly with js objects• fiddle directly with js functions
  51. 51. Call Ruby code from JavaScript
  52. 52. Call Ruby code from JavaScript
  53. 53. Call Rubybound methods
  54. 54. Call Rubybound methods
  55. 55. Call Ruby procs/lambdas
  56. 56. Call Ruby procs/lambdas
  57. 57. Call RubyEmbed Classes
  58. 58. Call RubyExtend Classes
  59. 59. Ruby Properties
  60. 60. Properties attr_reader
  61. 61. Properties attr_reader
  62. 62. Properties attr_accessor
  63. 63. Properties attr_accessor
  64. 64. PropertiesDynamic (hashish)
  65. 65. PropertiesDynamic (hashish)
  66. 66. PropertiesDynamic (hashish)
  67. 67. PropertiesDynamic (hashish)method_missing() !
  68. 68. Access Ruby Code• call ruby methods• fiddle with ruby properties• dynamically fiddle with ruby properties
  69. 69. The Ruby Racer In the year 3000
  70. 70. Safe eval()
  71. 71. Safe eval()
  72. 72. Safe eval()
  73. 73. Safe eval()
  74. 74. Other Stuff• Multi threaded• Custom Heap Snapshots• Multiple V8 virtual machines per Ruby Process
  75. 75. Challenges
  76. 76. Challenges• garbage collection
  77. 77. Challenges• garbage collection• garbage collection
  78. 78. Challenges• garbage collection• garbage collection• garbage collection
  79. 79. The Ruby Racer in the year 3000• Robust, Secure, Efficient• JRuby, Rbx, MRI• Cross platform (wherever ruby+v8 run)• Binary gems everywhere
  80. 80. The Ruby Racer in the year 3000Where do you want to go?
  81. 81. The Ruby Racer• github.com/cowboyd/therubyracer• irc://irc.freenode.net/therubyracer• therubyracer@googlegroups.com

×