1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
31 #include "execution.h"
33 #include "spaces-inl.h"
39 // If no message listeners have been registered this one is called
41 void MessageHandler::DefaultMessageReport(Isolate* isolate,
42 const MessageLocation* loc,
43 Handle<Object> message_obj) {
44 SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
46 PrintF("%s\n", str.get());
48 HandleScope scope(isolate);
49 Handle<Object> data(loc->script()->name(), isolate);
50 SmartArrayPointer<char> data_str;
52 data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
53 PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
54 loc->start_pos(), str.get());
59 Handle<JSMessageObject> MessageHandler::MakeMessageObject(
63 Vector< Handle<Object> > args,
64 Handle<String> stack_trace,
65 Handle<JSArray> stack_frames) {
66 Factory* factory = isolate->factory();
67 Handle<String> type_handle = factory->InternalizeUtf8String(type);
68 Handle<FixedArray> arguments_elements =
69 factory->NewFixedArray(args.length());
70 for (int i = 0; i < args.length(); i++) {
71 arguments_elements->set(i, *args[i]);
73 Handle<JSArray> arguments_handle =
74 factory->NewJSArrayWithElements(arguments_elements);
78 Handle<Object> script_handle = factory->undefined_value();
80 start = loc->start_pos();
82 script_handle = GetScriptWrapper(loc->script());
85 Handle<Object> stack_trace_handle = stack_trace.is_null()
86 ? Handle<Object>::cast(factory->undefined_value())
87 : Handle<Object>::cast(stack_trace);
89 Handle<Object> stack_frames_handle = stack_frames.is_null()
90 ? Handle<Object>::cast(factory->undefined_value())
91 : Handle<Object>::cast(stack_frames);
93 Handle<JSMessageObject> message =
94 factory->NewJSMessageObject(type_handle,
100 stack_frames_handle);
106 void MessageHandler::ReportMessage(Isolate* isolate,
107 MessageLocation* loc,
108 Handle<Object> message) {
109 // We are calling into embedder's code which can throw exceptions.
110 // Thus we need to save current exception state, reset it to the clean one
111 // and ignore scheduled exceptions callbacks can throw.
113 // We pass the exception object into the message handler callback though.
114 Object* exception_object = isolate->heap()->undefined_value();
115 if (isolate->has_pending_exception()) {
116 isolate->pending_exception()->ToObject(&exception_object);
118 Handle<Object> exception_handle(exception_object, isolate);
120 Isolate::ExceptionScope exception_scope(isolate);
121 isolate->clear_pending_exception();
122 isolate->set_external_caught_exception(false);
124 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
125 v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle);
127 v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
128 int global_length = global_listeners.length();
129 if (global_length == 0) {
130 DefaultMessageReport(isolate, loc, message);
131 if (isolate->has_scheduled_exception()) {
132 isolate->clear_scheduled_exception();
135 for (int i = 0; i < global_length; i++) {
136 HandleScope scope(isolate);
137 if (global_listeners.get(i)->IsUndefined()) continue;
138 v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
139 Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
140 v8::MessageCallback callback =
141 FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
142 Handle<Object> callback_data(listener.get(1), isolate);
144 // Do not allow exceptions to propagate.
145 v8::TryCatch try_catch;
146 callback(api_message_obj, callback_data->IsUndefined()
148 : v8::Utils::ToLocal(callback_data));
150 if (isolate->has_scheduled_exception()) {
151 isolate->clear_scheduled_exception();
158 Handle<String> MessageHandler::GetMessage(Isolate* isolate,
159 Handle<Object> data) {
160 Factory* factory = isolate->factory();
161 Handle<String> fmt_str =
162 factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("FormatMessage"));
163 Handle<JSFunction> fun =
166 isolate->js_builtins_object()->
167 GetPropertyNoExceptionThrown(*fmt_str)));
168 Handle<JSMessageObject> message = Handle<JSMessageObject>::cast(data);
169 Handle<Object> argv[] = { Handle<Object>(message->type(), isolate),
170 Handle<Object>(message->arguments(), isolate) };
172 bool caught_exception;
173 Handle<Object> result =
174 Execution::TryCall(fun,
175 isolate->js_builtins_object(),
180 if (caught_exception || !result->IsString()) {
181 return factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("<error>"));
183 Handle<String> result_string = Handle<String>::cast(result);
184 // A string that has been obtained from JS code in this way is
185 // likely to be a complicated ConsString of some sort. We flatten it
186 // here to improve the efficiency of converting it to a C string and
187 // other operations that are likely to take place (see GetLocalizedMessage
189 FlattenString(result_string);
190 return result_string;
194 SmartArrayPointer<char> MessageHandler::GetLocalizedMessage(
196 Handle<Object> data) {
197 HandleScope scope(isolate);
198 return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
202 } } // namespace v8::internal