/// <summary> /// Generates code that restores the parent scope as the active scope. /// </summary> /// <param name="generator"> The generator to output the CIL to. </param> /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param> internal void GenerateScopeDestruction(ILGenerator generator, OptimizationInfo optimizationInfo) { if (this.ExistsAtRuntime == false) { return; } // Modify the scope variable so it points at the parent scope. EmitHelpers.LoadScope(generator); generator.Call(ReflectionHelpers.Scope_ParentScope); EmitHelpers.StoreScope(generator); }
/// <summary> /// Generates code that creates a new scope. /// </summary> /// <param name="generator"> The generator to output the CIL to. </param> /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param> internal override void GenerateScopeCreation(ILGenerator generator, OptimizationInfo optimizationInfo) { // Allocate storage for each variable if the declarative scope object has been optimized away. if (optimizationInfo.OptimizeDeclarativeScopes == false) { // Create a new declarative scope. // parentScope EmitHelpers.LoadScope(generator); // declaredVariableNames generator.LoadInt32(this.DeclaredVariableCount); generator.NewArray(typeof(string)); int i = 0; foreach (string variableName in this.DeclaredVariableNames) { generator.Duplicate(); generator.LoadInt32(i++); generator.LoadString(variableName); generator.StoreArrayElement(typeof(string)); } // DeclarativeScope.CreateRuntimeScope(parentScope, declaredVariableNames) generator.Call(ReflectionHelpers.DeclarativeScope_CreateRuntimeScope); // Save the new scope. EmitHelpers.StoreScope(generator); } else { // The declarative scope can be optimized away entirely. foreach (var variable in this.DeclaredVariables) { variable.Store = null; variable.Type = PrimitiveType.Any; } // Indicate the scope was not created. this.ExistsAtRuntime = false; } }
/// <summary> /// Generates code that creates a new scope. /// </summary> /// <param name="generator"> The generator to output the CIL to. </param> /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param> internal override void GenerateScopeCreation(ILGenerator generator, OptimizationInfo optimizationInfo) { // Create a new runtime object scope. EmitHelpers.LoadScope(generator); // parent scope if (this.ScopeObjectExpression == null) { EmitHelpers.LoadScriptEngine(generator); generator.Call(ReflectionHelpers.ScriptEngine_Global); } else { this.ScopeObjectExpression.GenerateCode(generator, optimizationInfo); EmitConversion.ToObject(generator, this.ScopeObjectExpression.ResultType, optimizationInfo); } generator.LoadBoolean(this.ProvidesImplicitThisValue); generator.LoadBoolean(this.CanDeclareVariables); generator.Call(ReflectionHelpers.ObjectScope_CreateRuntimeScope); // Save the new scope. EmitHelpers.StoreScope(generator); }