Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 | 59 |
| 60 bool Dictionary::get(const StringView& key, v8::Local<v8::Value>& value) const { | 60 bool Dictionary::get(const StringView& key, v8::Local<v8::Value>& value) const { |
| 61 if (!m_isolate) | 61 if (!m_isolate) |
| 62 return false; | 62 return false; |
| 63 return getInternal(v8String(m_isolate, key), value); | 63 return getInternal(v8String(m_isolate, key), value); |
| 64 } | 64 } |
| 65 | 65 |
| 66 DictionaryIterator Dictionary::getIterator( | 66 DictionaryIterator Dictionary::getIterator( |
| 67 ExecutionContext* executionContext) const { | 67 ExecutionContext* executionContext) const { |
| 68 v8::Local<v8::Value> iteratorGetter; | 68 v8::Local<v8::Value> iteratorGetter; |
| 69 // TODO(alancutter): Support callable objects as well as functions. | |
| 70 if (!getInternal(v8::Symbol::GetIterator(m_isolate), iteratorGetter) || | 69 if (!getInternal(v8::Symbol::GetIterator(m_isolate), iteratorGetter) || |
| 71 !iteratorGetter->IsFunction()) | 70 !iteratorGetter->IsFunction()) |
| 72 return nullptr; | 71 return nullptr; |
| 73 v8::Local<v8::Value> iterator; | 72 v8::Local<v8::Value> iterator; |
| 74 if (!v8Call(V8ScriptRunner::callFunction( | 73 if (!v8Call(V8ScriptRunner::callFunction( |
| 75 v8::Local<v8::Function>::Cast(iteratorGetter), | 74 v8::Local<v8::Function>::Cast(iteratorGetter), |
| 76 executionContext, m_options, 0, nullptr, m_isolate), | 75 executionContext, m_options, 0, nullptr, m_isolate), |
| 77 iterator)) | 76 iterator)) |
| 78 return nullptr; | 77 return nullptr; |
| 79 if (!iterator->IsObject()) | 78 if (!iterator->IsObject()) |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 98 bool Dictionary::getInternal(const v8::Local<v8::Value>& key, | 97 bool Dictionary::getInternal(const v8::Local<v8::Value>& key, |
| 99 v8::Local<v8::Value>& result) const { | 98 v8::Local<v8::Value>& result) const { |
| 100 v8::Local<v8::Object> object; | 99 v8::Local<v8::Object> object; |
| 101 if (!toObject(object)) | 100 if (!toObject(object)) |
| 102 return false; | 101 return false; |
| 103 | 102 |
| 104 DCHECK(m_isolate); | 103 DCHECK(m_isolate); |
| 105 DCHECK_EQ(m_isolate, v8::Isolate::GetCurrent()); | 104 DCHECK_EQ(m_isolate, v8::Isolate::GetCurrent()); |
| 106 if (!v8CallBoolean(object->Has(v8Context(), key))) | 105 if (!v8CallBoolean(object->Has(v8Context(), key))) |
| 107 return false; | 106 return false; |
| 107 | |
| 108 // Swallow a possible exception in v8::Object::Get(). | |
| 109 // TODO(bashi,yukishiino): Should rethrow the exception. | |
| 110 v8::TryCatch tryCatch(isolate()); | |
| 108 return object->Get(v8Context(), key).ToLocal(&result); | 111 return object->Get(v8Context(), key).ToLocal(&result); |
| 109 } | 112 } |
| 110 | 113 |
| 111 static inline bool propertyKey(v8::Local<v8::Context> v8Context, | 114 static inline bool propertyKey(v8::Local<v8::Context> v8Context, |
| 112 v8::Local<v8::Array> properties, | 115 v8::Local<v8::Array> properties, |
| 113 uint32_t index, | 116 uint32_t index, |
| 114 v8::Local<v8::String>& key) { | 117 v8::Local<v8::String>& key) { |
| 115 v8::Local<v8::Value> property; | 118 v8::Local<v8::Value> property; |
| 116 if (!properties->Get(v8Context, index).ToLocal(&property)) | 119 if (!properties->Get(v8Context, index).ToLocal(&property)) |
| 117 return false; | 120 return false; |
| 118 return property->ToString(v8Context).ToLocal(&key); | 121 return property->ToString(v8Context).ToLocal(&key); |
| 119 } | 122 } |
| 120 | 123 |
| 121 bool Dictionary::getOwnPropertiesAsStringHashMap( | 124 bool Dictionary::getOwnPropertiesAsStringHashMap( |
| 122 HashMap<String, String>& hashMap) const { | 125 HashMap<String, String>& hashMap) const { |
| 123 v8::Local<v8::Object> object; | 126 v8::Local<v8::Object> object; |
| 124 if (!toObject(object)) | 127 if (!toObject(object)) |
| 125 return false; | 128 return false; |
| 126 | 129 |
| 127 v8::Local<v8::Array> properties; | 130 v8::Local<v8::Array> properties; |
| 128 if (!object->GetOwnPropertyNames(v8Context()).ToLocal(&properties)) | 131 if (!object->GetOwnPropertyNames(v8Context()).ToLocal(&properties)) |
| 129 return false; | 132 return false; |
| 133 // Swallow a possible exception in v8::Object::Get(). | |
| 134 // TODO(bashi,yukishiino): Should rethrow the exception. | |
| 135 v8::TryCatch tryCatch(isolate()); | |
| 130 for (uint32_t i = 0; i < properties->Length(); ++i) { | 136 for (uint32_t i = 0; i < properties->Length(); ++i) { |
| 131 v8::Local<v8::String> key; | 137 v8::Local<v8::String> key; |
| 132 if (!propertyKey(v8Context(), properties, i, key)) | 138 if (!propertyKey(v8Context(), properties, i, key)) |
| 133 continue; | 139 continue; |
| 134 if (!v8CallBoolean(object->Has(v8Context(), key))) | 140 if (!v8CallBoolean(object->Has(v8Context(), key))) |
|
haraken
2016/11/12 07:45:13
Just to confirm: The line 138 and 140 never throws
Yuki
2016/11/14 06:57:25
I expected so, but after talking with bashi@, I re
haraken
2016/11/14 07:13:25
Sounds reasonable. Our plan is to deprecate v8Call
| |
| 135 continue; | 141 continue; |
| 136 | 142 |
| 137 v8::Local<v8::Value> value; | 143 v8::Local<v8::Value> value; |
| 138 if (!object->Get(v8Context(), key).ToLocal(&value)) | 144 if (!object->Get(v8Context(), key).ToLocal(&value)) { |
| 145 tryCatch.Reset(); | |
| 139 continue; | 146 continue; |
| 147 } | |
| 140 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); | 148 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); |
| 141 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); | 149 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); |
| 142 if (!static_cast<const String&>(stringKey).isEmpty()) | 150 if (!static_cast<const String&>(stringKey).isEmpty()) |
| 143 hashMap.set(stringKey, stringValue); | 151 hashMap.set(stringKey, stringValue); |
| 144 } | 152 } |
| 145 | 153 |
| 146 return true; | 154 return true; |
| 147 } | 155 } |
| 148 | 156 |
| 149 bool Dictionary::getPropertyNames(Vector<String>& names) const { | 157 bool Dictionary::getPropertyNames(Vector<String>& names) const { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 166 | 174 |
| 167 return true; | 175 return true; |
| 168 } | 176 } |
| 169 | 177 |
| 170 bool Dictionary::toObject(v8::Local<v8::Object>& object) const { | 178 bool Dictionary::toObject(v8::Local<v8::Object>& object) const { |
| 171 return !isUndefinedOrNull() && | 179 return !isUndefinedOrNull() && |
| 172 m_options->ToObject(v8Context()).ToLocal(&object); | 180 m_options->ToObject(v8Context()).ToLocal(&object); |
| 173 } | 181 } |
| 174 | 182 |
| 175 } // namespace blink | 183 } // namespace blink |
| OLD | NEW |