Пример #1
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            object time, t;            
            long seconds = 0;
            long uSeconds = 0;

            if (Class.rb_scan_args(caller, rest, 1, 1, false) == 2)
            {
                time = rest[0];
                t = rest[1];
                seconds = Numeric.rb_num2long(rest[0], caller);
                uSeconds = Numeric.rb_num2long(rest[1], caller);
            }
            else 
            {
                time = rest[0];
                Time.rb_time_timeval(rest[0], out seconds, out uSeconds, caller);
            }

            if (time is Time)
            {
                bool gmt = ((Time)time).gmt;
                t = Time.time_new_internal(seconds, uSeconds, gmt, caller);
            }
            else
            {
                t = Time.time_new_internal(seconds, uSeconds, false, caller);
                ((Time)t).value = ((Time)t).value.ToLocalTime();
            }

            return t;
        }
Пример #2
0
 internal static int GETASTER(ref int t, ref int p, out int n, int end,
     string fmtString, ref int posArg, ref int nextArg,
     object nextValue, object tmp, Array argv, Frame caller)
 {
     t = p++;
     n = 0;
     for (; p < end && char.IsDigit(fmtString[p]); p++)
     {
         n = 10 * n + (fmtString[p] - '0');
     }
     if (p >= end)
     {
         throw new ArgumentError("malformed format string - %*[0-9]").raise(caller);
     }
     if (fmtString[p] == '$')
     {
         tmp = GETPOSARG(caller, n, ref posArg, argv);
     }
     else
     {
         tmp = GETARG(caller, ref posArg, ref nextArg, nextValue, argv);
         p = t;
     }
     return Numeric.rb_num2long(tmp, caller);
 }
Пример #3
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            if ((rest.Count == 0) || ((rest.Count == 0) && (rest.value[0].Equals(recv))))
                return recv;

            return Eval.CallPrivate(rest.value[0], caller, "new", block, rest);
        }
Пример #4
0
        public override object Call(Class last_class, object str, Frame caller, Proc block, Array argv)
        {
            int argc = argv.Count;
            object result;

            if (argc < 1 || 2 < argc)
            {
                throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "wrong number of arguments ({0} for 1)", argc)).raise(caller);
            }

            Array buf = new Array();
            int i;
            for (i = 0; i < argc; i++)
            {
                buf.Add(argv[i]);
            }
            result = rb_str_aref_m.singleton.Call(last_class, str, caller, null, buf);
            
            if (result != null)
            {
                buf.Add(new String());
                rb_str_aset_m.singleton.Call(last_class, str, caller, null, buf);
            }

            return result;
        }            
Пример #5
0
        public override object Call(Class klass, object recv, Frame caller, Proc block, Array rest)
        {
            int count = Class.rb_scan_args(caller, rest, 1, 1, false);

            if (block != null && count == 2)
            {
                Errors.rb_warn("block supersedes default value argument");
            }

            object if_none = null;
            if (count == 2)
                if_none = rest[1];

            string name = String.StringValue(rest[0], caller);
            string val = System.Environment.GetEnvironmentVariable(name);

            if (val == null)
            {
                if (block != null)
                    return Proc.rb_yield(block, caller, rest[0]);

                if (if_none != null)
                    return if_none;

                throw new IndexError("key not found").raise(caller);
            }
            else if (Env.PATH_ENV.Equals(name, System.StringComparison.OrdinalIgnoreCase) && !Env.rb_env_path_tainted())
                return new String(val);
            else
                return Env.env_str_new2(caller, val);
        }
Пример #6
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            int i = Numeric.rb_num2long(recv, caller);
            if (rest.Count == 0)
                return new String(i.ToString(CultureInfo.InvariantCulture));

            return rb_big_to_s.singleton.Call(last_class, new Bignum(i), caller, block, rest);
        }
Пример #7
0
        internal static Class rb_struct_define(string name, Frame caller, params string[] members)
        {
            Array ary = new Array();
            foreach (string mem in members)
                ary.Add(new Symbol(mem));

            return make_struct(name, ary, Ruby.Runtime.Init.rb_cStruct, caller);
        }
