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:
parent
e13f662e63
commit
a253485712
3 changed files with 32 additions and 19 deletions
|
@ -15,6 +15,7 @@
|
|||
* \tparam DEST destination type for the converted data
|
||||
* \tparam RET is the return type of T's methods
|
||||
*/
|
||||
|
||||
template<class DEST, class RET> class RubyValueSource {
|
||||
|
||||
/**
|
||||
|
@ -27,22 +28,28 @@ template<class DEST, class RET> class RubyValueSource {
|
|||
~RubyValueSource() {}
|
||||
|
||||
|
||||
RET operator() (VALUE& value) {
|
||||
bool operator() (VALUE& value, RET& result) {
|
||||
switch (TYPE(value)) {
|
||||
case T_FIXNUM:
|
||||
return dest.pushInt(FIX2INT(value));
|
||||
result = dest.pushInt(FIX2INT(value));
|
||||
return true;
|
||||
case T_FLOAT:
|
||||
return dest.pushDouble(NUM2DBL(value));
|
||||
result = dest.pushDouble(NUM2DBL(value));
|
||||
return true;
|
||||
case T_STRING:
|
||||
return convertString(value);
|
||||
result = convertString(value);
|
||||
return true;
|
||||
case T_NIL:
|
||||
return dest.pushNull();
|
||||
result = dest.pushNull();
|
||||
return true;
|
||||
case T_TRUE:
|
||||
return dest.pushBool(true);
|
||||
result = dest.pushBool(true);
|
||||
return true;
|
||||
case T_FALSE:
|
||||
return dest.pushBool(false);
|
||||
result = dest.pushBool(false);
|
||||
return true;
|
||||
}
|
||||
return dest.pushUndefined();
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
@ -25,21 +25,28 @@ VALUE V82RB(Handle<Value>& value) {
|
|||
}
|
||||
|
||||
Local<Value> RB2V8(VALUE value) {
|
||||
|
||||
VALUE valueClass = rb_class_of(value);
|
||||
|
||||
if(valueClass == rb_cProc || valueClass == rb_cMethod) {
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(RacerRubyInvocationCallback, External::Wrap((void *)value));
|
||||
return t->GetFunction();
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -4,11 +4,10 @@ using namespace v8;
|
|||
|
||||
|
||||
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() {
|
||||
//printf("Disposing v8 reference\n");
|
||||
handle.Dispose();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue