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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/Dictionary.cpp
diff --git a/third_party/WebKit/Source/bindings/core/v8/Dictionary.cpp b/third_party/WebKit/Source/bindings/core/v8/Dictionary.cpp
index 7d7442cdb7e359fe66f0a42011df553b14519e25..d944341d5e746590f3228268491f4a993d4ecfa4 100644
--- a/third_party/WebKit/Source/bindings/core/v8/Dictionary.cpp
+++ b/third_party/WebKit/Source/bindings/core/v8/Dictionary.cpp
@@ -31,36 +31,37 @@
namespace blink {
-Dictionary& Dictionary::operator=(const Dictionary& optionsObject) {
- m_options = optionsObject.m_options;
- m_isolate = optionsObject.m_isolate;
- return *this;
-}
+Dictionary::Dictionary(v8::Isolate* isolate,
+ v8::Local<v8::Value> dictObj,
+ ExceptionState& exceptionState)
+ : m_isolate(isolate) {
+ DCHECK(isolate);
+
+ // https://heycam.github.io/webidl/#es-dictionary
+ // Type of an ECMAScript value must be Undefined, Null or Object.
+ if (dictObj.IsEmpty() || dictObj->IsUndefined() || dictObj->IsNull()) {
+ m_value = dictObj;
+ return;
+ }
-bool Dictionary::isObject() const {
- return !isUndefinedOrNull() && m_options->IsObject();
-}
+ 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.
+ if (dictObj->ToObject(v8Context()).ToLocal(&m_dictObj)) {
+ m_value = m_dictObj;
+ return;
+ }
-bool Dictionary::isUndefinedOrNull() const {
- if (m_options.IsEmpty())
- return true;
- return blink::isUndefinedOrNull(m_options);
+ // Otherwise, throw a TypeError.
+ DCHECK(tryCatch.HasCaught());
+ exceptionState.rethrowV8Exception(tryCatch.Exception());
}
bool Dictionary::hasProperty(const StringView& key) const {
- v8::Local<v8::Object> object;
- if (!toObject(object))
+ if (m_dictObj.IsEmpty())
return false;
- DCHECK(m_isolate);
- DCHECK_EQ(m_isolate, v8::Isolate::GetCurrent());
- return v8CallBoolean(object->Has(v8Context(), v8String(m_isolate, key)));
-}
-
-bool Dictionary::get(const StringView& key, v8::Local<v8::Value>& value) const {
- if (!m_isolate)
- return false;
- return getInternal(v8String(m_isolate, key), value);
+ // TODO(bashi,yukishiino): Should rethrow the exception.
+ // Has() on a revoked proxy will throw an exception.
+ return v8CallBoolean(m_dictObj->Has(v8Context(), v8String(m_isolate, key)));
}
DictionaryIterator Dictionary::getIterator(
@@ -72,7 +73,7 @@ DictionaryIterator Dictionary::getIterator(
v8::Local<v8::Value> iterator;
if (!v8Call(V8ScriptRunner::callFunction(
v8::Local<v8::Function>::Cast(iteratorGetter),
- executionContext, m_options, 0, nullptr, m_isolate),
+ executionContext, m_dictObj, 0, nullptr, m_isolate),
iterator))
return nullptr;
if (!iterator->IsObject())
@@ -88,7 +89,9 @@ bool Dictionary::get(const StringView& key, Dictionary& value) const {
if (v8Value->IsObject()) {
ASSERT(m_isolate);
ASSERT(m_isolate == v8::Isolate::GetCurrent());
- value = Dictionary(m_isolate, v8Value);
+ // TODO(bashi,yukishiino): Should rethrow the exception.
+ TrackExceptionState exceptionState;
+ value = Dictionary(m_isolate, v8Value, exceptionState);
}
return true;
@@ -96,19 +99,16 @@ bool Dictionary::get(const StringView& key, Dictionary& value) const {
bool Dictionary::getInternal(const v8::Local<v8::Value>& key,
v8::Local<v8::Value>& result) const {
- v8::Local<v8::Object> object;
- if (!toObject(object))
+ if (m_dictObj.IsEmpty())
return false;
- DCHECK(m_isolate);
- DCHECK_EQ(m_isolate, v8::Isolate::GetCurrent());
- if (!v8CallBoolean(object->Has(v8Context(), key)))
+ if (!v8CallBoolean(m_dictObj->Has(v8Context(), key)))
return false;
// Swallow a possible exception in v8::Object::Get().
// TODO(bashi,yukishiino): Should rethrow the exception.
v8::TryCatch tryCatch(isolate());
- return object->Get(v8Context(), key).ToLocal(&result);
+ return m_dictObj->Get(v8Context(), key).ToLocal(&result);
}
static inline bool propertyKey(v8::Local<v8::Context> v8Context,
@@ -123,12 +123,11 @@ static inline bool propertyKey(v8::Local<v8::Context> v8Context,
bool Dictionary::getOwnPropertiesAsStringHashMap(
HashMap<String, String>& hashMap) const {
- v8::Local<v8::Object> object;
- if (!toObject(object))
+ if (m_dictObj.IsEmpty())
return false;
v8::Local<v8::Array> properties;
- if (!object->GetOwnPropertyNames(v8Context()).ToLocal(&properties))
+ if (!m_dictObj->GetOwnPropertyNames(v8Context()).ToLocal(&properties))
return false;
// Swallow a possible exception in v8::Object::Get().
// TODO(bashi,yukishiino): Should rethrow the exception.
@@ -138,11 +137,11 @@ bool Dictionary::getOwnPropertiesAsStringHashMap(
v8::Local<v8::String> key;
if (!propertyKey(v8Context(), properties, i, key))
continue;
- if (!v8CallBoolean(object->Has(v8Context(), key)))
+ if (!v8CallBoolean(m_dictObj->Has(v8Context(), key)))
continue;
v8::Local<v8::Value> value;
- if (!object->Get(v8Context(), key).ToLocal(&value)) {
+ if (!m_dictObj->Get(v8Context(), key).ToLocal(&value)) {
tryCatch.Reset();
continue;
}
@@ -156,18 +155,17 @@ bool Dictionary::getOwnPropertiesAsStringHashMap(
}
bool Dictionary::getPropertyNames(Vector<String>& names) const {
- v8::Local<v8::Object> object;
- if (!toObject(object))
+ if (m_dictObj.IsEmpty())
return false;
v8::Local<v8::Array> properties;
- if (!object->GetPropertyNames(v8Context()).ToLocal(&properties))
+ if (!m_dictObj->GetPropertyNames(v8Context()).ToLocal(&properties))
return false;
for (uint32_t i = 0; i < properties->Length(); ++i) {
v8::Local<v8::String> key;
if (!propertyKey(v8Context(), properties, i, key))
continue;
- if (!v8CallBoolean(object->Has(v8Context(), key)))
+ if (!v8CallBoolean(m_dictObj->Has(v8Context(), key)))
continue;
TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
names.append(stringKey);
@@ -176,9 +174,4 @@ bool Dictionary::getPropertyNames(Vector<String>& names) const {
return true;
}
-bool Dictionary::toObject(v8::Local<v8::Object>& object) const {
- return !isUndefinedOrNull() &&
- m_options->ToObject(v8Context()).ToLocal(&object);
-}
-
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698