1 Ucode implements function scoping, make sure that let variables are 2 invisible outside of the function scope. 3 4 -- Expect stdout -- 5 a_global=true 6 a_local=true 7 8 b_global=true 9 b_local=false 10 11 c_global=true 12 c_local=false 13 14 15 When seting a nonlocal variable, it is set in the nearest parent 16 scope containing the variable or in the root scope if the variable 17 was not found. 18 19 x=2 20 y= 21 z=1 22 23 24 Variables implicitly declared by for-in or counting for loops follow the same 25 scoping rules. 26 27 inner2 f_a=3 28 inner2 f_b= 29 inner2 f_c=3 30 inner2 f_d= 31 inner2 f_e=3 32 33 inner f_a=3 34 inner f_b= 35 inner f_c=3 36 inner f_d= 37 inner f_e=3 38 39 outer f_a=3 40 outer f_b= 41 outer f_c= 42 outer f_d= 43 outer f_e=3 44 -- End -- 45 46 -- Testcase -- 47 {% 48 a_global = true; 49 let a_local = true; 50 51 function test() { 52 b_global = true; 53 let b_local = true; 54 55 function test2() { 56 c_global = true; 57 let c_local = true; 58 } 59 60 test2(); 61 } 62 63 test(); 64 -%} 65 66 a_global={{ !!a_global }} 67 a_local={{ !!a_local }} 68 69 b_global={{ !!b_global }} 70 b_local={{ !!b_local }} 71 72 c_global={{ !!c_global }} 73 c_local={{ !!c_local }} 74 75 76 When seting a nonlocal variable, it is set in the nearest parent 77 scope containing the variable or in the root scope if the variable 78 was not found. 79 80 {% 81 x = 1; 82 83 function scope1() { 84 x = 2; 85 let y; 86 87 function scope2() { 88 // this does not set "y" in the root scope but overwrites the 89 // variable declared in the "scope1" function scope. 90 y = 2; 91 92 // this sets "z" in the root scope because it was not declared 93 // anywhere yet 94 z = 1; 95 } 96 97 scope2(); 98 } 99 100 scope1(); 101 -%} 102 103 x={{ x }} 104 y={{ y }} 105 z={{ z }} 106 107 108 Variables implicitly declared by for-in or counting for loops follow the same 109 scoping rules. 110 111 {% 112 function scope3() { 113 // f_a is not declared local and be set in the root scope 114 for (f_a = 1; f_a < 3; f_a++) 115 ; 116 117 for (let f_b = 1; f_b < 3; f_b++) 118 ; 119 120 let f_c; 121 122 function scope4() { 123 // f_c is not declared local but declared in the parent scope, it 124 // will be set there 125 for (f_c in [1, 2, 3]) 126 ; 127 128 for (let f_d in [1, 2, 3]) 129 ; 130 131 // f_e is not declared, it will be set in the root scope 132 for (f_e in [1, 2, 3]) 133 ; 134 135 print("inner2 f_a=", f_a, "\n"); 136 print("inner2 f_b=", f_b, "\n"); 137 print("inner2 f_c=", f_c, "\n"); 138 print("inner2 f_d=", f_d, "\n"); 139 print("inner2 f_e=", f_e, "\n"); 140 print("\n"); 141 } 142 143 scope4(); 144 145 print("inner f_a=", f_a, "\n"); 146 print("inner f_b=", f_b, "\n"); 147 print("inner f_c=", f_c, "\n"); 148 print("inner f_d=", f_d, "\n"); 149 print("inner f_e=", f_e, "\n"); 150 print("\n"); 151 } 152 153 scope3(); 154 155 print("outer f_a=", f_a, "\n"); 156 print("outer f_b=", f_b, "\n"); 157 print("outer f_c=", f_c, "\n"); 158 print("outer f_d=", f_d, "\n"); 159 print("outer f_e=", f_e, "\n"); 160 %} 161 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt