OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/inspector/StringUtil.h" | 5 #include "src/inspector/StringUtil.h" |
6 | 6 |
7 #include "src/inspector/protocol/Protocol.h" | 7 #include "src/inspector/protocol/Protocol.h" |
8 | 8 |
9 #include <limits> | |
10 | |
11 namespace v8_inspector { | 9 namespace v8_inspector { |
12 | 10 |
13 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) { | 11 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) { |
14 if (string.isEmpty()) return v8::String::Empty(isolate); | 12 if (string.isEmpty()) return v8::String::Empty(isolate); |
15 DCHECK(string.length() < v8::String::kMaxLength); | |
16 return v8::String::NewFromTwoByte( | 13 return v8::String::NewFromTwoByte( |
17 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), | 14 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), |
18 v8::NewStringType::kNormal, static_cast<int>(string.length())) | 15 v8::NewStringType::kNormal, string.length()) |
19 .ToLocalChecked(); | 16 .ToLocalChecked(); |
20 } | 17 } |
21 | 18 |
22 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, | 19 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, |
23 const String16& string) { | 20 const String16& string) { |
24 if (string.isEmpty()) return v8::String::Empty(isolate); | 21 if (string.isEmpty()) return v8::String::Empty(isolate); |
25 DCHECK(string.length() < v8::String::kMaxLength); | |
26 return v8::String::NewFromTwoByte( | 22 return v8::String::NewFromTwoByte( |
27 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), | 23 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), |
28 v8::NewStringType::kInternalized, | 24 v8::NewStringType::kInternalized, string.length()) |
29 static_cast<int>(string.length())) | |
30 .ToLocalChecked(); | 25 .ToLocalChecked(); |
31 } | 26 } |
32 | 27 |
33 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, | 28 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, |
34 const char* str) { | 29 const char* str) { |
35 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized) | 30 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalized) |
36 .ToLocalChecked(); | 31 .ToLocalChecked(); |
37 } | 32 } |
38 | 33 |
39 v8::Local<v8::String> toV8String(v8::Isolate* isolate, | 34 v8::Local<v8::String> toV8String(v8::Isolate* isolate, |
40 const StringView& string) { | 35 const StringView& string) { |
41 if (!string.length()) return v8::String::Empty(isolate); | 36 if (!string.length()) return v8::String::Empty(isolate); |
42 DCHECK(string.length() < v8::String::kMaxLength); | |
43 if (string.is8Bit()) | 37 if (string.is8Bit()) |
44 return v8::String::NewFromOneByte( | 38 return v8::String::NewFromOneByte( |
45 isolate, reinterpret_cast<const uint8_t*>(string.characters8()), | 39 isolate, reinterpret_cast<const uint8_t*>(string.characters8()), |
46 v8::NewStringType::kNormal, static_cast<int>(string.length())) | 40 v8::NewStringType::kNormal, string.length()) |
47 .ToLocalChecked(); | 41 .ToLocalChecked(); |
48 return v8::String::NewFromTwoByte( | 42 return v8::String::NewFromTwoByte( |
49 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), | 43 isolate, reinterpret_cast<const uint16_t*>(string.characters16()), |
50 v8::NewStringType::kNormal, static_cast<int>(string.length())) | 44 v8::NewStringType::kNormal, string.length()) |
51 .ToLocalChecked(); | 45 .ToLocalChecked(); |
52 } | 46 } |
53 | 47 |
54 String16 toProtocolString(v8::Local<v8::String> value) { | 48 String16 toProtocolString(v8::Local<v8::String> value) { |
55 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) | 49 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) |
56 return String16(); | 50 return String16(); |
57 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]); | 51 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]); |
58 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length()); | 52 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length()); |
59 return String16(buffer.get(), value->Length()); | 53 return String16(buffer.get(), value->Length()); |
60 } | 54 } |
(...skipping 29 matching lines...) Expand all Loading... |
90 if (string.characters16()[i] != prefix[j]) return false; | 84 if (string.characters16()[i] != prefix[j]) return false; |
91 } | 85 } |
92 } | 86 } |
93 return true; | 87 return true; |
94 } | 88 } |
95 | 89 |
96 namespace protocol { | 90 namespace protocol { |
97 | 91 |
98 std::unique_ptr<protocol::Value> parseJSON(const StringView& string) { | 92 std::unique_ptr<protocol::Value> parseJSON(const StringView& string) { |
99 if (!string.length()) return nullptr; | 93 if (!string.length()) return nullptr; |
100 DCHECK(string.length() <= std::numeric_limits<int>::max()); | 94 if (string.is8Bit()) |
101 if (string.is8Bit()) { | 95 return protocol::parseJSON(string.characters8(), string.length()); |
102 return protocol::parseJSON(string.characters8(), | 96 return protocol::parseJSON(string.characters16(), string.length()); |
103 static_cast<int>(string.length())); | |
104 } | |
105 return protocol::parseJSON(string.characters16(), | |
106 static_cast<int>(string.length())); | |
107 } | 97 } |
108 | 98 |
109 std::unique_ptr<protocol::Value> parseJSON(const String16& string) { | 99 std::unique_ptr<protocol::Value> parseJSON(const String16& string) { |
110 if (!string.length()) return nullptr; | 100 if (!string.length()) return nullptr; |
111 DCHECK(string.length() <= std::numeric_limits<int>::max()); | 101 return protocol::parseJSON(string.characters16(), string.length()); |
112 return protocol::parseJSON(string.characters16(), | |
113 static_cast<int>(string.length())); | |
114 } | 102 } |
115 | 103 |
116 } // namespace protocol | 104 } // namespace protocol |
117 | 105 |
118 std::unique_ptr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context, | 106 std::unique_ptr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context, |
119 v8::Local<v8::Value> value, | 107 v8::Local<v8::Value> value, |
120 int maxDepth) { | 108 int maxDepth) { |
121 if (value.IsEmpty()) { | 109 if (value.IsEmpty()) { |
122 UNREACHABLE(); | 110 UNREACHABLE(); |
123 return nullptr; | 111 return nullptr; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) { | 186 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string) { |
199 return wrapUnique(new StringBufferImpl(string)); | 187 return wrapUnique(new StringBufferImpl(string)); |
200 } | 188 } |
201 | 189 |
202 StringBufferImpl::StringBufferImpl(String16& string) { | 190 StringBufferImpl::StringBufferImpl(String16& string) { |
203 m_owner.swap(string); | 191 m_owner.swap(string); |
204 m_string = toStringView(m_owner); | 192 m_string = toStringView(m_owner); |
205 } | 193 } |
206 | 194 |
207 } // namespace v8_inspector | 195 } // namespace v8_inspector |
OLD | NEW |