Пример #8
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            Array memo = new Array();

            Eval.CallPrivate(recv, caller, "each", new Proc(null, null, new to_a(memo), 1));

            return memo;
        }
Пример #9
0
 public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
 {
     if (Class.rb_scan_args(caller, rest, 0, 1, false) == 1)
     {
         return rb_str_replace.singleton.Call1(last_class, recv, caller, null, rest[0]);
     }
     else
         return recv;
 }
Пример #10
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            Array ary = new Array();
            foreach (uint id in Symbol.sym_tbl.Values)
            {
                ary.Add(new Symbol(id));
            }

            return ary;
        }
Пример #11
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            if (rest.Count == 0)
                return recv;

            Exception exc = (Exception)recv;
            exc.instance_variable_set("mesg", rest.value[0]);

            return exc;
        }
Пример #12
0
 public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
 {
     if (rest.Count > 0)
         if (rest[0] is Class)
             return each_object((Class)rest[0], caller, block);
         else
             throw new TypeError("class or module required").raise(caller);
     else
         return each_object(null, caller, block);
 }
Пример #13
0
        internal static object GETNTHARG(Frame caller, int nth, Array argv)
        {
            if (nth >= argv.Count)
            {
                throw new ArgumentError("too few arguments.").raise(caller);
            }
            else
            {
                return argv[nth];
            }

        }
Пример #14
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            Array ary;

            if (rest.Count < 0)
                throw new ArgumentError("negative number of arguments").raise(caller);

            if (rest.Count > 0)
                ary = new Array((Class)recv, rest);
            else
                ary = new Array((Class)recv);
            
            return ary;
        }
Пример #15
0
        public RubyException raise(Frame caller)
        {
            Array backtrace = new Array();

            Frame frame = caller;
            while (frame != null)
            {
                backtrace.Add(new String(frame.callPoint()));
                frame = frame.caller;
            }

            this.instance_variable_set("bt", backtrace);

            return rubyException;
        }
Пример #16
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            if (((IO)recv).f != null)
                throw new RuntimeError("reinitializing File").raise(caller);

            int fd;
            if (0 < rest.Count && rest.Count < 3)
            {
                fd = Object.CheckConvert<int>(rest[0], "to_int", caller);
                if (fd > 0)
                    return rb_io_initialize.singleton.Call(last_class, recv, caller, block, fd, rest);
            }

            return IO.rb_open_file((IO)recv, caller, rest);
        }
Пример #17
0
        internal static Class make_struct(string name, Array members, Class klass, Frame caller)
        {
            // Compiler Bug???: Found bug here the following code crashes this method:
            // Customer = Struct.new( :name, :address, :zip )  

            // FIXME: OBJ_FREEZE(members);            
            Class nstr = null;

            if (name == null)
            {
                nstr = new Class(null, klass, Class.Type.Class);
                Class.rb_make_metaclass(nstr, klass.my_class);
                nstr.class_inherited(klass, caller);
            }
            else
            {
                if (!Symbol.is_const_id(name))
                {
                    throw new NameError("identifier " + name + " needs to be constant").raise(caller);
                }
                if (klass.const_defined(name, false))
                {
                    // rb_warn
                    klass.remove_const(name);
                }

                nstr = Class.rb_define_class_under(klass, name, klass, caller);
            }
            nstr.instance_variable_set("__size__", members.Count);
            nstr.instance_variable_set("__members__", members);

            Class.rb_define_alloc_func(nstr, Methods.struct_alloc.singleton);
            Class.rb_define_singleton_method(nstr, "new", Methods.rb_class_new_instance.singleton, -1, caller);
            Class.rb_define_singleton_method(nstr, "[]", Methods.rb_class_new_instance.singleton, -1, caller);
            Class.rb_define_singleton_method(nstr, "members", Methods.rb_struct_s_members.singleton, 0, caller);

            foreach (object m in members)
            {
                string id = Symbol.rb_to_id(caller, m);
                if (Symbol.is_local_id(id) || Symbol.is_const_id(id))
                {
                    Class.rb_define_method(nstr, id, new AttrReaderMethodBody(id), 0, caller);
                    Class.rb_define_method(nstr, id + "=", new AttrWriterMethodBody(id), 1, caller);
                }
            }

            return nstr;
        }
