/// <inheritdoc />
        public override IASSourceMapper GetASSourceMapper(ASSourceMappingDescriptor descriptor)
        {
            foreach (PrimitiveMapper mapper in mappers)
                if (mapper.SupportsSourceMapping(descriptor))
                    return mapper;

            return null;
        }
        /// <inheritdoc />
        public override IASSourceMapper GetASSourceMapper(ASSourceMappingDescriptor descriptor)
        {
            if (descriptor.SourceKind == ASTypeKind.Object)
            {
                ActionScriptClassMapping classMapping = mappingTable.GetClassMappingByAlias(descriptor.SourceClassAlias);
                if (classMapping != null && descriptor.TargetNativeType.IsAssignableFrom(classMapping.NativeType))
                {
                    return new ObjectMapper(classMapping);
                }
            }

            return null;
        }
        /// <summary>
        /// Gets an ActionScript source mapper that satisfies the specified descriptor.
        /// </summary>
        /// <param name="descriptor">The source mapping descriptor</param>
        /// <returns>The ActionScript target mapper, or null if no compatible mapper can be obtained by any registered factory</returns>
        public IASSourceMapper GetASSourceMapper(ASSourceMappingDescriptor descriptor)
        {
            lock (syncRoot)
            {
                IASSourceMapper mapper;
                if (!sourceMapperCache.TryGetValue(descriptor, out mapper))
                {
                    foreach (IASMapperFactory factory in mapperFactories)
                    {
                        mapper = factory.GetASSourceMapper(descriptor);
                        if (mapper != null)
                            break;
                    }

                    sourceMapperCache.Add(descriptor, mapper);
                }

                return mapper;
            }
        }
Esempio n. 4
0
        private object UncachedToNative(IASValue asValue, Type nativeType)
        {
            ASTypeKind          kind         = asValue.Kind;
            ASClass             asClass      = asValue.Class;
            string              classAlias   = asClass != null ? asClass.ClassAlias : "";
            ASValueContentFlags contentFlags = asValue.ContentFlags;

            Type defaultNativeType = mappingTable.GetDefaultNativeType(kind, classAlias, contentFlags);

            // If the requested native type is not as precise as the default native type,
            // then use the default native type instead.  This rule is intended to avoid
            // the ambiguities that occur if we try to map to type "object" or some other
            // type that is too general.
            if (nativeType == null || nativeType.IsAssignableFrom(defaultNativeType))
            {
                nativeType = defaultNativeType;
            }

            // Quick short circuit if the desired type is a subtype of IASValue so mapping won't help.
            if (!typeof(IASValue).IsAssignableFrom(nativeType))
            {
                // Note: This will handle uninitialized values by returning null if no trivial
                //       conversion is possible without complete initialization.
                object value = asValue.GetNativeValue(nativeType);
                if (value != null)
                {
                    return(value);
                }

                // If the value isn't initialized then give up because we might have to do mapping.
                if (!asValue.IsInitialized)
                {
                    throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                                  "The ActionScript value cannot be mapped to type '{0}' because it is not completely initialized.",
                                                                  nativeType != null ? nativeType.FullName : "<default>"));
                }

                // Use mappers.
                // FIXME: This won't work if mapping ends up being recursive!
                ASSourceMappingDescriptor descriptor = new ASSourceMappingDescriptor(kind, classAlias, contentFlags, nativeType);
                IASSourceMapper           mapper     = mappingTable.GetASSourceMapper(descriptor);
                if (mapper != null)
                {
                    return(mapper.ToNative(this, asValue, nativeType));
                }
            }

            // Apply default handling for null references.
            if (asValue == ASNull.Value && !nativeType.IsValueType)
            {
                return(null);
            }

            // As a last resort, if we can assign the AS value to the original type requested then
            // do so.  We generally prefer mappings over returning IASValue instances, but if there
            // is no other choice...
            if (nativeType.IsInstanceOfType(asValue))
            {
                return(asValue);
            }

            // Give up!
            throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                          "Cannot find a suitable mapper for mapping an ActionScript value of kind '{0}' with class alias '{1}' to an instance of type '{2}'.",
                                                          asValue.Kind, classAlias, nativeType != null ? nativeType.FullName : "<default>"));
        }
 /// <summary>
 /// Gets an ActionScript source mapper that satisfies the specified descriptor.
 /// </summary>
 /// <param name="descriptor">The source mapping descriptor</param>
 /// <returns>The ActionScript target mapper, or null if no compatible mapper can be obtained by this factory</returns>
 public virtual IASSourceMapper GetASSourceMapper(ASSourceMappingDescriptor descriptor)
 {
     return null;
 }
 public override bool SupportsSourceMapping(ASSourceMappingDescriptor descriptor)
 {
     ASTypeKind sourceKind = descriptor.SourceKind;
     return descriptor.TargetNativeType.IsEnum
         && (sourceKind == ASTypeKind.Int29 || sourceKind == ASTypeKind.Number || sourceKind == ASTypeKind.String);
 }
            public virtual bool SupportsSourceMapping(ASSourceMappingDescriptor descriptor)
            {
                // Note: Use strict equality here because we don't want to use 
                //       most primitive mappings if the result type is underspecified.
                //       How would we know whether to map to "int" or "long"?  The mapping
                //       would be ambiguous!  Instead we handle this case with default native type
                //       mappings elsewhere.
                if (descriptor.TargetNativeType == type)
                {
                    foreach (ASTypeKind kind in kinds)
                        if (descriptor.SourceKind == kind)
                            return true;
                }

                return false;
            }