Esempio n. 1
0
 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);
     }
 }
Esempio n. 2
0
    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);
        }
    }
Esempio n. 3
0
    // 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();
    }
Esempio n. 4
0
    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();
    }
Esempio n. 6
0
 public static int get_count(P6any fcni)
 {
     if (!fcni.Isa(Kernel.CodeMO))
         return 1; // can't introspect fake subs (?)
     return get_count((SubInfo) fcni.GetSlot("info"));
 }
Esempio n. 7
0
 public static Variable arity(P6any fcni)
 {
     if (!fcni.Isa(Kernel.CodeMO))
         return MakeInt(1); // can't introspect fake subs (?)
     SubInfo si = (SubInfo) fcni.GetSlot("info");
     if (si.sig == null)
         return MakeInt(1);
     int arity = 0;
     foreach (Parameter p in si.sig.parms) {
         int fl = p.flags;
         if ((fl & (Parameter.SLURPY_CAP | Parameter.SLURPY_POS |
                 Parameter.SLURPY_PCL | Parameter.SLURPY_NAM |
                 Parameter.OPTIONAL | Parameter.DEFOUTER |
                 Parameter.HASDEFAULT)) != 0)
             continue;
         if ((fl & Parameter.POSITIONAL) == 0) continue;
         arity++;
     }
     return MakeInt(arity);
 }
Esempio n. 8
0
 public static Variable arity(Constants c, P6any fcni)
 {
     if (!fcni.Isa(c.setting.CodeMO))
         return c.setting.MakeInt(1); // can't introspect fake subs (?)
     SubInfo si = (SubInfo) Kernel.GetInfo(fcni);
     if (si.sig == null)
         return c.setting.MakeInt(1);
     return c.setting.MakeInt(sig_arity(si.sig));
 }
Esempio n. 9
0
 public static int get_count(P6any fcni)
 {
     if (!fcni.Isa(fcni.mo.setting.CodeMO))
         return 1; // can't introspect fake subs (?)
     return sig_count(Kernel.GetInfo(fcni).sig);
 }