Пример #18
0
        public override object Call(Class klass, object recv, Frame caller, Proc block, Array rest)
        {
            throw new NotImplementedError("rb_thread_s_new not supported").raise(caller);

            //Ruby.Thread th = new Thread();
            ////volatile VALUE *pos;

            ////pos = th->stk_pos;
            //Eval.CallPrivate(th, caller, "initialize", block, rest.value.ToArray());
            ////if (th->stk_pos == 0) {
            ////    rb_raise(rb_eThreadError, "uninitialized thread - check `%s#initialize'",
            ////        rb_class2name(klass));
            ////}

            //return th;
        }
Пример #19
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            string name = null;
            string[] members = new string[0];

            /**
            if (rest.Count > 0)
            {
                if (!(rest[0] is Symbol))
                {
                    name = Symbol.rb_to_id(rest[0]);
                    if (rest.Count > 1)
                        rest = new Array(rest.value.GetRange(1, rest.Count - 1));
                }
            }
            */

            // BBTAG: the following is a temporary work-around as the compiler currently does not
            // differentiate between Symbols and Strings: if the first identifier is a valid const 
            // name, then we assume it is the name of the struct

            if (rest.Count > 0)
            {
                string id = Symbol.rb_to_id(caller, rest[0]);
                if (Symbol.is_const_id(id))
                {
                    name = id;
                    if (rest.Count > 1)
                        rest = new Array(rest.value.GetRange(1, rest.Count - 1));
                }
            }

            if (rest.Count > 0)
            {
                members = new string[rest.Count];

                for (int i = 0; i < rest.Count; i++)
                    members[i] = Symbol.rb_to_id(caller, rest[i]);
            }

            Class st = Struct.make_struct(name, new Array(members), Ruby.Runtime.Init.rb_cStruct, caller);

            if (block != null)
                rb_mod_module_eval.singleton.Call(last_class, st, caller, block, new Array());

            return st;
        }
Пример #20
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array argv)
        {
            String str;//, kcode;
            //int kcode_saved = reg_kcode;

            if (Class.rb_scan_args(caller, argv, 1, 1, false) == 2)
            {
                //rb_set_kcode(StringValuePtr(kcode));
                //curr_kcode = reg_kcode;
                //reg_kcode = kcode_saved;
            }
            str = String.RStringValue(argv[0], caller);
            str = Regexp.rb_reg_quote(str);
            //kcode_reset_option();

            return str;
        }
Пример #21
0
        public override object Call1(Class last_class, object recv, Frame caller, Proc block, object level)
        {
            Array list = new Array();
            int skip = (int)level;

            int i = 0;
            while (caller != null)
            {
                if (i >= skip)
                    list.Add(new String(caller.callPoint()));

                caller = caller.caller;
                i++;
            }

            return list;
        }
Пример #22
0
        public override object Call0(Class last_class, object group, Frame caller, Proc block)
        {
            ThreadGroup data;            
            Array ary;

            data = (ThreadGroup)group;
            ary = new Array();

            foreach(Thread th in Thread.thread_list){
                if (th.thGroup == data.group)
                {
                    ary.Add(th.thread);
                }
            }

            return ary;
        }
Пример #23
0
        public override object Call(Class last_class, object thread, Frame caller, Proc block, Array args)
        {
            throw new Ruby.NotImplementedError("rb_thread_initialize not supported").raise(caller);

            //if (block == null)
            //{
            //    throw new ThreadError("`initialize': must be called with a block (ThreadError)").raise(caller);
            //}


            ////this is naive
            //((Thread)thread).ThreadStart(caller, block, args);
            //return null;

            ////TODO: a lot of work is going on in here. 
            ////return rb_thread_start_0(rb_thread_yield, args, Thread.rb_thread_check(thread));

        }
Пример #24
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, object p1, object p2, Array rest)
        {
            throw new NotImplementedError("rb_thread_start not supported").raise(caller);

            //Ruby.Thread thread = new Thread();
            //if (block == null)
            //{
            //    throw new Ruby.ThreadError("`initialize': must be called with a block (ThreadError)").raise(caller);
            //}
            //thread.ThreadStart(caller, block, rest);

            ////  HOW DO I GENERATE THIS ERROR?
            ////    if (th->stk_pos == 0) {
            ////        rb_raise(rb_eThreadError, "uninitialized thread - check `%s#initialize'",
            ////            rb_class2name(klass));
            ////    }
            //return thread;
        }
