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" |
| 35 | #include "top.h" |
| 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 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()); |
| 51 | SmartPointer<char> data_str; |
| 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 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 65 | Handle<JSMessageObject> MessageHandler::MakeMessageObject( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | const char* type, |
| 67 | MessageLocation* loc, |
| 68 | Vector< Handle<Object> > args, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 69 | Handle<String> stack_trace, |
| 70 | Handle<JSArray> stack_frames) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 71 | Handle<String> type_handle = Factory::LookupAsciiSymbol(type); |
| 72 | Handle<FixedArray> arguments_elements = |
| 73 | Factory::NewFixedArray(args.length()); |
| 74 | for (int i = 0; i < args.length(); i++) { |
| 75 | arguments_elements->set(i, *args[i]); |
| 76 | } |
| 77 | Handle<JSArray> arguments_handle = |
| 78 | Factory::NewJSArrayWithElements(arguments_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 80 | int start = 0; |
| 81 | int end = 0; |
| 82 | Handle<Object> script_handle = Factory::undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | if (loc) { |
| 84 | start = loc->start_pos(); |
| 85 | end = loc->end_pos(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 86 | script_handle = GetScriptWrapper(loc->script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 89 | Handle<Object> stack_trace_handle = stack_trace.is_null() |
| 90 | ? Factory::undefined_value() |
| 91 | : Handle<Object>::cast(stack_trace); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 93 | Handle<Object> stack_frames_handle = stack_frames.is_null() |
| 94 | ? Factory::undefined_value() |
| 95 | : Handle<Object>::cast(stack_frames); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 97 | Handle<JSMessageObject> message = |
| 98 | Factory::NewJSMessageObject(type_handle, |
| 99 | arguments_handle, |
| 100 | start, |
| 101 | end, |
| 102 | script_handle, |
| 103 | stack_trace_handle, |
| 104 | stack_frames_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 106 | return message; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | void MessageHandler::ReportMessage(MessageLocation* loc, |
| 111 | Handle<Object> message) { |
| 112 | v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
| 113 | |
| 114 | v8::NeanderArray global_listeners(Factory::message_listeners()); |
| 115 | int global_length = global_listeners.length(); |
| 116 | if (global_length == 0) { |
| 117 | DefaultMessageReport(loc, message); |
| 118 | } else { |
| 119 | for (int i = 0; i < global_length; i++) { |
| 120 | HandleScope scope; |
| 121 | if (global_listeners.get(i)->IsUndefined()) continue; |
| 122 | v8::NeanderObject listener(JSObject::cast(global_listeners.get(i))); |
| 123 | Handle<Proxy> callback_obj(Proxy::cast(listener.get(0))); |
| 124 | v8::MessageCallback callback = |
| 125 | FUNCTION_CAST<v8::MessageCallback>(callback_obj->proxy()); |
| 126 | Handle<Object> callback_data(listener.get(1)); |
| 127 | callback(api_message_obj, v8::Utils::ToLocal(callback_data)); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | Handle<String> MessageHandler::GetMessage(Handle<Object> data) { |
| 134 | Handle<String> fmt_str = Factory::LookupAsciiSymbol("FormatMessage"); |
| 135 | Handle<JSFunction> fun = |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 136 | Handle<JSFunction>(JSFunction::cast( |
| 137 | Top::builtins()->GetPropertyNoExceptionThrown(*fmt_str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | Object** argv[1] = { data.location() }; |
| 139 | |
| 140 | bool caught_exception; |
| 141 | Handle<Object> result = |
| 142 | Execution::TryCall(fun, Top::builtins(), 1, argv, &caught_exception); |
| 143 | |
| 144 | if (caught_exception || !result->IsString()) { |
| 145 | return Factory::LookupAsciiSymbol("<error>"); |
| 146 | } |
| 147 | Handle<String> result_string = Handle<String>::cast(result); |
| 148 | // A string that has been obtained from JS code in this way is |
| 149 | // likely to be a complicated ConsString of some sort. We flatten it |
| 150 | // here to improve the efficiency of converting it to a C string and |
| 151 | // other operations that are likely to take place (see GetLocalizedMessage |
| 152 | // for example). |
| 153 | FlattenString(result_string); |
| 154 | return result_string; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | SmartPointer<char> MessageHandler::GetLocalizedMessage(Handle<Object> data) { |
| 159 | HandleScope scope; |
| 160 | return GetMessage(data)->ToCString(DISALLOW_NULLS); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | } } // namespace v8::internal |