public JsValue Call(JsValue thisObject, JsValue[] arguments)
        {
            // direct calls on a NamespaceReference constructor object is creating a generic type
            var genericTypes = new Type[arguments.Length];

            for (int i = 0; i < arguments.Length; i++)
            {
                var genericTypeReference = arguments.At(i);
                if (genericTypeReference == Undefined.Instance || !genericTypeReference.IsObject() || genericTypeReference.AsObject().Class != "TypeReference")
                {
                    throw new JavaScriptException(Engine.TypeError, "Invalid generic type parameter");
                }

                genericTypes[i] = arguments.At(i).As <TypeReference>().Type;
            }

            var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As <TypeReference>();

            if (typeReference == null)
            {
                return(Undefined.Instance);
            }

            var genericType = typeReference.Type.MakeGenericType(genericTypes);

            return(TypeReference.CreateTypeReference(Engine, genericType));
        }
        public override JsValue Get(string propertyName)
        {
            var newPath = _path + "." + propertyName;

            Type type;

            if (Engine.TypeCache.TryGetValue(newPath, out type))
            {
                if (type == null)
                {
                    return(new NamespaceReference(Engine, newPath));
                }

                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search for type in mscorlib
            type = Type.GetType(newPath);
            if (type != null)
            {
                Engine.TypeCache.Add(newPath, type);
                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search in loaded assemblies
            foreach (var assembly in new [] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }.Distinct())
            {
                type = assembly.GetType(newPath);
                if (type != null)
                {
                    Engine.TypeCache.Add(newPath, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }
            }

            // search in lookup asemblies
            foreach (var assembly in Engine.Options.GetLookupAssemblies())
            {
                type = assembly.GetType(newPath);
                if (type != null)
                {
                    Engine.TypeCache.Add(newPath, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }
            }

            // the new path doesn't represent a known class, thus return a new namespace instance

            Engine.TypeCache.Add(newPath, null);
            return(new NamespaceReference(Engine, newPath));
        }
Пример #3
0
        JsValue ICallable.Call(JsValue thisObject, JsValue[] arguments)
        {
            // direct calls on a NamespaceReference constructor object is creating a generic type
            var genericTypes = new Type[arguments.Length];

            for (int i = 0; i < arguments.Length; i++)
            {
                var genericTypeReference = arguments[i];
                if (genericTypeReference.IsUndefined() ||
                    !genericTypeReference.IsObject() ||
                    genericTypeReference.AsObject().Class != ObjectClass.TypeReference)
                {
                    ExceptionHelper.ThrowTypeError(_engine.Realm, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?");
                }

                genericTypes[i] = ((TypeReference)genericTypeReference).ReferenceType;
            }

            var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As <TypeReference>();

            if (ReferenceEquals(typeReference, null))
            {
                return(Undefined);
            }

            try
            {
                var genericType = typeReference.ReferenceType.MakeGenericType(genericTypes);

                return(TypeReference.CreateTypeReference(Engine, genericType));
            }
            catch (Exception e)
            {
                ExceptionHelper.ThrowTypeError(_engine.Realm, "Invalid generic type parameter on " + _path + ", if this is not a generic type / method, are you missing a lookup assembly?", e);
                return(null);
            }
        }
        public JsValue GetPath(string path)
        {
            Type type;

            if (Engine.TypeCache.TryGetValue(path, out type))
            {
                if (type == null)
                {
                    return(new NamespaceReference(Engine, path));
                }

                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search for type in mscorlib
            type = Type.GetType(path);
            if (type != null)
            {
                Engine.TypeCache.Add(path, type);
                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search in loaded assemblies
#if NETFX_CORE
            var lookupAssemblies = new[] { typeof(NamespaceReference).GetTypeInfo().Assembly };
#else
            var lookupAssemblies = new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() };
#endif

            foreach (var assembly in lookupAssemblies)
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    Engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }
            }

            // search in lookup assemblies
            foreach (var assembly in Engine.Options._LookupAssemblies)
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    Engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }

                var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
                var trimPath      = path.Substring(0, lastPeriodPos);
                type = GetType(assembly, trimPath);
                if (type != null)
                {
                    foreach (Type nType in GetAllNestedTypes(type))
                    {
                        if (nType.FullName.Replace("+", ".").Equals(path.Replace("+", ".")))
                        {
                            Engine.TypeCache.Add(path.Replace("+", "."), nType);
                            return(TypeReference.CreateTypeReference(Engine, nType));
                        }
                    }
                }
            }

            // the new path doesn't represent a known class, thus return a new namespace instance

            Engine.TypeCache.Add(path, null);
            return(new NamespaceReference(Engine, path));
        }
        public JsValue GetPath(string path)
        {
            if (_engine.TypeCache.TryGetValue(path, out var type))
            {
                if (type == null)
                {
                    return(new NamespaceReference(_engine, path));
                }

                return(TypeReference.CreateTypeReference(_engine, type));
            }

            // in CoreCLR, for example, classes that used to be in
            // mscorlib were moved away, and only stubs remained, because
            // of that, we do the search on the lookup assemblies first,
            // and only then in mscorlib. Probelm usage: System.IO.File.CreateText

            // search in loaded assemblies
            var lookupAssemblies = new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() };

            foreach (var assembly in lookupAssemblies)
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    _engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(_engine, type));
                }
            }

            // search in lookup assemblies
            var comparedPath = path.Replace("+", ".");

            foreach (var assembly in _engine.Options._LookupAssemblies)
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    _engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(_engine, type));
                }

                var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
                var trimPath      = path.Substring(0, lastPeriodPos);
                type = GetType(assembly, trimPath);
                if (type != null)
                {
                    foreach (Type nType in GetAllNestedTypes(type))
                    {
                        if (nType.FullName.Replace("+", ".").Equals(comparedPath))
                        {
                            _engine.TypeCache.Add(comparedPath, nType);
                            return(TypeReference.CreateTypeReference(_engine, nType));
                        }
                    }
                }
            }

            // search for type in mscorlib
            type = System.Type.GetType(path);
            if (type != null)
            {
                _engine.TypeCache.Add(path, type);
                return(TypeReference.CreateTypeReference(_engine, type));
            }

            // the new path doesn't represent a known class, thus return a new namespace instance

            _engine.TypeCache.Add(path, null);
            return(new NamespaceReference(_engine, path));
        }
