From d46ff17315dfea2fee1e72b35bae2d10919cf87c Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Thu, 3 Jun 2010 13:41:52 +0300 Subject: [PATCH] invoke javascript methods from ruby. invoke javascript constructors from ruby. --- lib/v8/object.rb | 26 +++++++++++++++++++++----- spec/redjs | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/v8/object.rb b/lib/v8/object.rb index 2332fac..6cfa665 100644 --- a/lib/v8/object.rb +++ b/lib/v8/object.rb @@ -2,19 +2,19 @@ module V8 class Object include Enumerable - + def initialize(native, context = nil) @native = native @context = context || C::Context::GetEntered() raise ScriptError, "V8::Object.new called without an open V8 context" unless @context end - + def [](key) @context.enter do To.ruby(@native.Get(To.v8(key))) end end - + def []=(key, value) value.tap do @context.enter do @@ -22,13 +22,13 @@ module V8 end end end - + def to_s @context.enter do To.rb(@native.ToString()) end end - + def each @context.enter do for prop in To.rb(@native.GetPropertyNames()) @@ -36,6 +36,22 @@ module V8 end end end + + def respond_to?(method) + self[method] != nil + end + + def method_missing(name, *args, &block) + return super(name, *args, &block) unless self.respond_to?(name) + property = self[name] + if property.kind_of?(V8::Function) + property.methodcall(self, *args) + elsif args.empty? + property + else + raise ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty? + end + end end end diff --git a/spec/redjs b/spec/redjs index 08ba26e..69c7cf3 160000 --- a/spec/redjs +++ b/spec/redjs @@ -1 +1 @@ -Subproject commit 08ba26e9295d69c09ecc0ed76c1346b98e52a066 +Subproject commit 69c7cf355d38fda586f4eb1093b0043ecd9430c6