Skip to content

Suggestion: option to throw exception when adding non-public types #657

@zvrba

Description

@zvrba

Minimal reproducible example for version 7.5.0 is given below.

When WithIndexer is internal, the program throws "Microsoft.ClearScript.ScriptEngineException: 'TypeError: Cannot read properties of undefined (reading 'get')'."

When WithIndexer is made public, the test program gives the expected output.

Now, this issue has bitten me a couple of times already, and every time I used ca 15 minutes to figure out what's wrong: googling around, comparing with the suggestions in old issues, trying to make small changes, etc, until it finally dawns on me: PUBLIC! I'm repeatedly stumbling over this, and it's never "obvious".

It would be nice to have an option that writes out a warning or even throws exception when an internal type is given to AddHostType or AddHostObject. It seemingly works, until it doesn't.

I'm unsure what behavior I'd like when adding an internal class that derives from a public class or implements an internal interface.

Perhaps some general event that is raised in all such cases instead would be more appropriate than logging or throwing. It'd immensely help with diagnostics.

PS: Thanks for this great project. It makes it is really easy to script a C# app with JS.

using Microsoft.ClearScript.V8;

namespace ClearScriptTest;

internal class Program
{
    static readonly V8ScriptEngine Engine = new();

    static void Main(string[] args)
    {
        Engine.AddHostType("Console", typeof(Console));
        Engine.AddHostObject("I", new WithIndexer());

        Engine.Execute(TestIndexer);
        Console.WriteLine("DONE");

    }

    static readonly string TestIndexer =
@"
Console.WriteLine(I);
Console.WriteLine(I.Item.get(0));
I.Item.set(2, -2);
Console.WriteLine(I.Item.get(2));
";
}

/*public*/ class WithIndexer
{
    private readonly int[] x = new int[10];

    public int this[int i] {
        get => x[i];
        set => x[i] = value;
    }

}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions