/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
v1.9.4
src/ScriptEngine/Machine/Variables.cs
302 строки
8 KB
Mr-Rm
fix #1590,#1429: обработка ошибки доступа по индексу при передаче параметра
27 сен 2025, 19:11
27 сен 2025, 19:11
8f21b90
Код
Авторство
О чём код?
/*---------------------------------------------------------- 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; namespace ScriptEngine.Machine { public interface IVariable : IValue { IValue Value { get; set; } string Name { get; } } public class Variable : IVariable { private IValue _val; public string Name { get; private set; } private Variable() { } #region Factory public static IVariable Create(IValue val, string symbol) { return new Variable() { _val = val, Name = symbol }; } public static IVariable Create(IValue val, VariableInfo metadata) { return new Variable() { _val = val, Name = metadata.Identifier }; } public static IVariable CreateReference(IVariable variable, string refName) { if (variable is VariableReference vref && vref.isIndexed()) { _ = vref.Value; // проверить правильность индекса через его чтение } return new VariableReference(variable, refName); } public static IVariable CreateContextPropertyReference(IRuntimeContextInstance context, int propertyNumber, string refName) { return new VariableReference(context, propertyNumber, refName); } public static IVariable CreateIndexedPropertyReference(IRuntimeContextInstance context, IValue index, string refName) { return new VariableReference(context, index, refName); } #endregion #region IVariable Members public IValue Value { get { return _val; } set { _val = value; } } #endregion #region IValue Members public DataType DataType { get { return Value.DataType; } } public TypeDescriptor SystemType { get { return Value.SystemType; } } public decimal AsNumber() { return Value.AsNumber(); } public DateTime AsDate() { return Value.AsDate(); } public bool AsBoolean() { return Value.AsBoolean(); } public string AsString() { return Value.AsString(); } public IRuntimeContextInstance AsObject() { return Value.AsObject(); } public IValue GetRawValue() { return Value; } #endregion #region IComparable<IValue> Members public int CompareTo(IValue other) { return Value.CompareTo(other); } #endregion #region IEquatable<IValue> Members public bool Equals(IValue other) { return Value.Equals(other); } #endregion #region Reference private class VariableReference : IVariable { private readonly ReferenceType _refType; private readonly IVariable _referencedValue; private readonly IRuntimeContextInstance _context; private readonly int _contextPropertyNumber; private readonly IValue _index; public string Name { get; private set; } public bool isIndexed() => _refType == ReferenceType.IndexedProperty; public VariableReference(IVariable var, string name) { _refType = ReferenceType.Simple; _referencedValue = var; Name = name; } public VariableReference(IRuntimeContextInstance context, int propertyNumber, string name) { _refType = ReferenceType.ContextProperty; _context = context; _contextPropertyNumber = propertyNumber; Name = name; } public VariableReference(IRuntimeContextInstance context, IValue index, string name) { _refType = ReferenceType.IndexedProperty; _context = context; _index = index; Name = name; } private enum ReferenceType { Simple, ContextProperty, IndexedProperty } #region IVariable Members public IValue Value { get { if (_refType == ReferenceType.Simple) { return _referencedValue.Value; } else if (_refType == ReferenceType.ContextProperty) { if (_context.IsPropReadable(_contextPropertyNumber)) return _context.GetPropValue(_contextPropertyNumber); else throw RuntimeException.PropIsNotReadableException(""); } else { return _context.GetIndexedValue(_index); } } set { if (_refType == ReferenceType.Simple) { _referencedValue.Value = value; } else if (_refType == ReferenceType.ContextProperty) { if(_context.IsPropWritable(_contextPropertyNumber)) _context.SetPropValue(_contextPropertyNumber, value); else throw RuntimeException.PropIsNotWritableException(""); } else { _context.SetIndexedValue(_index, value); } } } #endregion #region IValue Members public DataType DataType { get { return Value.DataType; } } public TypeDescriptor SystemType { get { return Value.SystemType; } } public decimal AsNumber() { return Value.AsNumber(); } public DateTime AsDate() { return Value.AsDate(); } public bool AsBoolean() { return Value.AsBoolean(); } public string AsString() { return Value.AsString(); } public IRuntimeContextInstance AsObject() { return Value.AsObject(); } public IValue GetRawValue() { return Value.GetRawValue(); } #endregion #region IComparable<IValue> Members public int CompareTo(IValue other) { return Value.CompareTo(other); } #endregion #region IEquatable<IValue> Members public bool Equals(IValue other) { return Value.Equals(other); } #endregion } #endregion } }