This project has moved and is read-only. For the latest updates, please go here.

What the AccessContext is for and why V8ScriptEngine with name?

Nov 8, 2013 at 12:21 PM
Torsten (user zimmybln) posted the following question:

Hello ClearScript,

Two question about object design and purpose:

1.) What the property AccessContext of the V8ScriptEngine is for? It looks promising but I can't see any usage of this at the moment.

2.) Why the V8ScriptEngine Type has got a constructor with name? I can't see that I can use it anytime.

With best regards,
Nov 8, 2013 at 1:29 PM
Edited Nov 8, 2013 at 5:22 PM
Hi Torsten,

AccessContext may be useful for exposing non-public resources. From the ClearScript Library Reference: By setting this property to a type you declare that script code running in the current script engine is to be treated as if it were part of that type's implementation. Doing so does not expose any host resources to script code, but it affects which host resources are importable and which members of exposed resources are accessible.

Consider this sample app:
using System;
using Microsoft.ClearScript.V8;

namespace Test
    internal class Foo
        public string Bar() { return "Qux"; }

    public static class TestApp
        public static void Main()
            using (var engine = new V8ScriptEngine())
                engine.AccessContext = typeof(TestApp);
                engine.AddHostObject("foo", new Foo());
Without the AccessContext assignment, Bar would not be accessible to script code. In general we recommend that you avoid this property. ClearScript does not have a robust solution for exposing non-public resources. However, if that's a requirement in your scenario, AccessContext and/or HostItemFlags.PrivateAccess may be sufficient.

As for Name, that's really just for diagnostic purposes. For example, script debuggers such as Visual Studio and Eclipse sometimes display the name in their user interfaces and logs.

Good luck!