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);
};
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:
#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>();
}
+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,
bool catchable_by_javascript) {
// Find the top-most try-catch handler.
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.
// 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
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
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_));
// 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();
// 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);
}
-THREADED_TEST(ExceptionGetStackTrace) {
+THREADED_TEST(ExceptionGetMessage) {
LocalContext context;
v8::HandleScope scope(context->GetIsolate());
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::Message> message = v8::Exception::GetMessage(error);
+ CHECK(!message.IsEmpty());
+ CHECK_EQ(2, message->GetLineNumber());
+ CHECK_EQ(2, message->GetStartColumn());
- v8::Handle<v8::StackTrace> stackTrace = v8::Exception::GetStackTrace(error);
+ v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace();
CHECK(!stackTrace.IsEmpty());
CHECK_EQ(2, stackTrace->GetFrameCount());
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) {
promise_revoke_counter++;
CcTest::global()->Set(v8_str("revoked"), message.GetPromise());
CHECK(message.GetValue().IsEmpty());
- CHECK(message.GetStackTrace().IsEmpty());
}
}