inlineBuiltIns.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // Notes on running this script:
  6. // - rldirs.xml is set up in the following way so that this script is called 2 times:
  7. // - First, it's called for interpreted variant.
  8. // - Then, when it's called for dynapogo variant, jshost is called with: -args dynapogo.
  9. // - This script is not called for the default variant.
  10. // - Idea:
  11. // - Collect dynamic profile cache when called for interpreted variant.
  12. // - Use dynamic profle cache when called with -args dynapogo.
  13. // - Some tests cause bailout by passing different parameter to test function
  14. // as when dynamic profile cache was created.
  15. // - How to manually run/repro:
  16. // - jshost -nonative -dynamicprofilecache:inlineBuiltIns.dpl inlineBuiltIns.js
  17. // - jshost -forcenative -dynamicprofileinput:inlineBuiltIns.dpl inlineBuiltIns.js -args dynapogo
  18. // - also trying using -testtrace:bailout to make sure you get the bailouts.
  19. // TODO: change passing -args native to jshost and instead
  20. // add support to WScript to expose getFlagByString() for Js::ConfigFlagsTable flags and check for -native.
  21. if (typeof (WScript) != "undefined") {
  22. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js", "self");
  23. }
  24. var Runtime = {
  25. // returns true when this jshost is called with arguments: -args native
  26. get isDynapogo() {
  27. return WScript.Arguments.length > 0 && WScript.Arguments[0] == "dynapogo";
  28. }
  29. };
  30. var tests = {
  31. test01: {
  32. name: "Bailout on function = null",
  33. body: function () {
  34. function TestSin(x) {
  35. var r1 = Math.sin(x);
  36. return r1;
  37. };
  38. if (Runtime.isDynapogo) {
  39. var sinOrig = Math.sin; // back up Math.sin
  40. assert.throws(function() {
  41. Math.sin = null;
  42. var r = TestSin({});
  43. assert.isTrue(isNaN(r));
  44. }, TypeError);
  45. Math.sin = sinOrig; // restore Math.sin
  46. }
  47. else TestSin();
  48. }
  49. },
  50. test02: {
  51. name: "Bailout on function = object, not a function",
  52. body: function () {
  53. function TestSin(x) {
  54. var r1 = Math.sin(x);
  55. return r1;
  56. };
  57. if (Runtime.isDynapogo) {
  58. var sinOrig = Math.sin; // back up Math.sin
  59. assert.throws(function() {
  60. Math.sin = {};
  61. var r = TestSin({});
  62. assert.isTrue(isNaN(r));
  63. }, TypeError);
  64. Math.sin = sinOrig; // restore Math.sin
  65. }
  66. else TestSin();
  67. }
  68. },
  69. test03: {
  70. name: "Bailout on function = wrong function",
  71. body: function () {
  72. function TestSin(x) {
  73. var r1 = Math.sin(x);
  74. return r1;
  75. };
  76. var sinOrig = Math.sin; // back up Math.sin
  77. Math.sin = Math.cos;
  78. var r = TestSin({});
  79. assert.isTrue(isNaN(r));
  80. Math.sin = sinOrig; // restore Math.sin
  81. }
  82. },
  83. test04: {
  84. name: "Bailout on argument = string",
  85. body: function () {
  86. function TestSin(x) {
  87. var r1 = Math.sin(x);
  88. return r1;
  89. };
  90. var r = TestSin("string");
  91. assert.isTrue(isNaN(r));
  92. }
  93. },
  94. test05: {
  95. name: "Bailout on argument = object",
  96. body: function () {
  97. function TestSin(x) {
  98. var r1 = Math.sin(x);
  99. return r1;
  100. };
  101. var r = TestSin({});
  102. assert.isTrue(isNaN(r));
  103. }
  104. },
  105. test06: {
  106. name: "Bailout on 2nd argument = string",
  107. body: function () {
  108. function TestPow(x, y) {
  109. var r1 = Math.pow(x, y);
  110. return r1;
  111. };
  112. var r = TestPow(2, "string");
  113. assert.isTrue(isNaN(r));
  114. }
  115. },
  116. test07: {
  117. name: "Float/int type specialized argOuts which we restore at bailout",
  118. body: function () {
  119. // As long as there is no assert/crash, we are fine.
  120. (function() {
  121. var i = -8.1E+18;
  122. var r = Math.pow(1, Math.exp(Math.atan2(1, ((~i) - 737882964))));
  123. })();
  124. (function() {
  125. var e = 1;
  126. return Math.pow(e >> 1, 3.2)
  127. })();
  128. (function() {
  129. var e = 1;
  130. Math.atan2(1, Math.pow((e >>= 1), Math.tan((-1031883772 * Math.abs(-951135089)))));
  131. })();
  132. (function() {
  133. var ary = new Array();
  134. ary[0] = 0;
  135. Math.pow(1808815940.1, -ary[0]);
  136. })();
  137. (function() {
  138. return Math.pow(Math.sin(1), Math.pow(1, 1));
  139. })();
  140. (function() {
  141. var o = { x: 0 };
  142. var func0 = function()
  143. {
  144. Math.pow(1.1, o.x * -1);
  145. }
  146. Math.atan2(func0(), 1);
  147. })();
  148. }
  149. },
  150. test08: {
  151. name: "Bailout on argument after function copy-prop into InlineBuiltInStart",
  152. body: function () {
  153. for(var i in [0, 1])
  154. assert.isTrue(isNaN(Math.pow(Math.pow(/x/, 0.1), 0.1)));
  155. }
  156. },
  157. test09: {
  158. name: "Bailout (pre-op) on 2nd arg which is updated in the place of the call - make sure 1st arg is not updated",
  159. body: function() {
  160. var accumulator = "";
  161. var vOf = function vOf() { accumulator += "x"; return 3; }
  162. function testFunc() {
  163. var i = 1;
  164. do {
  165. // We need to make sure that we pass original value of i (== 1) as 1st arg.
  166. var x = Math.pow(i, Runtime.isDynapogo ? i = { valueOf: vOf } : 1);
  167. } while (vOf == undefined);
  168. }
  169. testFunc();
  170. if (Runtime.isDynapogo) {
  171. assert.areEqual("x", accumulator, "valueOf was called wrong number of times");
  172. }
  173. }
  174. },
  175. }; // tests.
  176. testRunner.runTests(tests);