Type models a static type definition for an Obj class. A Type lifecycle: 1) Hollow: in this state we know basic identity of the type, and it's inheritance hierarchy. A type is setup to be hollow during Pod.load(). 2) Reflected: in this state we read all the slot definitions from the fcode to populate the slot tables used to for reflection. At this point clients can discover the signatures of the Type. 3) Emitted: the final state of loading a Type is to emit to a Java class called "fan.{pod}.{type}". Once emitted we can instantiate the type or call it's methods. 4) Finished: once we have reflected the slots into memory and emitted the Java class, the last stage is to bind the all the java.lang.reflect representations to the Slots for dynamic dispatch. We delay this until needed by Method or Field for a reflection invocation Type models sys::Type. Implementation classes are: - ClassType - GenericType (ListType, MapType, FuncType) - NullableType
Inheritance: FanObj, Literal
Exemplo n.º 1
0
        int executeType(string target)
        {
            if (target.IndexOf("::") < 0)
            {
                target += "::Main.main";
            }
            else if (target.IndexOf('.') < 0)
            {
                target += ".main";
            }

            try
            {
                int    dot  = target.IndexOf('.');
                Type   type = Type.find(target.Substring(0, dot), true);
                Method main = type.method(target.Substring(dot + 1), true);
                return(callMain(type, main));
            }
            catch (Exception e)
            {
                if (e is Err.Val)
                {
                    ((Err.Val)e).err().trace();
                }
                else
                {
                    Err.dumpStack(e);
                }
                return(-1);
            }
        }
Exemplo n.º 2
0
        //////////////////////////////////////////////////////////////////////////
        // Constructor
        //////////////////////////////////////////////////////////////////////////
        public FuncType(Type[] pars, Type ret)
            : base(Sys.FuncType)
        {
            this.m_params = pars;
              this.m_ret    = ret;

              // I am a generic parameter type if any my args or
              // return type are generic parameter types.
              this.m_genericParameterType |= ret.isGenericParameter();
              for (int i=0; i<m_params.Length; ++i)
            this.m_genericParameterType |= m_params[i].isGenericParameter();
        }
Exemplo n.º 3
0
 public static List findAll(Type t)
 {
     string qname = t.qname();
       List list = new List(Sys.ServiceType);
       lock (m_lock)
       {
     Node node = (Node)byType[qname];
     while (node != null)
     {
       list.add(node.service);
       node = node.next;
     }
       }
       return list.ro();
 }
Exemplo n.º 4
0
Arquivo: Enum.cs Projeto: nomit007/f4
 protected static Enum doFromStr(Type t, string name, bool check)
 {
     // the compiler marks the value fields with the Enum flag
       Slot slot = t.slot(name, false);
       if (slot != null && (slot.m_flags & FConst.Enum) != 0)
       {
     try
     {
       return (Enum)((Field)slot).get(null);
     }
     catch (System.Exception)
     {
     }
       }
       if (!check) return null;
       throw ParseErr.make(t.qname(), name).val;
 }
Exemplo n.º 5
0
        public int executeFile(FileInfo file)
        {
            LocalFile f = (LocalFile)(new LocalFile(file).normalize());

            // use Fantom reflection to run compiler::Main.compileScript(File)
            Pod pod = null;

            try
            {
                pod = Env.cur().compileScript(f).pod();
            }
            catch (Err.Val e)
            {
                System.Console.WriteLine("ERROR: cannot compile script");
                e.err().trace();
                return(-1);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("ERROR: cannot compile script");
                System.Console.WriteLine(e);
                return(-1);
            }

            List   types = pod.types();
            Type   type  = null;
            Method main  = null;

            for (int i = 0; i < types.sz(); ++i)
            {
                type = (Type)types.get(i);
                main = type.method("main", false);
                if (main != null)
                {
                    break;
                }
            }

            if (main == null)
            {
                System.Console.WriteLine("ERROR: missing main method: " + ((Type)types.get(0)).name() + ".main()");
                return(-1);
            }

            return(callMain(type, main));
        }
