diff options
author | portix <portix@gmx.net> | 2014-03-04 17:25:27 +0100 |
---|---|---|
committer | portix <portix@gmx.net> | 2014-03-04 17:25:27 +0100 |
commit | ddd604ad9b11695539b2c0d7d12ef9648d269efe (patch) | |
tree | c157cbc2ea056079135adcf37217e422079fa47b | |
parent | 7c222c5a3063e5af5889653814d40a4efa59fa5c (diff) | |
download | dwb-ddd604ad9b11695539b2c0d7d12ef9648d269efe.zip |
Define namespace, use namespace in all api modules
-rw-r--r-- | scripts/lib/console.js.in | 3 | ||||
-rw-r--r-- | scripts/lib/data.js.in | 374 | ||||
-rw-r--r-- | scripts/lib/dwb.js.in | 6 | ||||
-rw-r--r-- | scripts/lib/extensions.js.in | 7 | ||||
-rw-r--r-- | scripts/lib/io.js.in | 2 | ||||
-rw-r--r-- | scripts/lib/net.js.in | 2 | ||||
-rw-r--r-- | scripts/lib/signals.js.in | 6 | ||||
-rw-r--r-- | scripts/lib/system.js.in | 2 | ||||
-rw-r--r-- | scripts/lib/util.js.in | 4 | ||||
-rw-r--r-- | src/scripts.c | 92 |
10 files changed, 297 insertions, 201 deletions
diff --git a/scripts/lib/console.js.in b/scripts/lib/console.js.in index eb57630d..5aaac894 100644 --- a/scripts/lib/console.js.in +++ b/scripts/lib/console.js.in @@ -1,5 +1,7 @@ // See COPYING for copyright and license details (function() { + var console = namespace("console"); + /** * @name assert * @memberOf console @@ -72,6 +74,7 @@ * @function * @param {Object} argument Argument passed to real function * */ + var tabs = namespace("tabs"); function execute(f, arg) { tabs.current.inject("console." + f + ".apply(console, JSON.parse(arguments[0]));", diff --git a/scripts/lib/data.js.in b/scripts/lib/data.js.in index 22efb201..192cccdd 100644 --- a/scripts/lib/data.js.in +++ b/scripts/lib/data.js.in @@ -1,193 +1,197 @@ // See COPYING for copyright and license details (function () { - var configDir = data.configDir; - var profile = data.profile; - function filterNull(object) { return object !== null; } - function filterEmpty(line) { return line.trim().length > 0; } - function mapDefault(line) { return line.trim(); } - function mapQuickmarks(line) { - var token = line.trim().split(" "); - return { - mark : token[0], - url : token.slice(1).join(" ") - }; - } - function mapSearchengines(line) { - var engine = null; - try { + var data = namespace("data"); + var io = namespace("io"); + var net = namespace("net"); + + var configDir = data.configDir; + var profile = data.profile; + function filterNull(object) { return object !== null; } + function filterEmpty(line) { return line.trim().length > 0; } + function mapDefault(line) { return line.trim(); } + function mapQuickmarks(line) { var token = line.trim().split(" "); - var keyword = token[0]; - var url = token.slice(1).join(" "); - var purl = net.parseUri(url); - var host = purl ? purl.host : null; - engine = { - keyword : keyword, - url : url, - host : host + return { + mark : token[0], + url : token.slice(1).join(" ") }; - } - catch (e) { } - return engine; + } + function mapSearchengines(line) { + var engine = null; + try { + var token = line.trim().split(" "); + var keyword = token[0]; + var url = token.slice(1).join(" "); + var purl = net.parseUri(url); + var host = purl ? purl.host : null; + engine = { + keyword : keyword, + url : url, + host : host + }; + } + catch (e) { } + return engine; - } - Object.defineProperties(data, { - /** - * Parses data to an array - * - * @name parse - * @memberOf data - * @function - * @type Array - * - * @param {String} name - * Name of the data to parse, can be one of <i>bookmarks</i>, - * <i>cookiesWhitelist</i>, <i>sessionCookiesWhitelist</i>, - * <i>scriptWhitelist</i>, <i>pluginsWhitelist</i>, <i>quickmarks</i> or - * <i>searchEngines</i>. - * - * @returns - * Array of parsed objects; - * <b>quickmarks</b> will return an array of objects - * with properties <b>mark</b> and <b>url</b> and - * <b>searchEngines</b> will return an array of objects with properties <b>keyword</b>, - * <b>url</b> and <b>host</b>. All other values will return an - * array of strings. - * */ - parse : { - value : function(name) { - var mapFunction; - switch (name) { - case "bookmarks": - case "cookiesWhitelist" : - case "sessionCookiesWhitelist" : - case "scriptWhitelist" : - case "pluginsWhitelist" : - mapFunction = mapDefault; - break; - case "quickmarks" : - mapFunction = mapQuickmarks; - break; - case "searchEngines" : - mapFunction = mapSearchengines; - break; - default : - throw new Error("Parsing " + name + " is not supported!"); - } - var data = null; + } + Object.defineProperties(data, { + /** + * Parses data to an array + * + * @name parse + * @memberOf data + * @function + * @type Array + * + * @param {String} name + * Name of the data to parse, can be one of <i>bookmarks</i>, + * <i>cookiesWhitelist</i>, <i>sessionCookiesWhitelist</i>, + * <i>scriptWhitelist</i>, <i>pluginsWhitelist</i>, <i>quickmarks</i> or + * <i>searchEngines</i>. + * + * @returns + * Array of parsed objects; + * <b>quickmarks</b> will return an array of objects + * with properties <b>mark</b> and <b>url</b> and + * <b>searchEngines</b> will return an array of objects with properties <b>keyword</b>, + * <b>url</b> and <b>host</b>. All other values will return an + * array of strings. + * */ + parse : { + value : function(name) { + var mapFunction; + switch (name) { + case "bookmarks": + case "cookiesWhitelist" : + case "sessionCookiesWhitelist" : + case "scriptWhitelist" : + case "pluginsWhitelist" : + mapFunction = mapDefault; + break; + case "quickmarks" : + mapFunction = mapQuickmarks; + break; + case "searchEngines" : + mapFunction = mapSearchengines; + break; + default : + throw new Error("Parsing " + name + " is not supported!"); + } + var data = null; - try { - data = io.read(this[name]); - } - catch (e) { - return []; - } - return data.split("\n").filter(filterEmpty).map(mapFunction).filter(filterNull); - } - }, - /** - * The bookmark file - * @name bookmarks - * @memberOf data - * @readonly - * @type String - * */ - "bookmarks" : { value : configDir + "/" + profile + "/bookmarks", enumerable : true }, - /** - * The history file - * @name history - * @memberOf data - * @readonly - * @type String - * */ - "history" : { value : configDir + "/" + profile + "/history", enumerable : true }, - /** - * The cookie file - * @name cookies - * @memberOf data - * @readonly - * @type String - * */ - "cookies" : { value : configDir + "/" + profile + "/cookies", enumerable : true }, - /** - * The quickmarks file - * @name quickmarks - * @memberOf data - * @readonly - * @type String - * */ - "quickmarks" : { value : configDir + "/" + profile + "/quickmarks", enumerable : true }, - /** - * The whitelist for persistent cookies - * @name cookiesWhitelist - * @memberOf data - * @readonly - * @type String - * */ - "cookiesWhitelist" : { value : configDir + "/" + profile + "/cookies.allow", enumerable : true }, - /** - * The whitelist for session cookies - * @name sessionCookiesWhitelist - * @memberOf data - * @readonly - * @type String - * */ - "sessionCookiesWhitelist" : { value : configDir + "/" + profile + "/cookies_session.allow", enumerable : true }, - /** - * The whitelist for plugins - * @name pluginsWhitelist - * @memberOf data - * @readonly - * @type String - * */ - "pluginsWhitelist" : { value : configDir + "/" + profile + "/plugins.allow", enumerable : true }, - /** - * The whitelist for scripts - * @name scriptWhitelist - * @memberOf data - * @readonly - * @type String - * */ - "scriptWhitelist" : { value : configDir + "/" + profile + "/scripts.allow", enumerable : true }, - /** - * The session file - * @name session - * @memberOf data - * @readonly - * @type String - * */ - "session" : { value : configDir + "/" + profile + "/session", enumerable : true }, - /** - * The custom keys file - * @name customKeys - * @memberOf data - * @readonly - * @type String - * */ - "customKeys" : { value : configDir + "/" + profile + "/custom_keys", enumerable : true }, - /** - * The keyboard configuration file - * @name keys - * @memberOf data - * @readonly - * @type String - * */ - "keys" : { value : configDir + "/keys", enumerable : true }, - /** - * The settings configuration file - * @name settings - * @memberOf data - * @readonly - * @type String - * */ - "settings" : { value : configDir + "/settings", enumerable : true }, - /** - * The searchengines file - * @name searchEngines - * @memberOf data - * @readonly - * @type String - * */ - "searchEngines" : { value : configDir + "/searchengines", enumerable : true } - }); + try { + data = io.read(this[name]); + } + catch (e) { + return []; + } + return data.split("\n").filter(filterEmpty).map(mapFunction).filter(filterNull); + } + }, + /** + * The bookmark file + * @name bookmarks + * @memberOf data + * @readonly + * @type String + * */ + "bookmarks" : { value : configDir + "/" + profile + "/bookmarks", enumerable : true }, + /** + * The history file + * @name history + * @memberOf data + * @readonly + * @type String + * */ + "history" : { value : configDir + "/" + profile + "/history", enumerable : true }, + /** + * The cookie file + * @name cookies + * @memberOf data + * @readonly + * @type String + * */ + "cookies" : { value : configDir + "/" + profile + "/cookies", enumerable : true }, + /** + * The quickmarks file + * @name quickmarks + * @memberOf data + * @readonly + * @type String + * */ + "quickmarks" : { value : configDir + "/" + profile + "/quickmarks", enumerable : true }, + /** + * The whitelist for persistent cookies + * @name cookiesWhitelist + * @memberOf data + * @readonly + * @type String + * */ + "cookiesWhitelist" : { value : configDir + "/" + profile + "/cookies.allow", enumerable : true }, + /** + * The whitelist for session cookies + * @name sessionCookiesWhitelist + * @memberOf data + * @readonly + * @type String + * */ + "sessionCookiesWhitelist" : { value : configDir + "/" + profile + "/cookies_session.allow", enumerable : true }, + /** + * The whitelist for plugins + * @name pluginsWhitelist + * @memberOf data + * @readonly + * @type String + * */ + "pluginsWhitelist" : { value : configDir + "/" + profile + "/plugins.allow", enumerable : true }, + /** + * The whitelist for scripts + * @name scriptWhitelist + * @memberOf data + * @readonly + * @type String + * */ + "scriptWhitelist" : { value : configDir + "/" + profile + "/scripts.allow", enumerable : true }, + /** + * The session file + * @name session + * @memberOf data + * @readonly + * @type String + * */ + "session" : { value : configDir + "/" + profile + "/session", enumerable : true }, + /** + * The custom keys file + * @name customKeys + * @memberOf data + * @readonly + * @type String + * */ + "customKeys" : { value : configDir + "/" + profile + "/custom_keys", enumerable : true }, + /** + * The keyboard configuration file + * @name keys + * @memberOf data + * @readonly + * @type String + * */ + "keys" : { value : configDir + "/keys", enumerable : true }, + /** + * The settings configuration file + * @name settings + * @memberOf data + * @readonly + * @type String + * */ + "settings" : { value : configDir + "/settings", enumerable : true }, + /** + * The searchengines file + * @name searchEngines + * @memberOf data + * @readonly + * @type String + * */ + "searchEngines" : { value : configDir + "/searchengines", enumerable : true } + }); })(); Object.freeze(data); diff --git a/scripts/lib/dwb.js.in b/scripts/lib/dwb.js.in index fc74f239..470fb754 100644 --- a/scripts/lib/dwb.js.in +++ b/scripts/lib/dwb.js.in @@ -1,5 +1,11 @@ // See COPYING for copyright and license details (function() { + var gui = namespace("gui"); + var io = namespace("io"); + var tabs = namespace("tabs"); + var timer = namespace("timer"); + var util = namespace("util"); + var _modules = {}; var _requires = {}; var _callbacks = []; diff --git a/scripts/lib/extensions.js.in b/scripts/lib/extensions.js.in index 08c375f3..c90433f3 100644 --- a/scripts/lib/extensions.js.in +++ b/scripts/lib/extensions.js.in @@ -101,12 +101,19 @@ * */ (function () { + var data = namespace("data"); + var extensions = namespace("extensions"); + var io = namespace("io"); + var system = namespace("system"); + var util = namespace("util"); + var _config = {}; var _registered = {}; var _configLoaded = false; var _chromePages = {}; var _chromeLoading = []; + function _getPlugin(name, filename) { if (system.fileTest(filename, FileTest.exists)) diff --git a/scripts/lib/io.js.in b/scripts/lib/io.js.in index 473ff6ed..498cd980 100644 --- a/scripts/lib/io.js.in +++ b/scripts/lib/io.js.in @@ -1,5 +1,7 @@ // See COPYING for copyright and license details (function () { + var io = namespace("io"); + var prefixMessage = "\n==> DEBUG [MESSAGE] : "; var prefixFile = "\n==> DEBUG [FILE] : "; var prefixError = "\n==> DEBUG [ERROR] : "; diff --git a/scripts/lib/net.js.in b/scripts/lib/net.js.in index b6bd8b07..0c7277d4 100644 --- a/scripts/lib/net.js.in +++ b/scripts/lib/net.js.in @@ -1,5 +1,7 @@ // See COPYING for copyright and license details (function() { + var net = namespace("net"); + var tldEnd = new RegExp("\\.tld$"); Object.defineProperties(net, { /** diff --git a/scripts/lib/signals.js.in b/scripts/lib/signals.js.in index 21a83536..06fbf20f 100644 --- a/scripts/lib/signals.js.in +++ b/scripts/lib/signals.js.in @@ -1,6 +1,8 @@ // See COPYING for copyright and license details -(function () -{ +(function () { + var signals = namespace("signals"); + var tabs = namespace("tabs"); + var _sigCount = {}; var _byName = {}; var _byId = {}; diff --git a/scripts/lib/system.js.in b/scripts/lib/system.js.in index 3c7c0e4c..63d9a01e 100644 --- a/scripts/lib/system.js.in +++ b/scripts/lib/system.js.in @@ -1,5 +1,7 @@ // See COPYING for copyright and license details (function() { + var system = namespace("system"); + Object.defineProperties(system, { "spawn" : { diff --git a/scripts/lib/util.js.in b/scripts/lib/util.js.in index c6a55ccc..af793351 100644 --- a/scripts/lib/util.js.in +++ b/scripts/lib/util.js.in @@ -1,5 +1,9 @@ // See COPYING for copyright and license details (function () { + var util = namespace("util"); + var data = namespace("data"); + var tabs = namespace("tabs"); + Object.defineProperties(util, { /** diff --git a/src/scripts.c b/src/scripts.c index 840b3d8b..71980b04 100644 --- a/src/scripts.c +++ b/src/scripts.c @@ -279,6 +279,22 @@ static gboolean s_debugging = false; static GHashTable *s_exports = NULL; static GSList *s_servers = NULL; static gboolean s_keymap_dirty = false; +enum { + NAMESPACE_CLIPBOARD, + NAMESPACE_CONSOLE, + NAMESPACE_DATA, + NAMESPACE_EXTENSIONS, + NAMESPACE_GUI, + NAMESPACE_IO, + NAMESPACE_NET, + NAMESPACE_SIGNALS, + NAMESPACE_SYSTEM, + NAMESPACE_TABS, + NAMESPACE_TIMER, + NAMESPACE_UTIL, + NAMESPACE_LAST, +}; +static JSValueRef s_namespaces[NAMESPACE_LAST]; /* Only defined once */ static JSValueRef UNDEFINED, NIL; @@ -2220,6 +2236,37 @@ global_execute(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, s exit(0); return JSValueMakeBoolean(ctx, status == STATUS_OK); }/*}}}*/ +static JSValueRef +global_namespace(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + const static char *mapping[] = { + [NAMESPACE_CLIPBOARD] = "clipboard", + [NAMESPACE_CONSOLE] = "console", + [NAMESPACE_DATA] = "data", + [NAMESPACE_EXTENSIONS] = "extensions", + [NAMESPACE_GUI] = "gui", + [NAMESPACE_IO] = "io", + [NAMESPACE_NET] = "net", + [NAMESPACE_SIGNALS] = "signals", + [NAMESPACE_SYSTEM] = "system", + [NAMESPACE_TABS] = "tabs", + [NAMESPACE_TIMER] = "timer", + [NAMESPACE_UTIL] = "util", + }; + JSValueRef ret = NIL; + if (argc > 0) { + char *name = js_value_to_char(ctx, argv[0], PROP_LENGTH, exc); + if (name != NULL) { + for (int i; i<NAMESPACE_LAST; i++) { + if (!strcmp(name, mapping[i])) { + ret = s_namespaces[i]; + break; + } + } + g_free(name); + } + } + return ret; +} /** * Exit dwb @@ -2443,8 +2490,7 @@ error_out: } /** * Same as {@link provide}, but can only be called from an archive. The module - * can only be required by the same archive - * inside an archive. + * can only be required by the same archive in which the module was provided. * * @name xprovide * @function @@ -6290,6 +6336,7 @@ create_object(JSContextRef ctx, JSClassRef class, JSObjectRef obj, JSClassAttrib { JSObjectRef ret = JSObjectMake(ctx, class, private); js_set_property(ctx, obj, name, ret, attr, NULL); + JSValueProtect(ctx, ret); return ret; }/*}}}*/ @@ -6473,6 +6520,7 @@ create_global_object() { pthread_rwlock_wrlock(&s_context_lock); JSClassDefinition cd; + JSValueRef o; s_ref_quark = g_quark_from_static_string("dwb_js_ref"); JSGlobalContextRef ctx; @@ -6483,6 +6531,7 @@ create_global_object() }; JSStaticFunction global_functions[] = { + { "namespace", global_namespace, kJSDefaultAttributes }, { "execute", global_execute, kJSDefaultAttributes }, { "exit", global_exit, kJSDefaultAttributes }, { "_bind", global_bind, kJSDefaultAttributes }, @@ -6526,7 +6575,8 @@ create_global_object() { 0, 0, 0, 0 }, }; class = create_class("data", NULL, data_values, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "data", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "data", NULL); + s_namespaces[NAMESPACE_DATA] = o; JSClassRelease(class); /** @@ -6543,7 +6593,8 @@ create_global_object() { 0, 0, 0 }, }; class = create_class("timer", timer_functions, NULL, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "timer", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "timer", NULL); + s_namespaces[NAMESPACE_TIMER] = o; JSClassRelease(class); /** * @namespace @@ -6560,7 +6611,8 @@ create_global_object() { 0, 0, 0 }, }; class = create_class("net", net_functions, NULL, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "net", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "net", NULL); + s_namespaces[NAMESPACE_NET] = o; JSClassRelease(class); @@ -6600,7 +6652,8 @@ create_global_object() { 0, 0, 0 }, }; class = create_class("io", io_functions, NULL, NULL); - create_object(ctx, class, global_object, kJSPropertyAttributeDontDelete, "io", NULL); + o = create_object(ctx, class, global_object, kJSPropertyAttributeDontDelete, "io", NULL); + s_namespaces[NAMESPACE_IO] = o; JSClassRelease(class); /** @@ -6622,7 +6675,8 @@ create_global_object() { 0, 0, 0 }, }; class = create_class("system", system_functions, NULL, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "system", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "system", NULL); + s_namespaces[NAMESPACE_SYSTEM] = o; JSClassRelease(class); JSStaticValue tab_values[] = { @@ -6650,7 +6704,8 @@ create_global_object() * }); * */ class = create_class("tabs", NULL, tab_values, tabs_get); - create_object(ctx, class, global_object, kJSDefaultAttributes, "tabs", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "tabs", NULL); + s_namespaces[NAMESPACE_TABS] = o; JSClassRelease(class); /** @@ -6714,11 +6769,13 @@ create_global_object() cd.setProperty = signal_set; class = JSClassCreate(&cd); - create_object(ctx, class, global_object, kJSDefaultAttributes, "signals", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "signals", NULL); + s_namespaces[NAMESPACE_SIGNALS] = o; JSClassRelease(class); class = create_class("extensions", NULL, NULL, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "extensions", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "extensions", NULL); + s_namespaces[NAMESPACE_EXTENSIONS] = o; JSClassRelease(class); class = create_class("Signal", NULL, NULL, NULL); @@ -6747,7 +6804,8 @@ create_global_object() { 0, 0, 0 }, }; class = create_class("util", util_functions, NULL, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "util", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "util", NULL); + s_namespaces[NAMESPACE_UTIL] = o; JSClassRelease(class); /** @@ -6764,7 +6822,8 @@ create_global_object() { 0, 0, 0 }, }; class = create_class("clipboard", clipboard_functions, NULL, NULL); - create_object(ctx, class, global_object, kJSDefaultAttributes, "clipboard", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "clipboard", NULL); + s_namespaces[NAMESPACE_CLIPBOARD] = o; JSClassRelease(class); @@ -6778,7 +6837,8 @@ create_global_object() * @name console * @static * */ - create_object(ctx, NULL, global_object, kJSDefaultAttributes, "console", NULL); + o = create_object(ctx, NULL, global_object, kJSDefaultAttributes, "console", NULL); + s_namespaces[NAMESPACE_CONSOLE] = o; /** * Base class for webkit/gtk objects @@ -7114,7 +7174,8 @@ create_global_object() cd.className = "gui"; cd.staticValues = gui_values; class = JSClassCreate(&cd); - create_object(ctx, class, global_object, kJSDefaultAttributes, "gui", NULL); + o = create_object(ctx, class, global_object, kJSDefaultAttributes, "gui", NULL); + s_namespaces[NAMESPACE_GUI] = o; JSClassRelease(class); /** @@ -7687,6 +7748,9 @@ scripts_end(gboolean clean_all) VIEW(gl)->script_wv = NULL; } } + for (int i=0; i<NAMESPACE_LAST; i++) { + JSValueUnprotect(s_global_context, s_namespaces[i]); + } JSValueUnprotect(s_global_context, s_array_contructor); |