/// <summary> /// Executes this script, using the supplied values to initialize any variables in the script. /// </summary> /// <param name="context">The set of values to substitute into the script.</param> /// <returns>The return value of the script.</returns> public object Run(IDictionary context) { lock (_syncLock) { return(_script.Run(context)); } }
public override object Evaluate(object arg) { if (string.IsNullOrEmpty(this.Text)) { return(null); } if (this.Text == AutomaticVariableToken) { return(arg); } try { // create the script if not yet created if (_script == null) { _script = CreateScript(this.Text); } // evaluate the test expression var context = new Dictionary <string, object> { { AutomaticVariableToken, arg } }; return(_script.Run(context)); } catch (Exception e) { throw new SpecificationException(string.Format(SR.ExceptionJScriptEvaluation, this.Text), e); } }
public override object Evaluate(object arg) { if (string.IsNullOrEmpty(this.Text)) { return(null); } if (this.Text == AUTOMATIC_VARIABLE_TOKEN) { return(arg); } try { // create the script if not yet created if (_script == null) { _script = CreateScript(this.Text); } // evaluate the test expression Dictionary <string, object> context = new Dictionary <string, object>(); context.Add(AUTOMATIC_VARIABLE_TOKEN, arg); return(_script.Run(context)); } catch (Exception e) { throw new SpecificationException(string.Format(SR.ExceptionJScriptEvaluation, this.Text), e); } }
/// <summary> /// Overload that allows the output of the template evaluation to be written directly to a <see cref="TextWriter"/>. /// </summary> /// <param name="context">A dictionary of objects to pass into the script.</param> /// <param name="output">A text writer to which the output should be written.</param> public void Evaluate(Dictionary <string, object> context, TextWriter output) { try { // add a variable for the output stream to the context context["__out__"] = output; // create executable script if not created if (_script == null) { var variables = CollectionUtils.Map(context.Keys, (string s) => s).ToArray(); _script = ScriptEngineFactory.GetEngine("jscript").CreateScript(_inversion, variables); } _script.Run(context); } catch (Exception e) { throw new ActiveTemplateException(SR.ExceptionTemplateEvaluation, e); } }