blob: 8b81d21e0d3bef6934ae56c4983997cf5213ddfb [file] [log] [blame]
Ben Murdochf3b273f2017-01-17 12:11:28 +00001// 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
Rubin Xu7bc1b612021-02-16 09:38:50 +00005#ifndef V8_INSPECTOR_STRING_UTIL_H_
6#define V8_INSPECTOR_STRING_UTIL_H_
7
8#include <stdint.h>
Ben Murdochf3b273f2017-01-17 12:11:28 +00009
Ben Murdoch62ed6312017-06-06 11:06:27 +010010#include <memory>
11
Rubin Xu7bc1b612021-02-16 09:38:50 +000012#include "../../third_party/inspector_protocol/crdtp/protocol_core.h"
13#include "include/v8-inspector.h"
Ben Murdoch62ed6312017-06-06 11:06:27 +010014#include "src/base/logging.h"
Ben Murdochf3b273f2017-01-17 12:11:28 +000015#include "src/base/macros.h"
16#include "src/inspector/string-16.h"
17
Ben Murdochf3b273f2017-01-17 12:11:28 +000018namespace v8_inspector {
19
20namespace protocol {
21
22class Value;
23
24using String = v8_inspector::String16;
Ben Murdochf3b273f2017-01-17 12:11:28 +000025
26class StringUtil {
27 public:
Rubin Xu7bc1b612021-02-16 09:38:50 +000028 static String fromUTF8(const uint8_t* data, size_t length) {
29 return String16::fromUTF8(reinterpret_cast<const char*>(data), length);
Ben Murdochf3b273f2017-01-17 12:11:28 +000030 }
Rubin Xu7bc1b612021-02-16 09:38:50 +000031
32 static String fromUTF16LE(const uint16_t* data, size_t length) {
33 return String16::fromUTF16LE(data, length);
Ben Murdochf3b273f2017-01-17 12:11:28 +000034 }
Rubin Xu7bc1b612021-02-16 09:38:50 +000035
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();
Ben Murdoch62ed6312017-06-06 11:06:27 +010040 }
Rubin Xu7bc1b612021-02-16 09:38:50 +000041 static size_t CharacterCount(const String& s) { return s.length(); }
Ben Murdochf3b273f2017-01-17 12:11:28 +000042};
43
Rubin Xu7bc1b612021-02-16 09:38:50 +000044// 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};
Ben Murdochf3b273f2017-01-17 12:11:28 +000063} // namespace protocol
64
Ben Murdochf3b273f2017-01-17 12:11:28 +000065v8::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.
Rubin Xu7bc1b612021-02-16 09:38:50 +000070String16 toProtocolString(v8::Isolate*, v8::Local<v8::String>);
71String16 toProtocolStringWithTypeCheck(v8::Isolate*, v8::Local<v8::Value>);
Ben Murdochf3b273f2017-01-17 12:11:28 +000072String16 toString16(const StringView&);
73StringView toStringView(const String16&);
74bool stringViewStartsWith(const StringView&, const char*);
75
Rubin Xu7bc1b612021-02-16 09:38:50 +000076// Creates a string buffer instance which owns |str|, a 16 bit string.
77std::unique_ptr<StringBuffer> StringBufferFrom(String16 str);
Ben Murdochf3b273f2017-01-17 12:11:28 +000078
Rubin Xu7bc1b612021-02-16 09:38:50 +000079// Creates a string buffer instance which owns |str|, an 8 bit string.
80// 8 bit strings are used for LATIN1 text (which subsumes 7 bit ASCII, e.g.
81// our generated JSON), as well as for CBOR encoded binary messages.
82std::unique_ptr<StringBuffer> StringBufferFrom(std::vector<uint8_t> str);
Ben Murdochf3b273f2017-01-17 12:11:28 +000083
Rubin Xu7bc1b612021-02-16 09:38:50 +000084String16 stackTraceIdToString(uintptr_t id);
85
86} // namespace v8_inspector
87
88// See third_party/inspector_protocol/crdtp/serializer_traits.h.
89namespace v8_crdtp {
90
91template <>
92struct ProtocolTypeTraits<v8_inspector::String16> {
93 static bool Deserialize(DeserializerState* state,
94 v8_inspector::String16* value);
95 static void Serialize(const v8_inspector::String16& value,
96 std::vector<uint8_t>* bytes);
Ben Murdochf3b273f2017-01-17 12:11:28 +000097};
98
Rubin Xu7bc1b612021-02-16 09:38:50 +000099template <>
100struct ProtocolTypeTraits<v8_inspector::protocol::Binary> {
101 static bool Deserialize(DeserializerState* state,
102 v8_inspector::protocol::Binary* value);
103 static void Serialize(const v8_inspector::protocol::Binary& value,
104 std::vector<uint8_t>* bytes);
105};
Ben Murdochf3b273f2017-01-17 12:11:28 +0000106
Rubin Xu7bc1b612021-02-16 09:38:50 +0000107template <>
108struct SerializerTraits<v8_inspector::protocol::Binary> {
109 static void Serialize(const v8_inspector::protocol::Binary& binary,
110 std::vector<uint8_t>* out);
111};
112
113namespace detail {
114template <>
115struct MaybeTypedef<v8_inspector::String16> {
116 typedef ValueMaybe<v8_inspector::String16> type;
117};
118
119template <>
120struct MaybeTypedef<v8_inspector::protocol::Binary> {
121 typedef ValueMaybe<v8_inspector::protocol::Binary> type;
122};
123
124} // namespace detail
125
126} // namespace v8_crdtp
127
128#endif // V8_INSPECTOR_STRING_UTIL_H_