Exemplo n.º 6
0
        public Facet decode(Type type, string s)
        {
            try
              {
            // if no string use make/defVal
            if (s.Length == 0) return (Facet)type.make();

            // decode using normal Fantom serialization
            return (Facet)ObjDecoder.decode(s);
              }
              catch (System.Exception e)
              {
            string msg = "ERROR: Cannot decode facet " + type + ": " + s;
            System.Console.WriteLine(msg);
            Err.dumpStack(e);
            m_map.Remove(type);
            throw IOErr.make(msg).val;
              }
        }
Exemplo n.º 7
0
        public override bool @is(Type type)
        {
            if (this == type) return true;
              if (type is FuncType)
              {
            FuncType t = (FuncType)type;

            // match return type (if void is needed, anything matches)
            if (t.m_ret != Sys.VoidType && !m_ret.@is(t.m_ret)) return false;

            // match params - it is ok for me to have less than
            // the type params (if I want to ignore them), but I
            // must have no more
            if (m_params.Length > t.m_params.Length) return false;
            for (int i=0; i<m_params.Length; ++i)
              if (!t.m_params[i].@is(m_params[i])) return false;

            // this method works for the specified method type
            return true;
              }
              return @base().@is(type);
        }
Exemplo n.º 8
0
        static int callMain(Type t, Method m)
        {
            // check parameter type and build main arguments
            List args;
            List pars = m.@params();

            if (pars.sz() == 0)
            {
                args = null;
            }
            else if (((Param)pars.get(0)).type().@is(Sys.StrType.toListOf()) &&
                     (pars.sz() == 1 || ((Param)pars.get(1)).hasDefault()))
            {
                args = new List(Sys.ObjType, new object[] { Env.cur().args() });
            }
            else
            {
                System.Console.WriteLine("ERROR: Invalid parameters for main: " + m.signature());
                return(-1);
            }

            // invoke
            try
            {
                if (m.isStatic())
                {
                    return(toResult(m.callList(args)));
                }
                else
                {
                    return(toResult(m.callOn(t.make(), args)));
                }
            }
            catch (Err.Val ex)
            {
                ex.err().trace();
                return(-1);
            }
        }
Exemplo n.º 9
0
 static bool isServiceType(Type t)
 {
     return t != Sys.ObjType && t != Sys.ServiceType && t.isPublic();
 }
Exemplo n.º 10
0
        private void findMixins(Type t, Hashtable acc)
        {
            // if mixin I haven't seen add to accumulator
              string qname = t.qname();
              if (t.isMixin() && acc[qname] == null)
            acc[qname] = t;

              // recurse
              for (int i=0; i<t.mixins().sz(); ++i)
            findMixins((Type)t.mixins().get(i), acc);
        }
Exemplo n.º 11
0
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 internal NullableType(Type root)
 {
     m_root = root;
       m_signature = root.signature() + "?";
 }
Exemplo n.º 12
0
        /// <summary>
        /// Recursively parameterize the pars of a method type.
        /// </summary>
        internal FuncType parameterizeFuncType(FuncType t)
        {
            Type[] pars = new Type[t.m_params.Length];
              for (int i=0; i<pars.Length; i++)
              {
            Type param = t.m_params[i];
            if (param.isGenericParameter()) param = doParameterize(param);
            pars[i] = param;
              }

              Type ret = t.m_ret;
              if (ret.isGenericParameter()) ret = doParameterize(ret);

              return new FuncType(pars, ret);
        }
Exemplo n.º 13
0
 public override sealed Type toNullable()
 {
     if (m_nullable == null) m_nullable = new NullableType(this);
       return m_nullable;
 }
Exemplo n.º 14
0
Arquivo: Fant.cs Projeto: nomit007/f4
        private Method[] methods(Type type, string methodName)
        {
            // named test
              if (methodName != "*") return new Method[] { type.method(methodName, true) };

              // all methods which start with "test"
              List all = type.methods();
              ArrayList acc = new ArrayList();
              for (int i=0; i<all.sz(); i++)
              {
            Method m = (Method)all.get(i);
            if (m.name().StartsWith("test") && !m.isAbstract()) acc.Add(m);
              }
              return (Method[])acc.ToArray(System.Type.GetType("Fan.Sys.Method"));
        }
