コード例 #1
0
        public object Eval()
        {
            Namespace ns = (Namespace)RT.CurrentNSVar.deref();

            ns.importClass(RT.classForName(_c));
            return(null);
        }
コード例 #2
0
ファイル: HostExpr.cs プロジェクト: stjordanis/clojure-clr
        public static Type MaybeType(object form, bool stringOk)
        {
            if (form is Type)
            {
                return((Type)form);
            }

            Type t = null;

            if (form is Symbol)
            {
                Symbol sym = (Symbol)form;
                if (sym.Namespace == null) // if ns-qualified, can't be classname
                {
                    if (Util.equals(sym, Compiler.CompileStubSymVar.get()))
                    {
                        return((Type)Compiler.CompileStubClassVar.get());
                    }
                    if (sym.Name.IndexOf('.') > 0 || sym.Name[sym.Name.Length - 1] == ']')  // Array.  JVM version detects [whatever  notation.

                    {
                        t = RT.classForNameE(sym.Name);
                    }
                    else
                    {
                        object o = Compiler.CurrentNamespace.GetMapping(sym);
                        if (o is Type)
                        {
                            t = (Type)o;
                        }
                        else if (Compiler.LocalEnvVar.deref() != null && ((IPersistentMap)Compiler.LocalEnvVar.deref()).containsKey(form))  // JVM casts to java.util.Map
                        {
                            return(null);
                        }
                        else
                        {
                            try
                            {
                                t = RT.classForName(sym.Name);
                            }
                            catch (Exception)
                            {
                                // aargh
                                // leave t set to null -> return null
                            }
                        }
                    }
                }
            }
            else if (stringOk && form is string)
            {
                t = RT.classForNameE((string)form);
            }

            return(t);
        }
コード例 #3
0
        public static Type MaybeType(object form, bool stringOk)
        {
            if (form is Type)
            {
                return((Type)form);
            }

            Type t = null;

            if (form is Symbol)
            {
                Symbol sym = (Symbol)form;
                if (sym.Namespace == null) // if ns-qualified, can't be classname
                {
                    if (Util.equals(sym, Compiler.CompileStubSymVar.get()))
                    {
                        return((Type)Compiler.CompileStubClassVar.get());
                    }
                    if (sym.Name.IndexOf('.') > 0 || sym.Name[sym.Name.Length - 1] == ']')  // Array.  JVM version detects [whatever  notation.

                    {
                        t = RT.classForName(sym.Name);
                    }
                    else
                    {
                        object o = Compiler.CurrentNamespace.GetMapping(sym);
                        if (o is Type)
                        {
                            t = (Type)o;
                        }
                        else
                        {
                            try
                            {
                                t = RT.classForName(sym.Name);
                            }
                            catch (Exception)
                            {
                                //aargh
                            }
                        }
                    }
                }
            }
            else if (stringOk && form is string)
            {
                t = RT.classForName((string)form);
            }

            return(t);
        }
コード例 #4
0
        [Test]
        public void FindTypeInAssembly()
        {
            AssemblyName aname = new AssemblyName("MyAssy");
            AssemblyBuilder assyBldr = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.RunAndSave, ".");
            ModuleBuilder moduleBldr = assyBldr.DefineDynamicModule(aname.Name, aname.Name + ".dll", true);
            TypeBuilder tb = moduleBldr.DefineType("clojure.proxy.LongName.MyType", TypeAttributes.Public);
            Type myType = tb.CreateType();

            Type[] allTypes = assyBldr.GetTypes();
            Expect(allTypes.Contains<Type>(myType));
            Type findType = assyBldr.GetType("clojure.proxy.LongName.MyType", false);
            Expect(findType).Not.To.Be.Null();
            Type rtFound = RT.classForName("clojure.proxy.LongName.MyType");
            Expect(rtFound).Not.To.Be.Null();
           
コード例 #5
0
ファイル: FnExpr.cs プロジェクト: ryrency/Misc
        // This naming convention drawn from the Java code.
        internal void ComputeNames(ISeq form, string name)
        {
            FnMethod enclosingMethod = (FnMethod)Compiler.METHODS.deref();

            string baseName = enclosingMethod != null
                ? (enclosingMethod.Fn._name + "$")
                : (Compiler.Munge(Compiler.CurrentNamespace.Name.Name) + "$");

            if (RT.second(form) is Symbol)
            {
                name = ((Symbol)RT.second(form)).Name;
            }

            _simpleName   = (name == null ? "fn" : Compiler.Munge(name).Replace(".", "_DOT_")) + "__" + RT.nextID();
            _name         = baseName + _simpleName;
            _internalName = _name.Replace('.', '/');
            _fnType       = RT.classForName(_internalName);
            // fn.fntype = Type.getObjectType(fn.internalName) -- JAVA
        }
