1
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"
33 #include "messages.h"
34 #include "spaces-inl.h"
35
36 namespace v8 {
37 namespace internal {
38
39
40 // If no message listeners have been registered this one is called
41 // by default.
DefaultMessageReport(const MessageLocation * loc,Handle<Object> message_obj)42 void 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
MakeMessageObject(const char * type,MessageLocation * loc,Vector<Handle<Object>> args,Handle<String> stack_trace,Handle<JSArray> stack_frames)59 Handle<JSMessageObject> MessageHandler::MakeMessageObject(
60 const char* type,
61 MessageLocation* loc,
62 Vector< Handle<Object> > args,
63 Handle<String> stack_trace,
64 Handle<JSArray> stack_frames) {
65 Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type);
66 Handle<FixedArray> arguments_elements =
67 FACTORY->NewFixedArray(args.length());
68 for (int i = 0; i < args.length(); i++) {
69 arguments_elements->set(i, *args[i]);
70 }
71 Handle<JSArray> arguments_handle =
72 FACTORY->NewJSArrayWithElements(arguments_elements);
73
74 int start = 0;
75 int end = 0;
76 Handle<Object> script_handle = FACTORY->undefined_value();
77 if (loc) {
78 start = loc->start_pos();
79 end = loc->end_pos();
80 script_handle = GetScriptWrapper(loc->script());
81 }
82
83 Handle<Object> stack_trace_handle = stack_trace.is_null()
84 ? FACTORY->undefined_value()
85 : Handle<Object>::cast(stack_trace);
86
87 Handle<Object> stack_frames_handle = stack_frames.is_null()
88 ? FACTORY->undefined_value()
89 : Handle<Object>::cast(stack_frames);
90
91 Handle<JSMessageObject> message =
92 FACTORY->NewJSMessageObject(type_handle,
93 arguments_handle,
94 start,
95 end,
96 script_handle,
97 stack_trace_handle,
98 stack_frames_handle);
99
100 return message;
101 }
102
103
ReportMessage(Isolate * isolate,MessageLocation * loc,Handle<Object> message)104 void MessageHandler::ReportMessage(Isolate* isolate,
105 MessageLocation* loc,
106 Handle<Object> message) {
107 // 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
114 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
115
116 v8::NeanderArray global_listeners(FACTORY->message_listeners());
117 int global_length = global_listeners.length();
118 if (global_length == 0) {
119 DefaultMessageReport(loc, message);
120 if (isolate->has_scheduled_exception()) {
121 isolate->clear_scheduled_exception();
122 }
123 } 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)));
128 Handle<Proxy> callback_obj(Proxy::cast(listener.get(0)));
129 v8::MessageCallback callback =
130 FUNCTION_CAST<v8::MessageCallback>(callback_obj->proxy());
131 Handle<Object> callback_data(listener.get(1));
132 {
133 // Do not allow exceptions to propagate.
134 v8::TryCatch tryCatch;
135 callback(api_message_obj, v8::Utils::ToLocal(callback_data));
136 }
137 if (isolate->has_scheduled_exception()) {
138 isolate->clear_scheduled_exception();
139 }
140 }
141 }
142 }
143
144
GetMessage(Handle<Object> data)145 Handle<String> MessageHandler::GetMessage(Handle<Object> data) {
146 Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage");
147 Handle<JSFunction> fun =
148 Handle<JSFunction>(
149 JSFunction::cast(
150 Isolate::Current()->js_builtins_object()->
151 GetPropertyNoExceptionThrown(*fmt_str)));
152 Object** argv[1] = { data.location() };
153
154 bool caught_exception;
155 Handle<Object> result =
156 Execution::TryCall(fun,
157 Isolate::Current()->js_builtins_object(), 1, argv, &caught_exception);
158
159 if (caught_exception || !result->IsString()) {
160 return FACTORY->LookupAsciiSymbol("<error>");
161 }
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
GetLocalizedMessage(Handle<Object> data)173 SmartPointer<char> MessageHandler::GetLocalizedMessage(Handle<Object> data) {
174 HandleScope scope;
175 return GetMessage(data)->ToCString(DISALLOW_NULLS);
176 }
177
178
179 } } // namespace v8::internal
180