1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00

specs and javascript_backtrace fix for JSError + deprecate JavascriptError

This commit is contained in:
kares 2011-12-09 11:28:27 +01:00
parent 7a74b534cd
commit b59f15acbe
6 changed files with 111 additions and 51 deletions

View file

@ -22,6 +22,7 @@ Rhino.extend Rhino::To
require 'rhino/object' require 'rhino/object'
require 'rhino/context' require 'rhino/context'
require 'rhino/error'
require 'rhino/rhino_ext' require 'rhino/rhino_ext'
require 'rhino/ruby_object' require 'rhino/ruby_object'
require 'rhino/ruby_function' require 'rhino/ruby_function'

View file

@ -90,23 +90,18 @@ module Rhino
# * <tt>source_name</tt> - associated name for this source code. Mainly useful for backtraces. # * <tt>source_name</tt> - associated name for this source code. Mainly useful for backtraces.
# * <tt>line_number</tt> - associate this number with the first line of executing source. Mainly useful for backtraces # * <tt>line_number</tt> - associate this number with the first line of executing source. Mainly useful for backtraces
def eval(source, source_name = "<eval>", line_number = 1) def eval(source, source_name = "<eval>", line_number = 1)
self.open do open do
begin if IO === source || StringIO === source
scope = Rhino.to_javascript(@scope) result = @native.evaluateReader(@scope, IOReader.new(source), source_name, line_number, nil)
if IO === source || StringIO === source else
result = @native.evaluateReader(scope, IOReader.new(source), source_name, line_number, nil) result = @native.evaluateString(@scope, source.to_s, source_name, line_number, nil)
else
result = @native.evaluateString(scope, source.to_s, source_name, line_number, nil)
end
Rhino.to_ruby(result)
rescue JS::RhinoException => e
raise JavascriptError, e
end end
Rhino.to_ruby(result)
end end
end end
def evaluate(*args) # :nodoc: def evaluate(*args) # :nodoc:
self.eval(*args) eval(*args) # an alias
end end
# Read the contents of <tt>filename</tt> and evaluate it as javascript. Returns the result of evaluating the # Read the contents of <tt>filename</tt> and evaluate it as javascript. Returns the result of evaluating the
@ -163,40 +158,50 @@ module Rhino
end end
end end
# Enter this context for operations. Some methods such as eval() will # Enter this context for operations.
# fail unless this context is open # Some methods such as eval() will fail unless this context is open !
def open def open(&block)
begin do_open(&block)
@factory.enterContext(@native) rescue JS::RhinoException => e
yield self raise Rhino::JSError.new(e)
ensure
JS::Context.exit()
end if block_given?
end end
private
def do_open
begin
@factory.enterContext(@native)
yield self
ensure
JS::Context.exit
end
end
end end
class IOReader < java.io.Reader #:nodoc: class IOReader < java.io.Reader # :nodoc:
def initialize(io) def initialize(io)
@io = io @io = io
end end
def read(charbuffer, offset, length) # implement int Reader#read(char[] buffer, int offset, int length)
def read(buffer, offset, length)
str = nil
begin begin
str = @io.read(length) str = @io.read(length)
if str.nil?
return -1
else
jstring = java.lang.String.new(str)
for i in 0 .. jstring.length - 1
charbuffer[i + offset] = jstring.charAt(i)
end
return jstring.length
end
rescue StandardError => e rescue StandardError => e
raise java.io.IOException.new, "Failed reading from ruby IO object" raise java.io.IOException.new, "Failed reading from ruby IO object"
end end
if str.nil?
return -1
else
jstr = str.to_java
for i in 0 .. jstr.length - 1
buffer[i + offset] = jstr.charAt(i)
end
return jstr.length
end
end end
end end
@ -216,23 +221,7 @@ module Rhino
class ContextError < StandardError # :nodoc: class ContextError < StandardError # :nodoc:
end end
class JavascriptError < StandardError # :nodoc: class RunawayScriptError < ContextError # :nodoc:
def initialize(native)
@native = native
end
def message
@native.cause.details
end
def javascript_backtrace
@native.getScriptStackTrace()
end
end
JSError = JavascriptError
class RunawayScriptError < StandardError # :nodoc:
end end
end end

View file

@ -8,6 +8,9 @@ module Rhino
when 'J' then when 'J' then
warn "[DEPRECATION] `Rhino::J` is deprecated, use `Rhino::JS` instead." warn "[DEPRECATION] `Rhino::J` is deprecated, use `Rhino::JS` instead."
return JS return JS
when 'JavascriptError' then
warn "[DEPRECATION] `Rhino::JavascriptError` is deprecated, use `Rhino::JSError` instead."
return JSError
when 'NativeObject' then when 'NativeObject' then
warn "[DEPRECATION] `Rhino::NativeObject` is no longer used, returning a stub." warn "[DEPRECATION] `Rhino::NativeObject` is no longer used, returning a stub."
return @@stub_class return @@stub_class

25
lib/rhino/error.rb Normal file
View file

@ -0,0 +1,25 @@
module Rhino
class JSError < StandardError
def initialize(native)
@native = native # NativeException wrapping a Java Throwable
end
# most likely a Rhino::JS::JavaScriptException
def cause
@native.respond_to?(:cause) ? @native.cause : nil
end
def message
cause ? cause.details : @native.to_s
end
def javascript_backtrace
cause.is_a?(JS::RhinoException) ? cause.getScriptStackTrace : nil
end
end
end

View file

@ -33,5 +33,9 @@ describe 'deprecations' do
it "NativeFunction constant exists" do it "NativeFunction constant exists" do
lambda { Rhino::NativeFunction }.should_not raise_error lambda { Rhino::NativeFunction }.should_not raise_error
end end
it "JavascriptError returns JSError" do
lambda { Rhino::JavascriptError.should be(Rhino::JSError) }.should_not raise_error
end
end end

38
spec/rhino/error_spec.rb Normal file
View file

@ -0,0 +1,38 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))
describe Rhino::JSError do
it "works as a StandardError with a message being passed" do
js_error = Rhino::JSError.new 'an error message'
lambda { js_error.to_s && js_error.inspect }.should_not raise_error
js_error.cause.should be_nil
js_error.message.should == 'an error message'
js_error.javascript_backtrace.should be_nil
end
it "might wrap a RhinoException wrapped in a NativeException like error" do
# JRuby's NativeException.new(rhino_e) does not work as it is
# intended to handle Java exceptions ... no new on the Ruby side
native_error_class = Class.new(RuntimeError) do
def initialize(cause)
@cause = cause
end
def cause
@cause
end
end
rhino_e = Rhino::JS::JavaScriptException.new("42".to_java)
js_error = Rhino::JSError.new native_error_class.new(rhino_e)
lambda { js_error.to_s && js_error.inspect }.should_not raise_error
js_error.cause.should be(rhino_e)
js_error.message.should == '42'
js_error.javascript_backtrace.should_not be_nil
end
end