GenerateScopeCreation() abstract private method

Generates code that creates a new scope.
abstract private GenerateScopeCreation ( ILGenerator generator, OptimizationInfo optimizationInfo ) : void
generator ILGenerator The generator to output the CIL to.
optimizationInfo OptimizationInfo Information about any optimizations that should be performed.
return void
Esempio n. 1
0
        /// <summary>
        /// Generates CIL for the statement.
        /// </summary>
        /// <param name="generator"> The generator to output the CIL to. </param>
        /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
        public override void GenerateCode(ILGenerator generator, OptimizationInfo optimizationInfo)
        {
            // Generate code for the start of the statement.
            var statementLocals = new StatementLocals()
            {
                NonDefaultSourceSpanBehavior = true
            };

            GenerateStartOfStatement(generator, optimizationInfo, statementLocals);

            // Generate scope creation.
            Scope.GenerateScopeCreation(generator, optimizationInfo);
            Scope.GenerateHoistedDeclarations(generator, optimizationInfo);

            foreach (var statement in this.Statements)
            {
                // Generate code for the statement.
                statement.GenerateCode(generator, optimizationInfo);
            }

            // Generate code for the end of the statement.
            GenerateEndOfStatement(generator, optimizationInfo, statementLocals);
        }
Esempio n. 2
0
        /// <summary>
        /// Generates CIL for the expression.
        /// </summary>
        /// <param name="generator"> The generator to output the CIL to. </param>
        /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
        public override void GenerateCode(ILGenerator generator, OptimizationInfo optimizationInfo)
        {
            // If the class was named, then we need to create a new scope to hold the name.
            if (Name != null)
            {
                Scope.GenerateScopeCreation(generator, optimizationInfo);
            }

            // engine
            EmitHelpers.LoadScriptEngine(generator);

            // name
            generator.LoadStringOrNull(this.Name);

            // extends
            if (Extends == null)
            {
                generator.LoadNull();
            }
            else
            {
                Extends.GenerateCode(generator, optimizationInfo);
                EmitConversion.ToAny(generator, Extends.ResultType);
            }

            // constructor
            if (Constructor == null)
            {
                generator.LoadNull();
            }
            else
            {
                Constructor.GenerateCode(generator, optimizationInfo);
            }

            // ConstructClass(ScriptEngine engine, string name, object extends, FunctionInstance constructor)
            generator.CallStatic(ReflectionHelpers.ReflectionHelpers_ConstructClass);

            // Create a variable to hold the container instance value.
            var containerVariable = generator.CreateTemporaryVariable(typeof(ObjectInstance));

            foreach (var member in this.Members)
            {
                // class.InstancePrototype
                generator.Duplicate();
                if (!member.Name.IsStatic)
                {
                    generator.Call(ReflectionHelpers.FunctionInstance_InstancePrototype);
                }

                // Store this in a variable so that FunctionExpression.GenerateCode can retrieve it.
                generator.Duplicate();
                generator.StoreVariable(containerVariable);

                // The key can be a property name or an expression that evaluates to a name.
                if (member.Name.HasStaticName)
                {
                    generator.LoadString(member.Name.StaticName);
                }
                else
                {
                    member.Name.ComputedName.GenerateCode(generator, optimizationInfo);
                    EmitConversion.ToPropertyKey(generator, member.Name.ComputedName.ResultType);
                }

                // Emit the function value.
                member.ContainerVariable = containerVariable;
                member.GenerateCode(generator, optimizationInfo);
                member.ContainerVariable = null;

                if (member.Name.IsGetter)
                {
                    // Add a getter to the object.
                    generator.Call(ReflectionHelpers.ReflectionHelpers_SetClassGetter);
                }
                else if (member.Name.IsSetter)
                {
                    // Add a setter to the object.
                    generator.Call(ReflectionHelpers.ReflectionHelpers_SetClassSetter);
                }
                else
                {
                    // Add a new property to the object.
                    generator.Call(ReflectionHelpers.ReflectionHelpers_SetClassValue);
                }
            }

            // Release the variable that we created above.
            generator.ReleaseTemporaryVariable(containerVariable);

            // Store the class name in the scope.
            if (Name != null)
            {
                generator.Duplicate();
                new NameExpression(Scope, Name).GenerateSet(generator, optimizationInfo, PrimitiveType.Object);
            }
        }