• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_INSPECTOR_STRING_UTIL_H_
6 #define V8_INSPECTOR_STRING_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "../../third_party/inspector_protocol/crdtp/protocol_core.h"
13 #include "include/v8-inspector.h"
14 #include "src/base/logging.h"
15 #include "src/base/macros.h"
16 #include "src/inspector/string-16.h"
17 
18 namespace v8_inspector {
19 
20 namespace protocol {
21 
22 class Value;
23 
24 using String = v8_inspector::String16;
25 
26 class StringUtil {
27  public:
fromUTF8(const uint8_t * data,size_t length)28   static String fromUTF8(const uint8_t* data, size_t length) {
29     return String16::fromUTF8(reinterpret_cast<const char*>(data), length);
30   }
31 
fromUTF16LE(const uint16_t * data,size_t length)32   static String fromUTF16LE(const uint16_t* data, size_t length) {
33     return String16::fromUTF16LE(data, length);
34   }
35 
CharactersLatin1(const String & s)36   static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }
CharactersUTF8(const String & s)37   static const uint8_t* CharactersUTF8(const String& s) { return nullptr; }
CharactersUTF16(const String & s)38   static const uint16_t* CharactersUTF16(const String& s) {
39     return s.characters16();
40   }
CharacterCount(const String & s)41   static size_t CharacterCount(const String& s) { return s.length(); }
42 };
43 
44 // A read-only sequence of uninterpreted bytes with reference-counted storage.
45 class V8_EXPORT Binary {
46  public:
47   Binary() = default;
48 
data()49   const uint8_t* data() const { return bytes_->data(); }
size()50   size_t size() const { return bytes_->size(); }
51   String toBase64() const;
52   static Binary fromBase64(const String& base64, bool* success);
fromSpan(const uint8_t * data,size_t size)53   static Binary fromSpan(const uint8_t* data, size_t size) {
54     return Binary(std::make_shared<std::vector<uint8_t>>(data, data + size));
55   }
56 
57  private:
58   std::shared_ptr<std::vector<uint8_t>> bytes_;
59 
Binary(std::shared_ptr<std::vector<uint8_t>> bytes)60   explicit Binary(std::shared_ptr<std::vector<uint8_t>> bytes)
61       : bytes_(bytes) {}
62 };
63 }  // namespace protocol
64 
65 v8::Local<v8::String> toV8String(v8::Isolate*, const String16&);
66 v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&);
67 v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*);
68 v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&);
69 // TODO(dgozman): rename to toString16.
70 String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>);
71 String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>);
72 String16 toString16(const StringView&);
73 StringView toStringView(const String16&);
74 template <size_t N>
toStringView(const char * str[N])75 StringView toStringView(const char* str[N]) {
76   return StringView(reinterpret_cast<const uint8_t*>(str), N);
77 }
78 bool stringViewStartsWith(const StringView&, const char*);
79 
80 // Creates a string buffer instance which owns |str|, a 16 bit string.
81 std::unique_ptr<StringBuffer> StringBufferFrom(String16 str);
82 
83 // Creates a string buffer instance which owns |str|, an 8 bit string.
84 // 8 bit strings are used for LATIN1 text (which subsumes 7 bit ASCII, e.g.
85 // our generated JSON), as well as for CBOR encoded binary messages.
86 std::unique_ptr<StringBuffer> StringBufferFrom(std::vector<uint8_t> str);
87 
88 String16 stackTraceIdToString(uintptr_t id);
89 
90 }  // namespace v8_inspector
91 
92 // See third_party/inspector_protocol/crdtp/serializer_traits.h.
93 namespace v8_crdtp {
94 
95 template <>
96 struct ProtocolTypeTraits<v8_inspector::String16> {
97   static bool Deserialize(DeserializerState* state,
98                           v8_inspector::String16* value);
99   static void Serialize(const v8_inspector::String16& value,
100                         std::vector<uint8_t>* bytes);
101 };
102 
103 template <>
104 struct ProtocolTypeTraits<v8_inspector::protocol::Binary> {
105   static bool Deserialize(DeserializerState* state,
106                           v8_inspector::protocol::Binary* value);
107   static void Serialize(const v8_inspector::protocol::Binary& value,
108                         std::vector<uint8_t>* bytes);
109 };
110 
111 namespace detail {
112 template <>
113 struct MaybeTypedef<v8_inspector::String16> {
114   typedef ValueMaybe<v8_inspector::String16> type;
115 };
116 
117 template <>
118 struct MaybeTypedef<v8_inspector::protocol::Binary> {
119   typedef ValueMaybe<v8_inspector::protocol::Binary> type;
120 };
121 
122 }  // namespace detail
123 
124 }  // namespace v8_crdtp
125 
126 #endif  // V8_INSPECTOR_STRING_UTIL_H_
127