blob: 290f756770c7d4b287ee2f5ce6d5b2326b7fe521 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/api.h"
8#include "src/execution.h"
9#include "src/heap/spaces-inl.h"
10#include "src/messages.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000011
12namespace v8 {
13namespace internal {
14
15
16// If no message listeners have been registered this one is called
17// by default.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018void MessageHandler::DefaultMessageReport(Isolate* isolate,
19 const MessageLocation* loc,
Steve Blocka7e24c12009-10-30 11:49:00 +000020 Handle<Object> message_obj) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021 SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000022 if (loc == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023 PrintF("%s\n", str.get());
Steve Blocka7e24c12009-10-30 11:49:00 +000024 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025 HandleScope scope(isolate);
26 Handle<Object> data(loc->script()->name(), isolate);
Ben Murdoch589d6972011-11-30 16:04:58 +000027 SmartArrayPointer<char> data_str;
Steve Blocka7e24c12009-10-30 11:49:00 +000028 if (data->IsString())
29 data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
31 loc->start_pos(), str.get());
Steve Blocka7e24c12009-10-30 11:49:00 +000032 }
33}
34
35
Steve Block1e0659c2011-05-24 12:43:12 +010036Handle<JSMessageObject> MessageHandler::MakeMessageObject(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +000038 const char* type,
39 MessageLocation* loc,
40 Vector< Handle<Object> > args,
Ben Murdoch3bec4d22010-07-22 14:51:16 +010041 Handle<JSArray> stack_frames) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 Factory* factory = isolate->factory();
43 Handle<String> type_handle = factory->InternalizeUtf8String(type);
Steve Block1e0659c2011-05-24 12:43:12 +010044 Handle<FixedArray> arguments_elements =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 factory->NewFixedArray(args.length());
Steve Block1e0659c2011-05-24 12:43:12 +010046 for (int i = 0; i < args.length(); i++) {
47 arguments_elements->set(i, *args[i]);
48 }
49 Handle<JSArray> arguments_handle =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 factory->NewJSArrayWithElements(arguments_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +000051
Steve Block1e0659c2011-05-24 12:43:12 +010052 int start = 0;
53 int end = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 Handle<Object> script_handle = factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000055 if (loc) {
56 start = loc->start_pos();
57 end = loc->end_pos();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 script_handle = Script::GetWrapper(loc->script());
Steve Blocka7e24c12009-10-30 11:49:00 +000059 }
Steve Blocka7e24c12009-10-30 11:49:00 +000060
Steve Block1e0659c2011-05-24 12:43:12 +010061 Handle<Object> stack_frames_handle = stack_frames.is_null()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 ? Handle<Object>::cast(factory->undefined_value())
Steve Block1e0659c2011-05-24 12:43:12 +010063 : Handle<Object>::cast(stack_frames);
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Steve Block1e0659c2011-05-24 12:43:12 +010065 Handle<JSMessageObject> message =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 factory->NewJSMessageObject(type_handle,
Steve Block1e0659c2011-05-24 12:43:12 +010067 arguments_handle,
68 start,
69 end,
70 script_handle,
Steve Block1e0659c2011-05-24 12:43:12 +010071 stack_frames_handle);
Steve Blocka7e24c12009-10-30 11:49:00 +000072
Steve Block1e0659c2011-05-24 12:43:12 +010073 return message;
Steve Blocka7e24c12009-10-30 11:49:00 +000074}
75
76
Ben Murdoch8b112d22011-06-08 16:22:53 +010077void MessageHandler::ReportMessage(Isolate* isolate,
78 MessageLocation* loc,
Steve Blocka7e24c12009-10-30 11:49:00 +000079 Handle<Object> message) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010080 // 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 Murdochb8a8cc12014-11-26 15:28:44 +000083
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 Murdoch8b112d22011-06-08 16:22:53 +010091 Isolate::ExceptionScope exception_scope(isolate);
92 isolate->clear_pending_exception();
93 isolate->set_external_caught_exception(false);
94
Steve Blocka7e24c12009-10-30 11:49:00 +000095 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle);
Steve Blocka7e24c12009-10-30 11:49:00 +000097
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
Steve Blocka7e24c12009-10-30 11:49:00 +000099 int global_length = global_listeners.length();
100 if (global_length == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 DefaultMessageReport(isolate, loc, message);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100102 if (isolate->has_scheduled_exception()) {
103 isolate->clear_scheduled_exception();
104 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 } else {
106 for (int i = 0; i < global_length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 if (global_listeners.get(i)->IsUndefined()) continue;
109 v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000110 Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 v8::MessageCallback callback =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100112 FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 Handle<Object> callback_data(listener.get(1), isolate);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100114 {
115 // Do not allow exceptions to propagate.
Ben Murdoch257744e2011-11-30 15:57:28 +0000116 v8::TryCatch try_catch;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 callback(api_message_obj, callback_data->IsUndefined()
118 ? api_exception_obj
119 : v8::Utils::ToLocal(callback_data));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100120 }
121 if (isolate->has_scheduled_exception()) {
122 isolate->clear_scheduled_exception();
123 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 }
125 }
126}
127
128
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129Handle<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 Blocka7e24c12009-10-30 11:49:00 +0000139
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 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 Blocka7e24c12009-10-30 11:49:00 +0000145 }
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 Murdochb8a8cc12014-11-26 15:28:44 +0000152 result_string = String::Flatten(result_string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 return result_string;
154}
155
156
Ben Murdoch589d6972011-11-30 16:04:58 +0000157SmartArrayPointer<char> MessageHandler::GetLocalizedMessage(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 Isolate* isolate,
Ben Murdoch589d6972011-11-30 16:04:58 +0000159 Handle<Object> data) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 HandleScope scope(isolate);
161 return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000162}
163
164
165} } // namespace v8::internal