public void destroyClassReally(IClassObject classObject) { if (!isMainThread()) { logError("只能在主线程中使用ClassPool,子线程中请使用ClassPoolThread代替"); return; } bool inuse = isInuse(classObject); classObject.resetProperty(); classObject.setDestroy(true); // 从已使用列表中移除 if (inuse) { removeInuse(classObject); } // 从未使用列表中移除 else { if (mUnusedList.TryGetValue(classObject.GetType(), out HashSet <IClassObject> list) && list.Count > 0) { list.Remove(classObject); } } }
protected bool markUnused(IClassObject classObject, ref string info) { // 加入未使用列表 Type type = classObject.GetType(); if (!mUnusedList.ContainsKey(type)) { mUnusedList.Add(type, new List <IClassObject>()); } else { if (mUnusedList[type].Contains(classObject)) { info = "ClassObject is in Unused list! can not add again!"; return(false); } } mUnusedList[type].Add(classObject); // 从使用列表移除,要确保操作的都是从本类创建的实例 if (!mInusedList.ContainsKey(type)) { info = "can not find class type in Inused List! type : " + type; return(false); } if (!mInusedList[type].Remove(classObject)) { info = "Inused List not contains class object!"; return(false); } return(true); }
// 返回值表示是否是new出来的对象,false则为从回收列表中重复使用的对象 public IClassObject newClass(out bool isNewObject) { mListLock.waitForUnlock(); isNewObject = false; IClassObject obj = null; // 先从未使用的列表中查找是否有可用的对象 if (mUnusedList.Count > 0) { foreach (var item in mUnusedList) { obj = item; break; } mUnusedList.Remove(obj); } // 未使用列表中没有,创建一个新的 else { obj = createInstance <IClassObject>(mType); // 创建实例时重置是为了与后续复用的实例状态保持一致 obj.resetProperty(); isNewObject = true; } obj.setAssignID(++mAssignIDSeed); obj.setDestroy(false); // 添加到已使用列表 mInusedList.Add(obj); mListLock.unlock(); return(obj); }
/// <summary> /// Create a class object. /// </summary> /// <param name="context">The runtime context.</param> /// <param name="name">The name of the class.</param> /// <param name="super">The super class.</param> /// <param name="members">The members of the class.</param> /// <returns>The class object with the name <paramref name="name"/>, the super class <paramref name="super"/> and the members <paramref name="members"/>.</returns> public virtual IClassObject CreateClass(RuntimeContext context, string name, IClassObject super, MemberCollection members) { var clscrt = new ScriptClassCreator(context, name, super); clscrt.DeclareMembers(members); return(clscrt.Class); }
private ExecuteResult ExecuteStatement(ClassDeclarationStatement statement) { var name = statement.Name; IClassObject super = null; if (statement.Super != null) { super = this.EvaluateExpression(statement.Super) as IClassObject; if (super == null) { throw new RuntimeException(statement.Super.LinePragma, ExceptionResource.TypeExpected); } } try { IClassObject value = ObjectCreator.CreateClass(this, name, super, statement.Members); Memory.DecalreClass(this, name, value); } catch (RuntimeException) { throw; } catch (Exception ex) { throw new RuntimeException(statement.LinePragma, ex.Message, ex); } return(ExecuteResult.Next); }
// 返回值表示是否是new出来的对象,false则为从回收列表中重复使用的对象 public bool newClass(out IClassObject obj, Type type) { bool isNewObject = false; obj = null; // 锁定期间不能调用任何其他非库函数,否则可能会发生死锁 mListLock.waitForUnlock(); try { // 先从未使用的列表中查找是否有可用的对象 if (mUnusedList.ContainsKey(type) && mUnusedList[type].Count > 0) { obj = mUnusedList[type].Pop(); isNewObject = false; } // 未使用列表中没有,创建一个新的 else { obj = createInstance <IClassObject>(type); isNewObject = true; } // 添加到已使用列表 addInuse(obj); } catch (Exception e) { logError(e.Message); } mListLock.unlock(); // 重置实例 obj?.resetProperty(); return(isNewObject); }
public bool isInuse(IClassObject classObject) { mListLock.waitForUnlock(); bool inuse = mInusedList.Contains(classObject); mListLock.unlock(); return(inuse); }
public bool isInuse(IClassObject classObject) { if (!isMainThread()) { logError("只能在主线程中使用ClassPool,子线程中请使用ClassPoolThread代替"); return(false); } return(mInusedList.TryGetValue(Typeof(classObject), out HashSet <IClassObject> list) && list.Contains(classObject)); }
public void destroyClassReally(IClassObject classObject) { mListLock.waitForUnlock(); if (!mPoolList.TryGetValue(Typeof(classObject), out ClassPoolSingle singlePool)) { logError("找不到类对象的对象池"); } singlePool.destroyClassReally(classObject); mListLock.unlock(); }
// 仅用于主工程中的类,否则无法识别 public T newClass <T>(out T obj) where T : class, IClassObject { IClassObject classObj = newClass(Typeof <T>(), out _); obj = classObj as T; if (obj == null) { logError("创建类实例失败,可能传入的type类型与目标类型不一致"); } return(obj); }
/// <summary> /// Declares a class with the specified a name. /// </summary> /// <param name="context">The runtime context.</param> /// <param name="name">The class name.</param> /// <param name="value">The class object.</param> /// <exception cref="InvalidOperationException">The class has not been declared in hoisting. <see cref="HoistingDecalreClass"/> should be called first.</exception> protected virtual void DecalreClass(RuntimeContext context, string name, IClassObject value) { if (this.Classes.ContainsKey(name)) { this.Classes[name] = value; } else { throw new InvalidOperationException( string.Format(ExceptionResource.Undefined, name)); } }
public bool isInuse(IClassObject classObject) { mListLock.waitForUnlock(); if (!mPoolList.TryGetValue(Typeof(classObject), out ClassPoolSingle singlePool)) { logError("找不到类对象的对象池"); } bool inuse = singlePool.isInuse(classObject); mListLock.unlock(); return(inuse); }
public void destroyClass(IClassObject classObject) { if (!isMainThread()) { logError("只能在主线程中使用ClassPool,子线程中请使用ClassPoolThread代替"); return; } classObject.resetProperty(); classObject.setDestroy(true); addUnuse(classObject); removeInuse(classObject); }
protected void removeInuse(IClassObject classObject) { // 从使用列表移除,要确保操作的都是从本类创建的实例 Type type = Typeof(classObject); if (!mInusedList.TryGetValue(type, out HashSet <IClassObject> classList)) { logError("can not find class type in Inused List! Type: " + type); } if (!classList.Remove(classObject)) { logError("Inused List not contains class object! Type: " + type); } }
/// <summary> /// Initialize a new instance of the <see cref="MethodContext"/> class with the specified parameters. /// </summary> /// <param name="cls">The class of the scope.</param> /// <param name="self">The <c>this</c> reference bound in the scope.</param> /// <param name="closure">The closure scope of the context.</param> /// <exception cref="ArgumentNullException"><paramref name="cls"/> or <paramref name="closure"/> is <see langword="null"/>.</exception> public MethodContext( IClassObject cls, IScriptObject self, RuntimeContext closure) : base(closure) { if (cls == null) { throw new ArgumentNullException(); } _class = cls; _this = self; IsStatic = self == null; }
public void destroyClass(IClassObject classObject) { mListLock.waitForUnlock(); try { addUnuse(classObject); removeInuse(classObject); } catch (Exception e) { logError(e.Message); } mListLock.unlock(); }
protected void removeInuse(IClassObject classObject) { // 从使用列表移除,要确保操作的都是从本类创建的实例 Type type = classObject.GetType(); if (!mInusedList.ContainsKey(type)) { logError("can not find class type in Inused List! type : " + type); } if (!mInusedList[type].Remove(classObject)) { logError("Inused List not contains class object!"); } }
/// <summary> /// Initialize a new instance of the <see cref="ScriptMethod"/> class using the specified parameter. /// </summary> /// <param name="cls">The class that the method belongs to.</param> /// <param name="parameters">The parameters of the method.</param> /// <param name="statements">The statements in the method body.</param> /// <param name="closure">The closure scope of the method.</param> /// <exception cref="ArgumentNullException"><paramref name="closure"/> or <paramref name="cls"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="parameters"/> is invalid.</exception> public ScriptMethod( IClassObject cls, ParameterCollection parameters, StatementCollection statements, RuntimeContext closure) { if (closure == null || cls == null) { throw new ArgumentNullException(); } FunctionHelper.CheckParameters(parameters); Class = cls; Parameters = parameters; Statements = statements; Closure = closure; }
public void destroyClass(IClassObject classObject) { mListLock.waitForUnlock(); classObject.resetProperty(); classObject.setDestroy(true); // 加入未使用列表 if (!mUnusedList.Add(classObject)) { logError("对象已经在未使用列表中,无法再次销毁! Type: " + mType); } // 从使用列表移除,要确保操作的都是从本类创建的实例 if (!mInusedList.Remove(classObject)) { logError("从未使用列表中移除失败! Type: " + mType); } mListLock.unlock(); }
//---------------------------------------------------------------------------------------------------------------------------------------------- protected void addInuse(IClassObject classObject) { Type type = classObject.GetType(); if (!mInusedList.ContainsKey(type)) { mInusedList.Add(type, new List <IClassObject>()); } else { if (mInusedList[type].Contains(classObject)) { logError("object is in inused list"); return; } } // 加入使用列表 mInusedList[type].Add(classObject); }
protected void addUnuse(IClassObject classObject) { // 加入未使用列表 Type type = classObject.GetType(); if (!mUnusedList.ContainsKey(type)) { mUnusedList.Add(type, new Stack <IClassObject>()); } else { if (mUnusedList[type].Contains(classObject)) { logError("ClassObject is in Unused list! can not add again!"); return; } } mUnusedList[type].Push(classObject); }
protected void addUnuse(IClassObject classObject) { // 加入未使用列表 Type type = Typeof(classObject); if (!mUnusedList.TryGetValue(type, out HashSet <IClassObject> objList)) { objList = new HashSet <IClassObject>(); mUnusedList.Add(type, objList); } else { if (objList.Contains(classObject)) { logError("ClassObject is in Unused list! can not add again! Type: " + type); return; } } objList.Add(classObject); }
public void destroyClass(IClassObject classObject) { string info = ""; bool ret = false; mListLock.waitForUnlock(); try { ret = markUnused(classObject, ref info); } catch (Exception e) { logError(e.Message); } mListLock.unlock(); if (!ret) { logError(info); } }
//---------------------------------------------------------------------------------------------------------------------------------------------- protected void addInuse(IClassObject classObject) { Type type = Typeof(classObject); if (!mInusedList.TryGetValue(type, out HashSet <IClassObject> objList)) { objList = new HashSet <IClassObject>(); mInusedList.Add(type, objList); } else { if (objList.Contains(classObject)) { logError("object is in inused list"); return; } } // 加入使用列表 objList.Add(classObject); }
// 返回值表示是否是new出来的对象,false则为从回收列表中重复使用的对象 public bool newClass(out IClassObject obj, Type type) { bool isNewObject = false; string info = null; obj = null; // 锁定期间不能调用任何其他非库函数,否则可能会发生死锁 mListLock.waitForUnlock(); try { // 先从未使用的列表中查找是否有可用的对象 if (mUnusedList.ContainsKey(type) && mUnusedList[type].Count > 0) { obj = mUnusedList[type][0]; isNewObject = false; } // 未使用列表中没有,创建一个新的 else { obj = createInstance <IClassObject>(type); isNewObject = true; } // 标记为已使用 if (!markUsed(obj)) { info = "ClassObject is in Inused list! can not add again!"; } } catch (Exception e) { logError(e.Message); } mListLock.unlock(); if (!isEmpty(info)) { logError(info); } // 重置实例 obj?.resetProperty(); return(isNewObject); }
/// <summary> /// Declares a class with the specified a name. /// </summary> /// <param name="context">The runtime context.</param> /// <param name="name">The class name.</param> /// <param name="value">The class object.</param> protected override void DecalreClass(RuntimeContext context, string name, IClassObject value) { if (this.Classes.ContainsKey(name)) { this.Classes[name] = value; } else if (this.Variables.ContainsKey(name)) { Variables.Remove(name); this.Classes.Add(name, value); } else if (this.Functions.ContainsKey(name)) { Functions.Remove(name); this.Classes.Add(name, value); } else { this.Classes.Add(name, value); } }
public ScriptClassCreator(RuntimeContext closure, string name, IClassObject super) { ScriptClass sup; if (super == null) { sup = ObjectClass.Instance; } else { sup = super as ScriptClass; if (sup == null) { throw new InvalidOperationException(ExceptionResource.NotInheritable); } } Super = sup; Class = new ScriptClass(name, sup, null); Context = new FunctionContext(closure); Context.Memory.HoistingDecalreClass(Context, name); Context.Memory.DecalreClass(Context, name, Class); }
// 返回值表示是否是new出来的对象,false则为从回收列表中重复使用的对象 public IClassObject newClass(Type type, out bool isNewObject) { isNewObject = false; if (!isMainThread()) { logError("只能在主线程中使用此对象池,子线程中请使用ClassPoolThread代替"); return(null); } if (type == null) { return(null); } IClassObject obj = null; // 先从未使用的列表中查找是否有可用的对象 if (mUnusedList.TryGetValue(type, out HashSet <IClassObject> classList) && classList.Count > 0) { foreach (var item in classList) { obj = item; break; } classList.Remove(obj); isNewObject = false; } // 未使用列表中没有,创建一个新的 else { obj = createInstance <IClassObject>(type); // 创建实例时重置是为了与后续复用的实例状态保持一致 obj.resetProperty(); isNewObject = true; } obj.setAssignID(++mAssignIDSeed); obj.setDestroy(false); // 添加到已使用列表 addInuse(obj); return(obj); }
public void destroyClassReally(IClassObject classObject) { bool inuse = isInuse(classObject); mListLock.waitForUnlock(); classObject.resetProperty(); classObject.setDestroy(true); // 从已使用列表中移除 if (inuse) { // 从使用列表移除,要确保操作的都是从本类创建的实例 if (!mInusedList.Remove(classObject)) { logError("在未使用列表中找不到对象! Type: " + mType); } } // 从未使用列表中移除 else { mUnusedList.Remove(classObject); } mListLock.unlock(); }
protected bool markUsed(IClassObject classObject) { Type type = classObject.GetType(); if (!mInusedList.ContainsKey(type)) { mInusedList.Add(type, new List <IClassObject>()); } else { if (mInusedList[type].Contains(classObject)) { return(false); } } // 加入使用列表 mInusedList[type].Add(classObject); // 从未使用列表移除 if (mUnusedList.ContainsKey(type)) { mUnusedList[type].Remove(classObject); } return(true); }