Revision: 22231
Author:   [email protected]
Date:     Mon Jul  7 09:47:43 2014 UTC
Log:      Avoid brittle use of .bind in Promise.all

[email protected]
BUG=v8:3420
LOG=Y

Review URL: https://codereview.chromium.org/366103005
http://code.google.com/p/v8/source/detail?r=22231

Modified:
 /branches/bleeding_edge/src/promise.js
 /branches/bleeding_edge/test/mjsunit/es6/promises.js

=======================================
--- /branches/bleeding_edge/src/promise.js      Thu Jul  3 15:56:01 2014 UTC
+++ /branches/bleeding_edge/src/promise.js      Mon Jul  7 09:47:43 2014 UTC
@@ -283,11 +283,15 @@
       } else {
         for (var i = 0; i < values.length; ++i) {
           this.resolve(values[i]).then(
-            function(i, x) {
-              resolutions[i] = x;
-              if (--count === 0) deferred.resolve(resolutions);
-            }.bind(UNDEFINED, i),  // TODO(rossberg): use let loop once
-                                    // available
+            (function() {
+ // Nested scope to get closure over current i (and avoid .bind).
+              // TODO(rossberg): Use for-let instead once available.
+              var i_captured = i;
+              return function(x) {
+                resolutions[i_captured] = x;
+                if (--count === 0) deferred.resolve(resolutions);
+              };
+            })(),
             function(r) { deferred.reject(r) }
           );
         }
=======================================
--- /branches/bleeding_edge/test/mjsunit/es6/promises.js Tue May 6 14:48:34 2014 UTC +++ /branches/bleeding_edge/test/mjsunit/es6/promises.js Mon Jul 7 09:47:43 2014 UTC
@@ -27,6 +27,42 @@

 // Flags: --allow-natives-syntax

+// Make sure we don't rely on functions patchable by monkeys.
+var call = Function.prototype.call.call.bind(Function.prototype.call)
+var observe = Object.observe;
+var getOwnPropertyNames = Object.getOwnPropertyNames
+var defineProperty = Object.defineProperty
+
+function clear(o) {
+ if (o === null || (typeof o !== 'object' && typeof o !== 'function')) return
+  clear(o.__proto__)
+  var properties = getOwnPropertyNames(o)
+  for (var i in properties) {
+    clearProp(o, properties[i])
+  }
+}
+
+function clearProp(o, name) {
+  var poisoned = {caller: 0, callee: 0, arguments: 0}
+  try {
+    var x = o[name]
+    o[name] = undefined
+    clear(x)
+  } catch(e) {} // assertTrue(name in poisoned) }
+}
+
+// Find intrinsics and null them out.
+var globals = Object.getOwnPropertyNames(this)
+var whitelist = {Promise: true, TypeError: true}
+for (var i in globals) {
+  var name = globals[i]
+ if (name in whitelist || name[0] === name[0].toLowerCase()) delete globals[i]
+}
+for (var i in globals) {
+  if (globals[i]) clearProp(this, globals[i])
+}
+
+
 var asyncAssertsExpected = 0;

 function assertAsyncRan() { ++asyncAssertsExpected }
@@ -43,7 +79,7 @@
 function assertAsyncDone(iteration) {
   var iteration = iteration || 0
   var dummy = {}
-  Object.observe(dummy,
+  observe(dummy,
     function() {
       if (asyncAssertsExpected === 0)
         assertAsync(true, "all")
@@ -777,13 +813,13 @@
   MyPromise.__proto__ = Promise
   MyPromise.defer = function() {
     log += "d"
-    return this.__proto__.defer.call(this)
+    return call(this.__proto__.defer, this)
   }

   MyPromise.prototype.__proto__ = Promise.prototype
   MyPromise.prototype.chain = function(resolve, reject) {
     log += "c"
-    return this.__proto__.__proto__.chain.call(this, resolve, reject)
+    return call(this.__proto__.__proto__.chain, this, resolve, reject)
   }

   log = ""

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to