You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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",newFunc<TestClass>(()=>newTestClass()));_context.Run(@" let c = new TestClass(); c.foo()").Should().Be(true);
problems with this approach:
instanceOf checks are not working. c instanceof TestClass is false
Prototype extensions are not working (TestClass.prototype.newMethod = function() {...})
Things to consider for the solution
Objects which are created by C# code should also be able to pass to JavaScript under an existing prototype
Fine grained control about which types are constructable by JS is required
Solution proposal
add method like SetConstructor(ConstructorInfo constructor) to the context which generates a prototype and registers under the name
when wrapping an object:
lookup whether a prototype already exists for the type
if no prototype is known: generate a prototype (but do not make it available to the context under a name)
wrap object by using the newly generated or existing prototype
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.
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)?
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)?
Activity
oliverbock commentedon Oct 5, 2018
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 commentedon Oct 5, 2018
With
ConstructorInfoI 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 viaDeclaringType.Ablu commentedon Oct 5, 2018
I will break it into small self-contained tasks:
This hopefully keeps the individual PRs reviewable and small(er)
WIP: Add support for registered constructors
Add support for registered constructors
Ablu commentedon Jan 29, 2019
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 commentedon Jan 29, 2019
Also we just realized that overloaded constructors would need additional support
Add support for registered constructors
Add support for registered constructors
Add support for registered constructors
Add support for registered constructors
Add support for registered constructors