rubyjs / therubyracer

Embed the V8 Javascript Interpreter into Ruby
1.66k stars 191 forks source link

Implement numbers #352

Closed cowboyd closed 9 years ago

cowboyd commented 9 years ago

This was originally submitted as https://github.com/stormbreakerbg/therubyracer/pull/4

This implements the JavaScript types Number, Int32, and Uint32. Previously, I had been cheating and always using low-level ruby types everywhere and then converting them to their v8 equivalents at the last minute in the C code. Now that this properly wraps numeric types, If a method returns an Int32, then ruby will see it as a V8::C::Int32 and not a Fixnum etc..

Not only does this represent the v8 API as faithfully in Ruby as possible, it also prevents the actual number values from being copied every single time they are passed from V8 to Ruby. Instead, there is just one copy in the V8 heap until it is deallocated.

Also, in this PR, I've experimented with using c++11 lambda pointers to implement the Ruby methods. I think that it makes the code a lot more readable, and it eliminates something like %25 of the boiler-plate. If this pans out, then it may be worth converting the whole codebase to this style.

cowboyd commented 9 years ago

wrong branch