2009-10-15 23:23:53 -04:00
|
|
|
#include "ruby_data.h"
|
|
|
|
#include "v8_data.h"
|
|
|
|
#include "generic_data.h"
|
2009-10-07 23:35:39 -04:00
|
|
|
#include <stdio.h>
|
2009-10-08 22:38:26 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* struct that encapsulates a v8 context. this
|
|
|
|
* object's lifetime matches the lifetime of
|
|
|
|
* the ruby v8 context item.
|
|
|
|
*/
|
|
|
|
typedef struct v8_context {
|
|
|
|
v8_context() : context(v8::Context::New()) {}
|
|
|
|
~v8_context() {
|
|
|
|
context.Dispose();
|
|
|
|
}
|
|
|
|
v8::Persistent<v8::Context> context;
|
2009-10-08 21:22:29 -04:00
|
|
|
} v8_context;
|
2009-10-07 23:35:39 -04:00
|
|
|
|
2009-10-08 20:46:59 -04:00
|
|
|
extern "C" {
|
2009-10-17 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* ruby init method for the extension
|
|
|
|
*/
|
|
|
|
void Init_v8();
|
2009-10-01 22:17:06 -04:00
|
|
|
}
|
2009-10-08 23:27:41 -04:00
|
|
|
|
2009-10-08 20:46:59 -04:00
|
|
|
VALUE v8_allocate(VALUE clazz);
|
2009-10-08 21:22:29 -04:00
|
|
|
void v8_mark(v8_context *s);
|
|
|
|
void v8_free(v8_context *s);
|
2009-10-01 22:17:06 -04:00
|
|
|
|
2009-10-08 23:50:51 -04:00
|
|
|
VALUE eval(VALUE self, VALUE javascript);
|
2009-10-07 23:35:39 -04:00
|
|
|
|
2009-10-08 20:46:59 -04:00
|
|
|
VALUE rb_mModule;
|
|
|
|
VALUE rb_cV8;
|
|
|
|
|
|
|
|
extern "C" {
|
2009-10-17 23:45:02 -04:00
|
|
|
void Init_v8() {
|
|
|
|
rb_mModule = rb_define_module("V8");
|
|
|
|
rb_cV8 = rb_define_class_under(rb_mModule, "Context", rb_cObject);
|
|
|
|
rb_define_alloc_func(rb_cV8, v8_allocate);
|
|
|
|
rb_define_method(rb_cV8, "eval", (VALUE(*)(...)) eval, 1);
|
|
|
|
}
|
2009-10-08 20:46:59 -04:00
|
|
|
}
|
2009-10-07 23:35:39 -04:00
|
|
|
|
2009-10-08 20:46:59 -04:00
|
|
|
VALUE v8_allocate(VALUE clazz) {
|
2009-10-08 21:22:29 -04:00
|
|
|
v8_context *s = new v8_context;
|
2009-10-08 20:46:59 -04:00
|
|
|
return Data_Wrap_Struct(clazz, v8_mark, v8_free, s);
|
|
|
|
}
|
2009-10-07 23:35:39 -04:00
|
|
|
|
2009-10-08 21:22:29 -04:00
|
|
|
void v8_mark(v8_context *s) {
|
2009-10-08 20:46:59 -04:00
|
|
|
// marked for gc?
|
|
|
|
}
|
2009-10-07 23:35:39 -04:00
|
|
|
|
2009-10-08 21:22:29 -04:00
|
|
|
void v8_free(v8_context *s) {
|
|
|
|
delete s;
|
2009-10-08 20:46:59 -04:00
|
|
|
}
|
2009-10-07 23:35:39 -04:00
|
|
|
|
2009-10-17 23:45:02 -04:00
|
|
|
/**
|
|
|
|
* eval a javascript. Currently, return
|
|
|
|
* types can only be primitive types.
|
|
|
|
* @param self ruby handle to a v8_context struct
|
|
|
|
* @param javascript, should be a string
|
|
|
|
* @return the result of evaluating the script, if
|
|
|
|
* it can be converted
|
|
|
|
*/
|
2009-10-08 23:50:51 -04:00
|
|
|
VALUE eval(VALUE self, VALUE javascript) {
|
2009-10-17 23:45:02 -04:00
|
|
|
v8_context* s = 0;
|
|
|
|
Data_Get_Struct(self, struct v8_context, s);
|
|
|
|
v8::Context::Scope context_scope(s->context);
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
RubyValueSource<StringDest, std::string> tostring;
|
|
|
|
const std::string text(tostring.push(javascript));
|
|
|
|
v8::Handle<v8::String> source = v8::String::New(text.c_str());
|
|
|
|
v8::Handle<v8::Script> script = v8::Script::Compile(source);
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> local_result = script->Run();
|
|
|
|
V8HandleSource<RubyDest, VALUE> toValue;
|
|
|
|
return toValue.push(local_result);
|
2009-10-08 20:46:59 -04:00
|
|
|
}
|