blob: 4cbf0af74711adfcf54f5636524aca5f718c087a [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001
2// Copyright 2006-2008 the V8 project authors. All rights reserved.
3// 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"
Steve Block6ded16b2010-05-10 14:33:55 +010033#include "messages.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "spaces-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36namespace v8 {
37namespace internal {
38
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());
50 SmartPointer<char> data_str;
51 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
Steve Block1e0659c2011-05-24 12:43:12 +010059Handle<JSMessageObject> MessageHandler::MakeMessageObject(
Steve Blocka7e24c12009-10-30 11:49:00 +000060 const char* type,
61 MessageLocation* loc,
62 Vector< Handle<Object> > args,
Ben Murdoch3bec4d22010-07-22 14:51:16 +010063 Handle<String> stack_trace,
64 Handle<JSArray> stack_frames) {
Steve Block44f0eee2011-05-26 01:26:41 +010065 Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type);
Steve Block1e0659c2011-05-24 12:43:12 +010066 Handle<FixedArray> arguments_elements =
Steve Block44f0eee2011-05-26 01:26:41 +010067 FACTORY->NewFixedArray(args.length());
Steve Block1e0659c2011-05-24 12:43:12 +010068 for (int i = 0; i < args.length(); i++) {
69 arguments_elements->set(i, *args[i]);
70 }
71 Handle<JSArray> arguments_handle =
Steve Block44f0eee2011-05-26 01:26:41 +010072 FACTORY->NewJSArrayWithElements(arguments_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +000073
Steve Block1e0659c2011-05-24 12:43:12 +010074 int start = 0;
75 int end = 0;
Steve Block44f0eee2011-05-26 01:26:41 +010076 Handle<Object> script_handle = FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000077 if (loc) {
78 start = loc->start_pos();
79 end = loc->end_pos();
Steve Block1e0659c2011-05-24 12:43:12 +010080 script_handle = GetScriptWrapper(loc->script());
Steve Blocka7e24c12009-10-30 11:49:00 +000081 }
Steve Blocka7e24c12009-10-30 11:49:00 +000082
Steve Block1e0659c2011-05-24 12:43:12 +010083 Handle<Object> stack_trace_handle = stack_trace.is_null()
Steve Block44f0eee2011-05-26 01:26:41 +010084 ? FACTORY->undefined_value()
Steve Block1e0659c2011-05-24 12:43:12 +010085 : Handle<Object>::cast(stack_trace);
Steve Blocka7e24c12009-10-30 11:49:00 +000086
Steve Block1e0659c2011-05-24 12:43:12 +010087 Handle<Object> stack_frames_handle = stack_frames.is_null()
Steve Block44f0eee2011-05-26 01:26:41 +010088 ? FACTORY->undefined_value()
Steve Block1e0659c2011-05-24 12:43:12 +010089 : Handle<Object>::cast(stack_frames);
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Steve Block1e0659c2011-05-24 12:43:12 +010091 Handle<JSMessageObject> message =
Steve Block44f0eee2011-05-26 01:26:41 +010092 FACTORY->NewJSMessageObject(type_handle,
Steve Block1e0659c2011-05-24 12:43:12 +010093 arguments_handle,
94 start,
95 end,
96 script_handle,
97 stack_trace_handle,
98 stack_frames_handle);
Steve Blocka7e24c12009-10-30 11:49:00 +000099
Steve Block1e0659c2011-05-24 12:43:12 +0100100 return message;
Steve Blocka7e24c12009-10-30 11:49:00 +0000101}
102
103
Ben Murdoch8b112d22011-06-08 16:22:53 +0100104void MessageHandler::ReportMessage(Isolate* isolate,
105 MessageLocation* loc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 Handle<Object> message) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100107 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
115
Steve Block44f0eee2011-05-26 01:26:41 +0100116 v8::NeanderArray global_listeners(FACTORY->message_listeners());
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 int global_length = global_listeners.length();
118 if (global_length == 0) {
119 DefaultMessageReport(loc, message);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100120 if (isolate->has_scheduled_exception()) {
121 isolate->clear_scheduled_exception();
122 }
Steve Blocka7e24c12009-10-30 11:49:00 +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)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000128 Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 v8::MessageCallback callback =
Ben Murdoch257744e2011-11-30 15:57:28 +0000130 FUNCTION_CAST<v8::MessageCallback>(callback_obj->address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 Handle<Object> callback_data(listener.get(1));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100132 {
133 // Do not allow exceptions to propagate.
Ben Murdoch257744e2011-11-30 15:57:28 +0000134 v8::TryCatch try_catch;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100135 callback(api_message_obj, v8::Utils::ToLocal(callback_data));
136 }
137 if (isolate->has_scheduled_exception()) {
138 isolate->clear_scheduled_exception();
139 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 }
141 }
142}
143
144
145Handle<String> MessageHandler::GetMessage(Handle<Object> data) {
Steve Block44f0eee2011-05-26 01:26:41 +0100146 Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage");
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100148 Handle<JSFunction>(
149 JSFunction::cast(
150 Isolate::Current()->js_builtins_object()->
151 GetPropertyNoExceptionThrown(*fmt_str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 Object** argv[1] = { data.location() };
153
154 bool caught_exception;
155 Handle<Object> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100156 Execution::TryCall(fun,
157 Isolate::Current()->js_builtins_object(), 1, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
159 if (caught_exception || !result->IsString()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100160 return FACTORY->LookupAsciiSymbol("<error>");
Steve Blocka7e24c12009-10-30 11:49:00 +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