diff --git a/examples/browser.js b/examples/browser.js index dd39413..b9ffb83 100644 --- a/examples/browser.js +++ b/examples/browser.js @@ -1,22 +1,33 @@ -var gir = require("../gir"); -gir.init(); -var gtk = gir.load("Gtk", "3.0"); - -var WebKit = require("./webkit"); +var gir = require("../gir"), + gtk = gir.load("Gtk"), + WebKit = gir.load("WebKit"); gtk.init(0); -var win = new gtk.Window(); +var win = new gtk.Window({ + name: "main_window", + title: "test" +}); var sw = new gtk.ScrolledWindow(); -win.__call__("add", sw); +win.add(sw); var view = new WebKit.WebView(); -view.__call__("load_uri", "http://www.google.com/"); -sw.__call__("add", view); +view.loadUri("http://www.google.com/"); +sw.add(view); + +win.setSizeRequest(640, 480); +win.showAll(); + +win.title = "test2"; -win.__call__("set_size_request", 640, 480); -win.__call__("show_all"); +// change the ee that we automatically call __watch_signal__ +// also make sure that we call unwatch or sth like that when there is no cb left +sw.__watch_signal__("destroy"); +sw.on("destroy", function() { + console.log("destroy"); + gtk.mainQuit(); +}); gtk.main(); diff --git a/examples/gtk.js b/examples/gtk.js index fdb7033..95eb584 100644 --- a/examples/gtk.js +++ b/examples/gtk.js @@ -1,103 +1,35 @@ var gir = require("../gir"), - EventEmitter = require("events").EventEmitter; + gtk = gir.load("Gtk"); -gir.init(); +gtk.init(0); -var gtk = exports.gtk = gir.load("Gtk", "2.0"); +var win = new gtk.Window({type: gtk.WindowType.toplevel, title:"trololol"}); +var button = new gtk.Button(); -extend(true, gtk.Object.prototype, EventEmitter.prototype); +win.__call__("set_border_width", 10); -/** - * Adopted from jquery's extend method. Under the terms of MIT License. - * - * http://code.jquery.com/jquery-1.4.2.js - * - * Modified by Brian White to use Array.isArray instead of the custom isArray - * method - */ -function extend() { - // copy reference to target object - var target = arguments[0] || {}, - i = 1, - length = arguments.length, - deep = false, - options, - name, - src, - copy; +button.__call__("set_label", "hallo, welt!"); - // Handle a deep copy situation - if (typeof target === "boolean") { - deep = target; - target = arguments[1] || {}; - // skip the boolean and the target - i = 2; - } +win.__call__("add", button); +win.__call__("show_all"); - // Handle case when target is a string or something (possible in deep copy) - if (typeof target !== "object" && !typeof target === 'function') - target = {}; - var isPlainObject = function(obj) { - // Must be an Object. - // Because of IE, we also have to check the presence of the constructor - // property. - // Make sure that DOM nodes and window objects don't pass through, as well - if (!obj || toString.call(obj) !== "[object Object]" || obj.nodeType - || obj.setInterval) - return false; - - var has_own_constructor = hasOwnProperty.call(obj, "constructor"); - var has_is_prop_of_method = hasOwnProperty.call(obj.constructor.prototype, - "isPrototypeOf"); - // Not own constructor property must be Object - if (obj.constructor && !has_own_constructor && !has_is_prop_of_method) - return false; - - // Own properties are enumerated firstly, so to speed up, - // if last one is own, then all properties are own. +var w2 = button.__call__("get_parent_window"); +console.log(w2); - var last_key; - for (key in obj) - last_key = key; - - return typeof last_key === "undefined" || hasOwnProperty.call(obj, last_key); - }; +win.__watch_signal__("destroy"); +button.__watch_signal__("clicked"); +win.on("destroy", function() { + console.log("destroyed", arguments[0] instanceof gtk.Window); + gtk.mainQuit(); +}); +button.on("clicked", function() { + console.log("click :)", arguments[0] instanceof gtk.Button, arguments[0] == button); +}); - for (; i < length; i++) { - // Only deal with non-null/undefined values - if ((options = arguments[i]) !== null) { - // Extend the base object - for (name in options) { - src = target[name]; - copy = options[name]; +console.log(win.__call__("set_property", "name", "test")); +console.log(win.__get_property__("name")); - // Prevent never-ending loop - if (target === copy) - continue; - // Recurse if we're merging object literal values or arrays - if (deep && copy && (isPlainObject(copy) || Array.isArray(copy))) { - var clone = src && (isPlainObject(src) || Array.isArray(src) - ? src : (Array.isArray(copy) ? [] : {})); - - // Never move original objects, clone them - target[name] = extend(deep, clone, copy); - - // Don't bring in undefined values - } else if (typeof copy !== "undefined") - target[name] = copy; - } - } - } - - // Return the modified object - return target; -}; - -/* -now, make the api nicer :) -*/ - -module.exports = gtk; +gtk.main(); diff --git a/examples/gtk_test.js b/examples/gtk_test.js deleted file mode 100644 index 4970c7f..0000000 --- a/examples/gtk_test.js +++ /dev/null @@ -1,34 +0,0 @@ -var gtk = require("./gtk"); - -gtk.init(0); - -var win = new gtk.Window({type: gtk.WindowType.toplevel, title:"trololol"}); -var button = new gtk.Button(); - -win.__call__("set_border_width", 10); - -button.__call__("set_label", "hallo, welt!"); - -win.__call__("add", button); -win.__call__("show_all"); - - -var w2 = button.__call__("get_parent_window"); -console.log(w2); - -win.__watch_signal__("destroy"); -button.__watch_signal__("clicked"); - -win.on("destroy", function() { - console.log("destroyed", arguments[0] instanceof gtk.Window); - gtk.mainQuit(); -}); -button.on("clicked", function() { - console.log("click :)", arguments[0] instanceof gtk.Button, arguments[0] == button); -}); - -console.log(win.__call__("set_property", "name", "test")); -console.log(win.__get_property__("name")); - - -gtk.main(); diff --git a/examples/libxml2.js b/examples/libxml2.js index 3ad127f..58b8dcf 100644 --- a/examples/libxml2.js +++ b/examples/libxml2.js @@ -1,105 +1,4 @@ -var gir = require("../gir"), - EventEmitter = require("events").EventEmitter; - -gir.init(); +var gir = require("../gir"); var libxml2 = exports.gtk = gir.load("libxml2"); console.log(libxml2); - -//extend(true, libxml2.Object.prototype, EventEmitter.prototype); - -/** - * Adopted from jquery's extend method. Under the terms of MIT License. - * - * http://code.jquery.com/jquery-1.4.2.js - * - * Modified by Brian White to use Array.isArray instead of the custom isArray - * method - */ - /* -function extend() { - // copy reference to target object - var target = arguments[0] || {}, - i = 1, - length = arguments.length, - deep = false, - options, - name, - src, - copy; - - // Handle a deep copy situation - if (typeof target === "boolean") { - deep = target; - target = arguments[1] || {}; - // skip the boolean and the target - i = 2; - } - - // Handle case when target is a string or something (possible in deep copy) - if (typeof target !== "object" && !typeof target === 'function') - target = {}; - - var isPlainObject = function(obj) { - // Must be an Object. - // Because of IE, we also have to check the presence of the constructor - // property. - // Make sure that DOM nodes and window objects don't pass through, as well - if (!obj || toString.call(obj) !== "[object Object]" || obj.nodeType - || obj.setInterval) - return false; - - var has_own_constructor = hasOwnProperty.call(obj, "constructor"); - var has_is_prop_of_method = hasOwnProperty.call(obj.constructor.prototype, - "isPrototypeOf"); - // Not own constructor property must be Object - if (obj.constructor && !has_own_constructor && !has_is_prop_of_method) - return false; - - // Own properties are enumerated firstly, so to speed up, - // if last one is own, then all properties are own. - - var last_key; - for (key in obj) - last_key = key; - - return typeof last_key === "undefined" || hasOwnProperty.call(obj, last_key); - }; - - - for (; i < length; i++) { - // Only deal with non-null/undefined values - if ((options = arguments[i]) !== null) { - // Extend the base object - for (name in options) { - src = target[name]; - copy = options[name]; - - // Prevent never-ending loop - if (target === copy) - continue; - - // Recurse if we're merging object literal values or arrays - if (deep && copy && (isPlainObject(copy) || Array.isArray(copy))) { - var clone = src && (isPlainObject(src) || Array.isArray(src) - ? src : (Array.isArray(copy) ? [] : {})); - - // Never move original objects, clone them - target[name] = extend(deep, clone, copy); - - // Don't bring in undefined values - } else if (typeof copy !== "undefined") - target[name] = copy; - } - } - } - - // Return the modified object - return target; -}; -*/ -/* -now, make the api nicer :) -*/ - -module.exports = libxml2; diff --git a/examples/midgard.js b/examples/midgard.js index f509d06..8762d58 100644 --- a/examples/midgard.js +++ b/examples/midgard.js @@ -1,6 +1,4 @@ -var Midgard, gir; -gir = require("../gir"); -gir.init(); -Midgard = gir.load("Midgard"); +var gir = require("../gir"), + Midgard = gir.load("Midgard"); console.log(Midgard); diff --git a/examples/multiple_test.js b/examples/multiple_test.js deleted file mode 100644 index 7fc6a81..0000000 --- a/examples/multiple_test.js +++ /dev/null @@ -1,6 +0,0 @@ -var gtk = require("./gtk"), - notify = require("./notify"); - - -notify.init("a"); -gtk.init(0, null); diff --git a/examples/namespaces.js b/examples/namespaces.js new file mode 100644 index 0000000..a8fd05f --- /dev/null +++ b/examples/namespaces.js @@ -0,0 +1,13 @@ +var gir = require("../gir"); + +console.log(gir.searchPath()); + +gir.load("Gtk"); + +console.log(gir.loadedNamespaces()); + +console.log(gir.getDependencies("Gtk")); + +console.log(gir.getVersions("Gtk")); + +console.log(gir.isRegistered("Gtk", "3.0")); diff --git a/examples/notify.js b/examples/notify.js index 89573bc..2761c9f 100644 --- a/examples/notify.js +++ b/examples/notify.js @@ -1,103 +1,6 @@ var gir = require("../gir"), - EventEmitter = require("events").EventEmitter; + notify = gir.load("Notify"); -gir.init(); +console.log(notify.init("ich")); -var notify = exports.notify = gir.load("Notify"); - -extend(true, notify.Notification.prototype, EventEmitter.prototype); - -/** - * Adopted from jquery's extend method. Under the terms of MIT License. - * - * http://code.jquery.com/jquery-1.4.2.js - * - * Modified by Brian White to use Array.isArray instead of the custom isArray - * method - */ -function extend() { - // copy reference to target object - var target = arguments[0] || {}, - i = 1, - length = arguments.length, - deep = false, - options, - name, - src, - copy; - - // Handle a deep copy situation - if (typeof target === "boolean") { - deep = target; - target = arguments[1] || {}; - // skip the boolean and the target - i = 2; - } - - // Handle case when target is a string or something (possible in deep copy) - if (typeof target !== "object" && !typeof target === 'function') - target = {}; - - var isPlainObject = function(obj) { - // Must be an Object. - // Because of IE, we also have to check the presence of the constructor - // property. - // Make sure that DOM nodes and window objects don't pass through, as well - if (!obj || toString.call(obj) !== "[object Object]" || obj.nodeType - || obj.setInterval) - return false; - - var has_own_constructor = hasOwnProperty.call(obj, "constructor"); - var has_is_prop_of_method = hasOwnProperty.call(obj.constructor.prototype, - "isPrototypeOf"); - // Not own constructor property must be Object - if (obj.constructor && !has_own_constructor && !has_is_prop_of_method) - return false; - - // Own properties are enumerated firstly, so to speed up, - // if last one is own, then all properties are own. - - var last_key; - for (key in obj) - last_key = key; - - return typeof last_key === "undefined" || hasOwnProperty.call(obj, last_key); - }; - - - for (; i < length; i++) { - // Only deal with non-null/undefined values - if ((options = arguments[i]) !== null) { - // Extend the base object - for (name in options) { - src = target[name]; - copy = options[name]; - - // Prevent never-ending loop - if (target === copy) - continue; - - // Recurse if we're merging object literal values or arrays - if (deep && copy && (isPlainObject(copy) || Array.isArray(copy))) { - var clone = src && (isPlainObject(src) || Array.isArray(src) - ? src : (Array.isArray(copy) ? [] : {})); - - // Never move original objects, clone them - target[name] = extend(deep, clone, copy); - - // Don't bring in undefined values - } else if (typeof copy !== "undefined") - target[name] = copy; - } - } - } - - // Return the modified object - return target; -}; - -/* -now, make the api nicer :) -*/ - -module.exports = notify; +var n = new notify.Notification(); diff --git a/examples/notify_test.js b/examples/notify_test.js deleted file mode 100644 index b61ca35..0000000 --- a/examples/notify_test.js +++ /dev/null @@ -1,12 +0,0 @@ -var notify = require("./notify"); - -console.log(notify.init("ich")); - -var n = new notify.Notification(); -var created = n.__call__("new", "a", "a", "a", "a"); - -for(var k in n) { - console.log(k); -} - -console.log(notify.Notification.__methods__); diff --git a/examples/webkit.js b/examples/webkit.js deleted file mode 100644 index 0efe0f0..0000000 --- a/examples/webkit.js +++ /dev/null @@ -1,8 +0,0 @@ -var gir = require("../gir"), - EventEmitter = require("events").EventEmitter; - -gir.init(); - -var webkit = exports.webkit = gir.load("WebKit"); - -module.exports = webkit; diff --git a/gir.js b/gir.js index 137982b..6b9c9f0 100644 --- a/gir.js +++ b/gir.js @@ -1 +1,152 @@ -var gir = module.exports = require('./build/Release/girepository.node'); +var gir = require('./build/Release/girepository.node'), + EventEmitter = require("events").EventEmitter; + +gir.init(); + +var oldLoad = gir.load; +gir.load = function(namespace, version) { + if(!version) { + var namespaces = gir.loadedNamespaces(); + for(var i=0; i Func::Call(GObject *obj, GIFunctionInfo *info, const Arguments &ar if(g_function_info_get_flags(info) == GI_FUNCTION_IS_CONSTRUCTOR) { // rly not sure about this - printf("constructor! returns %s\n", g_type_tag_to_string( g_type_info_get_tag( g_callable_info_get_return_type(info) ) )); + //printf("constructor! returns %s\n", g_type_tag_to_string( g_type_info_get_tag( g_callable_info_get_return_type(info) ) )); obj = NULL; } @@ -36,10 +36,10 @@ Handle Func::Call(GObject *obj, GIFunctionInfo *info, const Arguments &ar out_args_c++; in_args_c++; } - printf("%s %s\n", g_type_tag_to_string(g_type_info_get_tag(g_arg_info_get_type(arg))), g_base_info_get_name(arg)); + //printf("%s %s\n", g_type_tag_to_string(g_type_info_get_tag(g_arg_info_get_type(arg))), g_base_info_get_name(arg)); g_base_info_unref(arg); } - printf("in_args_c is %d, out_args_c is %d, offest ist %d\n", in_args_c, out_args_c, offset_); + //printf("in_args_c is %d, out_args_c is %d, offest ist %d\n", in_args_c, out_args_c, offset_); GIArgument in_args[in_args_c]; GIArgument out_args[out_args_c]; diff --git a/src/namespace_loader.cc b/src/namespace_loader.cc index c1d1a06..20dcc0d 100644 --- a/src/namespace_loader.cc +++ b/src/namespace_loader.cc @@ -15,7 +15,12 @@ std::map NamespaceLoader::type_libs; void NamespaceLoader::Initialize(Handle target) { GIR_SET_METHOD(target, "load", NamespaceLoader::Load); - GIR_SET_METHOD(target, "search_path", NamespaceLoader::SearchPath); + GIR_SET_METHOD(target, "searchPath", NamespaceLoader::SearchPath); + GIR_SET_METHOD(target, "isRegistered", NamespaceLoader::IsRegistered); + GIR_SET_METHOD(target, "getDependencies", NamespaceLoader::GetDependencies); + GIR_SET_METHOD(target, "loadedNamespaces", NamespaceLoader::LoadedNamespaces); + GIR_SET_METHOD(target, "getVersion", NamespaceLoader::GetVersion); + GIR_SET_METHOD(target, "getVersions", NamespaceLoader::GetVersions); } Handle NamespaceLoader::Load(const Arguments &args) { @@ -157,6 +162,116 @@ Handle NamespaceLoader::SearchPath(const Arguments &args) { return scope.Close(res); } +Handle NamespaceLoader::IsRegistered(const Arguments &args) { + HandleScope scope; + + if(!repo) { + repo = g_irepository_get_default(); + } + + if(args.Length() < 1 || !args[0]->IsString()) { + return BAD_ARGS(); + } + + String::Utf8Value namespace_(args[0]->ToString()); + char *version = NULL; + + if(args.Length() > 1 && args[1]->IsString()) { + String::Utf8Value version_(args[1]); + version = *version_; + } + + return scope.Close(Boolean::New(g_irepository_is_registered(repo, *namespace_, version))); +} + +Handle NamespaceLoader::GetDependencies(const Arguments &args) { + HandleScope scope; + + if(!repo) { + repo = g_irepository_get_default(); + } + + if(args.Length() < 1 || !args[0]->IsString()) { + return BAD_ARGS(); + } + + String::Utf8Value namespace_(args[0]->ToString()); + + char **versions = g_irepository_get_dependencies(repo, *namespace_); + + int size = 0; + while(versions[size] != NULL) { size++; } + + Handle res = Array::New(size); + for(int i=0; versions[i] != NULL; i++) { + res->Set(i, String::New(versions[i])); + } + + return scope.Close(res); +} + +Handle NamespaceLoader::LoadedNamespaces(const Arguments &args) { + HandleScope scope; + + if(!repo) { + repo = g_irepository_get_default(); + } + + char **namespaces = g_irepository_get_loaded_namespaces(repo); + + int size = 0; + while(namespaces[size] != NULL) { size++; } + + Handle res = Array::New(size); + for(int i=0; namespaces[i] != NULL; i++) { + res->Set(i, String::New(namespaces[i])); + } + + return scope.Close(res); +} + +Handle NamespaceLoader::GetVersion(const Arguments &args) { + HandleScope scope; + + if(!repo) { + repo = g_irepository_get_default(); + } + + if(args.Length() < 1 || !args[0]->IsString()) { + return BAD_ARGS(); + } + + String::Utf8Value namespace_(args[0]->ToString()); + + const char *version = g_irepository_get_version(repo, *namespace_); + + return scope.Close(String::New(version)); +} + +Handle NamespaceLoader::GetVersions(const Arguments &args) { + HandleScope scope; + + if(!repo) { + repo = g_irepository_get_default(); + } + + if(args.Length() < 1 || !args[0]->IsString()) { + return BAD_ARGS(); + } + + String::Utf8Value namespace_(args[0]->ToString()); + + GList *versions = g_irepository_enumerate_versions(repo, *namespace_); + int length = g_list_length(versions); + Handle res = Array::New(length); + + for(int i=0; iSet(i, String::New((char*)g_list_nth_data(versions, i))); + } + + return scope.Close(res); +} + } diff --git a/src/namespace_loader.h b/src/namespace_loader.h index 66ac2cf..24321ac 100644 --- a/src/namespace_loader.h +++ b/src/namespace_loader.h @@ -17,6 +17,11 @@ class NamespaceLoader { static v8::Handle Load(const v8::Arguments &args); static v8::Handle SearchPath(const v8::Arguments &args); + static v8::Handle IsRegistered(const v8::Arguments &args); + static v8::Handle GetDependencies(const v8::Arguments &args); + static v8::Handle LoadedNamespaces(const v8::Arguments &args); + static v8::Handle GetVersion(const v8::Arguments &args); + static v8::Handle GetVersions(const v8::Arguments &args); private: static v8::Handle LoadNamespace(char *namespace_, char *version); diff --git a/src/types/function.cc b/src/types/function.cc index ef4cf7e..7c1b175 100644 --- a/src/types/function.cc +++ b/src/types/function.cc @@ -5,8 +5,6 @@ #include #include -#include -#include using namespace v8; diff --git a/src/types/object.cc b/src/types/object.cc index 700be66..8e6096e 100644 --- a/src/types/object.cc +++ b/src/types/object.cc @@ -226,8 +226,13 @@ void GIRObject::Initialize(Handle target, char *namespace_) { std::vector::iterator it; std::vector::iterator temp; GIObjectInfo* parent; + std::vector roots; + Handle objs = Array::New(templates.size()); + int i = 0; for(it = templates.begin(); it != templates.end(); ++it) { + objs->Set(i++, String::New(g_base_info_get_name(it->info))); + parent = g_object_info_get_parent(it->info); if(strcmp(it->namespace_, namespace_) != 0 || !parent) { continue; @@ -236,8 +241,12 @@ void GIRObject::Initialize(Handle target, char *namespace_) { for(temp = templates.begin(); temp != templates.end(); ++temp) { if(g_base_info_equal(temp->info, parent)) { it->function->Inherit(temp->function); + break; } } + if(temp == templates.end()) { + roots.push_back(g_base_info_get_name(it->info)); + } } for(it = templates.begin(); it != templates.end(); ++it) { if(strcmp(it->namespace_, namespace_) == 0) { @@ -245,6 +254,16 @@ void GIRObject::Initialize(Handle target, char *namespace_) { } } + int rootsLength = roots.size(); + Handle v8roots = Array::New(rootsLength); + i = 0; + for(std::vector::iterator it = roots.begin(); it != roots.end(); it++) { + v8roots->Set(i++, String::New(*it)); + } + + target->Set(String::New("__roots__"), v8roots); + target->Set(String::New("__objects__"), objs); + emit_symbol = NODE_PSYMBOL("emit"); } diff --git a/wscript b/wscript index c250336..ccea997 100644 --- a/wscript +++ b/wscript @@ -14,8 +14,6 @@ def configure(conf): conf.check_tool("node_addon") conf.check_cfg(package='gobject-introspection-1.0', uselib_store='GIREPOSITORY', args='--cflags --libs') conf.check_cfg(package='glib', uselib_store='GLIB', args='--cflags --libs') - conf.check_cfg(package='gtk+-2.0', uselib_store='GTK', args='--cflags --libs') - conf.check_cfg(package='gdk-2.0', uselib_store='GDK', args='--cflags --libs') def build(bld): obj = bld.new_task_gen("cxx", "shlib", "node_addon")