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

Can now embed ruby objects and call their public methods from javascript.

This commit is contained in:
Charles Lowell 2010-01-10 00:13:03 +02:00
parent e13f662e63
commit a253485712
3 changed files with 32 additions and 19 deletions

View file

@ -15,6 +15,7 @@
* \tparam DEST destination type for the converted data * \tparam DEST destination type for the converted data
* \tparam RET is the return type of T's methods * \tparam RET is the return type of T's methods
*/ */
template<class DEST, class RET> class RubyValueSource { template<class DEST, class RET> class RubyValueSource {
/** /**
@ -27,22 +28,28 @@ template<class DEST, class RET> class RubyValueSource {
~RubyValueSource() {} ~RubyValueSource() {}
RET operator() (VALUE& value) { bool operator() (VALUE& value, RET& result) {
switch (TYPE(value)) { switch (TYPE(value)) {
case T_FIXNUM: case T_FIXNUM:
return dest.pushInt(FIX2INT(value)); result = dest.pushInt(FIX2INT(value));
return true;
case T_FLOAT: case T_FLOAT:
return dest.pushDouble(NUM2DBL(value)); result = dest.pushDouble(NUM2DBL(value));
return true;
case T_STRING: case T_STRING:
return convertString(value); result = convertString(value);
return true;
case T_NIL: case T_NIL:
return dest.pushNull(); result = dest.pushNull();
return true;
case T_TRUE: case T_TRUE:
return dest.pushBool(true); result = dest.pushBool(true);
return true;
case T_FALSE: case T_FALSE:
return dest.pushBool(false); result = dest.pushBool(false);
return true;
} }
return dest.pushUndefined(); return false;
} }
private: private:

View file

@ -24,22 +24,29 @@ VALUE V82RB(Handle<Value>& value) {
return Qnil; return Qnil;
} }
Local<Value> RB2V8(VALUE value) { Local<Value> RB2V8(VALUE value) {
VALUE valueClass = rb_class_of(value); VALUE valueClass = rb_class_of(value);
if(valueClass == rb_cProc || valueClass == rb_cMethod) { if(valueClass == rb_cProc || valueClass == rb_cMethod) {
Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)value)); Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)value));
return t->GetFunction(); return t->GetFunction();
} }
convert_rb_to_v8_t convert; convert_rb_to_v8_t convert;
return convert(value); Local<Value> result;
} if (convert(value, result)) {
return result;
std::string RB2String(VALUE value) { }
convert_rb_to_string_t convert;
return convert(value); Local<ObjectTemplate> tmpl = ObjectTemplate::New();
VALUE methods = rb_funcall(value, rb_intern("public_methods"), 1, Qfalse);
int len = RARRAY(methods)->len;
for (int i = 0; i < len; i++) {
VALUE method_name = RARRAY(methods)->ptr[i];
VALUE method = rb_funcall(value, rb_intern("method"), 1, method_name);
Local<String> keystr = (String *)*RB2V8(method_name);
tmpl->Set(keystr, RB2V8(method));
}
return tmpl->NewInstance();
} }
std::string V82String(Handle<Value>& value) { std::string V82String(Handle<Value>& value) {

View file

@ -4,11 +4,10 @@ using namespace v8;
v8_ref::v8_ref(Handle<void> object, VALUE ref) : handle(Persistent<void>::New(object)), references(ref) { v8_ref::v8_ref(Handle<void> object, VALUE ref) : handle(Persistent<void>::New(object)), references(ref) {
//printf("Allocating v8 reference\n");
} }
v8_ref::~v8_ref() { v8_ref::~v8_ref() {
//printf("Disposing v8 reference\n");
handle.Dispose(); handle.Dispose();
} }