Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / v8 / src / messages.cc
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
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"
32 #include "messages.h"
33 #include "spaces-inl.h"
34
35 namespace v8 {
36 namespace internal {
37
38
39 // If no message listeners have been registered this one is called
40 // by default.
41 void MessageHandler::DefaultMessageReport(Isolate* isolate,
42                                           const MessageLocation* loc,
43                                           Handle<Object> message_obj) {
44   SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
45   if (loc == NULL) {
46     PrintF("%s\n", str.get());
47   } else {
48     HandleScope scope(isolate);
49     Handle<Object> data(loc->script()->name(), isolate);
50     SmartArrayPointer<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.get() ? data_str.get() : "<unknown>",
54            loc->start_pos(), str.get());
55   }
56 }
57
58
59 Handle<JSMessageObject> MessageHandler::MakeMessageObject(
60     Isolate* isolate,
61     const char* type,
62     MessageLocation* loc,
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]);
72   }
73   Handle<JSArray> arguments_handle =
74       factory->NewJSArrayWithElements(arguments_elements);
75
76   int start = 0;
77   int end = 0;
78   Handle<Object> script_handle = factory->undefined_value();
79   if (loc) {
80     start = loc->start_pos();
81     end = loc->end_pos();
82     script_handle = GetScriptWrapper(loc->script());
83   }
84
85   Handle<Object> stack_trace_handle = stack_trace.is_null()
86       ? Handle<Object>::cast(factory->undefined_value())
87       : Handle<Object>::cast(stack_trace);
88
89   Handle<Object> stack_frames_handle = stack_frames.is_null()
90       ? Handle<Object>::cast(factory->undefined_value())
91       : Handle<Object>::cast(stack_frames);
92
93   Handle<JSMessageObject> message =
94       factory->NewJSMessageObject(type_handle,
95                                   arguments_handle,
96                                   start,
97                                   end,
98                                   script_handle,
99                                   stack_trace_handle,
100                                   stack_frames_handle);
101
102   return message;
103 }
104
105
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.
112
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);
117   }
118   Handle<Object> exception_handle(exception_object, isolate);
119
120   Isolate::ExceptionScope exception_scope(isolate);
121   isolate->clear_pending_exception();
122   isolate->set_external_caught_exception(false);
123
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);
126
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();
133     }
134   } else {
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);
143       {
144         // Do not allow exceptions to propagate.
145         v8::TryCatch try_catch;
146         callback(api_message_obj, callback_data->IsUndefined()
147                                       ? api_exception_obj
148                                       : v8::Utils::ToLocal(callback_data));
149       }
150       if (isolate->has_scheduled_exception()) {
151         isolate->clear_scheduled_exception();
152       }
153     }
154   }
155 }
156
157
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 =
164       Handle<JSFunction>(
165           JSFunction::cast(
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) };
171
172   bool caught_exception;
173   Handle<Object> result =
174       Execution::TryCall(fun,
175                          isolate->js_builtins_object(),
176                          ARRAY_SIZE(argv),
177                          argv,
178                          &caught_exception);
179
180   if (caught_exception || !result->IsString()) {
181     return factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("<error>"));
182   }
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
188   // for example).
189   FlattenString(result_string);
190   return result_string;
191 }
192
193
194 SmartArrayPointer<char> MessageHandler::GetLocalizedMessage(
195     Isolate* isolate,
196     Handle<Object> data) {
197   HandleScope scope(isolate);
198   return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
199 }
200
201
202 } }  // namespace v8::internal