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

74 lines
2 KiB
C++
Raw Normal View History

2009-12-18 02:48:06 -05:00
#include "v8_obj.h"
#include "v8_ref.h"
2010-05-13 17:53:09 -04:00
#include "v8_value.h"
2009-12-18 02:48:06 -05:00
#include "converters.h"
using namespace v8;
2009-12-25 21:59:46 -05:00
#include <cstdio>
2010-05-12 18:30:53 -04:00
VALUE rr_cV8_C_Object;
2009-12-18 02:48:06 -05:00
void rr_init_obj() {
2010-05-12 18:30:53 -04:00
rr_cV8_C_Object = rr_define_class("Object", rr_cV8_C_Value);
2010-05-13 17:53:09 -04:00
rr_define_singleton_method(rr_cV8_C_Object, "new", v8_Object_New, 0);
rr_define_method(rr_cV8_C_Object, "Get", v8_Object_Get, 1);
rr_define_method(rr_cV8_C_Object, "Set", v8_Object_Set, 2);
rr_define_method(rr_cV8_C_Object, "GetPropertyNames", v8_Object_GetPropertyNames, 0);
rr_define_method(rr_cV8_C_Object, "ToString", v8_Object_ToString, 0);
rr_define_method(rr_cV8_C_Object, "context", v8_Object_context, 0);
}
2010-05-13 18:14:54 -04:00
VALUE rr_reflect_v8_object(Handle<Value> value) {
Local<Object> object(Object::Cast(*value));
return V8_Ref_Create(rr_cV8_C_Object, object);
2010-05-13 18:14:54 -04:00
}
2010-01-10 13:56:10 -05:00
namespace {
Local<Object> unwrap(VALUE robj) {
return V8_Ref_Get<Object>(robj);
}
}
2009-12-18 02:48:06 -05:00
VALUE v8_Object_New(VALUE clazz) {
HandleScope handles;
return V8_Ref_Create(clazz, Object::New());
}
VALUE v8_Object_Get(VALUE self, VALUE key) {
HandleScope handles;
2010-01-10 13:56:10 -05:00
Local<Object> obj = unwrap(self);
VALUE keystr = rb_str_to_str(key);
Local<Value> value = obj->Get(RB2V8(keystr));
return V82RB(value);
2009-12-18 02:48:06 -05:00
}
VALUE v8_Object_Set(VALUE self, VALUE key, VALUE value) {
HandleScope handles;
2010-01-10 13:56:10 -05:00
Local<Object> obj = unwrap(self);
2009-12-18 02:48:06 -05:00
VALUE keystr = rb_funcall(key, rb_intern("to_s"), 0);
2009-12-25 21:59:46 -05:00
obj->Set(RB2V8(keystr), RB2V8(value));
2009-12-18 02:48:06 -05:00
return Qnil;
}
2010-01-10 13:56:10 -05:00
VALUE v8_Object_GetPropertyNames(VALUE self) {
HandleScope handles;
Local<Object> object = unwrap(self);
Local<Value> names = object->GetPropertyNames();
return V82RB(names);
}
VALUE v8_Object_context(VALUE self) {
HandleScope handles;
Local<Object> object = unwrap(self);
Local<Value> cxt = object->GetHiddenValue(String::New("TheRubyRacer::Context"));
return cxt.IsEmpty() ? Qnil : (VALUE)External::Unwrap(cxt);
}
VALUE v8_Object_ToString(VALUE self) {
HandleScope handles;
Local<Object> object = unwrap(self);
Local<Value> string = object->ToString();
return V82RB(string);
}