Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squirrel limits variable value to int32 #5942

Closed
DorpsGek opened this issue Mar 14, 2014 · 7 comments
Closed

Squirrel limits variable value to int32 #5942

DorpsGek opened this issue Mar 14, 2014 · 7 comments
Labels
component: AI/Game script (squirrel) This issue is related to Squirrel (Scripting language) flyspray This issue is imported from FlySpray (https://bugs.openttd.org/)

Comments

@DorpsGek
Copy link
Member

The_Dude opened the ticket and wrote:

Function GSCompany::GetQuarterlyCompanyValue returns Money(int64) value, but squirrel swiftly interprets it as int32, so company values exceeding INT32_MAX cannot be treated properly. This goes for all API functions returning Money data type.

Reported version: Version?
Operating system: All


This issue was imported from FlySpray: https://bugs.openttd.org/task/5942
@DorpsGek
Copy link
Member Author

DorpsGek commented Apr 6, 2014

krinn wrote:

look at my report https://bugs.openttd.org/task/5410

This prevent overflow (as squirrel have no overflow handling).
if your running 32bits, it's normal your int64 is a int32. But 64bits should just return the int64, it's not doing it?

So in practice AI/GS should aim at 32bits only, as that value is arch dependent.
You can detect the arch type if you want implement special conditions for 32/64bits arch (enforce limit or even stopping your AI if on 32bits...).


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942#comment13215

@DorpsGek
Copy link
Member Author

DorpsGek commented Apr 6, 2014

The_Dude wrote:

Size of int is indeed 8 on 64bit, but the return value is limiter to INT32_MAX. There is no overflow, you mention in your report (probably it was prevented), but still, according to documentation, I expect int64 to be returned.

Any company with higher value will return invalid value. Fixing only the overflow is lazy fix.


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942#comment13219

@DorpsGek
Copy link
Member Author

DorpsGek commented Apr 9, 2014

peter1138 wrote:

@krinn A 64 bit integer is 64 bits even on a 32 bit system.

But Squirrel simply doesn't support 64 bit integers.


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942#comment13222

@DorpsGek
Copy link
Member Author

DorpsGek commented Apr 9, 2014

krinn wrote:

There's no such integer or anything in squirrel, you cannot even define a type, squirrel assign it itself, so i'm not speaking of int64, but for squirrel the "integer" is 8 or 4 depending on arch. That mean 64bits or 32bits.
And despite what you said, squirrel handle 64bits (on paper) : http://squirrel-lang.org/doc/squirrel2.html# d0e2296 look at intsize -> size in bytes of the internal VM rapresentation for integers(4 for 32bits builds 8 for 64bits builds). And the user confirm the return value is 8.
So yes the doc says 64bits is support.

But the reporter is also wrong :
The doc itself doesn't tell anyone it will return a 64bits value -> http://nogo.openttd.org/api/trunk/classGSCompany.html# 2e07560e5c138bb641a99372e71b0055
static Money GSCompany::GetQuarterlyCompanyValue ( CompanyID company,
uint32 quarter
) [static]
You cannot expect uint32 returning a 64bits value even on a 64bits arch no?

So, it might be valid for a "request feature/change/improvment" bugtype to change return value to allow 64bits, but as-is reporter expecting 64bits when doc state it return a 32bits is a "not a bug".

If anyone wants to implement that, other functions should be added (to GS/AI API) to detect 64/32bits (pretty easy as intsize answer it but users shouldn't have to read squirrel doc to get it), because like i said squirrel lack overflow handling, this doesn't mean it don't overflow, this mean you cannot know when it has overflow and if "-345353" number is a negative company value or an overflow value, something a performing AI can met easy with 32bits limits.
It won't be perfect, but AI/GS authors right now can only assume 32bits and block any addition to company value if openttd return the maxint value of 32bits.
And it was really a good idea that openttd return max uint32 and not the overflow result ! I'm not saying it was perfect, not saying it cannot be push forward, but it was wise choice than letting squirrel handling the overflow value and AI/GS author cannot know it has happen.


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942#comment13223

@DorpsGek
Copy link
Member Author

DorpsGek commented Apr 9, 2014

krinn wrote:

Ooops, i see not all company function return the uint32
It really tell it return "Money" type and some uint32 type...

That's a real bug than :)


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942#comment13224

@DorpsGek
Copy link
Member Author

DorpsGek commented Apr 9, 2014

The_Dude wrote:

Yes, it is a bug, either in code or in documentation. At least it is misleading, that function returns Money, but squrrel will limit it to int32 on any architecture.
Since it is as you said squirrel feature, one way to handle Money returning function would be to return it as list of some divided number and multiplier (like 1e10 could be 1e4, 1e6, or 1e4, 'M') or as string.


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942#comment13225

@DorpsGek
Copy link
Member Author

Rubidium closed the ticket.

Reason for closing: Fixed

In r26585


This comment was imported from FlySpray: https://bugs.openttd.org/task/5942

@DorpsGek DorpsGek added flyspray This issue is imported from FlySpray (https://bugs.openttd.org/) Goal/Game script labels Apr 7, 2018
@frosch123 frosch123 added the component: AI/Game script (squirrel) This issue is related to Squirrel (Scripting language) label Apr 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: AI/Game script (squirrel) This issue is related to Squirrel (Scripting language) flyspray This issue is imported from FlySpray (https://bugs.openttd.org/)
Projects
None yet
Development

No branches or pull requests

2 participants