| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Ericsson AB. All rights reserved. | 3 * Copyright (C) 2012 Ericsson AB. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 #include "bindings/v8/V8StringResource.h" | 39 #include "bindings/v8/V8StringResource.h" |
| 40 #include "bindings/v8/V8ThrowException.h" | 40 #include "bindings/v8/V8ThrowException.h" |
| 41 #include "bindings/v8/V8ValueCache.h" | 41 #include "bindings/v8/V8ValueCache.h" |
| 42 #include "heap/Heap.h" | 42 #include "heap/Heap.h" |
| 43 #include "wtf/MathExtras.h" | 43 #include "wtf/MathExtras.h" |
| 44 #include "wtf/text/AtomicString.h" | 44 #include "wtf/text/AtomicString.h" |
| 45 #include <v8.h> | 45 #include <v8.h> |
| 46 | 46 |
| 47 namespace WebCore { | 47 namespace WebCore { |
| 48 | 48 |
| 49 class DOMWindow; | 49 class DOMWindow; |
| 50 class Document; | 50 class Document; |
| 51 class EventListener; | 51 class EventListener; |
| 52 class ExecutionContext; | 52 class ExceptionState; |
| 53 class ExceptionState; | 53 class LocalFrame; |
| 54 class LocalFrame; | 54 class NodeFilter; |
| 55 class NodeFilter; | 55 class ExecutionContext; |
| 56 class ScriptWrappable; | 56 class ScriptWrappable; |
| 57 class V8PerContextData; | 57 class XPathNSResolver; |
| 58 class XPathNSResolver; | 58 |
| 59 | 59 const int kMaxRecursionDepth = 22; |
| 60 const int kMaxRecursionDepth = 22; | 60 |
| 61 | 61 // Schedule a JavaScript error to be thrown. |
| 62 // Schedule a JavaScript error to be thrown. | 62 v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*); |
| 63 v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*); | 63 |
| 64 | 64 // Schedule a JavaScript error to be thrown. |
| 65 // Schedule a JavaScript error to be thrown. | 65 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); |
| 66 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); | 66 |
| 67 | 67 // A helper for throwing JavaScript TypeError. |
| 68 // A helper for throwing JavaScript TypeError. | 68 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); |
| 69 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); | 69 |
| 70 | 70 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); |
| 71 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); | 71 |
| 72 | 72 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v
8::Value>& info, int index) |
| 73 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v8::V
alue>& info, int index) | 73 { |
| 74 { | 74 return index >= info.Length() ? v8::Local<v8::Value>() : info[index]; |
| 75 return index >= info.Length() ? v8::Local<v8::Value>() : info[index]; | 75 } |
| 76 } | 76 |
| 77 | 77 template<typename CallbackInfo, typename V> |
| 78 template<typename CallbackInfo, typename V> | 78 inline void v8SetReturnValue(const CallbackInfo& info, V v) |
| 79 inline void v8SetReturnValue(const CallbackInfo& info, V v) | 79 { |
| 80 { | 80 info.GetReturnValue().Set(v); |
| 81 info.GetReturnValue().Set(v); | 81 } |
| 82 } | 82 |
| 83 | 83 template<typename CallbackInfo> |
| 84 template<typename CallbackInfo> | 84 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v) |
| 85 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v) | 85 { |
| 86 { | 86 info.GetReturnValue().Set(v); |
| 87 info.GetReturnValue().Set(v); | 87 } |
| 88 } | 88 |
| 89 | 89 template<typename CallbackInfo> |
| 90 template<typename CallbackInfo> | 90 inline void v8SetReturnValueInt(const CallbackInfo& info, int v) |
| 91 inline void v8SetReturnValueInt(const CallbackInfo& info, int v) | 91 { |
| 92 { | 92 info.GetReturnValue().Set(v); |
| 93 info.GetReturnValue().Set(v); | 93 } |
| 94 } | 94 |
| 95 | 95 template<typename CallbackInfo> |
| 96 template<typename CallbackInfo> | 96 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v) |
| 97 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v) | 97 { |
| 98 { | 98 info.GetReturnValue().Set(v); |
| 99 info.GetReturnValue().Set(v); | 99 } |
| 100 } | 100 |
| 101 | 101 template<typename CallbackInfo> |
| 102 template<typename CallbackInfo> | 102 inline void v8SetReturnValueNull(const CallbackInfo& info) |
| 103 inline void v8SetReturnValueNull(const CallbackInfo& info) | 103 { |
| 104 { | 104 info.GetReturnValue().SetNull(); |
| 105 info.GetReturnValue().SetNull(); | 105 } |
| 106 } | 106 |
| 107 | 107 template<typename CallbackInfo> |
| 108 template<typename CallbackInfo> | 108 inline void v8SetReturnValueUndefined(const CallbackInfo& info) |
| 109 inline void v8SetReturnValueUndefined(const CallbackInfo& info) | 109 { |
| 110 { | 110 info.GetReturnValue().SetUndefined(); |
| 111 info.GetReturnValue().SetUndefined(); | 111 } |
| 112 } | 112 |
| 113 | 113 template<typename CallbackInfo> |
| 114 template<typename CallbackInfo> | 114 inline void v8SetReturnValueEmptyString(const CallbackInfo& info) |
| 115 inline void v8SetReturnValueEmptyString(const CallbackInfo& info) | 115 { |
| 116 { | 116 info.GetReturnValue().SetEmptyString(); |
| 117 info.GetReturnValue().SetEmptyString(); | 117 } |
| 118 } | 118 |
| 119 | 119 template <class CallbackInfo> |
| 120 template <class CallbackInfo> | 120 inline void v8SetReturnValueString(const CallbackInfo& info, const String& s
tring, v8::Isolate* isolate) |
| 121 inline void v8SetReturnValueString(const CallbackInfo& info, const String& strin
g, v8::Isolate* isolate) | 121 { |
| 122 { | 122 if (string.isNull()) { |
| 123 if (string.isNull()) { | 123 v8SetReturnValueEmptyString(info); |
| 124 v8SetReturnValueEmptyString(info); | 124 return; |
| 125 return; | 125 } |
| 126 } | 126 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString
(info.GetReturnValue(), string.impl()); |
| 127 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf
o.GetReturnValue(), string.impl()); | 127 } |
| 128 } | 128 |
| 129 | 129 template <class CallbackInfo> |
| 130 template <class CallbackInfo> | 130 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const Str
ing& string, v8::Isolate* isolate) |
| 131 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String&
string, v8::Isolate* isolate) | 131 { |
| 132 { | 132 if (string.isNull()) { |
| 133 if (string.isNull()) { | 133 v8SetReturnValueNull(info); |
| 134 v8SetReturnValueNull(info); | 134 return; |
| 135 return; | 135 } |
| 136 } | 136 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString
(info.GetReturnValue(), string.impl()); |
| 137 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf
o.GetReturnValue(), string.impl()); | 137 } |
| 138 } | 138 |
| 139 | 139 template <class CallbackInfo> |
| 140 template <class CallbackInfo> | 140 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, cons
t String& string, v8::Isolate* isolate) |
| 141 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const St
ring& string, v8::Isolate* isolate) | 141 { |
| 142 { | 142 if (string.isNull()) { |
| 143 if (string.isNull()) { | 143 v8SetReturnValueUndefined(info); |
| 144 v8SetReturnValueUndefined(info); | 144 return; |
| 145 return; | 145 } |
| 146 } | 146 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString
(info.GetReturnValue(), string.impl()); |
| 147 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf
o.GetReturnValue(), string.impl()); | 147 } |
| 148 } | 148 |
| 149 | 149 // Convert v8::String to a WTF::String. If the V8 string is not already |
| 150 // Convert v8::String to a WTF::String. If the V8 string is not already | 150 // an external string then it is transformed into an external string at this |
| 151 // an external string then it is transformed into an external string at this | 151 // point to avoid repeated conversions. |
| 152 // point to avoid repeated conversions. | 152 inline String toCoreString(v8::Handle<v8::String> value) |
| 153 inline String toCoreString(v8::Handle<v8::String> value) | 153 { |
| 154 { | 154 return v8StringToWebCoreString<String>(value, Externalize); |
| 155 return v8StringToWebCoreString<String>(value, Externalize); | 155 } |
| 156 } | 156 |
| 157 | 157 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value) |
| 158 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value) | 158 { |
| 159 { | 159 if (value.IsEmpty() || value->IsNull()) |
| 160 if (value.IsEmpty() || value->IsNull()) | 160 return String(); |
| 161 return String(); | 161 return toCoreString(value); |
| 162 return toCoreString(value); | 162 } |
| 163 } | 163 |
| 164 | 164 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> va
lue) |
| 165 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value) | 165 { |
| 166 { | 166 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) |
| 167 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) | 167 return String(); |
| 168 return String(); | 168 return toCoreString(value); |
| 169 return toCoreString(value); | 169 } |
| 170 } | 170 |
| 171 | 171 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value) |
| 172 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value) | 172 { |
| 173 { | 173 return v8StringToWebCoreString<AtomicString>(value, Externalize); |
| 174 return v8StringToWebCoreString<AtomicString>(value, Externalize); | 174 } |
| 175 } | 175 |
| 176 | 176 // This method will return a null String if the v8::Value does not contain a
v8::String. |
| 177 // This method will return a null String if the v8::Value does not contain a v8:
:String. | 177 // It will not call ToString() on the v8::Value. If you want ToString() to b
e called, |
| 178 // It will not call ToString() on the v8::Value. If you want ToString() to be ca
lled, | 178 // please use the V8TRYCATCH_FOR_V8STRINGRESOURCE_*() macros instead. |
| 179 // please use the V8TRYCATCH_FOR_V8STRINGRESOURCE_*() macros instead. | 179 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> val
ue) |
| 180 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value) | 180 { |
| 181 { | 181 if (value.IsEmpty() || !value->IsString()) |
| 182 if (value.IsEmpty() || !value->IsString()) | 182 return String(); |
| 183 return String(); | 183 |
| 184 return toCoreString(value.As<v8::String>()); | 184 return toCoreString(value.As<v8::String>()); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Convert a string to a V8 string. | 187 // Convert a string to a V8 string. |
| 188 // Return a V8 external string that shares the underlying buffer with the given | 188 // Return a V8 external string that shares the underlying buffer with the gi
ven |
| 189 // WebCore string. The reference counting mechanism is used to keep the | 189 // WebCore string. The reference counting mechanism is used to keep the |
| 190 // underlying buffer alive while the string is still live in the V8 engine. | 190 // underlying buffer alive while the string is still live in the V8 engine. |
| 191 inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& strin
g) | 191 inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& s
tring) |
| 192 { | 192 { |
| 193 if (string.isNull()) | 193 if (string.isNull()) |
| 194 return v8::String::Empty(isolate); | 194 return v8::String::Empty(isolate); |
| 195 return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(stri
ng.impl(), isolate); | 195 return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(
string.impl(), isolate); |
| 196 } | 196 } |
| 197 | 197 |
| 198 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s
tr) | 198 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const cha
r* str) |
| 199 { | 199 { |
| 200 ASSERT(isolate); | 200 ASSERT(isolate); |
| 201 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString
, strlen(str)); | 201 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedSt
ring, strlen(str)); |
| 202 } | 202 } |
| 203 | 203 |
| 204 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s
tr, size_t length) | 204 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const cha
r* str, size_t length) |
| 205 { | 205 { |
| 206 ASSERT(isolate); | 206 ASSERT(isolate); |
| 207 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString
, length); | 207 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedSt
ring, length); |
| 208 } | 208 } |
| 209 | 209 |
| 210 inline v8::Handle<v8::Value> v8Undefined() | 210 inline v8::Handle<v8::Value> v8Undefined() |
| 211 { | 211 { |
| 212 return v8::Handle<v8::Value>(); | 212 return v8::Handle<v8::Value>(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 template <class T> | 215 template <class T> |
| 216 struct V8ValueTraits { | 216 struct V8ValueTraits { |
| 217 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate
* isolate) | 217 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Iso
late* isolate) |
| 218 { | 218 { |
| 219 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate); | 219 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate); |
| 220 } | 220 } |
| 221 }; | 221 }; |
| 222 | 222 |
| 223 template<> | 223 template<> |
| 224 struct V8ValueTraits<String> { | 224 struct V8ValueTraits<String> { |
| 225 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Is
olate* isolate) | 225 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8
::Isolate* isolate) |
| 226 { | 226 { |
| 227 return v8String(isolate, value); | 227 return v8String(isolate, value); |
| 228 } | 228 } |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 template<> | 231 template<> |
| 232 struct V8ValueTraits<AtomicString> { | 232 struct V8ValueTraits<AtomicString> { |
| 233 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value,
v8::Isolate* isolate) | 233 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& val
ue, v8::Isolate* isolate) |
| 234 { | 234 { |
| 235 return v8String(isolate, value); | 235 return v8String(isolate, value); |
| 236 } | 236 } |
| 237 }; | 237 }; |
| 238 | 238 |
| 239 template<> | 239 template<> |
| 240 struct V8ValueTraits<unsigned> { | 240 struct V8ValueTraits<unsigned> { |
| 241 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8::
Isolate* isolate) | 241 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value,
v8::Isolate* isolate) |
| 242 { | 242 { |
| 243 return v8::Integer::NewFromUnsigned(isolate, value); | 243 return v8::Integer::NewFromUnsigned(isolate, value); |
| 244 } | 244 } |
| 245 }; | 245 }; |
| 246 | 246 |
| 247 template<> | 247 template<> |
| 248 struct V8ValueTraits<unsigned long> { | 248 struct V8ValueTraits<unsigned long> { |
| 249 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value,
v8::Isolate* isolate) | 249 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& va
lue, v8::Isolate* isolate) |
| 250 { | 250 { |
| 251 return v8::Integer::NewFromUnsigned(isolate, value); | 251 return v8::Integer::NewFromUnsigned(isolate, value); |
| 252 } | 252 } |
| 253 }; | 253 }; |
| 254 | 254 |
| 255 template<> | 255 template<> |
| 256 struct V8ValueTraits<float> { | 256 struct V8ValueTraits<float> { |
| 257 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Iso
late* isolate) | 257 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8:
:Isolate* isolate) |
| 258 { | 258 { |
| 259 return v8::Number::New(isolate, value); | 259 return v8::Number::New(isolate, value); |
| 260 } | 260 } |
| 261 }; | 261 }; |
| 262 | 262 |
| 263 template<> | 263 template<> |
| 264 struct V8ValueTraits<double> { | 264 struct V8ValueTraits<double> { |
| 265 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Is
olate* isolate) | 265 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8
::Isolate* isolate) |
| 266 { | 266 { |
| 267 return v8::Number::New(isolate, value); | 267 return v8::Number::New(isolate, value); |
| 268 } | 268 } |
| 269 }; | 269 }; |
| 270 | 270 |
| 271 template<typename T, size_t inlineCapacity> | 271 template<typename T, size_t inlineCapacity> |
| 272 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Iso
late* isolate) | 272 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8:
:Isolate* isolate) |
| 273 { | 273 { |
| 274 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 274 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
| 275 int index = 0; | 275 int index = 0; |
| 276 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); | 276 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end(); |
| 277 typedef V8ValueTraits<T> TraitsType; | 277 typedef V8ValueTraits<T> TraitsType; |
| 278 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi
n(); iter != end; ++iter) | 278 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.
begin(); iter != end; ++iter) |
| 279 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value
(*iter, isolate)); | 279 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8V
alue(*iter, isolate)); |
| 280 return result; | 280 return result; |
| 281 } | 281 } |
| 282 | 282 |
| 283 template<typename T, size_t inlineCapacity> | 283 template<typename T, size_t inlineCapacity> |
| 284 v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator, v8:
:Isolate* isolate) | 284 v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator,
v8::Isolate* isolate) |
| 285 { | 285 { |
| 286 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); | 286 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size()); |
| 287 int index = 0; | 287 int index = 0; |
| 288 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end(); | 288 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.en
d(); |
| 289 typedef V8ValueTraits<T> TraitsType; | 289 typedef V8ValueTraits<T> TraitsType; |
| 290 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.
begin(); iter != end; ++iter) | 290 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = itera
tor.begin(); iter != end; ++iter) |
| 291 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value
(*iter, isolate)); | 291 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8V
alue(*iter, isolate)); |
| 292 return result; | 292 return result; |
| 293 } | 293 } |
| 294 | 294 |
| 295 // Conversion flags, used in toIntXX/toUIntXX. | 295 // Conversion flags, used in toIntXX/toUIntXX. |
| 296 enum IntegerConversionConfiguration { | 296 enum IntegerConversionConfiguration { |
| 297 NormalConversion, | 297 NormalConversion, |
| 298 EnforceRange, | 298 EnforceRange, |
| 299 Clamp | 299 Clamp |
| 300 }; | 300 }; |
| 301 | 301 |
| 302 // Convert a value to a 8-bit signed integer. The conversion fails if the | 302 // Convert a value to a 8-bit signed integer. The conversion fails if the |
| 303 // value cannot be converted to a number or the range violated per WebIDL: | 303 // value cannot be converted to a number or the range violated per WebIDL: |
| 304 // http://www.w3.org/TR/WebIDL/#es-byte | 304 // http://www.w3.org/TR/WebIDL/#es-byte |
| 305 int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionSt
ate&); | 305 int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); |
| 306 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState
) | 306 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) |
| 307 { | 307 { |
| 308 return toInt8(value, NormalConversion, exceptionState); | 308 return toInt8(value, NormalConversion, exceptionState); |
| 309 } | 309 } |
| 310 | 310 |
| 311 // Convert a value to a 8-bit integer assuming the conversion cannot fail. | 311 // Convert a value to a 8-bit integer assuming the conversion cannot fail. |
| 312 int8_t toInt8(v8::Handle<v8::Value>); | 312 int8_t toInt8(v8::Handle<v8::Value>); |
| 313 | 313 |
| 314 // Convert a value to a 8-bit unsigned integer. The conversion fails if the | 314 // Convert a value to a 8-bit unsigned integer. The conversion fails if the |
| 315 // value cannot be converted to a number or the range violated per WebIDL: | 315 // value cannot be converted to a number or the range violated per WebIDL: |
| 316 // http://www.w3.org/TR/WebIDL/#es-octet | 316 // http://www.w3.org/TR/WebIDL/#es-octet |
| 317 uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); | 317 uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excep
tionState&); |
| 318 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) | 318 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) |
| 319 { | 319 { |
| 320 return toUInt8(value, NormalConversion, exceptionState); | 320 return toUInt8(value, NormalConversion, exceptionState); |
| 321 } | 321 } |
| 322 | 322 |
| 323 // Convert a value to a 8-bit unsigned integer assuming the conversion cannot fa
il. | 323 // Convert a value to a 8-bit unsigned integer assuming the conversion canno
t fail. |
| 324 uint8_t toUInt8(v8::Handle<v8::Value>); | 324 uint8_t toUInt8(v8::Handle<v8::Value>); |
| 325 | 325 |
| 326 // Convert a value to a 16-bit signed integer. The conversion fails if the | 326 // Convert a value to a 16-bit signed integer. The conversion fails if the |
| 327 // value cannot be converted to a number or the range violated per WebIDL: | 327 // value cannot be converted to a number or the range violated per WebIDL: |
| 328 // http://www.w3.org/TR/WebIDL/#es-short | 328 // http://www.w3.org/TR/WebIDL/#es-short |
| 329 int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); | 329 int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excep
tionState&); |
| 330 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) | 330 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) |
| 331 { | 331 { |
| 332 return toInt16(value, NormalConversion, exceptionState); | 332 return toInt16(value, NormalConversion, exceptionState); |
| 333 } | 333 } |
| 334 | 334 |
| 335 // Convert a value to a 16-bit integer assuming the conversion cannot fail. | 335 // Convert a value to a 16-bit integer assuming the conversion cannot fail. |
| 336 int16_t toInt16(v8::Handle<v8::Value>); | 336 int16_t toInt16(v8::Handle<v8::Value>); |
| 337 | 337 |
| 338 // Convert a value to a 16-bit unsigned integer. The conversion fails if the | 338 // Convert a value to a 16-bit unsigned integer. The conversion fails if the |
| 339 // value cannot be converted to a number or the range violated per WebIDL: | 339 // value cannot be converted to a number or the range violated per WebIDL: |
| 340 // http://www.w3.org/TR/WebIDL/#es-unsigned-short | 340 // http://www.w3.org/TR/WebIDL/#es-unsigned-short |
| 341 uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); | 341 uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exc
eptionState&); |
| 342 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) | 342 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& except
ionState) |
| 343 { | 343 { |
| 344 return toUInt16(value, NormalConversion, exceptionState); | 344 return toUInt16(value, NormalConversion, exceptionState); |
| 345 } | 345 } |
| 346 | 346 |
| 347 // Convert a value to a 16-bit unsigned integer assuming the conversion cannot f
ail. | 347 // Convert a value to a 16-bit unsigned integer assuming the conversion cann
ot fail. |
| 348 uint16_t toUInt16(v8::Handle<v8::Value>); | 348 uint16_t toUInt16(v8::Handle<v8::Value>); |
| 349 | 349 |
| 350 // Convert a value to a 32-bit signed integer. The conversion fails if the | 350 // Convert a value to a 32-bit signed integer. The conversion fails if the |
| 351 // value cannot be converted to a number or the range violated per WebIDL: | 351 // value cannot be converted to a number or the range violated per WebIDL: |
| 352 // http://www.w3.org/TR/WebIDL/#es-long | 352 // http://www.w3.org/TR/WebIDL/#es-long |
| 353 int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); | 353 int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excep
tionState&); |
| 354 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) | 354 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) |
| 355 { | 355 { |
| 356 return toInt32(value, NormalConversion, exceptionState); | 356 return toInt32(value, NormalConversion, exceptionState); |
| 357 } | 357 } |
| 358 | 358 |
| 359 // Convert a value to a 32-bit integer assuming the conversion cannot fail. | 359 // Convert a value to a 32-bit integer assuming the conversion cannot fail. |
| 360 int32_t toInt32(v8::Handle<v8::Value>); | 360 int32_t toInt32(v8::Handle<v8::Value>); |
| 361 | 361 |
| 362 // Convert a value to a 32-bit unsigned integer. The conversion fails if the | 362 // Convert a value to a 32-bit unsigned integer. The conversion fails if the |
| 363 // value cannot be converted to a number or the range violated per WebIDL: | 363 // value cannot be converted to a number or the range violated per WebIDL: |
| 364 // http://www.w3.org/TR/WebIDL/#es-unsigned-long | 364 // http://www.w3.org/TR/WebIDL/#es-unsigned-long |
| 365 uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); | 365 uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exc
eptionState&); |
| 366 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) | 366 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& except
ionState) |
| 367 { | 367 { |
| 368 return toUInt32(value, NormalConversion, exceptionState); | 368 return toUInt32(value, NormalConversion, exceptionState); |
| 369 } | 369 } |
| 370 | 370 |
| 371 // Convert a value to a 32-bit unsigned integer assuming the conversion cannot f
ail. | 371 // Convert a value to a 32-bit unsigned integer assuming the conversion cann
ot fail. |
| 372 uint32_t toUInt32(v8::Handle<v8::Value>); | 372 uint32_t toUInt32(v8::Handle<v8::Value>); |
| 373 | 373 |
| 374 // Convert a value to a 64-bit signed integer. The conversion fails if the | 374 // Convert a value to a 64-bit signed integer. The conversion fails if the |
| 375 // value cannot be converted to a number or the range violated per WebIDL: | 375 // value cannot be converted to a number or the range violated per WebIDL: |
| 376 // http://www.w3.org/TR/WebIDL/#es-long-long | 376 // http://www.w3.org/TR/WebIDL/#es-long-long |
| 377 int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); | 377 int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excep
tionState&); |
| 378 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) | 378 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) |
| 379 { | 379 { |
| 380 return toInt64(value, NormalConversion, exceptionState); | 380 return toInt64(value, NormalConversion, exceptionState); |
| 381 } | 381 } |
| 382 | 382 |
| 383 // Convert a value to a 64-bit integer assuming the conversion cannot fail. | 383 // Convert a value to a 64-bit integer assuming the conversion cannot fail. |
| 384 int64_t toInt64(v8::Handle<v8::Value>); | 384 int64_t toInt64(v8::Handle<v8::Value>); |
| 385 | 385 |
| 386 // Convert a value to a 64-bit unsigned integer. The conversion fails if the | 386 // Convert a value to a 64-bit unsigned integer. The conversion fails if the |
| 387 // value cannot be converted to a number or the range violated per WebIDL: | 387 // value cannot be converted to a number or the range violated per WebIDL: |
| 388 // http://www.w3.org/TR/WebIDL/#es-unsigned-long-long | 388 // http://www.w3.org/TR/WebIDL/#es-unsigned-long-long |
| 389 uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); | 389 uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exc
eptionState&); |
| 390 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) | 390 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& except
ionState) |
| 391 { | 391 { |
| 392 return toUInt64(value, NormalConversion, exceptionState); | 392 return toUInt64(value, NormalConversion, exceptionState); |
| 393 } | 393 } |
| 394 | 394 |
| 395 // Convert a value to a 64-bit unsigned integer assuming the conversion cannot f
ail. | 395 // Convert a value to a 64-bit unsigned integer assuming the conversion cann
ot fail. |
| 396 uint64_t toUInt64(v8::Handle<v8::Value>); | 396 uint64_t toUInt64(v8::Handle<v8::Value>); |
| 397 | 397 |
| 398 // Convert a value to a single precision float, which might fail. | 398 // Convert a value to a single precision float, which might fail. |
| 399 float toFloat(v8::Handle<v8::Value>, ExceptionState&); | 399 float toFloat(v8::Handle<v8::Value>, ExceptionState&); |
| 400 | 400 |
| 401 // Convert a value to a single precision float assuming the conversion cannot fa
il. | 401 // Convert a value to a single precision float assuming the conversion canno
t fail. |
| 402 inline float toFloat(v8::Local<v8::Value> value) | 402 inline float toFloat(v8::Local<v8::Value> value) |
| 403 { | |
| 404 return static_cast<float>(value->NumberValue()); | |
| 405 } | |
| 406 | |
| 407 inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate) | |
| 408 { | |
| 409 return value ? v8::True(isolate) : v8::False(isolate); | |
| 410 } | |
| 411 | |
| 412 inline double toCoreDate(v8::Handle<v8::Value> object) | |
| 413 { | |
| 414 if (object->IsDate()) | |
| 415 return v8::Handle<v8::Date>::Cast(object)->ValueOf(); | |
| 416 if (object->IsNumber()) | |
| 417 return object->NumberValue(); | |
| 418 return std::numeric_limits<double>::quiet_NaN(); | |
| 419 } | |
| 420 | |
| 421 inline v8::Handle<v8::Value> v8DateOrNull(double value, v8::Isolate* isolate) | |
| 422 { | |
| 423 ASSERT(isolate); | |
| 424 return std::isfinite(value) ? v8::Date::New(isolate, value) : v8::Handle<v8:
:Value>::Cast(v8::Null(isolate)); | |
| 425 } | |
| 426 | |
| 427 // FIXME: Remove the special casing for NodeFilter and XPathNSResolver. | |
| 428 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); | |
| 429 PassRefPtrWillBeRawPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Value>,
v8::Isolate*); | |
| 430 | |
| 431 template<class T> struct NativeValueTraits; | |
| 432 | |
| 433 template<> | |
| 434 struct NativeValueTraits<String> { | |
| 435 static inline String nativeValue(const v8::Handle<v8::Value>& value, v8::Iso
late* isolate) | |
| 436 { | |
| 437 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue,
value, String()); | |
| 438 return stringValue; | |
| 439 } | |
| 440 }; | |
| 441 | |
| 442 template<> | |
| 443 struct NativeValueTraits<unsigned> { | |
| 444 static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v8::I
solate* isolate) | |
| 445 { | |
| 446 return toUInt32(value); | |
| 447 } | |
| 448 }; | |
| 449 | |
| 450 template<> | |
| 451 struct NativeValueTraits<float> { | |
| 452 static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::Isol
ate* isolate) | |
| 453 { | 403 { |
| 454 return static_cast<float>(value->NumberValue()); | 404 return static_cast<float>(value->NumberValue()); |
| 455 } | 405 } |
| 456 }; | 406 |
| 457 | 407 inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate) |
| 458 template<> | 408 { |
| 459 struct NativeValueTraits<double> { | 409 return value ? v8::True(isolate) : v8::False(isolate); |
| 460 static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Iso
late* isolate) | 410 } |
| 461 { | 411 |
| 462 return static_cast<double>(value->NumberValue()); | 412 inline double toCoreDate(v8::Handle<v8::Value> object) |
| 463 } | 413 { |
| 464 }; | 414 if (object->IsDate()) |
| 465 | 415 return v8::Handle<v8::Date>::Cast(object)->ValueOf(); |
| 466 template<> | 416 if (object->IsNumber()) |
| 467 struct NativeValueTraits<v8::Handle<v8::Value> > { | 417 return object->NumberValue(); |
| 468 static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>&
value, v8::Isolate* isolate) | 418 return std::numeric_limits<double>::quiet_NaN(); |
| 469 { | 419 } |
| 470 return value; | 420 |
| 471 } | 421 inline v8::Handle<v8::Value> v8DateOrNull(double value, v8::Isolate* isolate
) |
| 472 }; | 422 { |
| 473 | 423 ASSERT(isolate); |
| 474 // Converts a JavaScript value to an array as per the Web IDL specification: | 424 return std::isfinite(value) ? v8::Date::New(isolate, value) : v8::Handle
<v8::Value>::Cast(v8::Null(isolate)); |
| 475 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array | 425 } |
| 476 template <class T, class V8T> | 426 |
| 477 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value, ui
nt32_t length, v8::Isolate* isolate, bool* success = 0) | 427 // FIXME: Remove the special casing for NodeFilter and XPathNSResolver. |
| 478 { | 428 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); |
| 479 Vector<RefPtr<T> > result; | 429 PassRefPtrWillBeRawPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Val
ue>, v8::Isolate*); |
| 480 result.reserveInitialCapacity(length); | 430 |
| 481 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 431 template<class T> struct NativeValueTraits; |
| 482 for (uint32_t i = 0; i < length; ++i) { | 432 |
| 483 v8::Handle<v8::Value> element = object->Get(i); | 433 template<> |
| 484 if (V8T::hasInstance(element, isolate)) { | 434 struct NativeValueTraits<String> { |
| 485 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast(
element); | 435 static inline String nativeValue(const v8::Handle<v8::Value>& value, v8:
:Isolate* isolate) |
| 486 result.uncheckedAppend(V8T::toNative(elementObject)); | 436 { |
| 487 } else { | 437 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringVal
ue, value, String()); |
| 488 if (success) | 438 return stringValue; |
| 489 *success = false; | 439 } |
| 490 throwTypeError("Invalid Array element type", isolate); | 440 }; |
| 441 |
| 442 template<> |
| 443 struct NativeValueTraits<unsigned> { |
| 444 static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v
8::Isolate* isolate) |
| 445 { |
| 446 return toUInt32(value); |
| 447 } |
| 448 }; |
| 449 |
| 450 template<> |
| 451 struct NativeValueTraits<float> { |
| 452 static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::
Isolate* isolate) |
| 453 { |
| 454 return static_cast<float>(value->NumberValue()); |
| 455 } |
| 456 }; |
| 457 |
| 458 template<> |
| 459 struct NativeValueTraits<double> { |
| 460 static inline double nativeValue(const v8::Handle<v8::Value>& value, v8:
:Isolate* isolate) |
| 461 { |
| 462 return static_cast<double>(value->NumberValue()); |
| 463 } |
| 464 }; |
| 465 |
| 466 template<> |
| 467 struct NativeValueTraits<v8::Handle<v8::Value> > { |
| 468 static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Val
ue>& value, v8::Isolate* isolate) |
| 469 { |
| 470 return value; |
| 471 } |
| 472 }; |
| 473 |
| 474 // Converts a JavaScript value to an array as per the Web IDL specification: |
| 475 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
| 476 template <class T, class V8T> |
| 477 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value
, uint32_t length, v8::Isolate* isolate, bool* success = 0) |
| 478 { |
| 479 Vector<RefPtr<T> > result; |
| 480 result.reserveInitialCapacity(length); |
| 481 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
| 482 for (uint32_t i = 0; i < length; ++i) { |
| 483 v8::Handle<v8::Value> element = object->Get(i); |
| 484 |
| 485 if (V8T::hasInstance(element, isolate)) { |
| 486 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::C
ast(element); |
| 487 result.uncheckedAppend(V8T::toNative(elementObject)); |
| 488 } else { |
| 489 if (success) |
| 490 *success = false; |
| 491 throwTypeError("Invalid Array element type", isolate); |
| 492 return Vector<RefPtr<T> >(); |
| 493 } |
| 494 } |
| 495 return result; |
| 496 } |
| 497 |
| 498 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length,
v8::Isolate*); |
| 499 |
| 500 template <class T, class V8T> |
| 501 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argu
mentIndex, v8::Isolate* isolate, bool* success = 0) |
| 502 { |
| 503 if (success) |
| 504 *success = true; |
| 505 |
| 506 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 507 uint32_t length = 0; |
| 508 if (value->IsArray()) { |
| 509 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 510 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { |
| 511 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argu
mentIndex), isolate); |
| 491 return Vector<RefPtr<T> >(); | 512 return Vector<RefPtr<T> >(); |
| 492 } | 513 } |
| 493 } | 514 |
| 494 return result; | 515 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su
ccess); |
| 495 } | 516 } |
| 496 | 517 |
| 497 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8::
Isolate*); | 518 template <class T, class V8T> |
| 498 | 519 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const St
ring& propertyName, v8::Isolate* isolate, bool* success = 0) |
| 499 template <class T, class V8T> | 520 { |
| 500 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argument
Index, v8::Isolate* isolate, bool* success = 0) | 521 if (success) |
| 501 { | 522 *success = true; |
| 502 if (success) | 523 |
| 503 *success = true; | 524 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 504 | 525 uint32_t length = 0; |
| 505 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 526 if (value->IsArray()) { |
| 506 uint32_t length = 0; | 527 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 507 if (value->IsArray()) { | 528 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { |
| 508 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 529 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyN
ame), isolate); |
| 509 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { | 530 return Vector<RefPtr<T> >(); |
| 510 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument
Index), isolate); | 531 } |
| 511 return Vector<RefPtr<T> >(); | 532 |
| 512 } | 533 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su
ccess); |
| 513 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, succes
s); | 534 } |
| 514 } | 535 |
| 515 | 536 // Converts a JavaScript value to an array as per the Web IDL specification: |
| 516 template <class T, class V8T> | 537 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
| 517 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const String
& propertyName, v8::Isolate* isolate, bool* success = 0) | 538 template <class T> |
| 518 { | 539 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::
Isolate* isolate) |
| 519 if (success) | 540 { |
| 520 *success = true; | 541 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 521 | 542 uint32_t length = 0; |
| 522 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 543 if (value->IsArray()) { |
| 523 uint32_t length = 0; | 544 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 524 if (value->IsArray()) { | 545 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { |
| 525 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 546 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argu
mentIndex), isolate); |
| 526 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { | 547 return Vector<T>(); |
| 527 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName)
, isolate); | 548 } |
| 528 return Vector<RefPtr<T> >(); | 549 |
| 529 } | 550 Vector<T> result; |
| 530 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, succes
s); | 551 result.reserveInitialCapacity(length); |
| 531 } | 552 typedef NativeValueTraits<T> TraitsType; |
| 532 | 553 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
| 533 // Converts a JavaScript value to an array as per the Web IDL specification: | 554 for (uint32_t i = 0; i < length; ++i) |
| 534 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array | 555 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isola
te)); |
| 535 template <class T> | 556 return result; |
| 536 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isol
ate* isolate) | 557 } |
| 537 { | 558 |
| 538 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 559 template <class T> |
| 539 uint32_t length = 0; | 560 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info,
int startIndex) |
| 540 if (value->IsArray()) { | 561 { |
| 541 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 562 ASSERT(startIndex <= info.Length()); |
| 542 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { | 563 Vector<T> result; |
| 543 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument
Index), isolate); | 564 typedef NativeValueTraits<T> TraitsType; |
| 544 return Vector<T>(); | 565 int length = info.Length(); |
| 545 } | 566 result.reserveInitialCapacity(length); |
| 546 | 567 for (int i = startIndex; i < length; ++i) |
| 547 Vector<T> result; | 568 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsol
ate())); |
| 548 result.reserveInitialCapacity(length); | 569 return result; |
| 549 typedef NativeValueTraits<T> TraitsType; | 570 } |
| 550 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 571 |
| 551 for (uint32_t i = 0; i < length; ++i) | 572 // Validates that the passed object is a sequence type per WebIDL spec |
| 552 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate))
; | 573 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence |
| 553 return result; | 574 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3
2_t& length, v8::Isolate* isolate) |
| 554 } | 575 { |
| 555 | 576 // Attempt converting to a sequence if the value is not already an array
but is |
| 556 template <class T> | 577 // any kind of object except for a native Date object or a native RegExp
object. |
| 557 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int
startIndex) | 578 ASSERT(!value->IsArray()); |
| 558 { | 579 // FIXME: Do we really need to special case Date and RegExp object? |
| 559 ASSERT(startIndex <= info.Length()); | 580 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 |
| 560 Vector<T> result; | 581 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { |
| 561 typedef NativeValueTraits<T> TraitsType; | 582 // The caller is responsible for reporting a TypeError. |
| 562 int length = info.Length(); | 583 return v8Undefined(); |
| 563 result.reserveInitialCapacity(length); | 584 } |
| 564 for (int i = startIndex; i < length; ++i) | 585 |
| 565 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate(
))); | 586 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 566 return result; | 587 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
| 567 } | 588 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length"); |
| 568 | 589 |
| 569 // Validates that the passed object is a sequence type per WebIDL spec | 590 // FIXME: The specification states that the length property should be us
ed as fallback, if value |
| 570 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence | 591 // is not a platform object that supports indexed properties. If it supp
orts indexed properties, |
| 571 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t&
length, v8::Isolate* isolate) | 592 // length should actually be one greater than value’s maximum indexed pr
operty index. |
| 572 { | 593 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol))
; |
| 573 // Attempt converting to a sequence if the value is not already an array but
is | 594 |
| 574 // any kind of object except for a native Date object or a native RegExp obj
ect. | 595 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { |
| 575 ASSERT(!value->IsArray()); | 596 // The caller is responsible for reporting a TypeError. |
| 576 // FIXME: Do we really need to special case Date and RegExp object? | 597 return v8Undefined(); |
| 577 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 | 598 } |
| 578 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { | 599 |
| 579 // The caller is responsible for reporting a TypeError. | 600 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); |
| 580 return v8Undefined(); | 601 length = sequenceLength; |
| 581 } | 602 |
| 582 | 603 return v8Value; |
| 583 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 604 } |
| 584 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 605 |
| 585 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length"); | 606 v8::Isolate* toIsolate(ExecutionContext*); |
| 586 | 607 v8::Isolate* toIsolate(LocalFrame*); |
| 587 // FIXME: The specification states that the length property should be used a
s fallback, if value | 608 |
| 588 // is not a platform object that supports indexed properties. If it supports
indexed properties, | 609 WrapperWorldType worldType(v8::Isolate*); |
| 589 // length should actually be one greater than value’s maximum indexed proper
ty index. | 610 WrapperWorldType worldTypeInMainThread(v8::Isolate*); |
| 590 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol)); | 611 |
| 591 | 612 DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*); |
| 592 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { | 613 DOMWindow* toDOMWindow(v8::Handle<v8::Context>); |
| 593 // The caller is responsible for reporting a TypeError. | 614 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>); |
| 594 return v8Undefined(); | 615 |
| 595 } | 616 DOMWindow* callingDOMWindow(v8::Isolate*); |
| 596 | 617 ExecutionContext* callingExecutionContext(v8::Isolate*); |
| 597 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); | 618 DOMWindow* enteredDOMWindow(v8::Isolate*); |
| 598 length = sequenceLength; | 619 Document* currentDocument(v8::Isolate*); |
| 599 return v8Value; | 620 ExecutionContext* currentExecutionContext(v8::Isolate*); |
| 600 } | 621 |
| 601 | 622 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperW
orld. |
| 602 v8::Isolate* toIsolate(ExecutionContext*); | 623 // This method returns an empty context if there is no frame or the frame is
already detached. |
| 603 v8::Isolate* toIsolate(LocalFrame*); | 624 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld*); |
| 604 | 625 // Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld. |
| 605 WrapperWorldType worldType(v8::Isolate*); | 626 // This method returns an empty context if the frame is already detached. |
| 606 WrapperWorldType worldTypeInMainThread(v8::Isolate*); | 627 v8::Local<v8::Context> toV8Context(v8::Isolate*, LocalFrame*, DOMWrapperWorl
d*); |
| 607 | 628 |
| 608 DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*); | 629 // Returns the frame object of the window object associated with |
| 609 DOMWindow* toDOMWindow(v8::Handle<v8::Context>); | 630 // a context, if the window is currently being displayed in the LocalFrame. |
| 610 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>); | 631 LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>); |
| 611 | 632 |
| 612 DOMWindow* callingDOMWindow(v8::Isolate*); | 633 // If the current context causes out of memory, JavaScript setting |
| 613 ExecutionContext* callingExecutionContext(v8::Isolate*); | 634 // is disabled and it returns true. |
| 614 DOMWindow* enteredDOMWindow(v8::Isolate*); | 635 bool handleOutOfMemory(); |
| 615 Document* currentDocument(v8::Isolate*); | 636 v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*); |
| 616 ExecutionContext* currentExecutionContext(v8::Isolate*); | 637 void crashIfV8IsDead(); |
| 617 | 638 |
| 618 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperWorld
. | 639 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value) |
| 619 // This method returns an empty context if there is no frame or the frame is alr
eady detached. | 640 { |
| 620 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld*); | 641 return value->IsNull() || value->IsUndefined(); |
| 621 // Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld. | 642 } |
| 622 // This method returns an empty context if the frame is already detached. | 643 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>); |
| 623 v8::Local<v8::Context> toV8Context(v8::Isolate*, LocalFrame*, DOMWrapperWorld*); | 644 |
| 624 | 645 // Attaches |environment| to |function| and returns it. |
| 625 // Returns the frame object of the window object associated with | 646 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function,
v8::Handle<v8::Value> environment, v8::Isolate* isolate) |
| 626 // a context, if the window is currently being displayed in the LocalFrame. | 647 { |
| 627 LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>); | 648 return v8::Function::New(isolate, function, environment); |
| 628 | 649 } |
| 629 // If the current context causes out of memory, JavaScript setting | 650 |
| 630 // is disabled and it returns true. | 651 // FIXME: This will be soon embedded in the generated code. |
| 631 bool handleOutOfMemory(); | 652 template<class Collection> static void indexedPropertyEnumerator(const v8::P
ropertyCallbackInfo<v8::Array>& info) |
| 632 v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*); | 653 { |
| 633 void crashIfV8IsDead(); | 654 Collection* collection = reinterpret_cast<Collection*>(info.Holder()->Ge
tAlignedPointerFromInternalField(v8DOMWrapperObjectIndex)); |
| 634 | 655 int length = collection->length(); |
| 635 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value) | 656 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), len
gth); |
| 636 { | 657 for (int i = 0; i < length; ++i) { |
| 637 return value->IsNull() || value->IsUndefined(); | 658 // FIXME: Do we need to check that the item function returns a non-n
ull value for this index? |
| 638 } | 659 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate()
, i); |
| 639 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>); | 660 properties->Set(integer, integer); |
| 640 | 661 } |
| 641 // Attaches |environment| to |function| and returns it. | 662 v8SetReturnValue(info, properties); |
| 642 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::
Handle<v8::Value> environment, v8::Isolate* isolate) | 663 } |
| 643 { | 664 |
| 644 return v8::Function::New(isolate, function, environment); | 665 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, co
nst char*); |
| 645 } | 666 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8
::Handle<v8::String>); |
| 646 | 667 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*, v8::H
andle<v8::Value>); |
| 647 // FIXME: This will be soon embedded in the generated code. | 668 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::Str
ing>, v8::Handle<v8::Value>); |
| 648 template<class Collection> static void indexedPropertyEnumerator(const v8::Prope
rtyCallbackInfo<v8::Array>& info) | 669 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*); |
| 649 { | 670 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::
String>); |
| 650 Collection* collection = reinterpret_cast<Collection*>(info.Holder()->GetAli
gnedPointerFromInternalField(v8DOMWrapperObjectIndex)); | 671 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script
Wrappable*, const char*); |
| 651 int length = collection->length(); | 672 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script
Wrappable*, v8::Handle<v8::String>); |
| 652 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length)
; | 673 |
| 653 for (int i = 0; i < length; ++i) { | 674 // These methods store hidden values into an array that is stored in the int
ernal field of a DOM wrapper. |
| 654 // FIXME: Do we need to check that the item function returns a non-null
value for this index? | 675 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int
cacheIndex, v8::Isolate*); |
| 655 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i)
; | 676 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>
, int cacheIndex, v8::Isolate*); |
| 656 properties->Set(integer, integer); | 677 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* ol
dValue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*); |
| 657 } | 678 |
| 658 v8SetReturnValue(info, properties); | 679 // Converts a DOM object to a v8 value. |
| 659 } | 680 // This is a no-inline version of toV8(). If you want to call toV8() |
| 660 | 681 // without creating #include cycles, you can use this function instead. |
| 661 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const
char*); | 682 // Each specialized implementation will be generated. |
| 662 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Ha
ndle<v8::String>); | 683 template<typename T> |
| 663 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*, v8::Handl
e<v8::Value>); | 684 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationC
ontext, v8::Isolate*); |
| 664 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>
, v8::Handle<v8::Value>); | 685 |
| 665 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*); | 686 // Result values for platform object 'deleter' methods, |
| 666 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::Stri
ng>); | 687 // http://www.w3.org/TR/WebIDL/#delete |
| 667 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrap
pable*, const char*); | 688 enum DeleteResult { |
| 668 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrap
pable*, v8::Handle<v8::String>); | 689 DeleteSuccess, |
| 669 | 690 DeleteReject, |
| 670 // These methods store hidden values into an array that is stored in the interna
l field of a DOM wrapper. | 691 DeleteUnknownProperty |
| 671 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac
heIndex, v8::Isolate*); | 692 }; |
| 672 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, in
t cacheIndex, v8::Isolate*); | 693 |
| 673 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldVal
ue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*); | 694 class V8IsolateInterruptor : public ThreadState::Interruptor { |
| 674 | 695 public: |
| 675 // Converts a DOM object to a v8 value. | 696 explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate)
{ } |
| 676 // This is a no-inline version of toV8(). If you want to call toV8() | 697 |
| 677 // without creating #include cycles, you can use this function instead. | 698 static void onInterruptCallback(v8::Isolate* isolate, void* data) |
| 678 // Each specialized implementation will be generated. | 699 { |
| 679 template<typename T> | 700 reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted(); |
| 680 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationConte
xt, v8::Isolate*); | 701 } |
| 681 | 702 |
| 682 // Result values for platform object 'deleter' methods, | 703 virtual void requestInterrupt() OVERRIDE |
| 683 // http://www.w3.org/TR/WebIDL/#delete | 704 { |
| 684 enum DeleteResult { | 705 m_isolate->RequestInterrupt(&onInterruptCallback, this); |
| 685 DeleteSuccess, | 706 } |
| 686 DeleteReject, | 707 |
| 687 DeleteUnknownProperty | 708 virtual void clearInterrupt() OVERRIDE |
| 688 }; | 709 { |
| 689 | 710 m_isolate->ClearInterrupt(); |
| 690 class V8IsolateInterruptor : public ThreadState::Interruptor { | 711 } |
| 691 public: | 712 |
| 692 explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate) { } | 713 private: |
| 693 | 714 v8::Isolate* m_isolate; |
| 694 static void onInterruptCallback(v8::Isolate* isolate, void* data) | 715 }; |
| 695 { | |
| 696 reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted(); | |
| 697 } | |
| 698 | |
| 699 virtual void requestInterrupt() OVERRIDE | |
| 700 { | |
| 701 m_isolate->RequestInterrupt(&onInterruptCallback, this); | |
| 702 } | |
| 703 | |
| 704 virtual void clearInterrupt() OVERRIDE | |
| 705 { | |
| 706 m_isolate->ClearInterrupt(); | |
| 707 } | |
| 708 | |
| 709 private: | |
| 710 v8::Isolate* m_isolate; | |
| 711 }; | |
| 712 | |
| 713 class V8BindingTestScope { | |
| 714 public: | |
| 715 static PassOwnPtr<V8BindingTestScope> create(v8::Isolate*); | |
| 716 | |
| 717 private: | |
| 718 explicit V8BindingTestScope(v8::Isolate*); | |
| 719 | |
| 720 v8::HandleScope m_handleScope; | |
| 721 v8::Handle<v8::Context> m_context; | |
| 722 v8::Context::Scope m_contextScope; | |
| 723 RefPtr<DOMWrapperWorld> m_world; | |
| 724 OwnPtr<V8PerContextData> m_perContextData; | |
| 725 }; | |
| 726 | 716 |
| 727 } // namespace WebCore | 717 } // namespace WebCore |
| 728 | 718 |
| 729 #endif // V8Binding_h | 719 #endif // V8Binding_h |
| OLD | NEW |