Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | #include "messages.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "spaces-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 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()); |
| 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 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 59 | Handle<JSMessageObject> MessageHandler::MakeMessageObject( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | const char* type, |
| 61 | MessageLocation* loc, |
| 62 | Vector< Handle<Object> > args, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 63 | Handle<String> stack_trace, |
| 64 | Handle<JSArray> stack_frames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 65 | Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 66 | Handle<FixedArray> arguments_elements = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | FACTORY->NewFixedArray(args.length()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 68 | for (int i = 0; i < args.length(); i++) { |
| 69 | arguments_elements->set(i, *args[i]); |
| 70 | } |
| 71 | Handle<JSArray> arguments_handle = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 72 | FACTORY->NewJSArrayWithElements(arguments_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 74 | int start = 0; |
| 75 | int end = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 76 | Handle<Object> script_handle = FACTORY->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | if (loc) { |
| 78 | start = loc->start_pos(); |
| 79 | end = loc->end_pos(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 80 | script_handle = GetScriptWrapper(loc->script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 83 | Handle<Object> stack_trace_handle = stack_trace.is_null() |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 84 | ? FACTORY->undefined_value() |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 85 | : Handle<Object>::cast(stack_trace); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 87 | Handle<Object> stack_frames_handle = stack_frames.is_null() |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 88 | ? FACTORY->undefined_value() |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 89 | : Handle<Object>::cast(stack_frames); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 91 | Handle<JSMessageObject> message = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 92 | FACTORY->NewJSMessageObject(type_handle, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 93 | arguments_handle, |
| 94 | start, |
| 95 | end, |
| 96 | script_handle, |
| 97 | stack_trace_handle, |
| 98 | stack_frames_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 100 | return message; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 104 | void MessageHandler::ReportMessage(Isolate* isolate, |
| 105 | MessageLocation* loc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | Handle<Object> message) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [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 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
| 115 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 116 | v8::NeanderArray global_listeners(FACTORY->message_listeners()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | int global_length = global_listeners.length(); |
| 118 | if (global_length == 0) { |
| 119 | DefaultMessageReport(loc, message); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 120 | if (isolate->has_scheduled_exception()) { |
| 121 | isolate->clear_scheduled_exception(); |
| 122 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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))); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 128 | Handle<Foreign> callback_obj(Foreign::cast(listener.get(0))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | v8::MessageCallback callback = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 130 | FUNCTION_CAST<v8::MessageCallback>(callback_obj->address()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | Handle<Object> callback_data(listener.get(1)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 132 | { |
| 133 | // Do not allow exceptions to propagate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 134 | v8::TryCatch try_catch; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 135 | callback(api_message_obj, v8::Utils::ToLocal(callback_data)); |
| 136 | } |
| 137 | if (isolate->has_scheduled_exception()) { |
| 138 | isolate->clear_scheduled_exception(); |
| 139 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | Handle<String> MessageHandler::GetMessage(Handle<Object> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 146 | Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | Handle<JSFunction> fun = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 148 | Handle<JSFunction>( |
| 149 | JSFunction::cast( |
| 150 | Isolate::Current()->js_builtins_object()-> |
| 151 | GetPropertyNoExceptionThrown(*fmt_str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | Object** argv[1] = { data.location() }; |
| 153 | |
| 154 | bool caught_exception; |
| 155 | Handle<Object> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 156 | Execution::TryCall(fun, |
| 157 | Isolate::Current()->js_builtins_object(), 1, argv, &caught_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | |
| 159 | if (caught_exception || !result->IsString()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | return FACTORY->LookupAsciiSymbol("<error>"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 |