Scratch-vm: Numeric Infinity (1/0) turns into "0" when saving to sb2 format

Created on 8 Nov 2018  路  9Comments  路  Source: LLK/scratch-vm

Expected Behavior

In scratch 2: You should be able to save the value (1/0) (constructed using the () / () operator block). The value is listed as Infinity in the list or variable.

Actual Behavior

In scratch 2: You cannot save the value (1/0) (constructed using the () / () operator block). The value is listed as Infinity, then is turned into 0 in the sb2 creation.

Steps to Reproduce

  1. Either:
    a. Create a variable.
    b. Give the variable the value (1/0) (using the () / () operator block) using the set () to () block.
  2. Or:
    a. Create a list.
    b. Add the value (1/0) (using the () / () operator block) to the list using the add () to () block.
  3. Notice that the number shows up as Infinity in the visual representation of the list or variable.
  4. Save the project and download the program.
  5. Exit the window.
  6. Notice:
    a. The value has turned to 0
    b. If you inspect the project.json, the value there is 0 as well

See this project for an example: https://scratch.mit.edu/projects/260496681/#editor

This may or may not be a compatibility issue with scratch 3.

Operating System and Browser

Debian 10 and Firefox 62

bug compatibility critical needs-discussion needs-triage

Most helpful comment

For some more context, this actually is a problem for saving scratch3 projects:

In Scratch3, the variable gets serialized using JSON.stringify, which does not work with values like Infinity or NaN (see here for why), so they get serialized to null, which then makes the parser fail. that makes the project not loadable by file input, and cannot be saved on the server because validation fails (null is not a number or string).

Found by @ericrosenbaum

All 9 comments

Hmm -- cool bug! I might be mistaken, but I don't think this affects Scratch 3.0: fixing the bug won't break (nor fix) existing projects; and we can't turn the 0s back into Infinities because what if the user actually put a 0? Do you agree?

@joker314 it might just be something to keep in mind when people are implementing scratch 3 saving! I agree probably not worth fixing in scratch 2 :)

Duplicate of https://github.com/LLK/scratch-flash/issues/1290
(Though I guess it's worth having the compatibility discussion, since I can't see an obvious reason why Scratch 3 should save 卤Infinity as zero, unless someone can conjure up some plausible case why Scratch 2 behaved in this way, and why Scratch 3 needs to follow on and be compatible.)

/cc @kchadha @ktbee

For some more context, this actually is a problem for saving scratch3 projects:

In Scratch3, the variable gets serialized using JSON.stringify, which does not work with values like Infinity or NaN (see here for why), so they get serialized to null, which then makes the parser fail. that makes the project not loadable by file input, and cannot be saved on the server because validation fails (null is not a number or string).

Found by @ericrosenbaum

Thanks @paulkaplan and @ericrosenbaum. I'm raising the priority of this to "critical" and pushing it up in the compatibility project backlog.

/cc @ktbee @kchadha

How should we solve this problem? Do we want to do the same thing as scratch 2 and serialize Infinity and NaN as 0?

@kchadha That seems like the right way to resolve the issue IMO.

But then Infinity and NaN get weird when stored... why not use a custom serializer or at least a third-party one that handles such values properly?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

towerofnix picture towerofnix  路  3Comments

ericrosenbaum picture ericrosenbaum  路  3Comments

kchadha picture kchadha  路  6Comments

griffpatch picture griffpatch  路  6Comments

apple502j picture apple502j  路  6Comments