|
15 | 15 | using TryExpression = YantraJS.Expressions.YTryCatchFinallyExpression; |
16 | 16 | using YantraJS.Expressions; |
17 | 17 | using YantraJS.Core; |
| 18 | +using YantraJS.Core.LambdaGen; |
| 19 | +using YantraJS.Core.Types; |
18 | 20 |
|
19 | 21 | namespace YantraJS.ExpHelper |
20 | 22 | { |
21 | 23 | public class JSTemplateStringBuilder |
22 | 24 | { |
23 | | - private static Type type = typeof(JSTemplateString); |
| 25 | + // private static Type type = typeof(JSTemplateString); |
24 | 26 |
|
25 | | - private static ConstructorInfo _new = |
26 | | - type.GetConstructor(new Type[] { typeof(int) }); |
| 27 | + //private static ConstructorInfo _new = |
| 28 | + // type.GetConstructor(new Type[] { typeof(int) }); |
27 | 29 |
|
28 | | - private static MethodInfo _addQuasi = |
29 | | - type.GetMethod(nameof(JSTemplateString.AddQuasi)); |
| 30 | + //private static MethodInfo _addQuasi = |
| 31 | + // type.GetMethod(nameof(JSTemplateString.AddQuasi)); |
30 | 32 |
|
31 | | - private static MethodInfo _addExpression = |
32 | | - type.GetMethod(nameof(JSTemplateString.AddExpression)); |
| 33 | + //private static MethodInfo _addExpression = |
| 34 | + // type.GetMethod(nameof(JSTemplateString.AddExpression)); |
33 | 35 |
|
34 | | - private static MethodInfo _toJSString = |
35 | | - type.GetMethod(nameof(JSTemplateString.ToJSString)); |
| 36 | + //private static MethodInfo _toJSString = |
| 37 | + // type.GetMethod(nameof(JSTemplateString.ToJSString)); |
36 | 38 |
|
37 | | - private static MethodInfo _addString = |
38 | | - type.PublicMethod(nameof(JSTemplateString.Add), typeof(string)); |
| 39 | + //private static MethodInfo _addString = |
| 40 | + // type.PublicMethod(nameof(JSTemplateString.Add), typeof(string)); |
39 | 41 |
|
40 | | - private static MethodInfo _addValue = |
41 | | - type.PublicMethod(nameof(JSTemplateString.Add), typeof(JSValue)); |
| 42 | + //private static MethodInfo _addValue = |
| 43 | + // type.PublicMethod(nameof(JSTemplateString.Add), typeof(JSValue)); |
42 | 44 |
|
43 | 45 | public static Expression New(IEnumerable<Expression> select, int total) |
44 | 46 | { |
45 | 47 | var list = new Sequence<YElementInit>(); |
46 | | - var newExp = Expression.New(_new, Expression.Constant(total)); |
| 48 | + var newExp = NewLambdaExpression.NewExpression<JSTemplateString>(() => () => new JSTemplateString((int)0), |
| 49 | + Expression.Constant(total)); |
| 50 | + // var newExp = Expression.New(_new, Expression.Constant(total)); |
47 | 51 | var en = select.GetEnumerator(); |
| 52 | + |
| 53 | + var addStringMethod = TypeQuery.QueryInstanceMethod<JSTemplateString>(() => (x) => x.Add((string)"")); |
| 54 | + var addValueMethod = TypeQuery.QueryInstanceMethod<JSTemplateString>(() => (x) => x.Add((JSValue)null)); |
| 55 | + |
48 | 56 | while (en.MoveNext()) |
49 | 57 | { |
50 | 58 | var current = en.Current; |
51 | 59 | if (current.NodeType == YExpressionType.Constant) |
52 | 60 | { |
53 | 61 | // exp = Expression.Call(exp, _addQuasi, current); |
54 | | - list.Add(YExpression.ElementInit(_addString, current)); |
| 62 | + list.Add(YExpression.ElementInit(addStringMethod, current)); |
55 | 63 | continue; |
56 | 64 | } |
57 | | - list.Add(YExpression.ElementInit(_addValue, current)); |
| 65 | + list.Add(YExpression.ElementInit(addValueMethod, current)); |
58 | 66 | } |
59 | | - return Expression.Call( Expression.ListInit(newExp, list), _toJSString); |
| 67 | + return Expression.ListInit(newExp, list) |
| 68 | + .CallExpression<JSTemplateString>(() => (x) => x.ToJSString()); |
60 | 69 | } |
61 | 70 |
|
62 | 71 | //public static Expression New(List<string> quasis, IEnumerable<Expression> select) |
|
0 commit comments