messages.h 6.59 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7 8 9

// The infrastructure used for (localized) message reporting in V8.
//
// Note: there's a big unresolved issue about ownership of the data
// structures used by this framework.

10 11
#ifndef V8_EXECUTION_MESSAGES_H_
#define V8_EXECUTION_MESSAGES_H_
12

13 14
#include <memory>

15
#include "src/base/optional.h"
16
#include "src/common/message-template.h"
17
#include "src/handles/handles.h"
18

19 20
namespace v8 {
namespace internal {
21 22
namespace wasm {
class WasmCode;
23
}  // namespace wasm
24

25
// Forward declarations.
26
class AbstractCode;
27 28
class JSMessageObject;
class LookupIterator;
29
class PrimitiveHeapObject;
30
class SharedFunctionInfo;
31
class SourceInfo;
32
class WasmInstanceObject;
33

34
class V8_EXPORT_PRIVATE MessageLocation {
35
 public:
36 37 38
  // Constructors for when source positions are already known.
  // TODO(delphick): Collapse to a single constructor with a default parameter
  // when we stop using the GCC that requires this separation.
39
  MessageLocation(Handle<Script> script, int start_pos, int end_pos);
40
  MessageLocation(Handle<Script> script, int start_pos, int end_pos,
41
                  Handle<SharedFunctionInfo> shared);
42 43 44 45
  // Constructor for when source positions were not collected but which can be
  // reconstructed from the SharedFuncitonInfo and bytecode offset.
  MessageLocation(Handle<Script> script, Handle<SharedFunctionInfo> shared,
                  int bytecode_offset);
46
  MessageLocation();
47 48 49 50

  Handle<Script> script() const { return script_; }
  int start_pos() const { return start_pos_; }
  int end_pos() const { return end_pos_; }
51
  int bytecode_offset() const { return bytecode_offset_; }
52
  Handle<SharedFunctionInfo> shared() const { return shared_; }
53 54 55 56 57

 private:
  Handle<Script> script_;
  int start_pos_;
  int end_pos_;
58
  int bytecode_offset_;
59
  Handle<SharedFunctionInfo> shared_;
60 61
};

62 63 64 65 66 67 68 69 70 71
// Determines how stack trace collection skips frames.
enum FrameSkipMode {
  // Unconditionally skips the first frame. Used e.g. when the Error constructor
  // is called, in which case the first frame is always a BUILTIN_EXIT frame.
  SKIP_FIRST,
  // Skip all frames until a specified caller function is seen.
  SKIP_UNTIL_SEEN,
  SKIP_NONE,
};

jgruber's avatar
jgruber committed
72 73
class ErrorUtils : public AllStatic {
 public:
74 75 76
  // |kNone| is useful when you don't need the stack information at all, for
  // example when creating a deserialized error.
  enum class StackTraceCollection { kDetailed, kSimple, kNone };
77 78 79 80
  static MaybeHandle<JSObject> Construct(Isolate* isolate,
                                         Handle<JSFunction> target,
                                         Handle<Object> new_target,
                                         Handle<Object> message);
81
  static MaybeHandle<JSObject> Construct(
jgruber's avatar
jgruber committed
82
      Isolate* isolate, Handle<JSFunction> target, Handle<Object> new_target,
83
      Handle<Object> message, FrameSkipMode mode, Handle<Object> caller,
84
      StackTraceCollection stack_trace_collection);
jgruber's avatar
jgruber committed
85 86

  static MaybeHandle<String> ToString(Isolate* isolate, Handle<Object> recv);
87

88
  static Handle<JSObject> MakeGenericError(
89
      Isolate* isolate, Handle<JSFunction> constructor, MessageTemplate index,
90 91
      Handle<Object> arg0, Handle<Object> arg1, Handle<Object> arg2,
      FrameSkipMode mode);
92 93 94 95 96 97

  // Formats a textual stack trace from the given structured stack trace.
  // Note that this can call arbitrary JS code through Error.prepareStackTrace.
  static MaybeHandle<Object> FormatStackTrace(Isolate* isolate,
                                              Handle<JSObject> error,
                                              Handle<Object> stack_trace);
98

99 100 101 102 103 104 105
  static Handle<JSObject> NewIteratorError(Isolate* isolate,
                                           Handle<Object> source);
  static Handle<JSObject> NewCalledNonCallableError(Isolate* isolate,
                                                    Handle<Object> source);
  static Handle<JSObject> NewConstructedNonConstructable(Isolate* isolate,
                                                         Handle<Object> source);
  // Returns the Exception sentinel.
106 107
  static Object ThrowSpreadArgError(Isolate* isolate, MessageTemplate id,
                                    Handle<Object> object);
108
  // Returns the Exception sentinel.
109 110 111
  static Object ThrowLoadFromNullOrUndefined(Isolate* isolate,
                                             Handle<Object> object,
                                             MaybeHandle<Object> key);
jgruber's avatar
jgruber committed
112 113
};

114
class MessageFormatter {
115
 public:
116
  V8_EXPORT_PRIVATE static const char* TemplateString(MessageTemplate index);
117

118 119 120 121 122
  V8_EXPORT_PRIVATE static MaybeHandle<String> Format(Isolate* isolate,
                                                      MessageTemplate index,
                                                      Handle<String> arg0,
                                                      Handle<String> arg1,
                                                      Handle<String> arg2);
123 124

  static Handle<String> Format(Isolate* isolate, MessageTemplate index,
125 126 127
                               Handle<Object> arg0,
                               Handle<Object> arg1 = Handle<Object>(),
                               Handle<Object> arg2 = Handle<Object>());
128 129 130 131 132 133 134
};

// A message handler is a convenience interface for accessing the list
// of message listeners registered in an environment
class MessageHandler {
 public:
  // Returns a message object for the API to use.
135
  V8_EXPORT_PRIVATE static Handle<JSMessageObject> MakeMessageObject(
136 137
      Isolate* isolate, MessageTemplate type, const MessageLocation* location,
      Handle<Object> argument, Handle<FixedArray> stack_frames);
138 139

  // Report a formatted message (needs JS allocation).
140 141 142
  V8_EXPORT_PRIVATE static void ReportMessage(Isolate* isolate,
                                              const MessageLocation* loc,
                                              Handle<JSMessageObject> message);
143 144 145 146

  static void DefaultMessageReport(Isolate* isolate, const MessageLocation* loc,
                                   Handle<Object> message_obj);
  static Handle<String> GetMessage(Isolate* isolate, Handle<Object> data);
147 148
  static std::unique_ptr<char[]> GetLocalizedMessage(Isolate* isolate,
                                                     Handle<Object> data);
149 150 151 152 153 154

 private:
  static void ReportMessageNoExceptions(Isolate* isolate,
                                        const MessageLocation* loc,
                                        Handle<Object> message_obj,
                                        v8::Local<v8::Value> api_exception_obj);
155
};
156

157 158
}  // namespace internal
}  // namespace v8
159

160
#endif  // V8_EXECUTION_MESSAGES_H_