Exemplo n.º 1
0
        /// <summary>
        /// static void r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="has_encoding"></param>
        public void ReadInstanceVariable(object obj, ref bool has_encoding)
        {
            int        len  = ReadLong();
            RubyObject fobj = obj as RubyObject;

            if (len > 0)
            {
                do
                {
                    RubySymbol id  = ReadSymbol();
                    object     val = ReadObject();
                    Encoding   idx = GetEncoding(id, val);
                    if (idx != null)
                    {
                        if (fobj != null)
                        {
                            fobj.Encoding = idx;
                        }
                        has_encoding = true;
                    }
                    else
                    {
                        if (fobj != null)
                        {
                            fobj.InstanceVariable[id] = val;
                        }
                    }
                } while (--len > 0);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// static VALUE append_extmod(VALUE obj, VALUE extmod)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="extmod"></param>
        /// <returns></returns>
        public object AppendExtendedModule(object obj, List <RubyModule> extmod)
        {
            RubyObject fobj = obj as RubyObject;

            if (fobj != null)
            {
                fobj.ExtendModules.AddRange(extmod);
            }
            return(obj);
        }
Exemplo n.º 3
0
        /// <summary>
        /// static void w_extended(VALUE klass, struct dump_arg *arg, int check)
        /// </summary>
        /// <param name="klass"></param>
        /// <param name="check"></param>
        public void WriteExtended(object klass, bool check)
        {
            RubyObject fobj = klass as RubyObject;

            if (fobj != null)
            {
                foreach (RubyModule item in fobj.ExtendModules)
                {
                    WriteByte(RubyMarshal.Types.Extended);
                    WriteUnique(item.Symbol);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// static void w_class(char type, VALUE obj, struct dump_arg *arg, int check)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        /// <param name="check"></param>
        public void WriteClass(byte type, object obj, bool check)
        {
            object real_obj;

            if (this.m_compat_tbl.TryGetValue(obj, out real_obj))
            {
                obj = real_obj;
            }
            RubyObject fobj = obj as RubyObject;

            if (fobj != null)
            {
                RubyClass klass = RubyClass.GetClass(fobj.ClassName);
                WriteExtended(klass, check);
                WriteByte(type);
                WriteUnique(fobj.ClassName);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// static void w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="super"></param>
        public void WriteUserClass(object obj, RubyClass super)
        {
            RubyObject fobj = obj as RubyObject;

            if (fobj != null)
            {
                RubyClass klass = fobj.Class;
                WriteExtended(klass, true);
                if (klass != super)
                {
                    WriteByte(RubyMarshal.Types.UserClass);
                    WriteUnique(klass.Symbol);
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// static void w_ivar(VALUE obj, st_table *tbl, struct dump_call_arg *arg)
        /// </summary>
        /// <param name="obj"></param>
        public void WriteInstanceVariable(RubyObject obj, Dictionary <RubySymbol, object> tbl)
        {
            int num = tbl != null ? tbl.Count : 0;

            WriteEncoding(obj, num);
            if (tbl != null)
            {
                foreach (KeyValuePair <RubySymbol, object> item in tbl)
                {
                    if (item.Key == RubyMarshal.IDs.encoding)
                    {
                        continue;
                    }
                    if (item.Key == RubyMarshal.IDs.E)
                    {
                        continue;
                    }
                    WriteSymbol(item.Key);
                    WriteObject(item.Value);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// static VALUE r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
        /// </summary>
        /// <param name="hasivp"></param>
        /// <param name="ivp"></param>
        /// <param name="extmod"></param>
        /// <returns></returns>
        public object ReadObject0(bool hasivp, ref bool ivp, List <RubyModule> extmod)
        {
            object v    = null;
            int    type = ReadByte();
            int    id;
            object link;

            switch (type)
            {
            case RubyMarshal.Types.Link:
                id = ReadLong();
                if (!this.m_objects.TryGetValue(id, out link))
                {
                    throw new InvalidDataException("dump format error (unlinked)");
                }
                v = link;
                if (this.m_proc != null)
                {
                    v = this.m_proc(v);
                }
                break;

            case RubyMarshal.Types.InstanceVariable:
            {
                bool ivar = true;
                v = ReadObject0(ref ivar, extmod);
                bool hasenc = false;
                if (ivar)
                {
                    ReadInstanceVariable(v, ref hasenc);
                }
            }
            break;

            case RubyMarshal.Types.Extended:
            {
                RubyModule m = RubyModule.GetModule(ReadUnique());
                if (extmod == null)
                {
                    extmod = new List <RubyModule>();
                }
                extmod.Add(m);
                v = ReadObject0(extmod);
                RubyObject fobj = v as RubyObject;
                if (fobj != null)
                {
                    fobj.ExtendModules.AddRange(extmod);
                }
            }
            break;

            case RubyMarshal.Types.UserClass:
            {
                RubyClass c = RubyClass.GetClass(ReadUnique());

                v = ReadObject0(extmod);
                if (v is RubyObject)
                {
                    (v as RubyObject).ClassName = c.Symbol;
                }
            }
            break;

            case RubyMarshal.Types.Nil:
                v = RubyNil.Instance;
                v = Leave(v);
                break;

            case RubyMarshal.Types.True:
                v = true;
                v = Leave(v);
                break;

            case RubyMarshal.Types.False:
                v = false;
                v = Leave(v);
                break;

            case RubyMarshal.Types.Fixnum:
                v = ReadLong();
                v = Leave(v);
                break;

            case RubyMarshal.Types.Float:
            {
                double     d;
                RubyString fstr = ReadString();
                string     str  = fstr.Text;

                if (str == "inf")
                {
                    d = double.PositiveInfinity;
                }
                else if (str == "-inf")
                {
                    d = double.NegativeInfinity;
                }
                else if (str == "nan")
                {
                    d = double.NaN;
                }
                else
                {
                    if (str.Contains("\0"))
                    {
                        str = str.Remove(str.IndexOf("\0"));
                    }
                    d = Convert.ToDouble(str);
                }
                v = new RubyFloat(d);
                v = Entry(v);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Bignum:
            {
                int sign = 0;
                switch (ReadByte())
                {
                case 0x2b:
                    sign = 1;
                    break;

                case 0x2d:
                    sign = -1;
                    break;

                default:
                    sign = 0;
                    break;
                }
                int    num3  = ReadLong();
                int    index = num3 / 2;
                int    num5  = (num3 + 1) / 2;
                uint[] data  = new uint[num5];
                for (int i = 0; i < index; i++)
                {
                    data[i] = m_reader.ReadUInt32();
                }
                if (index != num5)
                {
                    data[index] = m_reader.ReadUInt16();
                }
                v = new RubyBignum(sign, data);
                v = Entry(v);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.String:
                v = Entry(ReadString());
                v = Leave(v);
                break;

            case RubyMarshal.Types.Regexp:
            {
                RubyString str          = ReadString();
                int        options      = ReadByte();
                bool       has_encoding = false;
                int        idx          = Prepare();
                if (hasivp)
                {
                    ReadInstanceVariable(str, ref has_encoding);
                    ivp = false;
                }
                if (!has_encoding)
                {
                    // TODO: 1.8 compatibility; remove escapes undefined in 1.8

                    /*
                     * char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
                     * long len = RSTRING_LEN(str);
                     * long bs = 0;
                     * for (; len-- > 0; *dst++ = *src++) {
                     *  switch (*src) {
                     *      case '\\': bs++; break;
                     *      case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
                     *      case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
                     *      case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
                     *      case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
                     *      case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
                     *      if (bs & 1) --dst;
                     *      default: bs = 0; break;
                     *  }
                     * }
                     * rb_str_set_len(str, dst - ptr);
                     */
                }
                v = Entry0(new RubyRegexp(str, (RubyRegexpOptions)options), idx);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Array:
            {
                int       len = ReadLong();
                RubyArray ary = new RubyArray();
                v = ary;
                v = Entry(v);
                while (len-- > 0)
                {
                    ary.Push(ReadObject());
                }
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Hash:
            case RubyMarshal.Types.HashWithDefault:
            {
                int      len  = ReadLong();
                RubyHash hash = new RubyHash();
                v = hash;
                v = Entry(v);
                while (len-- > 0)
                {
                    object key   = ReadObject();
                    object value = ReadObject();
                    hash.Add(key, value);
                }
                if (type == RubyMarshal.Types.HashWithDefault)
                {
                    hash.DefaultValue = ReadObject();
                }
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Struct:
            {
                int        idx   = Prepare();
                RubyStruct obj   = new RubyStruct();
                RubySymbol klass = ReadUnique();
                obj.ClassName = klass;
                int len = ReadLong();
                v = obj;
                v = Entry0(v, idx);
                while (len-- > 0)
                {
                    RubySymbol key   = ReadSymbol();
                    object     value = ReadObject();
                    obj.InstanceVariable[key] = value;
                }
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.UserDefined:
            {
                RubySymbol klass = ReadUnique();
                RubyString data  = ReadString();
                if (hasivp)
                {
                    ReadInstanceVariable(data);
                    ivp = false;
                }
                RubyUserdefinedDumpObject obj = new RubyUserdefinedDumpObject();
                obj.ClassName    = klass;
                obj.DumpedObject = data.Raw;
                v = obj;
                v = Entry(v);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.UserMarshal:
            {
                RubySymbol klass = ReadUnique();
                FuzzyUserdefinedMarshalDumpObject obj = new FuzzyUserdefinedMarshalDumpObject();
                v = obj;
                if (extmod != null)
                {
                    AppendExtendedModule(obj, extmod);
                }
                v = Entry(v);
                object data = ReadObject();
                obj.ClassName    = klass;
                obj.DumpedObject = data;
                v = Leave(v);
                if (extmod != null)
                {
                    extmod.Clear();
                }
            }
            break;

            case RubyMarshal.Types.Object:
            {
                int        idx   = Prepare();
                RubyObject obj   = new RubyObject();
                RubySymbol klass = ReadUnique();
                obj.ClassName = klass;
                v             = obj;
                v             = Entry0(v, idx);
                ReadInstanceVariable(v);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Class:
            {
                RubyString str = ReadString();
                v = RubyClass.GetClass(RubySymbol.GetSymbol(str));
                v = Entry(v);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Module:
            {
                RubyString str = ReadString();
                v = RubyModule.GetModule(RubySymbol.GetSymbol(str));
                v = Entry(v);
                v = Leave(v);
            }
            break;

            case RubyMarshal.Types.Symbol:
                if (hasivp)
                {
                    v   = ReadSymbolReal(ivp);
                    ivp = false;
                }
                else
                {
                    v = ReadSymbolReal(false);
                }
                v = Leave(v);
                break;

            case RubyMarshal.Types.SymbolLink:
                v = ReadSymbolLink();
                break;

            case RubyMarshal.Types.Data:
            /*  TODO: Data Support
             *  {
             *      VALUE klass = path2class(r_unique(arg));
             *      VALUE oldclass = 0;
             *
             *      v = obj_alloc_by_klass(klass, arg, &oldclass);
             *      if (!RB_TYPE_P(v, T_DATA)) {
             *          rb_raise(rb_eArgError, "dump format error");
             *      }
             *      v = r_entry(v, arg);
             *      if (!rb_respond_to(v, s_load_data)) {
             *          rb_raise(rb_eTypeError, "class %s needs to have instance method `_load_data'", rb_class2name(klass));
             *      }
             *      rb_funcall(v, s_load_data, 1, r_object0(arg, 0, extmod));
             *      check_load_arg(arg, s_load_data);
             *      v = r_leave(v, arg);
             *  }
             */
            case RubyMarshal.Types.ModuleOld:
            /*
             *  TODO: ModuleOld Support
             *  {
             *      volatile VALUE str = r_bytes(arg);
             *      v = rb_path_to_class(str);
             *      v = r_entry(v, arg);
             *      v = r_leave(v, arg);
             *  }
             */
            default:
                throw new InvalidDataException(string.Format("dump format error(0x{0:X2})", type));
            }
            return(v);
        }
Exemplo n.º 8
0
 internal RubyObjectInstanceVariableProxy(RubyObject obj)
 {
     this.obj = obj;
 }
Exemplo n.º 9
0
 public RubyObjectDebugView(RubyObject obj)
 {
     this.obj = obj;
 }
Exemplo n.º 10
0
        /// <summary>
        /// static void w_object(VALUE obj, struct dump_arg *arg, int limit)
        /// </summary>
        /// <param name="obj"></param>
        public void WriteObject(object obj)
        {
            int num;

            if (this.m_objects.TryGetValue(obj, out num))
            {
                WriteByte(RubyMarshal.Types.Link);
                WriteLong(num);
                return;
            }
            if (obj == null || obj == RubyNil.Instance)
            {
                WriteByte(RubyMarshal.Types.Nil);
            }
            else if (obj is bool && (bool)obj == true)
            {
                WriteByte(RubyMarshal.Types.True);
            }
            else if (obj is bool && (bool)obj == false)
            {
                WriteByte(RubyMarshal.Types.False);
            }
            else if (obj is int)
            {
                int v = (int)obj;
                // (2**30).class   => Bignum
                // (2**30-1).class => Fixnum
                // (-2**30-1).class=> Bignum
                // (-2**30).class  => Fixnum
                if (v <= 1073741823 && v >= -1073741824)
                {
                    WriteByte(RubyMarshal.Types.Fixnum);
                    WriteLong(v);
                }
                else
                {
                    WriteObject(RubyBignum.Create(v));
                }
            }
            else if (obj is RubySymbol)
            {
                WriteSymbol(obj as RubySymbol);
            }
            else
            {
                RubyObject fobj  = obj as RubyObject;
                bool       hasiv = false;
                if (fobj != null)
                {
                    hasiv = (obj as RubyObject).InstanceVariables.Count > 0 || fobj.Encoding != null;
                }

                if (obj is IRubyUserdefinedMarshalDumpObject)
                {
                    this.m_objects.Add(obj, this.m_objects.Count);
                    object result = (obj as IRubyUserdefinedMarshalDumpObject).Dump();
                    if (hasiv)
                    {
                        WriteByte(RubyMarshal.Types.InstanceVariable);
                    }
                    WriteClass(RubyMarshal.Types.UserMarshal, obj, false);
                    WriteObject(result);
                    if (hasiv)
                    {
                        WriteObjectInstanceVariable(fobj);
                    }
                    return;
                }
                if (obj is IRubyUserdefinedDumpObject)
                {
                    byte[] result = (obj as IRubyUserdefinedDumpObject).Dump();
                    if (hasiv)
                    {
                        WriteByte(RubyMarshal.Types.InstanceVariable);
                    }
                    WriteClass(RubyMarshal.Types.UserDefined, obj, false);
                    WriteBytes(result, result.Length);
                    if (hasiv)
                    {
                        WriteObjectInstanceVariable(fobj);
                    }
                    this.m_objects.Add(obj, this.m_objects.Count);
                    return;
                }
                this.m_objects.Add(obj, this.m_objects.Count);

                /*
                 * {
                 *  st_data_t compat_data;
                 *  rb_alloc_func_t allocator = rb_get_alloc_func(RBASIC(obj)->klass);
                 *  if (st_lookup(compat_allocator_tbl,
                 *                (st_data_t)allocator,
                 *                &compat_data)) {
                 *      marshal_compat_t *compat = (marshal_compat_t*)compat_data;
                 *      VALUE real_obj = obj;
                 *      obj = compat->dumper(real_obj);
                 *      st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
                 *      if (obj != real_obj && !ivtbl) hasiv = 0;
                 *  }
                 * }*/

                if (hasiv)
                {
                    WriteByte(RubyMarshal.Types.InstanceVariable);
                }

                if (obj is RubyClass)
                {
                    WriteByte(RubyMarshal.Types.Class);
                    WriteCString((obj as RubyClass).Name);
                }
                else if (obj is RubyModule)
                {
                    WriteByte(RubyMarshal.Types.Module);
                    WriteCString((obj as RubyModule).Name);
                }
                else if (obj is float)
                {
                    WriteByte(RubyMarshal.Types.Float);
                    WriteFloat((float)obj);
                }
                else if (obj is double)
                {
                    WriteByte(RubyMarshal.Types.Float);
                    WriteFloat((double)obj);
                }
                else if (obj is RubyFloat)
                {
                    WriteByte(RubyMarshal.Types.Float);
                    WriteFloat((RubyFloat)obj);
                }
                else if (obj is RubyBignum)
                {
                    RubyBignum value = (RubyBignum)obj;
                    char       ch;
                    if (value.Sign > 0)
                    {
                        ch = '+';
                    }
                    else if (value.Sign < 0)
                    {
                        ch = '-';
                    }
                    else
                    {
                        ch = '0';
                    }
                    this.m_writer.Write((byte)ch);
                    uint[] words = value.GetWords();
                    int    num2  = words.Length * 2;
                    int    index = words.Length - 1;
                    bool   flag  = false;
                    if ((words.Length > 0) && ((words[index] >> 0x10) == 0))
                    {
                        num--;
                        flag = true;
                    }
                    this.WriteLong(num2);
                    for (int i = 0; i < words.Length; i++)
                    {
                        if (flag && (i == index))
                        {
                            this.m_writer.Write((ushort)words[i]);
                        }
                        else
                        {
                            this.m_writer.Write(words[i]);
                        }
                    }
                }
                else if (obj is RubyString || obj is string)
                {
                    RubyString v;
                    if (obj is string)
                    {
                        v = new RubyString(obj as string);
                    }
                    else
                    {
                        v = (RubyString)obj;
                    }
                    WriteUserClass(v, RubyClass.GetClass("String"));
                    WriteByte(RubyMarshal.Types.String);
                    WriteBytes(v.Raw);
                }
                else if (obj is RubyRegexp)
                {
                    RubyRegexp v = (RubyRegexp)obj;
                    WriteUserClass(obj, RubyClass.GetClass("Regexp"));
                    WriteByte(RubyMarshal.Types.Regexp);
                    WriteBytes(v.Pattern.Raw);
                    WriteByte((byte)v.Options);
                }
                else if (obj is RubyArray || obj is List <object> )
                {
                    RubyArray v;
                    if (obj is List <object> )
                    {
                        v = new RubyArray(obj as List <object>);
                    }
                    else
                    {
                        v = (RubyArray)obj;
                    }
                    WriteUserClass(v, RubyClass.GetClass("Array"));
                    WriteByte(RubyMarshal.Types.Array);
                    WriteLong(v.Length);
                    for (int i = 0; i < v.Count; i++)
                    {
                        WriteObject(v[i]);
                    }
                }
                else if (obj is RubyHash)
                {
                    RubyHash v = (RubyHash)obj;
                    WriteUserClass(obj, RubyClass.GetClass("Hash"));
                    WriteByte(v.DefaultValue != null ? RubyMarshal.Types.HashWithDefault : RubyMarshal.Types.Hash);
                    WriteLong(v.Length);
                    foreach (KeyValuePair <object, object> item in v)
                    {
                        WriteObject(item.Key);
                        WriteObject(item.Value);
                    }
                    if (v.DefaultValue != null)
                    {
                        WriteObject(v.DefaultValue);
                    }
                }
                else if (obj is RubyStruct)
                {
                    RubyStruct v = (RubyStruct)obj;
                    WriteUserClass(obj, RubyClass.GetClass("Struct"));
                    WriteLong(v.InstanceVariables.Count);
                    foreach (KeyValuePair <RubySymbol, object> item in v.InstanceVariables)
                    {
                        WriteObject(item.Key);
                        WriteObject(item.Value);
                    }
                }
                else if (obj is RubyObject)
                {
                    WriteClass(RubyMarshal.Types.Object, obj, true);
                    WriteObjectInstanceVariable((RubyObject)obj);
                }

                /* TODO: Data
                 * case T_DATA:
                 *  {
                 *      VALUE v;
                 *
                 *      if (!rb_respond_to(obj, s_dump_data)) {
                 *          rb_raise(rb_eTypeError,
                 *                   "no _dump_data is defined for class %s",
                 *                   rb_obj_classname(obj));
                 *      }
                 *      v = rb_funcall(obj, s_dump_data, 0);
                 *      check_dump_arg(arg, s_dump_data);
                 *      w_class(TYPE_DATA, obj, arg, TRUE);
                 *      w_object(v, arg, limit);
                 *  }
                 *  break;*/
                else
                {
                    throw new InvalidDataException(string.Format("can't dump {0}", obj.GetType().FullName));
                }
                if (hasiv)
                {
                    WriteInstanceVariable(fobj, fobj.InstanceVariables);
                }
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// static void w_objivar(VALUE obj, struct dump_call_arg *arg)
 /// </summary>
 /// <param name="obj"></param>
 public void WriteObjectInstanceVariable(RubyObject obj)
 {
     WriteInstanceVariable(obj, obj.InstanceVariables);
 }