tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
185 stars 42 forks source link

Setting a variable to a BigInt value causes serious error on passage transition #314

Closed boyland closed 4 months ago

boyland commented 4 months ago

Describe the bug. If a passage sets a $ variable to a BigInt value, then clicking on a passage link gets a (confusing to the user) error message and no transition happen.

To Reproduce: Create an HTML file from

:: StoryData
{
        "ifid": "917FDF53-28CD-4DE5-B1CC-D861616AF323",
        "format": "SugarCube",
        "format-version": "2.36.0"
}

:: StoryTitle
Test BigInt in SugarCube

:: Start
This is the starting passage of a story.
<<set $test to 12345n>>
[[Next|Done]]

:: Done
The story is over.

Bring up the page and click "Next".

The first time, an error dialog comes up saying: (This is from Safari, Firefox is similar)

An error has occurred. You may be able to continue, but some parts may not work properly.

Error: JSON.stringify cannot serialize BigInt..

Stack Trace:
stringify@[native code]
value@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:60565
value@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:60066
momentActivate@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:122741
value@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:126522
enginePlay@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:264501
@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:147277
@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:45861
@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:106:45639
dispatch@https://pabst.ceas.uwm.edu/faculty/boyland/cs431/test.html:57:43069

The next time the link is clicked, nothing happens (no error message, but also no transition)

Expected behavior. We go to the "Done" passage.

Project details.

Desktop details.

Additional context. This is not hard to work around (don't have any BigInt variables), but it took me a while to figure out why my web page was erroring

I'm not actually using history or the sidebar -- I turn these off, but still get the error.

tmedwards commented 4 months ago

As you clearly see from [the documentation]( ), bigint primitives and BigInt objects are unsupported. Using unsupported features of the language that result in an error is not a surprising outcome.

I'm unsure what you want me to do about it by submitting a bug report. I mean the documentation clearly says it's unsupported.

Why's it unsupported?

The are multiple factors at work here:

  1. JSON doesn't support it and SugarCube uses JSON to serialize. Now, that's probably not a deal breaker. I could probably write code to work around that, but it would be ugly because it would mean transforming the bigint into a string and back.
  2. Not all supported browsers support it, and SugarCube v2's support target is IE9. There's literally nothing I can do about that, except document that it's unsupported—which I've done.
boyland commented 4 months ago

Good points. I'd missed the documentation (my bad). It doesn't mention BigInt anywhere, but it does omit them from the list of supported types.

Sorry, what I wanted was an error report that was more informative (I spent a while trying to avoid printing BigInt variables using <<=...>> only to discover that it was the transition itself that was causing the problem, and then figuring out it was the history system. In retrospect, maybe I should have figured out it was the history mechanism, or better reviewed the "Supported Types" section to notice that BigInt wasn't mentioned.

So, I guess I don't need anything from you.

tmedwards commented 4 months ago

I'm not sure what I could do wrt. generating a better error. Any error would come from the serializer, which it's already doing, and the error itself is actually pretty good—it tells you what went wrong and why. I'm not sure I could meaningfully better that.