-
Notifications
You must be signed in to change notification settings - Fork 76
/
Parser.Emit.cs
498 lines (455 loc) · 19.1 KB
/
Parser.Emit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#region -- copyright --
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
#endregion
using System;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Neo.IronLua
{
#region -- enum ReflectionFlag ------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
/// <summary></summary>
internal enum ReflectionFlag
{
None = 0,
Static = 1,
Instance = 2,
NonPublic = 4,
Public = 8,
IgnoreCase = 16,
NoException = 32,
NoArguments = 64
} //enum ReflectionFlag
#endregion
#region -- class Parser -------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
/// <summary></summary>
internal static partial class Parser
{
private static readonly Func<Expression, Expression> getExpressionFunction = GetExpression;
private static readonly Func<Expression, Type> getExpressionTypeFunction = GetExpressionType;
#region -- Debug-Information ------------------------------------------------------
private static Expression WrapDebugInfo(bool lWrap, bool lAfter, Token tStart, Token tEnd, Expression expr)
{
if (lWrap)
{
if (lAfter)
{
if (expr.Type == typeof(void))
return Expression.Block(expr, GetDebugInfo(tStart, tEnd));
else
{
ParameterExpression r = Expression.Variable(expr.Type);
return Expression.Block(r.Type, new ParameterExpression[] { r },
Expression.Assign(r, expr),
GetDebugInfo(tStart, tEnd),
r);
}
}
else
return Expression.Block(GetDebugInfo(tStart, tEnd), expr);
}
else
return expr;
} // func WrapDebugInfo
private static Expression GetDebugInfo(Token tStart, Token tEnd)
{
return Expression.DebugInfo(tStart.Start.Document, tStart.Start.Line, tStart.Start.Col, tEnd.End.Line, tEnd.End.Col);
} // func GetDebugInfo
#endregion
#region -- Emit Helper ------------------------------------------------------------
private static Expression SafeExpression(Func<Expression> f, Token tokenStart)
{
try
{
return f();
}
catch (LuaEmitException e)
{
throw ParseError(tokenStart, e.Message);
}
} // func SafeExpression
private static Expression ConvertExpressionNoneEmit(Lua runtime, Token tokenStart, Expression expr, Type toType, bool forceType)
{
expr = LuaEmit.ConvertToSingleResultExpression(expr, null, toType, runtime.GetConvertBinder);
if (forceType) // force
return Lua.EnsureType(expr, typeof(object));
else
return expr;
} // func ConvertExpressionNoneEmit
private static Expression ConvertObjectExpression(Lua runtime, Token tokenStart, Expression expr, bool convertToObject = false)
{
return ConvertExpressionNoneEmit(runtime, tokenStart, expr, typeof(object), convertToObject);
} // func ConvertObjectExpression
private static Expression ConvertExpression(Lua runtime, Token tokenStart, Expression expr, Type toType)
{
// dynamic convert
if (LuaEmit.TryConvert(expr, expr.Type, toType, runtime.GetConvertBinder, out var result))
return (Expression)result;
else
throw ParseError(tokenStart, ((LuaEmitException)result).Message);
} // func ConvertExpression
private static Expression GetResultExpression(Lua runtime, Token tStart, Expression expr, int iIndex)
{
return SafeExpression(() => LuaEmit.GetResultExpression(expr, iIndex), tStart);
} // func GetResultExpression
private static Expression UnaryOperationExpression(Lua runtime, Token tStart, ExpressionType op, Expression expr)
{
if (op != ExpressionType.ArrayLength)
expr = ConvertObjectExpression(runtime, tStart, expr);
return SafeExpression(() => LuaEmit.UnaryOperationExpression(runtime, op, expr, expr.Type, true), tStart);
} // func UnaryOperationExpression
private static Expression BinaryOperationExpression(Lua runtime, Token tStart, ExpressionType op, Expression expr1, Expression expr2)
{
expr1 = ConvertObjectExpression(runtime, tStart, expr1);
expr2 = ConvertObjectExpression(runtime, tStart, expr2);
return SafeExpression(() => LuaEmit.BinaryOperationExpression(runtime, op, expr1, expr1.Type, expr2, expr2.Type, true), tStart);
} // func BinaryOperationExpression
private static Expression ConcatOperationExpression(Lua runtime, Token tStart, Expression[] args)
{
if (Array.Exists(args, c => LuaEmit.IsDynamicType(c.Type))) // we have a dynamic type in the list -> to the concat on runtime
{
return SafeExpression(() => Expression.Call(Lua.ConcatStringMethodInfo, Expression.NewArrayInit(typeof(object),
from e in args select ConvertObjectExpression(runtime, tStart, e, true))), tStart);
}
else
{
return SafeExpression(() => Expression.Call(Lua.StringConcatMethodInfo, Expression.NewArrayInit(typeof(string),
from e in args select ConvertExpression(runtime, tStart, e, typeof(string)))), tStart);
}
} // func ConcatOperationExpression
private static Expression MemberGetSandbox(Scope scope, Expression getMember, Expression instance, string sMember)
{
return scope.Options.SandboxCore(getMember, instance, sMember);
} // func MemberGetSandbox
private static Expression MemberGetExpressionCore(Lua lua, Token tokenStart, Expression instance, string memberName)
{
// if this is a dynamic type, let the type deside what is to do
if (LuaEmit.IsDynamicType(instance.Type))
return DynamicExpression.Dynamic(lua.GetGetMemberBinder(memberName), typeof(object), ConvertObjectExpression(lua, tokenStart, instance, true));
else
{
Expression result;
switch (LuaEmit.TryGetMember(instance, instance.Type, memberName, false, out result))
{
case LuaTryGetMemberReturn.None:
throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.MemberNotFound, instance.Type.Name, memberName));
case LuaTryGetMemberReturn.NotReadable:
throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.CanNotReadMember, instance.Type.Name, memberName));
case LuaTryGetMemberReturn.ValidExpression:
return result;
default:
throw new ArgumentException("Internal return type of TryGetMember");
}
}
} // func MemberGetExpressionCore
private static Expression MemberGetExpression(Scope scope, Token tokenStart, Expression instance, string memberName)
=> MemberGetSandbox(scope, MemberGetExpressionCore(scope.Runtime, tokenStart, instance, memberName), instance, memberName);
private static Expression MemberSetExpressionCore(Lua lua, Token tokenStart, Expression instance, string memberName, Expression set)
{
if (LuaEmit.IsDynamicType(instance.Type)) // first call the dynamic interface
{
return DynamicExpression.Dynamic(lua.GetSetMemberBinder(memberName), typeof(object),
ConvertObjectExpression(lua, tokenStart, instance, true),
ConvertObjectExpression(lua, tokenStart, set, true)
);
}
else
{
Expression result;
switch (LuaEmit.TrySetMember(instance, instance.Type, memberName, false, (setType) => ConvertExpression(lua, tokenStart, set, setType), out result))
{
case LuaTrySetMemberReturn.None:
throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.MemberNotFound, instance.Type.Name, memberName));
case LuaTrySetMemberReturn.NotWritable:
throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.CanNotWriteMember, instance.Type.Name, memberName));
case LuaTrySetMemberReturn.ValidExpression:
return result;
default:
throw new ArgumentException("Internal return type of TrySetMember");
}
}
} // func MemberSetExpressionCore
private static Expression MemberSetExpression(Lua runtime, Token tokenStart, Expression instance, string memberName, bool setMethodMember, Expression set)
{
// Assign the value to a member
if (setMethodMember)
{
return Expression.Call(
ConvertExpression(runtime, tokenStart, instance, typeof(LuaTable)),
Lua.TableDefineMethodLightMethodInfo,
Expression.Constant(memberName, typeof(string)),
ConvertExpression(runtime, tokenStart, set, typeof(Delegate))
);
}
else
return SafeExpression(() => MemberSetExpressionCore(runtime, tokenStart, instance, memberName, set), tokenStart);
} // func MemberSetExpression
private static Expression IndexGetExpression(Scope scope, Token tStart, Expression instance, Expression[] indexes)
{
if (instance.Type == typeof(LuaTable))
{
if (indexes.Length == 1)
{
var arg = indexes[0];
if (LuaTable.IsIndexKey(arg.Type)) // integer access
{
return MemberGetSandbox(
scope,
Expression.Call(instance, Lua.TableGetValueKeyIntMethodInfo,
ConvertExpression(scope.Runtime, tStart, arg, typeof(int)),
Expression.Constant(false)
),
instance, null
);
}
else if (arg.Type == typeof(string)) // member access
{
return MemberGetSandbox(
scope,
Expression.Call(instance, Lua.TableGetValueKeyStringMethodInfo,
arg,
Expression.Constant(false),
Expression.Constant(false)
),
instance, null
);
}
else // key access
{
return MemberGetSandbox(
scope,
Expression.Call(instance, Lua.TableGetValueKeyObjectMethodInfo,
ConvertObjectExpression(scope.Runtime, tStart, arg, true),
Expression.Constant(false)
),
instance, null
);
}
}
else
{
return MemberGetSandbox(
scope,
Expression.Call(instance, Lua.TableGetValueKeyListMethodInfo,
Expression.NewArrayInit(typeof(object), from i in indexes select ConvertObjectExpression(scope.Runtime, tStart, i, true)),
Expression.Constant(false)
),
instance, null
);
}
}
else if (instance.Type == typeof(LuaResult) && indexes.Length == 1)
{
return MemberGetSandbox(
scope,
Expression.MakeIndex(
instance,
Lua.ResultIndexPropertyInfo,
new Expression[] { ConvertExpression(scope.Runtime, tStart, indexes[0], typeof(int)) }
),
instance, null
);
}
else
return MemberGetSandbox(scope, SafeExpression(() => LuaEmit.GetIndex(scope.Runtime, instance, indexes, getExpressionFunction, getExpressionTypeFunction, true), tStart), instance, null);
} // func IndexGetExpression
private static Expression IndexSetExpression(Lua runtime, Token tStart, Expression instance, Expression[] indexes, Expression set)
{
if (instance.Type == typeof(LuaTable))
{
if (indexes.Length == 1)
{
var arg = indexes[0];
if (LuaTable.IsIndexKey(arg.Type)) // integer access
{
return Expression.Call(instance, Lua.TableSetValueKeyIntMethodInfo,
ConvertExpression(runtime, tStart, arg, typeof(int)),
ConvertObjectExpression(runtime, tStart, set, true),
Expression.Constant(false)
);
}
else if (arg.Type == typeof(string)) // member access
{
return Expression.Call(instance, Lua.TableSetValueKeyStringMethodInfo,
arg,
ConvertObjectExpression(runtime, tStart, set, true),
Expression.Constant(false),
Expression.Constant(false)
);
}
else // key access
{
return Expression.Call(instance, Lua.TableSetValueKeyObjectMethodInfo,
ConvertObjectExpression(runtime, tStart, arg, true),
ConvertObjectExpression(runtime, tStart, set, true),
Expression.Constant(false)
);
}
}
else
{
return Expression.Call(instance, Lua.TableSetValueKeyListMethodInfo,
Expression.NewArrayInit(typeof(object), from i in indexes select ConvertObjectExpression(runtime, tStart, i, true)),
ConvertObjectExpression(runtime, tStart, set, true),
Expression.Constant(false)
);
}
}
else
return SafeExpression(() => LuaEmit.SetIndex(runtime, instance, indexes, set, getExpressionFunction, getExpressionTypeFunction, true), tStart);
} // func IndexSetExpression
private static Expression InvokeExpression(Scope scope, Token tStart, Expression instance, InvokeResult result, ArgumentsList arguments, bool lParse)
{
MethodInfo mi;
ConstantExpression constInstance = instance as ConstantExpression;
LuaType t;
if (constInstance != null && (t = constInstance.Value as LuaType) != null && t.Type != null) // we have a type, bind the ctor
{
var type = t.Type;
var typeInfo = type.GetTypeInfo();
var ci =
typeInfo.IsValueType && arguments.Count == 0 ?
null :
LuaEmit.FindMember(typeInfo.DeclaredConstructors.Where(c => c.IsPublic), arguments.CallInfo, arguments.Expressions, getExpressionTypeFunction, false);
if (ci == null && !typeInfo.IsValueType)
throw ParseError(tStart, String.Format(Properties.Resources.rsMemberNotResolved, type.Name, "ctor"));
return SafeExpression(() => LuaEmit.BindParameter(scope.Runtime,
args => ci == null ? Expression.New(type) : Expression.New(ci, args),
ci == null ? new ParameterInfo[0] : ci.GetParameters(),
arguments.CallInfo,
arguments.Expressions,
getExpressionFunction, getExpressionTypeFunction, true), tStart);
}
else if (LuaEmit.IsDynamicType(instance.Type))
{
// fallback is a dynamic call
return EnsureInvokeResult(scope, tStart,
DynamicExpression.Dynamic(scope.Runtime.GetInvokeBinder(arguments.CallInfo),
typeof(object),
new Expression[] { ConvertExpression(scope.Runtime, tStart, instance, typeof(object)) }.Concat(
from c in arguments.Expressions select Lua.EnsureType(c, typeof(object))
)
),
result, instance, null
);
}
else if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(instance.Type.GetTypeInfo()) && // test if the type is assignable from delegate
(mi = instance.Type.GetRuntimeMethods().Where(c => !c.IsStatic && c.IsPublic && c.Name == "Invoke").FirstOrDefault()) != null) // Search the Invoke method for the arguments
{
return EnsureInvokeResult(scope, tStart,
SafeExpression(() => LuaEmit.BindParameter<Expression>(
scope.Runtime,
args => Expression.Invoke(instance, args),
mi.GetParameters(),
arguments.CallInfo,
arguments.Expressions,
getExpressionFunction, getExpressionTypeFunction, true), tStart),
result, instance, null
);
}
else
throw ParseError(tStart, LuaEmitException.GetMessageText(LuaEmitException.InvokeNoDelegate, instance.Type.Name));
} // func InvokeExpression
private static Expression InvokeMemberExpression(Scope scope, Token tStart, Expression instance, string memberName, InvokeResult result, ArgumentsList arguments)
{
if (LuaEmit.IsDynamicType(instance.Type) || arguments.Expressions.Any(c => LuaEmit.IsDynamicType(c.Type)))
{
var dynamicArguments = new Expression[arguments.Count + 1];
// first argument is the instance
dynamicArguments[0] = ConvertObjectExpression(scope.Runtime, tStart, instance, false);
if (arguments.Count > 0)
{
// single object
for (var i = 0; i < arguments.Count - 1; i++)
dynamicArguments[i + 1] = ConvertObjectExpression(scope.Runtime, tStart, arguments.Expressions[i], false);
// last argument is different
if (arguments.CallInfo.ArgumentNames.Count > 0)
dynamicArguments[dynamicArguments.Length - 1] = ConvertObjectExpression(scope.Runtime, tStart, arguments.Expressions[arguments.Count - 1], false);
else
dynamicArguments[dynamicArguments.Length - 1] = Lua.EnsureType(arguments.Expressions[arguments.Count - 1], typeof(object));
}
return EnsureInvokeResult(scope, tStart,
DynamicExpression.Dynamic(scope.Runtime.GetInvokeMemberBinder(memberName, arguments.CallInfo), typeof(object), dynamicArguments),
result, instance, memberName
);
}
else
{
return EnsureInvokeResult(scope, tStart,
SafeExpression(() =>
{
Expression expr;
if (!LuaEmit.TryInvokeMember<Expression>(scope.Runtime, LuaType.GetType(instance.Type), instance, arguments.CallInfo, arguments.Expressions, memberName, false, e => e, e => e.Type, true, out expr))
throw new LuaEmitException(LuaEmitException.MemberNotFound, instance.Type, memberName);
return expr;
}, tStart), result, instance, memberName
);
}
} // func InvokeMemberExpression
private static Expression EnsureInvokeResult(Scope scope, Token tStart, Expression expr, InvokeResult result, Expression instance, string memberName)
{
switch (result)
{
case InvokeResult.LuaResult:
if (expr.Type == typeof(object))
return ConvertExpression(scope.Runtime, tStart, expr, typeof(LuaResult));
else
return MemberGetSandbox(scope, expr, instance, memberName);
case InvokeResult.Object:
if (LuaEmit.IsDynamicType(expr.Type))
return MemberGetSandbox(scope, DynamicExpression.Dynamic(scope.Runtime.GetConvertBinder(typeof(object)), typeof(object), ConvertExpression(scope.Runtime, tStart, expr, typeof(object))), instance, memberName);
else
return MemberGetSandbox(scope, expr, instance, memberName);
default:
return MemberGetSandbox(scope, expr, instance, memberName);
}
} // func EnsureInvokeResult
#endregion
internal static Type GetDelegateType(MethodInfo mi)
{
return Expression.GetDelegateType(
(
from p in mi.GetParameters()
select p.ParameterType
).Concat(
new Type[] { mi.ReturnType }
).ToArray()
);
} // func GetDelegateType
internal static Delegate CreateDelegate(object firstArgument, MethodInfo mi)
{
if ((mi.CallingConvention & CallingConventions.VarArgs) != 0)
throw new ArgumentException("Call of VarArgs not implemented.");
return mi.CreateDelegate(GetDelegateType(mi), firstArgument);
} // func CreateDelegateFromMethodInfo
private static Type GetExpressionType(Expression e)
{
return e.Type;
} // func GetExpressionType
private static Expression GetExpression(Expression e)
{
return e;
} // func GetExpression
} // class Parser
#endregion
}