Declaring Variables; Passing Context Between Lua Event Tasks - HMS Networks IXXAT CAN@net NT User Manual

Table of Contents

Advertisement

Description of Ixxat Lua ADK
2.4.3

Declaring Variables

Two types of data can be assigned to Lua variables:
values such as numbers, booleans, and strings
references such as functions, tables, and user data
For example, if the contents of variable a are assigned to variable b, there is a difference
between values and references. In case of a value, the content of a is copied into b. In case of a
reference, both a and b now refer to the same object. No content is copied. The Ixxat Lua ADK
provides the function table.copy() which returns a deep copy of the provided table
parameters.
In standard Lua all variables can be classified as globals or locals. By default, any variable
that is referenced and not previously declared as local is global. Global variables persist in
the global table until the variable is explicitly deleted.
To see what global variables are in scope, use the following command:
for k,v in pairs(_G) do print(k,v) end
Local variables are lexically scoped. This means the scope of local variables is limited to the block
where they are declared. A block is the body of a control structure, the body of a function or a
chunk (the file or string with the code where the variable is declared). Any variable can be
declared local within nested blocks or functions without affecting the enclosing scope.
Because locals are lexically scoped it is also possible to refer to local variables in an outer scope.
These variables are still accessible within the inner scope.
The Lua runtime internally uses hashed key access to retrieve keyed data from a table. Locals are
stored as a contiguous vector and are accessed directly by an index, which is faster. Access to
firmware based tables is particularly slow.
Using locals as follows is both a lot faster at runtime and generates less byte code instructions
for their access:
local can_send = can.send
2.4.4

Passing Context between Lua Event Tasks

A single Lua function is bound to any event callback task. The function is executed from within
the relevant C code using a lua_call(). Each function can invoke other functions and so on.
But each function must ultimately return the control to the C library code which then returns the
control to the firmware.
Lua local variables that are defined inside a function only exist within the context of this
executing Lua function. Therefore locals are unreferenced on exit and any local data and garbage
might be collected between the lua_call() actions.
Context can be passed in two ways between event tasks:
by global variables: any global variable persists until it is explicitly dereferenced by assigning
nil to the variable.
by local variables: local variables that are defined in an outer scope (see description of locals
in
Declaring Variables, p.
CAN@net NT/CANbridge NT User Manual
6).
6 (22)
4.02.0332.20002 1.2 en-US

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the IXXAT CAN@net NT and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Ixxat canbridge nt

Table of Contents