Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 5 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 7 | #include "src/api.h" |
| 8 | #include "src/execution.h" |
| 9 | #include "src/heap/spaces-inl.h" |
| 10 | #include "src/messages.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | |
| 16 | // If no message listeners have been registered this one is called |
| 17 | // by default. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 18 | void MessageHandler::DefaultMessageReport(Isolate* isolate, |
| 19 | const MessageLocation* loc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | Handle<Object> message_obj) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 21 | SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 22 | if (loc == NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 23 | PrintF("%s\n", str.get()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 24 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 25 | HandleScope scope(isolate); |
| 26 | Handle<Object> data(loc->script()->name(), isolate); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 27 | SmartArrayPointer<char> data_str; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 28 | if (data->IsString()) |
| 29 | data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 30 | PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>", |
| 31 | loc->start_pos(), str.get()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 36 | Handle<JSMessageObject> MessageHandler::MakeMessageObject( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 37 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | const char* type, |
| 39 | MessageLocation* loc, |
| 40 | Vector< Handle<Object> > args, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 41 | Handle<JSArray> stack_frames) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 42 | Factory* factory = isolate->factory(); |
| 43 | Handle<String> type_handle = factory->InternalizeUtf8String(type); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 44 | Handle<FixedArray> arguments_elements = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 45 | factory->NewFixedArray(args.length()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 46 | for (int i = 0; i < args.length(); i++) { |
| 47 | arguments_elements->set(i, *args[i]); |
| 48 | } |
| 49 | Handle<JSArray> arguments_handle = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 50 | factory->NewJSArrayWithElements(arguments_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 52 | int start = 0; |
| 53 | int end = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 54 | Handle<Object> script_handle = factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | if (loc) { |
| 56 | start = loc->start_pos(); |
| 57 | end = loc->end_pos(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 58 | script_handle = Script::GetWrapper(loc->script()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 61 | Handle<Object> stack_frames_handle = stack_frames.is_null() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 62 | ? Handle<Object>::cast(factory->undefined_value()) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 63 | : Handle<Object>::cast(stack_frames); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 65 | Handle<JSMessageObject> message = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 66 | factory->NewJSMessageObject(type_handle, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 67 | arguments_handle, |
| 68 | start, |
| 69 | end, |
| 70 | script_handle, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 71 | stack_frames_handle); |
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 | return message; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 77 | void MessageHandler::ReportMessage(Isolate* isolate, |
| 78 | MessageLocation* loc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | Handle<Object> message) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 80 | // We are calling into embedder's code which can throw exceptions. |
| 81 | // Thus we need to save current exception state, reset it to the clean one |
| 82 | // and ignore scheduled exceptions callbacks can throw. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 83 | |
| 84 | // We pass the exception object into the message handler callback though. |
| 85 | Object* exception_object = isolate->heap()->undefined_value(); |
| 86 | if (isolate->has_pending_exception()) { |
| 87 | exception_object = isolate->pending_exception(); |
| 88 | } |
| 89 | Handle<Object> exception_handle(exception_object, isolate); |
| 90 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 91 | Isolate::ExceptionScope exception_scope(isolate); |
| 92 | isolate->clear_pending_exception(); |
| 93 | isolate->set_external_caught_exception(false); |
| 94 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 96 | v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 98 | v8::NeanderArray global_listeners(isolate->factory()->message_listeners()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | int global_length = global_listeners.length(); |
| 100 | if (global_length == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 101 | DefaultMessageReport(isolate, loc, message); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 102 | if (isolate->has_scheduled_exception()) { |
| 103 | isolate->clear_scheduled_exception(); |
| 104 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | } else { |
| 106 | for (int i = 0; i < global_length; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 107 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | if (global_listeners.get(i)->IsUndefined()) continue; |
| 109 | v8::NeanderObject listener(JSObject::cast(global_listeners.get(i))); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 110 | Handle<Foreign> callback_obj(Foreign::cast(listener.get(0))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | v8::MessageCallback callback = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 112 | FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 113 | Handle<Object> callback_data(listener.get(1), isolate); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 114 | { |
| 115 | // Do not allow exceptions to propagate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 116 | v8::TryCatch try_catch; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 117 | callback(api_message_obj, callback_data->IsUndefined() |
| 118 | ? api_exception_obj |
| 119 | : v8::Utils::ToLocal(callback_data)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 120 | } |
| 121 | if (isolate->has_scheduled_exception()) { |
| 122 | isolate->clear_scheduled_exception(); |
| 123 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 129 | Handle<String> MessageHandler::GetMessage(Isolate* isolate, |
| 130 | Handle<Object> data) { |
| 131 | Factory* factory = isolate->factory(); |
| 132 | Handle<String> fmt_str = |
| 133 | factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("FormatMessage")); |
| 134 | Handle<JSFunction> fun = Handle<JSFunction>::cast(Object::GetProperty( |
| 135 | isolate->js_builtins_object(), fmt_str).ToHandleChecked()); |
| 136 | Handle<JSMessageObject> message = Handle<JSMessageObject>::cast(data); |
| 137 | Handle<Object> argv[] = { Handle<Object>(message->type(), isolate), |
| 138 | Handle<Object>(message->arguments(), isolate) }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 140 | MaybeHandle<Object> maybe_result = Execution::TryCall( |
| 141 | fun, isolate->js_builtins_object(), arraysize(argv), argv); |
| 142 | Handle<Object> result; |
| 143 | if (!maybe_result.ToHandle(&result) || !result->IsString()) { |
| 144 | return factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("<error>")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | } |
| 146 | Handle<String> result_string = Handle<String>::cast(result); |
| 147 | // A string that has been obtained from JS code in this way is |
| 148 | // likely to be a complicated ConsString of some sort. We flatten it |
| 149 | // here to improve the efficiency of converting it to a C string and |
| 150 | // other operations that are likely to take place (see GetLocalizedMessage |
| 151 | // for example). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 152 | result_string = String::Flatten(result_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | return result_string; |
| 154 | } |
| 155 | |
| 156 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 157 | SmartArrayPointer<char> MessageHandler::GetLocalizedMessage( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 158 | Isolate* isolate, |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 159 | Handle<Object> data) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 160 | HandleScope scope(isolate); |
| 161 | return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | } } // namespace v8::internal |