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

Side by Side Diff: Source/bindings/core/v8/Dictionary.cpp

Issue 964553003: bindings: Use V8 MaybeLocal<> APIs in Dictionary class (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 9 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 100
101 bool Dictionary::isUndefinedOrNull() const 101 bool Dictionary::isUndefinedOrNull() const
102 { 102 {
103 if (m_options.IsEmpty()) 103 if (m_options.IsEmpty())
104 return true; 104 return true;
105 return blink::isUndefinedOrNull(m_options); 105 return blink::isUndefinedOrNull(m_options);
106 } 106 }
107 107
108 bool Dictionary::hasProperty(const String& key) const 108 bool Dictionary::hasProperty(const String& key) const
109 { 109 {
110 if (isUndefinedOrNull()) 110 v8::Local<v8::Object> object;
111 if (!toObject(&object))
111 return false; 112 return false;
112 v8::Local<v8::Object> options = m_options->ToObject(m_isolate);
113 ASSERT(!options.IsEmpty());
114 113
115 ASSERT(m_isolate); 114 ASSERT(m_isolate);
116 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 115 ASSERT(m_isolate == v8::Isolate::GetCurrent());
117 ASSERT(m_exceptionState); 116 ASSERT(m_exceptionState);
118 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); 117 v8::Local<v8::String> v8Key = v8String(m_isolate, key);
119 return v8CallBoolean(options->Has(v8Context(), v8Key)); 118 return v8CallBoolean(object->Has(v8Context(), v8Key));
120 } 119 }
121 120
122 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const 121 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const
123 { 122 {
124 if (isUndefinedOrNull()) 123 v8::Local<v8::Object> object;
124 if (!toObject(&object))
125 return false; 125 return false;
126 v8::Local<v8::Object> options = m_options->ToObject(m_isolate);
127 ASSERT(!options.IsEmpty());
128 126
129 ASSERT(m_isolate); 127 ASSERT(m_isolate);
130 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 128 ASSERT(m_isolate == v8::Isolate::GetCurrent());
131 ASSERT(m_exceptionState); 129 ASSERT(m_exceptionState);
132 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); 130 v8::Local<v8::String> v8Key = v8String(m_isolate, key);
133 if (!v8CallBoolean(options->Has(v8Context(), v8Key))) 131 if (!v8CallBoolean(object->Has(v8Context(), v8Key)))
134 return false; 132 return false;
135 value = options->Get(v8Key); 133 return object->Get(v8Context(), v8Key).ToLocal(&value);
136 return !value.IsEmpty();
137 } 134 }
138 135
139 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const 136 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const
140 { 137 {
141 return getKey(key, value); 138 return getKey(key, value);
142 } 139 }
143 140
144 bool Dictionary::get(const String& key, Dictionary& value) const 141 bool Dictionary::get(const String& key, Dictionary& value) const
145 { 142 {
146 v8::Local<v8::Value> v8Value; 143 v8::Local<v8::Value> v8Value;
(...skipping 20 matching lines...) Expand all
167 if (v8Value->IsObject()) 164 if (v8Value->IsObject())
168 return get(key, value); 165 return get(key, value);
169 166
170 if (context.isNullable() && blink::isUndefinedOrNull(v8Value)) 167 if (context.isNullable() && blink::isUndefinedOrNull(v8Value))
171 return true; 168 return true;
172 169
173 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a Dictionary type.")); 170 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a Dictionary type."));
174 return false; 171 return false;
175 } 172 }
176 173
174 static inline bool propertyKey(v8::Local<v8::Context> v8Context, v8::Local<v8::A rray> properties, uint32_t index, v8::Local<v8::String>& key)
175 {
176 v8::Local<v8::Value> property;
177 if (!properties->Get(v8Context, index).ToLocal(&property))
178 return false;
179 return property->ToString(v8Context).ToLocal(&key);
180 }
181
177 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa p) const 182 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa p) const
178 { 183 {
179 if (!isObject()) 184 v8::Local<v8::Object> object;
180 return false; 185 if (!toObject(&object))
181
182 v8::Handle<v8::Object> options = m_options->ToObject(m_isolate);
183 if (options.IsEmpty())
184 return false; 186 return false;
185 187
186 v8::Local<v8::Array> properties; 188 v8::Local<v8::Array> properties;
187 if (!options->GetOwnPropertyNames(v8Context()).ToLocal(&properties)) 189 if (!object->GetOwnPropertyNames(v8Context()).ToLocal(&properties))
188 return true; 190 return true;
189 for (uint32_t i = 0; i < properties->Length(); ++i) { 191 for (uint32_t i = 0; i < properties->Length(); ++i) {
190 v8::Local<v8::String> key = properties->Get(i)->ToString(m_isolate); 192 v8::Local<v8::String> key;
191 if (!v8CallBoolean(options->Has(v8Context(), key))) 193 if (!propertyKey(v8Context(), properties, i, key))
194 continue;
195 if (!v8CallBoolean(object->Has(v8Context(), key)))
192 continue; 196 continue;
193 197
194 v8::Local<v8::Value> value = options->Get(key); 198 v8::Local<v8::Value> value;
199 if (!object->Get(v8Context(), key).ToLocal(&value))
200 continue;
195 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); 201 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
196 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); 202 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false);
197 if (!static_cast<const String&>(stringKey).isEmpty()) 203 if (!static_cast<const String&>(stringKey).isEmpty())
198 hashMap.set(stringKey, stringValue); 204 hashMap.set(stringKey, stringValue);
199 } 205 }
200 206
201 return true; 207 return true;
202 } 208 }
203 209
204 bool Dictionary::getPropertyNames(Vector<String>& names) const 210 bool Dictionary::getPropertyNames(Vector<String>& names) const
205 { 211 {
206 if (!isObject()) 212 v8::Local<v8::Object> object;
213 if (!toObject(&object))
207 return false; 214 return false;
208 215
209 v8::Handle<v8::Object> options = m_options->ToObject(m_isolate); 216 v8::Local<v8::Array> properties;
210 if (options.IsEmpty()) 217 if (!object->GetPropertyNames(v8Context()).ToLocal(&properties))
211 return false;
212
213 v8::Local<v8::Array> properties = options->GetPropertyNames();
214 if (properties.IsEmpty())
215 return true; 218 return true;
haraken 2015/03/27 03:33:33 I'm not sure if this should always be true. If the
bashi 2015/03/27 03:55:24 Yeah, I thought it was inconsistent, but kept the
haraken 2015/03/27 03:59:24 I'm not sure if this is right. If there is no prop
bashi 2015/03/27 04:34:25 GetPropertyNames() calls NewJSArray() regardless t
216 for (uint32_t i = 0; i < properties->Length(); ++i) { 219 for (uint32_t i = 0; i < properties->Length(); ++i) {
217 v8::Local<v8::String> key = properties->Get(i)->ToString(m_isolate); 220 v8::Local<v8::String> key;
218 if (!v8CallBoolean(options->Has(v8Context(), key))) 221 if (!propertyKey(v8Context(), properties, i, key))
222 continue;
223 if (!v8CallBoolean(object->Has(v8Context(), key)))
219 continue; 224 continue;
220 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); 225 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
221 names.append(stringKey); 226 names.append(stringKey);
222 } 227 }
223 228
224 return true; 229 return true;
225 } 230 }
226 231
227 void Dictionary::ConversionContext::resetPerPropertyContext() 232 void Dictionary::ConversionContext::resetPerPropertyContext()
228 { 233 {
(...skipping 12 matching lines...) Expand all
241 m_propertyTypeName = typeName; 246 m_propertyTypeName = typeName;
242 247
243 return *this; 248 return *this;
244 } 249 }
245 250
246 void Dictionary::ConversionContext::throwTypeError(const String& detail) 251 void Dictionary::ConversionContext::throwTypeError(const String& detail)
247 { 252 {
248 exceptionState().throwTypeError(detail); 253 exceptionState().throwTypeError(detail);
249 } 254 }
250 255
256 bool Dictionary::toObject(v8::Local<v8::Object>* object) const
haraken 2015/03/27 03:33:33 Can we change v8::Local<v8::Object>* to v8::Local<
bashi 2015/03/27 03:55:24 |object| is an out parameter, as ToLocal takes a p
257 {
258 return !isUndefinedOrNull() && m_options->ToObject(v8Context()).ToLocal(obje ct);
259 }
260
251 } // namespace blink 261 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698