Exemplo n.º 15
0
 //////////////////////////////////////////////////////////////////////////
 // Is/As
 //////////////////////////////////////////////////////////////////////////
 public static bool @is(object instance, Type type)
 {
     if (instance == null) return false;
       return FanObj.@typeof(instance).fits(type);
 }
Exemplo n.º 16
0
 internal UsingType(Type t, string n)
 {
     type = t; name = n;
 }
Exemplo n.º 17
0
 public override bool @is(Type type)
 {
     return m_root.@is(type);
 }
Exemplo n.º 18
0
 public static Service find(Type t)
 {
     return find(t.qname(), true);
 }
Exemplo n.º 19
0
Arquivo: Fant.cs Projeto: nomit007/f4
        private int runTest(Type type, Method method)
        {
            Method setup    = type.method("setup", true);
              Method teardown = type.method("teardown", true);

              FanSysTest test = null;
              List args = null;
              try
              {
            test = (FanSysTest)type.make();
            args = new List(Sys.ObjType, new object[] {test});
              }
              catch (System.Exception e)
              {
            System.Console.WriteLine();
            System.Console.WriteLine("ERROR: Cannot make test " + type);
            if (e is Err.Val)
              ((Err.Val)e).err().trace();
            else
              Err.dumpStack(e);
            return -1;
              }

              try
              {
            test.m_curTestMethod = method;
            setup.callList(args);
            method.callList(args);
            return test.verifyCount;
              }
              catch (System.Exception e)
              {
            //System.Console.WriteLine(" -- " + e.GetType() + " -- ");
            System.Console.WriteLine();
            System.Console.WriteLine("TEST FAILED");
            if (e is Err.Val)
              ((Err.Val)e).err().trace();
            else
              Err.dumpStack(e);
            return -1;
              }
              finally
              {
            try
            {
              if (args != null) teardown.callList(args);
            }
            catch (System.Exception e)
            {
              Err.dumpStack(e);
            }
            test.m_curTestMethod = null;
              }
        }
Exemplo n.º 20
0
 public static Service find(Type t, bool check)
 {
     return find(t.qname(), check);
 }
Exemplo n.º 21
0
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 internal GenericType(Type baseType)
 {
     m_base = baseType;
 }
Exemplo n.º 22
0
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 public FErrEmit(Emitter emitter, Type parent, FType type)
     : base(emitter, parent, type)
 {
 }
