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 858a3e9 commit b0ae75b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
9 changes: 9 additions & 0 deletions YantraJS.Core/LambdaGen/NewLambdaExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public static Expression CallExpression<TIn, TOut>(
return Expression.Call(@this, m, args);
}

public static Expression CallExpression<T>(
this Expression @this,
Func<Expression<Action<T>>> fx,
params Expression[] args)
{
var m = TypeQuery.QueryInstanceMethod(fx);
return Expression.Call(@this, m, args);
}

public static Expression CallExpression<TIn, T, TOut>(
this Expression @this,
Func<Expression<Func<TIn, T, TOut>>> fx,
Expand Down
20 changes: 11 additions & 9 deletions YantraJS.Core/LinqExpressions/LexicalScopeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using SwitchCase = YantraJS.Expressions.YSwitchCaseExpression;
using GotoExpression = YantraJS.Expressions.YGoToExpression;
using TryExpression = YantraJS.Expressions.YTryCatchFinallyExpression;
using YantraJS.Core.LambdaGen;

namespace YantraJS.ExpHelper
{
Expand Down Expand Up @@ -62,18 +63,19 @@ public static Expression NewScope(
type.InternalMethod(nameof(CallStackItem.Update));


public static Expression Update(Expression exp, int line, int column, Expression next)
{
return Expression.Block(
Expression.Assign(Expression.Field(exp, _Line), Expression.Constant(line)),
Expression.Assign(Expression.Field(exp, _Column), Expression.Constant(column)),
next
);
}
//public static Expression Update(Expression exp, int line, int column, Expression next)
//{
// return Expression.Block(
// Expression.Assign(Expression.Field(exp, _Line), Expression.Constant(line)),
// Expression.Assign(Expression.Field(exp, _Column), Expression.Constant(column)),
// next
// );
//}

public static Expression Pop(Expression exp, Expression context)
{
return Expression.Call(exp, _Pop , context);
return exp.CallExpression<CallStackItem>(() => (x) => x.Pop(null as JSContext), context);
// return Expression.Call(exp, _Pop , context);
}

//public static Expression SetPosition(Expression exp, int line, int column)
Expand Down
33 changes: 22 additions & 11 deletions YantraJS.Core/LinqExpressions/StringSpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Reflection;
using System.Text;
using YantraJS.Core;

using YantraJS.Core.LambdaGen;
using Exp = YantraJS.Expressions.YExpression;
using Expression = YantraJS.Expressions.YExpression;
using ParameterExpression = YantraJS.Expressions.YParameterExpression;
Expand All @@ -16,26 +16,37 @@ public static class StringSpanBuilder

public static Type RefType = typeof(StringSpan).MakeByRefType();

public static Type type = typeof(StringSpan);
// public static Type type = typeof(StringSpan);

private static ConstructorInfo _new =
type.Constructor(typeof(string), typeof(int), typeof(int));
//private static ConstructorInfo _new =
// type.Constructor(typeof(string), typeof(int), typeof(int));

internal static Expression New(Expression code, int start, int v)
{
return Expression.New(_new, code, Expression.Constant(start), Expression.Constant(v));
//return Expression.New(_new, code, Expression.Constant(start), Expression.Constant(v));
return NewLambdaExpression.NewExpression<StringSpan>(() => () => new StringSpan("", 0, 0)
, code
, Expression.Constant(start)
, Expression.Constant(v)
);
}

internal static Expression New(in StringSpan code)
{
return Expression.New(_new,
Expression.Constant(code.Source),
Expression.Constant(code.Offset),
Expression.Constant(code.Length));
return NewLambdaExpression.NewExpression<StringSpan>(() => () => new StringSpan("", 0, 0)
, Expression.Constant(code.Source)
, Expression.Constant(code.Offset)
, Expression.Constant(code.Length)
);
//return Expression.New(_new,
// Expression.Constant(code.Source),
// Expression.Constant(code.Offset),
// Expression.Constant(code.Length));
}


public static readonly Expression Empty =
Expression.Field(null, type.GetField(nameof(StringSpan.Empty)));
public static readonly Expression Empty =
NewLambdaExpression.StaticFieldExpression<StringSpan>(() => () => StringSpan.Empty);
// Expression.Field(null, type.GetField(nameof(StringSpan.Empty)));
}
}

0 comments on commit b0ae75b

Please sign in to comment.