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" |
| 35 | #include "top.h" |
| 36 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | // If no message listeners have been registered this one is called |
| 42 | // by default. |
| 43 | void MessageHandler::DefaultMessageReport(const MessageLocation* loc, |
| 44 | Handle<Object> message_obj) { |
| 45 | SmartPointer<char> str = GetLocalizedMessage(message_obj); |
| 46 | if (loc == NULL) { |
| 47 | PrintF("%s\n", *str); |
| 48 | } else { |
| 49 | HandleScope scope; |
| 50 | Handle<Object> data(loc->script()->name()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 51 | SmartPointer<char> data_str; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | if (data->IsString()) |
| 53 | data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS); |
| 54 | PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>", |
| 55 | loc->start_pos(), *str); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void MessageHandler::ReportMessage(const char* msg) { |
| 61 | PrintF("%s\n", msg); |
| 62 | } |
| 63 | |
| 64 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 65 | Handle<Object> MessageHandler::MakeMessageObject( |
| 66 | const char* type, |
| 67 | MessageLocation* loc, |
| 68 | Vector< Handle<Object> > args, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 69 | Handle<String> stack_trace, |
| 70 | Handle<JSArray> stack_frames) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | // Build error message object |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 72 | v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | Handle<Object> type_str = Factory::LookupAsciiSymbol(type); |
| 74 | Handle<Object> array = Factory::NewJSArray(args.length()); |
| 75 | for (int i = 0; i < args.length(); i++) |
| 76 | SetElement(Handle<JSArray>::cast(array), i, args[i]); |
| 77 | |
| 78 | Handle<JSFunction> fun(Top::global_context()->make_message_fun()); |
| 79 | int start, end; |
| 80 | Handle<Object> script; |
| 81 | if (loc) { |
| 82 | start = loc->start_pos(); |
| 83 | end = loc->end_pos(); |
| 84 | script = GetScriptWrapper(loc->script()); |
| 85 | } else { |
| 86 | start = end = 0; |
| 87 | script = Factory::undefined_value(); |
| 88 | } |
| 89 | Handle<Object> start_handle(Smi::FromInt(start)); |
| 90 | Handle<Object> end_handle(Smi::FromInt(end)); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 91 | Handle<Object> stack_trace_val = stack_trace.is_null() |
| 92 | ? Factory::undefined_value() |
| 93 | : Handle<Object>::cast(stack_trace); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 94 | Handle<Object> stack_frames_val = stack_frames.is_null() |
| 95 | ? Factory::undefined_value() |
| 96 | : Handle<Object>::cast(stack_frames); |
| 97 | const int argc = 7; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | Object** argv[argc] = { type_str.location(), |
| 99 | array.location(), |
| 100 | start_handle.location(), |
| 101 | end_handle.location(), |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 102 | script.location(), |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 103 | stack_trace_val.location(), |
| 104 | stack_frames_val.location() }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 106 | // Setup a catch handler to catch exceptions in creating the message. This |
| 107 | // handler is non-verbose to avoid calling MakeMessage recursively in case of |
| 108 | // an exception. |
| 109 | v8::TryCatch catcher; |
| 110 | catcher.SetVerbose(false); |
| 111 | catcher.SetCaptureMessage(false); |
| 112 | |
| 113 | // Format the message. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | bool caught_exception = false; |
| 115 | Handle<Object> message = |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 116 | Execution::Call(fun, Factory::undefined_value(), argc, argv, |
| 117 | &caught_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 118 | |
| 119 | // If creating the message (in JS code) resulted in an exception, we |
| 120 | // skip doing the callback. This usually only happens in case of |
| 121 | // stack overflow exceptions being thrown by the parser when the |
| 122 | // stack is almost full. |
| 123 | if (caught_exception) return Handle<Object>(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 124 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 125 | return message.EscapeFrom(&scope); |
| 126 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 127 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 128 | |
| 129 | void MessageHandler::ReportMessage(MessageLocation* loc, |
| 130 | Handle<Object> message) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
| 132 | |
| 133 | v8::NeanderArray global_listeners(Factory::message_listeners()); |
| 134 | int global_length = global_listeners.length(); |
| 135 | if (global_length == 0) { |
| 136 | DefaultMessageReport(loc, message); |
| 137 | } else { |
| 138 | for (int i = 0; i < global_length; i++) { |
| 139 | HandleScope scope; |
| 140 | if (global_listeners.get(i)->IsUndefined()) continue; |
| 141 | v8::NeanderObject listener(JSObject::cast(global_listeners.get(i))); |
| 142 | Handle<Proxy> callback_obj(Proxy::cast(listener.get(0))); |
| 143 | v8::MessageCallback callback = |
| 144 | FUNCTION_CAST<v8::MessageCallback>(callback_obj->proxy()); |
| 145 | Handle<Object> callback_data(listener.get(1)); |
| 146 | callback(api_message_obj, v8::Utils::ToLocal(callback_data)); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | Handle<String> MessageHandler::GetMessage(Handle<Object> data) { |
| 153 | Handle<String> fmt_str = Factory::LookupAsciiSymbol("FormatMessage"); |
| 154 | Handle<JSFunction> fun = |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 155 | Handle<JSFunction>(JSFunction::cast( |
| 156 | Top::builtins()->GetPropertyNoExceptionThrown(*fmt_str))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | Object** argv[1] = { data.location() }; |
| 158 | |
| 159 | bool caught_exception; |
| 160 | Handle<Object> result = |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 161 | Execution::TryCall(fun, Top::builtins(), 1, argv, &caught_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | |
| 163 | if (caught_exception || !result->IsString()) { |
| 164 | return Factory::LookupAsciiSymbol("<error>"); |
| 165 | } |
| 166 | Handle<String> result_string = Handle<String>::cast(result); |
| 167 | // A string that has been obtained from JS code in this way is |
| 168 | // likely to be a complicated ConsString of some sort. We flatten it |
| 169 | // here to improve the efficiency of converting it to a C string and |
| 170 | // other operations that are likely to take place (see GetLocalizedMessage |
| 171 | // for example). |
| 172 | FlattenString(result_string); |
| 173 | return result_string; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | SmartPointer<char> MessageHandler::GetLocalizedMessage(Handle<Object> data) { |
| 178 | HandleScope scope; |
| 179 | return GetMessage(data)->ToCString(DISALLOW_NULLS); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | } } // namespace v8::internal |