Comments (7)
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.
Released in 0.2.0 FYI!
https://github.com/aseemk/json5/blob/develop/CHANGELOG.md#v020-code-diff
from json5.
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.
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.
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.
Fixed by @jordanbtucker in pull #74.
from json5.
Oops, I'd forgotten to close this.
from json5.
Related Issues (20)
- Explain use cases front and center HOT 3
- Module '"node_modules/json5/lib/index"' has no default export HOT 1
- Support Integers outside the range `[-(2**53)+1, (2**53)-1]` HOT 6
- SyntaxError when require()ing JSON5 file in Jest test HOT 1
- Prototype Pollution in JSON5 HOT 11
- json5 latest is now 1.0.2 on npmjs - intentional? HOT 3
- Support Template Literals using backticks? HOT 1
- Provide `exports` config in `packages.json`
- Multiline JSON doesn't multiline HOT 1
- add key property support ? HOT 1
- Add `comma-dangle` option HOT 7
- Online JSON5 Editor(Formatter) is misleading? HOT 2
- Use .substring() instead of the deprecated .substr()
- Cannot stringify "\u0000"
- transitive dependency 'minimist' needs to be updated by rebuilding HOT 1
- question: commented unreachable code HOT 10
- JSON5.stringify() option: `trailingCommas` HOT 3
- Focus on ESNext HOT 1
- CLI should process consecutive JSONs HOT 1
- migrate to ESModules HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json5.