2010-05-08 12:58:43 -05:00
|
|
|
#include "rr.h"
|
2009-12-07 09:20:16 +02:00
|
|
|
#include "v8_cxt.h"
|
2009-12-18 09:48:06 +02:00
|
|
|
#include "v8_msg.h"
|
2010-01-18 00:25:40 +02:00
|
|
|
#include "v8_template.h"
|
2009-12-18 09:48:06 +02:00
|
|
|
#include "converters.h"
|
2009-12-07 09:20:16 +02:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2010-01-14 00:51:46 +02:00
|
|
|
VALUE V8_C_Context;
|
|
|
|
|
2010-01-13 17:13:13 +02:00
|
|
|
//TODO: rename everything to Context_
|
|
|
|
//TODO: do the object init from within here
|
|
|
|
|
2010-05-08 12:58:43 -05:00
|
|
|
|
2010-05-17 15:13:23 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
Local<Context> unwrap(VALUE value) {
|
|
|
|
return V8_Ref_Get<Context>(value);
|
2009-12-11 19:11:05 +02:00
|
|
|
}
|
2010-05-17 15:13:23 +03:00
|
|
|
|
|
|
|
VALUE New(int argc, VALUE *argv, VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
VALUE scope;
|
|
|
|
rb_scan_args(argc,argv, "01", &scope);
|
|
|
|
if (NIL_P(scope)) {
|
|
|
|
return V8_Ref_Create(self, Context::New());
|
|
|
|
} else {
|
|
|
|
Persistent<Context> context = Context::New(0, Racer_Create_V8_ObjectTemplate(scope));
|
|
|
|
Context::Scope enter(context);
|
|
|
|
context->Global()->SetHiddenValue(String::New("TheRubyRacer::RubyObject"), External::Wrap((void *)scope));
|
|
|
|
VALUE ref = V8_Ref_Create(self, context, scope);
|
|
|
|
context.Dispose();
|
|
|
|
return ref;
|
|
|
|
}
|
2010-01-14 00:51:46 +02:00
|
|
|
}
|
2010-01-13 17:13:13 +02:00
|
|
|
|
2010-05-17 15:13:23 +03:00
|
|
|
VALUE InContext(VALUE self) {
|
|
|
|
return Context::InContext() ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE GetEntered(VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
if (Context::InContext()) {
|
|
|
|
Local<Context> current = Context::GetEntered();
|
|
|
|
return V8_Ref_Create(self, current);
|
|
|
|
} else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Global(VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
return rr_v82rb(unwrap(self)->Global());
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Enter(VALUE self) {
|
|
|
|
unwrap(self)->Enter();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Exit(VALUE self) {
|
|
|
|
unwrap(self)->Exit();
|
|
|
|
return self;
|
|
|
|
}
|
2010-03-01 00:25:02 -06:00
|
|
|
}
|
|
|
|
|
2010-05-17 15:13:23 +03:00
|
|
|
void rr_init_cxt() {
|
|
|
|
VALUE Context = V8_C_Context = rr_define_class("Context");
|
|
|
|
rr_define_singleton_method(Context, "new", New, -1);
|
|
|
|
rr_define_singleton_method(Context, "New", New, -1);
|
|
|
|
rr_define_singleton_method(Context, "InContext", InContext, 0);
|
|
|
|
rr_define_singleton_method(Context, "GetEntered", GetEntered, 0);
|
|
|
|
rr_define_method(Context, "Global", Global, 0);
|
|
|
|
rr_define_method(Context, "Enter", Enter, 0);
|
|
|
|
rr_define_method(Context, "Exit", Exit, 0);
|
2009-12-20 19:31:02 +02:00
|
|
|
}
|
|
|
|
|
2010-05-17 15:13:23 +03:00
|
|
|
VALUE rr_reflect_v8_context(Handle<Context> value) {
|
|
|
|
return V8_Ref_Create(V8_C_Context, value);
|
2010-01-14 00:51:46 +02:00
|
|
|
}
|
|
|
|
|
2009-12-07 09:20:16 +02:00
|
|
|
|