コード例 #6
0
            protected override object Read(PushbackTextReader r, char eq)
            {
                if (!RT.booleanCast(RT.READEVAL.deref()))
                {
                    throw new Exception("EvalReader not allowed when *read-eval* is false.");
                }
                Object o = read(r, true, null, true);

                if (o is Symbol)
                {
                    return(RT.classForName(o.ToString()));
                }
                else if (o is IPersistentList)
                {
                    Symbol fs = (Symbol)RT.first(o);
                    if (fs.Equals(THE_VAR))
                    {
                        Symbol vs = (Symbol)RT.second(o);
                        return(RT.var(vs.Namespace, vs.Name));  //Compiler.resolve((Symbol) RT.second(o),true);
                    }
                    if (fs.Name.EndsWith("."))
                    {
                        Object[] args = RT.toArray(RT.next(o));
                        return(Reflector.InvokeConstructor(RT.classForName(fs.Name.Substring(0, fs.Name.Length - 1)), args));
                    }
                    if (Compiler.NamesStaticMember(fs))
                    {
                        Object[] args = RT.toArray(RT.next(o));
                        return(Reflector.InvokeStaticMethod(fs.Namespace, fs.Name, args));
                    }
                    Object v = Compiler.maybeResolveIn(Compiler.CurrentNamespace, fs);
                    if (v is Var)
                    {
                        return(((IFn)v).applyTo(RT.next(o)));
                    }
                    throw new Exception("Can't resolve " + fs);
                }
                else
                {
                    throw new ArgumentException("Unsupported #= form");
                }
            }
コード例 #7
0
ファイル: Reflector.cs プロジェクト: terkhorn/clojure-clr
        // At the moment, only used by the LispReader
        public static Object InvokeStaticMethod(String typeName, String methodName, Object[] args)
        {
            Type t = RT.classForName(typeName);

            return(InvokeStaticMethod(t, methodName, args));
        }
コード例 #8
0
ファイル: ClrTypeSpec.cs プロジェクト: terkhorn/clojure-clr
        public static Type GetTypeFromName(string name)
        {
            ClrTypeSpec spec = Parse(name);

            if (spec == null)
            {
                return(null);
            }
            return(spec.Resolve(
                       assyName => Assembly.Load(assyName),
                       //(assy, typeName) => assy == null ? RT.classForName(typeName) : assy.GetType(typeName));  <--- this goes into an infinite loop on a non-existent typename
                       (assy, typeName) => assy == null ? (name.Equals(typeName) ? null : RT.classForName(typeName)) : assy.GetType(typeName)));
        }
