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

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

Issue 2509713002: binding: Makes Dictionary throw an exception (constructor). (Closed)
Patch Set: Created 4 years, 1 month 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
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 13 matching lines...) Expand all
24 */ 24 */
25 25
26 #include "bindings/core/v8/Dictionary.h" 26 #include "bindings/core/v8/Dictionary.h"
27 27
28 #include "bindings/core/v8/V8ScriptRunner.h" 28 #include "bindings/core/v8/V8ScriptRunner.h"
29 #include "bindings/core/v8/V8StringResource.h" 29 #include "bindings/core/v8/V8StringResource.h"
30 #include "core/dom/ExecutionContext.h" 30 #include "core/dom/ExecutionContext.h"
31 31
32 namespace blink { 32 namespace blink {
33 33
34 Dictionary& Dictionary::operator=(const Dictionary& optionsObject) { 34 Dictionary::Dictionary(v8::Isolate* isolate,
35 m_options = optionsObject.m_options; 35 v8::Local<v8::Value> dictObj,
36 m_isolate = optionsObject.m_isolate; 36 ExceptionState& exceptionState)
37 return *this; 37 : m_isolate(isolate) {
38 } 38 DCHECK(isolate);
39 39
40 bool Dictionary::isObject() const { 40 // https://heycam.github.io/webidl/#es-dictionary
41 return !isUndefinedOrNull() && m_options->IsObject(); 41 // Type of an ECMAScript value must be Undefined, Null or Object.
42 } 42 if (dictObj.IsEmpty() || dictObj->IsUndefined() || dictObj->IsNull()) {
43 m_value = dictObj;
44 return;
45 }
43 46
44 bool Dictionary::isUndefinedOrNull() const { 47 v8::TryCatch tryCatch(isolate);
haraken 2016/11/16 17:28:57 I'd prefer checking IsObject and throw TypeError b
Yuki 2016/11/18 13:37:53 Done.
45 if (m_options.IsEmpty()) 48 if (dictObj->ToObject(v8Context()).ToLocal(&m_dictObj)) {
46 return true; 49 m_value = m_dictObj;
47 return blink::isUndefinedOrNull(m_options); 50 return;
51 }
52
53 // Otherwise, throw a TypeError.
54 DCHECK(tryCatch.HasCaught());
55 exceptionState.rethrowV8Exception(tryCatch.Exception());
48 } 56 }
49 57
50 bool Dictionary::hasProperty(const StringView& key) const { 58 bool Dictionary::hasProperty(const StringView& key) const {
51 v8::Local<v8::Object> object; 59 if (m_dictObj.IsEmpty())
52 if (!toObject(object))
53 return false; 60 return false;
54 61
55 DCHECK(m_isolate); 62 // TODO(bashi,yukishiino): Should rethrow the exception.
56 DCHECK_EQ(m_isolate, v8::Isolate::GetCurrent()); 63 // Has() on a revoked proxy will throw an exception.
57 return v8CallBoolean(object->Has(v8Context(), v8String(m_isolate, key))); 64 return v8CallBoolean(m_dictObj->Has(v8Context(), v8String(m_isolate, key)));
58 }
59
60 bool Dictionary::get(const StringView& key, v8::Local<v8::Value>& value) const {
61 if (!m_isolate)
62 return false;
63 return getInternal(v8String(m_isolate, key), value);
64 } 65 }
65 66
66 DictionaryIterator Dictionary::getIterator( 67 DictionaryIterator Dictionary::getIterator(
67 ExecutionContext* executionContext) const { 68 ExecutionContext* executionContext) const {
68 v8::Local<v8::Value> iteratorGetter; 69 v8::Local<v8::Value> iteratorGetter;
69 if (!getInternal(v8::Symbol::GetIterator(m_isolate), iteratorGetter) || 70 if (!getInternal(v8::Symbol::GetIterator(m_isolate), iteratorGetter) ||
70 !iteratorGetter->IsFunction()) 71 !iteratorGetter->IsFunction())
71 return nullptr; 72 return nullptr;
72 v8::Local<v8::Value> iterator; 73 v8::Local<v8::Value> iterator;
73 if (!v8Call(V8ScriptRunner::callFunction( 74 if (!v8Call(V8ScriptRunner::callFunction(
74 v8::Local<v8::Function>::Cast(iteratorGetter), 75 v8::Local<v8::Function>::Cast(iteratorGetter),
75 executionContext, m_options, 0, nullptr, m_isolate), 76 executionContext, m_dictObj, 0, nullptr, m_isolate),
76 iterator)) 77 iterator))
77 return nullptr; 78 return nullptr;
78 if (!iterator->IsObject()) 79 if (!iterator->IsObject())
79 return nullptr; 80 return nullptr;
80 return DictionaryIterator(v8::Local<v8::Object>::Cast(iterator), m_isolate); 81 return DictionaryIterator(v8::Local<v8::Object>::Cast(iterator), m_isolate);
81 } 82 }
82 83
83 bool Dictionary::get(const StringView& key, Dictionary& value) const { 84 bool Dictionary::get(const StringView& key, Dictionary& value) const {
84 v8::Local<v8::Value> v8Value; 85 v8::Local<v8::Value> v8Value;
85 if (!get(key, v8Value)) 86 if (!get(key, v8Value))
86 return false; 87 return false;
87 88
88 if (v8Value->IsObject()) { 89 if (v8Value->IsObject()) {
89 ASSERT(m_isolate); 90 ASSERT(m_isolate);
90 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 91 ASSERT(m_isolate == v8::Isolate::GetCurrent());
91 value = Dictionary(m_isolate, v8Value); 92 // TODO(bashi,yukishiino): Should rethrow the exception.
93 TrackExceptionState exceptionState;
94 value = Dictionary(m_isolate, v8Value, exceptionState);
92 } 95 }
93 96
94 return true; 97 return true;
95 } 98 }
96 99
97 bool Dictionary::getInternal(const v8::Local<v8::Value>& key, 100 bool Dictionary::getInternal(const v8::Local<v8::Value>& key,
98 v8::Local<v8::Value>& result) const { 101 v8::Local<v8::Value>& result) const {
99 v8::Local<v8::Object> object; 102 if (m_dictObj.IsEmpty())
100 if (!toObject(object))
101 return false; 103 return false;
102 104
103 DCHECK(m_isolate); 105 if (!v8CallBoolean(m_dictObj->Has(v8Context(), key)))
104 DCHECK_EQ(m_isolate, v8::Isolate::GetCurrent());
105 if (!v8CallBoolean(object->Has(v8Context(), key)))
106 return false; 106 return false;
107 107
108 // Swallow a possible exception in v8::Object::Get(). 108 // Swallow a possible exception in v8::Object::Get().
109 // TODO(bashi,yukishiino): Should rethrow the exception. 109 // TODO(bashi,yukishiino): Should rethrow the exception.
110 v8::TryCatch tryCatch(isolate()); 110 v8::TryCatch tryCatch(isolate());
111 return object->Get(v8Context(), key).ToLocal(&result); 111 return m_dictObj->Get(v8Context(), key).ToLocal(&result);
112 } 112 }
113 113
114 static inline bool propertyKey(v8::Local<v8::Context> v8Context, 114 static inline bool propertyKey(v8::Local<v8::Context> v8Context,
115 v8::Local<v8::Array> properties, 115 v8::Local<v8::Array> properties,
116 uint32_t index, 116 uint32_t index,
117 v8::Local<v8::String>& key) { 117 v8::Local<v8::String>& key) {
118 v8::Local<v8::Value> property; 118 v8::Local<v8::Value> property;
119 if (!properties->Get(v8Context, index).ToLocal(&property)) 119 if (!properties->Get(v8Context, index).ToLocal(&property))
120 return false; 120 return false;
121 return property->ToString(v8Context).ToLocal(&key); 121 return property->ToString(v8Context).ToLocal(&key);
122 } 122 }
123 123
124 bool Dictionary::getOwnPropertiesAsStringHashMap( 124 bool Dictionary::getOwnPropertiesAsStringHashMap(
125 HashMap<String, String>& hashMap) const { 125 HashMap<String, String>& hashMap) const {
126 v8::Local<v8::Object> object; 126 if (m_dictObj.IsEmpty())
127 if (!toObject(object))
128 return false; 127 return false;
129 128
130 v8::Local<v8::Array> properties; 129 v8::Local<v8::Array> properties;
131 if (!object->GetOwnPropertyNames(v8Context()).ToLocal(&properties)) 130 if (!m_dictObj->GetOwnPropertyNames(v8Context()).ToLocal(&properties))
132 return false; 131 return false;
133 // Swallow a possible exception in v8::Object::Get(). 132 // Swallow a possible exception in v8::Object::Get().
134 // TODO(bashi,yukishiino): Should rethrow the exception. 133 // TODO(bashi,yukishiino): Should rethrow the exception.
135 // Note that propertyKey() may throw an exception. 134 // Note that propertyKey() may throw an exception.
136 v8::TryCatch tryCatch(isolate()); 135 v8::TryCatch tryCatch(isolate());
137 for (uint32_t i = 0; i < properties->Length(); ++i) { 136 for (uint32_t i = 0; i < properties->Length(); ++i) {
138 v8::Local<v8::String> key; 137 v8::Local<v8::String> key;
139 if (!propertyKey(v8Context(), properties, i, key)) 138 if (!propertyKey(v8Context(), properties, i, key))
140 continue; 139 continue;
141 if (!v8CallBoolean(object->Has(v8Context(), key))) 140 if (!v8CallBoolean(m_dictObj->Has(v8Context(), key)))
142 continue; 141 continue;
143 142
144 v8::Local<v8::Value> value; 143 v8::Local<v8::Value> value;
145 if (!object->Get(v8Context(), key).ToLocal(&value)) { 144 if (!m_dictObj->Get(v8Context(), key).ToLocal(&value)) {
146 tryCatch.Reset(); 145 tryCatch.Reset();
147 continue; 146 continue;
148 } 147 }
149 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); 148 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
150 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); 149 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false);
151 if (!static_cast<const String&>(stringKey).isEmpty()) 150 if (!static_cast<const String&>(stringKey).isEmpty())
152 hashMap.set(stringKey, stringValue); 151 hashMap.set(stringKey, stringValue);
153 } 152 }
154 153
155 return true; 154 return true;
156 } 155 }
157 156
158 bool Dictionary::getPropertyNames(Vector<String>& names) const { 157 bool Dictionary::getPropertyNames(Vector<String>& names) const {
159 v8::Local<v8::Object> object; 158 if (m_dictObj.IsEmpty())
160 if (!toObject(object))
161 return false; 159 return false;
162 160
163 v8::Local<v8::Array> properties; 161 v8::Local<v8::Array> properties;
164 if (!object->GetPropertyNames(v8Context()).ToLocal(&properties)) 162 if (!m_dictObj->GetPropertyNames(v8Context()).ToLocal(&properties))
165 return false; 163 return false;
166 for (uint32_t i = 0; i < properties->Length(); ++i) { 164 for (uint32_t i = 0; i < properties->Length(); ++i) {
167 v8::Local<v8::String> key; 165 v8::Local<v8::String> key;
168 if (!propertyKey(v8Context(), properties, i, key)) 166 if (!propertyKey(v8Context(), properties, i, key))
169 continue; 167 continue;
170 if (!v8CallBoolean(object->Has(v8Context(), key))) 168 if (!v8CallBoolean(m_dictObj->Has(v8Context(), key)))
171 continue; 169 continue;
172 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); 170 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
173 names.append(stringKey); 171 names.append(stringKey);
174 } 172 }
175 173
176 return true; 174 return true;
177 } 175 }
178 176
179 bool Dictionary::toObject(v8::Local<v8::Object>& object) const {
180 return !isUndefinedOrNull() &&
181 m_options->ToObject(v8Context()).ToLocal(&object);
182 }
183
184 } // namespace blink 177 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698