#include "bindings/v8/DOMWrapperWorld.h"
#include "bindings/v8/ExceptionMessages.h"
+#include "bindings/v8/ScriptValue.h"
#include "bindings/v8/V8BindingMacros.h"
#include "bindings/v8/V8PerIsolateData.h"
#include "bindings/v8/V8StringResource.h"
#include "bindings/v8/V8ThrowException.h"
#include "bindings/v8/V8ValueCache.h"
-#include "heap/Heap.h"
+#include "platform/heap/Heap.h"
#include "wtf/MathExtras.h"
#include "wtf/text/AtomicString.h"
#include <v8.h>
namespace WebCore {
- class DOMWindow;
- class Document;
- class ExceptionState;
- class Frame;
- class NodeFilter;
- class ExecutionContext;
- class ScriptWrappable;
- class XPathNSResolver;
+class LocalDOMWindow;
+class Document;
+class EventListener;
+class ExecutionContext;
+class ExceptionState;
+class LocalFrame;
+class NodeFilter;
+class ScriptWrappable;
+class XPathNSResolver;
+
+namespace TraceEvent {
+class ConvertableToTraceFormat;
+}
+
+const int kMaxRecursionDepth = 22;
+
+// Schedule a JavaScript error to be thrown.
+v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*);
+
+// Schedule a JavaScript error to be thrown.
+v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*);
+
+// A helper for throwing JavaScript TypeError.
+v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*);
+
+// Helpers for throwing JavaScript TypeErrors for arity mismatches.
+void throwArityTypeErrorForMethod(const char* method, const char* type, const char* valid, unsigned provided, v8::Isolate*);
+void throwArityTypeErrorForConstructor(const char* type, const char* valid, unsigned provided, v8::Isolate*);
+void throwArityTypeError(ExceptionState&, const char* valid, unsigned provided);
+void throwMinimumArityTypeErrorForMethod(const char* method, const char* type, unsigned expected, unsigned providedLeastNumMandatoryParams, v8::Isolate*);
+void throwMinimumArityTypeErrorForConstructor(const char* type, unsigned expected, unsigned providedLeastNumMandatoryParams, v8::Isolate*);
+void throwMinimumArityTypeError(ExceptionState&, unsigned expected, unsigned providedLeastNumMandatoryParams);
+
+v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator();
+
+inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v8::Value>& info, int index)
+{
+ return index >= info.Length() ? v8::Local<v8::Value>() : info[index];
+}
+
+template<typename CallbackInfo, typename V>
+inline void v8SetReturnValue(const CallbackInfo& info, V v)
+{
+ info.GetReturnValue().Set(v);
+}
+
+template<typename CallbackInfo>
+inline void v8SetReturnValueBool(const CallbackInfo& info, bool v)
+{
+ info.GetReturnValue().Set(v);
+}
+
+template<typename CallbackInfo>
+inline void v8SetReturnValueInt(const CallbackInfo& info, int v)
+{
+ info.GetReturnValue().Set(v);
+}
+
+template<typename CallbackInfo>
+inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v)
+{
+ info.GetReturnValue().Set(v);
+}
+
+template<typename CallbackInfo>
+inline void v8SetReturnValueNull(const CallbackInfo& info)
+{
+ info.GetReturnValue().SetNull();
+}
+
+template<typename CallbackInfo>
+inline void v8SetReturnValueUndefined(const CallbackInfo& info)
+{
+ info.GetReturnValue().SetUndefined();
+}
+
+template<typename CallbackInfo>
+inline void v8SetReturnValueEmptyString(const CallbackInfo& info)
+{
+ info.GetReturnValue().SetEmptyString();
+}
+
+template <class CallbackInfo>
+inline void v8SetReturnValueString(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
+{
+ if (string.isNull()) {
+ v8SetReturnValueEmptyString(info);
+ return;
+ }
+ V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
+}
+
+template <class CallbackInfo>
+inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
+{
+ if (string.isNull()) {
+ v8SetReturnValueNull(info);
+ return;
+ }
+ V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
+}
+
+template <class CallbackInfo>
+inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
+{
+ if (string.isNull()) {
+ v8SetReturnValueUndefined(info);
+ return;
+ }
+ V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
+}
+
+// Convert v8::String to a WTF::String. If the V8 string is not already
+// an external string then it is transformed into an external string at this
+// point to avoid repeated conversions.
+inline String toCoreString(v8::Handle<v8::String> value)
+{
+ return v8StringToWebCoreString<String>(value, Externalize);
+}
+
+inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value)
+{
+ if (value.IsEmpty() || value->IsNull())
+ return String();
+ return toCoreString(value);
+}
+
+inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value)
+{
+ if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
+ return String();
+ return toCoreString(value);
+}
+
+inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value)
+{
+ return v8StringToWebCoreString<AtomicString>(value, Externalize);
+}
+
+// This method will return a null String if the v8::Value does not contain a v8::String.
+// It will not call ToString() on the v8::Value. If you want ToString() to be called,
+// please use the TONATIVE_FOR_V8STRINGRESOURCE_*() macros instead.
+inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value)
+{
+ if (value.IsEmpty() || !value->IsString())
+ return String();
+ return toCoreString(value.As<v8::String>());
+}
+
+// Convert a string to a V8 string.
+// Return a V8 external string that shares the underlying buffer with the given
+// WebCore string. The reference counting mechanism is used to keep the
+// underlying buffer alive while the string is still live in the V8 engine.
+inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& string)
+{
+ if (string.isNull())
+ return v8::String::Empty(isolate);
+ return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(string.impl(), isolate);
+}
+
+inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* str)
+{
+ ASSERT(isolate);
+ return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString, strlen(str));
+}
- const int kMaxRecursionDepth = 22;
+inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* str, size_t length)
+{
+ ASSERT(isolate);
+ return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString, length);
+}
- // Schedule a DOM exception to be thrown, if the exception code is different
- // from zero.
- v8::Handle<v8::Value> setDOMException(int, v8::Isolate*);
- v8::Handle<v8::Value> setDOMException(int, const String&, v8::Isolate*);
+inline v8::Handle<v8::Value> v8Undefined()
+{
+ return v8::Handle<v8::Value>();
+}
- // Schedule a JavaScript error to be thrown.
- v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*);
-
- // Schedule a JavaScript error to be thrown.
- v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*);
-
- // A helper for throwing JavaScript TypeError.
- v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*);
-
- v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator();
-
- v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8::Isolate*);
-
- inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v8::Value>& info, int index)
+template <class T>
+struct V8ValueTraits {
+ // FIXME: This function requires the associated generated header to be
+ // included. Also, this function does not match with other V8ValueTraits
+ // classes. Remove this V8ValueTraits if possible.
+ static inline v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
- return index >= info.Length() ? v8::Local<v8::Value>() : info[index];
+ return toV8(WTF::getPtr(value), creationContext, isolate);
}
+};
- template<typename CallbackInfo, typename V>
- inline void v8SetReturnValue(const CallbackInfo& info, V v)
+template<>
+struct V8ValueTraits<String> {
+ static inline v8::Handle<v8::Value> toV8Value(const String& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().Set(v);
+ return v8String(isolate, value);
}
+};
- template<typename CallbackInfo>
- inline void v8SetReturnValueBool(const CallbackInfo& info, bool v)
+template<>
+struct V8ValueTraits<AtomicString> {
+ static inline v8::Handle<v8::Value> toV8Value(const AtomicString& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().Set(v);
+ return v8String(isolate, value);
}
+};
- template<typename CallbackInfo>
- inline void v8SetReturnValueInt(const CallbackInfo& info, int v)
+template<size_t n>
+struct V8ValueTraits<char[n]> {
+ static inline v8::Handle<v8::Value> toV8Value(char const (&value)[n], v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().Set(v);
+ return v8String(isolate, value);
}
+};
- template<typename CallbackInfo>
- inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v)
+template<>
+struct V8ValueTraits<const char*> {
+ static inline v8::Handle<v8::Value> toV8Value(const char* const& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().Set(v);
+ return v8String(isolate, value);
}
+};
- template<typename CallbackInfo>
- inline void v8SetReturnValueNull(const CallbackInfo& info)
+template<>
+struct V8ValueTraits<int> {
+ static inline v8::Handle<v8::Value> toV8Value(const int& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().SetNull();
+ return v8::Integer::New(isolate, value);
}
+};
- template<typename CallbackInfo>
- inline void v8SetReturnValueUndefined(const CallbackInfo& info)
+template<>
+struct V8ValueTraits<long> {
+ static inline v8::Handle<v8::Value> toV8Value(const long& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().SetUndefined();
+ return v8::Integer::New(isolate, value);
}
+};
- template<typename CallbackInfo>
- inline void v8SetReturnValueEmptyString(const CallbackInfo& info)
+template<>
+struct V8ValueTraits<unsigned> {
+ static inline v8::Handle<v8::Value> toV8Value(const unsigned& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- info.GetReturnValue().SetEmptyString();
+ return v8::Integer::NewFromUnsigned(isolate, value);
}
+};
- template <class CallbackInfo>
- inline void v8SetReturnValueString(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
+template<>
+struct V8ValueTraits<unsigned long> {
+ static inline v8::Handle<v8::Value> toV8Value(const unsigned long& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- if (string.isNull()) {
- v8SetReturnValueEmptyString(info);
- return;
- }
- V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
+ return v8::Integer::NewFromUnsigned(isolate, value);
}
+};
- template <class CallbackInfo>
- inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
+template<>
+struct V8ValueTraits<float> {
+ static inline v8::Handle<v8::Value> toV8Value(const float& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- if (string.isNull()) {
- v8SetReturnValueNull(info);
- return;
- }
- V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
+ return v8::Number::New(isolate, value);
}
+};
- template <class CallbackInfo>
- inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
+template<>
+struct V8ValueTraits<double> {
+ static inline v8::Handle<v8::Value> toV8Value(const double& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- if (string.isNull()) {
- v8SetReturnValueUndefined(info);
- return;
- }
- V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
+ return v8::Number::New(isolate, value);
}
+};
- // Convert v8::String to a WTF::String. If the V8 string is not already
- // an external string then it is transformed into an external string at this
- // point to avoid repeated conversions.
- inline String toCoreString(v8::Handle<v8::String> value)
+template<>
+struct V8ValueTraits<bool> {
+ static inline v8::Handle<v8::Value> toV8Value(const bool& value, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- return v8StringToWebCoreString<String>(value, Externalize);
+ return v8::Boolean::New(isolate, value);
}
+};
- inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value)
- {
- if (value.IsEmpty() || value->IsNull())
- return String();
- return toCoreString(value);
- }
+// V8NullType and V8UndefinedType are used only for the value conversion.
+class V8NullType { };
+class V8UndefinedType { };
- inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value)
+template<>
+struct V8ValueTraits<V8NullType> {
+ static inline v8::Handle<v8::Value> toV8Value(const V8NullType&, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
- return String();
- return toCoreString(value);
+ return v8::Null(isolate);
}
+};
- inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value)
+template<>
+struct V8ValueTraits<V8UndefinedType> {
+ static inline v8::Handle<v8::Value> toV8Value(const V8UndefinedType&, v8::Handle<v8::Object>, v8::Isolate* isolate)
{
- return v8StringToWebCoreString<AtomicString>(value, Externalize);
+ return v8::Undefined(isolate);
}
+};
- // This method will return a null String if the v8::Value does not contain a v8::String.
- // It will not call ToString() on the v8::Value. If you want ToString() to be called,
- // please use the V8TRYCATCH_FOR_V8STRINGRESOURCE_*() macros instead.
- inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value)
+template<>
+struct V8ValueTraits<ScriptValue> {
+ static inline v8::Handle<v8::Value> toV8Value(const ScriptValue& value, v8::Handle<v8::Object>, v8::Isolate*)
{
- if (value.IsEmpty() || !value->IsString())
- return String();
-
- return toCoreString(value.As<v8::String>());
+ return value.v8Value();
}
+};
- // Convert a string to a V8 string.
- // Return a V8 external string that shares the underlying buffer with the given
- // WebCore string. The reference counting mechanism is used to keep the
- // underlying buffer alive while the string is still live in the V8 engine.
- inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& string)
+template<>
+struct V8ValueTraits<v8::Handle<v8::Value> > {
+ static inline v8::Handle<v8::Value> toV8Value(const v8::Handle<v8::Value>& value, v8::Handle<v8::Object>, v8::Isolate*)
{
- if (string.isNull())
- return v8::String::Empty(isolate);
- return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(string.impl(), isolate);
+ return value;
}
+};
- inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* str)
+template<>
+struct V8ValueTraits<v8::Local<v8::Value> > {
+ static inline v8::Handle<v8::Value> toV8Value(const v8::Local<v8::Value>& value, v8::Handle<v8::Object>, v8::Isolate*)
{
- ASSERT(isolate);
- return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString, strlen(str));
+ return value;
}
+};
- inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* str, size_t length)
+template<typename T, size_t inlineCapacity>
+v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
+{
+ v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
+ int index = 0;
+ typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
+ typedef V8ValueTraits<T> TraitsType;
+ for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
+ result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*iter, creationContext, isolate));
+ return result;
+}
+
+template<typename T, size_t inlineCapacity>
+v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
+{
+ v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
+ int index = 0;
+ typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end();
+ typedef V8ValueTraits<T> TraitsType;
+ for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
+ result->Set(v8::Integer::New(isolate, index++), TraitsType::toV8Value(*iter, creationContext, isolate));
+ return result;
+}
+
+template<typename T, size_t inlineCapacity>
+v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
+{
+ v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
+ int index = 0;
+ typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
+ for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
+ result->Set(v8::Integer::New(isolate, index++), toV8NoInline(WTF::getPtr(*iter), creationContext, isolate));
+ return result;
+}
+
+template<typename T, size_t inlineCapacity>
+v8::Handle<v8::Value> v8ArrayNoInline(const HeapVector<T, inlineCapacity>& iterator, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
+{
+ v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
+ int index = 0;
+ typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end();
+ for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
+ result->Set(v8::Integer::New(isolate, index++), toV8NoInline(WTF::getPtr(*iter), creationContext, isolate));
+ return result;
+}
+
+// Conversion flags, used in toIntXX/toUIntXX.
+enum IntegerConversionConfiguration {
+ NormalConversion,
+ EnforceRange,
+ Clamp
+};
+
+// Convert a value to a 8-bit signed integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-byte
+int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toInt8(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 8-bit integer assuming the conversion cannot fail.
+int8_t toInt8(v8::Handle<v8::Value>);
+
+// Convert a value to a 8-bit unsigned integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-octet
+uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toUInt8(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 8-bit unsigned integer assuming the conversion cannot fail.
+uint8_t toUInt8(v8::Handle<v8::Value>);
+
+// Convert a value to a 16-bit signed integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-short
+int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toInt16(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 16-bit integer assuming the conversion cannot fail.
+int16_t toInt16(v8::Handle<v8::Value>);
+
+// Convert a value to a 16-bit unsigned integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-unsigned-short
+uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toUInt16(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 16-bit unsigned integer assuming the conversion cannot fail.
+uint16_t toUInt16(v8::Handle<v8::Value>);
+
+// Convert a value to a 32-bit signed integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-long
+int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toInt32(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 32-bit integer assuming the conversion cannot fail.
+int32_t toInt32(v8::Handle<v8::Value>);
+
+// Convert a value to a 32-bit unsigned integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-unsigned-long
+uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toUInt32(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 32-bit unsigned integer assuming the conversion cannot fail.
+uint32_t toUInt32(v8::Handle<v8::Value>);
+
+// Convert a value to a 64-bit signed integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-long-long
+int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toInt64(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 64-bit integer assuming the conversion cannot fail.
+int64_t toInt64(v8::Handle<v8::Value>);
+
+// Convert a value to a 64-bit unsigned integer. The conversion fails if the
+// value cannot be converted to a number or the range violated per WebIDL:
+// http://www.w3.org/TR/WebIDL/#es-unsigned-long-long
+uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
+inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+{
+ return toUInt64(value, NormalConversion, exceptionState);
+}
+
+// Convert a value to a 64-bit unsigned integer assuming the conversion cannot fail.
+uint64_t toUInt64(v8::Handle<v8::Value>);
+
+// Convert a value to a single precision float, which might fail.
+float toFloat(v8::Handle<v8::Value>, ExceptionState&);
+
+// Convert a value to a single precision float assuming the conversion cannot fail.
+inline float toFloat(v8::Local<v8::Value> value)
+{
+ return static_cast<float>(value->NumberValue());
+}
+
+// Converts a value to a String, throwing if any code unit is outside 0-255.
+String toByteString(v8::Handle<v8::Value>, ExceptionState&);
+
+// Converts a value to a String, replacing unmatched UTF-16 surrogates with replacement characters.
+String toScalarValueString(v8::Handle<v8::Value>, ExceptionState&);
+
+inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate)
+{
+ return value ? v8::True(isolate) : v8::False(isolate);
+}
+
+inline double toCoreDate(v8::Handle<v8::Value> object)
+{
+ if (object->IsDate())
+ return v8::Handle<v8::Date>::Cast(object)->ValueOf();
+ if (object->IsNumber())
+ return object->NumberValue();
+ return std::numeric_limits<double>::quiet_NaN();
+}
+
+inline v8::Handle<v8::Value> v8DateOrNaN(double value, v8::Isolate* isolate)
+{
+ ASSERT(isolate);
+ return v8::Date::New(isolate, std::isfinite(value) ? value : std::numeric_limits<double>::quiet_NaN());
+}
+
+// FIXME: Remove the special casing for NodeFilter and XPathNSResolver.
+PassRefPtrWillBeRawPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Handle<v8::Object>, ScriptState*);
+PassRefPtrWillBeRawPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Value>, v8::Isolate*);
+
+template<class T> struct NativeValueTraits;
+
+template<>
+struct NativeValueTraits<String> {
+ static inline String nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
+ {
+ TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, String());
+ return stringValue;
+ }
+};
+
+template<>
+struct NativeValueTraits<unsigned> {
+ static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
+ {
+ return toUInt32(value);
+ }
+};
+
+template<>
+struct NativeValueTraits<float> {
+ static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
{
- ASSERT(isolate);
- return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString, length);
+ return static_cast<float>(value->NumberValue());
}
+};
- inline v8::Handle<v8::Value> v8Undefined()
+template<>
+struct NativeValueTraits<double> {
+ static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
{
- return v8::Handle<v8::Value>();
+ return static_cast<double>(value->NumberValue());
}
+};
- template <class T>
- struct V8ValueTraits {
- static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate* isolate)
- {
- return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate);
- }
- };
-
- template<>
- struct V8ValueTraits<String> {
- static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Isolate* isolate)
- {
- return v8String(isolate, value);
- }
- };
-
- template<>
- struct V8ValueTraits<AtomicString> {
- static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value, v8::Isolate* isolate)
- {
- return v8String(isolate, value);
- }
- };
-
- template<>
- struct V8ValueTraits<unsigned> {
- static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8::Isolate* isolate)
- {
- return v8::Integer::NewFromUnsigned(isolate, value);
- }
- };
-
- template<>
- struct V8ValueTraits<unsigned long> {
- static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value, v8::Isolate* isolate)
- {
- return v8::Integer::NewFromUnsigned(isolate, value);
- }
- };
-
- template<>
- struct V8ValueTraits<float> {
- static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Isolate* isolate)
- {
- return v8::Number::New(isolate, value);
- }
- };
-
- template<>
- struct V8ValueTraits<double> {
- static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Isolate* isolate)
- {
- return v8::Number::New(isolate, value);
- }
- };
-
- template<typename T, size_t inlineCapacity>
- v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Isolate* isolate)
+template<>
+struct NativeValueTraits<v8::Handle<v8::Value> > {
+ static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
{
- v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
- int index = 0;
- typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
- typedef V8ValueTraits<T> TraitsType;
- for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
- result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value(*iter, isolate));
- return result;
+ return value;
}
+};
- // Conversion flags, used in toIntXX/toUIntXX.
- enum IntegerConversionConfiguration {
- NormalConversion,
- EnforceRange,
- Clamp
- };
+v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8::Isolate*);
- // Convert a value to a 8-bit signed integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-byte
- int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
- {
- return toInt8(value, NormalConversion, exceptionState);
+// Converts a JavaScript value to an array as per the Web IDL specification:
+// http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
+template <class T, class V8T>
+Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value, uint32_t length, v8::Isolate* isolate, bool* success = 0)
+{
+ Vector<RefPtr<T> > result;
+ result.reserveInitialCapacity(length);
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
+ for (uint32_t i = 0; i < length; ++i) {
+ v8::Handle<v8::Value> element = object->Get(i);
+ if (V8T::hasInstance(element, isolate)) {
+ v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast(element);
+ result.uncheckedAppend(V8T::toNative(elementObject));
+ } else {
+ if (success)
+ *success = false;
+ throwTypeError("Invalid Array element type", isolate);
+ return Vector<RefPtr<T> >();
+ }
}
-
- // Convert a value to a 8-bit integer assuming the conversion cannot fail.
- int8_t toInt8(v8::Handle<v8::Value>);
-
- // Convert a value to a 8-bit unsigned integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-octet
- uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
- {
- return toUInt8(value, NormalConversion, exceptionState);
+ return result;
+}
+
+template <class T, class V8T>
+Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate, bool* success = 0)
+{
+ if (success)
+ *success = true;
+
+ v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
+ uint32_t length = 0;
+ if (value->IsArray()) {
+ length = v8::Local<v8::Array>::Cast(v8Value)->Length();
+ } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
+ throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
+ return Vector<RefPtr<T> >();
+ }
+ return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, success);
+}
+
+template <class T, class V8T>
+Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const String& propertyName, v8::Isolate* isolate, bool* success = 0)
+{
+ if (success)
+ *success = true;
+
+ v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
+ uint32_t length = 0;
+ if (value->IsArray()) {
+ length = v8::Local<v8::Array>::Cast(v8Value)->Length();
+ } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
+ throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName), isolate);
+ return Vector<RefPtr<T> >();
+ }
+ return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, success);
+}
+
+template <class T, class V8T>
+WillBeHeapVector<RefPtrWillBeMember<T> > toRefPtrWillBeMemberNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate, bool* success = 0)
+{
+ if (success)
+ *success = true;
+
+ v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
+ uint32_t length = 0;
+ if (value->IsArray()) {
+ length = v8::Local<v8::Array>::Cast(v8Value)->Length();
+ } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
+ throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
+ return WillBeHeapVector<RefPtrWillBeMember<T> >();
+ }
+
+ WillBeHeapVector<RefPtrWillBeMember<T> > result;
+ result.reserveInitialCapacity(length);
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
+ for (uint32_t i = 0; i < length; ++i) {
+ v8::Handle<v8::Value> element = object->Get(i);
+ if (V8T::hasInstance(element, isolate)) {
+ v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast(element);
+ result.uncheckedAppend(V8T::toNative(elementObject));
+ } else {
+ if (success)
+ *success = false;
+ throwTypeError("Invalid Array element type", isolate);
+ return WillBeHeapVector<RefPtrWillBeMember<T> >();
+ }
}
-
- // Convert a value to a 8-bit unsigned integer assuming the conversion cannot fail.
- uint8_t toUInt8(v8::Handle<v8::Value>);
-
- // Convert a value to a 16-bit signed integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-short
- int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+ return result;
+}
+
+// Converts a JavaScript value to an array as per the Web IDL specification:
+// http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
+template <class T>
+Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate)
+{
+ v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
+ uint32_t length = 0;
+ if (value->IsArray()) {
+ length = v8::Local<v8::Array>::Cast(v8Value)->Length();
+ } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
+ throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
+ return Vector<T>();
+ }
+
+ Vector<T> result;
+ result.reserveInitialCapacity(length);
+ typedef NativeValueTraits<T> TraitsType;
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
+ for (uint32_t i = 0; i < length; ++i)
+ result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate));
+ return result;
+}
+
+template <class T>
+Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex)
+{
+ ASSERT(startIndex <= info.Length());
+ Vector<T> result;
+ typedef NativeValueTraits<T> TraitsType;
+ int length = info.Length();
+ result.reserveInitialCapacity(length);
+ for (int i = startIndex; i < length; ++i)
+ result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate()));
+ return result;
+}
+
+// Validates that the passed object is a sequence type per WebIDL spec
+// http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
+inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t& length, v8::Isolate* isolate)
+{
+ // Attempt converting to a sequence if the value is not already an array but is
+ // any kind of object except for a native Date object or a native RegExp object.
+ ASSERT(!value->IsArray());
+ // FIXME: Do we really need to special case Date and RegExp object?
+ // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
+ if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
+ // The caller is responsible for reporting a TypeError.
+ return v8Undefined();
+ }
+
+ v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
+ v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length");
+
+ // FIXME: The specification states that the length property should be used as fallback, if value
+ // is not a platform object that supports indexed properties. If it supports indexed properties,
+ // length should actually be one greater than value’s maximum indexed property index.
+ TONATIVE_EXCEPTION(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol));
+
+ if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
+ // The caller is responsible for reporting a TypeError.
+ return v8Undefined();
+ }
+
+ TONATIVE_EXCEPTION(uint32_t, sequenceLength, lengthValue->Int32Value());
+ length = sequenceLength;
+ return v8Value;
+}
+
+v8::Isolate* toIsolate(ExecutionContext*);
+v8::Isolate* toIsolate(LocalFrame*);
+
+LocalDOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
+LocalDOMWindow* toDOMWindow(v8::Handle<v8::Context>);
+LocalDOMWindow* enteredDOMWindow(v8::Isolate*);
+LocalDOMWindow* currentDOMWindow(v8::Isolate*);
+LocalDOMWindow* callingDOMWindow(v8::Isolate*);
+ExecutionContext* toExecutionContext(v8::Handle<v8::Context>);
+ExecutionContext* currentExecutionContext(v8::Isolate*);
+ExecutionContext* callingExecutionContext(v8::Isolate*);
+
+// Returns a V8 context associated with a ExecutionContext and a DOMWrapperWorld.
+// This method returns an empty context if there is no frame or the frame is already detached.
+v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld&);
+// Returns a V8 context associated with a LocalFrame and a DOMWrapperWorld.
+// This method returns an empty context if the frame is already detached.
+v8::Local<v8::Context> toV8Context(LocalFrame*, DOMWrapperWorld&);
+
+// Returns the frame object of the window object associated with
+// a context, if the window is currently being displayed in the LocalFrame.
+LocalFrame* toFrameIfNotDetached(v8::Handle<v8::Context>);
+
+// If the current context causes out of memory, JavaScript setting
+// is disabled and it returns true.
+bool handleOutOfMemory();
+v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*);
+void crashIfV8IsDead();
+
+inline bool isUndefinedOrNull(v8::Handle<v8::Value> value)
+{
+ return value->IsNull() || value->IsUndefined();
+}
+v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>);
+
+// Attaches |environment| to |function| and returns it.
+inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::Handle<v8::Value> environment, v8::Isolate* isolate)
+{
+ return v8::Function::New(isolate, function, environment);
+}
+
+// FIXME: This will be soon embedded in the generated code.
+template<class Collection> static void indexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
+{
+ Collection* collection = reinterpret_cast<Collection*>(info.Holder()->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex));
+ int length = collection->length();
+ v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length);
+ for (int i = 0; i < length; ++i) {
+ // FIXME: Do we need to check that the item function returns a non-null value for this index?
+ v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i);
+ properties->Set(integer, integer);
+ }
+ v8SetReturnValue(info, properties);
+}
+
+// These methods store hidden values into an array that is stored in the internal field of a DOM wrapper.
+void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex, v8::Isolate*);
+void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex, v8::Isolate*);
+void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldValue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*);
+
+PassRefPtr<JSONValue> v8ToJSONValue(v8::Isolate*, v8::Handle<v8::Value>, int);
+
+// Converts a DOM object to a v8 value.
+// This is a no-inline version of toV8(). If you want to call toV8()
+// without creating #include cycles, you can use this function instead.
+// Each specialized implementation will be generated.
+template<typename T>
+v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationContext, v8::Isolate*);
+
+// ToV8Value<U, Context> is a class that converts a C++ object to a
+// v8 value. U has to be a class having a static method getCreationContext
+// which returns an object created from a target context.
+template<typename U, typename Context>
+class ToV8Value {
+public:
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const T& value, Context context, v8::Isolate* isolate)
{
- return toInt16(value, NormalConversion, exceptionState);
+ // Default implementaion: for types that don't need the context.
+ return V8ValueTraits<T>::toV8Value(value, context, isolate);
}
- // Convert a value to a 16-bit integer assuming the conversion cannot fail.
- int16_t toInt16(v8::Handle<v8::Value>);
-
- // Convert a value to a 16-bit unsigned integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-unsigned-short
- uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+ // Pointer specializations.
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(T* const& value, Context context, v8::Isolate* isolate)
{
- return toUInt16(value, NormalConversion, exceptionState);
+ return toV8NoInline(value, context, isolate);
}
-
- // Convert a value to a 16-bit unsigned integer assuming the conversion cannot fail.
- uint16_t toUInt16(v8::Handle<v8::Value>);
-
- // Convert a value to a 32-bit signed integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-long
- int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const RefPtr<T>& value, Context context, v8::Isolate* isolate)
{
- return toInt32(value, NormalConversion, exceptionState);
+ return toV8Value(value.get(), context, isolate);
}
-
- // Convert a value to a 32-bit integer assuming the conversion cannot fail.
- int32_t toInt32(v8::Handle<v8::Value>);
-
- // Convert a value to a 32-bit unsigned integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-unsigned-long
- uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const PassRefPtr<T>& value, Context context, v8::Isolate* isolate)
{
- return toUInt32(value, NormalConversion, exceptionState);
+ return toV8Value(value.get(), context, isolate);
}
-
- // Convert a value to a 32-bit unsigned integer assuming the conversion cannot fail.
- uint32_t toUInt32(v8::Handle<v8::Value>);
-
- // Convert a value to a 64-bit signed integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-long-long
- int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const OwnPtr<T>& value, Context context, v8::Isolate* isolate)
{
- return toInt64(value, NormalConversion, exceptionState);
+ return toV8Value(value.get(), context, isolate);
}
-
- // Convert a value to a 64-bit integer assuming the conversion cannot fail.
- int64_t toInt64(v8::Handle<v8::Value>);
-
- // Convert a value to a 64-bit unsigned integer. The conversion fails if the
- // value cannot be converted to a number or the range violated per WebIDL:
- // http://www.w3.org/TR/WebIDL/#es-unsigned-long-long
- uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
- inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const PassOwnPtr<T>& value, Context context, v8::Isolate* isolate)
{
- return toUInt64(value, NormalConversion, exceptionState);
+ return toV8Value(value.get(), context, isolate);
}
-
- // Convert a value to a 64-bit unsigned integer assuming the conversion cannot fail.
- uint64_t toUInt64(v8::Handle<v8::Value>);
-
- // Convert a value to a single precision float, which might fail.
- float toFloat(v8::Handle<v8::Value>, ExceptionState&);
-
- // Convert a value to a single precision float assuming the conversion cannot fail.
- inline float toFloat(v8::Local<v8::Value> value)
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const RawPtr<T>& value, Context context, v8::Isolate* isolate)
{
- return static_cast<float>(value->NumberValue());
- }
-
- WrapperWorldType worldType(v8::Isolate*);
- WrapperWorldType worldTypeInMainThread(v8::Isolate*);
-
- DOMWrapperWorld* isolatedWorldForIsolate(v8::Isolate*);
-
- template<class T> struct NativeValueTraits;
-
- template<>
- struct NativeValueTraits<String> {
- static inline String nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
- {
- V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, value, String());
- return stringValue;
- }
- };
-
- template<>
- struct NativeValueTraits<unsigned> {
- static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
- {
- return toUInt32(value);
- }
- };
-
- template<>
- struct NativeValueTraits<float> {
- static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
- {
- return static_cast<float>(value->NumberValue());
- }
- };
-
- template<>
- struct NativeValueTraits<double> {
- static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
- {
- return static_cast<double>(value->NumberValue());
- }
- };
-
- template<>
- struct NativeValueTraits<v8::Handle<v8::Value> > {
- static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
- {
- return value;
- }
- };
-
- // Converts a JavaScript value to an array as per the Web IDL specification:
- // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
- template <class T, class V8T>
- Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value, uint32_t length, v8::Isolate* isolate, bool* success = 0)
- {
- Vector<RefPtr<T> > result;
- result.reserveInitialCapacity(length);
- v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
- for (uint32_t i = 0; i < length; ++i) {
- v8::Handle<v8::Value> element = object->Get(i);
-
- if (V8T::hasInstance(element, isolate)) {
- v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast(element);
- result.uncheckedAppend(V8T::toNative(elementObject));
- } else {
- if (success)
- *success = false;
- throwTypeError("Invalid Array element type", isolate);
- return Vector<RefPtr<T> >();
- }
- }
- return result;
+ return toV8Value(value.get(), context, isolate);
}
- template <class T, class V8T>
- Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate, bool* success = 0)
+ // const char* should use V8ValueTraits.
+ static v8::Handle<v8::Value> toV8Value(const char* const& value, Context context, v8::Isolate* isolate)
{
- if (success)
- *success = true;
-
- v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
- uint32_t length = 0;
- if (value->IsArray()) {
- length = v8::Local<v8::Array>::Cast(v8Value)->Length();
- } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
- throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
- return Vector<RefPtr<T> >();
- }
-
- return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, success);
+ return V8ValueTraits<const char*>::toV8Value(value, context, isolate);
}
- template <class T, class V8T>
- Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const String& propertyName, v8::Isolate* isolate, bool* success = 0)
+ template<typename T, size_t inlineCapacity>
+ static v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value, Context context, v8::Isolate* isolate)
{
- if (success)
- *success = true;
-
- v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
- uint32_t length = 0;
- if (value->IsArray()) {
- length = v8::Local<v8::Array>::Cast(v8Value)->Length();
- } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
- throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName), isolate);
- return Vector<RefPtr<T> >();
- }
-
- return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, success);
+ return v8ArrayNoInline(value, context, isolate);
}
- // Converts a JavaScript value to an array as per the Web IDL specification:
- // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
- template <class T>
- Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate)
+ template<typename T, size_t inlineCapacity>
+ static v8::Handle<v8::Value> toV8Value(const HeapVector<T, inlineCapacity>& value, Context context, v8::Isolate* isolate)
{
- v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
- uint32_t length = 0;
- if (value->IsArray()) {
- length = v8::Local<v8::Array>::Cast(v8Value)->Length();
- } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
- throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
- return Vector<T>();
- }
-
- Vector<T> result;
- result.reserveInitialCapacity(length);
- typedef NativeValueTraits<T> TraitsType;
- v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
- for (uint32_t i = 0; i < length; ++i)
- result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate));
- return result;
- }
-
- template <class T>
- Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex)
- {
- ASSERT(startIndex <= info.Length());
- Vector<T> result;
- typedef NativeValueTraits<T> TraitsType;
- int length = info.Length();
- result.reserveInitialCapacity(length);
- for (int i = startIndex; i < length; ++i)
- result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate()));
- return result;
- }
-
- // Validates that the passed object is a sequence type per WebIDL spec
- // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
- inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t& length, v8::Isolate* isolate)
- {
- // Attempt converting to a sequence if the value is not already an array but is
- // any kind of object except for a native Date object or a native RegExp object.
- ASSERT(!value->IsArray());
- // FIXME: Do we really need to special case Date and RegExp object?
- // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
- if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
- // The caller is responsible for reporting a TypeError.
- return v8Undefined();
- }
-
- v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
- v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
- v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length");
-
- // FIXME: The specification states that the length property should be used as fallback, if value
- // is not a platform object that supports indexed properties. If it supports indexed properties,
- // length should actually be one greater than value’s maximum indexed property index.
- V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol));
-
- if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
- // The caller is responsible for reporting a TypeError.
- return v8Undefined();
- }
-
- V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value());
- length = sequenceLength;
-
- return v8Value;
+ return v8ArrayNoInline(value, context, isolate);
}
- PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*);
-
- DOMWindow* toNativeDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
-
- inline bool isUndefinedOrNull(v8::Handle<v8::Value> value)
+ template<typename T, size_t inlineCapacity>
+ static v8::Handle<v8::Value> toV8Value(const PersistentHeapVector<T, inlineCapacity>& value, Context context, v8::Isolate* isolate)
{
- return value->IsNull() || value->IsUndefined();
+ return v8ArrayNoInline(static_cast<HeapVector<T, inlineCapacity> >(value), context, isolate);
}
+};
- // Returns true if the provided object is to be considered a 'host object', as used in the
- // HTML5 structured clone algorithm.
- inline bool isHostObject(v8::Handle<v8::Object> object)
- {
- // If the object has any internal fields, then we won't be able to serialize or deserialize
- // them; conveniently, this is also a quick way to detect DOM wrapper objects, because
- // the mechanism for these relies on data stored in these fields. We should
- // catch external array data as a special case.
- return object->InternalFieldCount() || object->HasIndexedPropertiesInExternalArrayData();
- }
+// Result values for platform object 'deleter' methods,
+// http://www.w3.org/TR/WebIDL/#delete
+enum DeleteResult {
+ DeleteSuccess,
+ DeleteReject,
+ DeleteUnknownProperty
+};
- inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate)
- {
- return value ? v8::True(isolate) : v8::False(isolate);
- }
+class V8IsolateInterruptor : public ThreadState::Interruptor {
+public:
+ explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate) { }
- // Since v8Boolean(value, isolate) crashes if we pass a null isolate,
- // we need to use v8BooleanWithCheck(value, isolate) if an isolate can be null.
- //
- // FIXME: Remove all null isolates from V8 bindings, and remove v8BooleanWithCheck(value, isolate).
- inline v8::Handle<v8::Boolean> v8BooleanWithCheck(bool value, v8::Isolate* isolate)
+ static void onInterruptCallback(v8::Isolate* isolate, void* data)
{
- return isolate ? v8Boolean(value, isolate) : v8Boolean(value, v8::Isolate::GetCurrent());
+ reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted();
}
- inline double toWebCoreDate(v8::Handle<v8::Value> object)
+ virtual void requestInterrupt() OVERRIDE
{
- if (object->IsDate())
- return v8::Handle<v8::Date>::Cast(object)->ValueOf();
- if (object->IsNumber())
- return object->NumberValue();
- return std::numeric_limits<double>::quiet_NaN();
+ m_isolate->RequestInterrupt(&onInterruptCallback, this);
}
- inline v8::Handle<v8::Value> v8DateOrNull(double value, v8::Isolate* isolate)
+ virtual void clearInterrupt() OVERRIDE
{
- ASSERT(isolate);
- return std::isfinite(value) ? v8::Date::New(isolate, value) : v8::Handle<v8::Value>::Cast(v8::Null(isolate));
+ m_isolate->ClearInterrupt();
}
- PassRefPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Value>, v8::Isolate*);
+private:
+ v8::Isolate* m_isolate;
+};
- v8::Handle<v8::Object> toInnerGlobalObject(v8::Handle<v8::Context>);
- DOMWindow* toDOMWindow(v8::Handle<v8::Context>);
- ExecutionContext* toExecutionContext(v8::Handle<v8::Context>);
+class V8TestingScope {
+public:
+ explicit V8TestingScope(v8::Isolate*);
+ ScriptState* scriptState() const;
+ v8::Isolate* isolate() const;
+ ~V8TestingScope();
- DOMWindow* activeDOMWindow();
- ExecutionContext* activeExecutionContext();
- DOMWindow* firstDOMWindow();
- Document* currentDocument();
+private:
+ v8::HandleScope m_handleScope;
+ v8::Context::Scope m_contextScope;
+ RefPtr<ScriptState> m_scriptState;
+};
- // Returns the context associated with a ExecutionContext.
- v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld*);
+void GetDevToolsFunctionInfo(v8::Handle<v8::Function>, v8::Isolate*, int& scriptId, String& resourceName, int& lineNumber);
+PassRefPtr<TraceEvent::ConvertableToTraceFormat> devToolsTraceEventData(ExecutionContext*, v8::Handle<v8::Function>, v8::Isolate*);
- // Returns the frame object of the window object associated with
- // a context, if the window is currently being displayed in the Frame.
- Frame* toFrameIfNotDetached(v8::Handle<v8::Context>);
-
- inline DOMWrapperWorld* isolatedWorldForEnteredContext(v8::Isolate* isolate)
+class V8RethrowTryCatchScope FINAL {
+public:
+ explicit V8RethrowTryCatchScope(v8::TryCatch& block) : m_block(block) { }
+ ~V8RethrowTryCatchScope()
{
- v8::Handle<v8::Context> context = isolate->GetEnteredContext();
- if (context.IsEmpty())
- return 0;
- return DOMWrapperWorld::isolatedWorld(context);
- }
-
- // FIXME: This will be soon embedded in the generated code.
- template<class Collection> static void indexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
- {
- Collection* collection = reinterpret_cast<Collection*>(info.Holder()->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex));
- int length = collection->length();
- v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length);
- for (int i = 0; i < length; ++i) {
- // FIXME: Do we need to check that the item function returns a non-null value for this index?
- v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i);
- properties->Set(integer, integer);
- }
- v8SetReturnValue(info, properties);
+ // ReThrow() is a no-op if no exception has been caught, so always call.
+ m_block.ReThrow();
}
- // If the current context causes out of memory, JavaScript setting
- // is disabled and it returns true.
- bool handleOutOfMemory();
- v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*);
+private:
+ v8::TryCatch& m_block;
+};
- void crashIfV8IsDead();
-
- template <class T>
- v8::Handle<T> unsafeHandleFromRawValue(const T* value)
- {
- const v8::Handle<T>* handle = reinterpret_cast<const v8::Handle<T>*>(&value);
- return *handle;
- }
-
- // Attaches |environment| to |function| and returns it.
- inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::Handle<v8::Value> environment, v8::Isolate* isolate)
+class V8ResetTryCatchScope FINAL {
+public:
+ explicit V8ResetTryCatchScope(v8::TryCatch& block) : m_block(block) { }
+ ~V8ResetTryCatchScope()
{
- return v8::Function::New(isolate, function, environment);
+ m_block.Reset();
}
- v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*);
- v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>);
- bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*, v8::Handle<v8::Value>);
- bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>, v8::Handle<v8::Value>);
- bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*);
- bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>);
- v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrappable*, const char*);
- v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrappable*, v8::Handle<v8::String>);
-
- v8::Isolate* mainThreadIsolate();
- v8::Isolate* toIsolate(ExecutionContext*);
- v8::Isolate* toIsolate(Frame*);
-
- // Can only be called by blink::initialize
- void setMainThreadIsolate(v8::Isolate*);
-
- // Converts a DOM object to a v8 value.
- // This is a no-inline version of toV8(). If you want to call toV8()
- // without creating #include cycles, you can use this function instead.
- // Each specialized implementation will be generated.
- template<typename T>
- v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationContext, v8::Isolate*);
-
- // Result values for platform object 'deleter' methods,
- // http://www.w3.org/TR/WebIDL/#delete
- enum DeleteResult {
- DeleteSuccess,
- DeleteReject,
- DeleteUnknownProperty
- };
-
-#if ENABLE(OILPAN)
- class V8IsolateInterruptor : public ThreadState::Interruptor {
- public:
- explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate) { }
-
- static void onInterruptCallback(v8::Isolate* isolate, void* data)
- {
- reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted();
- }
-
- virtual void requestInterrupt() OVERRIDE
- {
- m_isolate->RequestInterrupt(&onInterruptCallback, this);
- }
-
- virtual void clearInterrupt() OVERRIDE
- {
- m_isolate->ClearInterrupt();
- }
-
- private:
- v8::Isolate* m_isolate;
- };
-#endif
+private:
+ v8::TryCatch& m_block;
+};
} // namespace WebCore