Esempio n. 10
0
    Variable GetFromHash()
    {
        SkipCharWS('{');
        // we can't make any assumptions about ordering here, as JSON
        // emitters can blindly reorder hashes
        string h_tag    = null;
        string h_anchor = null;
        Dictionary <string, string> h_key_ind = null;
        VarHash h_val_ind = null;
        VarHash zyg       = new VarHash();
        bool    comma     = false;

        while (true)
        {
            if (SkipWhite(true) == '}')
            {
                break;
            }
            if (comma)
            {
                SkipChar(',');
            }
            comma = true;
            SkipWhite(false);
            GetString();
            if (s_content_type == NONE && s_anchor == null && s_tag == "")
            {
                if (h_tag != null)
                {
                    Err("Tag specified twice");
                }
                h_tag = GetSimpleStringValue();
            }
            else if (s_content_type == NONE && s_tag == null && s_anchor == "")
            {
                if (h_anchor != null)
                {
                    Err("Anchor specified twice");
                }
                h_anchor = GetSimpleStringValue();
            }
            else if (s_content_type == NONE)
            {
                if (s_anchor == null || s_tag != null)
                {
                    Err("Invalid hash key form");
                }
                string k1 = s_anchor;
                if (h_key_ind == null)
                {
                    h_key_ind = new Dictionary <string, string>();
                }
                if (h_key_ind.ContainsKey(k1))
                {
                    Err("Key alias &" + k1 + " specified twice");
                }
                SkipCharWS(':');
                if (SkipWhite(true) != '"')
                {
                    Err("Non-scalar hash keys NYI in Niecza Perl 6");
                }
                GetString();
                if (s_tag != null || s_anchor != null || s_content_type != SCALAR)
                {
                    Err("Typed hash keys NYI in Niecza Perl 6");
                }
                h_key_ind[k1] = s_content;
            }
            else if (s_content_type == ALIAS)
            {
                string k1 = s_content;
                if (h_val_ind == null)
                {
                    h_val_ind = new VarHash();
                }
                if (h_val_ind.ContainsKey(k1))
                {
                    Err("Key alias *" + k1 + " used twice");
                }
                SkipCharWS(':');
                h_val_ind[k1] = GetObj();
            }
            else if (s_content_type == DIRECTIVE)
            {
                Err("Got directive in hash key position");
            }
            else
            {
                if (s_tag != null || s_anchor != null)
                {
                    Err("Typed hash keys NYI in Niecza Perl 6");
                }
                string k1 = s_content;
                SkipCharWS(':');
                zyg[k1] = GetObj();
            }
        }

        SkipChar('}');

        if (h_key_ind != null || h_val_ind != null)
        {
            h_key_ind = h_key_ind ?? new Dictionary <string, string>();
            h_val_ind = h_val_ind ?? new VarHash();

            foreach (KeyValuePair <string, string> kv in h_key_ind)
            {
                if (!h_val_ind.ContainsKey(kv.Key))
                {
                    Err("No value provided for indirect key *" + kv.Key);
                }
                Variable val = h_val_ind[kv.Key];
                h_val_ind.Remove(kv.Key);
                if (zyg.ContainsKey(kv.Value))
                {
                    Err("Indirect key &" + kv.Value + " collides with non-indirect key");
                }
                zyg[kv.Value] = val;
            }

            foreach (string k in h_val_ind.Keys)
            {
                Err("Indirect key &" + k + " is unused");
            }
        }

        Variable obj;

        if (h_tag != null)
        {
            if (!Utils.StartsWithInvariant("!perl6/", h_tag))
            {
                Err("Unsupported hash tag " + h_tag);
            }
            string s2  = "::GLOBAL::" + h_tag.Substring(7);
            int    cut = s2.LastIndexOf("::");

            Variable v_cursor = setting.GetVar(s2.Substring(0, cut),
                                               s2.Substring(cut + 2)).v;

            if (v_cursor.Rw)
            {
                Err(s2.Substring(2) + " does not name a loaded global class");
            }
            P6any p_cursor = v_cursor.Fetch();

            if (p_cursor.Isa(setting.HashMO))
            {
                if (p_cursor.mo.nslots != 0)
                {
                    Err("Cannot thaw Hash subclass " + p_cursor.mo.name + "; it has attributes");
                }
                obj = BoxRW <VarHash>(zyg, p_cursor.mo);
            }
            else
            {
                P6opaque dyo = new P6opaque(p_cursor.mo);
                for (int i = 0; i < dyo.mo.nslots; i++)
                {
                    string sn = dyo.mo.all_slot[i];
                    if (!zyg.ContainsKey(sn))
                    {
                        Err("No value for attribute " + sn + " in thawed value of class " + dyo.mo.name);
                    }
                    dyo.slots[i] = zyg[sn];
                    zyg.Remove(sn);
                }
                foreach (string key in zyg.Keys)
                {
                    Err("Attribute " + key + " not present in " + dyo.mo.name);
                }
                obj = Kernel.NewMuScalar(dyo);
            }
        }
        else
        {
            obj = BoxRW <VarHash>(zyg, setting.HashMO);
        }
        if (h_anchor != null)
        {
            AddAnchor(h_anchor, obj.Fetch());
        }
        return(obj);
    }
Esempio n. 11
0
 public static int get_count(P6any fcni)
 {
     if (!fcni.Isa(Kernel.CodeMO))
         return 1; // can't introspect fake subs (?)
     SubInfo si = (SubInfo) fcni.GetSlot("info");
     int[] sig = si.sig_i;
     if (sig == null)
         return 1;
     int arity = 0;
     for (int i = 0; i < sig.Length; i += SubInfo.SIG_I_RECORD) {
         int fl = sig[i + SubInfo.SIG_I_FLAGS];
         if ((fl & (SubInfo.SIG_F_SLURPY_CAP | SubInfo.SIG_F_SLURPY_POS |
                 SubInfo.SIG_F_SLURPY_PCL)) != 0)
             return int.MaxValue;
         if ((fl & SubInfo.SIG_F_POSITIONAL) == 0) continue;
         arity++;
     }
     return arity;
 }
Esempio n. 12
0
 public static Variable arity(P6any fcni)
 {
     if (!fcni.Isa(Kernel.CodeMO))
         return MakeInt(1); // can't introspect fake subs (?)
     SubInfo si = (SubInfo) fcni.GetSlot("info");
     int[] sig = si.sig_i;
     if (sig == null)
         return MakeInt(1);
     int arity = 0;
     for (int i = 0; i < sig.Length; i += SubInfo.SIG_I_RECORD) {
         int fl = sig[i + SubInfo.SIG_I_FLAGS];
         if ((fl & (SubInfo.SIG_F_SLURPY_CAP | SubInfo.SIG_F_SLURPY_POS |
                 SubInfo.SIG_F_SLURPY_PCL | SubInfo.SIG_F_SLURPY_NAM |
                 SubInfo.SIG_F_OPTIONAL | SubInfo.SIG_F_DEFOUTER |
                 SubInfo.SIG_F_HASDEFAULT)) != 0)
             continue;
         if ((fl & SubInfo.SIG_F_POSITIONAL) == 0) continue;
         arity++;
     }
     return MakeInt(arity);
 }