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); }
/// <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); }
/// <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()); }
// 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()); } }
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); }
/// <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()); }
// 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()); } } }
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); } }
public ModuleInstance GetModuleInstance() { ModuleInstance mi = self as ModuleInstance; if (mi != null) { return(mi); } ClassInstance ci = GetClassInstance(); if (ci != null) { return(ci.GetModuleInstance()); } return(null); }
/// <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)); }
/// <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); }
public DisposableFrame(VM vm, FunctionDefn def, ClassInstance ci, ModuleInstance mi) { this.vm = vm; vm.PushNewFrame(def, ci, mi); }
/// <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)); }
public InterfaceInstance(ClassInstance obj, InterfaceDefn i) { this.obj = obj; hinterface = i; }