Skip to main content

getroottable()

Provides a reference to the Squirrel root table of global variables

Availability

Device + Agent

Returns

Table — the root table

Description

This function returns the current root table, which is the table in which Squirrel stores global variables and main program functions.

The return value is most commonly used to check on the presence of specific global variables. For example, the imp API looks for a global table variable called nv. If this variable is established by device code, data saved within it will be automatically preserved across certain warm restarts. When impOS™ starts the device’s Squirrel virtual machine, it checks for preserved nv data. If such data exists, impOS adds nv and that data within it to the root table.

When the device code needs to preserve data, it should check for the presence of nv in the root table before attempting to create nv, which will generate an error if the data persistence table already exists. The example code below shows one way in which this can be used.

Example Code

This code wakes the imp every ten seconds to increment and log a counter. In between, the imp goes into a deep sleep. This clears the device’s memory and halts your code, so data which needs to be preserved during deep sleep — in this case, the variable count — may be stored in the nv table. The code uses getroottable() to check for the presence of this data when the code it re-run after deep sleep.