Integer overrun

Quick and stupid question:

What happens if a device variable exceeds 32767?

It becomes 32768.

:slight_smile: OK, more usefully: Squirrel integers are signed 32-bit and all integer arithmetic is modulo 2^32. So if a device variable exceeds 2,147,483,647 it becomes -2,147,483,648.

Peter

See told you it was a stupid question. Thanks! :slight_smile:

@peter, this discussion reminds of a post I made a few weeks ago that didn’t register a response.

Perhaps, I didn’t frame the problem so well. Methods device.send() and device.on() allow us to pass scalar values and complex structures between the device and agent. Some things just can’t be represented using the existing data-types, such as a 64bit integer.
I can create a class that encodes a 64b int.
It won’t be very speedy, but at least it will allow me to wrap the data into a single object.
The class can use appropriate meta methods for _add, _mul etc
I can put that class in both the device and agent.
However, moving a 64bit int from device-agent or agent-device can’t be seamless unless .send() and .on() support so way of encoding and decoding the instance.
Now that libraries are available, new data-types can supplement the existing ones by simply using #require in both agent and device. If the receiving party does not have the relevant class to instantiate a user-defined data-type, a run-time error would be the expected response.

You can implement that in a library, I think. Use Squirrel’s introspection (typeof, getclass, foreach etc) to serialise your instance into a table, then on the other end unpack the table (probably using a list of deserialisable classes, for safety’s sake) again using introspection (instance etc).

Peter

Yeah, but with device.send() I can send an obj with nested objects, including a blob, (which is non Squirrel) and it will be serialised and reconstituted at the other end. Do you suggest that I traverse the received object, detect the type of any serialised non-Squirrel data-types (such as a table with { “$long”:"\x0\x0\x0\x0\x0\x0\x0\x0" } ) and instantiate it?

Yes, I could do that, but I was just highlighting the utility of having agent.on() or device.on() doing it for us! As long as the data type is declared in both agent and device, it could be done automagically.

I believe that xxxx.send() doesn’t call _tostring when it serialises instances? It just dumps non-serialisable data. If it did call _tostring then xxxx.on() would just need to detect a table with { “$TYPE”:“VALUE” } and call TYPE(VALUE) to instantiate.

When I have a moment free, I’ll write some wrappers for .send() and .on() that would demonstrate how it could work.