// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_CALL_SITE_INFO_H_
#define V8_OBJECTS_CALL_SITE_INFO_H_

#include <optional>

#include "src/base/bit-field.h"
#include "src/objects/objects-body-descriptors.h"
#include "src/objects/struct.h"
#include "src/objects/trusted-pointer.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8::internal {

class MessageLocation;
class WasmInstanceObject;
class StructBodyDescriptor;

V8_OBJECT class CallSiteInfo : public Struct {
 public:
  using IsWasmBit = base::BitField<bool, 0, 1, uint32_t>;
  using IsAsmJsWasmBit = IsWasmBit::Next<bool, 1>;
  using IsStrictBit = IsAsmJsWasmBit::Next<bool, 1>;
  using IsConstructorBit = IsStrictBit::Next<bool, 1>;
  using IsAsmJsAtNumberConversionBit = IsConstructorBit::Next<bool, 1>;
  using IsAsyncBit = IsAsmJsAtNumberConversionBit::Next<bool, 1>;
  using IsBuiltinBit = IsAsyncBit::Next<bool, 1>;
  using IsSourcePositionComputedBit = IsBuiltinBit::Next<bool, 1>;
  using IsDeferredBaselineFrameBit = IsSourcePositionComputedBit::Next<bool, 1>;
  enum Flag : uint32_t {
    kNone = 0,
    kIsWasm = IsWasmBit::kMask,
    kIsAsmJsWasm = IsAsmJsWasmBit::kMask,
    kIsStrict = IsStrictBit::kMask,
    kIsConstructor = IsConstructorBit::kMask,
    kIsAsmJsAtNumberConversion = IsAsmJsAtNumberConversionBit::kMask,
    kIsAsync = IsAsyncBit::kMask,
    kIsBuiltin = IsBuiltinBit::kMask,
    kIsSourcePositionComputed = IsSourcePositionComputedBit::kMask,
    kIsDeferredBaselineFrame = IsDeferredBaselineFrameBit::kMask,
  };
  using Flags = base::Flags<Flag>;
  static constexpr int kFlagCount = 9;

#if V8_ENABLE_WEBASSEMBLY
  inline bool IsWasm() const;
  inline bool IsAsmJsWasm() const;
  inline bool IsAsmJsAtNumberConversion() const;
#if V8_ENABLE_DRUMBRAKE
  inline bool IsWasmInterpretedFrame() const;
#endif  // V8_ENABLE_DRUMBRAKE
  inline bool IsBuiltin() const;
#endif  // V8_ENABLE_WEBASSEMBLY

  inline bool IsStrict() const;
  inline bool IsConstructor() const;
  inline bool IsAsync() const;
  bool IsEval() const;
  bool IsUserJavaScript() const;
  bool IsSubjectToDebugging() const;
  bool IsMethodCall() const;
  bool IsToplevel() const;
  bool IsPromiseAll() const;
  bool IsPromiseAllSettled() const;
  bool IsPromiseAny() const;
  bool IsNative() const;

  inline Tagged<HeapObject> code_object(IsolateForSandbox isolate) const;
  inline void set_code_object(
      Tagged<Union<Code, BytecodeArray, Undefined>> code,
      WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  inline Tagged<JSAny> receiver_or_instance() const;
  inline void set_receiver_or_instance(
      Tagged<JSAny> value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  inline Tagged<Union<JSFunction, Smi>> function() const;
  inline void set_function(Tagged<Union<JSFunction, Smi>> value,
                           WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  inline int code_offset_or_source_position() const;
  inline void set_code_offset_or_source_position(
      int value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  inline int flags() const;
  inline void set_flags(int value);

  // Dispatched behavior.
  DECL_VERIFIER(CallSiteInfo)
  DECL_PRINTER(CallSiteInfo)

  // Used to signal that the requested field is unknown.
  static constexpr int kUnknown = kNoSourcePosition;

  // For delayed CallSiteInfo creation (storing the raw data for several
  // CallSiteInfos in a FixedArray).
  enum Fields { kCode = 0, kReceiver, kFunction, kOffset, kFlags, kCount };

  // Deferred flags: defined in the Torque bitfield, but only meaningful in
  // raw capture arrays.  Stripped before creating actual CallSiteInfo objects.
  static constexpr int kDeferredFlagsMask = kIsDeferredBaselineFrame;

  static DirectHandle<CallSiteInfo> ConstructFromRawData(
      Isolate* isolate, DirectHandle<FixedArray> frames, int index);

  // Expand a raw stack-capture array that may contain deferred entries
  // (optimized/baseline frames that were not Summarize'd at capture time)
  // into a fully-resolved raw array where every entry corresponds to one
  // logical JS frame.  Non-deferred entries are copied as-is.
  static Handle<FixedArray> ExpandDeferredFrames(Isolate* isolate,
                                                 Handle<FixedArray> raw_data);

  V8_EXPORT_PRIVATE static int GetLineNumber(DirectHandle<CallSiteInfo> info);
  V8_EXPORT_PRIVATE static int GetColumnNumber(DirectHandle<CallSiteInfo> info);

  static int GetEnclosingLineNumber(DirectHandle<CallSiteInfo> info);
  static int GetEnclosingColumnNumber(DirectHandle<CallSiteInfo> info);

  // Returns the script ID if one is attached,
  // Message::kNoScriptIdInfo otherwise.
  static MaybeDirectHandle<Script> GetScript(Isolate* isolate,
                                             DirectHandle<CallSiteInfo> info);
  int GetScriptId() const;
  Tagged<Object> GetScriptName() const;
  Tagged<Object> GetScriptNameOrSourceURL() const;
  Tagged<Object> GetScriptSource() const;
  Tagged<Object> GetScriptSourceMappingURL() const;

  static Handle<PrimitiveHeapObject> GetEvalOrigin(
      DirectHandle<CallSiteInfo> info);
  V8_EXPORT_PRIVATE static DirectHandle<PrimitiveHeapObject> GetFunctionName(
      DirectHandle<CallSiteInfo> info);
  static DirectHandle<String> GetFunctionDebugName(
      DirectHandle<CallSiteInfo> info);
  static DirectHandle<Object> GetMethodName(DirectHandle<CallSiteInfo> info);
  static DirectHandle<String> GetScriptHash(DirectHandle<CallSiteInfo> info);
  static DirectHandle<Object> GetTypeName(DirectHandle<CallSiteInfo> info);

#if V8_ENABLE_WEBASSEMBLY
  // These methods are only valid for Wasm and asm.js Wasm frames.
  uint32_t GetWasmFunctionIndex() const;
  Tagged<WasmInstanceObject> GetWasmInstance() const;
  static DirectHandle<Object> GetWasmModuleName(
      DirectHandle<CallSiteInfo> info);
#endif  // V8_ENABLE_WEBASSEMBLY

  // Returns the 0-based source position, which is the offset into the
  // Script in case of JavaScript and Asm.js, and the wire byte offset
  // in the module in case of actual Wasm. In case of async promise
  // combinator frames, this returns the index of the promise.
  static int GetSourcePosition(DirectHandle<CallSiteInfo> info);

  // Attempts to fill the |location| based on the |info|, and avoids
  // triggering source position table building for JavaScript frames.
  static bool ComputeLocation(DirectHandle<CallSiteInfo> info,
                              MessageLocation* location);

 private:
  static int ComputeSourcePosition(DirectHandle<CallSiteInfo> info, int offset);

  std::optional<Tagged<Script>> GetScript() const;
  Tagged<SharedFunctionInfo> GetSharedFunctionInfo() const;

  friend class Factory;
  friend class TorqueGeneratedCallSiteInfoAsserts;
  friend struct ObjectTraits<CallSiteInfo>;

  static constexpr IndirectPointerTagRange kCodeObjectTagRange =
      IndirectPointerTagRange(kBytecodeArrayIndirectPointerTag,
                              kCodeIndirectPointerTag);
  static_assert(kCodeObjectTagRange.Size() == 2);

  TrustedPointerMember<Union<Code, BytecodeArray>, kCodeObjectTagRange>
      code_object_;
  TaggedMember<JSAny> receiver_or_instance_;
  TaggedMember<Union<JSFunction, Smi>> function_;
  TaggedMember<Smi> code_offset_or_source_position_;
  TaggedMember<Smi> flags_;
} V8_OBJECT_END;

template <>
struct ObjectTraits<CallSiteInfo> {
  using BodyDescriptor = StackedBodyDescriptor<
      FixedBodyDescriptor<offsetof(CallSiteInfo, receiver_or_instance_),
                          sizeof(CallSiteInfo), sizeof(CallSiteInfo)>,
      WithStrongTrustedPointer<offsetof(CallSiteInfo, code_object_),
                               CallSiteInfo::kCodeObjectTagRange>>;
};

class IncrementalStringBuilder;
void SerializeCallSiteInfo(Isolate* isolate, DirectHandle<CallSiteInfo> frame,
                           IncrementalStringBuilder* builder);
V8_EXPORT_PRIVATE
MaybeDirectHandle<String> SerializeCallSiteInfo(
    Isolate* isolate, DirectHandle<CallSiteInfo> frame);

}  // namespace v8::internal

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_CALL_SITE_INFO_H_
