Example #1
0
        public override bool IsDefined(Type attributeType, bool inherit)
        {
            if (attributeType == null)
            {
                throw new ArgumentNullException(nameof(attributeType));
            }

            RuntimeType?attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

            if (attributeRuntimeType == null)
            {
                throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
            }

            return(CustomAttribute.IsDefined(this, attributeRuntimeType));
        }
Example #2
0
        public override Type?GetType(string className, bool throwOnError, bool ignoreCase)
        {
            // throw on null strings regardless of the value of "throwOnError"
            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            RuntimeType?  retType     = null;
            object?       keepAlive   = null;
            RuntimeModule thisAsLocal = this;

            GetType(JitHelpers.GetQCallModuleOnStack(ref thisAsLocal), className, throwOnError, ignoreCase, JitHelpers.GetObjectHandleOnStack(ref retType), JitHelpers.GetObjectHandleOnStack(ref keepAlive));
            GC.KeepAlive(keepAlive);
            return(retType);
        }
Example #3
0
        public override void Emit(OpCode opcode, Type type)
        {
            ArgumentNullException.ThrowIfNull(type);

            RuntimeType?rtType = type as RuntimeType;

            if (rtType == null)
            {
                throw new ArgumentException(SR.Argument_MustBeRuntimeType);
            }

            int token = GetTokenFor(rtType);

            EnsureCapacity(7);
            InternalEmit(opcode);
            PutInteger4(token);
        }
Example #4
0
        public static unsafe Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds)
        {
            if (elementType == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType);
            }
            if (lengths == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.lengths);
            }
            if (lowerBounds == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.lowerBounds);
            }
            if (lengths.Length != lowerBounds !.Length)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RanksAndBounds);
            }
            if (lengths.Length == 0)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NeedAtLeast1Rank);
            }

            RuntimeType?t = elementType.UnderlyingSystemType as RuntimeType;

            if (t == null)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType);
            }

            // Check to make sure the lenghts are all positive. Note that we check this here to give
            // a good exception message if they are not; however we check this again inside the execution
            // engine's low level allocation function after having made a copy of the array to prevent a
            // malicious caller from mutating the array after this check.
            for (int i = 0; i < lengths.Length; i++)
                if (lengths[i] < 0)
                {
                    ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.lengths, i, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
                }

            fixed(int *pLengths = &lengths[0])
            fixed(int *pLowerBounds = &lowerBounds[0])
            return(InternalCreate((void *)t.TypeHandle.Value, lengths.Length, pLengths, pLowerBounds));
        }
Example #5
0
        // *** ILGenerator api ***

        public override LocalBuilder DeclareLocal(Type localType, bool pinned)
        {
            ArgumentNullException.ThrowIfNull(localType);

            LocalBuilder localBuilder;

            RuntimeType?rtType = localType as RuntimeType;

            if (rtType == null)
            {
                throw new ArgumentException(SR.Argument_MustBeRuntimeType);
            }

            localBuilder = new LocalBuilder(m_localCount, localType, m_methodBuilder);
            // add the localType to local signature
            m_localSignature.AddArgument(localType, pinned);
            m_localCount++;
            return(localBuilder);
        }
Example #6
0
        // Create instance will create an array
        public static unsafe Array CreateInstance(Type elementType, int length)
        {
            if (elementType is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType);
            }
            if (length < 0)
            {
                ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
            }

            RuntimeType?t = elementType.UnderlyingSystemType as RuntimeType;

            if (t == null)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_MustBeType, ExceptionArgument.elementType);
            }
            return(InternalCreate((void *)t.TypeHandle.Value, 1, &length, null));
        }
Example #7
0
        public override object[] GetCustomAttributes(Type attributeType, bool inherit)
        {
            if (attributeType == null)
            {
                throw new ArgumentNullException(nameof(attributeType));
            }

            if (MdToken.IsNullToken(m_tkParamDef))
            {
                return(Array.Empty <object>());
            }

            RuntimeType?attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

            if (attributeRuntimeType == null)
            {
                throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
            }

            return(CustomAttribute.GetCustomAttributes(this, attributeRuntimeType));
        }
Example #8
0
        public override Type?GetType(string name, bool throwOnError, bool ignoreCase)
        {
            // throw on null strings regardless of the value of "throwOnError"
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            RuntimeType?        type      = null;
            object?             keepAlive = null;
            AssemblyLoadContext?assemblyLoadContextStack = AssemblyLoadContext.CurrentContextualReflectionContext;

            GetType(GetNativeHandle(),
                    name,
                    throwOnError,
                    ignoreCase,
                    JitHelpers.GetObjectHandleOnStack(ref type),
                    JitHelpers.GetObjectHandleOnStack(ref keepAlive),
                    JitHelpers.GetObjectHandleOnStack(ref assemblyLoadContextStack));
            GC.KeepAlive(keepAlive);

            return(type);
        }
Example #9
0
        public override Type?GetType(
            string name, // throw on null strings regardless of the value of "throwOnError"
            bool throwOnError, bool ignoreCase)
        {
            ArgumentNullException.ThrowIfNull(name);

            RuntimeType?        type      = null;
            object?             keepAlive = null;
            AssemblyLoadContext?assemblyLoadContextStack = AssemblyLoadContext.CurrentContextualReflectionContext;

            RuntimeAssembly runtimeAssembly = this;

            GetType(new QCallAssembly(ref runtimeAssembly),
                    name,
                    throwOnError,
                    ignoreCase,
                    ObjectHandleOnStack.Create(ref type),
                    ObjectHandleOnStack.Create(ref keepAlive),
                    ObjectHandleOnStack.Create(ref assemblyLoadContextStack));
            GC.KeepAlive(keepAlive);

            return(type);
        }
        internal RuntimeMethodInfo?GetParentDefinition()
        {
            if (!IsVirtual || m_declaringType.IsInterface)
            {
                return(null);
            }

            RuntimeType?parent = (RuntimeType?)m_declaringType.BaseType;

            if (parent == null)
            {
                return(null);
            }

            int slot = RuntimeMethodHandle.GetSlot(this);

            if (RuntimeTypeHandle.GetNumVirtuals(parent) <= slot)
            {
                return(null);
            }

            return((RuntimeMethodInfo?)RuntimeType.GetMethodBase(parent, RuntimeTypeHandle.GetMethodAt(parent, slot)));
        }
Example #11
0
        public static unsafe Array CreateInstance(Type elementType, int length1, int length2)
        {
            if (elementType is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementType);
            }
            if (length1 < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(
                    ExceptionArgument.length1,
                    ExceptionResource.ArgumentOutOfRange_NeedNonNegNum
                    );
            }
            if (length2 < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(
                    ExceptionArgument.length2,
                    ExceptionResource.ArgumentOutOfRange_NeedNonNegNum
                    );
            }

            RuntimeType?t = elementType.UnderlyingSystemType as RuntimeType;

            if (t == null)
            {
                ThrowHelper.ThrowArgumentException(
                    ExceptionResource.Arg_MustBeType,
                    ExceptionArgument.elementType
                    );
            }
            int *pLengths = stackalloc int[2];

            pLengths[0] = length1;
            pLengths[1] = length2;
            return(InternalCreate((void *)t.TypeHandle.Value, 2, pLengths, null));
        }
Example #12
0
 internal static void SetValue(RuntimeFieldInfo field, object?obj, object?value, RuntimeType?_, FieldAttributes _1, RuntimeType?_2, ref bool _3)
 {
     SetValueInternal(field, obj, value);
 }
 /// <summary>
 /// Initializes a new instance of the PublishWebServiceRequest class.
 /// </summary>
 /// <param name="snapshotId">ID of the snapshot to be used for service.
 /// **Optional**</param>
 /// <param name="code">Code to execute. Specific to the runtime type.
 /// **&lt;font color = 'red'&gt;Required&lt;/font&gt;**</param>
 /// <param name="description">Description for the web service.
 /// **Optional**</param>
 /// <param name="operationId">Swagger operationId/alias for web
 /// service. **Optional**</param>
 /// <param name="inputParameterDefinitions">Input parameters
 /// definitions for the execution. **Optional**</param>
 /// <param name="outputParameterDefinitions">Output parameter
 /// definitions for the execution. **Optional**</param>
 /// <param name="runtimeType">Type of the runtime. **Optional [Default
 /// R]**. Possible values include: 'R', 'Python'</param>
 /// <param name="initCode">Code that runs before each request. Specific
 /// to the runtime type. **Optional**</param>
 /// <param name="outputFileNames">Files that are returned by the
 /// response. **Optional**</param>
 public PublishWebServiceRequest(string snapshotId = default(string), string code = default(string), string description = default(string), string operationId = default(string), IList <ParameterDefinition> inputParameterDefinitions = default(IList <ParameterDefinition>), IList <ParameterDefinition> outputParameterDefinitions = default(IList <ParameterDefinition>), RuntimeType?runtimeType = default(RuntimeType?), string initCode = default(string), IList <string> outputFileNames = default(IList <string>))
 {
     SnapshotId  = snapshotId;
     Code        = code;
     Description = description;
     OperationId = operationId;
     InputParameterDefinitions  = inputParameterDefinitions;
     OutputParameterDefinitions = outputParameterDefinitions;
     RuntimeType     = runtimeType;
     InitCode        = initCode;
     OutputFileNames = outputFileNames;
     CustomInit();
 }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the Session class.
 /// </summary>
 /// <param name="id">Unique identifier representing the
 /// session.</param>
 /// <param name="name">Name of the session.</param>
 /// <param name="owner">Owner of the session.</param>
 /// <param name="runtimeType">Type of the runtime. Possible values
 /// include: 'R', 'Python'</param>
 public Session(string id = default(string), string name = default(string), string owner = default(string), RuntimeType?runtimeType = default(RuntimeType?))
 {
     Id          = id;
     Name        = name;
     Owner       = owner;
     RuntimeType = runtimeType;
     CustomInit();
 }
 /// <summary>
 /// Initializes a new instance of the TargetRuntime class.
 /// </summary>
 /// <param name="targetArchitecture">The target architecture. Possible
 /// values include: 'Amd64', 'Arm32v7'</param>
 /// <param name="osType">The target operating system. Possible values
 /// include: 'Linux', 'Windows'</param>
 /// <param name="runtimeType">The target runtime type. Possible values
 /// include: 'SparkPython', 'Tlc37', 'Tlc38', 'Tlc310', 'Python',
 /// 'PythonSlim', 'PythonCustom'</param>
 /// <param name="properties">The properties dictionary.</param>
 public TargetRuntime(ArchitectureType?targetArchitecture = default(ArchitectureType?), OSType?osType = default(OSType?), RuntimeType?runtimeType = default(RuntimeType?), IDictionary <string, string> properties = default(IDictionary <string, string>))
 {
     TargetArchitecture = targetArchitecture;
     OsType             = osType;
     RuntimeType        = runtimeType;
     Properties         = properties;
     CustomInit();
 }
Example #16
0
        protected virtual MethodInfo GetMethodImpl()
        {
            if ((_methodBase == null) || !(_methodBase is MethodInfo))
            {
                IRuntimeMethodInfo method        = FindMethodHandle();
                RuntimeType?       declaringType = RuntimeMethodHandle.GetDeclaringType(method);
                // need a proper declaring type instance method on a generic type
                if (
                    RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) ||
                    RuntimeTypeHandle.HasInstantiation(declaringType)
                    )
                {
                    bool isStatic =
                        (RuntimeMethodHandle.GetAttributes(method) & MethodAttributes.Static)
                        != (MethodAttributes)0;
                    if (!isStatic)
                    {
                        if (_methodPtrAux == IntPtr.Zero)
                        {
                            // The target may be of a derived type that doesn't have visibility onto the
                            // target method. We don't want to call RuntimeType.GetMethodBase below with that
                            // or reflection can end up generating a MethodInfo where the ReflectedType cannot
                            // see the MethodInfo itself and that breaks an important invariant. But the
                            // target type could include important generic type information we need in order
                            // to work out what the exact instantiation of the method's declaring type is. So
                            // we'll walk up the inheritance chain (which will yield exactly instantiated
                            // types at each step) until we find the declaring type. Since the declaring type
                            // we get from the method is probably shared and those in the hierarchy we're
                            // walking won't be we compare using the generic type definition forms instead.
                            Type?currentType = _target !.GetType();
                            Type targetType  = declaringType.GetGenericTypeDefinition();
                            while (currentType != null)
                            {
                                if (
                                    currentType.IsGenericType &&
                                    currentType.GetGenericTypeDefinition() == targetType
                                    )
                                {
                                    declaringType = currentType as RuntimeType;
                                    break;
                                }
                                currentType = currentType.BaseType;
                            }

                            // RCWs don't need to be "strongly-typed" in which case we don't find a base type
                            // that matches the declaring type of the method. This is fine because interop needs
                            // to work with exact methods anyway so declaringType is never shared at this point.
                            Debug.Assert(
                                currentType != null || _target.GetType().IsCOMObject,
                                "The class hierarchy should declare the method"
                                );
                        }
                        else
                        {
                            // it's an open one, need to fetch the first arg of the instantiation
                            MethodInfo invoke = this.GetType().GetMethod("Invoke") !;
                            declaringType = (RuntimeType)invoke.GetParameters()[0].ParameterType;
                        }
                    }
                }
                _methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method) !;
            }
            return((MethodInfo)_methodBase);
        }
Example #17
0
 /// <summary>
 /// Initializes a new instance of the CreateSessionRequest class.
 /// </summary>
 /// <param name="name">Name of the session. **Optional**</param>
 /// <param name="snapshotId">Id of the snapshot to be loaded into the
 /// new created session. **Optional**</param>
 /// <param name="runtimeType">Type of the runtime. **Optional [Default
 /// R]**. Possible values include: 'R', 'Python'</param>
 public CreateSessionRequest(string name = default(string), string snapshotId = default(string), RuntimeType?runtimeType = default(RuntimeType?))
 {
     Name        = name;
     SnapshotId  = snapshotId;
     RuntimeType = runtimeType;
     CustomInit();
 }
Example #18
0
 private RuntimeType InitializeFieldType()
 {
     return(m_fieldType = new Signature(this, m_declaringType).FieldType);
 }
Example #19
0
 internal static void SetValue(RuntimeFieldInfo field, object?obj, object?value, RuntimeType?fieldType, FieldAttributes fieldAttr, RuntimeType?declaringType, ref bool domainInitialized)
 {
     SetValueInternal(field, obj, value);
 }
Example #20
0
        private void Init(string name,
                          MethodAttributes attributes,
                          CallingConventions callingConvention,
                          Type?returnType,
                          Type[]?signature,
                          Type?owner,
                          Module?m,
                          bool skipVisibility,
                          bool transparentMethod)
        {
            DynamicMethod.CheckConsistency(attributes, callingConvention);

            // check and store the signature
            if (signature != null)
            {
                m_parameterTypes = new RuntimeType[signature.Length];
                for (int i = 0; i < signature.Length; i++)
                {
                    if (signature[i] == null)
                    {
                        throw new ArgumentException(SR.Arg_InvalidTypeInSignature);
                    }
                    m_parameterTypes[i] = (signature[i].UnderlyingSystemType as RuntimeType) !;
                    if (m_parameterTypes[i] == null || m_parameterTypes[i] == typeof(void))
                    {
                        throw new ArgumentException(SR.Arg_InvalidTypeInSignature);
                    }
                }
            }
            else
            {
                m_parameterTypes = Array.Empty <RuntimeType>();
            }

            // check and store the return value
            m_returnType = (returnType == null) ? (RuntimeType)typeof(void) : (returnType.UnderlyingSystemType as RuntimeType) !;
            if (m_returnType == null)
            {
                throw new NotSupportedException(SR.Arg_InvalidTypeInRetType);
            }

            if (transparentMethod)
            {
                Debug.Assert(owner == null && m == null, "owner and m cannot be set for transparent methods");
                m_module = GetDynamicMethodsModule();
                if (skipVisibility)
                {
                    m_restrictedSkipVisibility = true;
                }
            }
            else
            {
                Debug.Assert(m != null || owner != null, "Constructor should ensure that either m or owner is set");
                Debug.Assert(m == null || !m.Equals(s_anonymouslyHostedDynamicMethodsModule), "The user cannot explicitly use this assembly");
                Debug.Assert(m == null || owner == null, "m and owner cannot both be set");

                if (m != null)
                {
                    m_module = m.ModuleHandle.GetRuntimeModule(); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects.
                }
                else
                {
                    RuntimeType?rtOwner = null;
                    if (owner != null)
                    {
                        rtOwner = owner.UnderlyingSystemType as RuntimeType;
                    }

                    if (rtOwner != null)
                    {
                        if (rtOwner.HasElementType || rtOwner.ContainsGenericParameters ||
                            rtOwner.IsGenericParameter || rtOwner.IsInterface)
                        {
                            throw new ArgumentException(SR.Argument_InvalidTypeForDynamicMethod);
                        }

                        m_typeOwner = rtOwner;
                        m_module    = rtOwner.GetRuntimeModule();
                    }
                }

                m_skipVisibility = skipVisibility;
            }

            // initialize remaining fields
            m_ilGenerator  = null;
            m_fInitLocals  = true;
            m_methodHandle = null;

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention);
        }
Example #21
0
 internal static extern unsafe void SetValueDirect(RuntimeFieldInfo field, RuntimeType fieldType, void *pTypedRef, object value, RuntimeType?contextType);
Example #22
0
 internal static extern bool HasReferences(RuntimeType?type);
 internal static string ToSerializedValue(this RuntimeType?value) =>
 value == null ? null : ((RuntimeType)value).ToSerializedValue();