////////////////////////////////////////////////////////////////////////// // Bind ////////////////////////////////////////////////////////////////////////// public Func bind(List args) { if (args.sz() == 0) return this; if (args.sz() > @params().sz()) throw ArgErr.make("args.size > params.size").val; Type[] newParams = new Type[@params().sz()-args.sz()]; for (int i=0; i<newParams.Length; ++i) newParams[i] = ((Param)@params().get(args.sz()+i)).m_type; FuncType newType = new FuncType(newParams, this.returns()); return new BindFunc(newType, this, args); }
////////////////////////////////////////////////////////////////////////// // List ////////////////////////////////////////////////////////////////////////// public void writeList(List list) { // get of type Type of = list.of(); // decide if we're going output as single or multi-line format bool nl = isMultiLine(of); // figure out if we can use an inferred type bool inferred = false; if (curFieldType != null && curFieldType.fits(Sys.ListType)) { inferred = true; } // clear field type, so it doesn't get used for inference again curFieldType = null; // if we don't have an inferred type, then prefix of type if (!inferred) { wType(of); } // handle empty list int size = list.sz(); if (size == 0) { w("[,]"); return; } // items if (nl) { w('\n').wIndent(); } w('['); level++; for (int i = 0; i < size; ++i) { if (i > 0) { w(','); } if (nl) { w('\n').wIndent(); } writeObj(list.get(i)); } level--; if (nl) { w('\n').wIndent(); } w(']'); }
public void test(string pattern, bool verbose) { if (pattern == "*") { List pods = Pod.list(); for (int i = 0; i < pods.sz(); i++) { Pod p = (Pod)pods.get(i); test(p.name(), verbose); } return; } if (pattern == "sys" || pattern.StartsWith("sys::")) { writeLine(""); //if (pattern == "sys") // pattern = ""; //else // pattern = pattern.Substring(5); if (!Fanx.Test.Test.RunTests(null)) { failures++; } totalVerifyCount += Fanx.Test.Test.totalVerified; return; } string[] s = pattern.Split(new char[] { ':', '.' }); string podName = s[0]; string testName = (s.Length > 2) ? s[2] : "*"; string methodName = (s.Length > 3) ? s[3] : "*"; writeLine(""); Pod pod = Pod.doFind(podName, true, null); Type[] t = tests(pod, testName); for (int i = 0; i < t.Length; i++) { Method[] m = methods(t[i], methodName); for (int j = 0; j < m.Length; j++) { writeLine("-- Run: " + m[j] + "..."); int verifyCount = runTest(t[i], m[j]); if (verifyCount < 0) { failures++; failedMethods.Add(m[j]); continue; } writeLine(" Pass: "******" [" + verifyCount + "]"); methodCount++; totalVerifyCount += verifyCount; } testCount++; } }
/// <summary> /// Emit a mixin router from a class to the mixin body methods. /// </summary> public void emitMixinRouter(Method m) { string parent = FanUtil.toDotnetTypeName(m.parent()); string name = FanUtil.toDotnetMethodName(m.name()); string ret = FanUtil.toDotnetTypeName(m.inheritedReturns()); string[] parTypes = new string[] { parent }; 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]; string[] implParams = new string[i + 1]; implParams[0] = parent; 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(); implParams[j + 1] = s; } // 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(parent + "_", name, implParams, ret); code.MethInst(PERWAPI.MethodOp.call, meth); code.Inst(PERWAPI.Op.ret); } }
public virtual void walk(Func c) { c.call(this); if (isDir()) { List x = list(); for (int i = 0; i < x.sz(); ++i) { ((File)x.get(i)).walk(c); } } }
////////////////////////////////////////////////////////////////////////// // Bind ////////////////////////////////////////////////////////////////////////// public Func bind(List args) { if (args.sz() == 0) { return(this); } if (args.sz() > @params().sz()) { throw ArgErr.make("args.size > params.size").val; } Type[] newParams = new Type[@params().sz() - args.sz()]; for (int i = 0; i < newParams.Length; ++i) { newParams[i] = ((Param)@params().get(args.sz() + i)).m_type; } FuncType newType = new FuncType(newParams, this.returns()); return(new BindFunc(newType, this, args)); }
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); } }
public virtual List listFiles(Regex pattern) { List x = list(pattern); for (int i = x.sz() - 1; i >= 0; --i) { if (((File)x.get(i)).isDir()) { x.removeAt(i); } } return(x); }
////////////////////////////////////////////////////////////////////////// // Public ////////////////////////////////////////////////////////////////////////// public Type compile(File file, Map options) { // normalize the file path as our cache key file = file.normalize(); // unless force=true, check the cache if (!getOption(options, m_strForce, false)) { CachedScript c = getCache(file); // if cached, try to lookup type (it might have been GCed) if (c != null) { Type t1 = Type.find(c.typeName, false); if (t1 != null) { return(t1); } } } // generate a unique pod name string podName = generatePodName(file); // compile the script Pod pod = compile(podName, file, options); // get the primary type List types = pod.types(); Type t = null; for (int i = 0; i < types.sz(); ++i) { t = (Type)types.get(i); if (t.isPublic()) { break; } } if (t == null) { throw Err.make("Script file defines no public classes: " + file).val; } // put it into the cache putCache(file, t); return(t); }
////////////////////////////////////////////////////////////////////////// // Load ////////////////////////////////////////////////////////////////////////// private void load() { Log log = Log.get("podindex"); // load all the props List podNames = m_env.findAllPodNames(); Hashtable mutable = new Hashtable(podNames.sz() * 11); for (int i = 0; i < podNames.sz(); ++i) { string n = (string)podNames.get(i); try { FileSystemInfo f = ((LocalFile)m_env.findPodFile(n)).toDotnet(); loadPod(mutable, n, f); } catch (System.Exception e) { log.err("Cannot load " + n, e); } } // now make all the lists immutable List keys = new List(Sys.StrType); Hashtable immutable = new Hashtable(mutable.Count * 3); IDictionaryEnumerator en = mutable.GetEnumerator(); while (en.MoveNext()) { immutable[en.Key] = ((List)en.Value).toImmutable(); keys.add(en.Key); } this.m_index = immutable; this.m_keys = (List)keys.sort().toImmutable(); }
static Map read(Map defProps, Key key, List files) { if (files.isEmpty()) { return(defProps); } Map acc = defProps.dup(); for (int i = files.sz() - 1; i >= 0; --i) { InStream input = ((File)files.get(i)).@in(); try { acc.setAll(input.readProps()); } finally { input.close(); } } return((Map)acc.toImmutable()); }
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 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); } }
static void pods(String progName) { version(progName); long t1 = Duration.nowTicks(); List pods = Pod.list(); long t2 = Duration.nowTicks(); writeLine(""); writeLine("Fantom Pods [" + (t2 - t1) / 1000000L + "ms]:"); writeLine(" Pod Version"); writeLine(" --- -------"); for (int i = 0; i < pods.sz(); ++i) { Pod pod = (Pod)pods.get(i); writeLine(" " + FanStr.justl(pod.name(), 18L) + " " + FanStr.justl(pod.version().toStr(), 8)); } }
internal void attachTo(Control control) { // sync with native control this.m_control = control; if (m_pos != Fan.Fwt.Point.m_def) { pos(m_self, m_pos); } if (m_size != Fan.Fwt.Size.m_def) { size(m_self, m_size); } sync(null); // recursively attach my children List kids = m_self.m_kids; for (int i = 0; i < kids.sz(); ++i) { Widget kid = (Widget)kids.get(i); kid.m_peer.attach(kid); } }
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"))); }
private Type[] tests(Pod pod, string testName) { // named test if (testName != "*") { return new Type[] { pod.type(testName, true) } } ; // all types which subclass Test List all = pod.types(); ArrayList acc = new ArrayList(); for (int i = 0; i < all.sz(); i++) { Type x = (Type)all.get(i); if (x.@is(Sys.TestType) && !x.isAbstract()) { acc.Add(x); } } return((Type[])acc.ToArray(System.Type.GetType("Fan.Sys.Type"))); }
public override object callList(List args) { int origReq = m_orig.@params().sz(); int haveSize = m_bound.sz() + args.sz(); Method m = m_orig.method(); if (m != null) { origReq = m.minParams(); if (haveSize > origReq) { origReq = haveSize; } } if (origReq <= m_bound.sz()) { return(m_orig.callList(m_bound)); } object[] temp = new object[haveSize]; m_bound.copyInto(temp, 0, m_bound.sz()); args.copyInto(temp, m_bound.sz(), temp.Length - m_bound.sz()); return(m_orig.callList(new List(Sys.ObjType, temp))); }
////////////////////////////////////////////////////////////////////////// // Complex ////////////////////////////////////////////////////////////////////////// private void writeComplex(Type type, object obj, Serializable ser) { wType(type); bool first = true; object defObj = null; if (skipDefaults) { defObj = FanObj.@typeof(obj).make(); } List fields = type.fields(); for (int i = 0; i < fields.sz(); ++i) { Field f = (Field)fields.get(i); // skip static, transient, and synthetic (once) fields if (f.isStatic() || f.isSynthetic() || f.hasFacet(Sys.TransientType)) { continue; } // get the value object val = f.get(obj); // if skipping defaults if (defObj != null) { object defVal = f.get(defObj); if (OpUtil.compareEQ(val, defVal)) { continue; } } // if first then open braces if (first) { w('\n').wIndent().w('{').w('\n'); level++; first = false; } // field name = wIndent().w(f.name()).w('='); // field value curFieldType = f.type().toNonNullable(); writeObj(val); curFieldType = null; w('\n'); } // if collection if (ser.m_collection) { first = writeCollectionItems(type, obj, first); } // if we output fields, then close braces if (!first) { level--; wIndent().w('}'); } }
////////////////////////////////////////////////////////////////////////// // 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); }
public override object callList(List args) { int origReq = m_orig.@params().sz(); int haveSize = m_bound.sz() + args.sz(); Method m = m_orig.method(); if (m != null) { origReq = m.minParams(); if (haveSize > origReq) origReq = haveSize; } if (origReq <= m_bound.sz()) return m_orig.callList(m_bound); object[] temp = new object[haveSize]; m_bound.copyInto(temp, 0, m_bound.sz()); args.copyInto(temp, m_bound.sz(), temp.Length-m_bound.sz()); return m_orig.callList(new List(Sys.ObjType, temp)); }
private void doCopyTo(File to, object exclude, object overwrite) { // check exclude if (exclude is Regex) { if (((Regex)exclude).matches(m_uri.toStr())) { return; } } else if (exclude is Func) { if (((Func)exclude).call(this) == Boolean.True) { return; } } // check for overwrite if (to.exists()) { if (overwrite is Boolean) { if (overwrite == Boolean.False) { return; } } else if (overwrite is Func) { if (((Func)overwrite).call(this) == Boolean.False) { return; } } else { throw IOErr.make("No overwrite policy for `" + to + "`").val; } } // copy directory if (isDir()) { to.create(); List kids = list(); for (int i = 0; i < kids.sz(); ++i) { File kid = (File)kids.get(i); kid.doCopyTo(to.plusNameOf(kid), exclude, overwrite); } } // copy file contents else { OutStream @out = to.@out(); try { @in().pipe(@out); } finally { @out.close(); } } }