mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
allow wrapping of v8::External objects from Ruby
This commit is contained in:
parent
e6c6f4e04b
commit
2f3e4a0e3e
3 changed files with 38 additions and 0 deletions
|
@ -9,6 +9,7 @@
|
|||
#include "v8_template.h"
|
||||
#include "v8_try_catch.h"
|
||||
#include "v8_callbacks.h"
|
||||
#include "v8_external.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -29,5 +30,6 @@ extern "C" {
|
|||
rr_init_msg();
|
||||
rr_init_v8_try_catch();
|
||||
rr_init_v8_callbacks();
|
||||
rr_init_v8_external();
|
||||
}
|
||||
}
|
||||
|
|
31
ext/v8/v8_external.cpp
Normal file
31
ext/v8/v8_external.cpp
Normal file
|
@ -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<External>(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);
|
||||
}
|
5
ext/v8/v8_external.h
Normal file
5
ext/v8/v8_external.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#ifndef _RR_V8_EXTERNAL_
|
||||
#define _RR_V8_EXTERNAL_
|
||||
|
||||
void rr_init_v8_external();
|
||||
#endif
|
Loading…
Reference in a new issue