Script context

SSharp S# API

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

Script Context is a part of script that stores run-time information, such as: scopes, variables inside scopes, flags, functions, etc. With a help of Script Context host application may expose .NET objects for script. Script Context is a point of interoperability between runtime and host application.

Script runtime ensure that context is passed across all executable parts of script. For example, each user defined function implementing IInvokable interface will be supplied with an instance of context (as a first parameter) during its execution.

Context Switching¶

Run-time ensures that there is one-to-one relation between pre-compiled script instance and script context.

      Script s1 = Script.Compile("return A;");

      Script s2 = Script.Compile("return B;");


      s1.Contex = s2.Context;

      //After this statement s2.Context will be equal to null


Following scenario is possible: 


      Script s = Script.Compile("return A;");


      ScriptContext sc = new ScriptContext();

      sc.SetItem("A", 1);

      s.Context = sc;      


      ScriptContext sc1 = new ScriptContext();

      sc.SetItem("A", 10);

      s.Context = sc1;



Note: during context switching all references created with Context.Ref method will be cleared. Context.Ref mainly used by runtime to cache references and improve performance. Avoid using this function.