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); } }
////////////////////////////////////////////////////////////////////////// // 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(); }
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(); }
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; }
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)); }
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; } }
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); }
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); } }
static bool isServiceType(Type t) { return t != Sys.ObjType && t != Sys.ServiceType && t.isPublic(); }
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); }
////////////////////////////////////////////////////////////////////////// // Constructor ////////////////////////////////////////////////////////////////////////// internal NullableType(Type root) { m_root = root; m_signature = root.signature() + "?"; }
/// <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); }
public override sealed Type toNullable() { if (m_nullable == null) m_nullable = new NullableType(this); return m_nullable; }
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")); }
////////////////////////////////////////////////////////////////////////// // Is/As ////////////////////////////////////////////////////////////////////////// public static bool @is(object instance, Type type) { if (instance == null) return false; return FanObj.@typeof(instance).fits(type); }
internal UsingType(Type t, string n) { type = t; name = n; }
public override bool @is(Type type) { return m_root.@is(type); }
public static Service find(Type t) { return find(t.qname(), true); }
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; } }
public static Service find(Type t, bool check) { return find(t.qname(), check); }
////////////////////////////////////////////////////////////////////////// // Constructor ////////////////////////////////////////////////////////////////////////// internal GenericType(Type baseType) { m_base = baseType; }
////////////////////////////////////////////////////////////////////////// // Constructor ////////////////////////////////////////////////////////////////////////// public FErrEmit(Emitter emitter, Type parent, FType type) : base(emitter, parent, type) { }
public override Facet facet(Type t, bool c) { return m_base.facet(t, c); }
/// <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; }
////////////////////////////////////////////////////////////////////////// // Obj ////////////////////////////////////////////////////////////////////////// public override Type @typeof() { if (m_type == null) m_type = Type.find("concurrent::ActorPool"); return m_type; }
/// <summary> /// Parameterize t, where t is a generic parameter type such as V. /// </summary> protected abstract Type doParameterize(Type t);
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); } }
public override bool @is(Type type) { if (type == this || type == m_base) return true; return m_base.@is(type); }
////////////////////////////////////////////////////////////////////////// // Constructor ////////////////////////////////////////////////////////////////////////// public FMixinInterfaceEmit(Emitter emitter, Type parent, FType type) : base(emitter, parent, type) { }
/// <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); } }
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; }
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; } }
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); } }