Revision: 25021
Author:   [email protected]
Date:     Thu Oct 30 14:51:17 2014 UTC
Log: Introduce v8::Exception::GetMessage to find location of an error object.

API=v8::Exception::GetMessage
BUG=chromium:427954
[email protected]
LOG=Y

Committed: https://code.google.com/p/v8/source/detail?r=25015

Review URL: https://codereview.chromium.org/687253002
https://code.google.com/p/v8/source/detail?r=25021

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/src/isolate.cc
 /branches/bleeding_edge/src/isolate.h
 /branches/bleeding_edge/test/cctest/test-api.cc

=======================================
--- /branches/bleeding_edge/include/v8.h        Thu Oct 30 13:57:11 2014 UTC
+++ /branches/bleeding_edge/include/v8.h        Thu Oct 30 14:51:17 2014 UTC
@@ -4164,6 +4164,9 @@
   static Local<Value> TypeError(Handle<String> message);
   static Local<Value> Error(Handle<String> message);

+  static Local<Message> GetMessage(Handle<Value> exception);
+
+  // DEPRECATED. Use GetMessage()->GetStackTrace()
   static Local<StackTrace> GetStackTrace(Handle<Value> exception);
 };

@@ -4224,6 +4227,8 @@
   V8_INLINE Handle<Promise> GetPromise() const { return promise_; }
   V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
   V8_INLINE Handle<Value> GetValue() const { return value_; }
+
+  // DEPRECATED. Use v8::Exception::GetMessage(GetValue())->GetStackTrace()
V8_INLINE Handle<StackTrace> GetStackTrace() const { return stack_trace_; }

  private:
=======================================
--- /branches/bleeding_edge/src/api.cc  Thu Oct 30 13:57:11 2014 UTC
+++ /branches/bleeding_edge/src/api.cc  Thu Oct 30 14:51:17 2014 UTC
@@ -6971,6 +6971,17 @@
 #undef DEFINE_ERROR


+Local<Message> Exception::GetMessage(Handle<Value> exception) {
+  i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
+  if (!obj->IsHeapObject()) return Local<Message>();
+  i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
+  ENTER_V8(isolate);
+  i::HandleScope scope(isolate);
+  return Utils::MessageToLocal(
+      scope.CloseAndEscape(isolate->CreateMessage(obj, NULL)));
+}
+
+
 Local<StackTrace> Exception::GetStackTrace(Handle<Value> exception) {
   i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
   if (!obj->IsJSObject()) return Local<StackTrace>();
=======================================
--- /branches/bleeding_edge/src/isolate.cc      Thu Oct 30 13:57:11 2014 UTC
+++ /branches/bleeding_edge/src/isolate.cc      Thu Oct 30 14:51:17 2014 UTC
@@ -1045,6 +1045,40 @@
     }
   }
 }
