mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
use macros to shorten the ref alloc/deref cycle
This commit is contained in:
parent
adf98eeb57
commit
97cea13740
4 changed files with 14 additions and 27 deletions
13
v8_cxt.cpp
13
v8_cxt.cpp
|
@ -4,26 +4,19 @@
|
|||
|
||||
using namespace v8;
|
||||
|
||||
Local<Context> VALUE_TO_CONTEXT(VALUE value) {
|
||||
v8_ref* ref = 0;
|
||||
Data_Get_Struct(value, struct v8_ref, ref);
|
||||
return (Context *)(*ref->handle);
|
||||
}
|
||||
|
||||
VALUE v8_cxt_allocate(VALUE clazz) {
|
||||
v8_ref* ref = new v8_ref(Context::New());
|
||||
return Data_Wrap_Struct(clazz, v8_ref_mark , v8_ref_free, ref);
|
||||
return V8_Ref_Create(clazz, Context::New());
|
||||
}
|
||||
|
||||
VALUE v8_cxt_Global(VALUE self) {
|
||||
Local<Context> cxt = VALUE_TO_CONTEXT(self);
|
||||
V8_Ref_Get(Context, cxt, self);
|
||||
cxt->Global();
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE v8_cxt_open(VALUE self) {
|
||||
HandleScope handles;
|
||||
Local<Context> cxt = VALUE_TO_CONTEXT(self);
|
||||
V8_Ref_Get(Context, cxt, self);
|
||||
Context::Scope enter(cxt);
|
||||
if (rb_block_given_p()) {
|
||||
return rb_yield(self);
|
||||
|
|
6
v8_ref.h
6
v8_ref.h
|
@ -20,4 +20,10 @@ void v8_ref_mark(v8_ref* ref);
|
|||
void v8_ref_free(v8_ref* ref);
|
||||
|
||||
|
||||
//macros for helping make references
|
||||
|
||||
#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;
|
||||
|
||||
#endif
|
|
@ -8,25 +8,16 @@
|
|||
|
||||
using namespace v8;
|
||||
|
||||
Local<Script> VALUE_TO_SCRIPT(VALUE value) {
|
||||
v8_ref* ref = 0;
|
||||
Data_Get_Struct(value, struct v8_ref, ref);
|
||||
return (Script *)(*ref->handle);
|
||||
}
|
||||
|
||||
VALUE v8_script_new(VALUE self, VALUE source) {
|
||||
v8_ref* src_ref = 0;
|
||||
Data_Get_Struct(source, struct v8_ref, src_ref);
|
||||
HandleScope handles;
|
||||
|
||||
Local<String> src((String *)*src_ref->handle);
|
||||
v8_ref* script_ref = new v8_ref(Script::New(src));
|
||||
return Data_Wrap_Struct(self, v8_ref_mark, v8_ref_free, script_ref);
|
||||
V8_Ref_Get(String, src, source);
|
||||
return V8_Ref_Create(self, Script::New(src));
|
||||
}
|
||||
|
||||
VALUE v8_script_Run(VALUE self) {
|
||||
HandleScope handles;
|
||||
Local<Script> script = VALUE_TO_SCRIPT(self);
|
||||
V8_Ref_Get(Script, script, self);
|
||||
V8HandleSource<RubyDest, VALUE> toValue;
|
||||
Local<Value> result = script->Run();
|
||||
return toValue.push(result);
|
||||
|
|
|
@ -7,14 +7,11 @@ using namespace v8;
|
|||
|
||||
VALUE v8_str_new(VALUE clazz, VALUE str) {
|
||||
HandleScope handles;
|
||||
v8_ref* ref = new v8_ref(String::New(RSTRING(str)->ptr));
|
||||
return Data_Wrap_Struct(clazz, v8_ref_mark , v8_ref_free, ref);
|
||||
return V8_Ref_Create(clazz, String::New(RSTRING(str)->ptr));
|
||||
}
|
||||
|
||||
VALUE v8_str_to_s(VALUE self){
|
||||
HandleScope handles;
|
||||
v8_ref* ref = 0;
|
||||
Data_Get_Struct(self, struct v8_ref, ref);
|
||||
Local<String> str = (String *)*ref->handle;
|
||||
V8_Ref_Get(String, str, self);
|
||||
return rb_str_new2(*String::AsciiValue(str));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue