using System; using System.Collections.Generic; using System.Text; using ScriptNET.Ast; namespace ScriptNET.Runtime { /// /// Scope with contracts on variables /// public class FunctionScope : ScriptScope, INotifyingScope { #region Fields public bool SearchHierarchy { get; set; } #endregion #region Constructors /// /// Default Constructor /// public FunctionScope(IScriptScope parent) : base(parent) { } #endregion #region Methods public override object GetItem(string id, bool throwException) { ScopeArgs args = new ScopeArgs(id, RuntimeHost.NullValue); OnBeforeGetItem(args); if (args.Cancel) { if (args.Value != RuntimeHost.NullValue) { return args.Value; } else { throw new ScriptIdNotFoundException(id); } } args.Value = base.GetItem(id, throwException); OnAfterGetItem(args); if (args.Cancel) { throw new ScriptException("Canceled by event-handler"); } return args.Value; } public override void SetItem(string id, object value) { ScopeArgs args = new ScopeArgs(id, value); OnBeforeSetItem(args); if (args.Cancel) return; base.SetItem(id, args.Value); OnAfterSetItem(args); if (args.Cancel) { throw new ScriptException("Canceled by event-handler"); } } #endregion #region INotifyingScope Members protected virtual void OnBeforeGetItem(ScopeArgs args) { if (BeforeGetItem != null) BeforeGetItem(this, args); } protected virtual void OnAfterGetItem(ScopeArgs args) { if (AfterGetItem != null) AfterGetItem(this, args); } protected virtual void OnBeforeSetItem(ScopeArgs args) { if (BeforeSetItem!=null) BeforeSetItem(this, args); } protected virtual void OnAfterSetItem(ScopeArgs args) { if (AfterSetItem != null) AfterSetItem(this, args); } /// /// Event raised before performing getting item, allowing to /// cancel action or replace actual value /// public event ScopeSetEvent BeforeGetItem; /// /// Raised after performing get item action, allowing to replace /// resulting value or cancel action. Cancelling will raise ScriptException /// public event ScopeSetEvent AfterGetItem; /// /// Event raised before performing setting item action, allowing to /// cancel it or replace actual value /// public event ScopeSetEvent BeforeSetItem; /// /// Raised after performing set item action, allowing to cancel action. /// Cancelling will raise ScriptException /// public event ScopeSetEvent AfterSetItem; #endregion } /// /// Default activator for a FunctionScope. May be replaced in xml file configuration /// by custom implementation /// public class FunctionScopeActivator : IScopeActivator { #region IScopeActivator Members /// /// Creates a new Function scope /// /// /// arguments are ignored /// a new instance of FunctionScope public IScriptScope Create(IScriptScope parent, params object[] args) { return new FunctionScope(parent); } #endregion } }