/// <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; }
public IScriptScope Create(IScriptScope parent, params object[] args) { if (args.Length == 1) return new ScriptUsingScope(parent, args[0]); throw new NotSupportedException(); }
public IScriptScope Create(IScriptScope parent, params object[] args) { if (args.Length == 1) { return(new ScriptUsingScope(parent, args[0])); } throw new NotSupportedException(); }
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(); }
/// <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"); } }
protected virtual void Cleanup() { IScriptScope s = Scope; while (s != null) { IScriptScope toRemove = s; s = s.Parent; toRemove.Dispose(); } Result = null; Scope = null; }
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)); } }
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); }
/// <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)); }
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); //} }
/// <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); }
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; } } }
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)); }
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)); }
//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(); } } }
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)); }
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)); } }
public IScriptScope Create(IScriptScope parent, params object[] args) { return new LocalScope(parent); }
public LocalScope(IScriptScope parent): base(parent) { if (parent == null) throw new NotSupportedException("Can't create stand-alone local scope"); }
public ScopeValueReference(IScriptScope scope, string id) { Scope = scope; Id = id; }
/// <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); }
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(); }
public ScriptInterpreter(GameContext gameContext) { _gameContext = gameContext; Global = GetOrCreate(nameof(Global)); }
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); //} }
public IScriptScope Create(ScopeTypes id, IScriptScope parent) { return(Create((int)id, parent)); }
public IScriptScope Create(ScopeTypes id, IScriptScope parent, params object[] args) { return(Create((int)id, parent, args)); }
public IScriptScope Create(int id, IScriptScope parent, params object[] args) { return(scopeTypes[id].Create(parent, args)); }
/// <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; }
public IScriptScope Create(IScriptScope parent, params object[] args) { return(new ScriptScope(parent)); }
public IScriptScope Create(ScopeTypes id, IScriptScope parent) { return Create((int)id, parent); }
public ScriptScope(IScriptScope parent) { Parent = parent; }
public IScriptScope Create(ScopeTypes id, IScriptScope parent, params object[] args) { return Create((int)id, parent, args); }
/// <summary> /// Default Constructor /// </summary> public FunctionScope(IScriptScope parent) : base(parent) { }
public IScriptScope Create(int id, IScriptScope parent, params object[] args) { return scopeTypes[id].Create(parent, args); }