SIMION®
The field and particle trajectory simulator
Industry standard charged particle optics software
Refine your design: model > simulate > view > analyze > program > optimize it.
About | Documentation | Community/Support | Downloads | Ordering

SIMION 8.0 Issues/Bugs/Enhancements

Filter issues by query:
Issue I490 [80,lua,enhancement,possiblyresolved]
  Improved detection of undefined variables ("checkglobals")

  In Lua programs, typos in variable names can be hard to spot because,
  in general, Lua will not complain that a variable is undefined.
  For example, consider this program that defines two functions:

    function f(x) print(X) end
    function g(x) print(X + 1) end

  Lua gives no error when loading this code.  The first two lines
  might be wrong (e.g. "x" mistyped as "X") or it might not be (maybe
  X is some other global variable).  In fact, Lua has no way of
  knowing if the code is wrong.  The reason is that if a variable is
  not recognized by Lua as a local variable (e.g. by static
  declaration of the variable using a "local" keyword, "adjustable"
  keyword, or function parameter definition), the variable is instead
  interpreted as a global variable (as is the case for "X").  Now,
  whether a global variable is defined is not as easy to determine or
  describe.  X has the value t['X'] where t is the "environment table"
  of the currently running function.  X always has a value, though it
  is probably nil if X were a typo.  We might interpret X being nil as
  X being undefined, but whether X is nil can only be determined at
  run-time.  For example:

            -- X is "undefined"
    f(10)   -- print nil
    X = 2   -- X is defined
    f(10)   -- prints 2
    X = nil -- X is "undefined" again
    f(10)   -- prints nil

  Even the above runs without error.  When X is nil, print(X) becomes
  print(nil), and it is valid to print a nil value.  However, consider
  calling the function g:

    g(10)

  This fails with the error "attempt to perform arithmetic on global
  'X' (a nil value)".  The reasons is that print(X + 1) becomes
  print(nil + 1), and it is invalid to add nil to a number.  The error
  is not observed, however, until the code "nil + 1" actually
  executes.

  Now, is there a way to detect undefined variables more preemptively?
  A number of methods have been described (
  http://lua-users.org/wiki/DetectingUndefinedVariables ).  Probably
  the most suitable for most purposes is the "checkglobals" function,
  which was added to SIMION 8.0.5-TEST12.

  Assuming you have 8.0.5-TEST12 or above installed, you may add
  the checkglobals() function call to the bottom of your code to validating
  global accesses:

    function f(x) print(X) end
    function g(x) print(X + 1) end    
    checkglobals()

  Assuming X is nil when checkglobals() is executed, the above code
  now raises the error 'test.lua:3: accessed undefined variable "X" at
  line 1'.

  If you tend to avoid declaring global variables (instead always using
  locals), you might even move the checkglobals function to the top
  of your program to provide slightly stronger globals checking:

    checkglobals()
    function f2(x) print(x) end
    function g2(x) print(x + 1) end    

  The above fails with error 'test1.lua: accessed undefined variable
  "f2" at line 2' since line 2 attempted to create a new global variable
  f2.  There are a few ways to address this error now:

    -- use locals only
    checkglobals()
    local function f2(x) print(x) end
    local function g2(x) print(x + 1) end    

    -- create globals explicitly through the environment table.
    local env = getfenv()
    function env.f2(x) print(x) end
    function env.g2(x) print(x + 1) end    

  Resolved in 8.0.5-TEST12.

Any comments on this web page? (will be sent to IMI Adaptas SIMION Support)
[Optional] Your name: email: phone: