Exemplo n.º 1
0
    public override Frame InvokeMethod(Frame caller, string name,
                                       Variable[] pos, VarHash named)
    {
        if (name == "postcircumfix:<( )>")
        {
            int context = 1;
            if (named["context"] != null)
            {
                context = Context(named["context"]);
            }
            IntPtr[] args = MarshalPositionals(pos);
            IntPtr   ret  = Perl5Interpreter.SubCall(context, args, args.Length);


            caller.resultSlot = Perl5Interpreter.SVToVariable(ret);
            return(caller);
        }
        else
        {
            IntPtr[] args = MarshalPositionals(pos);
            IntPtr   ret  = MethodCall(name, args, args.Length);
            caller.resultSlot = Perl5Interpreter.SVToVariable(ret);
        }

        return(caller);
    }
Exemplo n.º 2
0
    void WriteHash(P6any obj)
    {
        int a = nextanchor++;

        anchors[obj] = a;
        VarHash entries =
            Kernel.UnboxAny <VarHash>(obj);

        o.Append('{');
        contUsed = true;
        o.AppendFormat("\"&\":\"A{0}\"", a);
        if (obj.mo != setting.HashMO)
        {
            if (obj.mo.nslots != 0)
            {
                throw new NieczaException("Cannot serialize subclasses of Hash  that add attributes");
            }
            o.Append(",\"!\":");
            WriteStr(true, "!perl6/" + obj.mo.name);
        }
        List <string> keys = new List <string>(entries.Keys);

        keys.Sort();
        foreach (string key in keys)
        {
            o.Append(',');

            // no object keys in hashes yet
            WriteStr(true, key);
            o.Append(':');
            WriteObj(entries[key].Fetch());
        }
        o.Append('}');
    }
Exemplo n.º 3
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);
        }
    }
Exemplo n.º 4
0
    public override Frame InvokeMethod(Frame caller, string name,
                Variable[] pos, VarHash named)
    {
        if (name == "postcircumfix:<( )>") {
                    int context = 1;
                    if (named != null && named["context"] != null) {
                        context = Context(named["context"]);
                    }
                    IntPtr[] args = MarshalPositionals(pos);
                    IntPtr ret = Perl5Interpreter.SubCall(context,args,args.Length);

                    caller.resultSlot = Perl5Interpreter.SVToVariable(ret);
                    return caller;
                } else {
                    IntPtr[] args = MarshalPositionals(pos);
                    IntPtr ret = MethodCall(name,args,args.Length);
                    caller.resultSlot = Perl5Interpreter.SVToVariable(ret);
                }

                return caller;
    }
Exemplo n.º 5
0
 public static Variable sysquery(int ix)
 {
     switch (ix) {
         case 0: return BoxLoS(Kernel.commandArgs);
         case 1: return Kernel.BoxAnyMO(programName ?? AppDomain.CurrentDomain.FriendlyName, Kernel.StrMO);
         case 2: return Kernel.BoxAnyMO(execName, Kernel.StrMO);
         case 3: return Kernel.BoxAnyMO(AppDomain.CurrentDomain.BaseDirectory, Kernel.StrMO);
         case 4: {
             VarHash ret = new VarHash();
             foreach (System.Collections.DictionaryEntry de in Environment.GetEnvironmentVariables()) {
                 ret[(string) de.Key] = Kernel.BoxAnyMO((string)de.Value, Kernel.StrMO);
             }
             return Kernel.BoxAnyMO(ret, Kernel.HashMO);
         }
         default: return null;
     }
 }
