| 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 ExceptionState; | 52 class ExecutionContext; |
| 53 class LocalFrame; | 53 class ExceptionState; |
| 54 class NodeFilter; | 54 class LocalFrame; |
| 55 class ExecutionContext; | 55 class NodeFilter; |
| 56 class ScriptWrappable; | 56 class ScriptWrappable; |
| 57 class XPathNSResolver; | 57 class V8PerContextData; |
| 58 | 58 class XPathNSResolver; |
| 59 const int kMaxRecursionDepth = 22; | 59 |
| 60 | 60 const int kMaxRecursionDepth = 22; |
| 61 // Schedule a JavaScript error to be thrown. | 61 |
| 62 v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*); | 62 // Schedule a JavaScript error to be thrown. |
| 63 | 63 v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*); |
| 64 // Schedule a JavaScript error to be thrown. | 64 |
| 65 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); | 65 // Schedule a JavaScript error to be thrown. |
| 66 | 66 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*); |
| 67 // A helper for throwing JavaScript TypeError. | 67 |
| 68 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); | 68 // A helper for throwing JavaScript TypeError. |
| 69 | 69 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*); |
| 70 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); | 70 |
| 71 | 71 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator(); |
| 72 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v
8::Value>& info, int index) | 72 |
| 73 { | 73 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v8::V
alue>& info, int index) |
| 74 return index >= info.Length() ? v8::Local<v8::Value>() : info[index]; | 74 { |
| 75 } | 75 return index >= info.Length() ? v8::Local<v8::Value>() : info[index]; |
| 76 | 76 } |
| 77 template<typename CallbackInfo, typename V> | 77 |
| 78 inline void v8SetReturnValue(const CallbackInfo& info, V v) | 78 template<typename CallbackInfo, typename V> |
| 79 { | 79 inline void v8SetReturnValue(const CallbackInfo& info, V v) |
| 80 info.GetReturnValue().Set(v); | 80 { |
| 81 } | 81 info.GetReturnValue().Set(v); |
| 82 | 82 } |
| 83 template<typename CallbackInfo> | 83 |
| 84 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v) | 84 template<typename CallbackInfo> |
| 85 { | 85 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v) |
| 86 info.GetReturnValue().Set(v); | 86 { |
| 87 } | 87 info.GetReturnValue().Set(v); |
| 88 | 88 } |
| 89 template<typename CallbackInfo> | 89 |
| 90 inline void v8SetReturnValueInt(const CallbackInfo& info, int v) | 90 template<typename CallbackInfo> |
| 91 { | 91 inline void v8SetReturnValueInt(const CallbackInfo& info, int v) |
| 92 info.GetReturnValue().Set(v); | 92 { |
| 93 } | 93 info.GetReturnValue().Set(v); |
| 94 | 94 } |
| 95 template<typename CallbackInfo> | 95 |
| 96 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v) | 96 template<typename CallbackInfo> |
| 97 { | 97 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v) |
| 98 info.GetReturnValue().Set(v); | 98 { |
| 99 } | 99 info.GetReturnValue().Set(v); |
| 100 | 100 } |
| 101 template<typename CallbackInfo> | 101 |
| 102 inline void v8SetReturnValueNull(const CallbackInfo& info) | 102 template<typename CallbackInfo> |
| 103 { | 103 inline void v8SetReturnValueNull(const CallbackInfo& info) |
| 104 info.GetReturnValue().SetNull(); | 104 { |
| 105 } | 105 info.GetReturnValue().SetNull(); |
| 106 | 106 } |
| 107 template<typename CallbackInfo> | 107 |
| 108 inline void v8SetReturnValueUndefined(const CallbackInfo& info) | 108 template<typename CallbackInfo> |
| 109 { | 109 inline void v8SetReturnValueUndefined(const CallbackInfo& info) |
| 110 info.GetReturnValue().SetUndefined(); | 110 { |
| 111 } | 111 info.GetReturnValue().SetUndefined(); |
| 112 | 112 } |
| 113 template<typename CallbackInfo> | 113 |
| 114 inline void v8SetReturnValueEmptyString(const CallbackInfo& info) | 114 template<typename CallbackInfo> |
| 115 { | 115 inline void v8SetReturnValueEmptyString(const CallbackInfo& info) |
| 116 info.GetReturnValue().SetEmptyString(); | 116 { |
| 117 } | 117 info.GetReturnValue().SetEmptyString(); |
| 118 | 118 } |
| 119 template <class CallbackInfo> | 119 |
| 120 inline void v8SetReturnValueString(const CallbackInfo& info, const String& s
tring, v8::Isolate* isolate) | 120 template <class CallbackInfo> |
| 121 { | 121 inline void v8SetReturnValueString(const CallbackInfo& info, const String& strin
g, v8::Isolate* isolate) |
| 122 if (string.isNull()) { | 122 { |
| 123 v8SetReturnValueEmptyString(info); | 123 if (string.isNull()) { |
| 124 return; | 124 v8SetReturnValueEmptyString(info); |
| 125 } | 125 return; |
| 126 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString
(info.GetReturnValue(), string.impl()); | 126 } |
| 127 } | 127 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf
o.GetReturnValue(), string.impl()); |
| 128 | 128 } |
| 129 template <class CallbackInfo> | 129 |
| 130 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const Str
ing& string, v8::Isolate* isolate) | 130 template <class CallbackInfo> |
| 131 { | 131 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String&
string, v8::Isolate* isolate) |
| 132 if (string.isNull()) { | 132 { |
| 133 v8SetReturnValueNull(info); | 133 if (string.isNull()) { |
| 134 return; | 134 v8SetReturnValueNull(info); |
| 135 } | 135 return; |
| 136 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString
(info.GetReturnValue(), string.impl()); | 136 } |
| 137 } | 137 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf
o.GetReturnValue(), string.impl()); |
| 138 | 138 } |
| 139 template <class CallbackInfo> | 139 |
| 140 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, cons
t String& string, v8::Isolate* isolate) | 140 template <class CallbackInfo> |
| 141 { | 141 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const St
ring& string, v8::Isolate* isolate) |
| 142 if (string.isNull()) { | 142 { |
| 143 v8SetReturnValueUndefined(info); | 143 if (string.isNull()) { |
| 144 return; | 144 v8SetReturnValueUndefined(info); |
| 145 } | 145 return; |
| 146 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString
(info.GetReturnValue(), string.impl()); | 146 } |
| 147 } | 147 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(inf
o.GetReturnValue(), string.impl()); |
| 148 | 148 } |
| 149 // Convert v8::String to a WTF::String. If the V8 string is not already | 149 |
| 150 // an external string then it is transformed into an external string at this | 150 // Convert v8::String to a WTF::String. If the V8 string is not already |
| 151 // point to avoid repeated conversions. | 151 // an external string then it is transformed into an external string at this |
| 152 inline String toCoreString(v8::Handle<v8::String> value) | 152 // point to avoid repeated conversions. |
| 153 { | 153 inline String toCoreString(v8::Handle<v8::String> value) |
| 154 return v8StringToWebCoreString<String>(value, Externalize); | 154 { |
| 155 } | 155 return v8StringToWebCoreString<String>(value, Externalize); |
| 156 | 156 } |
| 157 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value) | 157 |
| 158 { | 158 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value) |
| 159 if (value.IsEmpty() || value->IsNull()) | 159 { |
| 160 return String(); | 160 if (value.IsEmpty() || value->IsNull()) |
| 161 return toCoreString(value); | 161 return String(); |
| 162 } | 162 return toCoreString(value); |
| 163 | 163 } |
| 164 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> va
lue) | 164 |
| 165 { | 165 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value) |
| 166 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) | 166 { |
| 167 return String(); | 167 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) |
| 168 return toCoreString(value); | 168 return String(); |
| 169 } | 169 return toCoreString(value); |
| 170 | 170 } |
| 171 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value) | 171 |
| 172 { | 172 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value) |
| 173 return v8StringToWebCoreString<AtomicString>(value, Externalize); | 173 { |
| 174 } | 174 return v8StringToWebCoreString<AtomicString>(value, Externalize); |
| 175 | 175 } |
| 176 // This method will return a null String if the v8::Value does not contain a
v8::String. | 176 |
| 177 // It will not call ToString() on the v8::Value. If you want ToString() to b
e called, | 177 // This method will return a null String if the v8::Value does not contain a v8:
:String. |
| 178 // please use the V8TRYCATCH_FOR_V8STRINGRESOURCE_*() macros instead. | 178 // It will not call ToString() on the v8::Value. If you want ToString() to be ca
lled, |
| 179 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> val
ue) | 179 // please use the V8TRYCATCH_FOR_V8STRINGRESOURCE_*() macros instead. |
| 180 { | 180 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value) |
| 181 if (value.IsEmpty() || !value->IsString()) | 181 { |
| 182 return String(); | 182 if (value.IsEmpty() || !value->IsString()) |
| 183 | 183 return String(); |
| 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 gi
ven | 188 // Return a V8 external string that shares the underlying buffer with the given |
| 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& s
tring) | 191 inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& strin
g) |
| 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(
string.impl(), isolate); | 195 return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(stri
ng.impl(), isolate); |
| 196 } | 196 } |
| 197 | 197 |
| 198 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const cha
r* str) | 198 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s
tr) |
| 199 { | 199 { |
| 200 ASSERT(isolate); | 200 ASSERT(isolate); |
| 201 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedSt
ring, strlen(str)); | 201 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString
, strlen(str)); |
| 202 } | 202 } |
| 203 | 203 |
| 204 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const cha
r* str, size_t length) | 204 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* s
tr, size_t length) |
| 205 { | 205 { |
| 206 ASSERT(isolate); | 206 ASSERT(isolate); |
| 207 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedSt
ring, length); | 207 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString
, 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::Iso
late* isolate) | 217 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate
* 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
::Isolate* isolate) | 225 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Is
olate* 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& val
ue, v8::Isolate* isolate) | 233 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value,
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& va
lue, v8::Isolate* isolate) | 249 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value,
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:
:Isolate* isolate) | 257 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Iso
late* 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
::Isolate* isolate) | 265 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Is
olate* 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:
:Isolate* isolate) | 272 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Iso
late* 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.
begin(); iter != end; ++iter) | 278 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begi
n(); iter != end; ++iter) |
| 279 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8V
alue(*iter, isolate)); | 279 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value
(*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.en
d(); | 288 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end(); |
| 289 typedef V8ValueTraits<T> TraitsType; | 289 typedef V8ValueTraits<T> TraitsType; |
| 290 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = itera
tor.begin(); iter != end; ++iter) | 290 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.
begin(); iter != end; ++iter) |
| 291 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8V
alue(*iter, isolate)); | 291 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value
(*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, Excepti
onState&); | 305 int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionSt
ate&); |
| 306 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) | 306 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState
) |
| 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, Excep
tionState&); | 317 uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); |
| 318 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) | 318 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) |
| 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 canno
t fail. | 323 // Convert a value to a 8-bit unsigned integer assuming the conversion cannot fa
il. |
| 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, Excep
tionState&); | 329 int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); |
| 330 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) | 330 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) |
| 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, Exc
eptionState&); | 341 uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); |
| 342 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& except
ionState) | 342 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) |
| 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 cann
ot fail. | 347 // Convert a value to a 16-bit unsigned integer assuming the conversion cannot f
ail. |
| 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, Excep
tionState&); | 353 int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); |
| 354 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) | 354 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) |
| 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, Exc
eptionState&); | 365 uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); |
| 366 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& except
ionState) | 366 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) |
| 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 cann
ot fail. | 371 // Convert a value to a 32-bit unsigned integer assuming the conversion cannot f
ail. |
| 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, Excep
tionState&); | 377 int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Exception
State&); |
| 378 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptio
nState) | 378 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionSta
te) |
| 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, Exc
eptionState&); | 389 uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, Excepti
onState&); |
| 390 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& except
ionState) | 390 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionS
tate) |
| 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 cann
ot fail. | 395 // Convert a value to a 64-bit unsigned integer assuming the conversion cannot f
ail. |
| 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 canno
t fail. | 401 // Convert a value to a single precision float assuming the conversion cannot fa
il. |
| 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) |
| 403 { | 453 { |
| 404 return static_cast<float>(value->NumberValue()); | 454 return static_cast<float>(value->NumberValue()); |
| 405 } | 455 } |
| 406 | 456 }; |
| 407 inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate) | 457 |
| 408 { | 458 template<> |
| 409 return value ? v8::True(isolate) : v8::False(isolate); | 459 struct NativeValueTraits<double> { |
| 410 } | 460 static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Iso
late* isolate) |
| 411 | 461 { |
| 412 inline double toCoreDate(v8::Handle<v8::Value> object) | 462 return static_cast<double>(value->NumberValue()); |
| 413 { | 463 } |
| 414 if (object->IsDate()) | 464 }; |
| 415 return v8::Handle<v8::Date>::Cast(object)->ValueOf(); | 465 |
| 416 if (object->IsNumber()) | 466 template<> |
| 417 return object->NumberValue(); | 467 struct NativeValueTraits<v8::Handle<v8::Value> > { |
| 418 return std::numeric_limits<double>::quiet_NaN(); | 468 static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>&
value, v8::Isolate* isolate) |
| 419 } | 469 { |
| 420 | 470 return value; |
| 421 inline v8::Handle<v8::Value> v8DateOrNull(double value, v8::Isolate* isolate
) | 471 } |
| 422 { | 472 }; |
| 423 ASSERT(isolate); | 473 |
| 424 return std::isfinite(value) ? v8::Date::New(isolate, value) : v8::Handle
<v8::Value>::Cast(v8::Null(isolate)); | 474 // Converts a JavaScript value to an array as per the Web IDL specification: |
| 425 } | 475 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
| 426 | 476 template <class T, class V8T> |
| 427 // FIXME: Remove the special casing for NodeFilter and XPathNSResolver. | 477 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value, ui
nt32_t length, v8::Isolate* isolate, bool* success = 0) |
| 428 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*); | 478 { |
| 429 PassRefPtrWillBeRawPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Val
ue>, v8::Isolate*); | 479 Vector<RefPtr<T> > result; |
| 430 | 480 result.reserveInitialCapacity(length); |
| 431 template<class T> struct NativeValueTraits; | 481 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
| 432 | 482 for (uint32_t i = 0; i < length; ++i) { |
| 433 template<> | 483 v8::Handle<v8::Value> element = object->Get(i); |
| 434 struct NativeValueTraits<String> { | 484 if (V8T::hasInstance(element, isolate)) { |
| 435 static inline String nativeValue(const v8::Handle<v8::Value>& value, v8:
:Isolate* isolate) | 485 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast(
element); |
| 436 { | 486 result.uncheckedAppend(V8T::toNative(elementObject)); |
| 437 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringVal
ue, value, String()); | 487 } else { |
| 438 return stringValue; | 488 if (success) |
| 439 } | 489 *success = false; |
| 440 }; | 490 throwTypeError("Invalid Array element type", isolate); |
| 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); | |
| 512 return Vector<RefPtr<T> >(); | 491 return Vector<RefPtr<T> >(); |
| 513 } | 492 } |
| 514 | 493 } |
| 515 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su
ccess); | 494 return result; |
| 516 } | 495 } |
| 517 | 496 |
| 518 template <class T, class V8T> | 497 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8::
Isolate*); |
| 519 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const St
ring& propertyName, v8::Isolate* isolate, bool* success = 0) | 498 |
| 520 { | 499 template <class T, class V8T> |
| 521 if (success) | 500 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argument
Index, v8::Isolate* isolate, bool* success = 0) |
| 522 *success = true; | 501 { |
| 523 | 502 if (success) |
| 524 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 503 *success = true; |
| 525 uint32_t length = 0; | 504 |
| 526 if (value->IsArray()) { | 505 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 527 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 506 uint32_t length = 0; |
| 528 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { | 507 if (value->IsArray()) { |
| 529 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyN
ame), isolate); | 508 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 530 return Vector<RefPtr<T> >(); | 509 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { |
| 531 } | 510 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument
Index), isolate); |
| 532 | 511 return Vector<RefPtr<T> >(); |
| 533 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, su
ccess); | 512 } |
| 534 } | 513 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, succes
s); |
| 535 | 514 } |
| 536 // Converts a JavaScript value to an array as per the Web IDL specification: | 515 |
| 537 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array | 516 template <class T, class V8T> |
| 538 template <class T> | 517 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const String
& propertyName, v8::Isolate* isolate, bool* success = 0) |
| 539 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::
Isolate* isolate) | 518 { |
| 540 { | 519 if (success) |
| 541 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 520 *success = true; |
| 542 uint32_t length = 0; | 521 |
| 543 if (value->IsArray()) { | 522 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 544 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); | 523 uint32_t length = 0; |
| 545 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { | 524 if (value->IsArray()) { |
| 546 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argu
mentIndex), isolate); | 525 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 547 return Vector<T>(); | 526 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { |
| 548 } | 527 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName)
, isolate); |
| 549 | 528 return Vector<RefPtr<T> >(); |
| 550 Vector<T> result; | 529 } |
| 551 result.reserveInitialCapacity(length); | 530 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, succes
s); |
| 552 typedef NativeValueTraits<T> TraitsType; | 531 } |
| 553 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 532 |
| 554 for (uint32_t i = 0; i < length; ++i) | 533 // Converts a JavaScript value to an array as per the Web IDL specification: |
| 555 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isola
te)); | 534 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array |
| 556 return result; | 535 template <class T> |
| 557 } | 536 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isol
ate* isolate) |
| 558 | 537 { |
| 559 template <class T> | 538 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 560 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info,
int startIndex) | 539 uint32_t length = 0; |
| 561 { | 540 if (value->IsArray()) { |
| 562 ASSERT(startIndex <= info.Length()); | 541 length = v8::Local<v8::Array>::Cast(v8Value)->Length(); |
| 563 Vector<T> result; | 542 } else if (toV8Sequence(value, length, isolate).IsEmpty()) { |
| 564 typedef NativeValueTraits<T> TraitsType; | 543 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argument
Index), isolate); |
| 565 int length = info.Length(); | 544 return Vector<T>(); |
| 566 result.reserveInitialCapacity(length); | 545 } |
| 567 for (int i = startIndex; i < length; ++i) | 546 |
| 568 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsol
ate())); | 547 Vector<T> result; |
| 569 return result; | 548 result.reserveInitialCapacity(length); |
| 570 } | 549 typedef NativeValueTraits<T> TraitsType; |
| 571 | 550 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
| 572 // Validates that the passed object is a sequence type per WebIDL spec | 551 for (uint32_t i = 0; i < length; ++i) |
| 573 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence | 552 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate))
; |
| 574 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint3
2_t& length, v8::Isolate* isolate) | 553 return result; |
| 575 { | 554 } |
| 576 // Attempt converting to a sequence if the value is not already an array
but is | 555 |
| 577 // any kind of object except for a native Date object or a native RegExp
object. | 556 template <class T> |
| 578 ASSERT(!value->IsArray()); | 557 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int
startIndex) |
| 579 // FIXME: Do we really need to special case Date and RegExp object? | 558 { |
| 580 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 | 559 ASSERT(startIndex <= info.Length()); |
| 581 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { | 560 Vector<T> result; |
| 582 // The caller is responsible for reporting a TypeError. | 561 typedef NativeValueTraits<T> TraitsType; |
| 583 return v8Undefined(); | 562 int length = info.Length(); |
| 584 } | 563 result.reserveInitialCapacity(length); |
| 585 | 564 for (int i = startIndex; i < length; ++i) |
| 586 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); | 565 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate(
))); |
| 587 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); | 566 return result; |
| 588 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length"); | 567 } |
| 589 | 568 |
| 590 // FIXME: The specification states that the length property should be us
ed as fallback, if value | 569 // Validates that the passed object is a sequence type per WebIDL spec |
| 591 // is not a platform object that supports indexed properties. If it supp
orts indexed properties, | 570 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence |
| 592 // length should actually be one greater than value’s maximum indexed pr
operty index. | 571 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t&
length, v8::Isolate* isolate) |
| 593 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol))
; | 572 { |
| 594 | 573 // Attempt converting to a sequence if the value is not already an array but
is |
| 595 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { | 574 // any kind of object except for a native Date object or a native RegExp obj
ect. |
| 596 // The caller is responsible for reporting a TypeError. | 575 ASSERT(!value->IsArray()); |
| 597 return v8Undefined(); | 576 // FIXME: Do we really need to special case Date and RegExp object? |
| 598 } | 577 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806 |
| 599 | 578 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) { |
| 600 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); | 579 // The caller is responsible for reporting a TypeError. |
| 601 length = sequenceLength; | 580 return v8Undefined(); |
| 602 | 581 } |
| 603 return v8Value; | 582 |
| 604 } | 583 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value)); |
| 605 | 584 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value); |
| 606 v8::Isolate* toIsolate(ExecutionContext*); | 585 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length"); |
| 607 v8::Isolate* toIsolate(LocalFrame*); | 586 |
| 608 | 587 // FIXME: The specification states that the length property should be used a
s fallback, if value |
| 609 WrapperWorldType worldType(v8::Isolate*); | 588 // is not a platform object that supports indexed properties. If it supports
indexed properties, |
| 610 WrapperWorldType worldTypeInMainThread(v8::Isolate*); | 589 // length should actually be one greater than value’s maximum indexed proper
ty index. |
| 611 | 590 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol)); |
| 612 DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*); | 591 |
| 613 DOMWindow* toDOMWindow(v8::Handle<v8::Context>); | 592 if (lengthValue->IsUndefined() || lengthValue->IsNull()) { |
| 614 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>); | 593 // The caller is responsible for reporting a TypeError. |
| 615 | 594 return v8Undefined(); |
| 616 DOMWindow* callingDOMWindow(v8::Isolate*); | 595 } |
| 617 ExecutionContext* callingExecutionContext(v8::Isolate*); | 596 |
| 618 DOMWindow* enteredDOMWindow(v8::Isolate*); | 597 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value()); |
| 619 Document* currentDocument(v8::Isolate*); | 598 length = sequenceLength; |
| 620 ExecutionContext* currentExecutionContext(v8::Isolate*); | 599 return v8Value; |
| 621 | 600 } |
| 622 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperW
orld. | 601 |
| 623 // This method returns an empty context if there is no frame or the frame is
already detached. | 602 v8::Isolate* toIsolate(ExecutionContext*); |
| 624 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld*); | 603 v8::Isolate* toIsolate(LocalFrame*); |
| 625 // Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld. | 604 |
| 626 // This method returns an empty context if the frame is already detached. | 605 WrapperWorldType worldType(v8::Isolate*); |
| 627 v8::Local<v8::Context> toV8Context(v8::Isolate*, LocalFrame*, DOMWrapperWorl
d*); | 606 WrapperWorldType worldTypeInMainThread(v8::Isolate*); |
| 628 | 607 |
| 629 // Returns the frame object of the window object associated with | 608 DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*); |
| 630 // a context, if the window is currently being displayed in the LocalFrame. | 609 DOMWindow* toDOMWindow(v8::Handle<v8::Context>); |
| 631 LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>); | 610 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>); |
| 632 | 611 |
| 633 // If the current context causes out of memory, JavaScript setting | 612 DOMWindow* callingDOMWindow(v8::Isolate*); |
| 634 // is disabled and it returns true. | 613 ExecutionContext* callingExecutionContext(v8::Isolate*); |
| 635 bool handleOutOfMemory(); | 614 DOMWindow* enteredDOMWindow(v8::Isolate*); |
| 636 v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*); | 615 Document* currentDocument(v8::Isolate*); |
| 637 void crashIfV8IsDead(); | 616 ExecutionContext* currentExecutionContext(v8::Isolate*); |
| 638 | 617 |
| 639 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value) | 618 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperWorld
. |
| 640 { | 619 // This method returns an empty context if there is no frame or the frame is alr
eady detached. |
| 641 return value->IsNull() || value->IsUndefined(); | 620 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld*); |
| 642 } | 621 // Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld. |
| 643 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>); | 622 // This method returns an empty context if the frame is already detached. |
| 644 | 623 v8::Local<v8::Context> toV8Context(v8::Isolate*, LocalFrame*, DOMWrapperWorld*); |
| 645 // Attaches |environment| to |function| and returns it. | 624 |
| 646 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function,
v8::Handle<v8::Value> environment, v8::Isolate* isolate) | 625 // Returns the frame object of the window object associated with |
| 647 { | 626 // a context, if the window is currently being displayed in the LocalFrame. |
| 648 return v8::Function::New(isolate, function, environment); | 627 LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>); |
| 649 } | 628 |
| 650 | 629 // If the current context causes out of memory, JavaScript setting |
| 651 // FIXME: This will be soon embedded in the generated code. | 630 // is disabled and it returns true. |
| 652 template<class Collection> static void indexedPropertyEnumerator(const v8::P
ropertyCallbackInfo<v8::Array>& info) | 631 bool handleOutOfMemory(); |
| 653 { | 632 v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*); |
| 654 Collection* collection = reinterpret_cast<Collection*>(info.Holder()->Ge
tAlignedPointerFromInternalField(v8DOMWrapperObjectIndex)); | 633 void crashIfV8IsDead(); |
| 655 int length = collection->length(); | 634 |
| 656 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), len
gth); | 635 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value) |
| 657 for (int i = 0; i < length; ++i) { | 636 { |
| 658 // FIXME: Do we need to check that the item function returns a non-n
ull value for this index? | 637 return value->IsNull() || value->IsUndefined(); |
| 659 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate()
, i); | 638 } |
| 660 properties->Set(integer, integer); | 639 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>); |
| 661 } | 640 |
| 662 v8SetReturnValue(info, properties); | 641 // Attaches |environment| to |function| and returns it. |
| 663 } | 642 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::
Handle<v8::Value> environment, v8::Isolate* isolate) |
| 664 | 643 { |
| 665 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, co
nst char*); | 644 return v8::Function::New(isolate, function, environment); |
| 666 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8
::Handle<v8::String>); | 645 } |
| 667 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*, v8::H
andle<v8::Value>); | 646 |
| 668 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::Str
ing>, v8::Handle<v8::Value>); | 647 // FIXME: This will be soon embedded in the generated code. |
| 669 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*); | 648 template<class Collection> static void indexedPropertyEnumerator(const v8::Prope
rtyCallbackInfo<v8::Array>& info) |
| 670 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::
String>); | 649 { |
| 671 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script
Wrappable*, const char*); | 650 Collection* collection = reinterpret_cast<Collection*>(info.Holder()->GetAli
gnedPointerFromInternalField(v8DOMWrapperObjectIndex)); |
| 672 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, Script
Wrappable*, v8::Handle<v8::String>); | 651 int length = collection->length(); |
| 673 | 652 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length)
; |
| 674 // These methods store hidden values into an array that is stored in the int
ernal field of a DOM wrapper. | 653 for (int i = 0; i < length; ++i) { |
| 675 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int
cacheIndex, v8::Isolate*); | 654 // FIXME: Do we need to check that the item function returns a non-null
value for this index? |
| 676 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>
, int cacheIndex, v8::Isolate*); | 655 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i)
; |
| 677 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* ol
dValue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*); | 656 properties->Set(integer, integer); |
| 678 | 657 } |
| 679 // Converts a DOM object to a v8 value. | 658 v8SetReturnValue(info, properties); |
| 680 // This is a no-inline version of toV8(). If you want to call toV8() | 659 } |
| 681 // without creating #include cycles, you can use this function instead. | 660 |
| 682 // Each specialized implementation will be generated. | 661 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const
char*); |
| 683 template<typename T> | 662 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Ha
ndle<v8::String>); |
| 684 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationC
ontext, v8::Isolate*); | 663 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*, v8::Handl
e<v8::Value>); |
| 685 | 664 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>
, v8::Handle<v8::Value>); |
| 686 // Result values for platform object 'deleter' methods, | 665 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*); |
| 687 // http://www.w3.org/TR/WebIDL/#delete | 666 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::Stri
ng>); |
| 688 enum DeleteResult { | 667 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrap
pable*, const char*); |
| 689 DeleteSuccess, | 668 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrap
pable*, v8::Handle<v8::String>); |
| 690 DeleteReject, | 669 |
| 691 DeleteUnknownProperty | 670 // These methods store hidden values into an array that is stored in the interna
l field of a DOM wrapper. |
| 692 }; | 671 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac
heIndex, v8::Isolate*); |
| 693 | 672 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, in
t cacheIndex, v8::Isolate*); |
| 694 class V8IsolateInterruptor : public ThreadState::Interruptor { | 673 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldVal
ue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*); |
| 695 public: | 674 |
| 696 explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate)
{ } | 675 // Converts a DOM object to a v8 value. |
| 697 | 676 // This is a no-inline version of toV8(). If you want to call toV8() |
| 698 static void onInterruptCallback(v8::Isolate* isolate, void* data) | 677 // without creating #include cycles, you can use this function instead. |
| 699 { | 678 // Each specialized implementation will be generated. |
| 700 reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted(); | 679 template<typename T> |
| 701 } | 680 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationConte
xt, v8::Isolate*); |
| 702 | 681 |
| 703 virtual void requestInterrupt() OVERRIDE | 682 // Result values for platform object 'deleter' methods, |
| 704 { | 683 // http://www.w3.org/TR/WebIDL/#delete |
| 705 m_isolate->RequestInterrupt(&onInterruptCallback, this); | 684 enum DeleteResult { |
| 706 } | 685 DeleteSuccess, |
| 707 | 686 DeleteReject, |
| 708 virtual void clearInterrupt() OVERRIDE | 687 DeleteUnknownProperty |
| 709 { | 688 }; |
| 710 m_isolate->ClearInterrupt(); | 689 |
| 711 } | 690 class V8IsolateInterruptor : public ThreadState::Interruptor { |
| 712 | 691 public: |
| 713 private: | 692 explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate) { } |
| 714 v8::Isolate* m_isolate; | 693 |
| 715 }; | 694 static void onInterruptCallback(v8::Isolate* isolate, void* data) |
| 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 }; |
| 716 | 726 |
| 717 } // namespace WebCore | 727 } // namespace WebCore |
| 718 | 728 |
| 719 #endif // V8Binding_h | 729 #endif // V8Binding_h |
| OLD | NEW |