diff options
author | Kris Maglione <kris@vimperator.org> | 2010-08-28 18:57:59 -0400 |
---|---|---|
committer | Kris Maglione <kris@vimperator.org> | 2010-08-28 18:57:59 -0400 |
commit | f1ca59a83d17236e7c38b8bfe3479406beba6000 (patch) | |
tree | 64d3f501afd4e8dcadf9726a05ee2e1e590f53e1 /common/content | |
parent | 4e40abe6b0521f0a4a031a1c02889b4e1dc50d83 (diff) | |
download | pentadactyl-f1ca59a83d17236e7c38b8bfe3479406beba6000.tar.gz |
Use Object.keys/getOwnPropertyNames (and provide them if they don't exist).
Diffstat (limited to 'common/content')
-rwxr-xr-x | common/content/autocommands.js | 4 | ||||
-rw-r--r-- | common/content/base.js | 46 | ||||
-rw-r--r-- | common/content/javascript.js | 64 | ||||
-rw-r--r-- | common/content/modes.js | 2 | ||||
-rw-r--r-- | common/content/modules.js | 2 |
5 files changed, 44 insertions, 74 deletions
diff --git a/common/content/autocommands.js b/common/content/autocommands.js index 0e163b5c..fd9f4e0f 100755 --- a/common/content/autocommands.js +++ b/common/content/autocommands.js @@ -167,7 +167,7 @@ const AutoCommands = Module("autocommands", { if (event) { // NOTE: event can only be a comma separated list for |:au {event} {pat} {cmd}| - let validEvents = keys(config.autocommands); + let validEvents = Object.keys(config.autocommands); validEvents.push("*"); events = event.split(","); @@ -227,7 +227,7 @@ const AutoCommands = Module("autocommands", { let [event, url] = args; let defaultURL = url || buffer.URL; - let validEvents = keys(config.autocommands); + let validEvents = Object.keys(config.autocommands); // TODO: add command validators dactyl.assert(event != "*", diff --git a/common/content/base.js b/common/content/base.js index b13a1bf1..848f22d6 100644 --- a/common/content/base.js +++ b/common/content/base.js @@ -44,30 +44,34 @@ function allkeys(obj) { } } -function keys(obj) { - if (modules.services) { - try { - let ret = {}; - services.get("debugger").wrapValue(obj).getProperties(ret, {}); - for (let prop in values(ret.value)) - yield prop.name.stringValue; - return; - } - catch (e) {} +function debuggerProperties(obj) { + if (modules.services && options["jsdebugger"]) { + let ret = {}; + services.get("debugger").wrapValue(obj).getProperties(ret, {}); + return ret.value; } +} + +if (!Object.keys) + Object.keys = function keys(obj) [k for (k in obj) if (obj.hasOwnProperty(k))]; - if ("__iterator__" in obj) { - var iter = obj.__iterator__; - yield "__iterator__"; - // This is dangerous, but necessary. - delete obj.__iterator__; +if (!Object.getOwnPropertyNames) + Object.getOwnPropertyNames = function getOwnPropertyNames(obj) { + let res = debuggerProperties(obj); + if (res) + return [prop.name.stringValue for (prop in values(res))]; + return Object.keys(obj); } - for (var k in obj) - if (obj.hasOwnProperty(k)) - yield k; - if (iter !== undefined) - obj.__iterator__ = iter; + +function properties(obj, prototypes) { + let orig = obj; + let seen = {}; + for (; obj; obj = prototypes && obj.__proto__) + for (let key in values(Object.getOwnPropertyNames(obj))) + if (!prototypes || !set.add(seen, key) && obj != orig) + yield key } + function values(obj) { for (var k in obj) if (obj.hasOwnProperty(k)) @@ -273,7 +277,7 @@ function curry(fn, length, self, acc) { function update(target) { for (let i = 1; i < arguments.length; i++) { let src = arguments[i]; - foreach(keys(src || {}), function (k) { + Object.getOwnPropertyNames(src || {}).forEach(function (k) { var get = src.__lookupGetter__(k), set = src.__lookupSetter__(k); if (!get && !set) { diff --git a/common/content/javascript.js b/common/content/javascript.js index 686929ba..f721e761 100644 --- a/common/content/javascript.js +++ b/common/content/javascript.js @@ -26,74 +26,39 @@ const JavaScript = Module("javascript", { // Some object members are only accessible as function calls getKey: function (obj, key) { try { + // if (!Object.prototype.__lookupGetter__.call(obj, key)) return obj[key]; } - catch (e) { - return undefined; - } + catch (e) {} + return undefined; }, iter: function iter(obj, toplevel) { "use strict"; - toplevel = !!toplevel; - let seen = {}; - let ret = {}; + const self = this; if(obj == null) return; - if(options["jsdebugger"]) { - let orig = obj; - let top = services.get("debugger").wrapValue(obj); - - for (; obj; obj = !toplevel && obj.__proto__) { - services.get("debugger").wrapValue(obj).getProperties(ret, {}); - for (let prop in values(ret.value)) { - if (set.add(seen, prop.name.stringValue)) - continue; - if (toplevel || obj !== orig) - yield [prop.name.stringValue, top.getProperty(prop.name.stringValue).value.getWrappedValue()] - } - } - // The debugger doesn't list some properties. I can't guess why. - // This only lists ENUMERABLE properties. - try { - for (let k in orig) - if (k in orig && !(set.has(seen, k)) - && Object.hasOwnProperty(orig, k) == toplevel) - yield [k, this.getKey(orig, k)] - } - catch(e) {} - } + let orig = obj; + if(!options["jsdebugger"]) + function value(key) self.getKey(orig, key); else { - for (let k in allkeys(obj)) - try { - if (Object.hasOwnProperty(obj, k) == toplevel) - yield [k, this.getKey(obj, k)]; - } - catch (e) {} + let top = services.get("debugger").wrapValue(obj); + function value(key) top.getProperty(key).value.getWrappedValue(); } + for (let key in properties(obj, !toplevel)) + yield [key, value(key)]; }, - // Search the object for strings starting with @key. - // If @last is defined, key is a quoted string, it's - // wrapped in @last after @offset characters are sliced - // off of it and it's quoted. objectKeys: function objectKeys(obj, toplevel) { // Things we can dereference - if (["object", "string", "function"].indexOf(typeof obj) == -1) + if (!obj || ["object", "string", "function"].indexOf(typeof obj) == -1) return []; - if (!obj) + if (modules.isPrototypeOf(obj) && !toplevel) return []; - let completions; - if (modules.isPrototypeOf(obj)) - completions = [v for (v in Iterator(obj))]; - else { - completions = [k for (k in this.iter(obj, toplevel))]; - if (!toplevel) - completions = util.Array.uniq(completions, true); - } + let completions = [k for (k in this.iter(obj, toplevel))]; // Add keys for sorting later. // Numbers are parsed to ints. @@ -124,6 +89,7 @@ const JavaScript = Module("javascript", { return cache[key] = dactyl.eval(arg, context); } catch (e) { + this.context.message = "Error: " + e; return null; } finally { diff --git a/common/content/modes.js b/common/content/modes.js index dcc6bcab..dece106b 100644 --- a/common/content/modes.js +++ b/common/content/modes.js @@ -158,7 +158,7 @@ const Modes = Module("modes", { getCharModes: function (chr) [m for (m in values(this._modeMap)) if (m.char == chr)], - matchModes: function (obj) [m for (m in values(this._modeMap)) if (array(keys(obj)).every(function (k) obj[k] == (m[k] || false)))], + matchModes: function (obj) [m for (m in values(this._modeMap)) if (Object.keys(obj).every(function (k) obj[k] == (m[k] || false)))], // show the current mode string in the command line show: function () { diff --git a/common/content/modules.js b/common/content/modules.js index 637e5748..e5d5a2a5 100644 --- a/common/content/modules.js +++ b/common/content/modules.js @@ -104,7 +104,7 @@ window.addEventListener("load", function () { dactyl.reportError(e); } } - for (let mod in keys(module.INIT)) { + for (let mod in values(Object.keys(module.INIT))) { deferredInit[mod] = deferredInit[mod] || []; deferredInit[mod].push(init(mod, module)); } |