]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/base.jsm
Import 1.0 supporting Firefox up to 14.*
[dactyl.git] / common / modules / base.jsm
index bc767f68887cf45147ac8e9787c6a21b5c2e33d0..51d2f0d757ca93bd5fa1d5579fc2fb76b4bc2360 100644 (file)
@@ -289,7 +289,7 @@ function properties(obj, prototypes, debugger_) {
     for (; obj; obj = prototypes && prototype(obj)) {
         try {
             if (sandbox.Object.getOwnPropertyNames || !debugger_ || !services.debugger.isOn)
-                var iter = values(Object.getOwnPropertyNames(obj));
+                var iter = (v for each (v in Object.getOwnPropertyNames(obj)));
         }
         catch (e) {}
         if (!iter)
@@ -363,7 +363,7 @@ function keys(obj) iter(function keys() {
  * @returns {Generator}
  */
 function values(obj) iter(function values() {
-    if (isinstance(obj, ["Generator", "Iterator"]))
+    if (isinstance(obj, ["Generator", "Iterator", Iter]))
         for (let k in obj)
             yield k;
     else
@@ -1452,17 +1452,7 @@ function iter(obj, iface) {
             return iter(obj.enumerator());
         return iter(obj.enumerator);
     }
-    res.__noSuchMethod__ = function __noSuchMethod__(meth, args) {
-        if (meth in iter)
-            var res = iter[meth].apply(iter, [this].concat(args));
-        else
-            res = let (ary = array(this))
-                ary[meth] ? ary[meth].apply(ary, args) : ary.__noSuchMethod__(meth, args);
-        if (isinstance(res, ["Iterator", "Generator"]))
-            return iter(res);
-        return res;
-    };
-    return res;
+    return Iter(res);
 }
 update(iter, {
     toArray: function toArray(iter) array(iter).array,
@@ -1576,6 +1566,23 @@ update(iter, {
     }
 });
 
+const Iter = Class("Iter", {
+    init: function init(iter) {
+        this.iter = iter;
+        if ("__iterator__" in iter)
+            this.iter = iter.__iterator__();
+
+        if (this.iter.finalize)
+            this.finalize = function finalize() this.iter.finalize.apply(this.iter, arguments);
+    },
+
+    next: function next() this.iter.next(),
+
+    send: function send() this.iter.send.apply(this.iter, arguments),
+
+    __iterator__: function () this.iter
+});
+
 /**
  * Array utility methods.
  */
@@ -1731,6 +1738,40 @@ 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)));
+        if (isinstance(res, ["Iterator", "Generator"]))
+            return Iter(res);
+        return res;
+    };
+});
+
+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)));
+            if (isinstance(res, ["Iterator", "Generator"]))
+                return Iter(res);
+            if (isArray(res))
+                return array(res);
+            return res;
+        };
+});
+
+Object.getOwnPropertyNames(Array.prototype).forEach(function (k) {
+    if (!(k in iterProto) && callable(Array.prototype[k]))
+        iterProto[k] = function () {
+            let ary = iter(this).toArray();
+            let res = ary[k].apply(ary, arguments);
+            if (isArray(res))
+                return array(res);
+            return res;
+        };
+});
+
 endModule();
 
 // catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}