2010-05-08 13:58:43 -04:00
|
|
|
#include "rr.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"
|
2009-12-18 02:48:06 -05:00
|
|
|
#include "converters.h"
|
2009-12-07 02:20:16 -05:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2010-01-13 17:51:46 -05:00
|
|
|
VALUE V8_C_Context;
|
|
|
|
|
2010-01-13 10:13:13 -05:00
|
|
|
//TODO: rename everything to Context_
|
|
|
|
//TODO: do the object init from within here
|
|
|
|
|
2010-05-08 13:58:43 -04:00
|
|
|
void rr_init_cxt() {
|
|
|
|
VALUE Context = V8_C_Context = rr_define_class("Context");
|
|
|
|
rb_define_singleton_method(Context, "new", (VALUE(*)(...)) v8_Context_New, -1);
|
|
|
|
rb_define_singleton_method(Context, "InContext", (VALUE(*)(...)) v8_Context_InContext, 0);
|
|
|
|
rb_define_singleton_method(Context, "GetCurrent", (VALUE(*)(...)) v8_Context_GetCurrent, 0);
|
|
|
|
rb_define_method(Context, "Global", (VALUE(*)(...)) v8_cxt_Global, 0);
|
|
|
|
rb_define_method(Context, "open", (VALUE(*)(...)) v8_cxt_open, 0);
|
|
|
|
rb_define_method(Context, "eval", (VALUE(*)(...)) v8_cxt_eval, 2);
|
|
|
|
rb_define_method(Context, "eql?", (VALUE(*)(...)) v8_cxt_eql, 1);
|
|
|
|
rb_define_method(Context, "==", (VALUE(*)(...)) v8_cxt_eql, 1);
|
|
|
|
}
|
|
|
|
|
2009-12-11 12:11:05 -05:00
|
|
|
VALUE v8_Context_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 {
|
2010-01-17 17:25:40 -05:00
|
|
|
Persistent<Context> context = Context::New(0, Racer_Create_V8_ObjectTemplate(scope));
|
2010-01-13 10:13:13 -05:00
|
|
|
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;
|
2009-12-11 12:11:05 -05:00
|
|
|
}
|
2009-12-07 09:06:06 -05:00
|
|
|
}
|
|
|
|
|
2010-01-13 10:13:13 -05:00
|
|
|
VALUE v8_Context_InContext(VALUE self) {
|
2010-01-13 17:51:46 -05:00
|
|
|
return Context::InContext() ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE v8_Context_GetCurrent(VALUE self) {
|
|
|
|
HandleScope handles;
|
|
|
|
if (Context::InContext()) {
|
|
|
|
Local<Context> current = Context::GetCurrent();
|
|
|
|
return V8_Ref_Create(self, current);
|
|
|
|
} else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-01-13 10:13:13 -05:00
|
|
|
}
|
|
|
|
|
2009-12-07 09:06:06 -05:00
|
|
|
VALUE v8_cxt_Global(VALUE self) {
|
2009-12-18 02:48:06 -05:00
|
|
|
HandleScope handles;
|
2009-12-13 10:08:55 -05:00
|
|
|
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
2009-12-18 02:48:06 -05:00
|
|
|
Local<Value> global = *cxt->Global();
|
2009-12-25 23:17:30 -05:00
|
|
|
return V82RB(global);
|
2009-12-07 09:06:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE v8_cxt_open(VALUE self) {
|
|
|
|
HandleScope handles;
|
2009-12-13 10:08:55 -05:00
|
|
|
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
2009-12-07 09:06:06 -05:00
|
|
|
Context::Scope enter(cxt);
|
|
|
|
if (rb_block_given_p()) {
|
2010-01-09 11:55:37 -05:00
|
|
|
return rb_yield(self);
|
2009-12-07 09:06:06 -05:00
|
|
|
} else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2009-12-07 02:20:16 -05:00
|
|
|
}
|
|
|
|
|
2010-03-01 01:25:02 -05:00
|
|
|
VALUE Racer_Error_Message(TryCatch& exception) {
|
2010-05-10 09:13:18 -04:00
|
|
|
VALUE msg = V8_Wrap_Message(exception.Message());
|
2010-03-01 01:25:02 -05:00
|
|
|
Local<Value> stack = exception.StackTrace();
|
|
|
|
if (!stack.IsEmpty()) {
|
|
|
|
rb_iv_set(msg,"@stack",V82RB(stack));
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2010-02-08 11:53:20 -05:00
|
|
|
VALUE v8_cxt_eval(VALUE self, VALUE source, VALUE filename) {
|
2009-12-20 12:31:02 -05:00
|
|
|
HandleScope handles;
|
2010-01-09 11:55:37 -05:00
|
|
|
TryCatch exceptions;
|
2009-12-20 12:31:02 -05:00
|
|
|
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
|
|
|
Context::Scope enter(cxt);
|
|
|
|
Local<Value> source_str = RB2V8(source);
|
2010-02-08 11:53:20 -05:00
|
|
|
Local<Value> source_name = RTEST(filename) ? RB2V8(filename) : *String::New("<eval>");
|
|
|
|
Local<Script> script = Script::Compile(source_str->ToString(), source_name);
|
2010-03-01 01:25:02 -05:00
|
|
|
if (exceptions.HasCaught()) {
|
|
|
|
return Racer_Error_Message(exceptions);
|
|
|
|
}
|
2009-12-20 12:31:02 -05:00
|
|
|
Local<Value> result = script->Run();
|
2010-01-09 11:55:37 -05:00
|
|
|
if (exceptions.HasCaught()) {
|
2010-03-01 01:25:02 -05:00
|
|
|
return Racer_Error_Message(exceptions);
|
2010-01-09 11:55:37 -05:00
|
|
|
} else {
|
2010-05-13 17:53:09 -04:00
|
|
|
return rr_v82rb(result);
|
2010-01-09 11:55:37 -05:00
|
|
|
}
|
2009-12-20 12:31:02 -05:00
|
|
|
}
|
|
|
|
|
2010-01-13 17:51:46 -05:00
|
|
|
VALUE v8_cxt_eql(VALUE self, VALUE other) {
|
|
|
|
HandleScope handles;
|
|
|
|
if (RTEST(CLASS_OF(other) != V8_C_Context)) {
|
|
|
|
return Qnil;
|
|
|
|
} else {
|
|
|
|
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
|
|
|
Local<Context> that = V8_Ref_Get<Context>(other);
|
|
|
|
return cxt == that ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2009-12-07 02:20:16 -05:00
|
|
|
|