Пример #25
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array args)
        {
            if (args.Count == 0)
                return null;

            IO outstream;

            if (args[0] is String)
                outstream = (IO)IO.rb_stdout.value;
            else
            {
                outstream = (IO)args[0];
                args.value.RemoveAt(0);
            }

            IO.rb_io_write(outstream, rb_f_sprintf.singleton.Call(last_class, null, caller, null, args), caller);

            return null;
        }
Пример #26
0
 internal static object GETPOSARG(Frame caller, int n, ref int posArg, Array argv)
 {
     if (posArg > 0)
     {
         throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "numbered({0}) after unnumbered({1})", n, posArg)).raise(caller);
     }
     else
     {
         if (n < 1)
         {
             throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "invalid index - {0}$", n)).raise(caller);
         }
         else
         {
             posArg = -1;
             return GETNTHARG(caller, n, argv);
         }
     }
 }
Пример #27
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            System.Collections.ArrayList list;
            if (Class.rb_scan_args(caller, rest, 0, 1, false) == 0)
                rest.Add(true);

            if (Marshal.RTEST(rest[0]))
            {
                list = new System.Collections.ArrayList();
                Class.CLASS_OF(recv).get_all_specified_methods(list, Access.Public, Eval.Test(rest[0]));
                return Array.CreateUsing(list);
            }
            else
            {
                list = new System.Collections.ArrayList();
                Class.CLASS_OF(recv).public_methods(list);
                return new Array(list);
            }
        }
Пример #28
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array args)
        {
            Hash hash = (Hash)recv;

            Hash.rb_hash_modify(caller, hash);

            if (block != null)
            {
                if (args.Count > 0)
                    throw new ArgumentError("wrong number of arguments").raise(caller);
                hash.defaultProc = block;
            }
            else
            {
                if (Class.rb_scan_args(caller, args, 0, 1, false) > 0)
                    hash.defaultValue = args[0];
            }

            return hash;
        }
Пример #29
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            Hash hash;

            if ((rest.Count == 1) && (rest[0] is Hash))
            {
                hash = new Hash((Class)recv);
                hash.value = ((Hash)rest[0]).value;
                return hash;
            }

            if (rest.Count % 2 != 0)
                throw new ArgumentError("odd number of arguments for Hash").raise(caller);

            hash = new Hash((Class)recv);
            for (int i = 0; i < rest.Count; i += 2)
                rb_hash_aset.singleton.Call2(last_class, hash, caller, null, rest[i], rest[i + 1]);

            return hash;
        }
Пример #30
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array argv)
        {
            int argc = argv.Count;
            if (argc == 0)
            {
                return new Regexp("(?!)", 0);
            }
            else if (argc == 1)
            {
                Regexp v = Object.CheckConvert<Regexp>(argv[0], "to_regexp", caller);
                if (v != null)
                {
                    return v;
                }
                else
                {
                    object quote = rb_reg_s_quote.singleton.Call1(last_class, recv, caller, null, argv[0]);

                    object re = rb_reg_s_alloc.singleton.Call0(last_class, recv, caller, null);
                    return rb_reg_initialize_m.singleton.Call(last_class, re, caller, null, new Array(new object[] { quote }));
                }
            }
            else
            {
                System.Text.StringBuilder source = new System.Text.StringBuilder();
                for (int i = 0; i < argv.Count; i++)
                {
                    if (0 < i)
                        source.Append("|");
                    Regexp v = Object.CheckConvert<Regexp>(argv[i], "to_regexp", caller);
                    if (v != null)
                        source.Append(rb_reg_to_s.singleton.Call0(last_class, v, caller, null));
                    else
                        source.Append(rb_reg_s_quote.singleton.Call(last_class, null, caller, null, new Array(new object[] { argv[i] })));

                }
                return new Regexp(source.ToString(), 0);
            }
        }