2
0
Эх сурвалжийг харах

fix string[Infinity] and string.localeCompare.length

fix string[Infinity] and string.localeCompare.length
Frederic Torres 11 жил өмнө
parent
commit
f68c04945e

+ 20 - 2
Jint/Native/String/StringInstance.cs

@@ -31,8 +31,22 @@ namespace Jint.Native.String
 
         public JsValue PrimitiveValue { get; set; }
 
+        private static bool IsInt(double d)
+        {
+            if (d >= long.MinValue && d <= long.MaxValue)
+            {
+                var l = (long)d;
+                return l >= int.MinValue && l <= int.MaxValue;
+            }
+            else 
+                return false;
+        }
+
         public override PropertyDescriptor GetOwnProperty(string propertyName)
         {
+            if(propertyName == "Infinity")
+                return PropertyDescriptor.Undefined;
+
             var desc = base.GetOwnProperty(propertyName);
             if (desc != PropertyDescriptor.Undefined)
             {
@@ -45,9 +59,13 @@ namespace Jint.Native.String
             }
 
             var str = PrimitiveValue;
-            var index = (int)TypeConverter.ToInteger(propertyName);
+            var dIndex = TypeConverter.ToInteger(propertyName);
+            if(!IsInt(dIndex))
+                return PropertyDescriptor.Undefined;
+
+            var index = (int)dIndex;
             var len = str.AsString().Length;
-            if (len <= index)
+            if (len <= index || index < 0)
             {
                 return PropertyDescriptor.Undefined;
             }

+ 2 - 2
Jint/Native/String/StringPrototype.cs

@@ -45,7 +45,7 @@ namespace Jint.Native.String
             FastAddProperty("concat", new ClrFunctionInstance(Engine, Concat, 1), true, false, true);
             FastAddProperty("indexOf", new ClrFunctionInstance(Engine, IndexOf, 1), true, false, true);
             FastAddProperty("lastIndexOf", new ClrFunctionInstance(Engine, LastIndexOf, 1), true, false, true);
-            FastAddProperty("localeCompare", new ClrFunctionInstance(Engine, LocaleCompare), true, false, true);
+            FastAddProperty("localeCompare", new ClrFunctionInstance(Engine, LocaleCompare, 1), true, false, true);
             FastAddProperty("match", new ClrFunctionInstance(Engine, Match, 1), true, false, true);
             FastAddProperty("replace", new ClrFunctionInstance(Engine, Replace, 2), true, false, true);
             FastAddProperty("search", new ClrFunctionInstance(Engine, Search, 1), true, false, true);
@@ -591,7 +591,7 @@ namespace Jint.Native.String
 
             var s = TypeConverter.ToString(thisObj);
             var that = TypeConverter.ToString(arguments.At(0));
-
+            
             return string.CompareOrdinal(s, that);
         }