using System; using System.Collections.Generic; using System.Text; namespace ScriptNET.Runtime { /// /// Base implementation of ScriptContext object /// public class ScriptContext : IScriptContext { #region Properties private IScriptScope scope; /// /// Scope object /// public IScriptScope Scope { get { return scope; } private set { scope = value; } } /// /// Script source code. Should be provided before executing the script /// public string SourceCode { get; private set; } private ContextFlags flags = ContextFlags.Empty; bool IsSkip { get { return IsBreak() || IsContinue() || IsReturn(); } } private object result; /// /// Script Result object /// public object Result { get { return result; } set { result = value; } } #endregion #region Constructors /// /// Creates new Script Context with Default scope /// public ScriptContext() { CreateScope(); } #endregion #region Scope /// /// Creates new default nested scope /// public void CreateScope() { Scope = RuntimeHost.ScopeFactory.Create(ScopeTypes.Default, Scope); } /// /// Replace existing scope with new one /// /// public void CreateScope(IScriptScope scope) { if (scope.Parent != Scope) throw new ScriptException("Wrong scope structure"); Scope = scope; } /// /// Remove Local Scope /// public void RemoveLocalScope() { if (Scope.Parent != null) Scope = Scope.Parent; else throw new Exception("Can't remove global scope, use Scope.Clean"); } #region IScriptScope public object GetItem(string id, bool throwException) { return Scope.GetItem(id, throwException); } public void SetItem(string id, object value) { Scope.SetItem(id, value); } #endregion #endregion #region Break-Continue-Return /// /// Set return state of run-time /// /// true or false public void SetReturn(bool val) { if (val && IsContinue()) throw new ScriptException("Implementation: Implementation error, consult with developer"); if (val && IsBreak()) throw new ScriptException("Implementation: Implementation error, consult with developer"); if (val) flags = flags | ContextFlags.Return; else flags = flags & ~ContextFlags.Return; } /// /// Set break state of run-time /// /// true or false public void SetBreak(bool val) { if (val && IsContinue()) throw new ScriptException("Implementation: Implementation error, consult with developer"); if (val) flags = flags | ContextFlags.Break; else flags = flags & ~ContextFlags.Break; } /// /// Set continue state of run-time /// /// true or false public void SetContinue(bool val) { if (val && IsBreak()) throw new ScriptException("Implementation: Implementation error, consult with developer"); if (val) flags = flags | ContextFlags.Continue; else flags = flags & ~ContextFlags.Continue; } /// /// Return state /// /// true or false public bool IsReturn() { return (flags & ContextFlags.Return) == ContextFlags.Return; } /// /// Break state /// /// true or false public bool IsBreak() { return (flags & ContextFlags.Break) == ContextFlags.Break; } /// /// Continue state /// /// true or false public bool IsContinue() { return (flags & ContextFlags.Continue) == ContextFlags.Continue; } #endregion #region Function Defs /// /// Finds function definition in current scope /// /// function name /// IInvokable object public IInvokable GetFunctionDefinition(string name) { return scope.GetFunctionDefinition(name); } #endregion } /// /// Specify context state /// [Flags()] public enum ContextFlags { /// /// Initial state /// Empty = 0, /// /// Brake operator executed /// Break = 2, /// /// Continue operator executed /// Continue = 4, /// /// Return statement executed /// Return = 8 } }