Skip to main content

Electric Imp Squirrel vs Standard Squirrel

How the two implementations differ

There are a few, small differences between standard Squirrel and Electric Imp’s implementation of the language. The most obvious one is that the Squirrel mathematics library exists in its own namespace, math. So where the official Squirrel documentation talks about, say, sin(), in an imp application that method would be entered as math.sin(), ie.

Standard Squirrel:

local sineValue = sin(angleInDegrees * PI / 180);

Electric Imp Squirrel:

local sineValue = math.sin(angleInDegrees * PI / 180);

Note that standard Squirrel’s srand() method is present in Electric Imp Squirrel (as math.srand()), but has no effect on either devices or agents; the random number generator is automatically seeded correctly on startup for both devices and agents.

Use of the system library’s clock() function, though it is available to both agent and devices, is not recommended. Use date() on the agent or hardware.millis() on the device: both will yield more accurate, more useful data for measuring time intervals. date(), which returns the current date and time, is also available on the device, and both agent and device may also use time(); this returns the current date and time as elasped seconds since midnight, 1 Jan 1970.

Other differences are more minor still and relate to operations that are not relevant to imps. All the file handling calls, for instance, have been removed, as there is no filesystem on an imp. Thread creation and management features have also been removed because the imp intentionally runs impOS™ and Squirrel code in a single thread.

Library Present in impOS Absent from impOS
Base getroottable, assert, print, error,
array, type, callee, dummy
setroottable, getconsttable, setconsttable, compilestring, newthread,
suspend, setdebughook, seterrorhandler, getstackinfos
Blob All
IO All except for stream methods on blobs
Math All, in namespace math srand is present, but ineffective (see above)
String All
System clock, time, date getenv, system, remove, rename

Extensions to Standard Squirrel

In a few places Electric Imp Squirrel extends standard Squirrel in ways intended to benefit code running on devices and agents:

  • Instances of blob can be implicitly coerced to type string or explicitly converted: blob.tostring().
  • Indexing a string always produces character values in the range 0-255. Standard Squirrel exposes the underlying C char type, so was signed on some platforms.
  • The if ("X" in Y) construct now works even if Y has a delegate or a _get metamethod. This is what enables if ("pinA" in hardware).
  • bindenv keeps a strong reference to the bound object. In standard Squirrel it keeps a weak reference.