Skip to content

Commit

Permalink
dont create a second v8 instance for one GObject
Browse files Browse the repository at this point in the history
  • Loading branch information
swick committed Sep 8, 2011
1 parent 9b4dba0 commit 2a299b4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
4 changes: 2 additions & 2 deletions gir.js
Expand Up @@ -120,11 +120,11 @@ console.log(gtk.Button.__signals__);
button.__watch_signal__("clicked");

win.on("destroy", function() {
console.log("destroyed", arguments);
console.log("destroyed", arguments[0] instanceof gtk.Window);
gtk.mainQuit();
});
button.on("clicked", function() {
console.log("click :)", arguments);
console.log("click :)", arguments[0] instanceof gtk.Button, arguments[0] == button);
});

gtk.main();
79 changes: 49 additions & 30 deletions src/interfaces/object.cc
Expand Up @@ -13,13 +13,10 @@ namespace gir {
void empty_func(void) {};

std::vector<ObjectFunctionTemplate> GIRObject::templates;
std::vector<InstanceData> GIRObject::instances;
static Persistent<String> emit_symbol;

GIRObject::GIRObject(GIObjectInfo *info_) {
constructor(info_);
}

void GIRObject::constructor(GIObjectInfo *info_) {
info = info_;
GType t = g_registered_type_info_get_g_type(info);

Expand All @@ -42,19 +39,23 @@ Handle<Value> GIRObject::New(GObject *obj_, GIObjectInfo *info_) {
// very interesting: gtk.Winodw with a child. child.get_parent_window() returns a GIObjetInfo with name GdkWindow (Namespace GDK!)
// printf("type is %s\n", g_type_name(g_registered_type_info_get_g_type(info_)));

const char *name = g_base_info_get_name(info_);
Handle<Value> res = GetInstance(obj_);
if(res != Null()) {
return res;
}

Handle<Value> arg = Boolean::New(false);

std::vector<ObjectFunctionTemplate>::iterator it;

for(it = templates.begin(); it != templates.end(); ++it) {

if(g_base_info_equal(info_, it->info)) {
Handle<Value> res = it->function->GetFunction()->NewInstance(1, &arg);
res = it->function->GetFunction()->NewInstance(1, &arg);
if(!res.IsEmpty()) {
GIRObject *e = ObjectWrap::Unwrap<GIRObject>(res->ToObject());
e->info = info_;
e->obj = obj_;
e->abstract = false;

return res;
}
Expand All @@ -64,46 +65,41 @@ Handle<Value> GIRObject::New(GObject *obj_, GIObjectInfo *info_) {
return Null();
}

Handle<Value> GIRObject::New(GIPropertyInfo *prop) {
// FIXME: the whole function sucks...
GIObjectInfo *info_ = g_type_info_get_interface(g_property_info_get_type(prop));
Handle<Value> arg = Boolean::New(false);
Handle<Value> GIRObject::New(GObject *obj_, GType t) {
if(obj_ == NULL || !G_IS_OBJECT(obj_)) {
return Null();
}

Handle<Value> res = GetInstance(obj_);
if(res != Null()) {
return res;
}

Handle<Value> arg = Boolean::New(false);
std::vector<ObjectFunctionTemplate>::iterator it;

for(it = templates.begin(); it != templates.end(); ++it) {
if(g_base_info_equal(info_, it->info)) {
Handle<Value> res = it->function->GetFunction()->NewInstance(1, &arg);
if(t == it->type) {
res = it->function->GetFunction()->NewInstance(1, &arg);
if(!res.IsEmpty()) {
GIRObject *e = ObjectWrap::Unwrap<GIRObject>(res->ToObject());
// FIXME: WHY DOES IT NOT WORK?!
//e->constructor(info);
e->info = info_;
GType t = g_registered_type_info_get_g_type(info_);

e->abstract = g_object_info_get_abstract(info_);
if(e->abstract) {
e->obj = NULL;
}
else {
// gobject va_list, to allow construction parameters
e->obj = G_OBJECT(g_object_new(t, NULL));
}
e->info = it->info;
e->obj = obj_;
e->abstract = false;
return res;
}
return Null();
}
}
}

Handle<Value> GIRObject::New(GType t) {
return Null();
}

Handle<Value> GIRObject::New(const Arguments &args) {

if(args.Length() == 1 && args[0]->IsBoolean() && !args[0]->IsTrue()) {
GIRObject *obj = new GIRObject();
obj->Wrap(args.This());
PushInstance(obj, args.This());

return args.This();
}

Expand All @@ -123,6 +119,9 @@ Handle<Value> GIRObject::New(const Arguments &args) {

GIRObject *obj = new GIRObject(info);
obj->Wrap(args.This());
PushInstance(obj, args.This());

return args.This();
}

void GIRObject::Prepare(Handle<Object> target, GIObjectInfo *info) {
Expand All @@ -141,6 +140,7 @@ void GIRObject::Prepare(Handle<Object> target, GIObjectInfo *info) {
oft.type_name = name;
oft.info = info;
oft.function = t;
oft.type = g_registered_type_info_get_g_type(info);

templates.push_back(oft);

Expand Down Expand Up @@ -212,6 +212,25 @@ Handle<Value> GIRObject::Emit(Handle<Value> argv[], int length) {
return emit->Call(handle_, length, argv);
}

void GIRObject::PushInstance(GIRObject *obj, Handle<Value> value) {
Persistent<Object> p_value = Persistent<Object>::New(value->ToObject());
obj->MakeWeak();

InstanceData data;
data.obj = obj;
data.instance = p_value;
instances.push_back(data);
}

Handle<Value> GIRObject::GetInstance(GObject *obj) {
std::vector<InstanceData>::iterator it;
for(it = instances.begin(); it != instances.end(); it++) {
if(it->obj && it->obj->obj && it->obj->obj == obj) {
return it->instance;
}
}
return Null();
}

void GIRObject::SignalCallback(GClosure *closure,
GValue *return_value,
Expand Down
17 changes: 13 additions & 4 deletions src/interfaces/object.h
Expand Up @@ -16,30 +16,36 @@ struct ObjectFunctionTemplate {
char *type_name;
GIObjectInfo *info;
v8::Persistent<v8::FunctionTemplate> function;
GType type;
};

struct MarshalData {
GIRObject *that;
char *event_name;
};

struct InstanceData {
v8::Persistent<v8::Value> instance;
GIRObject *obj;
};


class GIRObject : public node::ObjectWrap {
public:
GIRObject() {};
GIRObject(GIObjectInfo *info_);

void constructor(GIObjectInfo *info_);

GObject *obj;
bool abstract;
GIBaseInfo *info;

static std::vector<InstanceData> instances;
static std::vector<ObjectFunctionTemplate> templates;

static v8::Handle<v8::Value> New(GObject *obj, GIObjectInfo *info);
static v8::Handle<v8::Value> New(GIPropertyInfo *prop_);
static v8::Handle<v8::Value> New(GType t);
static v8::Handle<v8::Value> New(GObject *obj, GType t);
static v8::Handle<v8::Value> New(const v8::Arguments &args);

static void Prepare(v8::Handle<v8::Object> target, GIObjectInfo *info);
static void SetPrototypeMethods(v8::Handle<v8::FunctionTemplate> t, char *name);

Expand All @@ -53,6 +59,9 @@ class GIRObject : public node::ObjectWrap {
static v8::Handle<v8::Value> WatchSignal(const v8::Arguments &args);
static v8::Handle<v8::Value> CallVFunc(const v8::Arguments &args);

static void PushInstance(GIRObject *obj, v8::Handle<v8::Value>);
static v8::Handle<v8::Value> GetInstance(GObject *obj);

static void SignalFinalize(gpointer data, GClosure *c);
static void SignalCallback(GClosure *closure,
GValue *return_value,
Expand Down
3 changes: 1 addition & 2 deletions src/values.cc
Expand Up @@ -8,7 +8,6 @@ using namespace v8;
namespace gir {

Handle<Value> GIRValue::FromGValue(GValue *v) {
printf("from gvalue type %s\n", g_type_name(G_VALUE_TYPE(v)));
GType type = G_VALUE_TYPE(v);
Handle<Value> value = Undefined();

Expand Down Expand Up @@ -72,7 +71,7 @@ Handle<Value> GIRValue::FromGValue(GValue *v) {

}
else if(g_type_is_a(type, G_TYPE_OBJECT)) {
//value = GIRObject::New(info);
value = GIRObject::New(G_OBJECT(g_value_get_object(v)), type);
}
return value;
}
Expand Down

0 comments on commit 2a299b4

Please sign in to comment.