Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/StringUtil.cpp

Issue 2295913003: [DevTools] Switch from platform/v8_inspector to v8/v8-inspector.h. (Closed)
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium 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 #include "platform/v8_inspector/StringUtil.h"
6
7 #include "platform/v8_inspector/protocol/Protocol.h"
8
9 namespace v8_inspector {
10
11 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string)
12 {
13 if (string.isEmpty())
14 return v8::String::Empty(isolate);
15 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe cked();
16 }
17
18 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String1 6& string)
19 {
20 if (string.isEmpty())
21 return v8::String::Empty(isolate);
22 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kInternalized, string.length()).ToLo calChecked();
23 }
24
25 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const char* s tr)
26 {
27 return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kInternalize d).ToLocalChecked();
28 }
29
30 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const StringView& string)
31 {
32 if (!string.length())
33 return v8::String::Empty(isolate);
34 if (string.is8Bit())
35 return v8::String::NewFromOneByte(isolate, reinterpret_cast<const uint8_ t*>(string.characters8()), v8::NewStringType::kNormal, string.length()).ToLocalC hecked();
36 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe cked();
37 }
38
39 String16 toProtocolString(v8::Local<v8::String> value)
40 {
41 if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
42 return String16();
43 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
44 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length());
45 return String16(buffer.get(), value->Length());
46 }
47
48 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value)
49 {
50 if (value.IsEmpty() || !value->IsString())
51 return String16();
52 return toProtocolString(value.As<v8::String>());
53 }
54
55 String16 toString16(const StringView& string)
56 {
57 if (!string.length())
58 return String16();
59 if (string.is8Bit())
60 return String16(reinterpret_cast<const char*>(string.characters8()), str ing.length());
61 return String16(reinterpret_cast<const UChar*>(string.characters16()), strin g.length());
62 }
63
64 StringView toStringView(const String16& string)
65 {
66 if (string.isEmpty())
67 return StringView();
68 return StringView(reinterpret_cast<const uint16_t*>(string.characters16()), string.length());
69 }
70
71 bool stringViewStartsWith(const StringView& string, const char* prefix)
72 {
73 if (!string.length())
74 return !(*prefix);
75 if (string.is8Bit()) {
76 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
77 if (string.characters8()[i] != prefix[j])
78 return false;
79 }
80 } else {
81 for (size_t i = 0, j = 0; prefix[j] && i < string.length(); ++i, ++j) {
82 if (string.characters16()[i] != prefix[j])
83 return false;
84 }
85 }
86 return true;
87 }
88
89 namespace protocol {
90
91 std::unique_ptr<protocol::Value> parseJSON(const StringView& string)
92 {
93 if (!string.length())
94 return nullptr;
95 if (string.is8Bit())
96 return protocol::parseJSON(string.characters8(), string.length());
97 return protocol::parseJSON(string.characters16(), string.length());
98 }
99
100 std::unique_ptr<protocol::Value> parseJSON(const String16& string)
101 {
102 if (!string.length())
103 return nullptr;
104 return protocol::parseJSON(string.characters16(), string.length());
105 }
106
107 } // namespace protocol
108
109 std::unique_ptr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context, v8::Local<v8::Value> value, int maxDepth)
110 {
111 if (value.IsEmpty()) {
112 NOTREACHED();
113 return nullptr;
114 }
115
116 if (!maxDepth)
117 return nullptr;
118 maxDepth--;
119
120 if (value->IsNull() || value->IsUndefined())
121 return protocol::Value::null();
122 if (value->IsBoolean())
123 return protocol::FundamentalValue::create(value.As<v8::Boolean>()->Value ());
124 if (value->IsNumber()) {
125 double doubleValue = value.As<v8::Number>()->Value();
126 int intValue = static_cast<int>(doubleValue);
127 if (intValue == doubleValue)
128 return protocol::FundamentalValue::create(intValue);
129 return protocol::FundamentalValue::create(doubleValue);
130 }
131 if (value->IsString())
132 return protocol::StringValue::create(toProtocolString(value.As<v8::Strin g>()));
133 if (value->IsArray()) {
134 v8::Local<v8::Array> array = value.As<v8::Array>();
135 std::unique_ptr<protocol::ListValue> inspectorArray = protocol::ListValu e::create();
136 uint32_t length = array->Length();
137 for (uint32_t i = 0; i < length; i++) {
138 v8::Local<v8::Value> value;
139 if (!array->Get(context, i).ToLocal(&value))
140 return nullptr;
141 std::unique_ptr<protocol::Value> element = toProtocolValue(context, value, maxDepth);
142 if (!element)
143 return nullptr;
144 inspectorArray->pushValue(std::move(element));
145 }
146 return std::move(inspectorArray);
147 }
148 if (value->IsObject()) {
149 std::unique_ptr<protocol::DictionaryValue> jsonObject = protocol::Dictio naryValue::create();
150 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
151 v8::Local<v8::Array> propertyNames;
152 if (!object->GetPropertyNames(context).ToLocal(&propertyNames))
153 return nullptr;
154 uint32_t length = propertyNames->Length();
155 for (uint32_t i = 0; i < length; i++) {
156 v8::Local<v8::Value> name;
157 if (!propertyNames->Get(context, i).ToLocal(&name))
158 return nullptr;
159 // FIXME(yurys): v8::Object should support GetOwnPropertyNames
160 if (name->IsString()) {
161 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedPrope rty(context, v8::Local<v8::String>::Cast(name));
162 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.From Just())
163 continue;
164 }
165 v8::Local<v8::String> propertyName;
166 if (!name->ToString(context).ToLocal(&propertyName))
167 continue;
168 v8::Local<v8::Value> property;
169 if (!object->Get(context, name).ToLocal(&property))
170 return nullptr;
171 std::unique_ptr<protocol::Value> propertyValue = toProtocolValue(con text, property, maxDepth);
172 if (!propertyValue)
173 return nullptr;
174 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue));
175 }
176 return std::move(jsonObject);
177 }
178 NOTREACHED();
179 return nullptr;
180 }
181
182 // static
183 std::unique_ptr<StringBuffer> StringBuffer::create(const StringView& string)
184 {
185 String16 owner = toString16(string);
186 return StringBufferImpl::adopt(owner);
187 }
188
189 // static
190 std::unique_ptr<StringBufferImpl> StringBufferImpl::adopt(String16& string)
191 {
192 return wrapUnique(new StringBufferImpl(string));
193 }
194
195 StringBufferImpl::StringBufferImpl(String16& string)
196 {
197 m_owner.swap(string);
198 m_string = toStringView(m_owner);
199 }
200
201 } // namespace v8_inspector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698