コード例 #9
0
        public static NewInstanceMethod Parse(ObjExpr objx, ISeq form, Symbol thisTag, Dictionary <IPersistentVector, IList <MethodInfo> > overrideables, Dictionary <IPersistentVector, IList <MethodInfo> > explicits)
        {
            // (methodname [this-name args*] body...)
            // this-name might be nil

            NewInstanceMethod method = new NewInstanceMethod(objx, (ObjMethod)Compiler.MethodVar.deref());

            Symbol dotName = (Symbol)RT.first(form);
            Symbol name;
            string methodName;

            int idx = dotName.Name.LastIndexOf(".");

            if (idx >= 0)
            {
                // we have an explicit interface implementation
                string dotNameStr    = dotName.Name;
                string interfaceName = dotNameStr.Substring(0, idx);

                method.ExplicitInterface = RT.classForName(interfaceName);
                if (method.ExplicitInterface == null)
                {
                    throw new ParseException(String.Format("Unable to find interface {0} for explicit method implemntation: {1}", interfaceName, dotNameStr));
                }

                methodName = dotNameStr.Substring(idx + 1);
                name       = (Symbol)Symbol.intern(null, Compiler.munge(dotName.Name)).withMeta(RT.meta(dotName));
            }
            else
            {
                name       = (Symbol)Symbol.intern(null, Compiler.munge(dotName.Name)).withMeta(RT.meta(dotName));
                methodName = name.Name;
            }

            IPersistentVector parms = (IPersistentVector)RT.second(form);

            if (parms.count() == 0 || !(parms.nth(0) is Symbol))
            {
                throw new ParseException("Must supply at least one argument for 'this' in: " + dotName);
            }

            Symbol thisName = (Symbol)parms.nth(0);

            parms = RT.subvec(parms, 1, parms.count());
            ISeq body = RT.next(RT.next(form));

            try
            {
                method.SpanMap = (IPersistentMap)Compiler.SourceSpanVar.deref();

                // register as the current method and set up a new env frame
                // PathNode pnade = new PathNode(PATHTYPE.PATH, (PathNode) CLEAR_PATH.get());
                Var.pushThreadBindings(
                    RT.mapUniqueKeys(
                        Compiler.MethodVar, method,
                        Compiler.LocalEnvVar, Compiler.LocalEnvVar.deref(),
                        Compiler.LoopLocalsVar, null,
                        Compiler.NextLocalNumVar, 0
                        // CLEAR_PATH, pnode,
                        // CLEAR_ROOT, pnode,
                        // CLEAR_SITES, PersistentHashMap.EMPTY
                        ));

                // register 'this' as local 0
                //method._thisBinding = Compiler.RegisterLocalThis(((thisName == null) ? dummyThis : thisName), thisTag, null);
                Compiler.RegisterLocalThis(((thisName == null) ? dummyThis : thisName), thisTag, null);

                IPersistentVector argLocals = PersistentVector.EMPTY;
                method._retType  = Compiler.TagType(Compiler.TagOf(name));
                method._argTypes = new Type[parms.count()];
                bool     hinted = Compiler.TagOf(name) != null;
                Type[]   pTypes = new Type[parms.count()];
                Symbol[] pSyms  = new Symbol[parms.count()];
                bool[]   pRefs  = new bool[parms.count()];

                for (int i = 0; i < parms.count(); i++)
                {
                    // Param should be symbol or (by-ref symbol)
                    Symbol p;
                    bool   isByRef = false;

                    object pobj = parms.nth(i);
                    if (pobj is Symbol)
                    {
                        p = (Symbol)pobj;
                    }
                    else if (pobj is ISeq)
                    {
                        ISeq   pseq   = (ISeq)pobj;
                        object first  = RT.first(pseq);
                        object second = RT.second(pseq);
                        if (!(first is Symbol && ((Symbol)first).Equals(HostExpr.ByRefSym)))
                        {
                            throw new ParseException("First element in parameter pair must be by-ref");
                        }
                        if (!(second is Symbol))
                        {
                            throw new ParseException("Params must be Symbols");
                        }
                        isByRef = true;
                        p       = (Symbol)second;
                        hinted  = true;
                    }
                    else
                    {
                        throw new ParseException("Params must be Symbols or of the form (by-ref Symbol)");
                    }

                    object tag = Compiler.TagOf(p);
                    if (tag != null)
                    {
                        hinted = true;
                    }
                    if (p.Namespace != null)
                    {
                        p = Symbol.intern(p.Name);
                    }
                    Type pType = Compiler.TagType(tag);
                    if (isByRef)
                    {
                        pType = pType.MakeByRefType();
                    }

                    pTypes[i] = pType;
                    pSyms[i]  = p;
                    pRefs[i]  = isByRef;
                }

                Dictionary <IPersistentVector, IList <MethodInfo> > matches =
                    method.IsExplicit
                    ? FindMethodsWithNameAndArity(method.ExplicitInterface, methodName, parms.count(), overrideables, explicits)
                    : FindMethodsWithNameAndArity(methodName, parms.count(), overrideables);

                IPersistentVector  mk = MSig(methodName, pTypes, method._retType);
                IList <MethodInfo> ms = null;
                if (matches.Count > 0)
                {
                    // multiple matches
                    if (matches.Count > 1)
                    {
                        // must be hinted and match one method
                        if (!hinted)
                        {
                            throw new ParseException("Must hint overloaded method: " + name.Name);
                        }
                        if (!matches.TryGetValue(mk, out ms))
                        {
                            throw new ParseException("Can't find matching overloaded method: " + name.Name);
                        }

                        method._minfos = ms;
                    }
                    else // one match
                    {
                        // if hinted, validate match,
                        if (hinted)
                        {
                            if (!matches.TryGetValue(mk, out ms))
                            {
                                throw new ParseException("Can't find matching method: " + name.Name + ", leave off hints for auto match.");
                            }

                            method._minfos = ms;

                            //if (m.ReturnType != method._retType)
                            //    throw new ArgumentException(String.Format("Mismatched return type: {0}, expected {1}, had: {2}",
                            //        name.Name, m.ReturnType.Name, method._retType.Name));
                        }
                        else // adopt found method sig
                        {
                            using (var e = matches.GetEnumerator())
                            {
                                e.MoveNext();
                                mk = e.Current.Key;
                                ms = e.Current.Value;
                            }
                            MethodInfo m = ms[0];
                            method._retType = m.ReturnType;
                            pTypes          = Compiler.GetTypes(m.GetParameters());
                            method._minfos  = ms;
                        }
                    }
                }
                else
                {
                    throw new ParseException("Can't define method not in interfaces: " + name.Name);
                }

                if (method.IsExplicit)
                {
                    method.ExplicitMethodInfo = ms[0];
                }

                // validate unique name + arity among additional methods

                for (int i = 0; i < parms.count(); i++)
                {
                    LocalBinding lb = Compiler.RegisterLocal(pSyms[i], null, new MethodParamExpr(pTypes[i]), true, pRefs[i]);
                    argLocals           = argLocals.assocN(i, lb);
                    method._argTypes[i] = pTypes[i];
                }

                Compiler.LoopLocalsVar.set(argLocals);
                method._name      = name.Name;
                method.MethodMeta = GenInterface.ExtractAttributes(RT.meta(name));
                method.Parms      = parms;
                method.ArgLocals  = argLocals;
                method.Body       = (new BodyExpr.Parser()).Parse(new ParserContext(RHC.Return), body);
                return(method);
            }
            finally
            {
                Var.popThreadBindings();
            }
        }