JavascriptNet / Javascript.Net

.Net bindings to the V8 JavaScript engine
BSD 2-Clause "Simplified" License
830 stars 150 forks source link

Proper prototype handling #60

Open Ablu opened 5 years ago

Ablu commented 5 years ago

Problem

Currently JavaScript.Net does not fully support prototype related use cases:

While it is possible to register a function which creates a new object like this:

_context.SetParameter("TestClass", new Func<TestClass>(() => new TestClass()));

_context.Run(@"
    let c = new TestClass();
    c.foo()").Should().Be(true);

problems with this approach:

Things to consider for the solution

Solution proposal

Side notes

This could also improve performance of wrapping objects since the analysis of type is only required once, repetitive passing could become a lot faster.


/cc @spahnke

@oliverbock do you think the proposal above is sane? Then I would start to implement it.

oliverbock commented 5 years ago

Sounds hard to implement, but useful. What will ConstructorInfo contain? Can you distinguish between the C# class instance (maps to the JavaScript object) and the C# method definitions (maps to the JavaScript object's prototype)?

Ablu commented 5 years ago

With ConstructorInfo I meant https://docs.microsoft.com/en-us/dotnet/api/system.reflection.constructorinfo?view=netframework-4.7.2. So I get full access to the type via DeclaringType.

Ablu commented 5 years ago

I will break it into small self-contained tasks:

  1. [x] change the current wrapping logic to be prototype based
  2. [x] reuse prototypes for same types
  3. [x] add actual support for JS called constructors

This hopefully keeps the individual PRs reviewable and small(er)

Ablu commented 5 years ago

It was simpler than anticipated before. The linked PR (#71) should fix/add this. The only thing which might need more work is inheritance on .NET side... However, I guess that could be handled as a separate issue (not sure whether we need it at the moment)?

Ablu commented 5 years ago

Also we just realized that overloaded constructors would need additional support