From 1f706632428a86bbfe697e61a3b15088b6071045 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Thu, 17 May 2012 22:03:34 -0500 Subject: [PATCH] FunctionTemplate, ObjectTemplate, Signature wrap these three interfaces which are critical for defining v8 functions and calbacks. --- ext/v8/rr.h | 26 +++++++++++++-- ext/v8/signature.cc | 16 ++++++++++ ext/v8/template.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 ext/v8/signature.cc diff --git a/ext/v8/rr.h b/ext/v8/rr.h index 00120dd..30add1c 100644 --- a/ext/v8/rr.h +++ b/ext/v8/rr.h @@ -69,9 +69,13 @@ public: return (new Holder(handle, Class))->value; } virtual operator v8::Handle() const { - Holder* holder = NULL; - Data_Get_Struct(this->value, class Holder, holder); - return holder->handle; + if (RTEST(this->value)) { + Holder* holder = NULL; + Data_Get_Struct(this->value, class Holder, holder); + return holder->handle; + } else { + return v8::Handle(); + } } inline v8::Handle operator->() const { return *this;} inline v8::Handle GetHandle() const { return *this;} @@ -323,15 +327,31 @@ public: inline Array(VALUE value) : Ref(value) {} }; +class Signature : public Ref { + static void Init(); + static VALUE New(int argc, VALUE argv[], VALUE self); + + inline Signature(v8::Handle sig) : Ref(sig) {} + inline Signature(VALUE value) : Ref(value) {} +}; + class Template { public: static void Init(); }; +class ObjectTemplate : public Ref { +public: + static void Init(); +}; + class FunctionTemplate : public Ref { public: static void Init(); + static VALUE New(int argc, VALUE argv[], VALUE self); + inline FunctionTemplate(VALUE value) : Ref(value) {} + inline FunctionTemplate(v8::Handle t) : Ref(t) {} }; class V8 { diff --git a/ext/v8/signature.cc b/ext/v8/signature.cc new file mode 100644 index 0000000..dc1b874 --- /dev/null +++ b/ext/v8/signature.cc @@ -0,0 +1,16 @@ +#include "rr.h" + +namespace rr { + void Signature::Init() { + ClassBuilder("Signature"). + defineMethod("New", &New). + store(&Class); + } + + VALUE Signature::New(int length, VALUE args[], VALUE self) { + VALUE receiver; VALUE argc; VALUE argv; + rb_scan_args(length, args, "03", &receiver, &argc, &argv); + std::vector< v8::Handle > parameters(Int(argc)); + return Signature(v8::Signature::New(FunctionTemplate(receiver), Int(argc), FunctionTemplate::array(argv, parameters))); + } +} \ No newline at end of file diff --git a/ext/v8/template.cc b/ext/v8/template.cc index ec72c48..de6369d 100644 --- a/ext/v8/template.cc +++ b/ext/v8/template.cc @@ -3,11 +3,89 @@ namespace rr { void Template::Init() { ClassBuilder("Template"); + ObjectTemplate::Init(); FunctionTemplate::Init(); } + void ObjectTemplate::Init() { + ClassBuilder("ObjectTemplate", "Template"). + store(&Class); + } + void FunctionTemplate::Init() { ClassBuilder("FunctionTemplate", "Template"). + defineMethod("New", &New). store(&Class); } + + VALUE FunctionTemplate::New(int argc, VALUE argv[], VALUE self) { + VALUE code; VALUE data; VALUE signature; + rb_scan_args(argc, argv, "03", &code, &data, &signature); + return FunctionTemplate(v8::FunctionTemplate::New()); + // InvocationCallback callback(code, data); + // return FunctionTemplate(v8::FunctionTemplate::New(callback, callback.data, Signature(signature))); + } + + // /** Creates a function template.*/ + // static Local New( + // InvocationCallback callback = 0, + // Handle data = Handle(), + // Handle signature = Handle()); + // /** Returns the unique function instance in the current execution context.*/ + // Local GetFunction(); + // + // /** + // * Set the call-handler callback for a FunctionTemplate. This + // * callback is called whenever the function created from this + // * FunctionTemplate is called. + // */ + // void SetCallHandler(InvocationCallback callback, + // Handle data = Handle()); + // + // /** Get the InstanceTemplate. */ + // Local InstanceTemplate(); + // + // /** Causes the function template to inherit from a parent function template.*/ + // void Inherit(Handle parent); + // + // /** + // * A PrototypeTemplate is the template used to create the prototype object + // * of the function created by this template. + // */ + // Local PrototypeTemplate(); + // + // + // /** + // * Set the class name of the FunctionTemplate. This is used for + // * printing objects created with the function created from the + // * FunctionTemplate as its constructor. + // */ + // void SetClassName(Handle name); + // + // /** + // * Determines whether the __proto__ accessor ignores instances of + // * the function template. If instances of the function template are + // * ignored, __proto__ skips all instances and instead returns the + // * next object in the prototype chain. + // * + // * Call with a value of true to make the __proto__ accessor ignore + // * instances of the function template. Call with a value of false + // * to make the __proto__ accessor not ignore instances of the + // * function template. By default, instances of a function template + // * are not ignored. + // */ + // void SetHiddenPrototype(bool value); + // + // /** + // * Sets the ReadOnly flag in the attributes of the 'prototype' property + // * of functions created from this FunctionTemplate to true. + // */ + // void ReadOnlyPrototype(); + // + // /** + // * Returns true if the given object is an instance of this function + // * template. + // */ + // bool HasInstance(Handle object); + } \ No newline at end of file