Exemplo n.º 23
0
 public override Facet facet(Type t, bool c)
 {
     return m_base.facet(t, c);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Parameterize t, where t is a generic parameter type such as V.
 /// </summary>
 internal Type parameterize(Type t)
 {
     bool nullable = t.isNullable();
       Type nn = t.toNonNullable();
       if (nn is ListType)
     t = parameterizeListType((ListType)nn);
       else if (nn is FuncType)
     t = parameterizeFuncType((FuncType)nn);
       else
     t = doParameterize(nn);
       return nullable ? t.toNullable() : t;
 }
Exemplo n.º 25
0
 //////////////////////////////////////////////////////////////////////////
 // Obj
 //////////////////////////////////////////////////////////////////////////
 public override Type @typeof()
 {
     if (m_type == null) m_type = Type.find("concurrent::ActorPool");
       return m_type;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Parameterize t, where t is a generic parameter type such as V.
 /// </summary>
 protected abstract Type doParameterize(Type t);
Exemplo n.º 27
0
 void complexAdd(Type t, object obj, Method addMethod, object val, int line)
 {
     try
       {
     addMethod.invoke(obj, new object[] { val });
       }
       catch (System.Exception ex)
       {
     throw err("Cannot call " + t.qname() + ".add: " + ex, line, ex);
       }
 }
Exemplo n.º 28
0
 public override bool @is(Type type)
 {
     if (type == this || type == m_base) return true;
       return m_base.@is(type);
 }
Exemplo n.º 29
0
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 public FMixinInterfaceEmit(Emitter emitter, Type parent, FType type)
     : base(emitter, parent, type)
 {
 }
Exemplo n.º 30
0
        /// <summary>
        /// The CLR requires all methods implemented by an interface
        /// to be marked as virtual.  However, Fantom (and Java) allow
        /// this:
        ///
        ///   mixin Mixin
        ///   {
        ///     abstract Long foo()
        ///   }
        ///
        ///   class Base
        ///   {
        ///     Long foo() { return 5 }
        ///   }
        ///
        ///   class Child : Base, Mixin
        ///   {
        ///     // don't need to implement foo() since Base defined it
        ///   }
        ///
        /// So to work correctly in the CLR we need to trap this case
        /// and emit a virtual router method on child to satisfy the
        /// interface requirement:
        ///
        ///   class Child : Base, Mixin
        ///   {
        ///     public virtual Long foo() { return base.foo(); }
        ///   }
        ///
        ///   TODO - optimize the intra-pod case
        ///
        /// </summary>
        public void emitInterfaceRouter(Type implType, Method m)
        {
            string impl = FanUtil.toDotnetTypeName(implType);
              string name = m.name();
              string ret  = FanUtil.toDotnetTypeName(m.inheritedReturns());
              List pars   = m.@params();
              int paramCount = pars.sz();

              // find first param with default value
              int firstDefault = paramCount;
              for (int i=0; i<paramCount; i++)
            if (((Param)pars.get(i)).hasDefault())
              { firstDefault = i; break; }

              // generate routers
              for (int i=firstDefault; i<=paramCount; i++)
              {
            string[] myParams = new string[i];
            string[] myParamNames = new string[i];

            for (int j=0; j<i; j++)
            {
              Param param = (Param)m.@params().get(j);
              Type pt = param.type();
              string s = FanUtil.toDotnetTypeName(pt);
              myParams[j] = s;
              myParamNames[j] = param.name();
            }

            // CLR requires public virtual
            PERWAPI.MethAttr attr = PERWAPI.MethAttr.Public | PERWAPI.MethAttr.Virtual;

            PERWAPI.CILInstructions code = emitter.emitMethod(name, ret, myParamNames, myParams,
              attr, new string[0], new string[0]);
            code.Inst(PERWAPI.Op.ldarg_0); // push this
            for (int p=0; p<i; p++)
            {
              // push args
              Param param = (Param)m.@params().get(p);
              FCodeEmit.loadVar(code, FanUtil.toDotnetStackType(param.type()), p+1);
            }
            PERWAPI.Method meth = emitter.findMethod(impl, name, myParams, ret);
            code.MethInst(PERWAPI.MethodOp.call, meth);
            code.Inst(PERWAPI.Op.ret);
              }
        }
Exemplo n.º 31
0
        private static Hashtable ftypes = new Hashtable(); // ftype[] lookup

        #endregion Fields

        #region Constructors

        //////////////////////////////////////////////////////////////////////////
        // Constructor
        //////////////////////////////////////////////////////////////////////////
        protected FTypeEmit(Emitter emitter, Type parent, FType type)
        {
            this.emitter = emitter;
              this.parent  = parent;
              this.pod     = type.m_pod;
              this.type    = type;
        }
Exemplo n.º 32
0
        void putCache(File file, Type t)
        {
            CachedScript c = new CachedScript();
              c.modified = file.modified();
              c.size     = file.size();
              c.typeName = t.qname();

              lock (m_cache) { m_cache[cacheKey(file)] = c; }
        }
Exemplo n.º 33
0
        private void emitMixinRouters(Type type)
        {
            // generate router method for each concrete instance method
              List methods = type.methods();
              for (int i=0; i<methods.sz(); i++)
              {
            Method m = (Method)methods.get(i);
            string name = m.name();

            // only emit router for non-abstract instance methods
            if (m.isStatic()) continue;
            if (m.isAbstract())
            {
              // however if abstract, check if a base class
              // has already implemented this method
              if (m.parent() == type && parent.slot(name, true).parent() == type)
              {
            Type b = parent.@base();
            while (b != null)
            {
              Slot s = b.slot(name, false);
              if (s != null && s.parent() == b)
              {
                new FMethodEmit(this).emitInterfaceRouter(b, m);
                break;
              }
              b = b.@base();
            }
              }
              continue;
            }

            // only emit the router unless this is the exact one I inherit
            if (parent.slot(name, true).parent() != type) continue;

            // do it
            new FMethodEmit(this).emitMixinRouter(m);
              }
        }