Index: Source/bindings/v8/ScriptFunction.h |
diff --git a/Source/core/testing/LayerRect.h b/Source/bindings/v8/ScriptFunction.h |
similarity index 54% |
copy from Source/core/testing/LayerRect.h |
copy to Source/bindings/v8/ScriptFunction.h |
index f945681417ff788255e29f66eca4932b87855dd2..c2d89bd23265ef1137622443471afd9d1f975501 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 "ScriptValue.h" |
+#include "V8Binding.h" |
abarth-chromium
2013/08/09 21:46:52
Please use paths from the Source directory
|
#include "wtf/RefCounted.h" |
-#include "wtf/RefPtr.h" |
abarth-chromium
2013/08/09 21:46:52
No need for this blank line.
|
-namespace WebCore { |
+#include <v8.h> |
-class Node; |
+namespace WebCore { |
-class LayerRect : public RefCounted<LayerRect> { |
+// to use this class, |
abarth-chromium
2013/08/09 21:46:52
?
|
+class ScriptFunction : public RefCounted<ScriptFunction> { |
public: |
- static PassRefPtr<LayerRect> create(PassRefPtr<Node> node, PassRefPtr<ClientRect> rect) |
+ ScriptFunction() { } |
+ virtual ~ScriptFunction() { } |
+ |
+ // override this |
+ virtual ScriptValue call(ScriptValue arg) { return ScriptValue(); } |
+ |
+ v8::Handle<v8::Function> toV8(v8::Isolate* isolate) |
{ |
- return adoptRef(new LayerRect(node, rect)); |
+ if (m_function.isEmpty()) { |
+ ref(); |
abarth-chromium
2013/08/09 21:46:52
Rather than calling ref() and deref(), maybe we sh
|
+ m_function.set(isolate, v8::FunctionTemplate::New(&callCallback, v8::External::New(this))->GetFunction()); |
+ m_function.makeWeak(this, &weakCallback); |
+ } |
+ return m_function.newLocal(isolate); |
} |
- Node* layerRootNode() const { return m_layerRootNode.get(); } |
- ClientRect* layerRelativeRect() const { return m_rect.get(); } |
- |
private: |
- LayerRect(PassRefPtr<Node> node, PassRefPtr<ClientRect> rect) |
- : m_layerRootNode(node) |
- , m_rect(rect) |
+ static void weakCallback(v8::Isolate*, v8::Persistent<v8::Function>*, ScriptFunction* self) |
{ |
+ self->deref(); |
abarth-chromium
2013/08/09 21:46:52
Don't we need to clear out m_function in this func
|
+ } |
+ |
+ static void callCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
+ { |
+ v8::Isolate* isolate = args.GetIsolate(); |
+ ASSERT(!args.Data().IsEmpty()); |
+ ScriptFunction* function = static_cast<ScriptFunction*>(args.Data().As<v8::External>()->Value()); |
abarth-chromium
2013/08/09 21:46:52
We need to hold a RefPtr<ScriptFunction> here in c
|
+ v8::Local<v8::Value> value = v8::Undefined(isolate); |
+ if (args.Length() > 0) |
+ value = args[0]; |
abarth-chromium
2013/08/09 21:46:52
Can we use the ? : operator to avoid calling v8::U
alecflett
2013/08/13 20:37:11
Doing that now - I see why it was done the other w
|
+ |
+ ScriptValue result = function->call(ScriptValue(value)); |
+ v8SetReturnValue(args, result.v8Value()); |
} |
- RefPtr<Node> m_layerRootNode; |
- RefPtr<ClientRect> m_rect; |
+ ScopedPersistent<v8::Function> m_function; |
+ friend class RefCounted<ScriptFunction>; |
}; |
} // namespace WebCore |