Exemplo n.º 1
0
 /// <summary>
 /// Replace existing scope with new one
 /// </summary>
 /// <param name="scope"></param>
 public IScriptScope CreateScope(IScriptScope scope)
 {
   if (scope.Parent != Scope)
     throw new ScriptRuntimeException(Strings.ScopeParentIsNotValid);
   Scope = scope;
   return Scope;
 }
Exemplo n.º 2
0
        public IScriptScope Create(IScriptScope parent, params object[] args)
        {
            if (args.Length == 1)
            return new ScriptUsingScope(parent, args[0]);

              throw new NotSupportedException();
        }
Exemplo n.º 3
0
        public IScriptScope Create(IScriptScope parent, params object[] args)
        {
            if (args.Length == 1)
            {
                return(new ScriptUsingScope(parent, args[0]));
            }

            throw new NotSupportedException();
        }
Exemplo n.º 4
0
        public IScriptScope Create(IScriptScope parent, params object[] args)
        {
            if (args == null || args.Length == 0)
            {
                ScriptScope result = new ScriptScope(parent);
                SetBaseItems(result);
                return(result);
            }

            throw new NotSupportedException();
        }
Exemplo n.º 5
0
 /// <summary>
 /// Remove Local Scope
 /// </summary>
 public void RemoveLocalScope()
 {
     if (Scope.Parent != null)
     {
         IScriptScope scopeToRemove = Scope;
         Scope = Scope.Parent;
         scopeToRemove.Dispose();
     }
     else
     {
         throw new Exception("Can't remove global scope, use Scope.Clean");
     }
 }
Exemplo n.º 6
0
        protected virtual void Cleanup()
        {
            IScriptScope s = Scope;

            while (s != null)
            {
                IScriptScope toRemove = s;
                s = s.Parent;
                toRemove.Dispose();
            }

            Result = null;
            Scope  = null;
        }
Exemplo n.º 7
0
        public ScriptUsingScope(IScriptScope parent, object usingObject)
            : base(parent)
        {
            if (parent == null) throw new ArgumentNullException("parent");
              if (usingObject == null) throw new ArgumentNullException("usingObject");

              Type type = usingObject as Type;
              if (type == null) type = usingObject.GetType(); ;

              IEnumerable<MethodInfo> methods = type.GetMethods(ObjectBinder.MethodFilter);
              foreach (MethodInfo method in methods)
              {
            SetItem(method.Name, new LateBoundMethod(method.Name, usingObject));
              }
        }
Exemplo n.º 8
0
            public object Invoke(IScriptContext context, object[] args)
            {
                string       id    = (string)args[0];
                IScriptScope scope = context.Scope;

                while (scope != null)
                {
                    if (scope.HasVariable(id))
                    {
                        return(true);
                    }
                    scope = scope.Parent;
                }
                return(false);
            }
Exemplo n.º 9
0
        /// <summary>
        /// Sets variable to the first scope in hierarchy which already has this variable
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        internal static void SetToParentScope(IScriptScope parent, string id, object value)
        {
            var scope = parent;

            while (scope != null)
            {
                if (scope.HasVariable(id))
                {
                    scope.SetItem(id, value);
                    return;
                }
                scope = scope.Parent;
            }

            throw new ScriptIdNotFoundException(string.Format(Strings.GlobalNameNotFound, id));
        }
