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

spike out super thin v8 wrappers

This commit is contained in:
Charles Lowell 2009-12-07 16:06:06 +02:00
parent 69bc0f317c
commit 006e846e6a
11 changed files with 155 additions and 38 deletions

30
spike.rb Normal file
View file

@ -0,0 +1,30 @@
`make clean all`
require 'v8';
#manually run a script
result = V8::C::Context.new.open do |cxt|
v8_str = V8::C::String.new('1 + 1')
v8_script = V8::C::Script.new(v8_str)
v8_script.Run()
end
puts "result: #{result}"
#define eval() purely in ruby
class V8::C::Context
def eval(javascript)
self.open do
source = V8::C::String.new(javascript)
script = V8::C::Script.new(source)
script.Run()
end
end
end
cxt = V8::C::Context.new
my_result = cxt.eval('7 * 6')
puts "my result: #{my_result}"

View file

@ -1,4 +0,0 @@
#!/usr/bin/env bash
make clean all
ruby -e "require 'v8'; V8::N::Context.new"

21
v8.cpp
View file

@ -3,6 +3,8 @@
#include "v8_data.h"
#include "v8_context.h"
#include "v8_cxt.h"
#include "v8_str.h"
#include "v8_script.h"
#include "v8_standalone.h"
#include <stdio.h>
@ -34,9 +36,22 @@ extern "C" {
rb_define_method(rb_cV8, "[]=", (VALUE(*)(...)) v8_context_inject, 2);
//native module setup
VALUE rb_mNative = rb_define_module_under(rb_mModule, "N");
VALUE V8_N_Context = rb_define_class_under(rb_mNative, "Context", rb_cObject);
rb_define_alloc_func(V8_N_Context, v8_cxt_allocate);
VALUE rb_mNative = rb_define_module_under(rb_mModule, "C");
//native context
VALUE V8__C__Context = rb_define_class_under(rb_mNative, "Context", rb_cObject);
rb_define_alloc_func(V8__C__Context, v8_cxt_allocate);
rb_define_method(V8__C__Context, "Global", (VALUE(*)(...)) v8_cxt_Global, 0);
rb_define_method(V8__C__Context, "open", (VALUE(*)(...)) v8_cxt_open, 0);
//native String
VALUE V8__C__String = rb_define_class_under(rb_mNative, "String", rb_cObject);
rb_define_singleton_method(V8__C__String, "new", (VALUE(*)(...)) v8_str_new, 1);
rb_define_method(V8__C__String, "to_s", (VALUE(*)(...)) v8_str_to_s, 0);
VALUE V8__C__Script = rb_define_class_under(rb_mNative, "Script", rb_cObject);
rb_define_singleton_method(V8__C__Script, "new", (VALUE(*)(...)) v8_script_new, 1);
rb_define_method(V8__C__Script, "Run", (VALUE(*)(...)) v8_script_Run, 0);
// js object setup
rb_cV8_JSObject = rb_define_class_under(rb_mModule, "JSObject", rb_cObject);

View file

@ -1,18 +1,35 @@
#include "v8_cxt.h"
#include "ruby.h"
#include "stdio.h"
using namespace v8;
v8_cxt::v8_cxt() : v8_ref<Context>(Context::New()) {
printf("Allocate Native V8 Context\n");
Local<Context> VALUE_TO_CONTEXT(VALUE value) {
v8_ref* ref = 0;
Data_Get_Struct(value, struct v8_ref, ref);
return (Context *)(*ref->handle);
}
VALUE v8_cxt_allocate(VALUE clazz) {
printf("This is the big leagues chew!\n");
v8_cxt* cxt = new v8_cxt();
return Data_Wrap_Struct(clazz, v8_ref_mark , v8_ref_free, cxt);
v8_ref* ref = new v8_ref(Context::New());
return Data_Wrap_Struct(clazz, v8_ref_mark , v8_ref_free, ref);
}
VALUE v8_cxt_Global(VALUE self) {
Local<Context> cxt = VALUE_TO_CONTEXT(self);
cxt->Global();
return Qnil;
}
VALUE v8_cxt_open(VALUE self) {
HandleScope handles;
Local<Context> cxt = VALUE_TO_CONTEXT(self);
Context::Scope enter(cxt);
if (rb_block_given_p()) {
return rb_yield(self);
} else {
return Qnil;
}
}

View file

@ -5,11 +5,9 @@
#include "v8.h"
#include "v8_ref.h"
class v8_cxt : v8_ref<v8::Context> {
public:
v8_cxt();
~v8_cxt();
};
//Context::Global();
VALUE v8_cxt_Global(VALUE self);
VALUE v8_cxt_open(VALUE self);
//memory management
VALUE v8_cxt_allocate(VALUE clazz);

View file

@ -3,22 +3,22 @@
using namespace v8;
template <class T> v8_ref<T>::v8_ref(Handle<T> object) : handle(*object) {
v8_ref::v8_ref(Handle<void> object) : handle(Persistent<void>::New(object)) {
printf("Allocating v8 reference\n");
}
template <class T> v8_ref<T>::~v8_ref() {
v8_ref::~v8_ref() {
printf("Disposing of v8 reference\n");
if (!handle.IsEmpty()) {
handle.Dispose();
}
}
void v8_ref_mark(v8_ref_data* ref) {
void v8_ref_mark(v8_ref* ref) {
}
void v8_ref_free(v8_ref_data* ref) {
void v8_ref_free(v8_ref* ref) {
delete ref;
}

View file

@ -3,30 +3,21 @@
#include <v8.h>
//this is a non-template superclass so that the mark() and free() functions
//don't need to be templatized. Not sure if this is valid... are
// class destructors virtual?
class v8_ref_data {
};
//the v8_ref wraps a v8 handle so that ruby can hold a reference to it.
template <class T> class v8_ref : v8_ref_data {
public:
struct v8_ref {
//takes a handle object and adds a new persistent handle for
//the referenced object
v8_ref(v8::Handle<T> object);
~v8_ref();
private:
v8::Persistent<T> handle;
v8_ref(v8::Handle<void> object);
virtual ~v8_ref();
v8::Persistent<void> handle;
};
//memory management
void v8_ref_mark(v8_ref_data* ref);
void v8_ref_free(v8_ref_data* ref);
void v8_ref_mark(v8_ref* ref);
void v8_ref_free(v8_ref* ref);
#endif

33
v8_script.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "v8.h"
#include "v8_ref.h"
#include "v8_script.h"
#include "v8_context.h"
#include "ruby_data.h"
#include "v8_data.h"
using namespace v8;
Local<Script> VALUE_TO_SCRIPT(VALUE value) {
v8_ref* ref = 0;
Data_Get_Struct(value, struct v8_ref, ref);
return (Script *)(*ref->handle);
}
VALUE v8_script_new(VALUE self, VALUE source) {
v8_ref* src_ref = 0;
Data_Get_Struct(source, struct v8_ref, src_ref);
HandleScope handles;
Local<String> src((String *)*src_ref->handle);
v8_ref* script_ref = new v8_ref(Script::New(src));
return Data_Wrap_Struct(self, v8_ref_mark, v8_ref_free, script_ref);
}
VALUE v8_script_Run(VALUE self) {
HandleScope handles;
Local<Script> script = VALUE_TO_SCRIPT(self);
V8HandleSource<RubyDest, VALUE> toValue;
Local<Value> result = script->Run();
return toValue.push(result);
}

8
v8_script.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef _RUBY_V8_SCRIPT_
#define _RUBY_V8_SCRIPT_
#include "ruby.h"
VALUE v8_script_new(VALUE self, VALUE source);
VALUE v8_script_Run(VALUE self);
#endif

20
v8_str.cpp Normal file
View file

@ -0,0 +1,20 @@
#include "v8_str.h"
#include "v8.h"
#include "v8_ref.h"
using namespace v8;
VALUE v8_str_new(VALUE clazz, VALUE str) {
HandleScope handles;
v8_ref* ref = new v8_ref(String::New(RSTRING(str)->ptr));
return Data_Wrap_Struct(clazz, v8_ref_mark , v8_ref_free, ref);
}
VALUE v8_str_to_s(VALUE self){
HandleScope handles;
v8_ref* ref = 0;
Data_Get_Struct(self, struct v8_ref, ref);
Local<String> str = (String *)*ref->handle;
return rb_str_new2(*String::AsciiValue(str));
}

9
v8_str.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef _RUBY_V8_STR_
#define _RUBY_V8_STR_
#include "ruby.h"
VALUE v8_str_new(VALUE clazz, VALUE str);
VALUE v8_str_to_s(VALUE self);
#endif