Exemplo n.º 6
0
        public object ObjRef()
        {
            var tag = (SerializationCode)Byte();

            if (Config.SerTrace)
            {
                Console.WriteLine("Reading {0} from {1}...", tag, rpointer - 1);
            }
            int i, j;

            switch (tag)
            {
            case SerializationCode.Null:
                return(null);

            case SerializationCode.ForeignRef:
                i = Int();
                j = Int();
                return(unit_map[i].bynum[j]);

            case SerializationCode.SelfRef:
                i = Int();
                return(unit.bynum[i]);

            case SerializationCode.NewUnitRef:
                return(LoadNewUnit());

            case SerializationCode.RuntimeUnit:
                return(RuntimeUnit.Thaw(this));

            case SerializationCode.SubInfo:
                return(SubInfo.Thaw(this));

            case SerializationCode.STable:
                return(STable.Thaw(this));

            case SerializationCode.StashEnt:
                return(StashEnt.Thaw(this));

            case SerializationCode.Rat:
                return(Rat.Thaw(this));

            case SerializationCode.FatRat:
                return(FatRat.Thaw(this));

            case SerializationCode.Complex:
                return(Complex.Thaw(this));

            case SerializationCode.BigInteger:
                return(BigInteger.Thaw(this));

            case SerializationCode.VarDeque:
                return(VarDeque.Thaw(this));

            case SerializationCode.VarHash:
                return(VarHash.Thaw(this));

            case SerializationCode.DispatchEnt:
                return(DispatchEnt.Thaw(this));

            //case SerializationCode.RxFrame:
            //    return RxFrame.Thaw(this);
            case SerializationCode.P6how:
                return(P6how.Thaw(this));

            case SerializationCode.CC:
                return(CC.Thaw(this));

            case SerializationCode.AltInfo:
                return(AltInfo.Thaw(this));

            case SerializationCode.Signature:
                return(Signature.Thaw(this));

            case SerializationCode.Parameter:
                return(Parameter.Thaw(this));

            case SerializationCode.ReflectObj:
                return(ReflectObj.Thaw(this));

            case SerializationCode.P6opaque:
                return(P6opaque.Thaw(this));

            case SerializationCode.Frame:
                return(Frame.Thaw(this));

            //Cursor,

            case SerializationCode.String:
                return(Register(String()));

            case SerializationCode.ArrP6any:
                return(RefsARegister <P6any>());

            case SerializationCode.ArrVariable:
                return(RefsARegister <Variable>());

            case SerializationCode.ArrString:
                return(RefsARegister <string>());

            case SerializationCode.ArrCC:
                return(RefsARegister <CC>());

            case SerializationCode.Boolean:
                return(Register(Byte() != 0));

            case SerializationCode.Int:
                return(Register(Int()));

            case SerializationCode.Double:
                return(Register(Double()));

            case SerializationCode.Type:
                return(Register(Type.GetType(String(), true)));

            case SerializationCode.SimpleVariable:
            case SerializationCode.SimpleVariable_1:
            case SerializationCode.SimpleVariable_2:
            case SerializationCode.SimpleVariable_3:
                return(SimpleVariable.Thaw(this,
                                           (int)tag - (int)SerializationCode.SimpleVariable));

            case SerializationCode.SubstrLValue:
                return(SubstrLValue.Thaw(this));

            case SerializationCode.TiedVariable:
                return(TiedVariable.Thaw(this));

            case SerializationCode.SubViviHook:
                return(SubViviHook.Thaw(this));

            case SerializationCode.ArrayViviHook:
                return(ArrayViviHook.Thaw(this));

            case SerializationCode.NewArrayViviHook:
                return(NewArrayViviHook.Thaw(this));

            case SerializationCode.HashViviHook:
                return(HashViviHook.Thaw(this));

            case SerializationCode.NewHashViviHook:
                return(NewHashViviHook.Thaw(this));

            case SerializationCode.LADNone:
                return(Register(new LADNone()));

            case SerializationCode.LADNull:
                return(Register(new LADNull()));

            case SerializationCode.LADDot:
                return(Register(new LADDot()));

            case SerializationCode.LADDispatcher:
                return(Register(new LADDispatcher()));

            case SerializationCode.LADImp:
                return(Register(new LADImp()));

            case SerializationCode.LADStr:
                return(LADStr.Thaw(this));

            case SerializationCode.LADStrNoCase:
                return(LADStrNoCase.Thaw(this));

            case SerializationCode.LADMethod:
                return(LADMethod.Thaw(this));

            case SerializationCode.LADParam:
                return(LADParam.Thaw(this));

            case SerializationCode.LADQuant:
                return(LADQuant.Thaw(this));

            case SerializationCode.LADSequence:
                return(LADSequence.Thaw(this));

            case SerializationCode.LADAny:
                return(LADAny.Thaw(this));

            case SerializationCode.LADCC:
                return(LADCC.Thaw(this));

            default:
                throw new ThawException("unexpected object tag " + tag);
            }
        }
Exemplo n.º 7
0
    public void UnpackCaps(P6any into)
    {
        List<VarDeque> posr = new List<VarDeque>();
        Dictionary<string,VarDeque> namr = new Dictionary<string,VarDeque>();
        CapInfo it = captures;

        for (; it != null; it = it.prev) {
            if (it.names[0] == null)
                continue; // special node
            foreach (string name in it.names) {
                int nami;
                VarDeque t;
                if (int.TryParse(name, out nami) && nami >= 0) {
                    while(posr.Count <= nami) posr.Add(new VarDeque());
                    t = posr[nami];
                } else {
                    if (!namr.TryGetValue(name, out t))
                        namr[name] = t = new VarDeque();
                }
                t.Unshift(it.cap);
            }
        }

        VarHash nam = new VarHash();
        Variable[] pos = new Variable[posr.Count];

        foreach (KeyValuePair<string, VarDeque> kv in namr)
            nam[kv.Key] = FixupList(kv.Value);
        for (int i = 0; i < pos.Length; i++)
            pos[i] = FixupList(posr[i]);

        into.SetSlot(Kernel.CaptureMO, "$!positionals", pos);
        into.SetSlot(Kernel.CaptureMO, "$!named", nam);
    }
Exemplo n.º 8
0
    public Variable O(VarHash caps)
    {
        Cursor nw = At(pos);
        foreach (KeyValuePair<string,Variable> kv in caps)
            nw.captures = new CapInfo(nw.captures, new string[] { kv.Key },
                    Kernel.NewMuScalar(kv.Value.Fetch()));

        return Kernel.NewROScalar(nw);
    }
