2011-10-06 09:31:38 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "api.h"
|
|
|
|
#include "execution.h"
|
2010-03-09 06:38:33 +00:00
|
|
|
#include "messages.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "spaces-inl.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// If no message listeners have been registered this one is called
|
|
|
|
// by default.
|
2013-02-15 09:27:10 +00:00
|
|
|
void MessageHandler::DefaultMessageReport(Isolate* isolate,
|
|
|
|
const MessageLocation* loc,
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> message_obj) {
|
2013-02-15 09:27:10 +00:00
|
|
|
SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (loc == NULL) {
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("%s\n", str.get());
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> data(loc->script()->name(), isolate);
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<char> data_str;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (data->IsString())
|
|
|
|
data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
|
|
|
|
loc->start_pos(), str.get());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-02 13:31:52 +00:00
|
|
|
Handle<JSMessageObject> MessageHandler::MakeMessageObject(
|
2013-06-04 10:30:05 +00:00
|
|
|
Isolate* isolate,
|
2008-09-09 18:55:41 +00:00
|
|
|
const char* type,
|
|
|
|
MessageLocation* loc,
|
|
|
|
Vector< Handle<Object> > args,
|
2010-07-12 13:17:27 +00:00
|
|
|
Handle<JSArray> stack_frames) {
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Handle<String> type_handle = factory->InternalizeUtf8String(type);
|
2011-02-08 19:42:14 +00:00
|
|
|
Handle<FixedArray> arguments_elements =
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewFixedArray(args.length());
|
2011-02-02 13:31:52 +00:00
|
|
|
for (int i = 0; i < args.length(); i++) {
|
2011-02-08 19:42:14 +00:00
|
|
|
arguments_elements->set(i, *args[i]);
|
2011-02-02 13:31:52 +00:00
|
|
|
}
|
2011-02-08 19:42:14 +00:00
|
|
|
Handle<JSArray> arguments_handle =
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewJSArrayWithElements(arguments_elements);
|
2011-02-02 13:31:52 +00:00
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
int end = 0;
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<Object> script_handle = factory->undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (loc) {
|
|
|
|
start = loc->start_pos();
|
|
|
|
end = loc->end_pos();
|
2014-04-16 13:28:11 +00:00
|
|
|
script_handle = Script::GetWrapper(loc->script());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-02-02 13:31:52 +00:00
|
|
|
|
2011-02-08 19:42:14 +00:00
|
|
|
Handle<Object> stack_frames_handle = stack_frames.is_null()
|
2013-06-04 10:30:05 +00:00
|
|
|
? Handle<Object>::cast(factory->undefined_value())
|
2011-02-02 13:31:52 +00:00
|
|
|
: Handle<Object>::cast(stack_frames);
|
|
|
|
|
|
|
|
Handle<JSMessageObject> message =
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewJSMessageObject(type_handle,
|
2011-02-02 13:31:52 +00:00
|
|
|
arguments_handle,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
script_handle,
|
|
|
|
stack_frames_handle);
|
|
|
|
|
|
|
|
return message;
|
2008-09-09 18:55:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-04-07 19:52:24 +00:00
|
|
|
void MessageHandler::ReportMessage(Isolate* isolate,
|
|
|
|
MessageLocation* loc,
|
2008-09-09 18:55:41 +00:00
|
|
|
Handle<Object> message) {
|
2011-04-07 19:52:24 +00:00
|
|
|
// We are calling into embedder's code which can throw exceptions.
|
|
|
|
// Thus we need to save current exception state, reset it to the clean one
|
|
|
|
// and ignore scheduled exceptions callbacks can throw.
|
2012-10-19 08:45:24 +00:00
|
|
|
|
|
|
|
// We pass the exception object into the message handler callback though.
|
|
|
|
Object* exception_object = isolate->heap()->undefined_value();
|
|
|
|
if (isolate->has_pending_exception()) {
|
2014-04-08 09:44:24 +00:00
|
|
|
exception_object = isolate->pending_exception();
|
2012-10-19 08:45:24 +00:00
|
|
|
}
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> exception_handle(exception_object, isolate);
|
2012-10-19 08:45:24 +00:00
|
|
|
|
2011-04-07 19:52:24 +00:00
|
|
|
Isolate::ExceptionScope exception_scope(isolate);
|
|
|
|
isolate->clear_pending_exception();
|
|
|
|
isolate->set_external_caught_exception(false);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
|
2012-10-19 08:45:24 +00:00
|
|
|
v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-06-04 10:30:05 +00:00
|
|
|
v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
|
2008-07-03 15:10:15 +00:00
|
|
|
int global_length = global_listeners.length();
|
|
|
|
if (global_length == 0) {
|
2013-02-15 09:27:10 +00:00
|
|
|
DefaultMessageReport(isolate, loc, message);
|
2011-04-07 19:52:24 +00:00
|
|
|
if (isolate->has_scheduled_exception()) {
|
|
|
|
isolate->clear_scheduled_exception();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < global_length; i++) {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (global_listeners.get(i)->IsUndefined()) continue;
|
2013-02-27 14:35:39 +00:00
|
|
|
v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
|
|
|
|
Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::MessageCallback callback =
|
2011-10-28 12:37:29 +00:00
|
|
|
FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
|
2013-02-27 14:35:39 +00:00
|
|
|
Handle<Object> callback_data(listener.get(1), isolate);
|
2011-04-11 16:16:52 +00:00
|
|
|
{
|
|
|
|
// Do not allow exceptions to propagate.
|
2011-04-18 13:28:00 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-02-27 14:35:39 +00:00
|
|
|
callback(api_message_obj, callback_data->IsUndefined()
|
|
|
|
? api_exception_obj
|
|
|
|
: v8::Utils::ToLocal(callback_data));
|
2011-04-11 16:16:52 +00:00
|
|
|
}
|
2011-04-07 19:52:24 +00:00
|
|
|
if (isolate->has_scheduled_exception()) {
|
|
|
|
isolate->clear_scheduled_exception();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<String> MessageHandler::GetMessage(Isolate* isolate,
|
|
|
|
Handle<Object> data) {
|
|
|
|
Factory* factory = isolate->factory();
|
2012-12-17 15:56:16 +00:00
|
|
|
Handle<String> fmt_str =
|
2013-02-28 17:03:34 +00:00
|
|
|
factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("FormatMessage"));
|
2014-04-11 12:47:34 +00:00
|
|
|
Handle<JSFunction> fun = Handle<JSFunction>::cast(Object::GetProperty(
|
|
|
|
isolate->js_builtins_object(), fmt_str).ToHandleChecked());
|
2012-11-09 08:22:02 +00:00
|
|
|
Handle<JSMessageObject> message = Handle<JSMessageObject>::cast(data);
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> argv[] = { Handle<Object>(message->type(), isolate),
|
|
|
|
Handle<Object>(message->arguments(), isolate) };
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-11 10:41:09 +00:00
|
|
|
MaybeHandle<Object> maybe_result = Execution::TryCall(
|
|
|
|
fun, isolate->js_builtins_object(), ARRAY_SIZE(argv), argv);
|
|
|
|
Handle<Object> result;
|
|
|
|
if (!maybe_result.ToHandle(&result) || !result->IsString()) {
|
2013-02-28 17:03:34 +00:00
|
|
|
return factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("<error>"));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Handle<String> result_string = Handle<String>::cast(result);
|
|
|
|
// A string that has been obtained from JS code in this way is
|
|
|
|
// likely to be a complicated ConsString of some sort. We flatten it
|
|
|
|
// here to improve the efficiency of converting it to a C string and
|
|
|
|
// other operations that are likely to take place (see GetLocalizedMessage
|
|
|
|
// for example).
|
2014-04-08 09:49:49 +00:00
|
|
|
result_string = String::Flatten(result_string);
|
2008-07-03 15:10:15 +00:00
|
|
|
return result_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<char> MessageHandler::GetLocalizedMessage(
|
2013-02-15 09:27:10 +00:00
|
|
|
Isolate* isolate,
|
2011-09-09 22:39:47 +00:00
|
|
|
Handle<Object> data) {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2013-02-25 14:46:09 +00:00
|
|
|
return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|