Skip to content

Commit

Permalink
Methods optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Feb 9, 2025
1 parent d7fefa8 commit 36887bb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
22 changes: 12 additions & 10 deletions YantraJS.Core/LinqExpressions/ObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object, string>(() => (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);
//}
}
}
81 changes: 48 additions & 33 deletions YantraJS.Core/LinqExpressions/ScriptInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ScriptInfo, string>(() => (x) => x.Code);
var _fileName = TypeQuery.QueryInstanceField<ScriptInfo, string>(() => (x) => x.FileName);
return
Expression.MemberInit(
NewLambdaExpression.NewExpression<ScriptInfo>(() => () => 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<ScriptInfo, string>(() => (x) => x.Code);
// return Expression.Field(scriptInfo, _code);
}

public static Expression FileName(Expression scriptInfo)
{
return Expression.Field(scriptInfo, _fileName);
return scriptInfo.FieldExpression<ScriptInfo, string>(() => (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<ScriptInfo, KeyString[]>(() => (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)
{
Expand All @@ -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<ScriptInfo, KeyString[]>(() => (x) => x.Indices)
,Expression.NewArrayInit(typeof(KeyString), list));
}
}
}

0 comments on commit 36887bb

Please sign in to comment.