Skip to content

Commit

Permalink
Methods optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Feb 24, 2025
1 parent 1cf9ab4 commit 843ee18
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions YantraJS.Core/LinqExpressions/JSTemplateStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,57 @@
using TryExpression = YantraJS.Expressions.YTryCatchFinallyExpression;
using YantraJS.Expressions;
using YantraJS.Core;
using YantraJS.Core.LambdaGen;
using YantraJS.Core.Types;

namespace YantraJS.ExpHelper
{
public class JSTemplateStringBuilder
{
private static Type type = typeof(JSTemplateString);
// private static Type type = typeof(JSTemplateString);

private static ConstructorInfo _new =
type.GetConstructor(new Type[] { typeof(int) });
//private static ConstructorInfo _new =
// type.GetConstructor(new Type[] { typeof(int) });

private static MethodInfo _addQuasi =
type.GetMethod(nameof(JSTemplateString.AddQuasi));
//private static MethodInfo _addQuasi =
// type.GetMethod(nameof(JSTemplateString.AddQuasi));

private static MethodInfo _addExpression =
type.GetMethod(nameof(JSTemplateString.AddExpression));
//private static MethodInfo _addExpression =
// type.GetMethod(nameof(JSTemplateString.AddExpression));

private static MethodInfo _toJSString =
type.GetMethod(nameof(JSTemplateString.ToJSString));
//private static MethodInfo _toJSString =
// type.GetMethod(nameof(JSTemplateString.ToJSString));

private static MethodInfo _addString =
type.PublicMethod(nameof(JSTemplateString.Add), typeof(string));
//private static MethodInfo _addString =
// type.PublicMethod(nameof(JSTemplateString.Add), typeof(string));

private static MethodInfo _addValue =
type.PublicMethod(nameof(JSTemplateString.Add), typeof(JSValue));
//private static MethodInfo _addValue =
// type.PublicMethod(nameof(JSTemplateString.Add), typeof(JSValue));

public static Expression New(IEnumerable<Expression> select, int total)
{
var list = new Sequence<YElementInit>();
var newExp = Expression.New(_new, Expression.Constant(total));
var newExp = NewLambdaExpression.NewExpression<JSTemplateString>(() => () => new JSTemplateString((int)0),
Expression.Constant(total));
// var newExp = Expression.New(_new, Expression.Constant(total));
var en = select.GetEnumerator();

var addStringMethod = TypeQuery.QueryInstanceMethod<JSTemplateString>(() => (x) => x.Add((string)""));
var addValueMethod = TypeQuery.QueryInstanceMethod<JSTemplateString>(() => (x) => x.Add((JSValue)null));

while (en.MoveNext())
{
var current = en.Current;
if (current.NodeType == YExpressionType.Constant)
{
// exp = Expression.Call(exp, _addQuasi, current);
list.Add(YExpression.ElementInit(_addString, current));
list.Add(YExpression.ElementInit(addStringMethod, current));
continue;
}
list.Add(YExpression.ElementInit(_addValue, current));
list.Add(YExpression.ElementInit(addValueMethod, current));
}
return Expression.Call( Expression.ListInit(newExp, list), _toJSString);
return Expression.ListInit(newExp, list)
.CallExpression<JSTemplateString>(() => (x) => x.ToJSString());
}

//public static Expression New(List<string> quasis, IEnumerable<Expression> select)
Expand Down

0 comments on commit 843ee18

Please sign in to comment.