1+ ##
2+ # An extensible conversion mechanism for converting objects to and
3+ # from their V8 representations.
4+ #
5+ # The Ruby Racer has two levels of representation for JavaScript
6+ # objects: the low-level C++ objects which are just thin wrappers
7+ # native counterparts. These are the objects in the `V8::C::*`
8+ # namespace. It also has high-level Ruby objects which can
9+ # either be instances of `V8::Object`, `V8::Date`, or just plain Ruby
10+ # objects that have representations in the JavaScript runtime.
11+ #
12+ # The conversion object held by the context captures this transition
13+ # from one object type to the other. It is implemented as a "middleware"
14+ # stack where at the base is the `Fundamental` conversion which does
15+ # basic conversion and identity mapping.
16+ #
17+ # In order to "extend" or override the conversion mechanism, you can
18+ # extend this object to add behaviors. For example, the following
19+ # extension will add a `__fromRuby__` property to every ruby object
20+ # that is embedded into this context.
21+ #
22+ # module TagRubyObjects
23+ # def to_v8(context, ruby_object)
24+ # super.tap do |v8_object|
25+ # v8_object.Set(V8::C::String::NewFromUtf8("__fromRuby__"), V8::C::Boolean::New(true))
26+ # end
27+ # end
28+ # end
29+ # context.conversion.extend TagRubyObjects
30+ #
31+ # @see V8::Conversion::Fundamental for the basic conversion operation.
132class V8 ::Conversion
233 include Fundamental
3- # include Identity
434
35+
36+ ##
37+ # Convert a low-level instance of `V8::C::Value` or one of its
38+ # subclasses into a Ruby object.
39+ #
40+ # The `Fundamental` conversion will call `v8_object.to_ruby`, but
41+ # any number of middlewares can be inserted between then.
42+ #
43+ # @param [V8::C::Value] the object to convert
44+ # @return [Object] the corresponding Ruby value
545 def to_ruby ( v8_object )
646 super v8_object
747 end
848
49+ ##
50+ # Convert a Ruby Object into a low-level `V8::C::Value` or one of
51+ # its subclasses. Note here that things like `V8::Object` are
52+ # considered Ruby objects and *not* V8 objects. So, for example, the
53+ # fundamental conversion for `V8::Object` will return a
54+ # `V8::C::Object`
55+ #
56+ # The `Fundamental` conversion will call
57+ # `ruby_object.to_v8(context)` optionally storing the result in an
58+ # identity map in the case where the result is a `V8::C::Object`
59+ #
60+ # @param [V8::Context] the Ruby context in the conversion happens
61+ # @param [Object] Ruby object to convert
62+ # @return [V8::C::Value] the v8 representation
963 def to_v8 ( context , ruby_object )
1064 super context , ruby_object
1165 end
1266end
1367
68+
69+ ##
70+ # The folowing represent the default conversions from instances of
71+ # `V8::C::Value` into their Ruby counterparts.
1472module V8 ::C
1573 class String
16- alias_method :to_ruby , :Utf8Value
74+ def to_ruby
75+ self . Utf8Value ( )
76+ end
1777 end
1878
1979 class Number
20- alias_method :to_ruby , :Value
80+ def to_ruby
81+ self . Value ( )
82+ end
2183 end
2284
2385 class Undefined
@@ -51,6 +113,9 @@ def to_ruby
51113 end
52114end
53115
116+ ##
117+ # The following are the default conversions from Ruby objects into
118+ # low-level C++ objects.
54119class String
55120 def to_v8 ( context )
56121 V8 ::C ::String ::NewFromUtf8 ( context . isolate . native , self )
@@ -69,25 +134,3 @@ def to_v8(context)
69134 V8 ::C ::Symbol ::For ( context . isolate . native , V8 ::C ::String ::NewFromUtf8 ( isolate , to_s ) )
70135 end
71136end
72-
73- # for type in [TrueClass, FalseClass, NilClass, Float] do
74- # type.class_eval do
75- # include V8::Conversion::Primitive
76- # end
77- # end
78-
79- # for type in [Class, Object, Array, Hash, String, Symbol, Time, Proc, Method, Fixnum] do
80- # type.class_eval do
81- # include V8::Conversion.const_get(type.name)
82- # end
83- # end
84-
85- # class UnboundMethod
86- # include V8::Conversion::Method
87- # end
88-
89- # for type in [:Object, :String, :Date] do
90- # V8::C::const_get(type).class_eval do
91- # include V8::Conversion::const_get("Native#{type}")
92- # end
93- # end
0 commit comments