Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: test/mjsunit/es6/promises.js

Issue 366103005: Avoid brittle use of .bind in Promise.all (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Thorough test & rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/promise.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --allow-natives-syntax 28 // Flags: --allow-natives-syntax
29 29
30 // Make sure we don't rely on functions patchable by monkeys.
31 var call = Function.prototype.call.call.bind(Function.prototype.call)
32 var observe = Object.observe;
33 var getOwnPropertyNames = Object.getOwnPropertyNames
34 var defineProperty = Object.defineProperty
35
36 function clear(o) {
37 if (o === null || (typeof o !== 'object' && typeof o !== 'function')) return
38 clear(o.__proto__)
39 var properties = getOwnPropertyNames(o)
40 for (var i in properties) {
41 clearProp(o, properties[i])
42 }
43 }
44
45 function clearProp(o, name) {
46 var poisoned = {caller: 0, callee: 0, arguments: 0}
47 try {
48 var x = o[name]
49 o[name] = undefined
50 clear(x)
51 } catch(e) {} // assertTrue(name in poisoned) }
52 }
53
54 // Find intrinsics and null them out.
55 var globals = Object.getOwnPropertyNames(this)
56 var whitelist = {Promise: true, TypeError: true}
57 for (var i in globals) {
58 var name = globals[i]
59 if (name in whitelist || name[0] === name[0].toLowerCase()) delete globals[i]
60 }
61 for (var i in globals) {
62 if (globals[i]) clearProp(this, globals[i])
63 }
64
65
30 var asyncAssertsExpected = 0; 66 var asyncAssertsExpected = 0;
31 67
32 function assertAsyncRan() { ++asyncAssertsExpected } 68 function assertAsyncRan() { ++asyncAssertsExpected }
33 69
34 function assertAsync(b, s) { 70 function assertAsync(b, s) {
35 if (b) { 71 if (b) {
36 print(s, "succeeded") 72 print(s, "succeeded")
37 } else { 73 } else {
38 %AbortJS(s + " FAILED!") // Simply throwing here will have no effect. 74 %AbortJS(s + " FAILED!") // Simply throwing here will have no effect.
39 } 75 }
40 --asyncAssertsExpected 76 --asyncAssertsExpected
41 } 77 }
42 78
43 function assertAsyncDone(iteration) { 79 function assertAsyncDone(iteration) {
44 var iteration = iteration || 0 80 var iteration = iteration || 0
45 var dummy = {} 81 var dummy = {}
46 Object.observe(dummy, 82 observe(dummy,
47 function() { 83 function() {
48 if (asyncAssertsExpected === 0) 84 if (asyncAssertsExpected === 0)
49 assertAsync(true, "all") 85 assertAsync(true, "all")
50 else if (iteration > 10) // Shouldn't take more. 86 else if (iteration > 10) // Shouldn't take more.
51 assertAsync(false, "all") 87 assertAsync(false, "all")
52 else 88 else
53 assertAsyncDone(iteration + 1) 89 assertAsyncDone(iteration + 1)
54 } 90 }
55 ) 91 )
56 dummy.dummy = dummy 92 dummy.dummy = dummy
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 function(r) { log += "r" + r; reject(r) } 806 function(r) { log += "r" + r; reject(r) }
771 ) 807 )
772 }) 808 })
773 promise.__proto__ = MyPromise.prototype 809 promise.__proto__ = MyPromise.prototype
774 return promise 810 return promise
775 } 811 }
776 812
777 MyPromise.__proto__ = Promise 813 MyPromise.__proto__ = Promise
778 MyPromise.defer = function() { 814 MyPromise.defer = function() {
779 log += "d" 815 log += "d"
780 return this.__proto__.defer.call(this) 816 return call(this.__proto__.defer, this)
781 } 817 }
782 818
783 MyPromise.prototype.__proto__ = Promise.prototype 819 MyPromise.prototype.__proto__ = Promise.prototype
784 MyPromise.prototype.chain = function(resolve, reject) { 820 MyPromise.prototype.chain = function(resolve, reject) {
785 log += "c" 821 log += "c"
786 return this.__proto__.__proto__.chain.call(this, resolve, reject) 822 return call(this.__proto__.__proto__.chain, this, resolve, reject)
787 } 823 }
788 824
789 log = "" 825 log = ""
790 var p1 = new MyPromise(function(resolve, reject) { resolve(1) }) 826 var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
791 var p2 = new MyPromise(function(resolve, reject) { reject(2) }) 827 var p2 = new MyPromise(function(resolve, reject) { reject(2) })
792 var d3 = MyPromise.defer() 828 var d3 = MyPromise.defer()
793 assertTrue(d3.promise instanceof Promise, "subclass/instance") 829 assertTrue(d3.promise instanceof Promise, "subclass/instance")
794 assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3") 830 assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3")
795 assertTrue(log === "nx1nr2dn", "subclass/create") 831 assertTrue(log === "nx1nr2dn", "subclass/create")
796 832
(...skipping 19 matching lines...) Expand all
816 Promise.all([11, Promise.accept(12), 13, MyPromise.accept(14), 15, 16]) 852 Promise.all([11, Promise.accept(12), 13, MyPromise.accept(14), 15, 16])
817 assertTrue(log === "nx14n", "subclass/all/arg") 853 assertTrue(log === "nx14n", "subclass/all/arg")
818 854
819 log = "" 855 log = ""
820 MyPromise.all([21, Promise.accept(22), 23, MyPromise.accept(24), 25, 26]) 856 MyPromise.all([21, Promise.accept(22), 23, MyPromise.accept(24), 25, 26])
821 assertTrue(log === "nx24nnx21nnx23nnnx25nnx26n", "subclass/all/self") 857 assertTrue(log === "nx24nnx21nnx23nnnx25nnx26n", "subclass/all/self")
822 })(); 858 })();
823 859
824 860
825 assertAsyncDone() 861 assertAsyncDone()
OLDNEW
« no previous file with comments | « src/promise.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698