Browse Source

Merge remote-tracking branch 'upstream/master'

Brian Beard 11 years ago
parent
commit
dc9851ab8c
4 changed files with 69 additions and 3 deletions
  1. 1 0
      .gitignore
  2. 44 0
      Jint.Tests/Runtime/EngineTests.cs
  3. 19 2
      Jint/Native/Json/JsonParser.cs
  4. 5 1
      README.md

+ 1 - 0
.gitignore

@@ -151,3 +151,4 @@ $RECYCLE.BIN/
 
 # Mac crap
 .DS_Store
+Jint.sln.ide/*

+ 44 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -705,6 +705,50 @@ namespace Jint.Tests.Runtime
             Assert.Equal(expected, result);
         }
 
+        [Fact]
+        public void JsonParserShouldParseNegativeNumber()
+        {
+            var engine = new Engine();
+            var result = engine.Execute(@"JSON.parse('{ ""x"":-1 }').x === -1").GetCompletionValue().AsBoolean();
+            Assert.Equal(true, result);
+
+            result = engine.Execute(@"JSON.parse('{ ""x"": -1 }').x === -1").GetCompletionValue().AsBoolean();
+            Assert.Equal(true, result);
+        }
+
+        [Fact]
+        public void JsonParserShouldDetectInvalidNegativeNumberSyntax()
+        {
+            var engine = new Engine();            
+            var code = @"
+                function f() {
+                    try {
+                        JSON.parse('{ ""x"": -.1 }'); // Not allowed
+                        return false;
+                    }
+                    catch(ex) {
+                        return ex.toString().indexOf('Unexpected token') !== -1;
+                    }
+                }
+                f();
+            ";
+            Assert.True(engine.Execute(code).GetCompletionValue().AsBoolean());
+
+            code = @"
+                function f() {
+                    try {
+                        JSON.parse('{ ""x"": - 1 }'); // Not allowed
+                        return false;
+                    }
+                    catch(ex) {
+                        return ex.toString().indexOf('Unexpected token') !== -1;
+                    }
+                }
+                f();
+            ";
+            Assert.True(engine.Execute(code).GetCompletionValue().AsBoolean());
+        }
+
         [Fact]
         public void ShouldBeCultureInvariant()
         {

+ 19 - 2
Jint/Native/Json/JsonParser.cs

@@ -161,9 +161,17 @@ namespace Jint.Native.Json
 
             int start = _index;
             string number = "";
+            
+            // Number start with a -
+            if (ch == '-')
+            {
+                number += _source.CharCodeAt(_index++).ToString();
+                ch = _source.CharCodeAt(_index);
+            }
+
             if (ch != '.')
             {
-                number = _source.CharCodeAt(_index++).ToString();
+                number += _source.CharCodeAt(_index++).ToString();
                 ch = _source.CharCodeAt(_index);
 
                 // Hex number starts with '0x'.
@@ -219,7 +227,7 @@ namespace Jint.Native.Json
             return new Token
                 {
                     Type = Tokens.Number,
-                    Value = Double.Parse(number, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture),
+                    Value = Double.Parse(number, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture),
                     LineNumber = _lineNumber,
                     LineStart = _lineStart,
                     Range = new[] {start, _index}
@@ -447,6 +455,15 @@ namespace Jint.Native.Json
                 return ScanPunctuator();
             }
 
+            if (ch == '-') // Negative Number
+            {
+                if (IsDecimalDigit(_source.CharCodeAt(_index + 1)))
+                {
+                    return ScanNumericLiteral();
+                }
+                return ScanPunctuator();
+            }
+
             if (IsDecimalDigit(ch))
             {
                 return ScanNumericLiteral();

+ 5 - 1
README.md

@@ -1,4 +1,4 @@
-[![Build status](https://ci.appveyor.com/api/projects/status/c84b8rdswh2w4744/branch/master)](https://ci.appveyor.com/project/SebastienRos/jint)
+[![Build status](http://teamcity.codebetter.com/app/rest/builds/buildType:(id:Jint_Master)/statusIcon)](http://teamcity.codebetter.com/project.html?projectId=Jint&tab=projectOverview)
 
 # Jint
 
@@ -124,3 +124,7 @@ Generic types are also supported. Here is how to declare, instantiate and use a
   - boolean -> bool
   - Regex -> RegExp
   - Function -> Delegate
+
+Continuous Integration kindly provided by  
+[![](http://www.jetbrains.com/img/banners/Codebetter300x250.png)](http://www.jetbrains.com/teamcity)
+