scratchfoundation / scratch-vm

Virtual Machine used to represent, run, and maintain the state of programs for Scratch 3.0
http://scratchfoundation.github.io/scratch-vm/
BSD 3-Clause "New" or "Revised" License
1.22k stars 1.51k forks source link

Numeric Infinity (1/0) turns into "0" when saving to sb2 format #1746

Closed spectranaut closed 5 years ago

spectranaut commented 6 years ago

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

joker314 commented 6 years ago

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?

spectranaut commented 5 years ago

@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 :)

TheLogFather commented 5 years ago

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.)

thisandagain commented 5 years ago

/cc @kchadha @ktbee

paulkaplan commented 5 years ago

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

thisandagain commented 5 years ago

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

kchadha commented 5 years ago

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

thisandagain commented 5 years ago

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

Kenny2github commented 5 years ago

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?