mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
more doc for the referece macros
This commit is contained in:
parent
97cea13740
commit
a975215abe
4 changed files with 44 additions and 1 deletions
|
@ -8,6 +8,7 @@ v8_ref::v8_ref(Handle<void> object) : handle(Persistent<void>::New(object)) {
|
|||
}
|
||||
|
||||
v8_ref::~v8_ref() {
|
||||
printf("Disposing v8 reference\n");
|
||||
handle.Dispose();
|
||||
}
|
||||
|
||||
|
|
13
v8_ref.h
13
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<T> 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<type> var = (type *)*__ref__->handle;
|
||||
//Dereferences a ruby reference to a V8 object and place it in a Local<T> 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<type> var = (type *)*__ ## var ## _ref__->handle;
|
||||
|
||||
#endif
|
26
v8_template.cpp
Normal file
26
v8_template.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "ruby.h"
|
||||
#include "v8.h"
|
||||
#include "v8_ref.h"
|
||||
#include "v8_template.h"
|
||||
|
||||
using namespace v8;
|
||||
|
||||
Handle<Value> RubyInvocationCallback(const Arguments& args) {
|
||||
Local<Value> 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<FunctionTemplate> t = FunctionTemplate::New(RubyInvocationCallback, String::New("This is some data!"));
|
||||
return V8_Ref_Create(clazz,t);
|
||||
}
|
||||
|
5
v8_template.h
Normal file
5
v8_template.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#ifndef _RUBY_V8_TEMPLATE_
|
||||
#define _RUBY_V8_TEMPLATE_
|
||||
|
||||
VALUE v8_template_Set(VALUE self, VALUE name, VALUE value);
|
||||
#endif
|
Loading…
Add table
Reference in a new issue