Пример #1
0
        protected void CallConstructor(VM vm, HeronValue[] args, ModuleInstance mi, ClassInstance ci)
        {
            // First we are going to invoke the auto-constructor
            GetAutoConstructor().Invoke(ci, vm, new HeronValue[] { });

            List <FunctionDefn> ctorlist = new List <FunctionDefn>(GetMethods("Constructor"));

            if (ctorlist == null)
            {
                return;
            }
            ctors = new FunDefnListValue(ci, "Constructor", ctorlist);
            if (ctors.Count == 0)
            {
                if (args.Length > 0)
                {
                    throw new Exception("No constructors have been defined and default constructor accepts no arguments");
                }
                else
                {
                    return;
                }
            }

            FunctionValue o = ctors.Resolve(vm, args);

            if (o == null)
            {
                throw new Exception("No matching constructor could be found");
            }

            o.Apply(vm, args);
        }
Пример #2
0
        /// <summary>
        /// Creates an instance of this class.
        /// </summary>
        /// <param name="env"></param>
        /// <returns></returns>
        public override HeronValue Instantiate(VM vm, HeronValue[] args, ModuleInstance m)
        {
            ClassInstance r = new ClassInstance(this, m);

            AddFields(r, m);
            CallConstructor(vm, args, m, r);
            return(r);
        }
Пример #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public Frame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     this.function = f;
     this.self = self;
     moduleInstance = mi;
     if (self != null)
         moduleDef = self.Type.GetModule();
     AddScope(new Scope());
 }
Пример #4
0
 // TODO: I may want to change the requirement that DuckValue needs a 
 // class instances, same with interface instance
 public DuckValue(ClassInstance obj, InterfaceDefn i)
     : base(obj, i)
 {
     HeronType t = obj.Type;
     foreach (FunctionDefn f in i.GetAllMethods())
     {
         if (!obj.SupportsFunction(f))
             throw new Exception("Failed to duck-type, object of type " + t.GetName() + " does not match interface " + i.GetName());
     }
 }
Пример #5
0
        public override HeronValue As(HeronType t)
        {
            if (type.name == t.name)
            {
                return(obj);
            }

            if (type is ClassDefn)
            {
                ClassInstance inst = obj as ClassInstance;
                if (inst == null)
                {
                    throw new Exception("Expected an instance of a class");
                }
                return(inst.As(t));
            }
            else if (type is InterfaceDefn)
            {
                InterfaceInstance ii = obj as InterfaceInstance;
                if (ii == null)
                {
                    throw new Exception("Expected an instance of an interface");
                }
                return(ii.As(t));
            }
            else if (type is DotNetClass)
            {
                if (!(t is DotNetClass))
                {
                    throw new Exception("External objects can only be cast to the type 'DotNetClass'");
                }

                if (t.Equals(type))
                {
                    return(obj);
                }
            }
            else if (t.name == "Any")
            {
                return(this);
            }
            else
            {
                Type from = type.GetSystemType();
                Type to   = t.GetSystemType();

                if (from != null && to != null && to.IsAssignableFrom(from))
                {
                    return(obj);
                }
            }

            return(null);
        }
Пример #6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public Frame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     this.function  = f;
     this.self      = self;
     moduleInstance = mi;
     if (self != null)
     {
         moduleDef = self.Type.GetModule();
     }
     AddScope(new Scope());
 }
Пример #7
0
        // TODO: I may want to change the requirement that DuckValue needs a
        // class instances, same with interface instance
        public DuckValue(ClassInstance obj, InterfaceDefn i)
            : base(obj, i)
        {
            HeronType t = obj.Type;

            foreach (FunctionDefn f in i.GetAllMethods())
            {
                if (!obj.SupportsFunction(f))
                {
                    throw new Exception("Failed to duck-type, object of type " + t.GetName() + " does not match interface " + i.GetName());
                }
            }
        }
Пример #8
0
        public void AddFields(ClassInstance i, ModuleInstance m)
        {
            i.AddField(new VarDesc("this"), i);

            foreach (FieldDefn field in fields)
            {
                i.AddField(field);
            }

            if (GetBaseClass() != null)
            {
                ClassInstance b = new ClassInstance(GetBaseClass(), m);
                GetBaseClass().AddFields(b, m);
                i.AddField(new VarDesc("base"), b);
            }
        }
Пример #9
0
        public ModuleInstance GetModuleInstance()
        {
            ModuleInstance mi = self as ModuleInstance;

            if (mi != null)
            {
                return(mi);
            }

            ClassInstance ci = GetClassInstance();

            if (ci != null)
            {
                return(ci.GetModuleInstance());
            }

            return(null);
        }
Пример #10
0
 /// <summary>
 /// Creates a new frame, and returns a frame manager, which will release the frame
 /// on Dispose.
 /// </summary>
 /// <param name="fun"></param>
 /// <param name="classInstance"></param>
 /// <returns></returns>
 public DisposableFrame CreateFrame(FunctionDefn fun, ClassInstance classInstance, ModuleInstance mi)
 {
     return(new DisposableFrame(this, fun, classInstance, mi));
 }
Пример #11
0
 /// <summary>
 /// Creates a new frame, and returns a frame manager, which will release the frame
 /// on Dispose.
 /// </summary>
 /// <param name="fun"></param>
 /// <param name="classInstance"></param>
 /// <returns></returns>
 public DisposableFrame CreateFrame(FunctionDefn fun, ClassInstance classInstance, ModuleInstance mi)
 {
     return new DisposableFrame(this, fun, classInstance, mi);
 }
Пример #12
0
 public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi)
 {
     this.vm = vm;
     vm.PushNewFrame(def, ci, mi);
 }
Пример #13
0
 /// <summary>
 /// Called when a new function execution starts.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public void PushNewFrame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     frames.Add(new Frame(f, self, mi));
 }
Пример #14
0
 public InterfaceInstance(ClassInstance obj, InterfaceDefn i)
 {
     this.obj   = obj;
     hinterface = i;
 }
Пример #15
0
 public InterfaceInstance(ClassInstance obj, InterfaceDefn i)
 {
     this.obj = obj;
     hinterface = i;
 }
Пример #16
0
 public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi)
 {
     this.vm = vm;
     vm.PushNewFrame(def, ci, mi);
 }
Пример #17
0
 /// <summary>
 /// Called when a new function execution starts.
 /// </summary>
 /// <param name="f"></param>
 /// <param name="self"></param>
 public void PushNewFrame(FunctionDefn f, ClassInstance self, ModuleInstance mi)
 {
     frames.Add(new Frame(f, self, mi));
 }