From 2f3e4a0e3e957cfebbe1fdc5ef2254d5222c03d8 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sat, 22 May 2010 09:16:00 +0300 Subject: [PATCH] allow wrapping of v8::External objects from Ruby --- ext/v8/v8.cpp | 2 ++ ext/v8/v8_external.cpp | 31 +++++++++++++++++++++++++++++++ ext/v8/v8_external.h | 5 +++++ 3 files changed, 38 insertions(+) create mode 100644 ext/v8/v8_external.cpp create mode 100644 ext/v8/v8_external.h diff --git a/ext/v8/v8.cpp b/ext/v8/v8.cpp index faf49c1..05029ab 100644 --- a/ext/v8/v8.cpp +++ b/ext/v8/v8.cpp @@ -9,6 +9,7 @@ #include "v8_template.h" #include "v8_try_catch.h" #include "v8_callbacks.h" +#include "v8_external.h" #include @@ -29,5 +30,6 @@ extern "C" { rr_init_msg(); rr_init_v8_try_catch(); rr_init_v8_callbacks(); + rr_init_v8_external(); } } diff --git a/ext/v8/v8_external.cpp b/ext/v8/v8_external.cpp new file mode 100644 index 0000000..9b18ab9 --- /dev/null +++ b/ext/v8/v8_external.cpp @@ -0,0 +1,31 @@ +#include "v8_external.h" +#include "rr.h" +#include "v8_ref.h" +using namespace v8; + +namespace { + + VALUE _Value(VALUE self) { + HandleScope scope; + return (VALUE)V8_Ref_Get(self)->Value(); + } + VALUE Wrap(VALUE rbclass, VALUE value) { + HandleScope scope; + return rr_v8_ref_create(rbclass, External::Wrap((void *)value)); + } + VALUE Unwrap(VALUE self, VALUE value) { + if (rb_obj_is_kind_of(value, self)) { + return _Value(value); + } else { + rb_raise(rb_eArgError, "cannot unwrap %s. It is not a kind of %s", RSTRING_PTR(rb_class_name(rb_class_of(value))), RSTRING_PTR(rb_class_name(self))); + return Qnil; + } + } +} + +void rr_init_v8_external() { + VALUE ExternalClass = rr_define_class("External"); + rr_define_singleton_method(ExternalClass, "Wrap", Wrap, 1); + rr_define_singleton_method(ExternalClass, "Unwrap", Unwrap, 1); + rr_define_method(ExternalClass, "Value", _Value, 0); +} \ No newline at end of file diff --git a/ext/v8/v8_external.h b/ext/v8/v8_external.h new file mode 100644 index 0000000..c51c75e --- /dev/null +++ b/ext/v8/v8_external.h @@ -0,0 +1,5 @@ +#ifndef _RR_V8_EXTERNAL_ +#define _RR_V8_EXTERNAL_ + +void rr_init_v8_external(); +#endif \ No newline at end of file