/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/ScriptEngine/Machine/Contexts/AutoContext.cs
157 строк
5 KB
EvilBeaver
Полностью удалены API без процесса
19 май 2025, 09:56
19 май 2025, 09:56
c6b37cd
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System.Collections.Generic; using System.Diagnostics; using OneScript.Contexts; using OneScript.Exceptions; using OneScript.Execution; using OneScript.Types; using ScriptEngine.Types; namespace ScriptEngine.Machine.Contexts { public abstract class AutoContext<TInstance> : PropertyNameIndexAccessor where TInstance : AutoContext<TInstance> { private static readonly ContextPropertyMapper<TInstance> _properties = new ContextPropertyMapper<TInstance>(); private static readonly ContextMethodsMapper<TInstance> _methods = new ContextMethodsMapper<TInstance>(); private static readonly HashSet<int> _warnedDeprecatedMethods = new HashSet<int>(); private static readonly TypeDescriptor _objectType = typeof(TInstance).GetTypeFromClassMarkup(); protected AutoContext() : base(_objectType) { } protected AutoContext(TypeDescriptor assignedType) : base(assignedType) { } public override bool IsPropReadable(int propNum) { return _properties.GetProperty(propNum).CanRead; } public override bool IsPropWritable(int propNum) { return _properties.GetProperty(propNum).CanWrite; } public override int GetPropertyNumber(string name) { return _properties.FindProperty(name); } public override IValue GetPropValue(int propNum) { try { return _properties.GetProperty(propNum).Getter((TInstance)this); } catch (System.Reflection.TargetInvocationException e) { Debug.Assert(e.InnerException != null); throw e.InnerException; } } public override void SetPropValue(int propNum, IValue newVal) { try { _properties.GetProperty(propNum).Setter((TInstance)this, newVal); } catch (System.Reflection.TargetInvocationException e) { Debug.Assert(e.InnerException != null); throw e.InnerException; } } public override int GetPropCount() { return _properties.Count; } public override string GetPropName(int propNum) { return _properties.GetProperty(propNum).Name; } public override int GetMethodNumber(string name) { return _methods.FindMethod(name); } public override int GetMethodsCount() { return _methods.Count; } public override BslMethodInfo GetMethodInfo(int methodNumber) { return _methods.GetRuntimeMethod(methodNumber); } public override BslPropertyInfo GetPropertyInfo(int propertyNumber) { return _properties.GetProperty(propertyNumber).PropertyInfo; } protected ContextPropertyMapper<TInstance> PropertyMapper => _properties; protected ContextMethodsMapper<TInstance> MethodMapper => _methods; private void CheckIfCallIsPossible(int methodNumber, IValue[] arguments) { var methodInfo = _methods.GetRuntimeMethod(methodNumber) as ContextMethodInfo; if(methodInfo == null) return; if (!methodInfo.IsDeprecated) { return; } if (methodInfo.IsForbiddenToUse) { throw RuntimeException.DeprecatedMethodCall(methodInfo.Name); } if (_warnedDeprecatedMethods.Contains(methodNumber)) { return; } SystemLogger.Write($"ВНИМАНИЕ! Вызов устаревшего метода {methodInfo.Name}"); _warnedDeprecatedMethods.Add(methodNumber); } public override void CallAsProcedure(int methodNumber, IValue[] arguments, IBslProcess process) { CheckIfCallIsPossible(methodNumber, arguments); try { _methods.GetCallableDelegate(methodNumber)((TInstance)this, arguments, process); } catch (System.Reflection.TargetInvocationException e) { Debug.Assert(e.InnerException != null); throw e.InnerException; } } public override void CallAsFunction(int methodNumber, IValue[] arguments, out IValue retValue, IBslProcess process) { CheckIfCallIsPossible(methodNumber, arguments); try { retValue = _methods.GetCallableDelegate(methodNumber)((TInstance)this, arguments, process); } catch (System.Reflection.TargetInvocationException e) { Debug.Assert(e.InnerException != null); throw e.InnerException; } } } }