<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Reflection;

namespace System.Linq.Expressions.Interpreter
{
    internal class NewInstruction : Instruction
    {
        protected readonly ConstructorInfo _constructor;
        protected readonly int _argumentCount;

        public NewInstruction(ConstructorInfo constructor, int argumentCount)
        {
            _constructor = constructor;
            _argumentCount = argumentCount;
        }

        public override int ConsumedStack => _argumentCount;
        public override int ProducedStack => 1;
        public override string InstructionName => "New";

        public override int Run(InterpretedFrame frame)
        {
            int first = frame.StackIndex - _argumentCount;

            object[] args = GetArgs(frame, first);

            object ret;
            try
            {
                ret = _constructor.Invoke(args);
            }
            catch (TargetInvocationException e)
            {
                ExceptionHelpers.UpdateForRethrow(e.InnerException);
                throw e.InnerException;
            }

            frame.Data[first] = ret;
            frame.StackIndex = first + 1;

            return +1;
        }

        protected object[] GetArgs(InterpretedFrame frame, int first)
        {
            if (_argumentCount > 0)
            {
                var args = new object[_argumentCount];

                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = frame.Data[first + i];
                }

                return args;
            }
            else
            {
                return Array.Empty<object>();
            }
        }

        public override string ToString() => "New " + _constructor.DeclaringType.Name + "(" + _constructor + ")";
    }

    internal class ByRefNewInstruction : NewInstruction
    {
        private readonly ByRefUpdater[] _byrefArgs;

        internal ByRefNewInstruction(ConstructorInfo target, int argumentCount, ByRefUpdater[] byrefArgs)
            : base(target, argumentCount)
        {
            _byrefArgs = byrefArgs;
        }

        public override string InstructionName => "ByRefNew";

        public sealed override int Run(InterpretedFrame frame)
        {
            int first = frame.StackIndex - _argumentCount;

            object[] args = GetArgs(frame, first);

            try
            {
                object ret;
                try
                {
                    ret = _constructor.Invoke(args);
                }
                catch (TargetInvocationException e)
                {
                    throw ExceptionHelpers.UpdateForRethrow(e.InnerException);
                }

                frame.Data[first] = ret;
                frame.StackIndex = first + 1;
            }
            finally
            {
                if (args != null)
                {
                    foreach (ByRefUpdater arg in _byrefArgs)
                    {
                        arg.Update(frame, args[arg.ArgumentIndex]);
                    }
                }
            }

            return 1;
        }<commit_msg>Remove impossible-to-fail branch. Add assertion to cover it.<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Reflection;

namespace System.Linq.Expressions.Interpreter
{
    internal class NewInstruction : Instruction
    {
        protected readonly ConstructorInfo _constructor;
        protected readonly int _argumentCount;

        public NewInstruction(ConstructorInfo constructor, int argumentCount)
        {
            _constructor = constructor;
            _argumentCount = argumentCount;
        }

        public override int ConsumedStack => _argumentCount;
        public override int ProducedStack => 1;
        public override string InstructionName => "New";

        public override int Run(InterpretedFrame frame)
        {
            int first = frame.StackIndex - _argumentCount;

            object[] args = GetArgs(frame, first);

            object ret;
            try
            {
                ret = _constructor.Invoke(args);
            }
            catch (TargetInvocationException e)
            {
                ExceptionHelpers.UpdateForRethrow(e.InnerException);
                throw e.InnerException;
            }

            frame.Data[first] = ret;
            frame.StackIndex = first + 1;

            return +1;
        }

        protected object[] GetArgs(InterpretedFrame frame, int first)
        {
            if (_argumentCount > 0)
            {
                var args = new object[_argumentCount];

                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = frame.Data[first + i];
                }

                return args;
            }
            else
            {
                return Array.Empty<object>();
            }
        }

        public override string ToString() => "New " + _constructor.DeclaringType.Name + "(" + _constructor + ")";
    }

    internal class ByRefNewInstruction : NewInstruction
    {
        private readonly ByRefUpdater[] _byrefArgs;

        internal ByRefNewInstruction(ConstructorInfo target, int argumentCount, ByRefUpdater[] byrefArgs)
            : base(target, argumentCount)
        {
            _byrefArgs = byrefArgs;
        }

        public override string InstructionName => "ByRefNew";

        public sealed override int Run(InterpretedFrame frame)
        {
            int first = frame.StackIndex - _argumentCount;

            object[] args = GetArgs(frame, first);
            Debug.Assert(args != null);

            try
            {
                object ret;
                try
                {
                    ret = _constructor.Invoke(args);
                }
                catch (TargetInvocationException e)
                {
                    throw ExceptionHelpers.UpdateForRethrow(e.InnerException);
                }

                frame.Data[first] = ret;
                frame.StackIndex = first + 1;
            }
            finally
            {
                foreach (ByRefUpdater arg in _byrefArgs)
                {
                    arg.Update(frame, args[arg.ArgumentIndex]);
                }
            }

            return 1;
        }
{'fraction_non_alphanumeric': 0.07321081727177649, 'fraction_numerical': 0.002091737636336471, 'mean_word_length': 1.4297640653357533, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7593961', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1538, 'n_words': 422}