Exemplo n.º 10
0
        private void ScopeBeforeSetItem(IScriptScope sender, ScopeArgs args)
        {
            //TODO: Performance improvement. Should be evaluated once per function call
            List <string> globalNames = GetGlobalNames(activeContext);

            if (globalNames.Contains(args.Name))
            {
                SetToParentScope(sender.Parent, args.Name, args.Value);
                args.Cancel = true;
            }

            //if (!sender.HasVariable(args.Name))
            //{
            //  args.Cancel = SetToParentScope(sender.Parent, args.Name, args.Value);
            //}
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sets variable to the first scope in hierarchy which already has this variable
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static void SetToParentScope(IScriptScope parent, string id, object value)
        {
            IScriptScope scope = parent;

            while (scope != null)
            {
                if (scope.HasVariable(id))
                {
                    scope.SetItem(id, value);
                    return;
                }
                scope = scope.Parent;
            }

            throw new ScriptIdNotFoundException(string.Format("Global name {0} was not found in scopes", id));
            //parent.SetItem(id, value);
        }
Exemplo n.º 12
0
        private void ScopeBeforeSetItem(IScriptScope sender, ScopeArgs args)
        {
            if (_globalNames.Contains(args.Name))
            {
                switch (args.Operation)
                {
                case ScopeOperation.Set:
                    ScriptQualifiedName.SetToParentScope(sender.Parent, args.Name, args.Value);
                    args.Cancel = true;
                    break;

                case ScopeOperation.Create:
                    throw new ScriptExecutionException(string.Format(Strings.LocalIdConflictWithGlobalList, args.Name));

                default:
                    break;
                }
            }
        }
Exemplo n.º 13
0
    public ScriptUsingScope(IScriptScope parent, object usingObject):
        base(parent)
    {
      if (parent == null) throw new ArgumentNullException("parent");
      if (usingObject == null) throw new ArgumentNullException("usingObject");
      _usingObject = usingObject;

      var type = usingObject as Type ?? usingObject.GetType();

      var methods = type.GetMethods(ObjectBinding.MethodFilter);
      foreach (var method in methods.Where(method => !_bindings.ContainsKey(method.Name)))
        _bindings.Add(method.Name, new DelayedMethodBinding(method.Name, usingObject));

      var properties = type.GetProperties(ObjectBinding.PropertyFilter);     
      foreach (var property in properties.Where(property => !_members.ContainsKey(property.Name)))
        _members.Add(property.Name, RuntimeHost.Binder.BindToMember(usingObject, property.Name, true));

      var fields = type.GetFields(ObjectBinding.FieldFilter);
      foreach (var field in fields.Where(field => !_members.ContainsKey(field.Name)))
        _members.Add(field.Name, RuntimeHost.Binder.BindToMember(usingObject, field.Name, true));
    }
Exemplo n.º 14
0
        public void SimpleFunctionScope()
        {
            ScriptContext context = new ScriptContext();
            IScriptScope  scope   = context.Scope;

            Script.RunCode(@"
        a = 4; b = 2; c = 3;
        function test1(a,b)
          global(c)
        {
          c = a+b;
          a = 15;
        }

        test1(2,3);", context);

            Assert.Equal(scope, context.Scope);
            Assert.Equal(5, context.GetItem("c", true));
            Assert.Equal(4, context.GetItem("a", true));
            Assert.Equal(2, context.GetItem("b", true));
        }
Exemplo n.º 15
0
        //TODO: Refactor
        public override void Evaluate(IScriptContext context)
        {
            if (ChildNodes.Count == 0)
            {
                return;
            }

            //Create local scope
            if (ShouldCreateScope)
            {
                IScriptScope scope = RuntimeHost.ScopeFactory.Create(ScopeTypes.Local, context.Scope);
                context.CreateScope(scope);
            }

            try
            {
                int index = 0;
                while (index < ChildNodes.Count)
                {
                    var node = (ScriptAst)ChildNodes[index];
                    node.Evaluate(context);

                    if (context.IsBreak() || context.IsReturn() || context.IsContinue())
                    {
                        break;
                    }

                    index++;
                }
            }
            finally
            {
                if (ShouldCreateScope)
                {
                    context.RemoveLocalScope();
                }
            }
        }
Exemplo n.º 16
0
        private object GetIndentifierValue(IScriptContext context, string identifier)
        {
            //object result = context.GetItem(identifier, false);
            //if (result != RuntimeHost.NoVariable) return result;

            if (IsGlobal)
            {
                _variable = null;
                IScriptScope scope = context.Scope.Parent;
                while (scope != null)
                {
                    if (scope.HasVariable(identifier))
                    {
                        return(scope.GetItem(identifier, true));
                    }
                    scope = scope.Parent;
                }
            }
            else
            {
                if (_variable != null && _variable.Value != null)
                {
                    return(_variable.Value);
                }

                object result;
                _variable = CreateRef(identifier, context, true, out result);

                if (result != RuntimeHost.NoVariable)
                {
                    return(result);
                }
            }

            return(RuntimeHost.HasType(identifier)
               ? (object)RuntimeHost.GetType(identifier)
               : NamespaceResolver.Get(identifier));
        }
Exemplo n.º 17
0
        public ScriptUsingScope(IScriptScope parent, object usingObject) :
            base(parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (usingObject == null)
            {
                throw new ArgumentNullException("usingObject");
            }
            _usingObject = usingObject;

            var type = usingObject as Type ?? usingObject.GetType();

            var methods = type.GetMethods(ObjectBinding.MethodFilter);

            foreach (var method in methods.Where(method => !_bindings.ContainsKey(method.Name)))
            {
                _bindings.Add(method.Name, new DelayedMethodBinding(method.Name, usingObject));
            }

            var properties = type.GetProperties(ObjectBinding.PropertyFilter);

            foreach (var property in properties.Where(property => !_members.ContainsKey(property.Name)))
            {
                _members.Add(property.Name, RuntimeHost.Binder.BindToMember(usingObject, property.Name, true));
            }

            var fields = type.GetFields(ObjectBinding.FieldFilter);

            foreach (var field in fields.Where(field => !_members.ContainsKey(field.Name)))
            {
                _members.Add(field.Name, RuntimeHost.Binder.BindToMember(usingObject, field.Name, true));
            }
        }
Exemplo n.º 18
0
 public IScriptScope Create(IScriptScope parent, params object[] args)
 {
   return new LocalScope(parent);
 }
Exemplo n.º 19
0
    /// <summary>
    /// Sets variable to the first scope in hierarchy which already has this variable
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="id"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    internal static void SetToParentScope(IScriptScope parent, string id, object value)
    {
      var scope = parent;
      while (scope != null)
      {
        if (scope.HasVariable(id))
        {
          scope.SetItem(id, value);
          return;
        }
        scope = scope.Parent;
      }

      throw new ScriptIdNotFoundException(string.Format(Strings.GlobalNameNotFound, id));
    }
Exemplo n.º 20
0
 public LocalScope(IScriptScope parent):
   base(parent)
 {
   if (parent == null)
     throw new NotSupportedException("Can't create stand-alone local scope");
 }
Exemplo n.º 21
0
 public ScopeValueReference(IScriptScope scope, string id)
 {
   Scope = scope;
   Id = id;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Creates a new Function scope
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="args">arguments are ignored</param>
 /// <returns>a new instance  of FunctionScope</returns>
 public IScriptScope Create(IScriptScope parent, params object[] args)
 {
   return new FunctionScope(parent);
 }
Exemplo n.º 23
0
    public IScriptScope Create(IScriptScope parent, params object[] args)
    {
      if (args == null || args.Length == 0)
      {
        ScriptScope result = new ScriptScope(parent);
        SetBaseItems(result);
        return result;
      }

      throw new NotSupportedException();
    }
Exemplo n.º 24
0
 public ScriptInterpreter(GameContext gameContext)
 {
     _gameContext = gameContext;
     Global       = GetOrCreate(nameof(Global));
 }
Exemplo n.º 25
0
        /// <summary>
        /// Sets variable to the first scope in hierarchy which already has this variable
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static void SetToParentScope(IScriptScope parent, string id, object value)
        {
            IScriptScope scope = parent;
              while (scope != null)
              {
            if (scope.HasVariable(id))
            {
              scope.SetItem(id, value);
              return;
            }
            scope = scope.Parent;
              }

              throw new ScriptIdNotFoundException(string.Format("Global name {0} was not found in scopes", id));
              //parent.SetItem(id, value);
        }
Exemplo n.º 26
0
        private void ScopeBeforeSetItem(IScriptScope sender, ScopeArgs args)
        {
            //TODO: Performance improvement. Should be evaluated once per function call
              List<string> globalNames = GetGlobalNames(activeContext);

              if (globalNames.Contains(args.Name))
              {
            SetToParentScope(sender.Parent, args.Name, args.Value);
            args.Cancel = true;
              }

              //if (!sender.HasVariable(args.Name))
              //{
              //  args.Cancel = SetToParentScope(sender.Parent, args.Name, args.Value);
              //}
        }
Exemplo n.º 27
0
 public IScriptScope Create(ScopeTypes id, IScriptScope parent)
 {
     return(Create((int)id, parent));
 }
Exemplo n.º 28
0
 public IScriptScope Create(ScopeTypes id, IScriptScope parent, params object[] args)
 {
     return(Create((int)id, parent, args));
 }
Exemplo n.º 29
0
 public IScriptScope Create(int id, IScriptScope parent, params object[] args)
 {
     return(scopeTypes[id].Create(parent, args));
 }
Exemplo n.º 30
0
 /// <summary>
 /// Replace existing scope with new one
 /// </summary>
 /// <param name="scope"></param>
 public void CreateScope(IScriptScope scope)
 {
     if (scope.Parent != Scope)
     throw new ScriptException("Wrong scope structure");
       Scope = scope;
 }
Exemplo n.º 31
0
 public ScopeValueReference(IScriptScope scope, string id)
 {
     Scope = scope;
     Id    = id;
 }
 public IScriptScope Create(IScriptScope parent, params object[] args)
 {
     return(new ScriptScope(parent));
 }
Exemplo n.º 33
0
 public IScriptScope Create(ScopeTypes id, IScriptScope parent)
 {
   return Create((int)id, parent);
 }
Exemplo n.º 34
0
 public ScriptScope(IScriptScope parent)
 {
   Parent = parent;
 }
Exemplo n.º 35
0
 public IScriptScope Create(ScopeTypes id, IScriptScope parent, params object[] args)
 {
   return Create((int)id, parent, args);
 }
Exemplo n.º 36
0
 /// <summary>
 /// Default Constructor
 /// </summary>
 public FunctionScope(IScriptScope parent)
   : base(parent)
 {
 }
Exemplo n.º 37
0
 public IScriptScope Create(int id, IScriptScope parent, params object[] args)
 {
   return scopeTypes[id].Create(parent, args);
 }
Exemplo n.º 38
0
 public ScriptScope(IScriptScope parent)
 {
     Parent = parent;
 }
Exemplo n.º 39
0
 /// <summary>
 /// Default Constructor
 /// </summary>
 public FunctionScope(IScriptScope parent)
     : base(parent)
 {
 }