2008-07-03 15:10:15 +00:00
|
|
|
|
2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#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.
|
|
|
|
void MessageHandler::DefaultMessageReport(const MessageLocation* loc,
|
|
|
|
Handle<Object> message_obj) {
|
|
|
|
SmartPointer<char> str = GetLocalizedMessage(message_obj);
|
|
|
|
if (loc == NULL) {
|
|
|
|
PrintF("%s\n", *str);
|
|
|
|
} else {
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<Object> data(loc->script()->name());
|
2008-09-11 11:24:45 +00:00
|
|
|
SmartPointer<char> data_str;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (data->IsString())
|
|
|
|
data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
|
|
|
|
PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>",
|
|
|
|
loc->start_pos(), *str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-02 13:31:52 +00:00
|
|
|
Handle<JSMessageObject> MessageHandler::MakeMessageObject(
|
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<String> stack_trace,
|
|
|
|
Handle<JSArray> stack_frames) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type);
|
2011-02-08 19:42:14 +00:00
|
|
|
Handle<FixedArray> arguments_elements =
|
2011-03-18 20:35:07 +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 =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewJSArrayWithElements(arguments_elements);
|
2011-02-02 13:31:52 +00:00
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
int end = 0;
|
2011-03-18 20:35:07 +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();
|
2011-02-02 13:31:52 +00:00
|
|
|
script_handle = GetScriptWrapper(loc->script());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-02-02 13:31:52 +00:00
|
|
|
|
|
|
|
Handle<Object> stack_trace_handle = stack_trace.is_null()
|
2011-03-18 20:35:07 +00:00
|
|
|
? FACTORY->undefined_value()
|
2011-02-02 13:31:52 +00:00
|
|
|
: Handle<Object>::cast(stack_trace);
|
|
|
|
|
2011-02-08 19:42:14 +00:00
|
|
|
Handle<Object> stack_frames_handle = stack_frames.is_null()
|
2011-03-18 20:35:07 +00:00
|
|
|
? FACTORY->undefined_value()
|
2011-02-02 13:31:52 +00:00
|
|
|
: Handle<Object>::cast(stack_frames);
|
|
|
|
|
|
|
|
Handle<JSMessageObject> message =
|
2011-03-18 20:35:07 +00:00
|
|
|
FACTORY->NewJSMessageObject(type_handle,
|
2011-02-02 13:31:52 +00:00
|
|
|
arguments_handle,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
script_handle,
|
|
|
|
stack_trace_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.
|
|
|
|
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);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
v8::NeanderArray global_listeners(FACTORY->message_listeners());
|
2008-07-03 15:10:15 +00:00
|
|
|
int global_length = global_listeners.length();
|
|
|
|
if (global_length == 0) {
|
|
|
|
DefaultMessageReport(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++) {
|
|
|
|
HandleScope scope;
|
|
|
|
if (global_listeners.get(i)->IsUndefined()) continue;
|
|
|
|
v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
|
|
|
|
Handle<Proxy> callback_obj(Proxy::cast(listener.get(0)));
|
|
|
|
v8::MessageCallback callback =
|
|
|
|
FUNCTION_CAST<v8::MessageCallback>(callback_obj->proxy());
|
|
|
|
Handle<Object> callback_data(listener.get(1));
|
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;
|
2011-04-11 16:16:52 +00:00
|
|
|
callback(api_message_obj, v8::Utils::ToLocal(callback_data));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> MessageHandler::GetMessage(Handle<Object> data) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage");
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSFunction> fun =
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSFunction>(
|
|
|
|
JSFunction::cast(
|
|
|
|
Isolate::Current()->js_builtins_object()->
|
|
|
|
GetPropertyNoExceptionThrown(*fmt_str)));
|
2008-07-03 15:10:15 +00:00
|
|
|
Object** argv[1] = { data.location() };
|
|
|
|
|
|
|
|
bool caught_exception;
|
|
|
|
Handle<Object> result =
|
2011-03-18 20:35:07 +00:00
|
|
|
Execution::TryCall(fun,
|
|
|
|
Isolate::Current()->js_builtins_object(), 1, argv, &caught_exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if (caught_exception || !result->IsString()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return FACTORY->LookupAsciiSymbol("<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).
|
|
|
|
FlattenString(result_string);
|
|
|
|
return result_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SmartPointer<char> MessageHandler::GetLocalizedMessage(Handle<Object> data) {
|
|
|
|
HandleScope scope;
|
|
|
|
return GetMessage(data)->ToCString(DISALLOW_NULLS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|