Пример #6
0
        public JsValue GetPath(string path)
        {
            Type type;

            if (Engine.TypeCache.TryGetValue(path, out type))
            {
                if (type == null)
                {
                    return(new NamespaceReference(Engine, path));
                }

                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search for type in mscorlib
            type = Type.GetType(path);
            if (type != null)
            {
                Engine.TypeCache.Add(path, type);
                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search in loaded assemblies
            foreach (var assembly in new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }.Distinct())
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    Engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }
            }

            // search in lookup assemblies
            foreach (var assembly in Engine.Options.GetLookupAssemblies())
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    Engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }

                var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
                var trimPath      = path.Substring(0, lastPeriodPos);
                type = GetType(assembly, trimPath);
                if (type != null)
                {
                    foreach (Type nType in GetAllNestedTypes(type))
                    {
                        if (nType.FullName.Replace("+", ".").Equals(path.Replace("+", ".")))
                        {
                            Engine.TypeCache.Add(path.Replace("+", "."), nType);
                            return(TypeReference.CreateTypeReference(Engine, nType));
                        }
                    }
                }
            }

            // the new path doesn't represent a known class, thus return a new namespace instance

            // Jesse: But first check if there is really such a namespace
            //        If we don't do this, Jint will accept dot-seperated
            //        strings of identifiers of arbritrary length without erroring.
            //        If eventually the user attempts to invoke a method at the end of the namespace
            //        chain, they would get a misleading error relating to trying to instantiate
            //        a generic.
            //        Hair loss likely would follow as they try to resolve the wrong thing.
            if (Engine.NamespaceCache == null)
            {
                Engine.NamespaceCache = new NamespaceCacheRoot();
                Engine.NamespaceCache.populate(getReferencedAssemblies(Engine));
            }

            if (!Engine.NamespaceCache.ContainsNamespace(path) && !isGenericTypeDefinition(path, getReferencedAssemblies(Engine)))
            {
                throw new JavaScriptException(Engine.TypeError, path + " is not defined");
            }

            Engine.TypeCache.Add(path, null);
            return(new NamespaceReference(Engine, path));
        }
Пример #7
0
        public JsValue GetPath(string path)
        {
            Type type;

            if (Engine.TypeCache.TryGetValue(path, out type))
            {
                if (type == null)
                {
                    return(new NamespaceReference(Engine, path));
                }

                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search for type in mscorlib
            type = Type.GetType(path);
            if (type != null)
            {
                Engine.TypeCache.Add(path, type);
                return(TypeReference.CreateTypeReference(Engine, type));
            }

            // search in loaded assemblies
            foreach (var assembly in new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }.Distinct())
            {
                type = assembly.GetType(path);
                if (type != null)
                {
                    Engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }
            }

            // search in lookup assemblies
            var assemblies = Engine.Options.LookupAssemblies;

            for (int i = 0, len = assemblies.Count; i < len; i++)
            {
                var assembly = assemblies[i];

                type = assembly.GetType(path);
                if (type != null)
                {
                    Engine.TypeCache.Add(path, type);
                    return(TypeReference.CreateTypeReference(Engine, type));
                }

                var lastPeriodPos = path.LastIndexOf(".", StringComparison.Ordinal);
                var trimPath      = path.Substring(0, lastPeriodPos);
                type = GetType(assembly, trimPath);
                if (type != null)
                {
                    foreach (Type nType in GetAllNestedTypes(type))
                    {
                        if (nType.FullName.Replace("+", ".").Equals(path.Replace("+", ".")))
                        {
                            Engine.TypeCache.Add(path.Replace("+", "."), nType);
                            return(TypeReference.CreateTypeReference(Engine, nType));
                        }
                    }
                }
            }

            // the new path doesn't represent a known class, thus return a new namespace instance

            Engine.TypeCache.Add(path, null);
            return(new NamespaceReference(Engine, path));
        }