Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
 }
Ejemplo n.º 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());
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
 }
Ejemplo n.º 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());
                }
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 12
0
 public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi)
 {
     this.vm = vm;
     vm.PushNewFrame(def, ci, mi);
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 14
0
 public InterfaceInstance(ClassInstance obj, InterfaceDefn i)
 {
     this.obj   = obj;
     hinterface = i;
 }
Ejemplo n.º 15
0
 public InterfaceInstance(ClassInstance obj, InterfaceDefn i)
 {
     this.obj = obj;
     hinterface = i;
 }
Ejemplo n.º 16
0
 public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi)
 {
     this.vm = vm;
     vm.PushNewFrame(def, ci, mi);
 }
Ejemplo n.º 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));
 }