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

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

Issue 22470008: Implement ScriptPromise and ScriptFunction (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Protect the ScriptFunction with a strong ref Created 7 years, 4 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/ScriptFunction.h
diff --git a/Source/core/testing/LayerRect.h b/Source/bindings/v8/ScriptFunction.h
similarity index 51%
copy from Source/core/testing/LayerRect.h
copy to Source/bindings/v8/ScriptFunction.h
index f945681417ff788255e29f66eca4932b87855dd2..63f8153048628272ef64a55fc096a9902efe1bbe 100644
--- a/Source/core/testing/LayerRect.h
+++ b/Source/bindings/v8/ScriptFunction.h
@@ -28,38 +28,57 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef LayerRect_h
-#define LayerRect_h
+#ifndef ScriptFunction_h
+#define ScriptFunction_h
-#include "core/dom/ClientRect.h"
-
-#include "wtf/PassRefPtr.h"
+#include "bindings/v8/GarbageCollected.h"
+#include "bindings/v8/ScriptValue.h"
+#include "bindings/v8/V8Binding.h"
#include "wtf/RefCounted.h"
-#include "wtf/RefPtr.h"
+#include <v8.h>
namespace WebCore {
-class Node;
-
-class LayerRect : public RefCounted<LayerRect> {
+// This class allows C++ to be passed as a v8::Function. To use it:
+// 1. Inherit from ScriptFunction and override call().
+// 2. toV8() will return a v8::Function that, when called, will call
+// the overridden call() method.
+//
+// This exists primarily to support ScriptPromise::then().
+class ScriptFunction : public RefCounted<ScriptFunction> {
public:
- static PassRefPtr<LayerRect> create(PassRefPtr<Node> node, PassRefPtr<ClientRect> rect)
+ ScriptFunction() { }
+
+ // override this
+ virtual ScriptValue call(ScriptValue arg) { return ScriptValue(); }
+
+ v8::Handle<v8::Function> toV8()
{
- return adoptRef(new LayerRect(node, rect));
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ if (m_function.isEmpty()) {
+ ref();
abarth-chromium 2013/08/15 00:23:54 You should be able to encapsulate all this nastine
+ m_function.set(isolate, v8::FunctionTemplate::New(&callCallback, v8::External::New(this))->GetFunction());
+ }
+ return m_function.newLocal(isolate);
}
- Node* layerRootNode() const { return m_layerRootNode.get(); }
- ClientRect* layerRelativeRect() const { return m_rect.get(); }
+protected:
+ virtual ~ScriptFunction() { }
private:
- LayerRect(PassRefPtr<Node> node, PassRefPtr<ClientRect> rect)
- : m_layerRootNode(node)
- , m_rect(rect)
+ static void callCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
{
+ v8::Isolate* isolate = args.GetIsolate();
+ ASSERT(!args.Data().IsEmpty());
+ RefPtr<ScriptFunction> function = adoptRef(static_cast<ScriptFunction*>(args.Data().As<v8::External>()->Value()));
+ v8::Local<v8::Value> value = args.Length() > 0 ? (args[0]) : v8::Local<v8::Value>(v8::Undefined(isolate));
+
+ ScriptValue result = function->call(ScriptValue(value));
+ v8SetReturnValue(args, result.v8Value());
}
- RefPtr<Node> m_layerRootNode;
- RefPtr<ClientRect> m_rect;
+ GarbageCollected<v8::Function> m_function;
abarth-chromium 2013/08/15 00:35:15 ScriptFunction need to hold a ScopedPeristent<v8::
+ friend class RefCounted<ScriptFunction>;
};
} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698