Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 32 | #include "messages.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "spaces-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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(const MessageLocation* loc, |
| 42 | Handle<Object> message_obj) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 43 | SmartArrayPointer<char> str = GetLocalizedMessage(message_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | if (loc == NULL) { |
| 45 | PrintF("%s\n", *str); |
| 46 | } else { |
| 47 | HandleScope scope; |
| 48 | Handle<Object> data(loc->script()->name()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 49 | SmartArrayPointer<char> data_str; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | if (data->IsString()) |
| 51 | data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS); |
| 52 | PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>", |
| 53 | loc->start_pos(), *str); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 58 | Handle<JSMessageObject> MessageHandler::MakeMessageObject( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | const char* type, |
| 60 | MessageLocation* loc, |
| 61 | Vector< Handle<Object> > args, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 62 | Handle<String> stack_trace, |
| 63 | Handle<JSArray> stack_frames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 64 | Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 65 | Handle<FixedArray> arguments_elements = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 66 | FACTORY->NewFixedArray(args.length()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 67 | for (int i = 0; i < args.length(); i++) { |
| 68 | arguments_elements->set(i, *args[i]); |
| 69 | } |
| 70 | Handle<JSArray> arguments_handle = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | FACTORY->NewJSArrayWithElements(arguments_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 73 | int start = 0; |
| 74 | int end = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 75 | Handle<Object> script_handle = FACTORY->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | if (loc) { |
| 77 | start = loc->start_pos(); |
| 78 | end = loc->end_pos(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 79 | script_handle = GetScriptWrapper(loc->script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 82 | Handle<Object> stack_trace_handle = stack_trace.is_null() |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 83 | ? Handle<Object>::cast(FACTORY->undefined_value()) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 84 | : Handle<Object>::cast(stack_trace); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 86 | Handle<Object> stack_frames_handle = stack_frames.is_null() |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 87 | ? Handle<Object>::cast(FACTORY->undefined_value()) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 88 | : Handle<Object>::cast(stack_frames); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 90 | Handle<JSMessageObject> message = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | FACTORY->NewJSMessageObject(type_handle, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 92 | arguments_handle, |
| 93 | start, |
| 94 | end, |
| 95 | script_handle, |
| 96 | stack_trace_handle, |
| 97 | stack_frames_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 99 | return message; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 103 | void MessageHandler::ReportMessage(Isolate* isolate, |
| 104 | MessageLocation* loc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | Handle<Object> message) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 106 | // We are calling into embedder's code which can throw exceptions. |
| 107 | // Thus we need to save current exception state, reset it to the clean one |
| 108 | // and ignore scheduled exceptions callbacks can throw. |
| 109 | Isolate::ExceptionScope exception_scope(isolate); |
| 110 | isolate->clear_pending_exception(); |
| 111 | isolate->set_external_caught_exception(false); |
| 112 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
| 114 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | v8::NeanderArray global_listeners(FACTORY->message_listeners()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | int global_length = global_listeners.length(); |
| 117 | if (global_length == 0) { |
| 118 | DefaultMessageReport(loc, message); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 119 | if (isolate->has_scheduled_exception()) { |
| 120 | isolate->clear_scheduled_exception(); |
| 121 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | } else { |
| 123 | for (int i = 0; i < global_length; i++) { |
| 124 | HandleScope scope; |
| 125 | if (global_listeners.get(i)->IsUndefined()) continue; |
| 126 | v8::NeanderObject listener(JSObject::cast(global_listeners.get(i))); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 127 | Handle<Foreign> callback_obj(Foreign::cast(listener.get(0))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | v8::MessageCallback callback = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | Handle<Object> callback_data(listener.get(1)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 131 | { |
| 132 | // Do not allow exceptions to propagate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 133 | v8::TryCatch try_catch; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 134 | callback(api_message_obj, v8::Utils::ToLocal(callback_data)); |
| 135 | } |
| 136 | if (isolate->has_scheduled_exception()) { |
| 137 | isolate->clear_scheduled_exception(); |
| 138 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | Handle<String> MessageHandler::GetMessage(Handle<Object> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 145 | Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | Handle<JSFunction> fun = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | Handle<JSFunction>( |
| 148 | JSFunction::cast( |
| 149 | Isolate::Current()->js_builtins_object()-> |
| 150 | GetPropertyNoExceptionThrown(*fmt_str))); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 151 | Handle<Object> argv[] = { data }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | |
| 153 | bool caught_exception; |
| 154 | Handle<Object> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 155 | Execution::TryCall(fun, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 156 | Isolate::Current()->js_builtins_object(), |
| 157 | ARRAY_SIZE(argv), |
| 158 | argv, |
| 159 | &caught_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | |
| 161 | if (caught_exception || !result->IsString()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 162 | return FACTORY->LookupAsciiSymbol("<error>"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | } |
| 164 | Handle<String> result_string = Handle<String>::cast(result); |
| 165 | // A string that has been obtained from JS code in this way is |
| 166 | // likely to be a complicated ConsString of some sort. We flatten it |
| 167 | // here to improve the efficiency of converting it to a C string and |
| 168 | // other operations that are likely to take place (see GetLocalizedMessage |
| 169 | // for example). |
| 170 | FlattenString(result_string); |
| 171 | return result_string; |
| 172 | } |
| 173 | |
| 174 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 175 | SmartArrayPointer<char> MessageHandler::GetLocalizedMessage( |
| 176 | Handle<Object> data) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | HandleScope scope; |
| 178 | return GetMessage(data)->ToCString(DISALLOW_NULLS); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | } } // namespace v8::internal |