v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
messages.cc
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include "v8.h"
29 
30 #include "api.h"
31 #include "execution.h"
32 #include "messages.h"
33 #include "spaces-inl.h"
34 
35 namespace v8 {
36 namespace internal {
37 
38 
39 // If no message listeners have been registered this one is called
40 // by default.
42  const MessageLocation* loc,
43  Handle<Object> message_obj) {
44  SmartArrayPointer<char> str = GetLocalizedMessage(isolate, message_obj);
45  if (loc == NULL) {
46  PrintF("%s\n", str.get());
47  } else {
48  HandleScope scope(isolate);
49  Handle<Object> data(loc->script()->name(), isolate);
50  SmartArrayPointer<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.get() ? data_str.get() : "<unknown>",
54  loc->start_pos(), str.get());
55  }
56 }
57 
58 
60  Isolate* isolate,
61  const char* type,
62  MessageLocation* loc,
63  Vector< Handle<Object> > args,
64  Handle<JSArray> stack_frames) {
65  Factory* factory = isolate->factory();
66  Handle<String> type_handle = factory->InternalizeUtf8String(type);
67  Handle<FixedArray> arguments_elements =
68  factory->NewFixedArray(args.length());
69  for (int i = 0; i < args.length(); i++) {
70  arguments_elements->set(i, *args[i]);
71  }
72  Handle<JSArray> arguments_handle =
73  factory->NewJSArrayWithElements(arguments_elements);
74 
75  int start = 0;
76  int end = 0;
77  Handle<Object> script_handle = factory->undefined_value();
78  if (loc) {
79  start = loc->start_pos();
80  end = loc->end_pos();
81  script_handle = GetScriptWrapper(loc->script());
82  }
83 
84  Handle<Object> stack_frames_handle = stack_frames.is_null()
85  ? Handle<Object>::cast(factory->undefined_value())
86  : Handle<Object>::cast(stack_frames);
87 
89  factory->NewJSMessageObject(type_handle,
90  arguments_handle,
91  start,
92  end,
93  script_handle,
94  stack_frames_handle);
95 
96  return message;
97 }
98 
99 
101  MessageLocation* loc,
103  // We are calling into embedder's code which can throw exceptions.
104  // Thus we need to save current exception state, reset it to the clean one
105  // and ignore scheduled exceptions callbacks can throw.
106 
107  // We pass the exception object into the message handler callback though.
108  Object* exception_object = isolate->heap()->undefined_value();
109  if (isolate->has_pending_exception()) {
110  isolate->pending_exception()->ToObject(&exception_object);
111  }
112  Handle<Object> exception_handle(exception_object, isolate);
113 
114  Isolate::ExceptionScope exception_scope(isolate);
115  isolate->clear_pending_exception();
116  isolate->set_external_caught_exception(false);
117 
118  v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
119  v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle);
120 
121  v8::NeanderArray global_listeners(isolate->factory()->message_listeners());
122  int global_length = global_listeners.length();
123  if (global_length == 0) {
124  DefaultMessageReport(isolate, loc, message);
125  if (isolate->has_scheduled_exception()) {
126  isolate->clear_scheduled_exception();
127  }
128  } else {
129  for (int i = 0; i < global_length; i++) {
130  HandleScope scope(isolate);
131  if (global_listeners.get(i)->IsUndefined()) continue;
132  v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
133  Handle<Foreign> callback_obj(Foreign::cast(listener.get(0)));
134  v8::MessageCallback callback =
135  FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
136  Handle<Object> callback_data(listener.get(1), isolate);
137  {
138  // Do not allow exceptions to propagate.
139  v8::TryCatch try_catch;
140  callback(api_message_obj, callback_data->IsUndefined()
141  ? api_exception_obj
142  : v8::Utils::ToLocal(callback_data));
143  }
144  if (isolate->has_scheduled_exception()) {
145  isolate->clear_scheduled_exception();
146  }
147  }
148  }
149 }
150 
151 
153  Handle<Object> data) {
154  Factory* factory = isolate->factory();
155  Handle<String> fmt_str =
156  factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("FormatMessage"));
157  Handle<JSFunction> fun =
160  isolate->js_builtins_object()->
161  GetPropertyNoExceptionThrown(*fmt_str)));
163  Handle<Object> argv[] = { Handle<Object>(message->type(), isolate),
164  Handle<Object>(message->arguments(), isolate) };
165 
166  bool caught_exception;
167  Handle<Object> result =
168  Execution::TryCall(fun,
169  isolate->js_builtins_object(),
170  ARRAY_SIZE(argv),
171  argv,
172  &caught_exception);
173 
174  if (caught_exception || !result->IsString()) {
175  return factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("<error>"));
176  }
177  Handle<String> result_string = Handle<String>::cast(result);
178  // A string that has been obtained from JS code in this way is
179  // likely to be a complicated ConsString of some sort. We flatten it
180  // here to improve the efficiency of converting it to a C string and
181  // other operations that are likely to take place (see GetLocalizedMessage
182  // for example).
183  FlattenString(result_string);
184  return result_string;
185 }
186 
187 
189  Isolate* isolate,
190  Handle<Object> data) {
191  HandleScope scope(isolate);
192  return GetMessage(isolate, data)->ToCString(DISALLOW_NULLS);
193 }
194 
195 
196 } } // namespace v8::internal
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
void FlattenString(Handle< String > string)
Definition: handles.cc:151
static Handle< Object > TryCall(Handle< JSFunction > func, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *caught_exception)
Definition: execution.cc:199
MaybeObject * pending_exception()
Definition: isolate.h:570
void PrintF(const char *format,...)
Definition: v8utils.cc:40
static Handle< T > cast(Handle< S > that)
Definition: handles.h:75
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV 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 expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in only print modified registers Don t break for ASM_UNIMPLEMENTED_BREAK macros print stack trace when an illegal exception is thrown randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot testing_bool_flag testing_int_flag string flag tmp file in which to serialize heap Print the time it takes to lazily compile hydrogen code stubs concurrent_recompilation concurrent_sweeping Print usage message
Definition: flags.cc:665
void(* MessageCallback)(Handle< Message > message, Handle< Value > error)
Definition: v8.h:3987
Handle< JSArray > NewJSArrayWithElements(Handle< FixedArrayBase > elements, ElementsKind elements_kind, int length, PretenureFlag pretenure=NOT_TENURED)
Definition: factory.cc:1456
static Foreign * cast(Object *obj)
int length()
Definition: api.cc:775
static Handle< String > GetMessage(Isolate *isolate, Handle< Object > data)
Definition: messages.cc:152
void clear_pending_exception()
Definition: isolate.h:579
void clear_scheduled_exception()
Definition: isolate.h:634
Factory * factory()
Definition: isolate.h:995
static Handle< JSMessageObject > MakeMessageObject(Isolate *isolate, const char *type, MessageLocation *loc, Vector< Handle< Object > > args, Handle< JSArray > stack_frames)
Definition: messages.cc:59
static Local< Message > MessageToLocal(v8::internal::Handle< v8::internal::Object > obj)
Handle< JSValue > GetScriptWrapper(Handle< Script > script)
Definition: handles.cc:240
Handle< FixedArray > NewFixedArray(int size, PretenureFlag pretenure=NOT_TENURED)
Definition: factory.cc:53
bool has_pending_exception()
Definition: isolate.h:587
Handle< JSBuiltinsObject > js_builtins_object()
Definition: isolate.h:680
#define STATIC_ASCII_VECTOR(x)
Definition: utils.h:570
Handle< Script > script() const
Definition: messages.h:77
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
V8_INLINE bool IsString() const
Definition: v8.h:6265
Handle< String > InternalizeOneByteString(Vector< const uint8_t > str)
Definition: factory.cc:232
static void DefaultMessageReport(Isolate *isolate, const MessageLocation *loc, Handle< Object > message_obj)
Definition: messages.cc:41
bool is_null() const
Definition: handles.h:81
static SmartArrayPointer< char > GetLocalizedMessage(Isolate *isolate, Handle< Object > data)
Definition: messages.cc:188
Handle< String > InternalizeUtf8String(Vector< const char > str)
Definition: factory.cc:218
bool has_scheduled_exception()
Definition: isolate.h:631
Handle< JSMessageObject > NewJSMessageObject(Handle< String > type, Handle< JSArray > arguments, int start_position, int end_position, Handle< Object > script, Handle< Object > stack_frames)
Definition: factory.cc:1594
static void ReportMessage(Isolate *isolate, MessageLocation *loc, Handle< Object > message)
Definition: messages.cc:100
#define ARRAY_SIZE(a)
Definition: globals.h:333
Definition: v8.h:124
static JSObject * cast(Object *obj)
v8::internal::Object * get(int index)
Definition: api.h:106
static JSFunction * cast(Object *obj)