blob: 4cbf0af74711adfcf54f5636524aca5f718c087a [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following
11// disclaimer in the documentation and/or other materials provided
12// with the distribution.
13// * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived
15// from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "v8.h"
30
31#include "api.h"
32#include "execution.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000033#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "spaces-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
39
40// If no message listeners have been registered this one is called
41// by default.
42void MessageHandler::DefaultMessageReport(const MessageLocation* loc,
43 Handle<Object> message_obj) {
44 SmartPointer<char> str = GetLocalizedMessage(message_obj);
45 if (loc == NULL) {
46 PrintF("%s\n", *str);
47 } else {
48 HandleScope scope;
49 Handle<Object> data(loc->script()->name());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000050 SmartPointer<char> data_str;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051 if (data->IsString())
52 data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
53 PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>",
54 loc->start_pos(), *str);
55 }
56}
57
58
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000059Handle<JSMessageObject> MessageHandler::MakeMessageObject(
ager@chromium.org9258b6b2008-09-11 09:11:10 +000060 const char* type,
61 MessageLocation* loc,
62 Vector< Handle<Object> > args,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +000063 Handle<String> stack_trace,
64 Handle<JSArray> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000065 Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +000066 Handle<FixedArray> arguments_elements =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000067 FACTORY->NewFixedArray(args.length());
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000068 for (int i = 0; i < args.length(); i++) {
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +000069 arguments_elements->set(i, *args[i]);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000070 }
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +000071 Handle<JSArray> arguments_handle =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000072 FACTORY->NewJSArrayWithElements(arguments_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000074 int start = 0;
75 int end = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000076 Handle<Object> script_handle = FACTORY->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077 if (loc) {
78 start = loc->start_pos();
79 end = loc->end_pos();
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000080 script_handle = GetScriptWrapper(loc->script());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000083 Handle<Object> stack_trace_handle = stack_trace.is_null()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 ? FACTORY->undefined_value()
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000085 : Handle<Object>::cast(stack_trace);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000086
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +000087 Handle<Object> stack_frames_handle = stack_frames.is_null()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000088 ? FACTORY->undefined_value()
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000089 : Handle<Object>::cast(stack_frames);
ager@chromium.org3bf7b912008-11-17 09:09:45 +000090
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000091 Handle<JSMessageObject> message =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 FACTORY->NewJSMessageObject(type_handle,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000093 arguments_handle,
94 start,
95 end,
96 script_handle,
97 stack_trace_handle,
98 stack_frames_handle);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000099
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000100 return message;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000101}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000103
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000104void MessageHandler::ReportMessage(Isolate* isolate,
105 MessageLocation* loc,
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000106 Handle<Object> message) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000107 // We are calling into embedder's code which can throw exceptions.
108 // Thus we need to save current exception state, reset it to the clean one
109 // and ignore scheduled exceptions callbacks can throw.
110 Isolate::ExceptionScope exception_scope(isolate);
111 isolate->clear_pending_exception();
112 isolate->set_external_caught_exception(false);
113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
115
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 v8::NeanderArray global_listeners(FACTORY->message_listeners());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 int global_length = global_listeners.length();
118 if (global_length == 0) {
119 DefaultMessageReport(loc, message);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000120 if (isolate->has_scheduled_exception()) {
121 isolate->clear_scheduled_exception();
122 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 } else {
124 for (int i = 0; i < global_length; i++) {
125 HandleScope scope;
126 if (global_listeners.get(i)->IsUndefined()) continue;
127 v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000128 Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 v8::MessageCallback callback =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000130 FUNCTION_CAST<v8::MessageCallback>(callback_obj->address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 Handle<Object> callback_data(listener.get(1));
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000132 {
133 // Do not allow exceptions to propagate.
ricow@chromium.orgdcebac02011-04-20 09:44:50 +0000134 v8::TryCatch try_catch;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000135 callback(api_message_obj, v8::Utils::ToLocal(callback_data));
136 }
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000137 if (isolate->has_scheduled_exception()) {
138 isolate->clear_scheduled_exception();
139 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 }
141 }
142}
143
144
145Handle<String> MessageHandler::GetMessage(Handle<Object> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 Handle<JSFunction> fun =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 Handle<JSFunction>(
149 JSFunction::cast(
150 Isolate::Current()->js_builtins_object()->
151 GetPropertyNoExceptionThrown(*fmt_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 Object** argv[1] = { data.location() };
153
154 bool caught_exception;
155 Handle<Object> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000156 Execution::TryCall(fun,
157 Isolate::Current()->js_builtins_object(), 1, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158
159 if (caught_exception || !result->IsString()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000160 return FACTORY->LookupAsciiSymbol("<error>");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 }
162 Handle<String> result_string = Handle<String>::cast(result);
163 // A string that has been obtained from JS code in this way is
164 // likely to be a complicated ConsString of some sort. We flatten it
165 // here to improve the efficiency of converting it to a C string and
166 // other operations that are likely to take place (see GetLocalizedMessage
167 // for example).
168 FlattenString(result_string);
169 return result_string;
170}
171
172
173SmartPointer<char> MessageHandler::GetLocalizedMessage(Handle<Object> data) {
174 HandleScope scope;
175 return GetMessage(data)->ToCString(DISALLOW_NULLS);
176}
177
178
179} } // namespace v8::internal