public BoundExpression GetOrCreateBoundExpression(IBindingContext binder, BoundExpressionOptions options) { Require.NotNull(binder, "binder"); Require.NotNull(options, "options"); var key = new CacheKey( _dynamicExpression.ParseResult.Identifiers, !DynamicExpression.IsLanguageCaseSensitive(_dynamicExpression.Language), binder, options ); lock (_syncRoot) { if (!_cache.TryGetValue(key, out var boundExpression)) { boundExpression = new BoundExpression( _dynamicExpression, key.OwnerType, key.Imports, key.IdentifierTypes, key.Options ); _cache.Add(key, boundExpression); } return(boundExpression); } }
public BoundExpression GetOrCreateBoundExpression(IBindingContext binder, BoundExpressionOptions options) { Require.NotNull(binder, "binder"); Require.NotNull(options, "options"); var key = new CacheKey( _dynamicExpression.ParseResult.Identifiers, !DynamicExpression.IsLanguageCaseSensitive(_dynamicExpression.Language), binder, options ); lock (_syncRoot) { BoundExpression boundExpression; if (!_cache.TryGetValue(key, out boundExpression)) { boundExpression = new BoundExpression( _dynamicExpression, key.OwnerType, key.Imports, key.IdentifierTypes, key.Options ); _cache.Add(key, boundExpression); } return boundExpression; } }
public CacheKey(IdentifierCollection identifiers, bool ignoreCase, IBindingContext bindingContext, BoundExpressionOptions options) { Options = options; OwnerType = bindingContext.OwnerType; var imports = bindingContext.Imports; Imports = new Import[imports == null ? 0 : imports.Count]; if (Imports.Length > 0) { imports.CopyTo(Imports, 0); } IdentifierTypes = new Type[identifiers.Count]; for (int i = 0; i < identifiers.Count; i++) { IdentifierTypes[i] = bindingContext.GetVariableType( identifiers[i].Name, ignoreCase ); } }
/// <summary> /// Invokes the expression with the specified expression context and /// binding options. /// </summary> /// <param name="expressionContext">The expression context used to /// bind and execute the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The result of the expression.</returns> public object Invoke(IExpressionContext expressionContext, BoundExpressionOptions options) { if (expressionContext == null) { expressionContext = new ExpressionContext(); } return(Bind(expressionContext, options).Invoke(expressionContext)); }
internal BoundExpressionOptions(BoundExpressionOptions other) { Require.NotNull(other, "other"); AllowPrivateAccess = other.AllowPrivateAccess; Checked = other.Checked; ResultType = other.ResultType; RestrictedSkipVisibility = other.RestrictedSkipVisibility; }
/// <summary> /// Binds the compiled expression with the specified binding context and options. /// </summary> /// <param name="binder">The binding context used to bind the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The bound expression.</returns> public IBoundExpression Bind(IBindingContext binder, BoundExpressionOptions options) { if (binder == null) { binder = new ExpressionContext(); } if (options == null) { options = new BoundExpressionOptions(); } options.Freeze(); return(Cached.GetOrCreateBoundExpression(binder, options)); }
private DynamicSignature CompileExpression(IExpression expression, Type ownerType, Import[] imports, Type[] identifierTypes, BoundExpressionOptions options, Resolver resolver) { var method = new DynamicMethod( "DynamicMethod", typeof(object), new[] { typeof(object[]) }, options.RestrictedSkipVisibility ); var il = method.GetILGenerator(); new Compiler(il, resolver).Compile(expression); #if DEBUG //WriteToDisk(expression, resolver); #endif return (DynamicSignature)method.CreateDelegate(typeof(DynamicSignature)); }
internal BoundExpression(CachedDynamicExpression dynamicExpression, Type ownerType, Import[] imports, Type[] identifierTypes, BoundExpressionOptions options) { Require.NotNull(dynamicExpression, "dynamicExpression"); Require.NotNull(imports, "imports"); Require.NotNull(identifierTypes, "identifierTypes"); _dynamicExpression = dynamicExpression; _parameterMap = BuildParameterMap(ownerType, identifierTypes); var resolver = new Resolver(_dynamicExpression, ownerType, imports, identifierTypes, _parameterMap, options); var resolvedTree = resolver.Resolve(_dynamicExpression.ParseResult.RootNode); #if DEBUG ResolvedExpression = resolvedTree; #endif _compiledMethod = CompileExpression(resolvedTree, ownerType, imports, identifierTypes, options, resolver); }
public Resolver(CachedDynamicExpression dynamicExpression, Type ownerType, Import[] imports, Type[] identifierTypes, int[] parameterMap, BoundExpressionOptions options) { Require.NotNull(dynamicExpression, "dynamicExpression"); Require.NotNull(imports, "imports"); Require.NotNull(identifierTypes, "identifierTypes"); Require.NotNull(parameterMap, "parameterMap"); DynamicExpression = dynamicExpression; OwnerType = ownerType; Imports = imports; IdentifierTypes = identifierTypes; Options = options; // Inverse the parameter map. IdentifierIndexes = new int[IdentifierTypes.Length]; for (int i = 0; i < IdentifierTypes.Length; i++) { IdentifierIndexes[i] = -1; if (IdentifierTypes[i] != null) { for (int j = 0; j < parameterMap.Length; j++) { if (parameterMap[j] == i) { IdentifierIndexes[i] = j; break; } } Debug.Assert(IdentifierIndexes[i] != -1); } } IgnoreCase = !global::Expressions.DynamicExpression.IsLanguageCaseSensitive(DynamicExpression.Language); }
public CacheKey(IdentifierCollection identifiers, bool ignoreCase, IBindingContext bindingContext, BoundExpressionOptions options) { Options = options; OwnerType = bindingContext.OwnerType; var imports = bindingContext.Imports; Imports = new Import[imports == null ? 0 : imports.Count]; if (Imports.Length > 0) imports.CopyTo(Imports, 0); IdentifierTypes = new Type[identifiers.Count]; for (int i = 0; i < identifiers.Count; i++) { IdentifierTypes[i] = bindingContext.GetVariableType( identifiers[i].Name, ignoreCase ); } }
/// <summary> /// Invokes the expression with the specified expression context and /// binding options. /// </summary> /// <param name="expressionContext">The expression context used to /// bind and execute the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The result of the expression.</returns> public object Invoke(IExpressionContext expressionContext, BoundExpressionOptions options) { var boundExpression = Bind(expressionContext ?? new ExpressionContext(), options); return(boundExpression.Invoke(expressionContext)); }
/// <summary> /// Binds the compiled expression with the specified binding context and options. /// </summary> /// <param name="binder">The binding context used to bind the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The bound expression.</returns> public IBoundExpression Bind(IBindingContext binder, BoundExpressionOptions options) { if (binder == null) binder = new ExpressionContext(); if (options == null) options = new BoundExpressionOptions(); options.Freeze(); return Cached.GetOrCreateBoundExpression(binder, options); }
private DynamicSignature CompileExpression(IExpression expression, Type ownerType, Import[] imports, Type[] identifierTypes, BoundExpressionOptions options, Resolver resolver) { var method = new DynamicMethod( "DynamicMethod", typeof(object), new[] { typeof(object[]) }, options.RestrictedSkipVisibility ); var il = method.GetILGenerator(); new Compiler(il, resolver).Compile(expression); #if DEBUG //WriteToDisk(expression, resolver); #endif return((DynamicSignature)method.CreateDelegate(typeof(DynamicSignature))); }
public BoundExpression GetOrCreateBoundExpression(IBindingContext binder, BoundExpressionOptions options) { return(_boundExpressionCache.GetOrCreateBoundExpression(binder, options)); }
/// <summary> /// Invokes the expression with the specified expression context and /// binding options. /// </summary> /// <param name="expressionContext">The expression context used to /// bind and execute the expression.</param> /// <param name="options">The options used to bind the expression.</param> /// <returns>The result of the expression.</returns> public object Invoke(IExpressionContext expressionContext, BoundExpressionOptions options) { if (expressionContext == null) expressionContext = new ExpressionContext(); return Bind(expressionContext, options).Invoke(expressionContext); }
private Func <object[], object> CompileExpression(IExpression expression, Type ownerType, Import[] imports, Type[] identifierTypes, BoundExpressionOptions options, Resolver resolver) { var method = new DynamicMethod( "DynamicMethod", typeof(object), new[] { typeof(object) }, typeof(BoundExpression).Module, options.RestrictedSkipVisibility ); var generator = method.GetILGenerator(); new Compiler(generator, resolver).Compile(expression); return((Func <object[], object>)method.CreateDelegate(typeof(Func <object[], object>))); }
public BoundExpression GetOrCreateBoundExpression(IBindingContext binder, BoundExpressionOptions options) { return _boundExpressionCache.GetOrCreateBoundExpression(binder, options); }