functions-in-tree-order-strict.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. // Note: This must be checked at script level because that is the only place
  3. // where function order is visible. We introduce some test(...) with
  4. // names to make sure the file does have some tests and a suite.
  5. // Note: In strict mode we expect almost the same result except that the
  6. // functions in blocks do not get hoisted up.
  7. // Only the indirect eval copy gets the global var environment.
  8. const evalViaArrow = x => eval(x);
  9. const evalRef = eval;
  10. const expectedBeforeEval = "3478CDHIOP";
  11. const expectedAfterEval = "3478CDHIOP";
  12. const expectedAfterEvalRef = "3478CDHIOPKJL";
  13. const expectOrderToBe = expectedOrder => {
  14. const currentOrder = Object.getOwnPropertyNames(this)
  15. .filter(s => s.length === 2 && s[0] === "f")
  16. .map(s => s[1])
  17. .join("");
  18. expect(currentOrder).toBe(expectedOrder);
  19. };
  20. test("in strict mode: function order should be in tree order and nothing in eval should be included, in strict mode functions should not be hoisted", () => {
  21. expectOrderToBe(expectedBeforeEval);
  22. });
  23. {
  24. function f1() {}
  25. expectOrderToBe(expectedBeforeEval);
  26. function f2() {}
  27. }
  28. expectOrderToBe(expectedBeforeEval);
  29. function f3() {}
  30. expectOrderToBe(expectedBeforeEval);
  31. function f4() {}
  32. expectOrderToBe(expectedBeforeEval);
  33. {
  34. function f5() {}
  35. function f6() {}
  36. }
  37. function f7() {}
  38. function f8() {}
  39. expectOrderToBe(expectedBeforeEval);
  40. eval(`
  41. expectOrderToBe(expectedAfterEval);
  42. function f9() {}
  43. {
  44. function fA() {}
  45. }
  46. function fB() {}
  47. expectOrderToBe(expectedAfterEval);
  48. `);
  49. expectOrderToBe(expectedAfterEval);
  50. function fC() {}
  51. function fD() {}
  52. expectOrderToBe(expectedAfterEval);
  53. // This eval does not do anything because it goes via a function, this means
  54. // its parent environment is not the global environment so it does not have
  55. // a global var environment and does not put the functions on `this`.
  56. evalViaArrow(`
  57. expectOrderToBe(expectedAfterEval);
  58. function fE() {}
  59. {
  60. expectOrderToBe(expectedAfterEval);
  61. function fF() {}
  62. }
  63. function fG() {}
  64. expectOrderToBe(expectedAfterEval);
  65. `);
  66. test("in strict mode: function order should be in tree order, functions in eval should be in order but at the back, in strict mode functions should not be hoisted", () => {
  67. expectOrderToBe(expectedAfterEval);
  68. });
  69. function fH() {}
  70. function fI() {}
  71. expectOrderToBe(expectedAfterEval);
  72. // This is an indirect eval, but still has the global scope as immediate
  73. // parent so it does influence the global `this`.
  74. evalRef(`
  75. expectOrderToBe(expectedAfterEvalRef);
  76. console.log(2, JSON.stringify(Object.getOwnPropertyNames(this).filter(s => s.length === 2)));
  77. function fJ() {}
  78. {
  79. expectOrderToBe(expectedAfterEvalRef);
  80. function fK() {}
  81. }
  82. function fL() {}
  83. expectOrderToBe(expectedAfterEvalRef);
  84. `);
  85. {
  86. function fM() {}
  87. function fN() {}
  88. }
  89. test("in strict mode: function order should be in tree order, functions in evalRef should be in order but at the back, in strict mode functions should not be hoisted", () => {
  90. expectOrderToBe(expectedAfterEvalRef);
  91. });
  92. function fO() {}
  93. function fP() {}
  94. {
  95. function fQ() {}
  96. {
  97. expectOrderToBe(expectedAfterEvalRef);
  98. }
  99. function fR() {}
  100. }
  101. expectOrderToBe(expectedAfterEvalRef);