1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ext/v8/v8_cxt.cpp

55 lines
1.3 KiB
C++
Raw Normal View History

#include "v8_cxt.h"
2009-12-18 02:48:06 -05:00
#include "v8_msg.h"
#include "converters.h"
using namespace v8;
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 {
Local<ObjectTemplate> t = V8_Ref_Get<ObjectTemplate>(scope);
return V8_Ref_Create(self, Context::New(0, t));
}
2009-12-07 09:06:06 -05:00
}
VALUE v8_cxt_Global(VALUE self) {
2009-12-18 02:48:06 -05:00
HandleScope handles;
Local<Context> cxt = V8_Ref_Get<Context>(self);
2009-12-18 02:48:06 -05:00
Local<Value> global = *cxt->Global();
return V82RB(global);
2009-12-07 09:06:06 -05:00
}
VALUE v8_cxt_open(VALUE self) {
HandleScope handles;
2009-12-18 02:48:06 -05:00
TryCatch exceptions;
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()) {
2009-12-18 02:48:06 -05:00
VALUE result = rb_yield(self);
if (exceptions.HasCaught()) {
return V8_Wrap_Message(exceptions.Message());
} else {
return result;
}
return result;
2009-12-07 09:06:06 -05:00
} else {
return Qnil;
}
}
VALUE v8_cxt_eval(VALUE self, VALUE source) {
HandleScope handles;
Local<Context> cxt = V8_Ref_Get<Context>(self);
Context::Scope enter(cxt);
Local<Value> source_str = RB2V8(source);
Local<Script> script = Script::Compile(source_str->ToString());
Local<Value> result = script->Run();
return V82RB(result);
}