Module: V8

Defined in:
lib/v8/weak.rb,
lib/v8/error.rb,
lib/v8/stack.rb,
lib/v8/context.rb,
lib/v8/version.rb,
ext/v8/locker.cc,
ext/v8/handles.cc,
ext/v8/trycatch.cc

Defined Under Namespace

Modules: C, Weak Classes: Access, Array, Context, Conversion, Error, Function, Object, StackFrame, StackTrace

Constant Summary collapse

VERSION =
"0.12.2"

Class Method Summary collapse

Class Method Details

.Error(trycatch) ⇒ V8::Error

Convert the result of a triggered JavaScript try/catch block into a V8::Error

This is a bit of a yak-shave because JavaScript let’s you throw all kinds of things. We do our best to make sure that the message property of the resulting V8::Error is as helpful as possible, and that it contains as much source location information as we can put onto it.

For example:

throw 4
throw 'four'
throw {number: 4}

are all valid cases, none of which actually reference an exception object with a stack trace and a message. only with something like:

throw new Error('fail!')

do you get the a proper stacktrace and a message property. However a lot of times JavaScript library authors are lazy and do this:

throw {message: 'foo', otherMetadata: 'bar'}

It’s common enough so we do the courtesy of having the resulting V8::Error have as its message in ruby land the ‘message’ property of the value object

To further complicate things, SyntaxErrors do not have a JavaScript stack (even if they occur during js execution). This can make debugging a nightmare so we copy in the source location of the syntax error into the message of the resulting V8::Error

Parameters:

  • native (V8::C::TryCatch)

    trycatch object that has been triggered

Returns:

  • (V8::Error)

    the error generated by this try/catch



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/v8/error.rb', line 140

def self.Error(trycatch)
  exception = trycatch.Exception()

  value = exception.to_ruby
  cause = nil
  message = trycatch.Message()
  javascript_backtrace = V8::StackTrace.new(message.GetStackTrace()) if message

  message = if !exception.kind_of?(V8::C::Value)
    exception.to_s==""?"Script Timed Out":exception.to_s
  elsif exception.IsNativeError()
    if cause = exception.GetHiddenValue("rr::Cause")
      cause = cause.Value()
    end
    if value['constructor'] == V8::Context.current['SyntaxError']
      info = trycatch.Message()
      resource_name = info.GetScriptResourceName().to_ruby
      "#{value['message']} at #{resource_name}:#{info.GetLineNumber()}:#{info.GetStartColumn() + 1}"
    else
      exception.Get("message").to_ruby
    end
  elsif exception.IsObject()
    value['message'] || value.to_s
  else
    value.to_s
  end
  V8::Error.new(message, value, javascript_backtrace, cause)
end