functions-in-tree-order-non-strict.js 2.9 KB

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