+
+
+void Isolate::ComputeLocationFromStackTrace(MessageLocation* target,
+                                            Handle<Object> exception) {
+  *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
+
+  if (!exception->IsJSObject()) return;
+  Handle<Name> key = factory()->stack_trace_symbol();
+  Handle<Object> property =
+      JSObject::GetDataProperty(Handle<JSObject>::cast(exception), key);
+  if (!property->IsJSArray()) return;
+  Handle<JSArray> simple_stack_trace = Handle<JSArray>::cast(property);
+
+ Handle<FixedArray> elements(FixedArray::cast(simple_stack_trace->elements()));
+  int elements_limit = Smi::cast(simple_stack_trace->length())->value();
+
+  for (int i = 1; i < elements_limit; i += 4) {
+    Handle<JSFunction> fun =
+        handle(JSFunction::cast(elements->get(i + 1)), this);
+    if (fun->IsFromNativeScript()) continue;
+    Handle<Code> code = handle(Code::cast(elements->get(i + 2)), this);
+    Handle<Smi> offset = handle(Smi::cast(elements->get(i + 3)), this);
+    Address pc = code->address() + offset->value();
+
+    Object* script = fun->shared()->script();
+    if (script->IsScript() &&
+        !(Script::cast(script)->source()->IsUndefined())) {
+      int pos = code->SourcePosition(pc);
+      Handle<Script> casted_script(Script::cast(script));
+      *target = MessageLocation(casted_script, pos, pos + 1);
+      break;
+    }
+  }
+}


 bool Isolate::ShouldReportException(bool* can_be_caught_externally,
@@ -1106,6 +1140,7 @@
 Handle<JSMessageObject> Isolate::CreateMessage(Handle<Object> exception,
                                                MessageLocation* location) {
   Handle<JSArray> stack_trace_object;
+  MessageLocation potential_computed_location;
   if (capture_stack_trace_for_uncaught_exceptions_) {
     if (IsErrorObject(exception)) {
       // We fetch the stack trace that corresponds to this error object.
@@ -1114,14 +1149,22 @@
       // at this throw site.
       stack_trace_object =
           GetDetailedStackTrace(Handle<JSObject>::cast(exception));
+      if (!location) {
+ ComputeLocationFromStackTrace(&potential_computed_location, exception);
+        location = &potential_computed_location;
+      }
     }
     if (stack_trace_object.is_null()) {
-      // Not an error object, we capture at throw site.
+      // Not an error object, we capture stack and location at throw site.
       stack_trace_object = CaptureCurrentStackTrace(
           stack_trace_for_uncaught_exceptions_frame_limit_,
           stack_trace_for_uncaught_exceptions_options_);
     }
   }
+  if (!location) {
+    ComputeLocation(&potential_computed_location);
+    location = &potential_computed_location;
+  }

   // If the exception argument is a custom object, turn it into a string
   // before throwing as uncaught exception.  Note that the pending
@@ -1227,11 +1270,9 @@
Handle<Object> message_obj = CreateMessage(exception_handle, location);

       thread_local_top()->pending_message_obj_ = *message_obj;
-      if (location != NULL) {
-        thread_local_top()->pending_message_script_ = *location->script();
- thread_local_top()->pending_message_start_pos_ = location->start_pos();
-        thread_local_top()->pending_message_end_pos_ = location->end_pos();
-      }
+      thread_local_top()->pending_message_script_ = *location->script();
+ thread_local_top()->pending_message_start_pos_ = location->start_pos();
+      thread_local_top()->pending_message_end_pos_ = location->end_pos();

       // If the abort-on-uncaught-exception flag is specified, abort on any
// exception not caught by JavaScript, even when an external handler is
@@ -1334,7 +1375,6 @@

if (thread_local_top_.pending_exception_ != heap()->termination_exception() &&
       thread_local_top_.has_pending_message_ &&
-      !thread_local_top_.pending_message_obj_->IsTheHole() &&
       !thread_local_top_.pending_message_obj_->IsTheHole()) {
     Handle<Script> script(
         Script::cast(thread_local_top_.pending_message_script_));
=======================================
--- /branches/bleeding_edge/src/isolate.h       Thu Oct 30 13:57:11 2014 UTC
+++ /branches/bleeding_edge/src/isolate.h       Thu Oct 30 14:51:17 2014 UTC
@@ -801,6 +801,11 @@
   // Attempts to compute the current source location, storing the
   // result in the target out parameter.
   void ComputeLocation(MessageLocation* target);
+  void ComputeLocationFromStackTrace(MessageLocation* target,
+                                     Handle<Object> exception);
+
+  Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
+                                        MessageLocation* location);

   // Out of resource exception helpers.
   Object* StackOverflow();
@@ -1201,9 +1206,6 @@
   // then return true.
   bool PropagatePendingExceptionToExternalTryCatch();

-  Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
-                                        MessageLocation* location);
-
// Traverse prototype chain to find out whether the object is derived from
   // the Error object.
   bool IsErrorObject(Handle<Object> obj);
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc Thu Oct 30 13:57:11 2014 UTC +++ /branches/bleeding_edge/test/cctest/test-api.cc Thu Oct 30 14:51:17 2014 UTC
@@ -8547,7 +8547,7 @@
 }


-THREADED_TEST(ExceptionGetStackTrace) {
+THREADED_TEST(ExceptionGetMessage) {
   LocalContext context;
   v8::HandleScope scope(context->GetIsolate());

@@ -8559,16 +8559,25 @@
   global->Set(v8_str("throwV8Exception"), fun->GetFunction());

   TryCatch try_catch;
-  CompileRun("function f1() { throwV8Exception(); }; f1();");
+  CompileRun(
+      "function f1() {\n"
+      "  throwV8Exception();\n"
+      "};\n"
+      "f1();");
   CHECK(try_catch.HasCaught());

   v8::Handle<v8::Value> error = try_catch.Exception();
-  v8::Handle<String> foo = v8_str("foo");
-  v8::Handle<String> message = v8_str("message");
+  v8::Handle<String> foo_str = v8_str("foo");
+  v8::Handle<String> message_str = v8_str("message");
   CHECK(error->IsObject());
-  CHECK(error.As<v8::Object>()->Get(message)->Equals(foo));
+  CHECK(error.As<v8::Object>()->Get(message_str)->Equals(foo_str));

- v8::Handle<v8::StackTrace> stackTrace = v8::Exception::GetStackTrace(error);
+  v8::Handle<v8::Message> message = v8::Exception::GetMessage(error);
+  CHECK(!message.IsEmpty());
+  CHECK_EQ(2, message->GetLineNumber());
+  CHECK_EQ(2, message->GetStartColumn());
+
+  v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace();
   CHECK(!stackTrace.IsEmpty());
   CHECK_EQ(2, stackTrace->GetFrameCount());

@@ -17873,7 +17882,8 @@
     promise_reject_counter++;
     CcTest::global()->Set(v8_str("rejected"), message.GetPromise());
     CcTest::global()->Set(v8_str("value"), message.GetValue());
-    v8::Handle<v8::StackTrace> stack_trace = message.GetStackTrace();
+    v8::Handle<v8::StackTrace> stack_trace =
+        v8::Exception::GetMessage(message.GetValue())->GetStackTrace();
     if (!stack_trace.IsEmpty()) {
       promise_reject_frame_count = stack_trace->GetFrameCount();
       if (promise_reject_frame_count > 0) {
@@ -17887,7 +17897,6 @@
     promise_revoke_counter++;
     CcTest::global()->Set(v8_str("revoked"), message.GetPromise());
     CHECK(message.GetValue().IsEmpty());
-    CHECK(message.GetStackTrace().IsEmpty());
   }
 }

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to