From a975215abecdda2166997491a05ff96b395d7197 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Thu, 10 Dec 2009 02:40:31 +0200 Subject: [PATCH] more doc for the referece macros --- v8_ref.cpp | 1 + v8_ref.h | 13 ++++++++++++- v8_template.cpp | 26 ++++++++++++++++++++++++++ v8_template.h | 5 +++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 v8_template.cpp create mode 100644 v8_template.h diff --git a/v8_ref.cpp b/v8_ref.cpp index d92bd94..e753ef0 100644 --- a/v8_ref.cpp +++ b/v8_ref.cpp @@ -8,6 +8,7 @@ v8_ref::v8_ref(Handle object) : handle(Persistent::New(object)) { } v8_ref::~v8_ref() { + printf("Disposing v8 reference\n"); handle.Dispose(); } diff --git a/v8_ref.h b/v8_ref.h index df2b102..5656125 100644 --- a/v8_ref.h +++ b/v8_ref.h @@ -22,8 +22,19 @@ void v8_ref_free(v8_ref* ref); //macros for helping make references +//creates a ruby VALUE reference out of any v8::Handle +// *clazz* a ruby VALUE corresponding to the ruby class of the resulting reference +// *handle* a v8::Handle object to reference from ruby. +// example: +// VALUE myclass = rb_define_class("MyClass"); +// VALUE instance = V8_Ref_Create(clazz, String::New("This is my instance")); + #define V8_Ref_Create(clazz,handle) Data_Wrap_Struct(clazz,v8_ref_mark, v8_ref_free, new v8_ref(handle)) -#define V8_Ref_Get(type,var,value) v8_ref* __ref__ = 0; Data_Get_Struct(value, struct v8_ref, __ref__); Local var = (type *)*__ref__->handle; +//Dereferences a ruby reference to a V8 object and place it in a Local handle. +// *type* V8 type of the reference. e.g. Context, Object, FunctionTemplate +// *var* the name of the variable to stuff the V8 object in. +// *value* the ruby VALUE object which contains the V8 reference (usually created with V8_Ref_Create()) +#define V8_Ref_Get(type,var,value) v8_ref* __ ## var ## _ref__ = 0; Data_Get_Struct(value, struct v8_ref, __ ## var ## _ref__); Local var = (type *)*__ ## var ## _ref__->handle; #endif \ No newline at end of file diff --git a/v8_template.cpp b/v8_template.cpp new file mode 100644 index 0000000..1c87dab --- /dev/null +++ b/v8_template.cpp @@ -0,0 +1,26 @@ +#include "ruby.h" +#include "v8.h" +#include "v8_ref.h" +#include "v8_template.h" + +using namespace v8; + +Handle RubyInvocationCallback(const Arguments& args) { + Local data = args.Data(); + return data->ToString(); +} + +VALUE v8_template_Set(VALUE self, VALUE name, VALUE value) { + HandleScope handles; + V8_Ref_Get(Template, tmpl, self); + V8_Ref_Get(Data, data, value); + tmpl->Set(RSTRING(name)->ptr, data); + return Qnil; +} + +VALUE v8_FunctionTemplate_New(VALUE clazz) { + HandleScope handles; + Local t = FunctionTemplate::New(RubyInvocationCallback, String::New("This is some data!")); + return V8_Ref_Create(clazz,t); +} + diff --git a/v8_template.h b/v8_template.h new file mode 100644 index 0000000..091e626 --- /dev/null +++ b/v8_template.h @@ -0,0 +1,5 @@ +#ifndef _RUBY_V8_TEMPLATE_ +#define _RUBY_V8_TEMPLATE_ + +VALUE v8_template_Set(VALUE self, VALUE name, VALUE value); +#endif \ No newline at end of file