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:
parent
7a74b534cd
commit
b59f15acbe
6 changed files with 111 additions and 51 deletions
|
@ -22,6 +22,7 @@ Rhino.extend Rhino::To
|
|||
|
||||
require 'rhino/object'
|
||||
require 'rhino/context'
|
||||
require 'rhino/error'
|
||||
require 'rhino/rhino_ext'
|
||||
require 'rhino/ruby_object'
|
||||
require 'rhino/ruby_function'
|
||||
|
|
|
@ -90,23 +90,18 @@ module Rhino
|
|||
# * <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
|
||||
def eval(source, source_name = "<eval>", line_number = 1)
|
||||
self.open do
|
||||
begin
|
||||
scope = Rhino.to_javascript(@scope)
|
||||
open do
|
||||
if IO === source || StringIO === source
|
||||
result = @native.evaluateReader(scope, IOReader.new(source), source_name, line_number, nil)
|
||||
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)
|
||||
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
|
||||
end
|
||||
|
||||
def evaluate(*args) # :nodoc:
|
||||
self.eval(*args)
|
||||
eval(*args) # an alias
|
||||
end
|
||||
|
||||
# Read the contents of <tt>filename</tt> and evaluate it as javascript. Returns the result of evaluating the
|
||||
|
@ -163,15 +158,23 @@ module Rhino
|
|||
end
|
||||
end
|
||||
|
||||
# Enter this context for operations. Some methods such as eval() will
|
||||
# fail unless this context is open
|
||||
def open
|
||||
# Enter this context for operations.
|
||||
# Some methods such as eval() will fail unless this context is open !
|
||||
def open(&block)
|
||||
do_open(&block)
|
||||
rescue JS::RhinoException => e
|
||||
raise Rhino::JSError.new(e)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def do_open
|
||||
begin
|
||||
@factory.enterContext(@native)
|
||||
yield self
|
||||
ensure
|
||||
JS::Context.exit()
|
||||
end if block_given?
|
||||
JS::Context.exit
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -182,20 +185,22 @@ module Rhino
|
|||
@io = io
|
||||
end
|
||||
|
||||
def read(charbuffer, offset, length)
|
||||
# implement int Reader#read(char[] buffer, int offset, int length)
|
||||
def read(buffer, offset, length)
|
||||
str = nil
|
||||
begin
|
||||
str = @io.read(length)
|
||||
rescue StandardError => e
|
||||
raise java.io.IOException.new, "Failed reading from ruby IO object"
|
||||
end
|
||||
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)
|
||||
jstr = str.to_java
|
||||
for i in 0 .. jstr.length - 1
|
||||
buffer[i + offset] = jstr.charAt(i)
|
||||
end
|
||||
return jstring.length
|
||||
end
|
||||
rescue StandardError => e
|
||||
raise java.io.IOException.new, "Failed reading from ruby IO object"
|
||||
return jstr.length
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -216,23 +221,7 @@ module Rhino
|
|||
class ContextError < StandardError # :nodoc:
|
||||
end
|
||||
|
||||
class JavascriptError < StandardError # :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:
|
||||
class RunawayScriptError < ContextError # :nodoc:
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -8,6 +8,9 @@ module Rhino
|
|||
when 'J' then
|
||||
warn "[DEPRECATION] `Rhino::J` is deprecated, use `Rhino::JS` instead."
|
||||
return JS
|
||||
when 'JavascriptError' then
|
||||
warn "[DEPRECATION] `Rhino::JavascriptError` is deprecated, use `Rhino::JSError` instead."
|
||||
return JSError
|
||||
when 'NativeObject' then
|
||||
warn "[DEPRECATION] `Rhino::NativeObject` is no longer used, returning a stub."
|
||||
return @@stub_class
|
||||
|
|
25
lib/rhino/error.rb
Normal file
25
lib/rhino/error.rb
Normal 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
|
|
@ -34,4 +34,8 @@ describe 'deprecations' do
|
|||
lambda { Rhino::NativeFunction }.should_not raise_error
|
||||
end
|
||||
|
||||
it "JavascriptError returns JSError" do
|
||||
lambda { Rhino::JavascriptError.should be(Rhino::JSError) }.should_not raise_error
|
||||
end
|
||||
|
||||
end
|
38
spec/rhino/error_spec.rb
Normal file
38
spec/rhino/error_spec.rb
Normal 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
|
Loading…
Reference in a new issue