From 36887bb0d5ea6030d5498c895b36ef6838fb9492 Mon Sep 17 00:00:00 2001 From: Akash Kava <39438041+ackava@users.noreply.github.com> Date: Sun, 9 Feb 2025 17:49:38 +0530 Subject: [PATCH] Methods optimized --- .../LinqExpressions/ObjectBuilder.cs | 22 ++--- .../LinqExpressions/ScriptInfoBuilder.cs | 81 +++++++++++-------- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/YantraJS.Core/LinqExpressions/ObjectBuilder.cs b/YantraJS.Core/LinqExpressions/ObjectBuilder.cs index a657b9d3..ed1a7453 100644 --- a/YantraJS.Core/LinqExpressions/ObjectBuilder.cs +++ b/YantraJS.Core/LinqExpressions/ObjectBuilder.cs @@ -11,28 +11,30 @@ using SwitchCase = YantraJS.Expressions.YSwitchCaseExpression; using GotoExpression = YantraJS.Expressions.YGoToExpression; using TryExpression = YantraJS.Expressions.YTryCatchFinallyExpression; +using YantraJS.Core.LambdaGen; namespace YantraJS.ExpHelper { public class ObjectBuilder { - private static Type type = typeof(object); + // private static Type type = typeof(object); - private static MethodInfo _ToString - = typeof(System.Object).GetMethod("ToString", new Type[] { }); + //private static MethodInfo _ToString + // = typeof(System.Object).GetMethod("ToString", new Type[] { }); public static Expression ToString(Expression value) { - return Expression.Call(value, _ToString); + return value.CallExpression(() => (x) => x.ToString(), value); + //return Expression.Call(value, _ToString); } - private static MethodInfo _ReferenceEquals - = type.StaticMethod(nameof(Object.ReferenceEquals), typeof(object), typeof(object)); + //private static MethodInfo _ReferenceEquals + // = type.StaticMethod(nameof(Object.ReferenceEquals), typeof(object), typeof(object)); - public static Expression RefEquals(Expression left, Expression right) - { - return Expression.Call(null, _ReferenceEquals, left, right); - } + //public static Expression RefEquals(Expression left, Expression right) + //{ + // return Expression.Call(null, _ReferenceEquals, left, right); + //} } } diff --git a/YantraJS.Core/LinqExpressions/ScriptInfoBuilder.cs b/YantraJS.Core/LinqExpressions/ScriptInfoBuilder.cs index b2436600..e0322059 100644 --- a/YantraJS.Core/LinqExpressions/ScriptInfoBuilder.cs +++ b/YantraJS.Core/LinqExpressions/ScriptInfoBuilder.cs @@ -4,6 +4,8 @@ using System.Text; using YantraJS.Core.CodeGen; using YantraJS.Core.Core.Storage; +using YantraJS.Core.LambdaGen; +using YantraJS.Core.Types; using YantraJS.ExpHelper; using Exp = YantraJS.Expressions.YExpression; using Expression = YantraJS.Expressions.YExpression; @@ -14,63 +16,75 @@ namespace YantraJS.Core.LinqExpressions public static class ScriptInfoBuilder { - public static readonly Type type = typeof(ScriptInfo); + // public static readonly Type type = typeof(ScriptInfo); - private static readonly ConstructorInfo _new = - type.GetConstructor(new Type[] { }); + //private static readonly ConstructorInfo _new = + // type.GetConstructor(new Type[] { }); - private static readonly FieldInfo _code = - type.GetField(nameof(ScriptInfo.Code)); + //private static readonly FieldInfo _code = + // type.GetField(nameof(ScriptInfo.Code)); - private static readonly FieldInfo _fileName = - type.GetField(nameof(ScriptInfo.FileName)); + //private static readonly FieldInfo _fileName = + // type.GetField(nameof(ScriptInfo.FileName)); - private static readonly FieldInfo _indices = - type.GetField(nameof(ScriptInfo.Indices)); + //private static readonly FieldInfo _indices = + // type.GetField(nameof(ScriptInfo.Indices)); - private static readonly FieldInfo _functions = - type.GetField(nameof(ScriptInfo.Functions)); + //private static readonly FieldInfo _functions = + // type.GetField(nameof(ScriptInfo.Functions)); - public static Expression Functions(Expression info) - { - return Expression.Field(info, _functions); - } + //public static Expression Functions(Expression info) + //{ + // return Expression.Field(info, _functions); + //} - public static Expression Function(Expression info, int index, Type type) - { - var fs = Expression.Field(info, _functions); - var fi = Expression.ArrayIndex(fs, Expression.Constant(index)); - return Expression.TypeAs(fi, type); - } + //public static Expression Function(Expression info, int index, Type type) + //{ + // var fs = Expression.Field(info, _functions); + // var fi = Expression.ArrayIndex(fs, Expression.Constant(index)); + // return Expression.TypeAs(fi, type); + //} public static Expression New(string fileName, string code) { - return - Expression.MemberInit(Expression.New(_new), - Expression.Bind(_code, Expression.Constant(code)), - Expression.Bind(_fileName, Expression.Constant(fileName))); + var _code = TypeQuery.QueryInstanceField(() => (x) => x.Code); + var _fileName = TypeQuery.QueryInstanceField(() => (x) => x.FileName); + return + Expression.MemberInit( + NewLambdaExpression.NewExpression(() => () => new ScriptInfo()), + Expression.Bind(_code, Expression.Constant(code)), + Expression.Bind(_fileName, Expression.Constant(fileName))); + //return + // Expression.MemberInit(Expression.New(_new), + // Expression.Bind(_code, Expression.Constant(code)), + // Expression.Bind(_fileName, Expression.Constant(fileName))); } public static Expression Code(Expression scriptInfo) { - return Expression.Field(scriptInfo, _code); + return scriptInfo.FieldExpression(() => (x) => x.Code); + // return Expression.Field(scriptInfo, _code); } public static Expression FileName(Expression scriptInfo) { - return Expression.Field(scriptInfo, _fileName); + return scriptInfo.FieldExpression(() => (x) => x.FileName); + // return Expression.Field(scriptInfo, _fileName); } public static Expression KeyString(Expression scriptInfo, int index) { - return Expression.ArrayIndex(Expression.Field(scriptInfo, _indices), Expression.Constant(index)); + return Expression.ArrayIndex( + // Expression.Field(scriptInfo, _indices) + scriptInfo.FieldExpression(() => (x) => x.Indices) + , Expression.Constant(index)); } - private static Expression Indices(Expression scriptInfo) - { - return Expression.Field(scriptInfo, _indices); - } + //private static Expression Indices(Expression scriptInfo) + //{ + // return Expression.Field(scriptInfo, _indices); + //} public static Expression Build(Expression scriptInfo, StringArray keyStrings) { @@ -84,7 +98,8 @@ public static Expression Build(Expression scriptInfo, StringArray keyStrings) list.Add(key); } return Expression.Assign( - Expression.Field(scriptInfo, _indices), Expression.NewArrayInit(typeof(KeyString), list)); + scriptInfo.FieldExpression(() => (x) => x.Indices) + ,Expression.NewArrayInit(typeof(KeyString), list)); } } }