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

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
« no previous file with comments | « Source/bindings/v8/ScriptPromiseResolverWithContext.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/bindings/v8/V8Binding.h
diff --git a/Source/bindings/v8/V8Binding.h b/Source/bindings/v8/V8Binding.h
index 9df3459b72321a5246616c4f480e916c9c124d9b..a8a185e67d37e67e1ab232ec8f29f943e7356a66 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,12 +217,83 @@ inline v8::Handle<v8::Value> v8Undefined()
return v8::Handle<v8::Value>();
}
+v8::Isolate* toIsolate(ExecutionContext*);
+v8::Isolate* toIsolate(LocalFrame*);
+
+DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
+DOMWindow* toDOMWindow(v8::Handle<v8::Context>);
+DOMWindow* enteredDOMWindow(v8::Isolate*);
+DOMWindow* currentDOMWindow(v8::Isolate*);
+DOMWindow* 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(v8::Isolate*, 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>);
+
+// 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)
+{
+ v8::Isolate* isolate = toIsolate(context);
+ v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current(isolate));
+ return toV8NoInline(impl, v8Context->Global(), 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.
+//
+// - toV8: A general conversion function. This function uses toV8NoInline,
+// This function doesn't use toV8, so this 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.
haraken 2014/04/17 12:42:27 I'm not sure if we need the toV8ValueNeedsCreation
yhirano 2014/04/18 01:09:42 Do you mean that V8ValueTraits are for the types t
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<>
@@ -230,6 +302,11 @@ struct V8ValueTraits<String> {
{
return v8String(isolate, value);
}
+ static inline v8::Handle<v8::Value> toV8Value(const String& value, v8::Isolate* isolate)
+ {
+ return v8String(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<>
@@ -238,6 +315,50 @@ struct V8ValueTraits<AtomicString> {
{
return v8String(isolate, value);
}
+ static inline v8::Handle<v8::Value> toV8Value(const AtomicString& value, v8::Isolate* isolate)
+ {
+ return v8String(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<const char*> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const char* const& value, v8::Isolate* isolate)
+ {
+ return v8String(isolate, value);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const char* const& value, v8::Isolate* isolate)
+ {
+ return v8String(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<int> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const int& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::New(isolate, value);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const int& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::New(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<long> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const long& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::New(isolate, value);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const long& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::New(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<>
@@ -246,6 +367,11 @@ struct V8ValueTraits<unsigned> {
{
return v8::Integer::NewFromUnsigned(isolate, value);
}
+ static inline v8::Handle<v8::Value> toV8Value(const unsigned& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::NewFromUnsigned(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<>
@@ -254,6 +380,11 @@ struct V8ValueTraits<unsigned long> {
{
return v8::Integer::NewFromUnsigned(isolate, value);
}
+ static inline v8::Handle<v8::Value> toV8Value(const unsigned long& value, v8::Isolate* isolate)
+ {
+ return v8::Integer::NewFromUnsigned(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<>
@@ -262,6 +393,11 @@ struct V8ValueTraits<float> {
{
return v8::Number::New(isolate, value);
}
+ static inline v8::Handle<v8::Value> toV8Value(const float& value, v8::Isolate* isolate)
+ {
+ return v8::Number::New(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<>
@@ -270,6 +406,93 @@ struct V8ValueTraits<double> {
{
return v8::Number::New(isolate, value);
}
+ static inline v8::Handle<v8::Value> toV8Value(const double& value, v8::Isolate* isolate)
+ {
+ return v8::Number::New(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<bool> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const bool& value, v8::Isolate* isolate)
+ {
+ return v8::Boolean::New(isolate, value);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const bool& value, v8::Isolate* isolate)
+ {
+ return v8::Boolean::New(isolate, value);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+// V8NullType and V8UndefinedType are used only for the value conversion.
+class V8NullType { };
+class V8UndefinedType { };
+
+template<>
+struct V8ValueTraits<V8NullType> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const V8NullType&, v8::Isolate* isolate)
+ {
+ return v8::Null(isolate);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const V8NullType&, v8::Isolate* isolate)
+ {
+ return v8::Null(isolate);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<V8UndefinedType> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const V8UndefinedType&, v8::Isolate* isolate)
+ {
+ return v8::Undefined(isolate);
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const V8UndefinedType&, v8::Isolate* isolate)
+ {
+ return v8::Undefined(isolate);
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<ScriptValue> {
+ static inline v8::Handle<v8::Value> arrayV8Value(const ScriptValue& value, v8::Isolate*)
+ {
+ return value.v8Value();
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const ScriptValue& value, v8::Isolate*)
+ {
+ return value.v8Value();
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<v8::Handle<v8::Value> > {
+ static inline v8::Handle<v8::Value> arrayV8Value(const v8::Handle<v8::Value>& value, v8::Isolate*)
+ {
+ return value;
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const v8::Handle<v8::Value>& value, v8::Isolate*)
+ {
+ return value;
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
+};
+
+template<>
+struct V8ValueTraits<v8::Local<v8::Value> > {
+ static inline v8::Handle<v8::Value> arrayV8Value(const v8::Local<v8::Value>& value, v8::Isolate*)
+ {
+ return value;
+ }
+ static inline v8::Handle<v8::Value> toV8Value(const v8::Local<v8::Value>& value, v8::Isolate*)
+ {
+ return value;
+ }
+ static const bool toV8ValueNeedsCreationContext = false;
};
template<typename T, size_t inlineCapacity>
@@ -307,6 +530,19 @@ v8::Handle<v8::Value> v8ArrayNoInline(const Vector<T, inlineCapacity>& iterator,
return result;
}
+template<typename T, size_t inlinCapacity>
+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,
@@ -650,29 +886,6 @@ inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t&
return v8Value;
}
-v8::Isolate* toIsolate(ExecutionContext*);
-v8::Isolate* toIsolate(LocalFrame*);
-
-DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
-DOMWindow* toDOMWindow(v8::Handle<v8::Context>);
-DOMWindow* enteredDOMWindow(v8::Isolate*);
-DOMWindow* currentDOMWindow(v8::Isolate*);
-DOMWindow* 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(v8::Isolate*, 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();
@@ -710,20 +923,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)
-{
- v8::Isolate* isolate = toIsolate(context);
- v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current(isolate));
- return toV8NoInline(impl, v8Context->Global(), isolate);
-}
-
// Result values for platform object 'deleter' methods,
// http://www.w3.org/TR/WebIDL/#delete
enum DeleteResult {
« no previous file with comments | « Source/bindings/v8/ScriptPromiseResolverWithContext.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698