Code Monkey home page Code Monkey logo

Comments (7)

aseemk avatar aseemk commented on September 25, 2024

As commit 9cf34ad's message shows, I was able to test this easily since ES5 doesn't throw an error on negative hexadecimal literals -- it just evaluates them to NaN. Fixed.

from json5.

aseemk avatar aseemk commented on September 25, 2024

Released in 0.2.0 FYI!

https://github.com/aseemk/json5/blob/develop/CHANGELOG.md#v020-code-diff

from json5.

rlidwka avatar rlidwka commented on September 25, 2024

I don't entirely understand why signed hexadecimals are not valid.

This seem to work just fine for me:

> eval('(function(){"use strict";return -0xC8})()')
-200

from json5.

jordanbtucker avatar jordanbtucker commented on September 25, 2024

I wanted to open this back up for discussion, clarify some things, and see if we want to change this behavior.

TL;DR
This issue was originally a bug because of a change in V8. Instead of fixing the bug, the feature was removed even though the feature didn't really have anything to do with the V8 bug. It was the implementation of the feature that needed a fix.

Signed numbers vs numeric literals

Signed numbers, whether decimal or hexadecimal, are not treated as literals when parsed by ES5. They are parsed as two separate tokens: a Punctuator (+ or - in this case) and a NumericLiteral (an unsigned decimal or hexadecimal number).

That sequence produces a UnaryExpression using the + or - operators. In either case, the text of the NumericLiteral is converted to a number using ES5's ToNumber operation, and in the case of the - operator, the mathematical value is negated.

The discrepancy between signed decimals and signed hexadecimals occurs within the ToNumber(String) operation. It allows signed decimal numbers but prohibits signed hexadecimal numbers, returning NaN instead. Compare the following. (The global Number function uses the ToNumber operation):

> [  eval('1'),   eval('0x1'),   eval('-1'),   eval('-0x1')]
[ 1, 1, -1, -1 ]

> [Number('1'), Number('0x1'), Number('-1'), Number('-0x1')]
[ 1, 1, -1, NaN ]

The V8 bug

The + unary operator with a String argument (i.e. +'1.234') also applies the ToNumber operation, which is what the V8 bug was about. Expressions like +'-0xF' and Number('-0xF') were resulting in -15 instead of NaN as called for in the spec. Note that this is separate from the actual unary expression -0xF; this is the string '-0xF' being converted to a number.

Conclusion

Signed hexadecimals aren't evil, nor are they disallowed in ES5. They just can't be converted from strings for whatever reason the ES5 authors had. This really has nothing to do with JSON5.

JSON5 has freed us from the restrictions of JSON so that we can have a lightweight data-interchange format that is even easier for humans to read and write. Why disallow signed hexadecimals just because ES5 doesn't convert them from strings?

from json5.

aseemk avatar aseemk commented on September 25, 2024

Great argument @jordanbtucker, and fantastically thorough explanation! Thanks.

I'm convinced: let's re-add support for negative hexadecimals. Updated the issue title.

Now we just have to figure out how, right? =)

from json5.

aseemk avatar aseemk commented on September 25, 2024

Fixed by @jordanbtucker in pull #74.

from json5.

aseemk avatar aseemk commented on September 25, 2024

Oops, I'd forgotten to close this.

from json5.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.