| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "sky/engine/config.h" | |
| 27 #include "sky/engine/bindings/core/v8/Dictionary.h" | |
| 28 | |
| 29 #include "bindings/core/v8/V8DOMError.h" | |
| 30 #include "bindings/core/v8/V8Element.h" | |
| 31 #include "bindings/core/v8/V8EventTarget.h" | |
| 32 #include "bindings/core/v8/V8Path2D.h" | |
| 33 #include "bindings/core/v8/V8VoidCallback.h" | |
| 34 #include "bindings/core/v8/V8Window.h" | |
| 35 #include "sky/engine/bindings/core/v8/ArrayValue.h" | |
| 36 #include "sky/engine/bindings/core/v8/ExceptionMessages.h" | |
| 37 #include "sky/engine/bindings/core/v8/ExceptionState.h" | |
| 38 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 39 #include "sky/engine/bindings/core/v8/custom/V8ArrayBufferViewCustom.h" | |
| 40 #include "sky/engine/bindings/core/v8/custom/V8Uint8ArrayCustom.h" | |
| 41 #include "sky/engine/wtf/MathExtras.h" | |
| 42 | |
| 43 namespace blink { | |
| 44 | |
| 45 Dictionary::Dictionary() | |
| 46 : m_isolate(0) | |
| 47 { | |
| 48 } | |
| 49 | |
| 50 Dictionary::Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate* isolat
e) | |
| 51 : m_options(options) | |
| 52 , m_isolate(isolate) | |
| 53 { | |
| 54 ASSERT(m_isolate); | |
| 55 } | |
| 56 | |
| 57 Dictionary::~Dictionary() | |
| 58 { | |
| 59 } | |
| 60 | |
| 61 Dictionary& Dictionary::operator=(const Dictionary& optionsObject) | |
| 62 { | |
| 63 m_options = optionsObject.m_options; | |
| 64 m_isolate = optionsObject.m_isolate; | |
| 65 return *this; | |
| 66 } | |
| 67 | |
| 68 Dictionary Dictionary::createEmpty(v8::Isolate* isolate) | |
| 69 { | |
| 70 return Dictionary(v8::Object::New(isolate), isolate); | |
| 71 } | |
| 72 | |
| 73 bool Dictionary::isObject() const | |
| 74 { | |
| 75 return !isUndefinedOrNull() && m_options->IsObject(); | |
| 76 } | |
| 77 | |
| 78 bool Dictionary::isUndefinedOrNull() const | |
| 79 { | |
| 80 if (m_options.IsEmpty()) | |
| 81 return true; | |
| 82 return blink::isUndefinedOrNull(m_options); | |
| 83 } | |
| 84 | |
| 85 bool Dictionary::hasProperty(const String& key) const | |
| 86 { | |
| 87 if (isUndefinedOrNull()) | |
| 88 return false; | |
| 89 v8::Local<v8::Object> options = m_options->ToObject(); | |
| 90 ASSERT(!options.IsEmpty()); | |
| 91 | |
| 92 ASSERT(m_isolate); | |
| 93 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
| 94 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); | |
| 95 if (!options->Has(v8Key)) | |
| 96 return false; | |
| 97 | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const | |
| 102 { | |
| 103 if (isUndefinedOrNull()) | |
| 104 return false; | |
| 105 v8::Local<v8::Object> options = m_options->ToObject(); | |
| 106 ASSERT(!options.IsEmpty()); | |
| 107 | |
| 108 ASSERT(m_isolate); | |
| 109 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
| 110 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); | |
| 111 if (!options->Has(v8Key)) | |
| 112 return false; | |
| 113 value = options->Get(v8Key); | |
| 114 if (value.IsEmpty()) | |
| 115 return false; | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const | |
| 120 { | |
| 121 return getKey(key, value); | |
| 122 } | |
| 123 | |
| 124 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) c
onst | |
| 125 { | |
| 126 v8::Local<v8::Value> v8Value; | |
| 127 if (!getKey(key, v8Value) || blink::isUndefinedOrNull(v8Value)) | |
| 128 return false; | |
| 129 | |
| 130 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
| 131 value = stringValue; | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, RefPtr<Element>&
value) const | |
| 136 { | |
| 137 v8::Local<v8::Value> v8Value; | |
| 138 if (!getKey(key, v8Value) || blink::isUndefinedOrNull(v8Value)) | |
| 139 return false; | |
| 140 | |
| 141 value = V8Element::toNativeWithTypeCheck(m_isolate, v8Value); | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, RefPtr<Path2D>&
value) const | |
| 146 { | |
| 147 v8::Local<v8::Value> v8Value; | |
| 148 if (!getKey(key, v8Value) || blink::isUndefinedOrNull(v8Value)) | |
| 149 return false; | |
| 150 | |
| 151 value = V8Path2D::toNativeWithTypeCheck(m_isolate, v8Value); | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 bool Dictionary::get(const String& key, Dictionary& value) const | |
| 156 { | |
| 157 v8::Local<v8::Value> v8Value; | |
| 158 if (!getKey(key, v8Value)) | |
| 159 return false; | |
| 160 | |
| 161 if (v8Value->IsObject()) { | |
| 162 ASSERT(m_isolate); | |
| 163 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
| 164 value = Dictionary(v8Value, m_isolate); | |
| 165 } | |
| 166 | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 bool Dictionary::set(const String& key, const v8::Handle<v8::Value>& value) | |
| 171 { | |
| 172 if (isUndefinedOrNull()) | |
| 173 return false; | |
| 174 v8::Local<v8::Object> options = m_options->ToObject(); | |
| 175 ASSERT(!options.IsEmpty()); | |
| 176 | |
| 177 return options->Set(v8String(m_isolate, key), value); | |
| 178 } | |
| 179 | |
| 180 bool Dictionary::set(const String& key, const String& value) | |
| 181 { | |
| 182 return set(key, v8String(m_isolate, value)); | |
| 183 } | |
| 184 | |
| 185 bool Dictionary::set(const String& key, unsigned value) | |
| 186 { | |
| 187 return set(key, v8::Integer::NewFromUnsigned(m_isolate, value)); | |
| 188 } | |
| 189 | |
| 190 bool Dictionary::set(const String& key, const Dictionary& value) | |
| 191 { | |
| 192 return set(key, value.v8Value()); | |
| 193 } | |
| 194 | |
| 195 bool Dictionary::convert(ConversionContext& context, const String& key, Dictiona
ry& value) const | |
| 196 { | |
| 197 ConversionContextScope scope(context); | |
| 198 | |
| 199 v8::Local<v8::Value> v8Value; | |
| 200 if (!getKey(key, v8Value)) | |
| 201 return true; | |
| 202 | |
| 203 if (v8Value->IsObject()) | |
| 204 return get(key, value); | |
| 205 | |
| 206 if (context.isNullable() && blink::isUndefinedOrNull(v8Value)) | |
| 207 return true; | |
| 208 | |
| 209 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n
ot have a Dictionary type.")); | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 213 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa
p) const | |
| 214 { | |
| 215 if (!isObject()) | |
| 216 return false; | |
| 217 | |
| 218 v8::Handle<v8::Object> options = m_options->ToObject(); | |
| 219 if (options.IsEmpty()) | |
| 220 return false; | |
| 221 | |
| 222 v8::Local<v8::Array> properties = options->GetOwnPropertyNames(); | |
| 223 if (properties.IsEmpty()) | |
| 224 return true; | |
| 225 for (uint32_t i = 0; i < properties->Length(); ++i) { | |
| 226 v8::Local<v8::String> key = properties->Get(i)->ToString(); | |
| 227 if (!options->Has(key)) | |
| 228 continue; | |
| 229 | |
| 230 v8::Local<v8::Value> value = options->Get(key); | |
| 231 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); | |
| 232 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); | |
| 233 if (!static_cast<const String&>(stringKey).isEmpty()) | |
| 234 hashMap.set(stringKey, stringValue); | |
| 235 } | |
| 236 | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 bool Dictionary::getOwnPropertyNames(Vector<String>& names) const | |
| 241 { | |
| 242 if (!isObject()) | |
| 243 return false; | |
| 244 | |
| 245 v8::Handle<v8::Object> options = m_options->ToObject(); | |
| 246 if (options.IsEmpty()) | |
| 247 return false; | |
| 248 | |
| 249 v8::Local<v8::Array> properties = options->GetOwnPropertyNames(); | |
| 250 if (properties.IsEmpty()) | |
| 251 return true; | |
| 252 for (uint32_t i = 0; i < properties->Length(); ++i) { | |
| 253 v8::Local<v8::String> key = properties->Get(i)->ToString(); | |
| 254 if (!options->Has(key)) | |
| 255 continue; | |
| 256 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); | |
| 257 names.append(stringKey); | |
| 258 } | |
| 259 | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 263 void Dictionary::ConversionContext::resetPerPropertyContext() | |
| 264 { | |
| 265 if (m_dirty) { | |
| 266 m_dirty = false; | |
| 267 m_isNullable = false; | |
| 268 m_propertyTypeName = ""; | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 Dictionary::ConversionContext& Dictionary::ConversionContext::setConversionType(
const String& typeName, bool isNullable) | |
| 273 { | |
| 274 ASSERT(!m_dirty); | |
| 275 m_dirty = true; | |
| 276 m_isNullable = isNullable; | |
| 277 m_propertyTypeName = typeName; | |
| 278 | |
| 279 return *this; | |
| 280 } | |
| 281 | |
| 282 void Dictionary::ConversionContext::throwTypeError(const String& detail) | |
| 283 { | |
| 284 exceptionState().throwTypeError(detail); | |
| 285 } | |
| 286 | |
| 287 } // namespace blink | |
| OLD | NEW |