]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/base.jsm
Import r6948 from upstream hg supporting Firefox up to 24.*
[dactyl.git] / common / modules / base.jsm
index 589c8042478c4c948821c6761d9202e592da283d..a9318eb414119a78cb118446827cf1f377a93379 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2009-2012 Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2009-2013 Kris Maglione <maglione.k@gmail.com>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
@@ -68,8 +68,8 @@ Object.defineProperty(defineModule.loadLog, "push", {
     }
 });
 defineModule.prefix = "";
-defineModule.dump = function dump_() {
-    let msg = Array.map(arguments, function (msg) {
+defineModule.dump = function dump_(...args) {
+    let msg = args.map(function (msg) {
         if (loaded.util && typeof msg == "object")
             msg = util.objectToString(msg);
         return msg;
@@ -78,13 +78,13 @@ defineModule.dump = function dump_() {
                .replace(/^./gm, JSMLoader.name + ": $&"));
 }
 defineModule.modules = [];
-defineModule.time = function time(major, minor, func, self) {
+defineModule.time = function time(major, minor, func, self, ...args) {
     let time = Date.now();
     if (typeof func !== "function")
         func = self[func];
 
     try {
-        var res = func.apply(self, Array.slice(arguments, 4));
+        var res = func.apply(self, args);
     }
     catch (e) {
         loaded.util && util.reportError(e);
@@ -241,7 +241,7 @@ function properties(obj, prototypes, debugger_) {
                 return array.uniq([k for (k in obj)].concat(
                     Object.getOwnPropertyNames(
                               XPCNativeWrapper.unwrap(obj))
-                          .filter(filter)))
+                          .filter(filter)));
             }
             else if (!e.stack) {
                 throw Error(e);
@@ -441,18 +441,18 @@ function curry(fn, length, self, acc) {
         return fn;
 
     // Close over function with 'this'
-    function close(self, fn) function () fn.apply(self, Array.slice(arguments));
+    function close(self, fn) function () fn.apply(self, arguments);
 
     if (acc == null)
         acc = [];
 
-    return function curried() {
-        let args = acc.concat(Array.slice(arguments));
-
+    return function curried(...args) {
         // The curried result should preserve 'this'
-        if (arguments.length == 0)
+        if (args.length == 0)
             return close(self || this, curried);
 
+        let args = acc.concat(args);
+
         if (args.length >= length)
             return fn.apply(self || this, args);
 
@@ -461,15 +461,14 @@ function curry(fn, length, self, acc) {
 }
 
 if (curry.bind)
-    var bind = function bind(meth, self) let (func = callable(meth) ? meth : self[meth])
-        func.bind.apply(func, Array.slice(arguments, 1));
+    var bind = function bind(meth, self, ...args) let (func = callable(meth) ? meth : self[meth])
+        func.bind.apply(func, [self].concat(args));
 else
-    var bind = function bind(func, self) {
+    var bind = function bind(func, self, ...args) {
         if (!callable(func))
             func = self[func];
 
-        let args = Array.slice(arguments, bind.length);
-        return function bound() func.apply(self, args.concat(Array.slice(arguments)));
+        return function bound(...args2) func.apply(self, args.concat(args2));
     };
 
 /**
@@ -574,8 +573,8 @@ function isString(val) objproto.toString.call(val) == "[object String]";
  */
 function callable(val) typeof val === "function" && !(val instanceof Ci.nsIDOMElement);
 
-function call(fn) {
-    fn.apply(arguments[1], Array.slice(arguments, 2));
+function call(fn, self, ...args) {
+    fn.apply(self, args);
     return fn;
 }
 
@@ -660,8 +659,8 @@ function update(target) {
                         func.superapply = function superapply(self, args)
                             let (meth = Object.getPrototypeOf(target)[k])
                                 meth && meth.apply(self, args);
-                        func.supercall = function supercall(self)
-                            func.superapply(self, Array.slice(arguments, 1));
+                        func.supercall = function supercall(self, ...args)
+                            func.superapply(self, args);
                     }
                 }
                 Object.defineProperty(target, k, desc);
@@ -687,8 +686,8 @@ function update_(target) {
                         func.superapply = function super_apply(self, args)
                             let (meth = Object.getPrototypeOf(target)[k])
                                 meth && meth.apply(self, args);
-                        func.supercall = function super_call(self)
-                            func.superapply(self, Array.slice(arguments, 1));
+                        func.supercall = function super_call(self, ...args)
+                            func.superapply(self, args);
                     }
                 }
                 Object.defineProperty(target, k, desc);
@@ -722,9 +721,8 @@ function update_(target) {
  *
  * @returns {function} The constructor for the resulting class.
  */
-function Class() {
+function Class(...args) {
 
-    var args = Array.slice(arguments);
     if (isString(args[0]))
         var name = args.shift();
     var superclass = Class;
@@ -953,14 +951,13 @@ Class.prototype = {
      * @returns {nsITimer} The timer which backs this timeout.
      */
     timeout: function timeout(callback, timeout) {
-        const self = this;
-        function timeout_notify(timer) {
-            if (self.stale ||
+        let timeout_notify = (timer) => {
+            if (this.stale ||
                     util.rehashing && !isinstance(Cu.getGlobalForObject(callback), ["BackstagePass"]))
                 return;
-            self.timeouts.splice(self.timeouts.indexOf(timer), 1);
-            util.trapErrors(callback, self);
-        }
+            this.timeouts.splice(this.timeouts.indexOf(timer), 1);
+            util.trapErrors(callback, this);
+        };
         let timer = services.Timer(timeout_notify, timeout || 0, services.Timer.TYPE_ONE_SHOT);
         this.timeouts.push(timer);
         return timer;
@@ -973,12 +970,11 @@ Class.prototype = {
      * localized properties.
      */
     update: function update() {
-        let self = this;
         // XXX: Duplication.
 
         for (let i = 0; i < arguments.length; i++) {
             let src = arguments[i];
-            Object.getOwnPropertyNames(src || {}).forEach(function (k) {
+            Object.getOwnPropertyNames(src || {}).forEach((k) => {
                 let desc = Object.getOwnPropertyDescriptor(src, k);
                 if (desc.value instanceof Class.Property)
                     desc = desc.value.init(k, this) || desc.value;
@@ -986,12 +982,16 @@ Class.prototype = {
                 if (typeof desc.value === "function") {
                     let func = desc.value.wrapped || desc.value;
                     if (!func.superapply) {
-                        func.__defineGetter__("super", function () Object.getPrototypeOf(self)[k]);
-                        func.superapply = function superapply(self, args)
-                            let (meth = Object.getPrototypeOf(self)[k])
-                                meth && meth.apply(self, args);
-                        func.supercall = function supercall(self)
-                            func.superapply(self, Array.slice(arguments, 1));
+                        func.__defineGetter__("super", () => Object.getPrototypeOf(this)[k]);
+
+                        func.superapply = function superapply(self, args) {
+                            let meth = Object.getPrototypeOf(self)[k];
+                            return meth && meth.apply(self, args);
+                        };
+
+                        func.supercall = function supercall(self, ...args) {
+                            return func.superapply(self, args);
+                        }
                     }
                 }
 
@@ -1080,7 +1080,7 @@ function XPCOMShim(interfaces) {
         getInterfaces: function (count) { count.value = 0; }
     });
     return (interfaces || []).reduce(function (shim, iface) shim.QueryInterface(Ci[iface]),
-                                     ip.data)
+                                     ip.data);
 };
 let stub = Class.Property({
     configurable: true,
@@ -1097,7 +1097,7 @@ var ErrorBase = Class("ErrorBase", Error, {
     init: function EB_init(message, level) {
         level = level || 0;
         let error = Error(message);
-        update(this, error)
+        update(this, error);
         this.stack = error.stack;
         this.message = message;
 
@@ -1127,22 +1127,22 @@ var Finished = Class("Finished", ErrorBase);
  * @param {Object} classProperties Properties to be applied to the class constructor.
  * @returns {Class}
  */
-function Module(name, prototype) {
+function Module(name, prototype, ...args) {
     try {
-        let init = callable(prototype) ? 4 : 3;
-        let proto = arguments[callable(prototype) ? 2 : 1];
+        let init = callable(prototype) ? 2 : 1;
+        let proto = callable(prototype) ? args[0] : prototype;
 
         proto._metaInit_ = function () {
             delete module.prototype._metaInit_;
             currentModule[name.toLowerCase()] = this;
         };
 
-        const module = Class.apply(Class, Array.slice(arguments, 0, init));
+        const module = Class.apply(Class, [name, prototype, ...args.slice(0, init)]);
         let instance = module();
         module.className = name.toLowerCase();
 
         instance.INIT = update(Object.create(Module.INIT),
-                               arguments[init] || {});
+                               args[init] || {});
 
         currentModule[module.className] = instance;
         defineModule.modules.push(instance);
@@ -1172,7 +1172,7 @@ Module.INIT = {
             module.isLocalModule = true;
 
             modules.jsmodules[this.constructor.className] = module;
-            locals.reverse().forEach(function (fn, i) update(objs[i], fn.apply(module, args)))
+            locals.reverse().forEach(function (fn, i) update(objs[i], fn.apply(module, args)));
 
             memoize(module, "closure", Class.makeClosure);
             module.instance = module;
@@ -1199,13 +1199,9 @@ Module.INIT = {
  *
  * @returns {function} The constructor for the new Struct.
  */
-function Struct() {
-    if (!/^[A-Z]/.test(arguments[0]))
-        var args = Array.slice(arguments, 0);
-    else {
-        var className = arguments[0];
-        args = Array.slice(arguments, 1);
-    }
+function Struct(...args) {
+    if (/^[A-Z]/.test(args[0]))
+        var className = args.shift();
 
     const Struct = Class(className || "Struct", StructBase, {
         length: args.length,
@@ -1599,9 +1595,9 @@ var array = Class("array", Array, {
             },
             array: ary,
             toString: function () this.array.toString(),
-            concat: function () this.__noSuchMethod__("concat", Array.slice(arguments)),
-            filter: function () this.__noSuchMethod__("filter", Array.slice(arguments)),
-            map: function () this.__noSuchMethod__("map", Array.slice(arguments))
+            concat: function (...args) this.__noSuchMethod__("concat", args),
+            filter: function (...args) this.__noSuchMethod__("filter", args),
+            map: function (...args) this.__noSuchMethod__("map", args)
         };
     }
 }, {
@@ -1734,8 +1730,8 @@ var array = Class("array", Array, {
 /* Make Minefield not explode, because Minefield exploding is not fun. */
 let iterProto = Iter.prototype;
 Object.keys(iter).forEach(function (k) {
-    iterProto[k] = function () {
-        let res = iter[k].apply(iter, [this].concat(Array.slice(arguments)));
+    iterProto[k] = function (...args) {
+        let res = iter[k].apply(iter, [this].concat(args));
         if (isinstance(res, ["Iterator", "Generator"]))
             return Iter(res);
         return res;
@@ -1744,8 +1740,8 @@ Object.keys(iter).forEach(function (k) {
 
 Object.keys(array).forEach(function (k) {
     if (!(k in iterProto))
-        iterProto[k] = function () {
-            let res = array[k].apply(array, [this.toArray()].concat(Array.slice(arguments)));
+        iterProto[k] = function (...args) {
+            let res = array[k].apply(array, [this.toArray()].concat(args));
             if (isinstance(res, ["Iterator", "Generator"]))
                 return Iter(res);
             if (isArray(res))
@@ -1769,4 +1765,4 @@ endModule();
 
 // catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}
 
-// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: