private void AssertFunctionResults(string expressionString, float[,] calls, object[] results, string[] paramNames = null) { var expressionCompiler = new ExpressionCompiler(paramNames == null ? new string[0] : paramNames); var operatorData = expressionCompiler.CompileToExpression(expressionString); var builder = new DynamicExpressionData(operatorData, expressionCompiler.parameters.Values.ToArray()); using var nativeOpData = new NativeArray <OperatorDefinition>(builder.OperatorSpaceNeeded, Allocator.Persistent); var paramSize = (ushort)calls.GetLength(1); var inputParams = new NativeArray <float>(paramSize, Allocator.Persistent); var opDataSpace = new JaggedIndexing { index = 0, length = builder.OperatorSpaceNeeded }; var expression = builder.WriteIntoOpDataArray( nativeOpData, opDataSpace); for (int call = 0; call < calls.GetLength(0); call++) { for (int param = 0; param < calls.GetLength(1); param++) { inputParams[param] = calls[call, param]; } var result = expression.EvaluateExpression( inputParams, new JaggedIndexing { index = 0, length = paramSize }, nativeOpData); if (results[call] is float floatVal) { Assert.AreEqual(result, floatVal); } else if (results[call] is bool boolValue) { if (boolValue) { Assert.IsTrue(result > 0); } else { Assert.IsFalse(result > 0); } } } inputParams.Dispose(); }
/// <summary> /// Given a targeted binding expression (in the form "foo->bar"), this method extracts the target name, target type, and expression text. /// </summary> private Boolean GetExpressionTargetInfo(ExpressionCompilerState state, DataSourceWrapperInfo dataSourceWrapperInfo, XObject source, ref String expText, out String expTarget, out Type expTargetType) { const string TargetExpressionDelimiter = "->"; var expOriginal = expText; var delimiterIndex = expText.IndexOf(TargetExpressionDelimiter); if (delimiterIndex >= 0) { var expPartTarget = expText.Substring(0, delimiterIndex); var expPartText = expText.Substring(delimiterIndex + TargetExpressionDelimiter.Length); var matchCandidates = (from element in dataSourceWrapperInfo.DataSourceDefinition.Definition.Descendants() where (String)element.Attribute("Name") == expPartTarget select element).ToList(); if (matchCandidates.Count == 0) { throw new BindingExpressionCompilationErrorException(source, dataSourceWrapperInfo.DataSourceDefinition.DefinitionPath, CompilerStrings.ExpressionTargetIsNotFound.Format(expPartTarget)); } if (matchCandidates.Count > 1) { throw new BindingExpressionCompilationErrorException(source, dataSourceWrapperInfo.DataSourceDefinition.DefinitionPath, CompilerStrings.ExpressionTargetIsAmbiguous.Format(expPartTarget)); } var match = matchCandidates.Single(); var matchName = match.Name.LocalName; expText = expPartText; expTargetType = ExpressionCompiler.GetPlaceholderType(dataSourceWrapperInfo.DataSourceType, matchName); if (expTargetType == null && !state.GetKnownType(matchName, out expTargetType)) { throw new BindingExpressionCompilationErrorException(source, dataSourceWrapperInfo.DataSourceDefinition.DefinitionPath, CompilerStrings.ExpressionTargetIsUnrecognizedType.Format(expOriginal, matchName)); } expTarget = String.Format("__UPF_GetElementByName<{0}>(\"{1}\").", GetCSharpTypeName(expTargetType), expPartTarget); return(true); } expTarget = default(String); expTargetType = dataSourceWrapperInfo.DataSourceType; return(false); }
public MethodBuilder DefineMethod( string methodName, ParameterExpression[] paramExprs, Expression bodyExpr, MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.Virtual, CallingConventions callingConventions = CallingConventions.Standard | CallingConventions.HasThis) { if (paramExprs == null) { paramExprs = Array.Empty <ParameterExpression>(); } Type[] paramTypes = new Type[paramExprs.Length]; string[] paramNames = new string[paramExprs.Length]; for (int i = 0; i < paramExprs.Length; i++) { paramTypes[i] = paramExprs[i].Type; paramNames[i] = paramExprs[i].Name; } if (callingConventions.HasFlag(CallingConventions.HasThis)) { ParameterExpression[] newParams = new ParameterExpression[paramExprs.Length + 1]; newParams[0] = This; Array.Copy(paramExprs, 0, newParams, 1, paramExprs.Length); paramExprs = newParams; } MethodBuilder newMethod = TypeBuilder.DefineMethod( methodName, methodAttributes, callingConventions, bodyExpr.Type, paramTypes); for (int i = 0; i < paramTypes.Length; i++) { newMethod.DefineParameter(i, ParameterAttributes.None, paramNames[i]); } bool success = ExpressionCompiler.CompileForTypeBuilder(paramExprs, bodyExpr, bodyExpr.Type, newMethod.GetILGenerator(), true); if (!success) { throw new InvalidOperationException($"Can't compile method"); } Methods.Add(new RuntimeTypeMethod(methodName, newMethod, paramTypes, bodyExpr.Type)); return(newMethod); }
/// <summary> /// Compiles the static. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <returns></returns> public Func <IServiceProvider, T, T2, TResult> CompileStatic <TResult>() { if (GetMethodInfo()?.IsStatic != true) { throw new NotSupportedException("Method must be a static method to compile as an static methods!"); } var(body, parameters) = base.Compile( typeof(T).GetTypeInfo(), typeof(T2).GetTypeInfo()); var lambda = Expression.Lambda <Func <IServiceProvider, T, T2, TResult> >(body, parameters); return(ExpressionCompiler.CompileFast(lambda)); }
/// <summary> /// Compiles this instance. /// </summary> /// <returns></returns> public Action <object, IServiceProvider, T, T2> Compile() { if (GetMethodInfo()?.IsStatic == true) { throw new NotSupportedException("Method must not be a static method to compile as an instance methods!"); } var(body, parameters) = base.Compile( typeof(T).GetTypeInfo(), typeof(T2).GetTypeInfo()); var lambda = Expression.Lambda <Action <object, IServiceProvider, T, T2> >(body, parameters); return(ExpressionCompiler.CompileFast(lambda)); }
public void Creates_Subquery_Expression() { Expression <Func <JObject, IEnumerable <int> > > expected = data => (IEnumerable <int>)Rewritten.List.Select( Rewritten.Interface.Cast(data["data"]["node"], "Repository")["issues"]["nodes"], issue => issue["number"].ToObject <int>()).ToList(); var expectedString = expected.ToReadableString(); var actual = ExpressionCompiler.GetSourceExpression(TestQueryFirstSubquery().ResultBuilder); var actualString = actual.ToReadableString(); Assert.Equal(ExpressionRewriterAssertions.StripWhitespace(expectedString), ExpressionRewriterAssertions.StripWhitespace(actualString)); }
public void ShouldTryAgain_Exception() { DkmUtilities.GetMetadataBytesPtrFunction gmdbpf = (AssemblyIdentity assemblyIdentity, out uint uSize) => { throw new Exception(); }; var references = ImmutableArray <MetadataBlock> .Empty; var missingAssemblyIdentities = ImmutableArray.Create(new AssemblyIdentity("A")); Assert.Throws <Exception>(() => ExpressionCompiler.ShouldTryAgainWithMoreMetadataBlocks(gmdbpf, missingAssemblyIdentities, ref references)); }
private IEnumerable <ServiceBinding> Resolve <TElement>(IInstanceFactoryResolver resolver, Type type) { foreach (InstanceFactory instanceFactory in resolver.TryResolveAll(typeof(TElement))) { var newBinding = new ServiceBinding(type, BindingMetadata.GeneratedInstance, instanceFactory.Context.Expression, instanceFactory.Context.Expression.Type, ConstructorResolvers.Default, Lifetimes.Transient); var expression = Expression.Lambda <Func <TElement> >(ExpressionCompiler.OptimizeExpression(instanceFactory.Context)); Func <Scoped, Expression <Func <TElement> > > del = scoped => expression; // we need to put this in a variable of this type or cast it else the static return type of the delegate will turn into a object.. var factory = new InstanceFactory(type, new ExpressionContext(expression), del); newBinding.Factories.Add(factory); yield return(newBinding); } }
public void ShouldTryAgain_RPC_E_DISCONNECTED() { IntPtr gmdbpf(AssemblyIdentity assemblyIdentity, out uint uSize) { Marshal.ThrowExceptionForHR(unchecked ((int)0x80010108)); throw ExceptionUtilities.Unreachable; } var references = ImmutableArray <MetadataBlock> .Empty; var missingAssemblyIdentities = ImmutableArray.Create(new AssemblyIdentity("A")); Assert.Throws <COMException>(() => ExpressionCompiler.ShouldTryAgainWithMoreMetadataBlocks(gmdbpf, missingAssemblyIdentities, ref references)); }
public void RegisteredTypeExtensionMethodTests() { ExpressionCompiler <string> regExtTest = new ExpressionCompiler <string>($"\"{RadarConst}\".PadLeft(12)"); regExtTest.TypeRegistry.RegisterType(typeof(StringExtensions)); Assert.IsTrue(regExtTest.ExecuteFunction().Trim().Equals(RadarConst)); System.Console.WriteLine($"Context Type for '{nameof(regExtTest)}': {regExtTest.TypeRegistry.GetContextType<string, object>()?.FullName}"); System.Console.WriteLine($"Expression for '{nameof(regExtTest)}': {regExtTest}"); System.Console.WriteLine($"Generated Context Type Count = {TypeRegistry.GeneratedContextTypeCount:N0}"); }
private void BuildSelectionNode(ReteBuilderContext context, ConditionElement condition) { var alphaCondition = ExpressionCompiler.CompileAlphaCondition(condition); SelectionNode selectionNode = context.CurrentAlphaNode .ChildNodes.OfType <SelectionNode>() .FirstOrDefault(sn => sn.Condition.Equals(alphaCondition)); if (selectionNode == null) { selectionNode = new SelectionNode(alphaCondition); context.CurrentAlphaNode.ChildNodes.Add(selectionNode); } context.CurrentAlphaNode = selectionNode; }
public void ShouldTryAgain_COR_E_BADIMAGEFORMAT() { DkmUtilities.GetMetadataBytesPtrFunction gmdbpf = (AssemblyIdentity assemblyIdentity, out uint uSize) => { Marshal.ThrowExceptionForHR(unchecked ((int)MetadataUtilities.COR_E_BADIMAGEFORMAT)); throw ExceptionUtilities.Unreachable; }; var references = ImmutableArray <MetadataBlock> .Empty; var missingAssemblyIdentities = ImmutableArray.Create(new AssemblyIdentity("A")); Assert.False(ExpressionCompiler.ShouldTryAgainWithMoreMetadataBlocks(gmdbpf, missingAssemblyIdentities, ref references)); Assert.Empty(references); }
public void TestCompilePerformance2() { Func <string, Expression <Func <Z, bool> > > f = qzz => z => z.Q[0].Qzz == qzz; for (int i = 0; i < 100000; i++) { var g = ExpressionCompiler.Compile(f(i.ToString())); Assert.That(g(new Z { Q = new[] { new Q { Qzz = i.ToString() } } })); } }
public static CompiledTemplate Compile(Template template, NameResolver nameResolver) { if (template == null) { throw new ArgumentNullException(nameof(template)); } return(template switch { LiteralText text => new CompiledLiteralText(text.Text), FormattedExpression expression => new CompiledFormattedExpression( ExpressionCompiler.Compile(expression.Expression, nameResolver), expression.Format, expression.Alignment), TemplateBlock block => new CompiledTemplateBlock(block.Elements.Select(e => Compile(e, nameResolver)).ToArray()), _ => throw new NotSupportedException() });
public StringIdentifiedAggregateFinder() { var idMember = DocumentMapping.FindIdMember(typeof(T)); var docParam = Expression.Parameter(typeof(T), "doc"); var keyParam = Expression.Parameter(typeof(string), "key"); var member = Expression.PropertyOrField(docParam, idMember.Name); var assign = Expression.Assign(member, keyParam); var lambda = Expression.Lambda <Action <T, string> >(assign, docParam, keyParam); _setId = ExpressionCompiler.Compile <Action <T, string> >(lambda); }
public BenchmarkRuleAction() { Expression <Action <IContext, string, int, decimal> > expression = (c, s, i, d) => PerformAction(c, s, i, d); var element = Element.Action(expression); var map = IndexMap.CreateMap(element.Imports, element.Imports); _ruleAction = ExpressionCompiler.CompileAction(element, element.Imports.ToList(), new List <DependencyElement>(), map); var compiledRule = new CompiledRule(null, element.Imports, new [] { _ruleAction }, null, map); var tuple = ToTuple("abcd", 4, 1.0m); var activation = new Activation(compiledRule, tuple); _actionContext = new ActionContext(Context.Session, activation, CancellationToken.None); }
public static Func <TTarget, TProperty> GetProperty <TTarget, TProperty>(PropertyInfo property) { var target = Expression.Parameter(property.DeclaringType, "target"); var method = property.GetGetMethod(); var callGetMethod = Expression.Call(target, method); var lambda = method.ReturnType == typeof(TProperty) ? Expression.Lambda <Func <TTarget, TProperty> >(callGetMethod, target) : Expression.Lambda <Func <TTarget, TProperty> >(Expression.Convert(callGetMethod, typeof(TProperty)), target); return(ExpressionCompiler.Compile <Func <TTarget, TProperty> >(lambda)); }
public void Logical_and_or_and() { var f = false; var t = true; var x = 1; var s = "Test"; Expression <Func <bool> > expr = () => (f || x == 1) && (s.Contains("S") || s.Contains("s")) || t; var dlg = ExpressionCompiler.TryCompile <Func <bool> >(expr); Assert.IsNotNull(dlg); Assert.IsTrue(dlg()); }
protected virtual int GetOutputCount(OutputItem outputItem, Object dropInItem) { var countExpression = outputItem?.Count ?? Output.Count; var calculateCount = ExpressionCompiler.CompileExpression <CountExpression>(countExpression); if (calculateCount == null) { return(1); } var random = GetRandom(); return(calculateCount(dropInItem.Price, dropInItem.quality, random.NextDouble(), random.NextDouble())); }
protected virtual int GetOutputQuality(OutputItem outputItem, Object dropInItem) { var qualityExpression = outputItem?.Quality ?? Output.Quality; var calculateQuality = ExpressionCompiler.CompileExpression <QualityExpression>(qualityExpression); if (calculateQuality == null) { return(0); } var random = GetRandom(); return(calculateQuality(dropInItem.Price, dropInItem.quality, random.NextDouble(), random.NextDouble())); }
private static MondProgram CompileImpl(Expression expression, MondCompilerOptions options) { options = options ?? new MondCompilerOptions(); expression.SetParent(null); expression.Simplify(); //using (var writer = new IndentTextWriter(Console.Out, " ")) // expression.Print(writer); var compiler = new ExpressionCompiler(options); return(compiler.Compile(expression)); }
public bool Parse(ExpressionCompiler compiler) { int i = compiler.Pos; int start = -1; char ch; while (i < compiler.Expression.Length) { ch = compiler.Expression[i++]; if (start == -1) { if (char.IsWhiteSpace(ch)) { continue; } else if (char.IsDigit(ch)) { start = i - 1; } else { return(false); } } else if (!char.IsDigit(ch)) { --i; break; } } if (start != -1) { string s = new string(compiler.Expression, start, i - start); s = s.Trim(); try { IntegerToken token = new IntegerToken(int.Parse(s), compiler.Pos); compiler.Parent.AddChild(token); compiler.Pos = i; return(true); } catch (System.FormatException e) { throw new ParserException("integer", compiler.Pos, "Unable to parse as int: " + s, e); } } return(false); }
public void Compile_ShouldCreateDelegate() { var comp = new ExpressionCompiler <Func <string, int> >(); var lengthPi = typeof(string).GetProperty("Length"); var str = comp.Parameter <string>("str"); var strLength = comp.Property(str, lengthPi); comp.Emit(strLength); var dlg = comp.Compile(); Assert.NotNull(dlg); Assert.Equal(4, dlg("four")); }
public void When_using_fast_expression_compilation() { var id = Guid.NewGuid(); var instance = new TestTarget(); var expr = CreateWriter <TestTarget>(); // both ExpressionCompiler.Compile and .CompileFast should work with action var write = ExpressionCompiler.Compile <Action <TestTarget, Guid> >(expr); write(instance, id); Assert.AreEqual(instance.ID, new CustomID(id)); }
private static void ShouldTryAgain_False(DkmUtilities.GetMetadataBytesPtrFunction gmdbpf) { var references = ImmutableArray <MetadataBlock> .Empty; var missingAssemblyIdentities = ImmutableArray.Create(new AssemblyIdentity("A")); Assert.False( ExpressionCompiler.ShouldTryAgainWithMoreMetadataBlocks( gmdbpf, missingAssemblyIdentities, ref references ) ); Assert.Empty(references); }
public AggregateFinder() { var idMember = DocumentMapping.FindIdMember(typeof(T)); var docParam = Expression.Parameter(typeof(T), "doc"); var idParam = Expression.Parameter(typeof(Guid), "id"); var member = Expression.PropertyOrField(docParam, idMember.Name); var assign = Expression.Assign(member, idParam); var lambda = Expression.Lambda <Action <T, Guid> >(assign, docParam, idParam); _setId = ExpressionCompiler.Compile <Action <T, Guid> >(lambda); }
public void Can_assign_Readonly_member_of_existing_object() { var a = new A(); var aConstExpr = Constant(a); var expr = Lambda <Func <A> >( MemberInit( aConstExpr, Bind(typeof(A).GetTypeInfo().DeclaredFields.First(m => m.Name == "R"), Constant(24)))); var f = ExpressionCompiler.TryCompile(expr); Assert.AreEqual(24, f().R); }
internal static CompileResult CompileExpressionWithRetry( ImmutableArray <MetadataBlock> metadataBlocks, EvaluationContextBase context, ExpressionCompiler.CompileDelegate <CompileResult> compile, DkmUtilities.GetMetadataBytesPtrFunction getMetaDataBytesPtr, out string errorMessage) { return(ExpressionCompiler.CompileWithRetry( metadataBlocks, DebuggerDiagnosticFormatter.Instance, (blocks, useReferencedModulesOnly) => context, compile, getMetaDataBytesPtr, out errorMessage)); }
private string ParseWithTypeLookup(string code, out SyntaxParser parser) { parser = Parser(code); var ast = parser.Parse(); var compiler = new ExpressionCompiler(); var expression = compiler.Compile(ast); if (compiler.Errors.Count > 0) { throw new Exception(string.Join(" ", compiler.Errors)); } var codeGenerator = new XzaarScriptCodeFormatter(); return(codeGenerator.Visit(expression).TrimEnd('\r', '\n')); }
private void BuildSelectionNode(ReteBuilderContext context, ExpressionElement element) { SelectionNode node = context.CurrentAlphaNode .ChildNodes.OfType <SelectionNode>() .FirstOrDefault(sn => ExpressionElementComparer.AreEqual(sn.ExpressionElement, element)); if (node == null) { var compiledExpression = ExpressionCompiler.CompileLhsFactExpression <bool>(element); node = new SelectionNode(element, compiledExpression); context.CurrentAlphaNode.ChildNodes.Add(node); } node.NodeInfo.Add(context.Rule, element); context.CurrentAlphaNode = node; }
private void EvaluateCommand_Click(object sender, EventArgs e) { try { var compiler = new ExpressionCompiler(); var function = compiler.Compile(ScriptInput.Text); var input = compiler.LastExpressionParams.ToArray(); var form = new ValuesInputForm(input); if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var result = function(form.Values); MessageBox.Show(string.Format("The result is: {0}", result), "Result"); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
internal StatementCompiler(IMetadataImporter metadataImporter, INamer namer, IErrorReporter errorReporter, ICompilation compilation, CSharpAstResolver resolver, IDictionary<IVariable, VariableData> variables, IDictionary<LambdaResolveResult, NestedFunctionData> nestedFunctions, IRuntimeLibrary runtimeLibrary, string thisAlias, ISet<string> usedVariableNames, NestedFunctionContext nestedFunctionContext, IMethod methodBeingCompiled, ITypeDefinition typeBeingCompiled, ExpressionCompiler expressionCompiler, SharedValue<int> nextLabelIndex, IVariable currentVariableForRethrow, IDictionary<object, string> currentGotoCaseMap) { _metadataImporter = metadataImporter; _namer = namer; _errorReporter = errorReporter; _compilation = compilation; _resolver = resolver; _variables = variables; _nestedFunctions = nestedFunctions; _runtimeLibrary = runtimeLibrary; _thisAlias = thisAlias; _usedVariableNames = usedVariableNames; _nestedFunctionContext = nestedFunctionContext; _methodBeingCompiled = methodBeingCompiled; _typeBeingCompiled = typeBeingCompiled; _currentVariableForRethrow = currentVariableForRethrow; _currentGotoCaseMap = currentGotoCaseMap; _nextLabelIndex = nextLabelIndex ?? new SharedValue<int>(1); _expressionCompiler = expressionCompiler ?? new ExpressionCompiler(compilation, metadataImporter, namer, runtimeLibrary, errorReporter, variables, nestedFunctions, v => CreateTemporaryVariable(v, _region), c => new StatementCompiler(_metadataImporter, _namer, _errorReporter, _compilation, _resolver, _variables, _nestedFunctions, _runtimeLibrary, thisAlias, _usedVariableNames, c, _methodBeingCompiled, _typeBeingCompiled), thisAlias, nestedFunctionContext, null, _methodBeingCompiled, _typeBeingCompiled); _result = new List<JsStatement>(); }