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 18namespace v8_inspector { 19 20namespace protocol { 21 22class Value; 23 24using String = v8_inspector::String16; 25 26class StringUtil { 27 public: 28 static String fromUTF8(const uint8_t* data, size_t length) { 29 return String16::fromUTF8(reinterpret_cast<const char*>(data), length); 30 } 31 32 static String fromUTF16LE(const uint16_t* data, size_t length) { 33 return String16::fromUTF16LE(data, length); 34 } 35 36 static const uint8_t* CharactersLatin1(const String& s) { return nullptr; } 37 static const uint8_t* CharactersUTF8(const String& s) { return nullptr; } 38 static const uint16_t* CharactersUTF16(const String& s) { 39 return s.characters16(); 40 } 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. 45class V8_EXPORT Binary { 46 public: 47 Binary() = default; 48 49 const uint8_t* data() const { return bytes_->data(); } 50 size_t size() const { return bytes_->size(); } 51 String toBase64() const; 52 static Binary fromBase64(const String& base64, bool* success); 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 60 explicit Binary(std::shared_ptr<std::vector<uint8_t>> bytes) 61 : bytes_(bytes) {} 62}; 63} // namespace protocol 64 65v8::Local<v8::String> toV8String(v8::Isolate*, const String16&); 66v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const String16&); 67v8::Local<v8::String> toV8StringInternalized(v8::Isolate*, const char*); 68v8::Local<v8::String> toV8String(v8::Isolate*, const StringView&); 69// TODO(dgozman): rename to toString16. 70String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>); 71String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>); 72String16 toString16(const StringView&); 73StringView toStringView(const String16&); 74template <size_t N> 75StringView toStringView(const char* str[N]) { 76 return StringView(reinterpret_cast<const uint8_t*>(str), N); 77} 78bool stringViewStartsWith(const StringView&, const char*); 79 80// Creates a string buffer instance which owns |str|, a 16 bit string. 81std::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. 86std::unique_ptr<StringBuffer> StringBufferFrom(std::vector<uint8_t> str); 87 88String16 stackTraceIdToString(uintptr_t id); 89 90} // namespace v8_inspector 91 92// See third_party/inspector_protocol/crdtp/serializer_traits.h. 93namespace v8_crdtp { 94 95template <> 96struct 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 103template <> 104struct 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 111namespace detail { 112template <> 113struct MaybeTypedef<v8_inspector::String16> { 114 typedef ValueMaybe<v8_inspector::String16> type; 115}; 116 117template <> 118struct 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