blob: a0793c2dfd7376dd3a066c9e320f31e2894c2272 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "execution.h"
Steve Block6ded16b2010-05-10 14:33:55 +010032#include "messages.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033#include "spaces-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35namespace v8 {
36namespace internal {
37
38
39// If no message listeners have been registered this one is called
40// by default.
41void MessageHandler::DefaultMessageReport(const MessageLocation* loc,
42 Handle<Object> message_obj) {
Ben Murdoch589d6972011-11-30 16:04:58 +000043 SmartArrayPointer<char> str = GetLocalizedMessage(message_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000044 if (loc == NULL) {
45 PrintF("%s\n", *str);
46 } else {
47 HandleScope scope;
48 Handle<Object> data(loc->script()->name());
Ben Murdoch589d6972011-11-30 16:04:58 +000049 SmartArrayPointer<char> data_str;
Steve Blocka7e24c12009-10-30 11:49:00 +000050 if (data->IsString())
51 data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
52 PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>",
53 loc->start_pos(), *str);
54 }
55}
56
57
Steve Block1e0659c2011-05-24 12:43:12 +010058Handle<JSMessageObject> MessageHandler::MakeMessageObject(
Steve Blocka7e24c12009-10-30 11:49:00 +000059 const char* type,
60 MessageLocation* loc,
61 Vector< Handle<Object> > args,
Ben Murdoch3bec4d22010-07-22 14:51:16 +010062 Handle<String> stack_trace,
63 Handle<JSArray> stack_frames) {
Steve Block44f0eee2011-05-26 01:26:41 +010064 Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type);
Steve Block1e0659c2011-05-24 12:43:12 +010065 Handle<FixedArray> arguments_elements =
Steve Block44f0eee2011-05-26 01:26:41 +010066 FACTORY->NewFixedArray(args.length());
Steve Block1e0659c2011-05-24 12:43:12 +010067 for (int i = 0; i < args.length(); i++) {
68 arguments_elements->set(i, *args[i]);
69 }
70 Handle<JSArray> arguments_handle =
Steve Block44f0eee2011-05-26 01:26:41 +010071 FACTORY->NewJSArrayWithElements(arguments_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +000072
Steve Block1e0659c2011-05-24 12:43:12 +010073 int start = 0;
74 int end = 0;
Steve Block44f0eee2011-05-26 01:26:41 +010075 Handle<Object> script_handle = FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000076 if (loc) {
77 start = loc->start_pos();
78 end = loc->end_pos();
Steve Block1e0659c2011-05-24 12:43:12 +010079 script_handle = GetScriptWrapper(loc->script());
Steve Blocka7e24c12009-10-30 11:49:00 +000080 }
Steve Blocka7e24c12009-10-30 11:49:00 +000081
Steve Block1e0659c2011-05-24 12:43:12 +010082 Handle<Object> stack_trace_handle = stack_trace.is_null()
Ben Murdoch3ef787d2012-04-12 10:51:47 +010083 ? Handle<Object>::cast(FACTORY->undefined_value())
Steve Block1e0659c2011-05-24 12:43:12 +010084 : Handle<Object>::cast(stack_trace);
Steve Blocka7e24c12009-10-30 11:49:00 +000085
Steve Block1e0659c2011-05-24 12:43:12 +010086 Handle<Object> stack_frames_handle = stack_frames.is_null()
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087 ? Handle<Object>::cast(FACTORY->undefined_value())
Steve Block1e0659c2011-05-24 12:43:12 +010088 : Handle<Object>::cast(stack_frames);
Steve Blocka7e24c12009-10-30 11:49:00 +000089
Steve Block1e0659c2011-05-24 12:43:12 +010090 Handle<JSMessageObject> message =
Steve Block44f0eee2011-05-26 01:26:41 +010091 FACTORY->NewJSMessageObject(type_handle,
Steve Block1e0659c2011-05-24 12:43:12 +010092 arguments_handle,
93 start,
94 end,
95 script_handle,
96 stack_trace_handle,
97 stack_frames_handle);
Steve Blocka7e24c12009-10-30 11:49:00 +000098
Steve Block1e0659c2011-05-24 12:43:12 +010099 return message;
Steve Blocka7e24c12009-10-30 11:49:00 +0000100}
101
102
Ben Murdoch8b112d22011-06-08 16:22:53 +0100103void MessageHandler::ReportMessage(Isolate* isolate,
104 MessageLocation* loc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 Handle<Object> message) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100106 // We are calling into embedder's code which can throw exceptions.
107 // Thus we need to save current exception state, reset it to the clean one
108 // and ignore scheduled exceptions callbacks can throw.
109 Isolate::ExceptionScope exception_scope(isolate);
110 isolate->clear_pending_exception();
111 isolate->set_external_caught_exception(false);
112
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
114
Steve Block44f0eee2011-05-26 01:26:41 +0100115 v8::NeanderArray global_listeners(FACTORY->message_listeners());
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 int global_length = global_listeners.length();
117 if (global_length == 0) {
118 DefaultMessageReport(loc, message);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100119 if (isolate->has_scheduled_exception()) {
120 isolate->clear_scheduled_exception();
121 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 } else {
123 for (int i = 0; i < global_length; i++) {
124 HandleScope scope;
125 if (global_listeners.get(i)->IsUndefined()) continue;
126 v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000127 Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 v8::MessageCallback callback =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129 FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 Handle<Object> callback_data(listener.get(1));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100131 {
132 // Do not allow exceptions to propagate.
Ben Murdoch257744e2011-11-30 15:57:28 +0000133 v8::TryCatch try_catch;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100134 callback(api_message_obj, v8::Utils::ToLocal(callback_data));
135 }
136 if (isolate->has_scheduled_exception()) {
137 isolate->clear_scheduled_exception();
138 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 }
140 }
141}
142
143
144Handle<String> MessageHandler::GetMessage(Handle<Object> data) {
Steve Block44f0eee2011-05-26 01:26:41 +0100145 Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage");
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100147 Handle<JSFunction>(
148 JSFunction::cast(
149 Isolate::Current()->js_builtins_object()->
150 GetPropertyNoExceptionThrown(*fmt_str)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100151 Handle<Object> argv[] = { data };
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153 bool caught_exception;
154 Handle<Object> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100155 Execution::TryCall(fun,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100156 Isolate::Current()->js_builtins_object(),
157 ARRAY_SIZE(argv),
158 argv,
159 &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000160
161 if (caught_exception || !result->IsString()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100162 return FACTORY->LookupAsciiSymbol("<error>");
Steve Blocka7e24c12009-10-30 11:49:00 +0000163 }
164 Handle<String> result_string = Handle<String>::cast(result);
165 // A string that has been obtained from JS code in this way is
166 // likely to be a complicated ConsString of some sort. We flatten it
167 // here to improve the efficiency of converting it to a C string and
168 // other operations that are likely to take place (see GetLocalizedMessage
169 // for example).
170 FlattenString(result_string);
171 return result_string;
172}
173
174
Ben Murdoch589d6972011-11-30 16:04:58 +0000175SmartArrayPointer<char> MessageHandler::GetLocalizedMessage(
176 Handle<Object> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 HandleScope scope;
178 return GetMessage(data)->ToCString(DISALLOW_NULLS);
179}
180
181
182} } // namespace v8::internal