From b483beb493f09761d3285b04e786b501fc4945c5 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 18 Jan 2010 13:06:38 +0200 Subject: [PATCH] update redjs to conform with v8 version. quick and dirty eval() --- lib/rhino/context.rb | 33 ++++++++++++++++----------------- lib/rhino/native_object.rb | 1 - spec/redjs | 2 +- spec/redjs_helper.rb | 2 +- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/rhino/context.rb b/lib/rhino/context.rb index 61f5472..68784cc 100644 --- a/lib/rhino/context.rb +++ b/lib/rhino/context.rb @@ -43,6 +43,10 @@ module Rhino new(options).open(&block) end + def eval(javascript) + new.eval(javascript) + end + end # Create a new javascript environment for executing javascript and ruby code. @@ -83,17 +87,19 @@ module Rhino # * source_name - associated name for this source code. Mainly useful for backtraces. # * line_number - associate this number with the first line of executing source. Mainly useful for backtraces def eval(source, source_name = "", line_number = 1) - begin - scope = To.javascript(@scope) - if IO === source || StringIO === source - result = @native.evaluateReader(scope, IOReader.new(source), source_name, line_number, nil) - else - result = @native.evaluateString(scope, source.to_s, source_name, line_number, nil) + self.open do + begin + scope = To.javascript(@scope) + if IO === source || StringIO === source + result = @native.evaluateReader(scope, IOReader.new(source), source_name, line_number, nil) + else + result = @native.evaluateString(scope, source.to_s, source_name, line_number, nil) + end + To.ruby result + rescue J::RhinoException => e + raise Rhino::RhinoError, e end - To.ruby result - rescue J::RhinoException => e - raise Rhino::RhinoError, e - end if open? + end end def evaluate(*args) # :nodoc: @@ -132,13 +138,6 @@ module Rhino end if block_given? end - private - - def open? - @native == J::Context.getCurrentContext() || (raise ContextError, "context must be open") - end - - end class IOReader < Java::JavaIo::Reader #:nodoc: diff --git a/lib/rhino/native_object.rb b/lib/rhino/native_object.rb index 537f963..4319a04 100644 --- a/lib/rhino/native_object.rb +++ b/lib/rhino/native_object.rb @@ -36,7 +36,6 @@ module Rhino # end def []=(k,v) - #@j.put(k.to_s,@j,To.javascript(v)) J::ScriptableObject.putProperty(@j, k.to_s, To.javascript(v)) end diff --git a/spec/redjs b/spec/redjs index 540f43d..9e8d547 160000 --- a/spec/redjs +++ b/spec/redjs @@ -1 +1 @@ -Subproject commit 540f43d897b928539bbb49243e899294dff9a0b5 +Subproject commit 9e8d547fc7b217f9cab0192c04753499f47fdb38 diff --git a/spec/redjs_helper.rb b/spec/redjs_helper.rb index d815999..aeacf94 100644 --- a/spec/redjs_helper.rb +++ b/spec/redjs_helper.rb @@ -1,5 +1,5 @@ -require File.dirname(__FILE__) + '/../spec_helper.rb' +require File.dirname(__FILE__) + '/spec_helper.rb' include Rhino