From 0b4b40a24106cae58497b44fa8ee89c3a69849aa Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 7 Dec 2009 09:20:16 +0200 Subject: [PATCH] add beginnings of a generic v8 reference --- ruby_data.h | 8 ++++---- spike.sh | 4 ++++ v8.cpp | 6 ++++++ v8_cxt.cpp | 22 ++++++++++++++++++++++ v8_cxt.h | 16 ++++++++++++++++ v8_object.cpp | 1 - v8_ref.cpp | 21 +++++++++++++++++++++ v8_ref.h | 24 ++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 spike.sh create mode 100644 v8_cxt.cpp create mode 100644 v8_cxt.h create mode 100644 v8_ref.cpp create mode 100644 v8_ref.h diff --git a/ruby_data.h b/ruby_data.h index 3247aeb..5f5ac57 100644 --- a/ruby_data.h +++ b/ruby_data.h @@ -44,10 +44,10 @@ template class RubyValueSource { return dest.pushBool(true, name); case T_FALSE: return dest.pushBool(false, name); - case T_DATA: - if (rb_is_function(value)) { - return dest.pushCode(new Code) - } + // case T_DATA: + // if (rb_is_function(value)) { + // return dest.pushCode(new Code) + // } } return dest.pushUndefined(name); } diff --git a/spike.sh b/spike.sh new file mode 100644 index 0000000..f8f3cdb --- /dev/null +++ b/spike.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +make clean all +ruby -e "require 'v8'; V8::N::Context.new" \ No newline at end of file diff --git a/v8.cpp b/v8.cpp index 569553f..2b25571 100644 --- a/v8.cpp +++ b/v8.cpp @@ -2,6 +2,7 @@ #include "generic_data.h" #include "v8_data.h" #include "v8_context.h" +#include "v8_cxt.h" #include "v8_standalone.h" #include @@ -32,6 +33,11 @@ extern "C" { rb_define_method(rb_cV8, "eval", (VALUE(*)(...)) v8_context_eval, 1); 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); + // js object setup rb_cV8_JSObject = rb_define_class_under(rb_mModule, "JSObject", rb_cObject); rb_define_alloc_func(rb_cV8_JSObject, v8_object_allocate); diff --git a/v8_cxt.cpp b/v8_cxt.cpp new file mode 100644 index 0000000..ad67cac --- /dev/null +++ b/v8_cxt.cpp @@ -0,0 +1,22 @@ + +#include "v8_cxt.h" +#include "ruby.h" +#include "stdio.h" + +using namespace v8; + +v8_cxt::v8_cxt() { + // v8_cxt::v8_cxt() { + handle = Context::New(); + printf("Allocate Native V8 Context\n"); + } + +} + +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, 0, 0, cxt); +} + + diff --git a/v8_cxt.h b/v8_cxt.h new file mode 100644 index 0000000..3cbd8f9 --- /dev/null +++ b/v8_cxt.h @@ -0,0 +1,16 @@ +#ifndef _RUBY_V8_CXT_ +#define _RUBY_V8_CXT_ + +#include "ruby.h" +#include "v8.h" +#include "v8_ref.h" + +class v8_cxt : v8_ref { +public: + v8_cxt(); + ~v8_cxt(); +}; + +//memory management +VALUE v8_cxt_allocate(VALUE clazz); +#endif \ No newline at end of file diff --git a/v8_object.cpp b/v8_object.cpp index 1c8c832..3752e1f 100644 --- a/v8_object.cpp +++ b/v8_object.cpp @@ -8,7 +8,6 @@ VALUE rb_cV8_JSObject; - using namespace v8; v8_object::v8_object(VALUE clazz) { diff --git a/v8_ref.cpp b/v8_ref.cpp new file mode 100644 index 0000000..77f1745 --- /dev/null +++ b/v8_ref.cpp @@ -0,0 +1,21 @@ +#include +#include "stdio.h" +using namespace v8; + +template v8_ref::v8_ref(Handle object) : handle(*object) { + printf("Allocated v8 reference\n"); +} + +template v8_ref::~v8_ref() { + printf("Disposing of v8 reference\n"); + handle.Dispose(); +} + +void v8_ref_mark(v8_ref_data* ref) { + +} + +void v8_ref_free(v8_ref_data* ref) { + delete ref; +} + diff --git a/v8_ref.h b/v8_ref.h new file mode 100644 index 0000000..895889d --- /dev/null +++ b/v8_ref.h @@ -0,0 +1,24 @@ +#ifndef _RUBY_V8_REF_ +#define _RUBY_V8_REF_ + +#include + +class v8_ref_data { + +}; + +template class v8_ref : v8_ref_data { +public: + v8_ref(v8::Handle object); + ~v8_ref(); +private: + v8::Persistent handle; +}; + + +//memory management +void v8_ref_mark(v8_ref_data* ref); +void v8_ref_free(v8_ref_data* ref); + + +#endif \ No newline at end of file