2010-05-08 13:58:43 -04:00
|
|
|
#include "rr.h"
|
2010-06-07 03:13:26 -04:00
|
|
|
#include "v8_ref.h"
|
2009-12-07 02:20:16 -05:00
|
|
|
#include "v8_cxt.h"
|
2009-12-18 02:48:06 -05:00
|
|
|
#include "v8_msg.h"
|
2010-01-17 17:25:40 -05:00
|
|
|
#include "v8_template.h"
|
2010-05-26 12:54:56 -04:00
|
|
|
#include "v8_external.h"
|
2009-12-07 02:20:16 -05:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
namespace {
|
2010-06-07 03:13:26 -04:00
|
|
|
|
|
|
|
VALUE ContextClass;
|
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
Local<Context> unwrap(VALUE value) {
|
|
|
|
return V8_Ref_Get<Context>(value);
|
2009-12-11 12:11:05 -05:00
|
|
|
}
|
2010-06-06 07:22:02 -04:00
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
VALUE New(int argc, VALUE *argv, VALUE self) {
|
|
|
|
HandleScope handles;
|
2010-06-06 07:22:02 -04:00
|
|
|
VALUE global_template; VALUE global_object;
|
|
|
|
rb_scan_args(argc,argv, "02", &global_template, &global_object);
|
|
|
|
Handle<ObjectTemplate> v8_global_template(NIL_P(global_template) ? Handle<ObjectTemplate>() : V8_Ref_Get<ObjectTemplate>(global_template));
|
|
|
|
Handle<Value> v8_global_object(NIL_P(global_object) ? Handle<Value>() : V8_Ref_Get<Value>(global_object));
|
|
|
|
Persistent<Context> cxt(Context::New(0, v8_global_template, v8_global_object));
|
2010-06-07 03:13:26 -04:00
|
|
|
VALUE ref = rr_v8_ref_create(self, cxt);
|
2010-06-06 07:22:02 -04:00
|
|
|
cxt.Dispose();
|
|
|
|
return ref;
|
2010-01-13 17:51:46 -05:00
|
|
|
}
|
2010-01-13 10:13:13 -05:00
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
VALUE InContext(VALUE self) {
|
|
|
|
return Context::InContext() ? Qtrue : Qfalse;
|
|
|
|
}
|
2010-06-07 03:13:26 -04:00
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
VALUE GetEntered(VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
if (Context::InContext()) {
|
|
|
|
Local<Context> current = Context::GetEntered();
|
2010-06-07 03:13:26 -04:00
|
|
|
return rr_v8_ref_create(self, current);
|
2010-05-17 08:13:23 -04:00
|
|
|
} else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
2010-06-07 03:13:26 -04:00
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
VALUE Global(VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
return rr_v82rb(unwrap(self)->Global());
|
|
|
|
}
|
2010-06-07 03:13:26 -04:00
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
VALUE Enter(VALUE self) {
|
2010-05-19 08:55:11 -04:00
|
|
|
HandleScope handles;
|
2010-05-17 08:13:23 -04:00
|
|
|
unwrap(self)->Enter();
|
|
|
|
return self;
|
|
|
|
}
|
2010-06-07 03:13:26 -04:00
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
VALUE Exit(VALUE self) {
|
2010-05-19 08:55:11 -04:00
|
|
|
HandleScope handles;
|
2010-05-17 08:13:23 -04:00
|
|
|
unwrap(self)->Exit();
|
|
|
|
return self;
|
|
|
|
}
|
2010-06-07 03:13:26 -04:00
|
|
|
|
2010-05-18 08:45:36 -04:00
|
|
|
VALUE IsEntered(VALUE self) {
|
2010-05-19 08:55:11 -04:00
|
|
|
HandleScope handles;
|
2010-05-18 08:45:36 -04:00
|
|
|
if (Context::InContext()) {
|
|
|
|
return rr_v82rb(unwrap(self) == Context::GetEntered());
|
|
|
|
} else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 11:46:30 -04:00
|
|
|
VALUE GetData(VALUE self) {
|
|
|
|
HandleScope scope;
|
|
|
|
return rr_v82rb(unwrap(self)->GetData());
|
|
|
|
}
|
|
|
|
VALUE SetData(VALUE self, VALUE data) {
|
|
|
|
HandleScope scope;
|
2010-08-28 17:48:49 -04:00
|
|
|
unwrap(self)->SetData(rr_rb2v8(data)->ToString());
|
2010-08-28 11:46:30 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
2010-03-01 01:25:02 -05:00
|
|
|
}
|
|
|
|
|
2010-05-17 08:13:23 -04:00
|
|
|
void rr_init_cxt() {
|
2010-06-07 03:13:26 -04:00
|
|
|
ContextClass = rr_define_class("Context");
|
2010-05-18 08:45:36 -04:00
|
|
|
rr_define_singleton_method(ContextClass, "New", New, -1);
|
|
|
|
rr_define_singleton_method(ContextClass, "InContext", InContext, 0);
|
|
|
|
rr_define_singleton_method(ContextClass, "GetEntered", GetEntered, 0);
|
|
|
|
rr_define_method(ContextClass, "Global", Global, 0);
|
|
|
|
rr_define_method(ContextClass, "Enter", Enter, 0);
|
|
|
|
rr_define_method(ContextClass, "Exit", Exit, 0);
|
|
|
|
rr_define_method(ContextClass, "IsEntered", IsEntered, 0);
|
2010-08-28 11:46:30 -04:00
|
|
|
rr_define_method(ContextClass, "GetData", GetData, 0);
|
|
|
|
rr_define_method(ContextClass, "SetData", SetData, 1);
|
2009-12-20 12:31:02 -05:00
|
|
|
}
|
|
|
|
|