Taritsyn / JavaScriptEngineSwitcher

JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines. This library allows you to quickly and easily switch to using of another JavaScript engine.
Apache License 2.0
439 stars 49 forks source link

Checks whether supports a .NET type by Switcher is not appropriate #114

Closed zhoupan closed 7 months ago

zhoupan commented 7 months ago

It is more appropriate to let the specific engine decide whether supports a .NET type. see: https://microsoft.github.io/ClearScript/Examples/Examples.html

// create a script engine
using (var engine = new V8ScriptEngine())
{
    // expose a host type
    engine.AddHostType("Console", typeof(Console));
    engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");
    // expose a host object
    engine.AddHostObject("random", new Random());
    engine.Execute("Console.WriteLine(random.NextDouble())");
    // expose entire assemblies
    engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
    engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");
}
/// <summary>
/// Validation helpers
/// </summary>
public static class ValidationHelpers
{
    /// <summary>
    /// List of supported types
    /// </summary>
    private static readonly Type[] _supportedTypes =
    {
        typeof(Undefined), typeof(Boolean), typeof(Int32), typeof(Double), typeof(String)
    };
}
Taritsyn commented 7 months ago

Hello, Zhoupan!

Have you tried the EmbedHostObject and EmbedHostType methods? EmbedHostObject method only have an explicit restriction for the DateTime type.

It is more appropriate to let the specific engine decide whether supports a .NET type.

Remember the main purpose of this library:

JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines (…). This library allows you to quickly and easily switch to using of another JavaScript engine.

zhoupan commented 7 months ago

Thank you for your help. EmbedHostObject and EmbedHostType works.