for-loop.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. function print(x) { WScript.Echo(x) }
  6. let a = 'global';
  7. for (let a = 'for'; f = function() { a += ' loop' }; ) {
  8. f();
  9. print(a);
  10. break;
  11. }
  12. print(a);
  13. for (let a in this) {
  14. let f = function() { a = 'for-in loop'; };
  15. f();
  16. print(a);
  17. break;
  18. }
  19. print(a);
  20. try { eval('for (let a = 123 in this) { }'); print('fail'); } catch (e) { print(e); }
  21. try { eval('for (const a = 123 in this) { }'); print('fail'); } catch (e) { print(e); }
  22. try { eval('function foo() { for (let a = 123 in this) { } } foo();'); print('fail'); } catch (e) { print(e); }
  23. try { eval('function foo() { for (const a = 123 in this) { } } foo();'); print('fail'); } catch (e) { print(e); }
  24. try { eval('function foo() { { for (var a = 123 in []) { } let a; } } foo();'); print('fail'); } catch (e) { print(e); }
  25. function test3() {
  26. eval('');
  27. v2;
  28. let v1;
  29. for (let v2; false;) {
  30. // this var should get a slot before v1 via EnsureScopeSlot
  31. // to ensure that the slot order is the same as the SlotArray
  32. var v2 = 0;
  33. }
  34. }
  35. test3();
  36. // Should allow (implicit) initialization of const in for-in/for-of
  37. function for_in() {
  38. for (const x in {a:'a',b:'b'}) {
  39. WScript.Echo(x);
  40. }
  41. }
  42. for_in();
  43. function for_of() {
  44. for (const x of ['a', 'b']) {
  45. WScript.Echo(x);
  46. }
  47. }
  48. for_of();
  49. // Should not allow const without initializer in standard for loop header
  50. try { eval('for (const x; x < 0;) { WScript.Echo(x); }'); } catch (e) { print(e); }