mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
simplify memoization logic
replace some `if defined?` statements with a memoized if-statement.
This commit is contained in:
parent
346262e1b3
commit
41f5c85bb2
1 changed files with 19 additions and 28 deletions
|
@ -1,55 +1,46 @@
|
|||
module Rhino
|
||||
|
||||
class JSError < StandardError
|
||||
|
||||
|
||||
def initialize(native)
|
||||
@native = native # NativeException wrapping a Java Throwable
|
||||
message = value ? value : ( cause ? cause.details : @native )
|
||||
super(message)
|
||||
end
|
||||
|
||||
|
||||
def inspect
|
||||
"#<#{self.class.name}: #{message}>"
|
||||
end
|
||||
|
||||
|
||||
# most likely a Rhino::JS::JavaScriptException
|
||||
def cause
|
||||
return @cause if defined?(@cause)
|
||||
@cause = begin
|
||||
if @native.respond_to?(:cause) && @native.cause
|
||||
@native.cause
|
||||
else
|
||||
@native.is_a?(JS::RhinoException) ? @native : nil
|
||||
end
|
||||
@cause ||= if @native.respond_to?(:cause) && @native.cause
|
||||
@native.cause
|
||||
else
|
||||
@native.is_a?(JS::RhinoException) ? @native : nil
|
||||
end
|
||||
end
|
||||
|
||||
def value
|
||||
return @value if defined?(@value)
|
||||
if cause.respond_to?(:value) # e.g. JavaScriptException.getValue
|
||||
@value = cause.value
|
||||
@value ||= if cause.respond_to?(:value) # e.g. JavaScriptException.getValue
|
||||
cause.value
|
||||
elsif ( unwrap = self.unwrap ) && unwrap.respond_to?(:value)
|
||||
@value = unwrap.value
|
||||
else
|
||||
@value = nil
|
||||
unwrap.value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def unwrap
|
||||
return @unwrap if defined?(@unwrap)
|
||||
cause = self.cause
|
||||
if cause && cause.is_a?(JS::WrappedException)
|
||||
@unwrap ||= if cause && cause.is_a?(JS::WrappedException)
|
||||
e = cause.getWrappedException
|
||||
if e && e.is_a?(Java::OrgJrubyExceptions::RaiseException)
|
||||
@unwrap = e.getException
|
||||
e.getException
|
||||
else
|
||||
@unwrap = e
|
||||
e
|
||||
end
|
||||
else
|
||||
@unwrap = nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def backtrace
|
||||
if js_backtrace = javascript_backtrace
|
||||
js_backtrace.push(*super)
|
||||
|
@ -57,7 +48,7 @@ module Rhino
|
|||
super
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def javascript_backtrace(keep_elements = false)
|
||||
if cause.is_a?(JS::RhinoException)
|
||||
cause.getScriptStack.map do |element| # ScriptStackElement[]
|
||||
|
@ -67,9 +58,9 @@ module Rhino
|
|||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Rhino::JS::RhinoException.useMozillaStackStyle(false)
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue