Page MenuHomeWildfire Games

Throw errors when encountering NaN vectors
Changes PlannedPublic

Authored by elexis on Jan 23 2018, 9:06 PM.

Details

Reviewers
vladislavbelov
Summary

When one passes the wrong order or number of arguments in a random map script and a vector is involved, the random map script often doesn't
show any kind of error and one can only notice that some entities were not placed or similar.
I cannot imagine any good coming from storing NaN in vectors, since every operation inside (and around?) the prototype assumes numbers.

Test Plan

Is this true for the simulation and GUI usage in general too?
Note that typeof NaN is "number": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Diff Detail

Event Timeline

elexis created this revision.Jan 23 2018, 9:06 PM

Again I ran into this having been useful if committed.

The JS simulation only has a handful of Vectors and doesn't use NaN anywhere.

The C++ simulation converts CFixedVector2D to a JS Vector2D when passing things over to JS.

There are few hundred occurrences of 2D vectors in C++. All of them look like innocent math, but you never know.
Though I would like to get rid of this patch and benefit from the warning, I suppose this should wait for right after the release unless someone can testify that this patch is good.

vladislavbelov requested changes to this revision.Feb 21 2018, 6:03 PM
vladislavbelov added a subscriber: vladislavbelov.

The rest of the patch looks good.

binaries/data/mods/public/globalscripts/vector.js
31

I suggest to cover it by quotes to understand that we passed empty string i.e.:

"x = '" + uneval(x) + "', y = '" + uneval(y) + "'";
binaries/data/mods/public/simulation/components/tests/test_Vector.js
160

It'd good to add few tests with string and undefined.

This revision now requires changes to proceed.Feb 21 2018, 6:03 PM
elexis requested review of this revision.Feb 21 2018, 6:06 PM

uneval returns quotes around strings.
Notice also that your request assuming that assumption to be true, would also make it impossible to distinguish "1" from "1".
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/uneval

The best argument against this patch is one use case I encountered, determining the min/max of vector components with more than 2 vectors.
We'd start with a vector(infinity, -infinity) and decrease/increase only.
But the vector can start with the values of the first vector or remain undefined to cover this use-case.

In D1251#53936, @elexis wrote:

uneval returns quotes around strings.
Notice also that your request assuming that assumption to be true, would also make it impossible to distinguish "1" from "1".
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/uneval

The best argument against this patch is one use case I encountered, determining the min/max of vector components with more than 2 vectors.
We'd start with a vector(infinity, -infinity) and decrease/increase only.
But the vector can start with the values of the first vector or remain undefined to cover this use-case.

What's about tests with strings?

Could add. As mentioned above this is mostly planned for after the release.
What about NaN or Infinity being a legitimate use case?

We agreed to allow Infinity, should confirm or update the patch to reflect that.

temple added inline comments.Feb 21 2018, 11:09 PM
binaries/data/mods/public/globalscripts/vector.js
251

three

How about introducing a NaNaNVector2D that 'inherits' all Vector2D functions and just replaces the set method?

The problem is only that using NaN or Infinity in a Vector is rather a nearly singular edge case, so we should rather have typeCheck = true in the vector construction and pass false for the one use case that wants infinity for some reason, right?

elexis planned changes to this revision.Mar 18 2018, 1:37 PM
Stan added a subscriber: Stan.Jan 19 2019, 1:54 PM

Wouldn't hurt to have this. What is missing ?

lyv added a subscriber: lyv.Jan 19 2019, 2:09 PM
In D1251#70539, @Stan wrote:

Wouldn't hurt to have this. What is missing ?

missing += rP21378;