v8  3.14.5(node0.10.28)
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  Handle<Object> message_obj) {
44  if (loc == NULL) {
45  PrintF("%s\n", *str);
46  } else {
47  HandleScope scope;
48  Handle<Object> data(loc->script()->name());
49  SmartArrayPointer<char> data_str;
50  if (data->IsString())
51  data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
52  PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>",
53  loc->start_pos(), *str);
54  }
55 }
56 
57 
59  const char* type,
60  MessageLocation* loc,
61  Vector< Handle<Object> > args,
62  Handle<String> stack_trace,
63  Handle<JSArray> stack_frames) {
64  Handle<String> type_handle = FACTORY->LookupAsciiSymbol(type);
65  Handle<FixedArray> arguments_elements =
66  FACTORY->NewFixedArray(args.length());
67  for (int i = 0; i < args.length(); i++) {
68  arguments_elements->set(i, *args[i]);
69  }
70  Handle<JSArray> arguments_handle =
71  FACTORY->NewJSArrayWithElements(arguments_elements);
72 
73  int start = 0;
74  int end = 0;
75  Handle<Object> script_handle = FACTORY->undefined_value();
76  if (loc) {
77  start = loc->start_pos();
78  end = loc->end_pos();
79  script_handle = GetScriptWrapper(loc->script());
80  }
81 
82  Handle<Object> stack_trace_handle = stack_trace.is_null()
83  ? Handle<Object>::cast(FACTORY->undefined_value())
84  : Handle<Object>::cast(stack_trace);
85 
86  Handle<Object> stack_frames_handle = stack_frames.is_null()
87  ? Handle<Object>::cast(FACTORY->undefined_value())
88  : Handle<Object>::cast(stack_frames);
89 
91  FACTORY->NewJSMessageObject(type_handle,
92  arguments_handle,
93  start,
94  end,
95  script_handle,
96  stack_trace_handle,
97  stack_frames_handle);
98 
99  return message;
100 }
101 
102 
104  MessageLocation* loc,
106  // We are calling into embedder's code which can throw exceptions.
107  // Thus we need to save current exception state, reset it to the clean one
108  // and ignore scheduled exceptions callbacks can throw.
109 
110  // We pass the exception object into the message handler callback though.
111  Object* exception_object = isolate->heap()->undefined_value();
112  if (isolate->has_pending_exception()) {
113  isolate->pending_exception()->ToObject(&exception_object);
114  }
115  Handle<Object> exception_handle(exception_object);
116 
117  Isolate::ExceptionScope exception_scope(isolate);
118  isolate->clear_pending_exception();
119  isolate->set_external_caught_exception(false);
120 
121  v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
122  v8::Local<v8::Value> api_exception_obj = v8::Utils::ToLocal(exception_handle);
123 
124  v8::NeanderArray global_listeners(FACTORY->message_listeners());
125  int global_length = global_listeners.length();
126  if (global_length == 0) {
127  DefaultMessageReport(loc, message);
128  if (isolate->has_scheduled_exception()) {
129  isolate->clear_scheduled_exception();
130  }
131  } else {
132  for (int i = 0; i < global_length; i++) {
133  HandleScope scope;
134  if (global_listeners.get(i)->IsUndefined()) continue;
135  Handle<Foreign> callback_obj(Foreign::cast(global_listeners.get(i)));
136  v8::MessageCallback callback =
137  FUNCTION_CAST<v8::MessageCallback>(callback_obj->foreign_address());
138  {
139  // Do not allow exceptions to propagate.
140  v8::TryCatch try_catch;
141  callback(api_message_obj, api_exception_obj);
142  }
143  if (isolate->has_scheduled_exception()) {
144  isolate->clear_scheduled_exception();
145  }
146  }
147  }
148 }
149 
150 
152  Handle<String> fmt_str = FACTORY->LookupAsciiSymbol("FormatMessage");
153  Handle<JSFunction> fun =
156  Isolate::Current()->js_builtins_object()->
157  GetPropertyNoExceptionThrown(*fmt_str)));
158  Handle<Object> argv[] = { data };
159 
160  bool caught_exception;
161  Handle<Object> result =
162  Execution::TryCall(fun,
163  Isolate::Current()->js_builtins_object(),
164  ARRAY_SIZE(argv),
165  argv,
166  &caught_exception);
167 
168  if (caught_exception || !result->IsString()) {
169  return FACTORY->LookupAsciiSymbol("<error>");
170  }
171  Handle<String> result_string = Handle<String>::cast(result);
172  // A string that has been obtained from JS code in this way is
173  // likely to be a complicated ConsString of some sort. We flatten it
174  // here to improve the efficiency of converting it to a C string and
175  // other operations that are likely to take place (see GetLocalizedMessage
176  // for example).
177  FlattenString(result_string);
178  return result_string;
179 }
180 
181 
183  Handle<Object> data) {
184  HandleScope scope;
185  return GetMessage(data)->ToCString(DISALLOW_NULLS);
186 }
187 
188 
189 } } // namespace v8::internal
void FlattenString(Handle< String > string)
Definition: handles.cc:211
static Handle< Object > TryCall(Handle< JSFunction > func, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *caught_exception)
Definition: execution.cc:192
MaybeObject * pending_exception()
Definition: isolate.h:542
void PrintF(const char *format,...)
Definition: v8utils.cc:40
static Handle< JSMessageObject > MakeMessageObject(const char *type, MessageLocation *loc, Vector< Handle< Object > > args, Handle< String > stack_trace, Handle< JSArray > stack_frames)
Definition: messages.cc:58
static Handle< T > cast(Handle< S > that)
Definition: handles.h:81
void(* MessageCallback)(Handle< Message > message, Handle< Value > error)
Definition: v8.h:2663
static Foreign * cast(Object *obj)
int length()
Definition: api.cc:857
void clear_pending_exception()
Definition: isolate.h:555
void clear_scheduled_exception()
Definition: isolate.h:609
static Local< Message > MessageToLocal(v8::internal::Handle< v8::internal::Object > obj)
Handle< JSValue > GetScriptWrapper(Handle< Script > script)
Definition: handles.cc:366
bool has_pending_exception()
Definition: isolate.h:561
static Handle< String > GetMessage(Handle< Object > data)
Definition: messages.cc:151
Handle< Script > script() const
Definition: messages.h:77
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
static void DefaultMessageReport(const MessageLocation *loc, Handle< Object > message_obj)
Definition: messages.cc:41
bool is_null() const
Definition: handles.h:87
static SmartArrayPointer< char > GetLocalizedMessage(Handle< Object > data)
Definition: messages.cc:182
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit 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 SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available 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 MIPS FPU instructions if expose natives in global object expose gc extension number of stack frames to capture disable builtin natives files print a stack trace if an assertion failure occurs use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations prepare for turning on always opt minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions automatically set the debug break flag when debugger commands are in the queue always cause a debug break before aborting 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 more details following each garbage collection print amount of external allocated memory after each time it is adjusted flush code that we expect not to use again before full gc do incremental marking steps track object counts and memory usage use caching Perform compaction on every full GC Never perform compaction on full GC testing only Compact code space on full incremental collections Default seed for initializing random allows verbose printing trace parsing and preparsing Check icache flushes in ARM and MIPS simulator Stack alingment in bytes in print stack trace when throwing exceptions randomize hashes to avoid predictable hash Fixed seed to use to hash property activate a timer that switches between V8 threads testing_bool_flag float flag Seed used for threading test randomness A filename with extra code to be included in the Print usage message
Definition: flags.cc:495
void set_external_caught_exception(bool value)
Definition: isolate.h:549
#define FACTORY
Definition: isolate.h:1434
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit 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 SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available 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 MIPS FPU instructions if NULL
Definition: flags.cc:301
bool has_scheduled_exception()
Definition: isolate.h:606
static void ReportMessage(Isolate *isolate, MessageLocation *loc, Handle< Object > message)
Definition: messages.cc:103
#define ARRAY_SIZE(a)
Definition: globals.h:281
Definition: v8.h:106
static JSFunction * cast(Object *obj)