Home | History | Annotate | Download | only in src
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_STRING_STREAM_H_
      6 #define V8_STRING_STREAM_H_
      7 
      8 #include "src/allocation.h"
      9 #include "src/base/smart-pointers.h"
     10 #include "src/handles.h"
     11 #include "src/vector.h"
     12 
     13 namespace v8 {
     14 namespace internal {
     15 
     16 // Forward declarations.
     17 class ByteArray;
     18 
     19 class StringAllocator {
     20  public:
     21   virtual ~StringAllocator() { }
     22   // Allocate a number of bytes.
     23   virtual char* allocate(unsigned bytes) = 0;
     24   // Allocate a larger number of bytes and copy the old buffer to the new one.
     25   // bytes is an input and output parameter passing the old size of the buffer
     26   // and returning the new size.  If allocation fails then we return the old
     27   // buffer and do not increase the size.
     28   virtual char* grow(unsigned* bytes) = 0;
     29 };
     30 
     31 
     32 // Normal allocator uses new[] and delete[].
     33 class HeapStringAllocator final : public StringAllocator {
     34  public:
     35   ~HeapStringAllocator() { DeleteArray(space_); }
     36   char* allocate(unsigned bytes) override;
     37   char* grow(unsigned* bytes) override;
     38 
     39  private:
     40   char* space_;
     41 };
     42 
     43 
     44 class FixedStringAllocator final : public StringAllocator {
     45  public:
     46   FixedStringAllocator(char* buffer, unsigned length)
     47       : buffer_(buffer), length_(length) {}
     48   ~FixedStringAllocator() override{};
     49   char* allocate(unsigned bytes) override;
     50   char* grow(unsigned* bytes) override;
     51 
     52  private:
     53   char* buffer_;
     54   unsigned length_;
     55   DISALLOW_COPY_AND_ASSIGN(FixedStringAllocator);
     56 };
     57 
     58 
     59 class FmtElm final {
     60  public:
     61   FmtElm(int value) : type_(INT) {  // NOLINT
     62     data_.u_int_ = value;
     63   }
     64   explicit FmtElm(double value) : type_(DOUBLE) {
     65     data_.u_double_ = value;
     66   }
     67   FmtElm(const char* value) : type_(C_STR) {  // NOLINT
     68     data_.u_c_str_ = value;
     69   }
     70   FmtElm(const Vector<const uc16>& value) : type_(LC_STR) {  // NOLINT
     71     data_.u_lc_str_ = &value;
     72   }
     73   FmtElm(Object* value) : type_(OBJ) {  // NOLINT
     74     data_.u_obj_ = value;
     75   }
     76   FmtElm(Handle<Object> value) : type_(HANDLE) {  // NOLINT
     77     data_.u_handle_ = value.location();
     78   }
     79   FmtElm(void* value) : type_(POINTER) {  // NOLINT
     80     data_.u_pointer_ = value;
     81   }
     82 
     83  private:
     84   friend class StringStream;
     85   enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER };
     86   Type type_;
     87   union {
     88     int u_int_;
     89     double u_double_;
     90     const char* u_c_str_;
     91     const Vector<const uc16>* u_lc_str_;
     92     Object* u_obj_;
     93     Object** u_handle_;
     94     void* u_pointer_;
     95   } data_;
     96 };
     97 
     98 
     99 class StringStream final {
    100  public:
    101   enum ObjectPrintMode { kPrintObjectConcise, kPrintObjectVerbose };
    102   StringStream(StringAllocator* allocator,
    103                ObjectPrintMode object_print_mode = kPrintObjectVerbose)
    104       : allocator_(allocator),
    105         object_print_mode_(object_print_mode),
    106         capacity_(kInitialCapacity),
    107         length_(0),
    108         buffer_(allocator_->allocate(kInitialCapacity)) {
    109     buffer_[0] = 0;
    110   }
    111 
    112   bool Put(char c);
    113   bool Put(String* str);
    114   bool Put(String* str, int start, int end);
    115   void Add(Vector<const char> format, Vector<FmtElm> elms);
    116   void Add(const char* format);
    117   void Add(Vector<const char> format);
    118   void Add(const char* format, FmtElm arg0);
    119   void Add(const char* format, FmtElm arg0, FmtElm arg1);
    120   void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2);
    121   void Add(const char* format,
    122            FmtElm arg0,
    123            FmtElm arg1,
    124            FmtElm arg2,
    125            FmtElm arg3);
    126   void Add(const char* format,
    127            FmtElm arg0,
    128            FmtElm arg1,
    129            FmtElm arg2,
    130            FmtElm arg3,
    131            FmtElm arg4);
    132 
    133   // Getting the message out.
    134   void OutputToFile(FILE* out);
    135   void OutputToStdOut() { OutputToFile(stdout); }
    136   void Log(Isolate* isolate);
    137   Handle<String> ToString(Isolate* isolate);
    138   base::SmartArrayPointer<const char> ToCString() const;
    139   int length() const { return length_; }
    140 
    141   // Object printing support.
    142   void PrintName(Object* o);
    143   void PrintFixedArray(FixedArray* array, unsigned int limit);
    144   void PrintByteArray(ByteArray* ba);
    145   void PrintUsingMap(JSObject* js_object);
    146   void PrintPrototype(JSFunction* fun, Object* receiver);
    147   void PrintSecurityTokenIfChanged(Object* function);
    148   // NOTE: Returns the code in the output parameter.
    149   void PrintFunction(Object* function, Object* receiver, Code** code);
    150 
    151   // Reset the stream.
    152   void Reset() {
    153     length_ = 0;
    154     buffer_[0] = 0;
    155   }
    156 
    157   // Mentioned object cache support.
    158   void PrintMentionedObjectCache(Isolate* isolate);
    159   static void ClearMentionedObjectCache(Isolate* isolate);
    160 #ifdef DEBUG
    161   bool IsMentionedObjectCacheClear(Isolate* isolate);
    162 #endif
    163 
    164   static const int kInitialCapacity = 16;
    165 
    166  private:
    167   void PrintObject(Object* obj);
    168 
    169   StringAllocator* allocator_;
    170   ObjectPrintMode object_print_mode_;
    171   unsigned capacity_;
    172   unsigned length_;  // does not include terminating 0-character
    173   char* buffer_;
    174 
    175   bool full() const { return (capacity_ - length_) == 1; }
    176   int space() const { return capacity_ - length_; }
    177 
    178   DISALLOW_IMPLICIT_CONSTRUCTORS(StringStream);
    179 };
    180 
    181 }  // namespace internal
    182 }  // namespace v8
    183 
    184 #endif  // V8_STRING_STREAM_H_
    185