public override string CompileToJs(ResolvedBinding binding, CompiledBindingExpression compiledExpression) { var expression = binding.GetExpression(); var visitor = new ExtractExpressionVisitor(ex => ex.NodeType == ExpressionType.Call); var rootCallback = visitor.Visit(expression); var js = SouldCompileCallback(rootCallback) ? JavascriptTranslator.CompileToJavascript(rootCallback, binding.DataContextTypeStack) : null; foreach (var param in visitor.ParameterOrder) { var callback = js == null ? null : $"function({param.Name}){{{js}}}"; var method = visitor.Replaced[param] as MethodCallExpression; js = CompileMethodCall(method, binding.DataContextTypeStack, callback); } return("var $context = ko.contextFor(this);var sender = this;(function(i_pageArea){with($context){" + js + "}})"); }
protected virtual Expression GetExpression(ResolvedPropertySetter a, out ResolvedBinding binding) { binding = null; if (a is ResolvedPropertyValue) { return(Expression.Constant(a.CastTo <ResolvedPropertyValue>().Value)); } else if (a is ResolvedPropertyBinding) { binding = a.CastTo <ResolvedPropertyBinding>().Binding; return(binding.GetExpression()); } else { return(null); } }
public new string CompileToJavascript(ResolvedBinding binding, CompiledBindingExpression compiledExpression, DotvvmConfiguration config) { var vmMapper = config.ServiceLocator.GetService <IViewModelSerializationMapper>(); var expression = binding.GetExpression(); var visitor = new ExtractExpressionVisitor(ex => ex.NodeType == ExpressionType.Call); var rootCallback = visitor.Visit(expression); var js = SouldCompileCallback(rootCallback) ? "resultPromise.resolve(" + JavascriptTranslator.FormatKnockoutScript(JavascriptTranslator.CompileToJavascript(rootCallback, binding.DataContextTypeStack, vmMapper), allowDataGlobal: false) + ")" : null; foreach (var param in visitor.ParameterOrder.Reverse <ParameterExpression>()) { if (js == null) { js = $"resultPromise.resolve({param.Name})"; } var callback = $"function({param.Name}){{{js}}}"; var method = visitor.Replaced[param] as MethodCallExpression; js = CompileMethodCall(method, binding.DataContextTypeStack, vmMapper, callback); } return("var $context = ko.contextFor(this);var sender = this;var resultPromise = new DotvvmPromise();(function(i_pageArea){with($context){" + js + "}})"); }
public virtual string CompileToJavascript(ResolvedBinding binding, CompiledBindingExpression expression) { var javascript = JavascriptTranslator.CompileToJavascript(binding.GetExpression(), binding.DataContextTypeStack); if (javascript == "$data") { javascript = "$rawData"; } else if (javascript.StartsWith("$data.", StringComparison.Ordinal)) { javascript = javascript.Substring("$data.".Length); } // do not produce try/eval on single properties if (javascript.Contains(".") || javascript.Contains("(")) { return("dotvvm.evaluator.tryEval(function(){return " + javascript + "})"); } else { return(javascript); } }
public BindingExpressionCompilationInfo PrecompileBinding(ResolvedBinding binding, string id, Type expectedType) { var compilerAttribute = GetCompilationAttribute(binding.BindingType); var requirements = compilerAttribute.GetRequirements(binding.BindingType); var result = new BindingExpressionCompilationInfo(); result.MethodName = TryExecute(binding.BindingNode, "Error while compiling binding to delegate.", requirements.Delegate, () => CompileMethod(compilerAttribute.CompileToDelegate(binding.GetExpression(), binding.DataContextTypeStack, expectedType))); result.UpdateMethodName = TryExecute(binding.BindingNode, "Error while compiling update delegate.", requirements.UpdateDelegate, () => CompileMethod(compilerAttribute.CompileToUpdateDelegate(binding.GetExpression(), binding.DataContextTypeStack))); result.OriginalString = TryExecute(binding.BindingNode, "hey, no, that should not happen. Really.", requirements.OriginalString, () => binding.Value); result.Expression = TryExecute(binding.BindingNode, "Could not get binding expression.", requirements.Expression, () => binding.GetExpression()); result.ActionFilters = TryExecute(binding.BindingNode, "", requirements.ActionFilters, () => GetActionAttributeData(binding.GetExpression())); result.Javascript = TryExecute(binding.BindingNode, "Could not compile binding to Javascript.", requirements.Javascript, () => compilerAttribute.CompileToJavascript(binding, new CompiledBindingExpression() { Expression = result.Expression, Id = id, OriginalString = result.OriginalString })); return(result); }
public virtual Expression GetExpression(ResolvedBinding binding) { return(binding.GetExpression()); }
public virtual ExpressionSyntax EmitCreateBinding(DefaultViewCompilerCodeEmitter emitter, ResolvedBinding binding, string id, Type expectedType) { var compilerAttribute = GetCompilationAttribute(binding.BindingType); var requirements = compilerAttribute.GetRequirements(binding.BindingType); var expression = new Lazy <Expression>(() => compilerAttribute.GetExpression(binding)); var compiled = new CompiledBindingExpression(); compiled.Delegate = TryExecute(binding.BindingNode, "Error while compiling binding to delegate.", requirements.Delegate, () => CompileExpression(compilerAttribute.CompileToDelegate(binding.GetExpression(), binding.DataContextTypeStack, expectedType), binding.DebugInfo)); compiled.UpdateDelegate = TryExecute(binding.BindingNode, "Error while compiling update delegate.", requirements.UpdateDelegate, () => compilerAttribute.CompileToUpdateDelegate(binding.GetExpression(), binding.DataContextTypeStack).Compile()); compiled.OriginalString = TryExecute(binding.BindingNode, "hey, no, that should not happen. Really.", requirements.OriginalString, () => binding.Value); compiled.Expression = TryExecute(binding.BindingNode, "Could not get binding expression.", requirements.Expression, () => binding.GetExpression()); compiled.Id = id; compiled.ActionFilters = TryExecute(binding.BindingNode, "", requirements.ActionFilters, () => compilerAttribute.GetActionFilters(binding.GetExpression()).ToArray()); compiled.Javascript = TryExecute(binding.BindingNode, "Could not compile binding to Javascript.", requirements.Javascript, () => compilerAttribute.CompileToJavascript(binding, compiled)); var index = Interlocked.Increment(ref globalBindingIndex); if (!GlobalBindingList.TryAdd(index, compiled)) { throw new Exception("internal bug"); } return(EmitGetCompiledBinding(index)); }