V8 Project
messages.cc
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/v8.h"
6 
7 #include "src/api.h"
8 #include "src/execution.h"
9 #include "src/heap/spaces-inl.h"
10 #include "src/messages.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 
16 // If no message listeners have been registered this one is called
17 // by default.
19  const MessageLocation* loc,
20  Handle<Object> message_obj) {
21  SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
22  if (loc == NULL) {
23  PrintF("%s\n", str.get());
24  } else {
25  HandleScope scope(isolate);
26  Handle<Object> data(loc->script()->name(), isolate);
27  SmartArrayPointer<char> data_str;
28  if (data->IsString())
29  data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
30  PrintF("%s:%i: %s\n", data_str.get() ? data_str.get() : "<unknown>",
31  loc->start_pos(), str.get());
32  }
33 }
34 
35 
37  Isolate* isolate,
38  const char* type,
39  MessageLocation* loc,
40  Vector< Handle<Object> > args,
41  Handle<JSArray> stack_frames) {
42  Factory* factory = isolate->factory();
43  Handle<String> type_handle = factory->InternalizeUtf8String(type);
44  Handle<FixedArray> arguments_elements =
45  factory->NewFixedArray(args.length());
46  for (int i = 0; i < args.length(); i++) {
47  arguments_elements->set(i, *args[i]);
48  }
49  Handle<JSArray> arguments_handle =
50  factory->NewJSArrayWithElements(arguments_elements);
51 
52  int start = 0;
53  int end = 0;
54  Handle<Object> script_handle = factory->undefined_value();
55  if (loc) {
56  start = loc->start_pos();
57  end = loc->end_pos();
58  script_handle = Script::GetWrapper(loc->script());
59  }
60 
61  Handle<Object> stack_frames_handle = stack_frames.is_null()
62  ? Handle<Object>::cast(factory->undefined_value())
63  : Handle<Object>::cast(stack_frames);
64 
65  Handle<JSMessageObject> message =
66  factory->NewJSMessageObject(type_handle,
67  arguments_handle,
68  start,
69  end,
70  script_handle,
71  stack_frames_handle);
72 
73  return message;
74 }
75 
76 
78  MessageLocation* loc,
79  Handle<Object> message) {
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.
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 
91  Isolate::ExceptionScope exception_scope(isolate);
92  isolate->clear_pending_exception();
93  isolate->set_external_caught_exception(false);
94 
95  v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
96  v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle);
97 
98  v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
99  int global_length = global_listeners.length();
100  if (global_length == 0) {
101  DefaultMessageReport(isolate, loc, message);
102  if (isolate->has_scheduled_exception()) {
103  isolate->clear_scheduled_exception();
104  }
105  } else {
106  for (int i = 0; i < global_length; i++) {
107  HandleScope scope(isolate);
108  if (global_listeners.get(i)->IsUndefined()) continue;
109  v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
110  Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
111  v8::MessageCallback callback =
112  FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
113  Handle<Object> callback_data(listener.get(1), isolate);
114  {
115  // Do not allow exceptions to propagate.
116  v8::TryCatch try_catch;
117  callback(api_message_obj, callback_data->IsUndefined()
118  ? api_exception_obj
119  : v8::Utils::ToLocal(callback_data));
120  }
121  if (isolate->has_scheduled_exception()) {
122  isolate->clear_scheduled_exception();
123  }
124  }
125  }
126 }
127 
128 
130  Handle<Object> data) {
131  Factory* factory = isolate->factory();
132  Handle<String> fmt_str =
133  factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("FormatMessage"));
135  isolate->js_builtins_object(), fmt_str).ToHandleChecked());
137  Handle<Object> argv[] = { Handle<Object>(message->type(), isolate),
138  Handle<Object>(message->arguments(), isolate) };
139 
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>"));
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).
152  result_string = String::Flatten(result_string);
153  return result_string;
154 }
155 
156 
158  Isolate* isolate,
159  Handle<Object> data) {
160  HandleScope scope(isolate);
161  return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
162 }
163 
164 
165 } } // namespace v8::internal
A light-weight stack-allocated object handle.
Definition: v8.h:334
int length()
Definition: api.cc:736
v8::internal::Object * get(int index)
Definition: api.cc:741
v8::internal::Object * get(int index)
Definition: api.h:83
An external exception handler.
Definition: v8.h:5271
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
static Local< Message > MessageToLocal(v8::internal::Handle< v8::internal::Object > obj)
static Handle< T > cast(Handle< S > that)
Definition: handles.h:116
bool is_null() const
Definition: handles.h:124
void clear_scheduled_exception()
Definition: isolate.h:631
bool has_scheduled_exception()
Definition: isolate.h:627
Object * pending_exception()
Definition: isolate.h:561
Handle< JSBuiltinsObject > js_builtins_object()
Definition: isolate.h:679
void clear_pending_exception()
Definition: isolate.h:572
Factory * factory()
Definition: isolate.h:982
bool has_pending_exception()
Definition: isolate.h:581
static void ReportMessage(Isolate *isolate, MessageLocation *loc, Handle< Object > message)
Definition: messages.cc:77
static Handle< String > GetMessage(Isolate *isolate, Handle< Object > data)
Definition: messages.cc:129
static SmartArrayPointer< char > GetLocalizedMessage(Isolate *isolate, Handle< Object > data)
Definition: messages.cc:157
static void DefaultMessageReport(Isolate *isolate, const MessageLocation *loc, Handle< Object > message_obj)
Definition: messages.cc:18
static Handle< JSMessageObject > MakeMessageObject(Isolate *isolate, const char *type, MessageLocation *loc, Vector< Handle< Object > > args, Handle< JSArray > stack_frames)
Definition: messages.cc:36
Handle< Script > script() const
Definition: messages.h:54
static MUST_USE_RESULT MaybeHandle< Object > GetProperty(LookupIterator *it)
Definition: objects.cc:109
static Handle< JSObject > GetWrapper(Handle< Script > script)
Definition: objects.cc:9741
static Handle< String > Flatten(Handle< String > string, PretenureFlag pretenure=NOT_TENURED)
Definition: objects-inl.h:3354
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define arraysize(array)
Definition: macros.h:86
@ DISALLOW_NULLS
Definition: objects.h:8337
void PrintF(const char *format,...)
Definition: utils.cc:80
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
void(* MessageCallback)(Handle< Message > message, Handle< Value > error)
Definition: v8.h:4120
#define STATIC_CHAR_VECTOR(x)
Definition: vector.h:154