11cb0ef41Sopenharmony_ci// Copyright 2006-2008 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci// The infrastructure used for (localized) message reporting in V8. 61cb0ef41Sopenharmony_ci// 71cb0ef41Sopenharmony_ci// Note: there's a big unresolved issue about ownership of the data 81cb0ef41Sopenharmony_ci// structures used by this framework. 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#ifndef V8_EXECUTION_MESSAGES_H_ 111cb0ef41Sopenharmony_ci#define V8_EXECUTION_MESSAGES_H_ 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci#include <memory> 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci#include "include/v8-local-handle.h" 161cb0ef41Sopenharmony_ci#include "src/base/optional.h" 171cb0ef41Sopenharmony_ci#include "src/common/message-template.h" 181cb0ef41Sopenharmony_ci#include "src/handles/handles.h" 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cinamespace v8 { 211cb0ef41Sopenharmony_cinamespace internal { 221cb0ef41Sopenharmony_cinamespace wasm { 231cb0ef41Sopenharmony_ciclass WasmCode; 241cb0ef41Sopenharmony_ci} // namespace wasm 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci// Forward declarations. 271cb0ef41Sopenharmony_ciclass AbstractCode; 281cb0ef41Sopenharmony_ciclass JSMessageObject; 291cb0ef41Sopenharmony_ciclass LookupIterator; 301cb0ef41Sopenharmony_ciclass PrimitiveHeapObject; 311cb0ef41Sopenharmony_ciclass SharedFunctionInfo; 321cb0ef41Sopenharmony_ciclass SourceInfo; 331cb0ef41Sopenharmony_ciclass WasmInstanceObject; 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE MessageLocation { 361cb0ef41Sopenharmony_ci public: 371cb0ef41Sopenharmony_ci // Constructors for when source positions are already known. 381cb0ef41Sopenharmony_ci // TODO(delphick): Collapse to a single constructor with a default parameter 391cb0ef41Sopenharmony_ci // when we stop using the GCC that requires this separation. 401cb0ef41Sopenharmony_ci MessageLocation(Handle<Script> script, int start_pos, int end_pos); 411cb0ef41Sopenharmony_ci MessageLocation(Handle<Script> script, int start_pos, int end_pos, 421cb0ef41Sopenharmony_ci Handle<SharedFunctionInfo> shared); 431cb0ef41Sopenharmony_ci // Constructor for when source positions were not collected but which can be 441cb0ef41Sopenharmony_ci // reconstructed from the SharedFuncitonInfo and bytecode offset. 451cb0ef41Sopenharmony_ci MessageLocation(Handle<Script> script, Handle<SharedFunctionInfo> shared, 461cb0ef41Sopenharmony_ci int bytecode_offset); 471cb0ef41Sopenharmony_ci MessageLocation(); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci Handle<Script> script() const { return script_; } 501cb0ef41Sopenharmony_ci int start_pos() const { return start_pos_; } 511cb0ef41Sopenharmony_ci int end_pos() const { return end_pos_; } 521cb0ef41Sopenharmony_ci int bytecode_offset() const { return bytecode_offset_; } 531cb0ef41Sopenharmony_ci Handle<SharedFunctionInfo> shared() const { return shared_; } 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci private: 561cb0ef41Sopenharmony_ci Handle<Script> script_; 571cb0ef41Sopenharmony_ci int start_pos_; 581cb0ef41Sopenharmony_ci int end_pos_; 591cb0ef41Sopenharmony_ci int bytecode_offset_; 601cb0ef41Sopenharmony_ci Handle<SharedFunctionInfo> shared_; 611cb0ef41Sopenharmony_ci}; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Determines how stack trace collection skips frames. 641cb0ef41Sopenharmony_cienum FrameSkipMode { 651cb0ef41Sopenharmony_ci // Unconditionally skips the first frame. Used e.g. when the Error constructor 661cb0ef41Sopenharmony_ci // is called, in which case the first frame is always a BUILTIN_EXIT frame. 671cb0ef41Sopenharmony_ci SKIP_FIRST, 681cb0ef41Sopenharmony_ci // Skip all frames until a specified caller function is seen. 691cb0ef41Sopenharmony_ci SKIP_UNTIL_SEEN, 701cb0ef41Sopenharmony_ci SKIP_NONE, 711cb0ef41Sopenharmony_ci}; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciclass ErrorUtils : public AllStatic { 741cb0ef41Sopenharmony_ci public: 751cb0ef41Sopenharmony_ci // |kDisabled| is useful when you don't need the stack information at all, for 761cb0ef41Sopenharmony_ci // example when creating a deserialized error. 771cb0ef41Sopenharmony_ci enum class StackTraceCollection { kEnabled, kDisabled }; 781cb0ef41Sopenharmony_ci static MaybeHandle<JSObject> Construct(Isolate* isolate, 791cb0ef41Sopenharmony_ci Handle<JSFunction> target, 801cb0ef41Sopenharmony_ci Handle<Object> new_target, 811cb0ef41Sopenharmony_ci Handle<Object> message, 821cb0ef41Sopenharmony_ci Handle<Object> options); 831cb0ef41Sopenharmony_ci static MaybeHandle<JSObject> Construct( 841cb0ef41Sopenharmony_ci Isolate* isolate, Handle<JSFunction> target, Handle<Object> new_target, 851cb0ef41Sopenharmony_ci Handle<Object> message, Handle<Object> options, FrameSkipMode mode, 861cb0ef41Sopenharmony_ci Handle<Object> caller, StackTraceCollection stack_trace_collection); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static MaybeHandle<String> ToString(Isolate* isolate, 891cb0ef41Sopenharmony_ci Handle<Object> recv); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci static Handle<JSObject> MakeGenericError( 921cb0ef41Sopenharmony_ci Isolate* isolate, Handle<JSFunction> constructor, MessageTemplate index, 931cb0ef41Sopenharmony_ci Handle<Object> arg0, Handle<Object> arg1, Handle<Object> arg2, 941cb0ef41Sopenharmony_ci FrameSkipMode mode); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci // Formats a textual stack trace from the given structured stack trace. 971cb0ef41Sopenharmony_ci // Note that this can call arbitrary JS code through Error.prepareStackTrace. 981cb0ef41Sopenharmony_ci static MaybeHandle<Object> FormatStackTrace(Isolate* isolate, 991cb0ef41Sopenharmony_ci Handle<JSObject> error, 1001cb0ef41Sopenharmony_ci Handle<Object> stack_trace); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci static Handle<JSObject> NewIteratorError(Isolate* isolate, 1031cb0ef41Sopenharmony_ci Handle<Object> source); 1041cb0ef41Sopenharmony_ci static Handle<JSObject> NewCalledNonCallableError(Isolate* isolate, 1051cb0ef41Sopenharmony_ci Handle<Object> source); 1061cb0ef41Sopenharmony_ci static Handle<JSObject> NewConstructedNonConstructable(Isolate* isolate, 1071cb0ef41Sopenharmony_ci Handle<Object> source); 1081cb0ef41Sopenharmony_ci // Returns the Exception sentinel. 1091cb0ef41Sopenharmony_ci static Object ThrowSpreadArgError(Isolate* isolate, MessageTemplate id, 1101cb0ef41Sopenharmony_ci Handle<Object> object); 1111cb0ef41Sopenharmony_ci // Returns the Exception sentinel. 1121cb0ef41Sopenharmony_ci static Object ThrowLoadFromNullOrUndefined(Isolate* isolate, 1131cb0ef41Sopenharmony_ci Handle<Object> object, 1141cb0ef41Sopenharmony_ci MaybeHandle<Object> key); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci static MaybeHandle<Object> GetFormattedStack(Isolate* isolate, 1171cb0ef41Sopenharmony_ci Handle<JSObject> error_object); 1181cb0ef41Sopenharmony_ci static void SetFormattedStack(Isolate* isolate, Handle<JSObject> error_object, 1191cb0ef41Sopenharmony_ci Handle<Object> formatted_stack); 1201cb0ef41Sopenharmony_ci}; 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ciclass MessageFormatter { 1231cb0ef41Sopenharmony_ci public: 1241cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static const char* TemplateString(MessageTemplate index); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static MaybeHandle<String> Format(Isolate* isolate, 1271cb0ef41Sopenharmony_ci MessageTemplate index, 1281cb0ef41Sopenharmony_ci Handle<String> arg0, 1291cb0ef41Sopenharmony_ci Handle<String> arg1, 1301cb0ef41Sopenharmony_ci Handle<String> arg2); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci static Handle<String> Format(Isolate* isolate, MessageTemplate index, 1331cb0ef41Sopenharmony_ci Handle<Object> arg0, 1341cb0ef41Sopenharmony_ci Handle<Object> arg1 = Handle<Object>(), 1351cb0ef41Sopenharmony_ci Handle<Object> arg2 = Handle<Object>()); 1361cb0ef41Sopenharmony_ci}; 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci// A message handler is a convenience interface for accessing the list 1391cb0ef41Sopenharmony_ci// of message listeners registered in an environment 1401cb0ef41Sopenharmony_ciclass MessageHandler { 1411cb0ef41Sopenharmony_ci public: 1421cb0ef41Sopenharmony_ci // Returns a message object for the API to use. 1431cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static Handle<JSMessageObject> MakeMessageObject( 1441cb0ef41Sopenharmony_ci Isolate* isolate, MessageTemplate type, const MessageLocation* location, 1451cb0ef41Sopenharmony_ci Handle<Object> argument, Handle<FixedArray> stack_frames); 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci // Report a formatted message (needs JS allocation). 1481cb0ef41Sopenharmony_ci V8_EXPORT_PRIVATE static void ReportMessage(Isolate* isolate, 1491cb0ef41Sopenharmony_ci const MessageLocation* loc, 1501cb0ef41Sopenharmony_ci Handle<JSMessageObject> message); 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci static void DefaultMessageReport(Isolate* isolate, const MessageLocation* loc, 1531cb0ef41Sopenharmony_ci Handle<Object> message_obj); 1541cb0ef41Sopenharmony_ci static Handle<String> GetMessage(Isolate* isolate, Handle<Object> data); 1551cb0ef41Sopenharmony_ci static std::unique_ptr<char[]> GetLocalizedMessage(Isolate* isolate, 1561cb0ef41Sopenharmony_ci Handle<Object> data); 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci private: 1591cb0ef41Sopenharmony_ci static void ReportMessageNoExceptions(Isolate* isolate, 1601cb0ef41Sopenharmony_ci const MessageLocation* loc, 1611cb0ef41Sopenharmony_ci Handle<Object> message_obj, 1621cb0ef41Sopenharmony_ci v8::Local<v8::Value> api_exception_obj); 1631cb0ef41Sopenharmony_ci}; 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci} // namespace internal 1661cb0ef41Sopenharmony_ci} // namespace v8 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci#endif // V8_EXECUTION_MESSAGES_H_ 169