How does ScriptEngin.AccessContext work? #328
-
Hi @ClearScriptLib ,
engineWithApiSet1.AccessContext = typeof(ApiSet1); But I was confused by the test methods in "AccessContextTest" So here are my questions: How could code like " engine.Evaluate("new AccessContextTestBase()")" would fail? I mean, the default engine.AccessContext is the default "global" context isn't? The next questions is that: Please help me to understand the magic here :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Another question would be, how does "engine.AccessContext = typeof(SomeContext)" affect the default global context, are those types/objects added by "AddHostType/AddHostObject" still accessible/importable in script after the new context is assigned? |
Beta Was this translation helpful? Give feedback.
-
Hi @nextfool,
For script code to use a host resource, that resource must be both exposed and accessible. As you've noted,
By default,
Within that test method, In other words, that statement empowers script code to use the same resources as managed code within After that statement,
Again, Please send any additional questions our way. Thank you! 1 Yes, putting a test class into the core assembly is ugly, but it's the only way we could test certain things without creating yet another project. |
Beta Was this translation helpful? Give feedback.
Hi @nextfool,
For script code to use a host resource, that resource must be both exposed and accessible. As you've noted,
TestInitialize
exposesAccessContextTestBase
. However, initially, that class isn't accessible, because it's internal to the ClearScript.Core assembly1.By default,
AccessContext
isnull
, which means that a host resource is accessible only if it has public visibility.