Exemplo n.º 9
0
 public static Variable sysquery(Constants c, int ix)
 {
     var s = c.setting;
     switch (ix) {
         case 0: return s.BoxLoS(Kernel.commandArgs);
         case 1: return s.MakeStr(programName ?? AppDomain.CurrentDomain.FriendlyName);
         case 2: return s.MakeStr(execName);
         case 3: return s.MakeStr(AppDomain.CurrentDomain.BaseDirectory);
         case 4: {
             VarHash ret = new VarHash();
             foreach (System.Collections.DictionaryEntry de in Environment.GetEnvironmentVariables()) {
                 ret[(string) de.Key] = Kernel.BoxAnyMO((string)de.Value, s.StrMO);
             }
             return Kernel.BoxAnyMO(ret, s.HashMO);
         }
         case 5: return s.MakeStr(Environment.OSVersion.Platform.ToString());
         case 6: return s.MakeStr(Environment.OSVersion.Version.ToString());
         case 7: return s.MakeStr(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString());
         default: return null;
     }
 }
Exemplo n.º 10
0
    public static P6any repr_clone(P6any obj, VarHash mods)
    {
        obj = obj.ReprClone();
        if (!mods.IsNonEmpty) return obj;

        Variable arg;
        foreach (STable m in obj.mo.mo.mro) {
            foreach (P6how.AttrInfo ai in m.mo.local_attr) {
                if ((ai.flags & P6how.A_PUBLIC) == 0) continue;
                if (!mods.TryGetValue(ai.name.Substring(2), out arg)) continue;

                EstablishSlot(obj, ai, arg);
            }
        }

        return obj;
    }
Exemplo n.º 11
0
 public static Variable InvokeSub(P6any obj, Variable[] pos, VarHash nam)
 {
     return Kernel.RunInferior(obj.Invoke(Kernel.GetInferiorRoot(obj.mo.setting), pos, nam));
 }
Exemplo n.º 12
0
 public static Variable InvokeMethod(string name, Variable[] pos, VarHash nam)
 {
     var o = pos[0].Fetch();
     return Kernel.RunInferior(o.InvokeMethod(
         Kernel.GetInferiorRoot(o.mo.setting), name, pos, nam));
 }
Exemplo n.º 13
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);
    }
Exemplo n.º 14
0
    internal Variable GetFromJson(bool top_level)
    {
        char look = SkipWhite(true);

        if (look == '[')
        {
            VarDeque q = new VarDeque();
            SkipChar('[');
            while (true)
            {
                look = SkipWhite(true);
                if (look == ']')
                {
                    break;
                }
                if (q.Count() != 0)
                {
                    SkipChar(',');
                }
                q.Push(GetFromJson(false));
            }
            SkipWhite(true);
            SkipChar(']');
            return(setting.MakeArray(q, new VarDeque()));
        }
        else if (look == '{')
        {
            VarHash q  = new VarHash();
            int     ct = 0;
            SkipChar('{');
            while (true)
            {
                look = SkipWhite(true);
                if (look == '}')
                {
                    break;
                }
                if (ct != 0)
                {
                    SkipCharWS(',');
                }
                ct++;
                string key = GetJsonString();
                SkipWhite(true);
                SkipChar(':');
                q[key] = GetFromJson(false);
            }
            SkipWhite(true);
            SkipChar('}');
            return(BoxRW <VarHash>(q, setting.HashMO));
        }
        else if (top_level)
        {
            Err("Top-level scalar found");
            return(null);
        }
        else if (look == '"')
        {
            return(BoxRW <string>(GetJsonString(), setting.StrMO));
        }
        else if (look == 'n')
        {
            SkipToken("null");
            return(Kernel.NewMuScalar(setting.AnyP));
        }
        else if (look == 't')
        {
            SkipToken("true");
            return(Kernel.NewMuScalar(setting.TrueV.Fetch()));
        }
        else if (look == 'f')
        {
            SkipToken("false");
            return(Kernel.NewMuScalar(setting.FalseV.Fetch()));
        }
        else
        {
            double d;
            string tx = GetJsonNumber();
            if (!Utils.S2NB(tx, out d))
            {
                Err("Unparsable number " + tx);
            }
            return(BoxRW <double>(d, setting.NumMO));
        }
    }
Exemplo n.º 15
0
    public Variable O(VarHash caps)
    {
        Cursor nw = At(pos);
        foreach (KeyValuePair<string,Variable> kv in caps)
            nw.captures = new CapInfo(nw.captures, new string[] { kv.Key },
                    Kernel.NewMuScalar(kv.Value.Fetch()));
        VarDeque ks = new VarDeque();

        P6opaque lst = new P6opaque(Kernel.ListMO);
        lst.slots[0 /*items*/] = ks;
        lst.slots[1 /*rest*/ ] = new VarDeque();

        ks.Push(Kernel.NewROScalar(nw));
        return Kernel.NewRWListVar(lst);
    }