void WriteVal(P6any obj) { if (!obj.IsDefined()) { o.Append("null"); } else if (obj.Isa(setting.BoolMO)) { o.Append(Kernel.UnboxAny <int>(obj) != 0 ? "true" : "false"); } else if (obj.Isa(setting.NumMO) || obj.Isa(setting.IntMO) || obj.Isa(setting.RatMO) || obj.Isa(setting.FatRatMO)) { o.Append(Utils.N2S(obj.mo.mro_raw_Numeric.Get(obj))); } else if (obj.Isa(setting.StrMO)) { o.Append('"'); JsyncWriter.AddStringContents(o, Kernel.UnboxAny <string>(obj)); o.Append('"'); } else { WriteObj(obj); } }
internal void WriteObj(P6any obj) { bool comma = false; bool def = obj.IsDefined(); if (def && obj.Isa(setting.HashMO)) { VarHash vh = Kernel.UnboxAny <VarHash>(obj); o.Append('{'); foreach (KeyValuePair <string, Variable> kv in vh) { if (comma) { o.Append(','); } comma = true; o.Append('"'); JsyncWriter.AddStringContents(o, kv.Key); o.Append('"'); o.Append(':'); WriteVal(kv.Value.Fetch()); } o.Append('}'); } else if (def && obj.Isa(setting.ListMO)) { VarDeque iter = obj.mo.mro_raw_iterator.Get(obj); o.Append('['); while (Kernel.IterHasFlat(iter, true)) { if (comma) { o.Append(','); } comma = true; WriteVal(iter.Shift().Fetch()); } o.Append(']'); } else if (JsyncWriter.FailSoft) { o.Append("\"*UNSERIALIZABLE*\""); } else { throw new NieczaException("JSON writer encountered value of type " + obj.mo.name); } }
// Better, but still fudgy. Relies too much on path structure. public static void cb_init_slave(Variable cb, P6any cmd_obj_dir, Variable unit, Variable staticSub, Variable type, Variable param, Variable value) { if (Downcaller.responder != null) { return; } Downcaller.UnitP = unit.Fetch(); Downcaller.StaticSubP = staticSub.Fetch(); Downcaller.TypeP = type.Fetch(); Downcaller.ParamP = param.Fetch(); Downcaller.ValueP = value.Fetch(); string[] obj_dirs = !cmd_obj_dir.IsDefined() ? new string[0] : cmd_obj_dir.Isa(cmd_obj_dir.mo.setting.StrMO) ? new string[] { cmd_obj_dir.mo.mro_raw_Str.Get(cmd_obj_dir) } : Builtins.UnboxLoS(Kernel.NewRWListVar(cmd_obj_dir)); if (obj_dirs.Length == 0) { obj_dirs = new string[2]; obj_dirs[0] = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("..", "obj")); obj_dirs[1] = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NieczaModuleCache"); } for (int i = 0; i < obj_dirs.Length; i++) { Directory.CreateDirectory(obj_dirs[i]); // mkdir -p obj_dirs[i] = Path.GetFullPath(obj_dirs[i]); } Downcaller.obj_dirs = obj_dirs; Downcaller.upcall_cb = cb; Downcaller.responder = (IDictionary) new Niecza.CLRBackend.DowncallReceiver(); }
internal void WriteObj(P6any obj) { int anchor; if (anchors.TryGetValue(obj, out anchor)) { WriteAnchor(anchor); } else if (!obj.IsDefined()) { WriteNull(); } else if (obj.Isa(setting.ListMO)) { WriteArray(obj); } else if (obj.Isa(setting.HashMO)) { WriteHash(obj); } else if (obj.Isa(setting.BoolMO)) { WriteBool(Kernel.UnboxAny <int>(obj) != 0); } else if (obj.Isa(setting.StrMO)) { WriteStr(true, Kernel.UnboxAny <string>(obj)); } else if (obj.Isa(setting.NumMO) || obj.Isa(setting.IntMO) || obj.Isa(setting.RatMO) || obj.Isa(setting.FatRatMO)) { WriteNum(obj.mo.mro_raw_Numeric.Get(obj)); } else { WriteGeneral(obj); } }
// Better, but still fudgy. Relies too much on path structure. public static void cb_init_slave(Variable cb, P6any cmd_obj_dir, Variable unit, Variable staticSub, Variable type, Variable param, Variable value) { if (Downcaller.responder != null) return; Downcaller.UnitP = unit.Fetch(); Downcaller.StaticSubP = staticSub.Fetch(); Downcaller.TypeP = type.Fetch(); Downcaller.ParamP = param.Fetch(); Downcaller.ValueP = value.Fetch(); string[] obj_dirs = !cmd_obj_dir.IsDefined() ? new string[0] : cmd_obj_dir.Isa(cmd_obj_dir.mo.setting.StrMO) ? new string[] { cmd_obj_dir.mo.mro_raw_Str.Get(cmd_obj_dir) } : Builtins.UnboxLoS(Kernel.NewRWListVar(cmd_obj_dir)) ; if (obj_dirs.Length == 0) { obj_dirs = new string[2]; obj_dirs[0] = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("..", "obj")); obj_dirs[1] = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NieczaModuleCache"); } for (int i = 0; i < obj_dirs.Length; i++) { Directory.CreateDirectory(obj_dirs[i]); // mkdir -p obj_dirs[i] = Path.GetFullPath(obj_dirs[i]); } Downcaller.obj_dirs = obj_dirs; Downcaller.upcall_cb = cb; Downcaller.responder = (IDictionary) new Niecza.CLRBackend.DowncallReceiver(); }
// If a number is <= NR_FLOAT, return it as if at NR_FLOAT public static double PromoteToFloat(int rank, P6any vret) { Rat r; FatRat fr; if (!vret.IsDefined()) return 0; switch (rank) { case NR_FIXINT: return Kernel.UnboxAny<int>(vret); case NR_BIGINT: return (double)Kernel.UnboxAny<BigInteger>(vret); case NR_FIXRAT: r = Kernel.UnboxAny<Rat>(vret); return (double)r.num / (double)r.den; case NR_FATRAT: fr = Kernel.UnboxAny<FatRat>(vret); return (double)fr.num / (double)fr.den; case NR_FLOAT: default: return Kernel.UnboxAny<double>(vret); } }
// If a number is <= NR_FIXRAT, return it as if at NR_FIXRAT public static Rat PromoteToFixRat(int rank, P6any vret) { if (!vret.IsDefined()) return new Rat(BigInteger.Zero, 1); switch (rank) { case NR_FIXINT: return new Rat(Kernel.UnboxAny<int>(vret), 1); case NR_BIGINT: return new Rat(Kernel.UnboxAny<BigInteger>(vret), 1); case NR_FIXRAT: default: return Kernel.UnboxAny<Rat>(vret); } }
// If a number is <= NR_FIXINT, return it as if at NR_FIXINT public static int PromoteToFixInt(int rank, P6any vret) { if (!vret.IsDefined()) return 0; return Kernel.UnboxAny<int>(vret); }
// If a number is <= NR_FATRAT, return it as if at NR_FATRAT public static FatRat PromoteToFatRat(int rank, P6any vret) { Rat r; if (!vret.IsDefined()) return new FatRat(BigInteger.Zero,BigInteger.One); switch (rank) { case NR_FIXINT: return new FatRat(Kernel.UnboxAny<int>(vret), BigInteger.One); case NR_BIGINT: return new FatRat(Kernel.UnboxAny<BigInteger>(vret), BigInteger.One); case NR_FIXRAT: r = Kernel.UnboxAny<Rat>(vret); return new FatRat(r.num, r.den); case NR_FATRAT: default: return Kernel.UnboxAny<FatRat>(vret); } }
// If a number is <= NR_COMPLEX, return it as if at NR_COMPLEX public static Complex PromoteToComplex(int rank, P6any vret) { Rat r; FatRat fr; if (!vret.IsDefined()) return new Complex(0,0); switch (rank) { case NR_FIXINT: return new Complex(Kernel.UnboxAny<int>(vret), 0); case NR_BIGINT: return new Complex((double)Kernel.UnboxAny<BigInteger>(vret), 0); case NR_FIXRAT: r = Kernel.UnboxAny<Rat>(vret); return new Complex((double)r.num / (double)r.den, 0); case NR_FATRAT: fr = Kernel.UnboxAny<FatRat>(vret); return new Complex((double)fr.num / (double)fr.den, 0); case NR_FLOAT: return new Complex(Kernel.UnboxAny<double>(vret), 0); case NR_COMPLEX: default: return Kernel.UnboxAny<Complex>(vret); } }
// If a number is <= NR_BIGINT, return it as if at NR_BIGINT public static BigInteger PromoteToBigInt(int rank, P6any vret) { if (!vret.IsDefined()) return BigInteger.Zero; switch (rank) { case NR_FIXINT: return Kernel.UnboxAny<int>(vret); case NR_BIGINT: default: return Kernel.UnboxAny<BigInteger>(vret); } }
public static Variable mixin(Constants c, P6any obj, Variable role_list, Variable init, Variable newtype) { VarDeque iter = start_iter(role_list); List<STable> roles = new List<STable>(); while (Kernel.IterHasFlat(iter, true)) roles.Add(iter.Shift().Fetch().mo); STable n = new STable(c.setting, obj.mo.name + "+" + Kernel.JoinS(",", roles)); n.how = Kernel.BoxAny<STable>(n, obj.mo.how).Fetch(); n.typeObj = n.initObj = new P6opaque(n); ((P6opaque)n.typeObj).slots = null; n.mo.superclasses.Add(obj.mo); n.mo.local_roles = roles; n.mo.Compose(); newtype.Store(n.typeObj); string aname = null; if (init != c.setting.AnyP) { if (!obj.IsDefined()) throw new NieczaException("Cannot initialize a slot when mixing into a type object"); if (n.mo.local_attr.Count != 1 || (n.mo.local_attr[0].flags & P6how.A_PUBLIC) == 0) throw new NieczaException("Role(s) being mixed in do not define precisely one, public attribute"); aname = n.mo.local_attr[0].name; } if (obj.IsDefined()) { obj.ChangeType(n); BuildMostDerived(obj); if (aname != null) Kernel.Assign((Variable)obj.GetSlot(n, aname), init); return obj; } else { return n.typeObj; } }
// Better, but still fudgy. Relies too much on path structure. public static void InitSlave(Variable cb, P6any cmd_obj_dir, Variable unit, Variable staticSub, Variable type, Variable param, Variable value) { if (responder != null) return; UnitP = unit.Fetch(); StaticSubP = staticSub.Fetch(); TypeP = type.Fetch(); ParamP = param.Fetch(); ValueP = value.Fetch(); obj_dir = Path.GetFullPath(cmd_obj_dir.IsDefined() ? cmd_obj_dir.mo.mro_raw_Str.Get(cmd_obj_dir) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NieczaModuleCache")); Directory.CreateDirectory(obj_dir); // like mkdir -p if (!File.Exists(Path.Combine(obj_dir, "Run.Kernel.dll"))) { File.Copy( Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Run.Kernel.dll"), Path.Combine(obj_dir, "Run.Kernel.dll") ); } AppDomain.CurrentDomain.AssemblyResolve += ObjLoader; upcall_cb = cb; responder = (IDictionary) Activator.CreateInstance(Type.GetType( "Niecza.CLRBackend.DowncallReceiver,Run.Kernel", true)); RawDowncall("set_binding", obj_dir, new UpcallReceiver()); }