Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Unified Diff: Source/bindings/v8/V8Binding.h

Issue 238723009: Implement V8ValueTraits<T>::toV8Value conversion functions (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/bindings/v8/V8Binding.h
diff --git a/Source/bindings/v8/V8Binding.h b/Source/bindings/v8/V8Binding.h
index 9df3459b72321a5246616c4f480e916c9c124d9b..4e8e47588b63e1f358501e11c1708c6054880be4 100644
--- a/Source/bindings/v8/V8Binding.h
+++ b/Source/bindings/v8/V8Binding.h
@@ -34,6 +34,7 @@
#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"
@@ -216,16 +217,66 @@ inline v8::Handle<v8::Value> v8Undefined()
return v8::Handle<v8::Value>();
}
+// 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*);
+
+// Every ValueTraits has the following methods and properties.
+// - arrayV8Value: A conversion function that is used to construct an array.
+// This function may use toV8 and including the associated
+// generated header is needed to call this function.
+//
+// - toV8Value: A general conversion function. This function uses toV8NoInline
+// if necessary, instead of toV8. Hence this function can be
+// called without including a generated header.
+// Some V8ValueTraitses have toV8Value whose type is
+// Handle<Value> toV8Value(const T&, Handle<Object>, Isolate*);
+// and the other have toV8Value whose type is
+// Handle<Value> toV8Value(const T&, Isolate*);
+// .
+// - toV8ValueNeedsCreationContext: set true if the class provides toV8 which
+// requires a creationContext as a parameter.
+// set false otherwise.
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);
}
+ static inline v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
+ {
+ return toV8NoInline(WTF::getPtr(value), creationContext, isolate);
+ }
+ static const bool toV8ValueNeedsCreationContext = true;
+};
+
+template <typename T>
+struct V8ValueTraits<T*> {
+ static inline v8::Handle<v8::Value> arrayV8Value(T* const& value, v8::Isolate* isolate)
+ {
+ return toV8(value, v8::Handle<v8::Object>(), isolate);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(T* const& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
+ {
+ return toV8NoInline(value, creationContext, isolate);
+ }
+ static const bool toV8ValueNeedsCreationContext = true;
+};
+
+template <typename T>
+struct V8ValueTraitsToV8ValueIsIdentialToArrayJsValue {
+ static inline v8::Handle<v8::Value> toV8Value(const T& value, v8::Isolate* isolate)
+ {
+ return V8ValueTraits<T>::arrayV8Value(value, isolate);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<>
-struct V8ValueTraits<String> {
+struct V8ValueTraits<String> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<String> {
static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Isolate* isolate)
{
return v8String(isolate, value);
@@ -233,7 +284,7 @@ struct V8ValueTraits<String> {
};
template<>
-struct V8ValueTraits<AtomicString> {
+struct V8ValueTraits<AtomicString> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<AtomicString> {
static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value, v8::Isolate* isolate)
{
return v8String(isolate, value);
@@ -241,7 +292,31 @@ struct V8ValueTraits<AtomicString> {
};
template<>
-struct V8ValueTraits<unsigned> {
+struct V8ValueTraits<const char*> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<const char*> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const char* const& value, v8::Isolate* isolate)
+ {
+ return v8String(isolate, value);
+ }
+};
+
+template<>
+struct V8ValueTraits<int> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<int> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const int& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::New(isolate, value);
+ }
+};
+
+template<>
+struct V8ValueTraits<long> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<long> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const long& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::New(isolate, value);
+ }
+};
+
+template<>
+struct V8ValueTraits<unsigned> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<unsigned> {
static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8::Isolate* isolate)
{
return v8::Integer::NewFromUnsigned(isolate, value);
@@ -249,7 +324,7 @@ struct V8ValueTraits<unsigned> {
};
template<>
-struct V8ValueTraits<unsigned long> {
+struct V8ValueTraits<unsigned long> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<unsigned long> {
static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value, v8::Isolate* isolate)
{
return v8::Integer::NewFromUnsigned(isolate, value);
@@ -257,7 +332,7 @@ struct V8ValueTraits<unsigned long> {
};
template<>
-struct V8ValueTraits<float> {
+struct V8ValueTraits<float> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<float> {
static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Isolate* isolate)
{
return v8::Number::New(isolate, value);
@@ -265,13 +340,93 @@ struct V8ValueTraits<float> {
};
template<>
-struct V8ValueTraits<double> {
+struct V8ValueTraits<double> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<double> {
static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Isolate* isolate)
{
return v8::Number::New(isolate, value);
}
};
+template<>
+struct V8ValueTraits<bool> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<bool> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const bool& value, v8::Isolate* isolate)
+ {
+ return v8::Boolean::New(isolate, value);
+ }
+};
+
+// V8NullType and V8UndefinedType are used only for the value conversion.
+class V8NullType { };
+class V8UndefinedType { };
+
+template<>
+struct V8ValueTraits<V8NullType> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<V8NullType> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const V8NullType&, v8::Isolate* isolate)
+ {
+ return v8::Null(isolate);
+ }
+};
+
+template<>
+struct V8ValueTraits<V8UndefinedType> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<V8UndefinedType> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const V8UndefinedType&, v8::Isolate* isolate)
+ {
+ return v8::Undefined(isolate);
+ }
+};
+
+template<>
+struct V8ValueTraits<ScriptValue> : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<ScriptValue> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const ScriptValue& value, v8::Isolate*)
+ {
+ return value.v8Value();
+ }
+};
+
+template<>
+struct V8ValueTraits<v8::Handle<v8::Value> > : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<v8::Handle<v8::Value> > {
+ static inline v8::Handle<v8::Value> arrayV8Value(const v8::Handle<v8::Value>& value, v8::Isolate*)
+ {
+ return value;
+ }
+};
+
+template<>
+struct V8ValueTraits<v8::Local<v8::Value> > : public V8ValueTraitsToV8ValueIsIdentialToArrayJsValue<v8::Local<v8::Value> > {
+ static inline v8::Handle<v8::Value> arrayV8Value(const v8::Local<v8::Value>& value, v8::Isolate*)
+ {
+ return value;
+ }
+};
+
+template<typename Context, typename Helper>
+struct ToV8ValueHelper {
+ template<typename T, bool toV8ValueNeedsCreationContext>
+ struct Switch;
+
+ template<typename T>
+ struct Switch<T, true> {
+ static v8::Handle<v8::Value> toV8Value(const T& value, const Context& context, v8::Isolate* isolate)
+ {
+ return Helper::toV8Value(value, context, isolate);
+ }
+ };
+
+ template<typename T>
+ struct Switch<T, false> {
+ static v8::Handle<v8::Value> toV8Value(const T& value, const Context& context, v8::Isolate* isolate)
+ {
+ return Helper::toV8Value(value, isolate);
+ }
+ };
+
+ template<typename T>
+ static v8::Handle<v8::Value> toV8Value(const T& value, const Context& context, v8::Isolate* isolate)
+ {
+ return Switch<T, V8ValueTraits<T>::toV8ValueNeedsCreationContext>::toV8Value(value, context, isolate);
+ }
+};
+
template<typename T, size_t inlineCapacity>
v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Isolate* isolate)
{
@@ -307,6 +462,19 @@ v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator,
return result;
}
+template<typename T, size_t inlinCapacity>
haraken 2014/04/18 03:59:58 inlineCapacity
+struct V8ValueTraits<Vector<T, inlinCapacity> > {
+ static inline v8::Handle<v8::Value> arrayV8Value(const Vector<T, inlinCapacity>& value, v8::Isolate* isolate)
+ {
+ return v8Array(value, isolate);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const Vector<T, inlinCapacity>& value, v8::Isolate* isolate)
+ {
+ return v8ArrayNoInline(value, isolate);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
// Conversion flags, used in toIntXX/toUIntXX.
enum IntegerConversionConfiguration {
NormalConversion,
@@ -710,12 +878,6 @@ void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cac
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*);
-// 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*);
template<typename T>
v8::Handle<v8::Value> toV8NoInline(T* impl, ExecutionContext* context)
{

Powered by Google App Engine
This is Rietveld 408576698