public virtual object make(List args) { Method make = method("make", false); if (make != null && make.isPublic()) { int numArgs = args == null ? 0 : args.sz(); List p = make.@params(); if ((numArgs == p.sz()) || (numArgs < p.sz() && ((Param)p.get(numArgs)).hasDefault())) { return(make.m_func.callList(args)); } } Slot defVal = slot("defVal", false); if (defVal is Field) { return(((Field)defVal).get(null)); } if (defVal is Method) { return(((Method)defVal).m_func.callList(null)); } throw Err.make("Type missing 'make' or 'defVal' slots: " + this).val; }
public override void finish() { if (m_finished) { return; } try { // ensure reflected and emitted reflect(); emit(); m_finished = true; // map .NET members to my slots for reflection; if // mixin then we do this for both the interface and // the static methods only of the implementation class finishSlots(m_type, false); if (isMixin()) { finishSlots(m_auxType, true); } } catch (Exception e) { Err.dumpStack(e); throw Err.make("Cannot emitFinish: " + m_qname + "." + m_finishing, e).val; } finally { m_finishing = null; } }
public Unit findDiv(Unit a, Unit b) { // compute dim/scale of a / b Dimension dim = a.m_dim.subtract(b.m_dim).intern(); double scale = a.m_scale / b.m_scale; // find all the matches Unit[] matches = match(dim, scale); if (matches.Length == 1) { return(matches[0]); } // right how our technique for resolving multiple matches is lame string expectedName = a.name() + "_per_" + b.name(); for (int i = 0; i < matches.Length; ++i) { if (matches[i].name().Contains(expectedName)) { return(matches[i]); } } // for now just give up throw Err.make("Cannot match to db: " + a + " / " + b).val; }
public static Pod load(InStream @in) { FPod fpod = null; try { fpod = new FPod(null, null); fpod.readFully(new ZipInputStream(SysInStream.dotnet(@in))); } catch (Exception e) { throw Err.make(e).val; } string name = fpod.m_podName; lock (m_podsByName) { // check for duplicate pod name if (m_podsByName[name] != null) { throw Err.make("Duplicate pod name: " + name).val; } // create Pod and add to master table Pod pod = new Pod(fpod); m_podsByName[name] = pod; //new SoftReference(pod); return(pod); } }
private void checkRun() { if (m_proc != null) { throw Err.make("Process already run").val; } }
////////////////////////////////////////////////////////////////////////// // Implementation ////////////////////////////////////////////////////////////////////////// private Future _send(object msg, Duration dur, Future whenDone) { // ensure immutable or safe copy msg = Sys.safe(msg); // don't deliver new messages to a stopped pool if (m_pool.isStopped()) { throw Err.make("ActorPool is stopped").val; } // get the future instance to manage this message's lifecycle Future f = new Future(msg); // either enqueue immediately or schedule with pool if (dur != null) { m_pool.schedule(this, dur, f); } else if (whenDone != null) { whenDone.sendWhenDone(this, f); } else { f = _enqueue(f, true); } return(f); }
private static Unit findMult(Unit a, Unit b) { // compute dim/scale of a * b Dimension dim = a.m_dim.add(b.m_dim).intern(); double scale = a.m_scale * b.m_scale; // find all the matches Unit[] matches = match(dim, scale); if (matches.Length == 1) { return(matches[0]); } // right how our technique for resolving multiple matches is lame string expectedName = a.name() + "_" + b.name(); for (int i = 0; i < matches.Length; ++i) { if (matches[i].name() == expectedName) { return(matches[i]); } } // for now just give up throw Err.make("Cannot match to db: " + a + " * " + b).val; }
////////////////////////////////////////////////////////////////////////// // Conversion ////////////////////////////////////////////////////////////////////////// public double convertTo(double scalar, Unit to) { if (m_dim != to.m_dim) { throw Err.make("Incovertable units: " + this + " and " + to).val; } return(((scalar * this.m_scale + this.m_offset) - to.m_offset) / to.m_scale); }
////////////////////////////////////////////////////////////////////////// // Lifecycle ////////////////////////////////////////////////////////////////////////// public Method curTestMethod() { if (m_curTestMethod == null) { throw Err.make("No test currently executing for " + @typeof()).val; } return(m_curTestMethod); }
public Process kill() { if (m_proc == null) { throw Err.make("Process not running").val; } m_proc.Kill(); return(this); }
public virtual void set(object instance, object value, bool checkConst) { m_parent.finish(); // check const if ((m_flags & FConst.Const) != 0) { if (checkConst) { throw ReadonlyErr.make("Cannot set const field " + qname()).val; } else if (value != null && !isImmutable(value)) { throw ReadonlyErr.make("Cannot set const field " + qname() + " with mutable value").val; } } // check static if ((m_flags & FConst.Static) != 0) { throw ReadonlyErr.make("Cannot set static field " + qname()).val; } // check generic type (the .NET runtime will check non-generics) if (m_type.isGenericInstance() && value != null) { if (!@typeof(value).@is(m_type.toNonNullable())) { throw ArgErr.make("Wrong type for field " + qname() + ": " + m_type + " != " + @typeof(value)).val; } } if (m_setter != null) { m_setter.invoke(instance, new object[] { value }); return; } try { m_reflect.SetValue(instance, unbox(value)); } catch (ArgumentException e) { throw ArgErr.make(e).val; } catch (Exception e) { if (m_reflect == null) { throw Err.make("Field not mapped to System.Reflection correctly").val; } throw Err.make(e).val; } }
public static List quantity(string quantity) { List list = (List)m_quantities[quantity]; if (list == null) { throw Err.make("Unknown unit database quantity: " + quantity).val; } return(list); }
public static Uuid make() { try { return(m_factory.make()); } catch (Exception e) { throw Err.make(e).val; } }
public static Unit fromStr(string name, bool check) { lock (m_byId) { Unit unit = (Unit)m_byId[name]; if (unit != null || !check) { return(unit); } throw Err.make("Unit not found: " + name).val; } }
public System.Type emit() { if (m_type == null) { if (Debug) { Console.WriteLine("-- emit: " + m_qname); } // make sure we have reflected to setup slots reflect(); // if sys class, just load it by name string podName = m_pod.m_name; if (podName == "sys") { try { m_dotnetRepr = FanUtil.isDotnetRepresentation(this); m_type = System.Type.GetType(FanUtil.toDotnetImplTypeName(podName, m_name)); } catch (Exception e) { Err.dumpStack(e); throw Err.make("Cannot load precompiled class: " + m_qname, e).val; } } // otherwise we need to emit it else { try { System.Type[] types = FTypeEmit.emitAndLoad(m_ftype); this.m_type = types[0]; if (types.Length > 1) { this.m_auxType = types[1]; } } catch (Exception e) { Err.dumpStack(e); throw Err.make("Cannot emit: " + m_qname, e).val; } } // we are done with our ftype now, gc it this.m_ftype = null; } return(m_type); }
////////////////////////////////////////////////////////////////////////// // Conversion ////////////////////////////////////////////////////////////////////////// public static string toChar(long self) { long val = self; if (val < 0 || val > 0xFFFF) { throw Err.make("Invalid unicode char: " + val).val; } if (val < FanStr.m_ascii.Length) { return(FanStr.m_ascii[(int)val]); } return("" + (char)val); }
////////////////////////////////////////////////////////////////////////// // Load ////////////////////////////////////////////////////////////////////////// internal void load(FPod fpod) { this.fpod = fpod; this.typesByName = new Hashtable(); // create a hollow Type for each FType (this requires two steps, // because we don't necessary have all the Types created for // superclasses until this loop completes) m_types = new ClassType[fpod.m_types.Length]; for (int i = 0; i < fpod.m_types.Length; i++) { // create type instance ClassType type = new ClassType(this, fpod.m_types[i]); // add to my data structures m_types[i] = type; if (typesByName[type.m_name] != null) { throw Err.make("Invalid pod: " + m_name + " type already defined: " + type.m_name).val; } typesByName[type.m_name] = type; } // get TypeType to use for mixin List (we need to handle case // when loading sys itself - and lookup within my own pod) Type typeType = Sys.TypeType; if (typeType == null) { typeType = (Type)typesByName["Type"]; } // now that everthing is mapped, we can fill in the super // class fields (unless something is wacked, this will only // use Types in my pod or in pods already loaded) for (int i = 0; i < fpod.m_types.Length; i++) { FType ftype = fpod.m_types[i]; ClassType type = m_types[i]; type.m_base = findType(ftype.m_base); List mixins = new List(typeType, ftype.m_mixins.Length); for (int j = 0; j < ftype.m_mixins.Length; j++) { mixins.add(findType(ftype.m_mixins[j])); } type.m_mixins = mixins.ro(); } }
private bool _isStatic() { try { // ensure parent has finished emitting so that reflect is populated m.m_parent.finish(); // return if .NET method(s) is static return(m.m_reflect[0].IsStatic); } catch (Exception) { throw Err.make("Method not mapped to System.Reflection correctly " + m.qname()).val; } }
public long join() { if (m_proc == null) { throw Err.make("Process not running").val; } try { m_proc.WaitForExit(); return(m_proc.ExitCode); } catch (System.Exception e) { throw Err.make(e).val; } }
public static Log find(string name, bool check) { lock (lockObj) { Log log = (Log)byName[name]; if (log != null) { return(log); } if (check) { throw Err.make("Unknown log: " + name).val; } return(null); } }
private void grow(int desiredSize) { int desired = (int)desiredSize; if (desired < 1) { throw Err.make("desired " + desired + " < 1").val; } int newSize = Math.Max(desired, m_size * 2); if (newSize < 10) { newSize = 10; } object[] temp = new object[newSize]; Array.Copy(m_values, temp, m_size); m_values = temp; }
public ActorPool join(Duration timeout) { if (!isStopped()) { throw Err.make("ActorPool is not stopped").val; } long ms = timeout == null ? System.Int32.MaxValue : timeout.millis(); try { if (m_threadPool.join(ms)) { return(this); } } catch (System.Threading.ThreadInterruptedException e) { throw InterruptedErr.make(e).val; } throw TimeoutErr.make("ActorPool.join timed out").val; }
public static Slot find(string qname, bool check) { string typeName, slotName; try { int dot = qname.IndexOf('.'); typeName = qname.Substring(0, dot); slotName = qname.Substring(dot + 1); } catch (Exception) { throw Err.make("Invalid slot qname \"" + qname + "\", use <pod>::<type>.<slot>").val; } Type type = Type.find(typeName, check); if (type == null) { return(null); } return(type.slot(slotName, check)); }
internal void _dispatch(Future future) { try { if (future.isCancelled()) { return; } if (m_pool.m_killed) { future.cancel(); return; } future.set(receive(future.m_msg)); } catch (Err.Val e) { future.err(e.m_err); } catch (System.Exception e) { future.err(Err.make(e)); } }
public virtual object get(object instance) { m_parent.finish(); if (m_getter != null) { return(m_getter.invoke(instance, Method.noArgs)); } try { return(FanUtil.box(m_reflect.GetValue(instance))); } catch (Exception e) { if (m_reflect == null) { throw Err.make("Field not mapped to System.Reflection.FieldInfo correctly").val; } throw Err.make(e).val; } }
////////////////////////////////////////////////////////////////////////// // Parsing ////////////////////////////////////////////////////////////////////////// public static Unit define(string str) { // parse Unit unit = null; try { unit = parseUnit(str); } catch (Exception) { throw ParseErr.make("Unit", str).val; } // register lock (m_byId) { // lookup by its ids for (int i = 0; i < unit.m_ids.sz(); ++i) { string id = (string)unit.m_ids.get(i); if (m_byId[id] != null) { throw Err.make("Unit id is already defined: " + id).val; } } // this is a new definition for (int i = 0; i < unit.m_ids.sz(); ++i) { m_byId[(string)unit.m_ids.get(i)] = unit; } m_list.add(unit); } return(unit); }
////////////////////////////////////////////////////////////////////////// // Lifecycle ////////////////////////////////////////////////////////////////////////// public Process run() { checkRun(); try { // arguments string fileName = m_command.get(0) as string; StringBuilder args = new StringBuilder(); for (int i = 1; i < m_command.sz(); i++) { if (i > 1) { args.Append(" "); } args.Append(m_command.get(i) as string); } // create process m_proc = new System.Diagnostics.Process(); m_proc.StartInfo.UseShellExecute = false; m_proc.StartInfo.FileName = fileName; m_proc.StartInfo.Arguments = args.ToString(); // environment if (m_env != null) { IDictionaryEnumerator en = m_env.pairsIterator(); while (en.MoveNext()) { string key = (string)en.Key; string val = (string)en.Value; m_proc.StartInfo.EnvironmentVariables[key] = val; } } // working directory if (m_dir != null) { m_proc.StartInfo.WorkingDirectory = ((LocalFile)m_dir).m_file.FullName; } // streams if (m_in != null) { m_proc.StartInfo.RedirectStandardInput = true; } m_proc.StartInfo.RedirectStandardOutput = true; m_proc.StartInfo.RedirectStandardError = true; m_proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(outHandler); m_proc.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(errHandler); // start it m_proc.Start(); // start async read/writes if (m_in != null) { new System.Threading.Thread( new System.Threading.ThreadStart(inHandler)).Start(); } m_proc.BeginOutputReadLine(); m_proc.BeginErrorReadLine(); return(this); } catch (System.Exception e) { m_proc = null; throw Err.make(e).val; } }
////////////////////////////////////////////////////////////////////////// // C# Convenience ////////////////////////////////////////////////////////////////////////// public new static IOErr make(string msg, Exception cause) { return(make(msg, Err.make(cause))); }
////////////////////////////////////////////////////////////////////////// // Construction ////////////////////////////////////////////////////////////////////////// public static UriScheme make() { throw Err.make("UriScheme is abstract").val; }
internal object invoke(object instance, object[] args) { if (m_reflect == null) { m_parent.finish(); } try { // zero index is full signature up to using max defaults int index = m_params.sz() - args.Length; if (m_parent.dotnetRepr() && isInstance()) { index++; } if (index < 0) { index = 0; } MethodInfo m = m_reflect[index]; //System.Console.WriteLine(">>> " + m_reflect.Length + "/" + index); //System.Console.WriteLine(m_reflect[index]); //System.Console.WriteLine("---"); //for (int i=0; i<m_reflect.Length; i++) // System.Console.WriteLine(m_reflect[i]); // TODO - not sure how this should work entirely yet, but we need // to be responsible for "boxing" Fantom wrappers and primitives // box the parameters ParameterInfo[] pars = m.GetParameters(); for (int i = 0; i < args.Length; i++) { System.Type pt = pars[i].ParameterType; if (pt == boolPrimitive && args[i] is Fan.Sys.Boolean) { args[i] = (args[i] as Fan.Sys.Boolean).booleanValue(); } else if (pt == doublePrimitive && args[i] is Fan.Sys.Double) { args[i] = (args[i] as Fan.Sys.Double).doubleValue(); } else if (pt == longPrimitive && args[i] is Fan.Sys.Long) { args[i] = (args[i] as Fan.Sys.Long).longValue(); } } // invoke method object ret = m.Invoke(instance, args); // box the return value return(FanUtil.box(ret)); } catch (ArgumentException e) { throw ArgErr.make(e).val; } catch (TargetInvocationException e) { Err err = Err.make(e.InnerException); err.m_stack = e.InnerException.StackTrace; throw err.val; } catch (Exception e) { if (m_reflect == null) { throw Err.make("Method not mapped to System.Reflection.MethodInfo correctly " + m_qname).val; } /* * System.Console.WriteLine("ERROR: " + signature()); * System.Console.WriteLine(" instance: " + instance); * System.Console.WriteLine(" args: " + (args == null ? "null" : ""+args.Length)); * for (int i=0; args != null && i<args.Length; ++i) * System.Console.WriteLine(" args[" + i + "] = " + args[i]); * Err.dumpStack(e); */ throw Err.make("Cannot call '" + this + "': " + e).val; } }