コード例 #1
0
        public override Value Evaluate(RubyContext context)
        {
#if DEBUG
            context.VM.CurrentEvalNode = this;
#endif

            if (context.HasValue(this.name))
            {
                return(context.GetValue(this.name));
            }

            throw new Exception(string.Format("unitialized constant '{0}'", name));
        }
コード例 #2
0
ファイル: RubyContext.cs プロジェクト: lihaochen910/RubySharp
        public bool HasValue(string name)
        {
            if (HasLocalValue(name))
            {
                return(true);
            }

            if (parent != null)
            {
                return(parent.HasValue(name));
            }

            return(false);
        }
コード例 #3
0
        public override Value Evaluate(RubyContext context)
        {
#if DEBUG
            context.VM.CurrentEvalNode = this;
#endif

            if (context.HasValue(name))
            {
                return(context.GetValue(name));
            }

            if (context.Self != null && context.Self is RClass)
            {
                var method = (( RClass )context.Self).GetInstanceMethod(name);
                if (method != null)
                {
                    return(method.Invoke(context.VM.rb_obj_value(context.Self), context, EmptyValues));
                }
            }

            throw new Exception(string.Format("unitialized constant '{0}'", name));
        }
コード例 #4
0
        public override Value Evaluate(RubyContext context)
        {
#if DEBUG
            context.VM.CurrentEvalNode = this;
#endif

            string name   = AstParser.GetNamePathFinalName(this.name);
            RClass target = (context.Self is RClass) ? ( RClass )context.Self : null;
            Value  value  = null;

            if (context.Module != null)
            {
                if (context.Module.constants.HasLocalValue(name))
                {
                    value = context.Module.constants.GetLocalValue(name);
                }
            }
            else if (context.HasValue(name))
            {
                value = context.GetValue(name);
            }

            if (value == null || !(value is RClass))
            {
                var classclass = context.RootContext.GetLocalValue("Class").As <RClass> ();
                var superclass = context.RootContext.GetLocalValue("Object").As <RClass> ();
                var parent     = target == null ? context.Module : target;

                if (super != null)
                {
                    superclass = super.Evaluate(context)?.As <RClass> ();
                    if (superclass == null)
                    {
                        VM.ThrowException($"superclass '{super}' not found.");
                    }
                }

                var newclass = context.VM.DefClass(classclass, name, superclass, parent);
                value = Value.Class(newclass);

                if (context.VM.IsCustomClass(superclass))
                {
                    context.VM.WriteCustomClassFlag(newclass, context.VM.GetCustomClassType(superclass));
                    context.VM.WriteCustomClassRClass(newclass, context.VM.GetCustomClassRClass(superclass));
                }

                if (parent == null)
                {
                    context.RootContext.SetLocalValue(name, value);
                }
                else
                {
                    parent.constants.SetLocalValue(name, value);
                }
            }

            var dclass = value.As <RClass> ();

            RubyContext classcontext = context.VM.NewContext(dclass, context);

            body.Evaluate(classcontext);

            return(null);
        }