Skip to content

Proper prototype handling #60

Open

Description

@Ablu

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:

  • 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.


/cc @spahnke

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

Activity

  1. oliverbock commented on Oct 5, 2018

    @oliverbock
    Contributor

    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)?

  2. Ablu commented on Oct 5, 2018

    @Ablu
    CollaboratorAuthor

    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.

  3. Ablu commented on Oct 5, 2018

    @Ablu
    CollaboratorAuthor

    I will break it into small self-contained tasks:

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

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

  4. Ablu commented on Jan 29, 2019

    @Ablu
    CollaboratorAuthor

    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)?

  5. Ablu commented on Jan 29, 2019

    @Ablu
    CollaboratorAuthor

    Also we just realized that overloaded constructors would need additional support

  6. added a commit that references this issue on Jan 29, 2019
  7. added a commit that references this issue on Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions