Ejemplo n.º 1
0
        /// <summary>
        /// Add information about dynamically created non-generic native format type
        /// to the diagnostic stream in form of a NativeFormatType blob.
        /// </summary>
        /// <param name="typeBuilder">TypeBuilder is used to query runtime type handle for the type</param>
        /// <param name="defType">Type to emit to the diagnostic stream</param>
        /// <param name="state"></param>
        public static void RegisterDebugDataForNativeFormatType(TypeBuilder typeBuilder, DefType defType, TypeBuilderState state)
        {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
            NativeFormatType nativeFormatType = defType as NativeFormatType;
            if (nativeFormatType == null)
            {
                return;
            }

            NativePrimitiveEncoder encoder = new NativePrimitiveEncoder();
            encoder.Init();

            byte nativeFormatTypeFlags = 0;

            SerializeDataBlobTypeAndFlags(
                ref encoder,
                SerializedDataBlobKind.NativeFormatType,
                nativeFormatTypeFlags);

            TypeManagerHandle moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(nativeFormatType.MetadataReader);

            encoder.WriteUnsignedLong(unchecked ((ulong)typeBuilder.GetRuntimeTypeHandle(defType).ToIntPtr().ToInt64()));
            encoder.WriteUnsigned(nativeFormatType.Handle.ToHandle(nativeFormatType.MetadataReader).AsUInt());
            encoder.WriteUnsignedLong(unchecked ((ulong)moduleHandle.GetIntPtrUNSAFE().ToInt64()));

            Instance.ThreadSafeWriteBytes(encoder.GetBytes());
#else
            return;
#endif
        }
 /// <summary>
 /// Reads a part of the samples as bytes in the specified output format from the sample provider.
 /// </summary>
 /// <param name="destination">The destination buffer that decoded samples are written to.</param>
 /// <param name="format">The destination format that the samples are decoded to.</param>
 /// <returns>The number of samples that could be read. Will be less than the destination size when the end of the sound is reached.</returns>
 public int Read(NativeSlice <byte> destination, NativeFormatType format)
 {
     CheckValidAndThrow();
     // Not doing any format/size checks here. byte is the only valid choice for 8-bits,
     // 24-bits as well as big-endian float. Users may have reasons to want 16-bit samples
     // carried via a buffer-of-bytes, so we're being totally flexible here.
     return(DSPSampleProviderInternal.Internal_ReadUInt8FromSampleProviderById(
                ProviderHandle, (int)format, destination.GetUnsafePtr(),
                destination.Length));
 }
        /// <summary>
        /// Reads a part of the samples as short integers in the specified output format from the sample provider.
        /// </summary>
        /// <param name="destination">The destination buffer that decoded samples are written to.</param>
        /// <param name="format">The destination format that the samples are decoded to.</param>
        /// <returns>The number of samples that could be read. Will be less than the destination size when the end of the sound is reached.</returns>
        public int Read(NativeSlice <short> destination, NativeFormatType format)
        {
            CheckValidAndThrow();
            if (format != NativeFormatType.PCM16_LE && format != NativeFormatType.PCM16_BE)
            {
                throw new ArgumentException("Using buffer of short to capture samples of a different size.");
            }

            return(DSPSampleProviderInternal.Internal_ReadSInt16FromSampleProviderById(
                       ProviderHandle, (int)format,
                       destination.GetUnsafePtr(), destination.Length));
        }
Ejemplo n.º 4
0
        internal TypeDesc GetTypeDescFromQHandle(QTypeDefinition qTypeDefinition)
        {
#if ECMA_METADATA_SUPPORT
            if (qTypeDefinition.IsNativeFormatMetadataBased)
#endif
            {
                MetadataReader           nativeFormatMetadataReader = qTypeDefinition.NativeFormatReader;
                TypeDefinitionHandle     typeDefinitionHandle       = qTypeDefinition.NativeFormatHandle;
                NativeFormatModuleInfo   module           = ModuleList.Instance.GetModuleInfoForMetadataReader(nativeFormatMetadataReader);
                NativeFormatMetadataUnit metadataUnit     = ResolveMetadataUnit(module);
                NativeFormatType         nativeFormatType = (NativeFormatType)metadataUnit.GetType(typeDefinitionHandle);
                return(nativeFormatType);
            }
#if ECMA_METADATA_SUPPORT
            else if (qTypeDefinition.IsEcmaFormatMetadataBased)
            {
                EcmaModuleInfo  module     = ModuleList.Instance.GetModuleInfoForMetadataReader(qTypeDefinition.EcmaFormatReader);
                Ecma.EcmaModule ecmaModule = ResolveEcmaModule(module);
                Ecma.EcmaType   ecmaType   = (Ecma.EcmaType)ecmaModule.GetType(qTypeDefinition.EcmaFormatHandle);
                return(ecmaType);
            }
#endif
            return(null);
        }
Ejemplo n.º 5
0
        public TypeDesc ResolveRuntimeTypeHandle(RuntimeTypeHandle rtth)
        {
            TypeDesc returnedType;

            if (_runtimeTypeHandleResolutionCache.TryGetValue(rtth, out returnedType))
            {
                return(returnedType);
            }

            if (rtth.Equals(CanonType.RuntimeTypeHandle))
            {
                returnedType = CanonType;
            }
            else if (rtth.Equals(UniversalCanonType.RuntimeTypeHandle))
            {
                returnedType = UniversalCanonType;
            }
            else if (RuntimeAugments.IsGenericTypeDefinition(rtth))
            {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                MetadataReader       metadataReader;
                TypeDefinitionHandle typeDefinitionHandle;
                if (TypeLoaderEnvironment.Instance.TryGetMetadataForNamedType(rtth, out metadataReader, out typeDefinitionHandle))
                {
                    IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader);
                    NativeFormatMetadataUnit metadataUnit     = ResolveMetadataUnit(moduleHandle);
                    NativeFormatType         nativeFormatType = (NativeFormatType)metadataUnit.GetType(typeDefinitionHandle);

                    returnedType = nativeFormatType;
                }
                if (returnedType == null)
#endif
                {
                    unsafe
                    {
                        TypeDesc[] genericParameters = new TypeDesc[rtth.ToEETypePtr()->GenericArgumentCount];
                        for (int i = 0; i < genericParameters.Length; i++)
                        {
                            genericParameters[i] = GetSignatureVariable(i, false);
                        }

                        returnedType = new NoMetadataType(this, rtth, null, new Instantiation(genericParameters), rtth.GetHashCode());
                    }
                }
            }
            else if (RuntimeAugments.IsGenericType(rtth))
            {
                RuntimeTypeHandle   typeDefRuntimeTypeHandle;
                RuntimeTypeHandle[] genericArgRuntimeTypeHandles;
                typeDefRuntimeTypeHandle = RuntimeAugments.GetGenericInstantiation(rtth, out genericArgRuntimeTypeHandles);

                DefType       typeDef     = (DefType)ResolveRuntimeTypeHandle(typeDefRuntimeTypeHandle);
                Instantiation genericArgs = ResolveRuntimeTypeHandles(genericArgRuntimeTypeHandles);
                returnedType = ResolveGenericInstantiation(typeDef, genericArgs);
            }
            else if (RuntimeAugments.IsArrayType(rtth))
            {
                RuntimeTypeHandle elementTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(rtth);
                TypeDesc          elementType       = ResolveRuntimeTypeHandle(elementTypeHandle);
                unsafe
                {
                    if (rtth.ToEETypePtr()->IsSzArray)
                    {
                        returnedType = GetArrayType(elementType);
                    }
                    else
                    {
                        returnedType = GetArrayType(elementType, rtth.ToEETypePtr()->ArrayRank);
                    }
                }
            }
            else if (RuntimeAugments.IsUnmanagedPointerType(rtth))
            {
                RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(rtth);
                TypeDesc          targetType       = ResolveRuntimeTypeHandle(targetTypeHandle);
                returnedType = GetPointerType(targetType);
            }
            else
            {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                MetadataReader       metadataReader;
                TypeDefinitionHandle typeDefinitionHandle;
                if (TypeLoaderEnvironment.Instance.TryGetMetadataForNamedType(rtth, out metadataReader, out typeDefinitionHandle))
                {
                    IntPtr moduleHandle = ModuleList.Instance.GetModuleForMetadataReader(metadataReader);
                    NativeFormatMetadataUnit metadataUnit = ResolveMetadataUnit(moduleHandle);
                    returnedType = metadataUnit.GetType(typeDefinitionHandle);
                }
                else
#endif
                {
                    returnedType = new NoMetadataType(this, rtth, null, Instantiation.Empty, rtth.GetHashCode());
                }
            }

            // We either retrieved an existing DefType that is already registered with the runtime
            // or one that is not associated with an EEType yet. If it's not associated, associate it.
            if (returnedType.RuntimeTypeHandle.IsNull())
            {
                TypeBuilderState state = returnedType.GetTypeBuilderStateIfExist();
                bool             skipStoringRuntimeTypeHandle = false;

                // If we've already attempted to lookup and failed to retrieve this type handle, we
                // may have already decided to create a new one. In that case, do not attempt to abort
                // that creation process as it may have already begun the process of type creation
                if (state != null && state.AttemptedAndFailedToRetrieveTypeHandle)
                {
                    skipStoringRuntimeTypeHandle = true;
                }

                if (!skipStoringRuntimeTypeHandle)
                {
                    returnedType.SetRuntimeTypeHandleUnsafe(rtth);
                }
            }

            _runtimeTypeHandleResolutionCache.Add(rtth, returnedType);

            return(returnedType.WithDebugName());
        }