Browse Source

Upgrade to latest Esprima, remove redundant casts (#452)

Marko Lahma 7 years ago
parent
commit
6164795167

+ 4 - 3
Jint/Engine.cs

@@ -444,7 +444,7 @@ namespace Jint
             }
         }
 
-        public object EvaluateExpression(Expression expression)
+        public object EvaluateExpression(INode expression)
         {
             _lastSyntaxNode = expression;
 
@@ -679,7 +679,8 @@ namespace Jint
         /// <summary>
         /// Invoke the current value as function.
         /// </summary>
-        /// <param name="propertyName">The arguments of the function call.</param>
+        /// <param name="propertyName">The name of the function to call.</param>
+        /// <param name="arguments">The arguments of the function call.</param>
         /// <returns>The value returned by the function call.</returns>
         public JsValue Invoke(string propertyName, params object[] arguments)
         {
@@ -868,7 +869,7 @@ namespace Jint
             for (var i = 0; i < variableDeclarations.Count; i++)
             {
                 var variableDeclaration = variableDeclarations[i];
-                var declarations  = (List<VariableDeclarator>) variableDeclaration.Declarations;
+                var declarations  = variableDeclaration.Declarations;
                 foreach (var d in declarations)
                 {
                     var dn = d.Id.As<Identifier>().Name;

+ 2 - 4
Jint/Jint.csproj

@@ -1,13 +1,11 @@
 <Project Sdk="Microsoft.NET.Sdk">
-
   <PropertyGroup>
     <NeutralLanguage>en-US</NeutralLanguage>
     <TargetFramework>netstandard2.0</TargetFramework>
     <AssemblyOriginatorKeyFile>Jint.snk</AssemblyOriginatorKeyFile>
     <SignAssembly>true</SignAssembly>
   </PropertyGroup>
-
   <ItemGroup>
-    <PackageReference Include="Esprima" Version="1.0.0-beta-1000" /> 
+    <PackageReference Include="Esprima" Version="1.0.0-beta-1022" />
   </ItemGroup>
-</Project>
+</Project>

+ 0 - 1
Jint/Native/Function/FunctionConstructor.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Linq;
 using Esprima;
 using Esprima.Ast;

+ 2 - 3
Jint/Native/Function/ScriptFunctionInstance.cs

@@ -1,5 +1,4 @@
-using System.Collections.Generic;
-using System.Runtime.CompilerServices;
+using System.Runtime.CompilerServices;
 using Esprima.Ast;
 using Jint.Native.Object;
 using Jint.Runtime;
@@ -52,7 +51,7 @@ namespace Jint.Native.Function
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static string[] GetParameterNames(IFunction functionDeclaration)
         {
-            var list = (List<INode>) functionDeclaration.Params;
+            var list = functionDeclaration.Params;
             var count = list.Count;
             var names = new string[count];
             for (var i = 0; i < count; ++i)

+ 1 - 2
Jint/Native/Json/JsonInstance.cs

@@ -1,5 +1,4 @@
-using Esprima;
-using Jint.Native.Object;
+using Jint.Native.Object;
 using Jint.Runtime;
 using Jint.Runtime.Interop;
 

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

@@ -196,7 +196,7 @@ namespace Jint.Native.String
             var len = s.Length;
             var intStart = ToIntegerSupportInfinity(start);
 
-            var intEnd = arguments.At(1) == Undefined.Instance ? len : (int)ToIntegerSupportInfinity(end);
+            var intEnd = arguments.At(1) == Undefined.Instance ? len : ToIntegerSupportInfinity(end);
             var finalStart = System.Math.Min(len, System.Math.Max(intStart, 0));
             var finalEnd = System.Math.Min(len, System.Math.Max(intEnd, 0));
             // Swap value if finalStart < finalEnd

+ 1 - 2
Jint/Native/Symbol/SymbolConstructor.cs

@@ -1,5 +1,4 @@
-using System;
-using Jint.Native.Function;
+using Jint.Native.Function;
 using Jint.Native.Object;
 using Jint.Runtime;
 using Jint.Runtime.Interop;

+ 1 - 11
Jint/Native/Symbol/SymbolPrototype.cs

@@ -1,19 +1,9 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Jint.Native.Array;
-using Jint.Native.Function;
-using Jint.Native.Object;
-using Jint.Native.RegExp;
+using Jint.Native.Object;
 using Jint.Runtime;
-using Jint.Runtime.Descriptors;
 using Jint.Runtime.Interop;
 
 namespace Jint.Native.Symbol
 {
-
-
     /// <summary>
     /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4
     /// </summary>

+ 4 - 3
Jint/Options.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Globalization;
 using System.Linq;
 using System.Reflection;
+using Jint.Native;
 using Jint.Runtime.Interop;
 
 namespace Jint
@@ -16,7 +17,7 @@ namespace Jint
         private bool _allowClr;
         private readonly List<IObjectConverter> _objectConverters = new List<IObjectConverter>();
         private int _maxStatements;
-        private int _maxRecursionDepth = -1; 
+        private int _maxRecursionDepth = -1;
         private TimeSpan _timeoutInterval;
         private CultureInfo _culture = CultureInfo.CurrentCulture;
         private TimeZoneInfo _localTimeZone = TimeZoneInfo.Local;
@@ -47,7 +48,7 @@ namespace Jint
         /// Allow the <code>debugger</code> statement to be called in a script.
         /// </summary>
         /// <remarks>
-        /// Because the <code>debugger</code> statement can start the 
+        /// Because the <code>debugger</code> statement can start the
         /// Visual Studio debugger, is it disabled by default
         /// </remarks>
         public Options AllowDebuggerStatement(bool allowDebuggerStatement = true)
@@ -112,7 +113,7 @@ namespace Jint
             _maxStatements = maxStatements;
             return this;
         }
-        
+
         public Options TimeoutInterval(TimeSpan timeoutInterval)
         {
             _timeoutInterval = timeoutInterval;

+ 1 - 1
Jint/Runtime/ExpressionIntepreter.cs

@@ -985,7 +985,7 @@ namespace Jint.Runtime
 
         public JsValue EvaluateArrayExpression(ArrayExpression arrayExpression)
         {
-            var elements = (List<ArrayExpressionElement>) arrayExpression.Elements;
+            var elements = arrayExpression.Elements;
             var count = elements.Count;
             var a = _engine.Array.Construct(new JsValue[] { count });
             for (var n = 0; n < count; n++)

+ 33 - 24
Jint/Runtime/Interop/DefaultTypeConverter.cs

@@ -1,10 +1,9 @@
 using System;
 using System.Collections.ObjectModel;
-using System.Linq;
-using System.Linq.Expressions;
-using Jint.Native;
 using System.Collections.Generic;
+using System.Linq.Expressions;
 using System.Reflection;
+using Jint.Native;
 
 namespace Jint.Runtime.Interop
 {
@@ -14,9 +13,9 @@ namespace Jint.Runtime.Interop
         private static readonly Dictionary<string, bool> _knownConversions = new Dictionary<string, bool>();
         private static readonly object _lockObject = new object();
 
-        private static MethodInfo convertChangeType = typeof(System.Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type), typeof(IFormatProvider) });
-        private static MethodInfo jsValueFromObject = typeof(JsValue).GetMethod("FromObject");
-        private static MethodInfo jsValueToObject = typeof(JsValue).GetMethod("ToObject");
+        private static readonly MethodInfo convertChangeType = typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type), typeof(IFormatProvider) });
+        private static readonly MethodInfo jsValueFromObject = typeof(JsValue).GetMethod("FromObject");
+        private static readonly MethodInfo jsValueToObject = typeof(JsValue).GetMethod("ToObject");
 
         public DefaultTypeConverter(Engine engine)
         {
@@ -67,13 +66,13 @@ namespace Jint.Runtime.Interop
                     {
                         var genericArguments = type.GetGenericArguments();
 
-                        var @params = new ParameterExpression[genericArguments.Count()];
-                        for (var i = 0; i < @params.Count(); i++)
+                        var @params = new ParameterExpression[genericArguments.Length];
+                        for (var i = 0; i < @params.Length; i++)
                         {
                             @params[i] = Expression.Parameter(genericArguments[i], genericArguments[i].Name + i);
                         }
                         var tmpVars = new Expression[@params.Length];
-                        for (var i = 0; i < @params.Count(); i++)
+                        for (var i = 0; i < @params.Length; i++)
                         {
                             var param = @params[i];
                             if (param.Type.IsValueType())
@@ -100,22 +99,21 @@ namespace Jint.Runtime.Interop
                     else if (genericType.Name.StartsWith("Func"))
                     {
                         var genericArguments = type.GetGenericArguments();
-                        var returnType = genericArguments.Last();
+                        var returnType = genericArguments[genericArguments.Length - 1];
 
-                        var @params = new ParameterExpression[genericArguments.Count() - 1];
-                        for (var i = 0; i < @params.Count(); i++)
+                        var @params = new ParameterExpression[genericArguments.Length - 1];
+                        for (var i = 0; i < @params.Length; i++)
                         {
                             @params[i] = Expression.Parameter(genericArguments[i], genericArguments[i].Name + i);
                         }
 
-                        var @vars =
-                            Expression.NewArrayInit(typeof(JsValue),
-                                @params.Select(p =>
-                                {
-                                    var boxingExpression = Expression.Convert(p, typeof(object));
-                                    return Expression.Call(null, jsValueFromObject, Expression.Constant(_engine, typeof(Engine)), boxingExpression);
-                                })
-                            );
+                        var initializers = new MethodCallExpression[@params.Length];
+                        for (int i = 0; i < @params.Length; i++)
+                        {
+                            var boxingExpression = Expression.Convert(@params[i], typeof(object));
+                            initializers[i]= Expression.Call(null, jsValueFromObject, Expression.Constant(_engine, typeof(Engine)), boxingExpression);
+                        }
+                        var @vars = Expression.NewArrayInit(typeof(JsValue), initializers);
 
                         // the final result's type needs to be changed before casting,
                         // for instance when a function returns a number (double) but C# expects an integer
@@ -148,12 +146,19 @@ namespace Jint.Runtime.Interop
                         var method = type.GetMethod("Invoke");
                         var arguments = method.GetParameters();
 
-                        var @params = new ParameterExpression[arguments.Count()];
-                        for (var i = 0; i < @params.Count(); i++)
+                        var @params = new ParameterExpression[arguments.Length];
+                        for (var i = 0; i < @params.Length; i++)
                         {
                             @params[i] = Expression.Parameter(typeof(object), arguments[i].Name);
                         }
-                        var @vars = Expression.NewArrayInit(typeof(JsValue), @params.Select(p => Expression.Call(null, typeof(JsValue).GetMethod("FromObject"), Expression.Constant(_engine, typeof(Engine)), p)));
+
+                        var initializers = new MethodCallExpression[@params.Length];
+                        for (int i = 0; i < @params.Length; i++)
+                        {
+                            initializers[i] = Expression.Call(null, typeof(JsValue).GetMethod("FromObject"), Expression.Constant(_engine, typeof(Engine)), @params[i]);
+                        }
+
+                        var @vars = Expression.NewArrayInit(typeof(JsValue), initializers);
 
                         var callExpression = Expression.Block(
                                                 Expression.Call(
@@ -179,7 +184,11 @@ namespace Jint.Runtime.Interop
                     throw new ArgumentException(String.Format("Value of object[] type is expected, but actual type is {0}.", value.GetType()));
 
                 var targetElementType = type.GetElementType();
-                var itemsConverted = source.Select(o => Convert(o, targetElementType, formatProvider)).ToArray();
+                var itemsConverted = new object[source.Length];
+                for (int i = 0; i < source.Length; i++)
+                {
+                    itemsConverted[i] = Convert(source[i], targetElementType, formatProvider);
+                }
                 var result = Array.CreateInstance(targetElementType, source.Length);
                 itemsConverted.CopyTo(result, 0);
                 return result;