mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
get rid of the disgusting macros in v8 references and replace them with template funcions
This commit is contained in:
parent
972cf8d4ab
commit
125497a742
7 changed files with 21 additions and 29 deletions
4
spike.rb
4
spike.rb
|
@ -32,9 +32,9 @@ o = V8::C::ObjectTemplate.new
|
|||
o.Set("hello", f)
|
||||
o.Set("nello", f2)
|
||||
V8::C::Context.new(o).open do |cxt|
|
||||
puts "r1: " + cxt.eval('nello()')
|
||||
puts "r1: " + cxt.eval('nello()').to_s
|
||||
puts "r2: " + cxt.eval('hello()')
|
||||
end;
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -11,20 +11,20 @@ VALUE v8_Context_New(int argc, VALUE *argv, VALUE self) {
|
|||
if (NIL_P(scope)) {
|
||||
return V8_Ref_Create(self, Context::New());
|
||||
} else {
|
||||
V8_Ref_Get(ObjectTemplate, t, scope);
|
||||
Local<ObjectTemplate> t = V8_Ref_Get<ObjectTemplate>(scope);
|
||||
return V8_Ref_Create(self, Context::New(0, t));
|
||||
}
|
||||
}
|
||||
|
||||
VALUE v8_cxt_Global(VALUE self) {
|
||||
V8_Ref_Get(Context, cxt, self);
|
||||
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
||||
cxt->Global();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE v8_cxt_open(VALUE self) {
|
||||
HandleScope handles;
|
||||
V8_Ref_Get(Context, cxt, self);
|
||||
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
||||
Context::Scope enter(cxt);
|
||||
if (rb_block_given_p()) {
|
||||
return rb_yield(self);
|
||||
|
|
|
@ -22,3 +22,6 @@ void v8_ref_free(v8_ref* ref) {
|
|||
delete ref;
|
||||
}
|
||||
|
||||
VALUE V8_Ref_Create(VALUE ruby_class, v8::Handle<void> handle, VALUE ref) {
|
||||
return Data_Wrap_Struct(ruby_class, v8_ref_mark, v8_ref_free, new v8_ref(handle, ref));
|
||||
}
|
||||
|
|
23
v8_ref.h
23
v8_ref.h
|
@ -20,23 +20,12 @@ struct v8_ref {
|
|||
void v8_ref_mark(v8_ref* ref);
|
||||
void v8_ref_free(v8_ref* ref);
|
||||
|
||||
VALUE V8_Ref_Create(VALUE ruby_class, v8::Handle<void> handle, VALUE ref = 0);
|
||||
|
||||
//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_Create2(clazz,handle, references) Data_Wrap_Struct(clazz,v8_ref_mark, v8_ref_free, new v8_ref(handle, references))
|
||||
|
||||
//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;
|
||||
template <class T> v8::Local<T> V8_Ref_Get(VALUE object) {
|
||||
v8_ref* ref = 0;
|
||||
Data_Get_Struct(object, struct v8_ref, ref);
|
||||
return (T *)*ref->handle;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -11,13 +11,13 @@ using namespace v8;
|
|||
|
||||
VALUE v8_script_new(VALUE self, VALUE source) {
|
||||
HandleScope handles;
|
||||
V8_Ref_Get(String, src, source);
|
||||
Local<String> src = V8_Ref_Get<String>(source);
|
||||
return V8_Ref_Create(self, Script::New(src));
|
||||
}
|
||||
|
||||
VALUE v8_script_Run(VALUE self) {
|
||||
HandleScope handles;
|
||||
V8_Ref_Get(Script, script, self);
|
||||
Local<Script> script = V8_Ref_Get<Script>(self);
|
||||
V8HandleSource<RubyDest, VALUE> toValue;
|
||||
Local<Value> result = script->Run();
|
||||
return toValue.push(result);
|
||||
|
|
|
@ -12,6 +12,6 @@ VALUE v8_str_new(VALUE clazz, VALUE str) {
|
|||
|
||||
VALUE v8_str_to_s(VALUE self){
|
||||
HandleScope handles;
|
||||
V8_Ref_Get(String, str, self);
|
||||
Local<String> str = V8_Ref_Get<String>(self);
|
||||
return rb_str_new2(*String::AsciiValue(str));
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ using namespace v8;
|
|||
Handle<Value> RubyInvocationCallback(const Arguments& args) {
|
||||
VALUE code = (VALUE)External::Unwrap(args.Data());
|
||||
if (NIL_P(code)) {
|
||||
return String::New("Code was nil");
|
||||
return Null();
|
||||
} else {
|
||||
VALUE result = rb_funcall(code, rb_intern("call"), 0);
|
||||
return String::New("Function Was Called");
|
||||
|
@ -17,8 +17,8 @@ Handle<Value> RubyInvocationCallback(const Arguments& args) {
|
|||
|
||||
VALUE v8_Template_Set(VALUE self, VALUE name, VALUE value) {
|
||||
HandleScope handles;
|
||||
V8_Ref_Get(Template, tmpl, self);
|
||||
V8_Ref_Get(Data, data, value);
|
||||
Local<Template> tmpl = V8_Ref_Get<Template>(self);
|
||||
Local<Data> data = V8_Ref_Get<Data>(value);
|
||||
tmpl->Set(RSTRING(name)->ptr, data);
|
||||
return Qnil;
|
||||
}
|
||||
|
@ -33,6 +33,6 @@ VALUE v8_FunctionTemplate_New(int argc, VALUE *argv, VALUE self) {
|
|||
rb_scan_args(argc, argv, "00&", &code);
|
||||
HandleScope handles;
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(RubyInvocationCallback, External::Wrap((void *)code));
|
||||
return V8_Ref_Create2(self,t,code);
|
||||
return V8_Ref_Create(self,t,code);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue