예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="p">looks declaring types if not found at this type</param>
        /// <returns></returns>
        public static ScriptAttribute Of(Type type, bool p)
        {
            if (p)
            {
                try
                {
                    Type x = type;

                    while (x != null)
                    {
                        ScriptAttribute a = OfProvider(x);

                        if (a != null)
                        {
                            return(a);
                        }

                        x = x.DeclaringType;
                    }
                }
                catch
                {
                    return(null);
                }

                return(null);
            }
            else
            {
                return(OfProvider(type));
            }
        }
예제 #2
0
        static bool IsNestedTypeOfScriptType(Type t)
        {
            Type x = t.DeclaringType;

            while (x != null)
            {
                if (ScriptAttribute.Of(x, false) != null)
                {
                    return(true);
                }

                x = x.DeclaringType;
            }

            return(false);
        }
예제 #3
0
        static IEnumerable <Assembly> LoadReferencedAssemblies(Assembly a)
        {
            foreach (AssemblyName z in a.GetReferencedAssemblies())
            {
                var x = AppDomain.CurrentDomain.Load(z);

                if (ScriptAttribute.Of(x) == null)
                {
                    continue;
                }

                yield return(x);

                foreach (Assembly c in LoadReferencedAssemblies(x))
                {
                    yield return(c);
                }
            }
        }
예제 #4
0
        public static Assembly[] LoadReferencedAssemblies(Assembly a, bool includethis, bool SkipMissingRefences = false)
        {
            var r = LoadDependencies(a, a, includethis, null, SkipMissingRefences).Distinct(
                new LoadDependenciesValue.EqualityComparer()
                ).ToArray().ToDictionary(i => i.Assembly, i => i.Dependencies.Distinct().ToArray());
            var k = r.Keys.OrderByDescending(
                kk =>
            {
                // fixme: we should apply namespace filters here
                var any = kk.GetTypes().Any(
                    kkk =>
                {
                    var sa = ScriptAttribute.OfProvider(kkk);

                    if (sa == null)
                    {
                        return(false);
                    }

                    // if the implements type is in the same assembly
                    // we skip this rule!
                    if (sa.Implements == null)
                    {
                        return(false);
                    }

                    return(kkk.Assembly != sa.Implements.Assembly);
                }
                    );

                return(any);
            }
                ).ToArray();

            // this is almost in the correct order
            // we need to consider that some assemlies are out of band
            // which mean they are only after the BCLImplementation
            // assemblies

            return(k);
        }
예제 #5
0
        static ScriptAttribute InternalOfProvider(ICustomAttributeProvider m)
        {
            // first call to this method shall prepare the cache for all types in the same assembly

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

            try
            {
                if (m is Type)
                {
                    // a context assembly can define any type to not be translated
                    // we might want to cache this
                    if (OfProviderContext.Any(k => k.NonScriptTypes.Contains(m)))
                    {
                        return(null);
                    }
                }

                var s = new ScriptAttribute[0];

                try
                {
                    s = m.GetCustomAttributes(typeof(ScriptAttribute), false) as ScriptAttribute[];
                }
                catch
                {
                    // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2013/201303/20130304-net-4-0
                    // most likely type cannot be loaded!
                }

                var x = s.Length == 0 ? null : s[0];

                var t = m as Type;
                if (t != null)
                {
                    var ts = t.Assembly.ToScriptAttributeOrDefault();

                    if (ts.NonScriptTypes != null && ts.NonScriptTypes.Contains(t))
                    {
                        return(null);
                    }

                    if (ts.IsScriptLibrary)
                    {
                        // we should not be overriding the script attribute if it already exists.

                        if (x == null)
                        {
                            x = new ScriptAttribute();
                        }
                    }
                }

                if (x == null)
                {
                    if (IsScriptLibraryViaObfuscationAttribute(m))
                    {
                        x = new ScriptAttribute();
                    }
                }


                // Whatif a corelib was marked as a IsScriptLib
                // this will confuse jsc currently...

                //if (x == null)

                if (x == null)
                {
                    if (t != null)
                    {
                        if (Enumerable.Any(
                                from p in OfProviderContext
                                let ScriptLibraries = p.Context.ToScriptAttributeOrDefault().ScriptLibraries
                                                      where ScriptLibraries != null
                                                      from l in ScriptLibraries

                                                      // A library which takes matters in its own hand
                                                      // should keep doing that...

                                                      where l.Assembly.GetCustomAttributes(typeof(ScriptAttribute), false).Length == 0
                                                      where l.Assembly == t.Assembly

                                                      select new { p, l }
                                ))
                        {
                            x = new ScriptAttribute();
                        }
                    }
                }


                return(x);
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
예제 #6
0
        public static Type[] FindTypes(Assembly a, ScriptType _scripttype)
        {
            List <Type> MyTypes = new List <Type>();

            if (ScriptAttribute.OfProvider(a) == null)
            {
                System.Diagnostics.Debugger.Break();
            }
            else
            {
                ScriptTypeFilterAttribute[] f = ScriptTypeFilterAttribute.ArrayOf(a, _scripttype);


                if (f.Length > 0)
                {
                    #region filters are defined


                    //Console.WriteLine("scripttype filters defined for assambly {0}", a);

                    //foreach (ScriptTypeFilterAttribute fi in f)
                    //    Console.WriteLine(fi);

                    foreach (Type z in a.GetTypes())
                    {
                        // lets detect anonymous types
                        if (IsAnonymousType(z))
                        {
                            // found one, yuppei!
                            MyTypes.Add(z);
                            continue;
                        }

                        // check for type filter, and if off bounds then skip


                        bool bOutOfNamespace = true;


                        foreach (ScriptTypeFilterAttribute fi in f)
                        {
                            if (fi.MatchType(z))
                            {
                                //Console.WriteLine("+ {0} : {1} [{2}, {3}]", z.FullName, fi.Filter, fi.Type, _scripttype);
                                bOutOfNamespace = false;
                                break;
                            }
                        }



                        if (bOutOfNamespace)
                        {
                            //Console.WriteLine("- {0}", z.FullName);

                            continue;
                        }

                        ScriptAttribute o = ScriptAttribute.OfProvider(z);
                        //ScriptAttribute o = ScriptAttribute.Of(z, true);

                        if (o != null)
                        {
                            MyTypes.Add(z);
                            continue;
                        }



                        if (z.IsEnum || IsCompilerGenerated(z))
                        {
                            if (IsNestedTypeOfScriptType(z))
                            {
                                MyTypes.Add(z);
                            }
                            else
                            {
                                Console.WriteLine(" type ignored : {0}", z.FullName);
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    var Obfuscation = (ObfuscationAttribute)a.GetCustomAttributes(typeof(ObfuscationAttribute), false).FirstOrDefault();

                    if (Obfuscation != null)
                    {
                        if (Obfuscation.Feature == "script")
                        {
                            return(a.GetTypes());
                        }
                    }
                }
            }

            return(MyTypes.ToArray());
        }
예제 #7
0
        static IEnumerable <LoadDependenciesValue> LoadDependencies(
            Assembly context,
            Assembly a,
            bool includethis,
            Action <Assembly> h,
            bool SkipMissingRefences = false
            )
        {
            //Console.WriteLine("enter LoadDependencies "
            //    + new
            //    {
            //        Environment.CurrentDirectory,
            //        context = context.Location,
            //        a = a.Location

            //    });

            var r = new LoadDependenciesValue();

            r.Assembly = a;

            Action <Assembly> Add =
                n =>
            {
                r.Dependencies = r.Dependencies.Concat(new[] { n }).ToArray();

                if (h != null)
                {
                    h(n);
                }
            };

            var ReferencedAssemblies = a.GetReferencedAssemblies()
                                       .Where(k => k.Name != a.GetName().Name)
                                       .Select(
                k =>
            {
                //Console.WriteLine("LoadDependencies " + new { k.Name });

                try
                {
                    //Additional information: Could not load file or assembly 'System.Data.SQLite, Version=1.0.90.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

                    //var LocalCopyHint = new FileInfo(a.Location).Directory + "/" + k.Name + ".dll";

                    //if (File.Exists(LocalCopyHint))
                    //{
                    //    // 20131226

                    //    return Assembly.LoadFrom(LocalCopyHint);
                    //}

                    return(AppDomain.CurrentDomain.Load(k));
                }
                catch
                {
                    if (SkipMissingRefences)
                    {
                        return(null);
                    }

                    throw;
                }
            }
                ).Where(k => k != null);

            var _as = ScriptAttribute.OfProvider(a);

            if (_as != null)
            {
                if (_as.ScriptLibraries != null)
                {
                    ReferencedAssemblies = ReferencedAssemblies.Concat(_as.ScriptLibraries.Select(k => k.Assembly));
                }
            }



            foreach (var x in ReferencedAssemblies)
            {
                if (ScriptAttribute.Of(x) == null)
                {
                    // either it is not a script library
                    // or it is in regards to context

                    var cs = ScriptAttribute.OfProvider(context);

                    if (cs != null)
                    {
                        if (cs.ScriptLibraries != null)
                        {
                            if (cs.ScriptLibraries.Any(k => k.Assembly == x))
                            {
                                goto ContinueAdd;
                            }
                        }
                    }

                    continue;
                }

ContinueAdd:
                Add(x);

                foreach (var v in LoadDependencies(context, x, true, Add))
                {
                    yield return(v);
                }
            }

            if (includethis)
            {
                yield return(r);
            }
        }
        static ScriptAttribute InternalOfProvider(ICustomAttributeProvider m)
        {
            // first call to this method shall prepare the cache for all types in the same assembly

            if (m == null)
                return null;

            try
            {
                if (m is Type)
                {
                    // a context assembly can define any type to not be translated
                    // we might want to cache this
                    if (OfProviderContext.Any(k => k.NonScriptTypes.Contains(m)))
                        return null;
                }

                var s = new ScriptAttribute[0];

                try
                {
                    s = m.GetCustomAttributes(typeof(ScriptAttribute), false) as ScriptAttribute[];
                }
                catch
                {
                    // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2013/201303/20130304-net-4-0
                    // most likely type cannot be loaded!
                }

                var x = s.Length == 0 ? null : s[0];

                var t = m as Type;
                if (t != null)
                {
                    var ts = t.Assembly.ToScriptAttributeOrDefault();

                    if (ts.NonScriptTypes != null && ts.NonScriptTypes.Contains(t))
                        return null;

                    // t.Assembly = {ScriptCoreLibJava, Version=4.5.0.0, Culture=neutral, PublicKeyToken=null}

                    if (ts.IsScriptLibrary)
                    {
                        // we should not be overriding the script attribute if it already exists.

                        if (x == null)
                            x = new ScriptAttribute();
                    }

                }

                if (x == null)
                {
                    if (IsScriptLibraryViaObfuscationAttribute(m))
                        x = new ScriptAttribute();
                }


                // Whatif a corelib was marked as a IsScriptLib
                // this will confuse jsc currently...

                //if (x == null)

                if (x == null)
                    if (t != null)
                        if (Enumerable.Any(
                            from p in OfProviderContext
                            let ScriptLibraries = p.Context.ToScriptAttributeOrDefault().ScriptLibraries
                            where ScriptLibraries != null
                            from l in ScriptLibraries

                                // A library which takes matters in its own hand
                                // should keep doing that...

                            where l.Assembly.GetCustomAttributes(typeof(ScriptAttribute), false).Length == 0
                            where l.Assembly == t.Assembly

                            select new { p, l }
                            ))
                            x = new ScriptAttribute();


                return x;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }