StackArgsWithFormals.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. var actuals = new Array();
  6. var hasAllPassed = true;
  7. function verify(expected, testCase)
  8. {
  9. if(actuals.toString() != expected.toString())
  10. {
  11. print(testCase + " failed.");
  12. print("ACTUALS:");
  13. for(var item = 0; item < actuals.length; actuals++)
  14. {
  15. print(actuals[item]);
  16. }
  17. print("EXPECTED");
  18. for(var item = 0; item < expected.length; expected++)
  19. {
  20. print(expected[item]);
  21. }
  22. hasAllPassed = false;
  23. }
  24. actuals = [];
  25. }
  26. //Test1 : Tests Optimization with bailout.
  27. var shouldBailout = false;
  28. var test1 = function (argMath3)
  29. {
  30. if(shouldBailout)
  31. {
  32. arguments[0] = 4;
  33. for(var o in arguments)
  34. {
  35. actuals.push(o);
  36. }
  37. }
  38. actuals.push(argMath3);
  39. };
  40. test1(10,2); // Interpreter
  41. shouldBailout = true;
  42. test1(10,2); // full jit
  43. verify([10,0,1,4], "TEST 1");
  44. //Test2: Test Inlinee Arguments optimization with StackArgsWithFormals
  45. var inner_test2 = function(argMath6=1) {
  46. return argMath6;
  47. };
  48. function test2(a,b,c) {
  49. return inner_test2.apply({}, arguments);
  50. }
  51. actuals.push(test2(1,2,3));
  52. actuals.push(test2(4,5,6));
  53. verify([1,4], "TEST 2");
  54. //Test3: Non Simple Parameter List
  55. function test3(a, b =2, c = 5)
  56. {
  57. if(shouldBailout)
  58. {
  59. arguments[2] = 10;
  60. var result = arguments[2] + a;
  61. actuals.push(result);
  62. return;
  63. }
  64. actuals.push(a+b+arguments[1]);
  65. }
  66. shouldBailout = false;
  67. test3(1,2,3,4);
  68. test3(5,6,7,8);
  69. shouldBailout = true;
  70. test3(10,11,12,14);
  71. verify([5,17,20], "TEST 3");
  72. //Test4: With Rest argument
  73. function test4(a,b,c,d,e) {
  74. inner_test4(a,b,c,d);
  75. }
  76. var inner_test4 = function (a, b, ...argArr0) {
  77. if (arguments.length > 1) {
  78. actuals.push(a + b);
  79. actuals.push(argArr0[0])
  80. }
  81. };
  82. test4(1,2,3,4);
  83. test4(5,6,7,8);
  84. verify([3,3,11,7], "TEST 4");
  85. //Test5: Inside Loop and Bails out due to out of range of actuals access
  86. function test5(a,b,c=10)
  87. {
  88. var result = 0;
  89. for(var i = 0; i < 5; i++)
  90. {
  91. result += arguments[i];
  92. }
  93. result += (a+c);
  94. actuals.push(result);
  95. }
  96. test5(1,2,3,4,5,6);
  97. test5(1,2,3);// Bail out here.
  98. verify([19, NaN], "TEST 5");
  99. //Test6 : Using strict mode
  100. function test6(a,b,c)
  101. {
  102. 'use strict';
  103. actuals.push(arguments[0] + arguments[2] + a + b);
  104. }
  105. test6(1,2,3);
  106. test6(4,5,6,7);
  107. verify([7,19], "TEST 6");
  108. //Test7 : Use strict with bail out
  109. function test7(a,b,c)
  110. {
  111. 'use strict';
  112. if(shouldBailout)
  113. {
  114. arguments[0] = 4;
  115. }
  116. actuals.push(a);
  117. }
  118. shouldBailout = false;
  119. test7(11,12,13);
  120. shouldBailout = true;
  121. test7(10,20,30);
  122. verify([11,10], "TEST 7");
  123. //Test 8 : for-in statement
  124. function test8(a,b)
  125. {
  126. for(a in [10,20])
  127. {
  128. actuals.push(a);
  129. actuals.push(arguments[0]);
  130. }
  131. }
  132. test8(1,2);
  133. test8('x','y');
  134. verify([0,0,1,1,0,0,1,1], "TEST 8");
  135. //Test 9 : Array Destructuring.
  136. function test9(a,b)
  137. {
  138. [a,b] = [10,20];
  139. actuals.push(a + arguments[0]);
  140. }
  141. test9(1,2);
  142. test9('x','y');
  143. verify([20,20], "TEST 9");
  144. //Test 10 : Object Destructuring.
  145. function test10(a,b)
  146. {
  147. ({a:a} = {a:10.5})
  148. actuals.push(a + arguments[0]);
  149. }
  150. test10(1,2);
  151. test10('x','y');
  152. verify([21,21], "TEST 10");
  153. //Test 11 : for-of statement
  154. function test11(a,b)
  155. {
  156. for([a] of [[10],[20]])
  157. {
  158. actuals.push(a + arguments[0]);
  159. }
  160. }
  161. test11(1,2);
  162. test11('x','y');
  163. verify([20,40,20,40], "TEST 11");
  164. var obj12 = { method1: function () {} };
  165. function test12_1(arg1) {
  166. this.prop1 = arg1;
  167. obj12.method1.apply(obj12, arguments);
  168. }
  169. function test12() {
  170. new test12_1(-{});
  171. }
  172. test12();
  173. test12();
  174. verify([], "TEST 12");
  175. function test13(a) {
  176. actuals.push(typeof arguments[1]);
  177. }
  178. test13(1,2);
  179. test13({}, {});
  180. verify(["number", "object"], "TEST 13");
  181. function test14(a) {
  182. actuals.push(arguments[1]);
  183. function test14_nested1()
  184. {
  185. }
  186. }
  187. test14(1,2);
  188. test14(4,5);
  189. verify([2,5], "TEST 14");
  190. function test15(a){
  191. actuals.push(arguments[0])
  192. var b = 10;
  193. function test15_nested1()
  194. {
  195. actuals.push(b);
  196. };
  197. test15_nested1();
  198. }
  199. test15(1);
  200. test15(2);
  201. verify([1, 10, 2, 10], "TEST 15");
  202. function test16(a,b){
  203. var c = 10;
  204. if(shouldBailout)
  205. {
  206. actuals.push(arguments[1]);
  207. }
  208. actuals.push(c);
  209. function test16_nested1()
  210. {
  211. }
  212. }
  213. shouldBailout = false;
  214. test16(1,2);
  215. shouldBailout = true;
  216. test16(3,4);
  217. verify([10, 4, 10], "TEST 16");
  218. function test17(a){
  219. var b = 20;
  220. actuals.push(arguments[0.1*10]);
  221. function test17_nested1(){
  222. actuals.push(b);
  223. }
  224. test17_nested1();
  225. }
  226. test17(1);
  227. test17(2);
  228. verify([undefined, 20, undefined, 20], "TEST 17");
  229. function test18()
  230. {
  231. eval('function inner(a,...b) {actuals.push(a + arguments[0]);}; inner(1,2); inner(3,4);');
  232. }
  233. test18();
  234. verify([2, 6], "TEST18");
  235. function test19(a, b)
  236. {
  237. 'use strict';
  238. b++;
  239. actuals.push(arguments[0] + b);
  240. }
  241. test19(1, 2);
  242. test19(3, 4);
  243. verify([4, 8], "TEST 19");
  244. function test20(v16) {
  245. while ({}.prop0) {
  246. var v17 = 0;
  247. function test20_inner() {
  248. v17;
  249. }
  250. arguments;
  251. }
  252. }
  253. test20();
  254. test20();
  255. if(hasAllPassed)
  256. {
  257. print("PASSED");
  258. }