christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 33 | #include "messages.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "spaces-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | // If no message listeners have been registered this one is called |
| 41 | // by default. |
| 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()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 50 | SmartPointer<char> data_str; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 59 | Handle<JSMessageObject> MessageHandler::MakeMessageObject( |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 60 | const char* type, |
| 61 | MessageLocation* loc, |
| 62 | Vector< Handle<Object> > args, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 63 | Handle<String> stack_trace, |
| 64 | Handle<JSArray> stack_frames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 65 | Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 66 | Handle<FixedArray> arguments_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | FACTORY->NewFixedArray(args.length()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 68 | for (int i = 0; i < args.length(); i++) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 69 | arguments_elements->set(i, *args[i]); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 70 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 71 | Handle<JSArray> arguments_handle = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 72 | FACTORY->NewJSArrayWithElements(arguments_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 74 | int start = 0; |
| 75 | int end = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 76 | Handle<Object> script_handle = FACTORY->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | if (loc) { |
| 78 | start = loc->start_pos(); |
| 79 | end = loc->end_pos(); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 80 | script_handle = GetScriptWrapper(loc->script()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 83 | Handle<Object> stack_trace_handle = stack_trace.is_null() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 84 | ? FACTORY->undefined_value() |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 85 | : Handle<Object>::cast(stack_trace); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 86 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 87 | Handle<Object> stack_frames_handle = stack_frames.is_null() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 88 | ? FACTORY->undefined_value() |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 89 | : Handle<Object>::cast(stack_frames); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 90 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 91 | Handle<JSMessageObject> message = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 92 | FACTORY->NewJSMessageObject(type_handle, |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 93 | arguments_handle, |
| 94 | start, |
| 95 | end, |
| 96 | script_handle, |
| 97 | stack_trace_handle, |
| 98 | stack_frames_handle); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 99 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 100 | return message; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 101 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 103 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 104 | void MessageHandler::ReportMessage(Isolate* isolate, |
| 105 | MessageLocation* loc, |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 106 | Handle<Object> message) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
| 115 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 116 | v8::NeanderArray global_listeners(FACTORY->message_listeners()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | int global_length = global_listeners.length(); |
| 118 | if (global_length == 0) { |
| 119 | DefaultMessageReport(loc, message); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 120 | if (isolate->has_scheduled_exception()) { |
| 121 | isolate->clear_scheduled_exception(); |
| 122 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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))); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 128 | Handle<Foreign> callback_obj(Foreign::cast(listener.get(0))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | v8::MessageCallback callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 130 | FUNCTION_CAST<v8::MessageCallback>(callback_obj->address()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | Handle<Object> callback_data(listener.get(1)); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 132 | { |
| 133 | // Do not allow exceptions to propagate. |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 134 | v8::TryCatch try_catch; |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 135 | callback(api_message_obj, v8::Utils::ToLocal(callback_data)); |
| 136 | } |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 137 | if (isolate->has_scheduled_exception()) { |
| 138 | isolate->clear_scheduled_exception(); |
| 139 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | Handle<String> MessageHandler::GetMessage(Handle<Object> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 146 | Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | Handle<JSFunction> fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 148 | Handle<JSFunction>( |
| 149 | JSFunction::cast( |
| 150 | Isolate::Current()->js_builtins_object()-> |
| 151 | GetPropertyNoExceptionThrown(*fmt_str))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | Object** argv[1] = { data.location() }; |
| 153 | |
| 154 | bool caught_exception; |
| 155 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 156 | Execution::TryCall(fun, |
| 157 | Isolate::Current()->js_builtins_object(), 1, argv, &caught_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | |
| 159 | if (caught_exception || !result->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 160 | return FACTORY->LookupAsciiSymbol("<error>"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
| 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 |