Beispiel #1
0
        public static Func <Value, RubyContext, IList <Value>, Value> Gen_BaseOp_IfExist(Type type, RClass @class, string op)
        {
            var methodInfo = type.GetMethods(BindingFlags.Static | BindingFlags.Public)
                             .Where(m => {
                if (!m.Name.Equals(op))
                {
                    return(false);
                }
                var parameters = m.GetParameters();
                if (parameters.Length != 2)
                {
                    return(false);
                }
                if (parameters[0].ParameterType != type)
                {
                    return(false);
                }
                return(true);
            }).FirstOrDefault();

            if (methodInfo != null)
            {
                Func <Value, RubyContext, IList <Value>, Value> func = (self, context, values) => {
                    object   userdata   = (( RObject )self.p).GetIV(VM.FIELD_USERDATA).p;
                    object[] parameters = new [] {
                        userdata,
                        context.VM.ValueToObject(context.VM.GetArg <Value> (values, 0))
                    };

                    object ret = methodInfo.Invoke(null, parameters);
                    if (ret == null)
                    {
                        return(Value.Nil());
                    }
                    if (ret.GetType() == typeof(int))
                    {
                        return(Value.Fixnum(( int )ret));
                    }
                    if (ret.GetType() == typeof(float))
                    {
                        return(Value.Float(( float )ret));
                    }
                    if (ret.GetType() == typeof(bool))
                    {
                        return(Value.Bool(( bool )ret));
                    }
                    if (ret.GetType() == typeof(string))
                    {
                        return(Value.Str(( string )ret));
                    }
                    return(Value.Data(RObject.CreateUserdataRObject(context.VM, context.VM.FindCustomClass(ret), ret)));
                };

                @class.SetInstanceMethod(operator_methods[op], func);

                return(func);
            }

            return(null);
        }
Beispiel #2
0
        public static Func <Value, RubyContext, IList <Value>, Value> GenGetProperty(PropertyInfo propertyInfo)
        {
            Func <Value, RubyContext, IList <Value>, Value> func = (self, context, values) => {
                object userdata = (( RObject )self.p).GetIV(VM.FIELD_USERDATA).p;
                if (propertyInfo.PropertyType == typeof(int))
                {
                    return(Value.Fixnum(( int )propertyInfo.GetValue(userdata, null)));
                }
                if (propertyInfo.PropertyType == typeof(float))
                {
                    return(Value.Float(( float )propertyInfo.GetValue(userdata, null)));
                }
                if (propertyInfo.PropertyType == typeof(bool))
                {
                    return(Value.Bool(( bool )propertyInfo.GetValue(userdata, null)));
                }
                if (propertyInfo.PropertyType == typeof(string))
                {
                    return(Value.Str(( string )propertyInfo.GetValue(userdata, null)));
                }
                return(Value.Data(RObject.CreateUserdataRObject(context.VM, (( RObject )self.p).Class, propertyInfo.GetValue(userdata, null))));
                // return Value.Ptr ( propertyInfo.GetValue ( userdata ) );
            };

            return(func);
        }
Beispiel #3
0
        public virtual RObject CreateInstance(VM vm)
        {
            var obj = new RObject();

            obj.vm     = vm;
            obj.@class = @class;
            return(obj);
        }
Beispiel #4
0
        public static RObject CreateUserdataRObject(VM vm, RClass @class, object userdata)
        {
            var obj = new RObject();

            obj.vm     = vm;
            obj.type   = ValueType.Data;
            obj.@class = @class;
            obj.SetIV(VM.FIELD_USERDATA, Value.Ptr(userdata));
            return(obj);
        }
Beispiel #5
0
        public static Func <Value, RubyContext, IList <Value>, Value> GenMethod(MethodInfo methodInfo, bool staticMethod = false)
        {
            Func <Value, RubyContext, IList <Value>, Value> func = (self, context, values) => {
                object   userdata       = (( RObject )self.p).GetIV(VM.FIELD_USERDATA).p;
                var      parametersInfo = methodInfo.Parameters();
                object[] parameters     = new object[parametersInfo.Count];
                for (var i = 0; i < parametersInfo.Count; ++i)
                {
                    parameters[i] = context.VM.ValueToObject(context.VM.GetArg <Value> (values, i));
                }
                object ret = methodInfo.Invoke(staticMethod ? null : userdata, parameters);
                if (ret == null)
                {
                    return(Value.Nil());
                }
                if (ret.GetType() == typeof(int))
                {
                    return(Value.Fixnum(( int )ret));
                }
                if (ret.GetType() == typeof(float))
                {
                    return(Value.Float(( float )ret));
                }
                if (ret.GetType() == typeof(bool))
                {
                    return(Value.Bool(( bool )ret));
                }
                if (ret.GetType() == typeof(string))
                {
                    return(Value.Str(( string )ret));
                }
                return(Value.Data(RObject.CreateUserdataRObject(context.VM, (( RObject )self.p).Class, ret)));
            };

            return(func);
        }
Beispiel #6
0
 internal RubyContext(RObject self, RubyContext parent)
 {
     this.self   = self;
     this.parent = parent;
 }
Beispiel #7
0
 internal RubyContext(RClass module, RubyContext parent)
 {
     this.module = module;
     this.parent = parent;
     self        = module;
 }
Beispiel #8
0
 public static Value Data(RObject obj)
 {
     return(new Value {
         type = ValueType.Data, p = obj
     });
 }