Invariants.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // Everything inside the loop except for the assignment to 'a' should get hoisted
  6. function test0() {
  7. var a;
  8. for(var i = 0; i < 1; ++i)
  9. a = (0x40000000 | 0) % 3;
  10. return a;
  11. }
  12. WScript.Echo("test0: " + test0());
  13. // The '-f' bails out, and the Neg is hoisted outside the loop. The multiplication is not type-specialized, so 'f' is converted
  14. // to var and that conversion is also hoisted outside the loop. The conversion to var happens after the bailout, so the value of
  15. // the var sym for 'f' is not valid at the time of bailout. So, bailout should use the int sym for 'f' to restore its value.
  16. function test1() {
  17. var c = 1;
  18. var f = (1 !== 0);
  19. f = f & 21037030;
  20. var g;
  21. for(var __loopvar1 = 0; c < (g = (((-f) ? (f * i32[(1) % 256]) : 1))) && __loopvar1 < 3; c++ + __loopvar1++) {
  22. }
  23. return g;
  24. }
  25. WScript.Echo("test1: " + test1());
  26. // In 'o.p &= 1', 'o' is converted to var. 'o' was const-propped with '0' though, so an LdC_A_I4 is created and hoisted to the
  27. // outer loop's landing pad. LdC_A_I4 should be considered a type-spec conversion here, so while making the var version of the
  28. // sym live, it should also preserve the int version of the sym as live.
  29. function test2() {
  30. for(var i = 0; i < 1; ++i) {
  31. var o = 0;
  32. for(var j = 0; j < 1; ++j)
  33. o.p &= 1;
  34. }
  35. }
  36. WScript.Echo("test2: " + test2());
  37. // When hoisting an invariant with a new dst, value type of the old dst should be copied over to the new dst.
  38. function test3() {
  39. var func1 = function () {
  40. return '6' + 'b!%$' + 'caller';
  41. };
  42. var func2 = function () {
  43. return '6' + 'b!%$' + 'caller';
  44. };
  45. var ary = Array();
  46. func1();
  47. for (var v1 = 0; v1 < 8; v1++) {
  48. WScript.Echo(func2());
  49. }
  50. WScript.Echo('subset_of_ary = ' + ary.slice());
  51. }
  52. test3();