enviornment of child function created by loadstring lua -
why calls f() , g() give different results? both inherit environment parent function, run, f() not change j.
mystring = "print('i,j in mystring before setting', i, j);\ = 'fi'; j ='fj'\ print('i,j in mystring after setting', i,j)" j = 1 print('j initial value', j) function run(i) = 'i' print('i initial value', i) f = loadstring(mystring) if not f print('load failed') else print('=== load ok, execute') f() print('=== end of execution') end print('i,j after f()', i,j) g = function() = 'gi'; j = 'gj' end g() print('i,j after g()', i,j) end run(i)
results:
j initial value 1 initial value === load ok, execute i,j in mystring before setting nil 1 i,j in mystring after setting fi fj === end of execution i,j after f() fj i,j after g() gi gj
note different values of after f() , g() calls.
you referring 2 separate variables.
when call f() it's accessing global i, whereas accessing run's argument in g().
Comments
Post a Comment