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

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 28 matching lines...) Expand all
39 #include "bindings/core/v8/V8Path2D.h" 39 #include "bindings/core/v8/V8Path2D.h"
40 #include "bindings/core/v8/V8Storage.h" 40 #include "bindings/core/v8/V8Storage.h"
41 #include "bindings/core/v8/V8TextTrack.h" 41 #include "bindings/core/v8/V8TextTrack.h"
42 #include "bindings/core/v8/V8VoidCallback.h" 42 #include "bindings/core/v8/V8VoidCallback.h"
43 #include "bindings/core/v8/V8Window.h" 43 #include "bindings/core/v8/V8Window.h"
44 #include "core/html/track/TrackBase.h" 44 #include "core/html/track/TrackBase.h"
45 #include "wtf/MathExtras.h" 45 #include "wtf/MathExtras.h"
46 46
47 namespace blink { 47 namespace blink {
48 48
49 #define TO_OBJECT_WITH_CHECK(context, value, object) \
50 { \
51 v8::MaybeLocal<v8::Object> maybeObject = value->ToObject(context); \
52 if (maybeObject.IsEmpty()) \
53 return false; \
54 if (!maybeObject.ToLocal(&object)) \
55 return false; \
56 }
57
49 static ExceptionState& emptyExceptionState() 58 static ExceptionState& emptyExceptionState()
50 { 59 {
51 AtomicallyInitializedStaticReference(WTF::ThreadSpecific<NonThrowableExcepti onState>, exceptionState, new ThreadSpecific<NonThrowableExceptionState>); 60 AtomicallyInitializedStaticReference(WTF::ThreadSpecific<NonThrowableExcepti onState>, exceptionState, new ThreadSpecific<NonThrowableExceptionState>);
52 return *exceptionState; 61 return *exceptionState;
53 } 62 }
54 63
55 Dictionary::Dictionary() 64 Dictionary::Dictionary()
56 : m_isolate(0) 65 : m_isolate(0)
57 , m_exceptionState(&emptyExceptionState()) 66 , m_exceptionState(&emptyExceptionState())
58 { 67 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 { 117 {
109 if (m_options.IsEmpty()) 118 if (m_options.IsEmpty())
110 return true; 119 return true;
111 return blink::isUndefinedOrNull(m_options); 120 return blink::isUndefinedOrNull(m_options);
112 } 121 }
113 122
114 bool Dictionary::hasProperty(const String& key) const 123 bool Dictionary::hasProperty(const String& key) const
115 { 124 {
116 if (isUndefinedOrNull()) 125 if (isUndefinedOrNull())
117 return false; 126 return false;
118 v8::Local<v8::Object> options = m_options->ToObject(m_isolate); 127 v8::Local<v8::Object> object;
119 ASSERT(!options.IsEmpty()); 128 TO_OBJECT_WITH_CHECK(context(), m_options, object);
120 129
121 ASSERT(m_isolate); 130 ASSERT(m_isolate);
122 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 131 ASSERT(m_isolate == v8::Isolate::GetCurrent());
123 ASSERT(m_exceptionState); 132 ASSERT(m_exceptionState);
124 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); 133 v8::Handle<v8::String> v8Key = v8String(m_isolate, key);
125 if (!options->Has(v8Key)) 134 if (v8Key.IsEmpty() || !object->Has(v8Key))
126 return false; 135 return false;
127 136
128 return true; 137 return true;
129 } 138 }
130 139
131 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const 140 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const
132 { 141 {
133 if (isUndefinedOrNull()) 142 if (isUndefinedOrNull())
134 return false; 143 return false;
135 v8::Local<v8::Object> options = m_options->ToObject(m_isolate); 144 v8::Local<v8::Object> object;
136 ASSERT(!options.IsEmpty()); 145 TO_OBJECT_WITH_CHECK(context(), m_options, object);
137 146
138 ASSERT(m_isolate); 147 ASSERT(m_isolate);
139 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 148 ASSERT(m_isolate == v8::Isolate::GetCurrent());
140 ASSERT(m_exceptionState); 149 ASSERT(m_exceptionState);
141 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); 150 v8::Handle<v8::String> v8Key = v8String(m_isolate, key);
142 if (!options->Has(v8Key)) 151 if (v8Key.IsEmpty() || !object->Has(v8Key))
143 return false; 152 return false;
144 value = options->Get(v8Key); 153 value = object->Get(v8Key);
145 if (value.IsEmpty()) 154 return !value.IsEmpty();
146 return false;
147 return true;
148 } 155 }
149 156
150 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const 157 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const
151 { 158 {
152 return getKey(key, value); 159 return getKey(key, value);
153 } 160 }
154 161
155 bool Dictionary::get(const String& key, Dictionary& value) const 162 bool Dictionary::get(const String& key, Dictionary& value) const
156 { 163 {
157 v8::Local<v8::Value> v8Value; 164 v8::Local<v8::Value> v8Value;
158 if (!getKey(key, v8Value)) 165 if (!getKey(key, v8Value))
159 return false; 166 return false;
160 167
161 if (v8Value->IsObject()) { 168 if (v8Value->IsObject()) {
162 ASSERT(m_isolate); 169 ASSERT(m_isolate);
163 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 170 ASSERT(m_isolate == v8::Isolate::GetCurrent());
164 value = Dictionary(v8Value, m_isolate, *m_exceptionState); 171 value = Dictionary(v8Value, m_isolate, *m_exceptionState);
165 } 172 }
166 173
167 return true; 174 return true;
168 } 175 }
169 176
170 bool Dictionary::set(const String& key, const v8::Handle<v8::Value>& value) 177 bool Dictionary::set(const String& key, const v8::Handle<v8::Value>& value)
171 { 178 {
172 if (isUndefinedOrNull()) 179 if (isUndefinedOrNull())
173 return false; 180 return false;
174 v8::Local<v8::Object> options = m_options->ToObject(m_isolate); 181 v8::Local<v8::Object> object;
175 ASSERT(!options.IsEmpty()); 182 TO_OBJECT_WITH_CHECK(context(), m_options, object);
176 ASSERT(m_exceptionState); 183 ASSERT(m_exceptionState);
177 184
178 return options->Set(v8String(m_isolate, key), value); 185 return object->Set(v8String(m_isolate, key), value);
179 } 186 }
180 187
181 bool Dictionary::set(const String& key, const String& value) 188 bool Dictionary::set(const String& key, const String& value)
182 { 189 {
183 return set(key, v8String(m_isolate, value)); 190 return set(key, v8String(m_isolate, value));
184 } 191 }
185 192
186 bool Dictionary::set(const String& key, unsigned value) 193 bool Dictionary::set(const String& key, unsigned value)
187 { 194 {
188 return set(key, v8::Integer::NewFromUnsigned(m_isolate, value)); 195 return set(key, v8::Integer::NewFromUnsigned(m_isolate, value));
(...skipping 20 matching lines...) Expand all
209 216
210 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a Dictionary type.")); 217 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a Dictionary type."));
211 return false; 218 return false;
212 } 219 }
213 220
214 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa p) const 221 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa p) const
215 { 222 {
216 if (!isObject()) 223 if (!isObject())
217 return false; 224 return false;
218 225
219 v8::Handle<v8::Object> options = m_options->ToObject(m_isolate); 226 v8::Local<v8::Object> object;
220 if (options.IsEmpty()) 227 TO_OBJECT_WITH_CHECK(context(), m_options, object);
221 return false;
222 228
223 v8::Local<v8::Array> properties = options->GetOwnPropertyNames(); 229 v8::Local<v8::Array> properties = object->GetOwnPropertyNames();
224 if (properties.IsEmpty()) 230 if (properties.IsEmpty())
225 return true; 231 return true;
226 for (uint32_t i = 0; i < properties->Length(); ++i) { 232 for (uint32_t i = 0; i < properties->Length(); ++i) {
227 v8::Local<v8::String> key = properties->Get(i)->ToString(m_isolate); 233 v8::Local<v8::Value> property = properties->Get(i);
228 if (!options->Has(key)) 234 if (property.IsEmpty())
235 continue;
236 v8::MaybeLocal<v8::String> maybeKey = properties->Get(i)->ToString(conte xt());
dcarney 2015/03/06 07:42:44 you want to use a macro here. basically, you shou
237 v8::Local<v8::String> key;
238 if (!maybeKey.ToLocal(&key) || !object->Has(key))
229 continue; 239 continue;
230 240
231 v8::Local<v8::Value> value = options->Get(key); 241 v8::Local<v8::Value> value = object->Get(key);
242 if (value.IsEmpty())
243 continue;
232 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); 244 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
233 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); 245 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false);
234 if (!static_cast<const String&>(stringKey).isEmpty()) 246 if (!static_cast<const String&>(stringKey).isEmpty())
235 hashMap.set(stringKey, stringValue); 247 hashMap.set(stringKey, stringValue);
236 } 248 }
237 249
238 return true; 250 return true;
239 } 251 }
240 252
241 bool Dictionary::getPropertyNames(Vector<String>& names) const 253 bool Dictionary::getPropertyNames(Vector<String>& names) const
242 { 254 {
243 if (!isObject()) 255 if (!isObject())
244 return false; 256 return false;
245 257
246 v8::Handle<v8::Object> options = m_options->ToObject(m_isolate); 258 v8::Local<v8::Object> object;
247 if (options.IsEmpty()) 259 TO_OBJECT_WITH_CHECK(context(), m_options, object);
248 return false;
249 260
250 v8::Local<v8::Array> properties = options->GetPropertyNames(); 261 v8::Local<v8::Array> properties = object->GetPropertyNames();
251 if (properties.IsEmpty()) 262 if (properties.IsEmpty())
252 return true; 263 return true;
253 for (uint32_t i = 0; i < properties->Length(); ++i) { 264 for (uint32_t i = 0; i < properties->Length(); ++i) {
254 v8::Local<v8::String> key = properties->Get(i)->ToString(m_isolate); 265 v8::Local<v8::Value> property = properties->Get(i);
255 if (!options->Has(key)) 266 if (property.IsEmpty())
267 continue;
268 v8::MaybeLocal<v8::String> maybeKey = properties->Get(i)->ToString(conte xt());
269 v8::Local<v8::String> key;
270 if (!maybeKey.ToLocal(&key) || !object->Has(key))
256 continue; 271 continue;
257 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); 272 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
258 names.append(stringKey); 273 names.append(stringKey);
259 } 274 }
260 275
261 return true; 276 return true;
262 } 277 }
263 278
264 void Dictionary::ConversionContext::resetPerPropertyContext() 279 void Dictionary::ConversionContext::resetPerPropertyContext()
265 { 280 {
(...skipping 12 matching lines...) Expand all
278 m_propertyTypeName = typeName; 293 m_propertyTypeName = typeName;
279 294
280 return *this; 295 return *this;
281 } 296 }
282 297
283 void Dictionary::ConversionContext::throwTypeError(const String& detail) 298 void Dictionary::ConversionContext::throwTypeError(const String& detail)
284 { 299 {
285 exceptionState().throwTypeError(detail); 300 exceptionState().throwTypeError(detail);
286 } 301 }
287 302
303 v8::Local<v8::Context> Dictionary::context() const
304 {
305 if (!m_isolate)
306 return v8::Local<v8::Context>();
307 return m_isolate->GetCurrentContext();
308 }
309
288 } // namespace blink 310 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698