/// <summary> /// Gets the value of the global variable with the given name. /// </summary> /// <param name="variableName"> The name of the variable to retrieve the value for. </param> /// <returns> The value of the global variable, or <c>null</c> otherwise. </returns> public object GetGlobalValue(string variableName) { if (variableName == null) { throw new ArgumentNullException("variableName"); } return(TypeUtilities.NormalizeValue(this.Global.GetPropertyValue(variableName))); }
/// <summary> /// Gets the value of the global variable with the given name and coerces it to the given /// type. /// </summary> /// <typeparam name="T"> The type to coerce the value to. </typeparam> /// <param name="variableName"> The name of the variable to retrieve the value for. </param> /// <returns> The value of the global variable, or <c>null</c> otherwise. </returns> /// <remarks> Note that <c>null</c> is coerced to the following values: <c>false</c> (if /// <typeparamref name="T"/> is <c>bool</c>), 0 (if <typeparamref name="T"/> is <c>int</c> /// or <c>double</c>), string.Empty (if <typeparamref name="T"/> is <c>string</c>). </remarks> public T GetGlobalValue <T>(string variableName) { if (variableName == null) { throw new ArgumentNullException("variableName"); } return(TypeConverter.ConvertTo <T>(this, TypeUtilities.NormalizeValue(this.Global.GetPropertyValue(variableName)))); }
/// <summary> /// Executes the compiled eval code. /// </summary> /// <param name="engine"> The script engine to use to execute the script. </param> /// <returns> The result of the eval. </returns> /// <exception cref="ArgumentNullException"> <paramref name="engine"/> is a <c>null</c> reference. </exception> public object Evaluate(ScriptEngine engine) { try { object result = methodGen.Execute(engine, RuntimeScope.CreateGlobalScope(engine), engine.Global); // Execute any pending callbacks. engine.ExecutePostExecuteSteps(); return(TypeUtilities.NormalizeValue(result)); } finally { // Ensure the list of post-execute steps is cleared if there is an exception. engine.ClearPostExecuteSteps(); } }
/// <summary> /// Executes the given source code. Execution is bound to the global scope. /// </summary> /// <param name="source"> The javascript source code to execute. </param> /// <returns> The result of executing the source code. </returns> /// <exception cref="ArgumentNullException"> <paramref name="code"/> is a <c>null</c> reference. </exception> public object Evaluate(ScriptSource source) { var methodGen = new Jurassic.Compiler.EvalMethodGenerator( this, // The script engine this.CreateGlobalScope(), // The variable scope. source, // The source code. CreateOptions(), // The compiler options. this.Global); // The value of the "this" keyword. // Parse if (this.ParsingStarted != null) { this.ParsingStarted(this, EventArgs.Empty); } methodGen.Parse(); // Optimize if (this.OptimizationStarted != null) { this.OptimizationStarted(this, EventArgs.Empty); } methodGen.Optimize(); // Generate code if (this.CodeGenerationStarted != null) { this.CodeGenerationStarted(this, EventArgs.Empty); } methodGen.GenerateCode(); VerifyGeneratedCode(); // Execute if (this.ExecutionStarted != null) { this.ExecutionStarted(this, EventArgs.Empty); } var result = methodGen.Execute(); // Normalize the result (convert null to Undefined, double to int, etc). return(TypeUtilities.NormalizeValue(result)); }