bind.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function write(v) { WScript.Echo(v + ""); }
  6. function Verify(expression, actualValue, expectedValue)
  7. {
  8. if (expectedValue != actualValue)
  9. {
  10. write("Failed: " + expression + ". Exp:" + expectedValue + " Act:" + actualValue);
  11. }
  12. else
  13. {
  14. write("PASS: " + expression + ":" + expectedValue);
  15. }
  16. }
  17. function PropertyExists(obj, propName)
  18. {
  19. return obj.hasOwnProperty(propName);
  20. }
  21. function check(value)
  22. {
  23. write(value);
  24. }
  25. function IncrVal()
  26. {
  27. check("IncrVal:: " + this.val + " args.length : " + arguments.length);
  28. this.val++;
  29. return this.val + " " + arguments.length;
  30. }
  31. // Global Variables
  32. var val = 100;
  33. var fGlobalThis;
  34. // Tests
  35. //-----------------------------------------------------------------------------------------------------------
  36. // Global Object tests
  37. fGlobalThis = IncrVal.bind();
  38. Verify("global object", fGlobalThis(), "101 0");
  39. Verify("global object", fGlobalThis(10), "102 1");
  40. Verify("global object", fGlobalThis(10,20), "103 2");
  41. val = 100;
  42. fGlobalThis = IncrVal.bind(this);
  43. Verify("global object", fGlobalThis(), "101 0");
  44. Verify("global object", fGlobalThis(10), "102 1");
  45. Verify("global object", fGlobalThis(10,20), "103 2");
  46. val = 100;
  47. fGlobalThis = IncrVal.bind(this, 50);
  48. Verify("global object", fGlobalThis(), "101 1");
  49. Verify("global object", fGlobalThis(10), "102 2");
  50. Verify("global object", fGlobalThis(10,20), "103 3");
  51. val = 100;
  52. fGlobalThis = IncrVal.bind(this, 50, 51);
  53. Verify("global object", fGlobalThis(), "101 2");
  54. Verify("global object", fGlobalThis(10), "102 3");
  55. Verify("global object", fGlobalThis(10,20), "103 4");
  56. //-----------------------------------------------------------------------------------------------------------
  57. function testGlobalBinding()
  58. {
  59. check("Testing Bind to global object");
  60. val = 100;
  61. var f1 = IncrVal.bind();
  62. Verify("GlobalObject length", f1.length, 0);
  63. Verify("global object", f1(), "101 0");
  64. Verify("global object", f1(10), "102 1");
  65. Verify("global object", f1(10,20), "103 2");
  66. }
  67. function testLocalBinding()
  68. {
  69. check("Testing Bind to local object");
  70. var objWithVal = { val : 200 }
  71. var fLocal = IncrVal.bind(objWithVal);
  72. Verify("Local length", fLocal.length, 0);
  73. Verify("Local object1", fLocal(), "201 0");
  74. Verify("Local object2", fLocal(10), "202 1");
  75. Verify("Local object3", fLocal(10,20), "203 2");
  76. objWithVal = { val : 200 }
  77. fLocal = IncrVal.bind(objWithVal, 50);
  78. Verify("local length", fLocal.length, 0);
  79. Verify("local object", fLocal(), "201 1");
  80. Verify("local object", fLocal(10), "202 2");
  81. Verify("local object", fLocal(10,20), "203 3");
  82. objWithVal = { val : 200 }
  83. fLocal = IncrVal.bind(objWithVal, 50, 51);
  84. Verify("local length", fLocal.length, 0);
  85. Verify("local object", fLocal(), "201 2");
  86. Verify("local object", fLocal(10), "202 3");
  87. Verify("local object", fLocal(10,20), "203 4");
  88. }
  89. function testBoundLength()
  90. {
  91. check("Testing Length");
  92. var obj = new Object();
  93. function f0() { }
  94. function f1(x1) { }
  95. function f2(x1,x2) { }
  96. function f3(x1,x2,x3) { }
  97. function f4(x1,x2,x3,x4) { }
  98. function f5(x1,x2,x3,x4,x5) { }
  99. var bf0 = f0.bind(); Verify("1 Length0 ", bf0.length, 0);
  100. var bf1 = f1.bind(); Verify("1 Length1 ", bf1.length, 1);
  101. var bf2 = f2.bind(); Verify("1 Length2 ", bf2.length, 2);
  102. var bf3 = f3.bind(); Verify("1 Length3 ", bf3.length, 3);
  103. var bf4 = f4.bind(); Verify("1 Length4 ", bf4.length, 4);
  104. var bf5 = f5.bind(); Verify("1 Length5 ", bf5.length, 5);
  105. bf0 = f0.bind(obj); Verify("2 Length0 ", bf0.length, 0);
  106. bf1 = f1.bind(obj); Verify("2 Length1 ", bf1.length, 1);
  107. bf2 = f2.bind(obj); Verify("2 Length2 ", bf2.length, 2);
  108. bf3 = f3.bind(obj); Verify("2 Length3 ", bf3.length, 3);
  109. bf4 = f4.bind(obj); Verify("2 Length4 ", bf4.length, 4);
  110. bf5 = f5.bind(obj); Verify("2 Length5 ", bf5.length, 5);
  111. bf0 = f0.bind(obj, 10); Verify("3 Length0 ", bf0.length, 0);
  112. bf1 = f1.bind(obj, 10); Verify("3 Length1 ", bf1.length, 0);
  113. bf2 = f2.bind(obj, 10); Verify("3 Length2 ", bf2.length, 1);
  114. bf3 = f3.bind(obj, 10); Verify("3 Length3 ", bf3.length, 2);
  115. bf4 = f4.bind(obj, 10); Verify("3 Length4 ", bf4.length, 3);
  116. bf5 = f5.bind(obj, 10); Verify("3 Length5 ", bf5.length, 4);
  117. }
  118. function testConstruction()
  119. {
  120. check("Testing Construction");
  121. function sum(a,b) { this.r = a + b; return this;}
  122. var obj = new Object();
  123. var add10 = sum.bind(obj, 10);
  124. var res = new add10(20);
  125. Verify("Construction ", res.r, 30);
  126. }
  127. var x = 20;
  128. var y = 30;
  129. function testProto()
  130. {
  131. function add()
  132. {
  133. return this.x + this.y;
  134. }
  135. Verify("Add Test", add(), 50);
  136. var o = { x: 5, y: 6};
  137. var f = add.bind(o);
  138. Verify("f Test", f(), 11);
  139. var f2 = new f();
  140. Verify("Proto Test", add.prototype.isPrototypeOf(f2), true);
  141. // Test toString inline cache behavior when a bound function has a non-typical prototype
  142. var a = decodeURIComponent.bind().toString;
  143. Verify("Proto toString Test", Function.prototype.bind(), '[object Function]');
  144. }
  145. function testConstruction1()
  146. {
  147. function Point(x,y)
  148. {
  149. this.x = x;
  150. this.y = y;
  151. }
  152. var p0 = Point.bind();
  153. var r0 = new p0(0, 1);
  154. Verify("TestConstruction0 x", r0.x, 0);
  155. Verify("TestConstruction0 y", r0.y, 1);
  156. var o1 = new Object();
  157. var p1 = Point.bind(o1);
  158. var r1 = new p1(100, 101);
  159. Verify("TestConstruction1 x", r1.x, 100);
  160. Verify("TestConstruction1 y", r1.y, 101);
  161. var o2 = new Object();
  162. var p2 = Point.bind(o2, 200);
  163. var r2 = new p2(201);
  164. Verify("TestConstruction2 x", r2.x, 200);
  165. Verify("TestConstruction2 y", r2.y, 201);
  166. var o3 = new Object();
  167. var p3 = Point.bind(o3, 300, 301);
  168. var r3 = new p3();
  169. Verify("TestConstruction3 x", r3.x, 300);
  170. Verify("TestConstruction3 y", r3.y, 301);
  171. }
  172. testGlobalBinding();
  173. testLocalBinding();
  174. testBoundLength();
  175. testConstruction();
  176. testProto();
  177. testConstruction1();