From 1349384c0883956862f609f6eb0f5730631e3ceb Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sat, 16 Jun 2012 12:31:08 -0500 Subject: [PATCH] allow embedding BigDecimal --- lib/v8.rb | 1 + lib/v8/conversion.rb | 2 +- lib/v8/conversion/bigdecimal.rb | 11 +++++++++++ lib/v8/conversion/object.rb | 2 -- spec/v8/conversion_spec.rb | 9 +++++++++ 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 lib/v8/conversion/bigdecimal.rb create mode 100644 spec/v8/conversion_spec.rb diff --git a/lib/v8.rb b/lib/v8.rb index 58b0f4f..0a54ff2 100644 --- a/lib/v8.rb +++ b/lib/v8.rb @@ -12,6 +12,7 @@ require 'v8/conversion/code' require 'v8/conversion/class' require 'v8/conversion/object' require 'v8/conversion/time' +require 'v8/conversion/bigdecimal' require 'v8/conversion/hash' require 'v8/conversion/array' require 'v8/conversion/proc' diff --git a/lib/v8/conversion.rb b/lib/v8/conversion.rb index b993dad..9da0b8c 100644 --- a/lib/v8/conversion.rb +++ b/lib/v8/conversion.rb @@ -18,7 +18,7 @@ for type in [TrueClass, FalseClass, NilClass, Numeric] do end end -for type in [Class, Object, Array, Hash, String, Symbol, Time, Proc, Method] do +for type in [Class, Object, Array, Hash, String, Symbol, Time, BigDecimal, Proc, Method] do type.class_eval do include V8::Conversion.const_get(name) end diff --git a/lib/v8/conversion/bigdecimal.rb b/lib/v8/conversion/bigdecimal.rb new file mode 100644 index 0000000..e78cb54 --- /dev/null +++ b/lib/v8/conversion/bigdecimal.rb @@ -0,0 +1,11 @@ +require 'bigdecimal' +class V8::Conversion + module BigDecimal + def to_v8 + context = V8::Context.current + constructor = context.to_v8(self.class) + object = constructor.NewInstance([V8::C::External::New(self)]) + return object + end + end +end \ No newline at end of file diff --git a/lib/v8/conversion/object.rb b/lib/v8/conversion/object.rb index 38d09da..5d55255 100644 --- a/lib/v8/conversion/object.rb +++ b/lib/v8/conversion/object.rb @@ -1,7 +1,5 @@ class V8::Conversion module Object - include V8::Util::Weakcell - def to_v8 context = V8::Context.current constructor = context.to_v8(self.class) diff --git a/spec/v8/conversion_spec.rb b/spec/v8/conversion_spec.rb new file mode 100644 index 0000000..783dfc1 --- /dev/null +++ b/spec/v8/conversion_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe V8::Conversion do + it "can embed BigDecimal values" do + cxt = V8::Context.new + cxt['big'] = BigDecimal.new('1.1') + cxt['big'].should eql BigDecimal.new('1.1') + end +end