The purpose of the script Scope is to:
- Resolve variables: associate value with name, return value by given name;
- Create reference to variable for fast access to value;
- Associate name with IInvokable object (function)
Scopes in S# forms a hierarchy:
There are different types of scopes: global, local, function scope, using statement scope, and events scope. All of them are used to resolve names: either variable's, type's, function's name or method's and properties' names in of certain object in case of using statement. It is possible to inherit and implement a Scope class to introduce custom behavior of name resolution. Before script execution the user can add objects,types and functions into Script's scope, so they will be available for script. For example:
List<int> vals = new List<int>();
vals.AddRange(new int[] { 1, 2, 3, 4 });
Script script = Script.Compile(@"
rez = 0;
foreach (number in numbers)
rez += number;"
);
//Adding variable to script's scope
script.Context.SetItem("numbers", vals);
object rez = script.Execute();
Console.WriteLine(rez);