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); } }
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)); }
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; } }
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")); }
/// <summary> /// simple := type "(" str ")" /// </summary> private object readSimple(int line, Type t) { // parse: type(str) consume(Token.LPAREN, "Expected ( in simple"); string str = consumeStr("Expected string literal for simple"); consume(Token.RPAREN, "Expected ) in simple"); // lookup the fromStr method t.finish(); Method m = t.method("fromStr", false); if (m == null) throw err("Missing method: " + t.qname() + ".fromStr", line); // invoke parse method to translate into instance try { return m.invoke(null, new object[] { str }); } catch (ParseErr.Val e) { throw ParseErr.make(e.err().msg() + " [Line " + line + "]").val; } catch (System.Exception e) { throw ParseErr.make(e.ToString() + " [Line " + line + "]", e).val; } }
////////////////////////////////////////////////////////////////////////// // Complex ////////////////////////////////////////////////////////////////////////// /// <summary> /// complex := type [fields] /// fields := "{" field (eos field)* "}" /// field := name "=" obj /// </summary> private object readComplex(int line, Type t, bool root) { Map toSet = new Map(Sys.FieldType, Sys.ObjType.toNullable()); List toAdd = new List(Sys.ObjType.toNullable()); // read fields/collection into toSet/toAdd readComplexFields(t, toSet, toAdd); // get the make constructor Method makeCtor = t.method("make", false); if (makeCtor == null || !makeCtor.isPublic()) throw err("Missing public constructor " + t.qname() + ".make", line); // get argument lists List args = null; if (root && options != null) args = (List)options.get("makeArgs"); // construct object object obj = null; bool setAfterCtor = true; try { // if first parameter is an function then pass toSet // as an it-block for setting the fields Param p = (Param)makeCtor.@params().first(); if (args == null && p != null && p.type().fits(Sys.FuncType)) { args = new List(Sys.ObjType).add(Field.makeSetFunc(toSet)); setAfterCtor = false; } // invoke make to construct object obj = makeCtor.callList(args); } catch (System.Exception e) { throw err("Cannot make " + t + ": " + e, line, e); } // set fields (if not passed to ctor as it-block) if (setAfterCtor && toSet.size() > 0) { IDictionaryEnumerator en = toSet.pairsIterator(); while (en.MoveNext()) { complexSet(obj, (Field)en.Key, en.Value, line); } } // add if (toAdd.size() > 0) { Method addMethod = t.method("add", false); if (addMethod == null) throw err("Method not found: " + t.qname() + ".add", line); for (int i=0; i<toAdd.sz(); ++i) complexAdd(t, obj, addMethod, toAdd.get(i), line); } return obj; }
////////////////////////////////////////////////////////////////////////// // Collection (@collection) ////////////////////////////////////////////////////////////////////////// private bool writeCollectionItems(Type type, object obj, bool first) { // lookup each method Method m = type.method("each", false); if (m == null) throw IOErr.make("Missing " + type.qname() + ".each").val; // call each(it) EachIterator it = new EachIterator(this, first); m.invoke(obj, new object[] { it }); return it.first; }