Skip to content

Can I extend BrowserWindow? #23

Closed
Closed

Description

@probablycorey

I am trying to make AtomWindow inherit from BrowserWindow but run into the error "require constructor call" which is thrown from atom-shell. Is it possible to do this?

The code I used is on the atom/inherit-browser-window branch.

Activity

  1. zcbenz commented on Jun 18, 2013

    @zcbenz
    Contributor

    A typical example of inheriting native class is:

    class AtomWindow extends BrowserWindow
      constructor: ->
        # blabla...
        super

    To make it work, the generated constructor should return the result of base class's constructor, like this:

    function AtomWindow() {
      return AtomWindow.__super__.constructor.apply(this, arguments);
    }

    But somehow coffeescript generated constructor like this:

    function AtomWindow() {
      AtomWindow.__super__.constructor.apply(this, arguments);
    }

    There seems to be tons of related issues in coffeescript's github page, I'm still investigating it.

  2. zcbenz commented on Jun 18, 2013

    @zcbenz
    Contributor

    Hmm, coffeescript is doing the right thing, my problem is now that the native class's internal fields are not inherited via prototype inheritance, this is a limitation of V8 or JavaScript, but v8-juice does have a workaround of it:
    http://code.google.com/p/v8-juice/source/browse/trunk/src/include/v8/juice/ClassWrap.h#480

  3. added a commit that references this issue on Jun 18, 2013
    dbaf886
  4. zcbenz commented on Jun 18, 2013

    @zcbenz
    Contributor

    After some experiments, I decided to make BrowserWindow not inheritable again, here are my reasons:

    1. Internal fields are not inheritable

      Because BrowserWindow is a wrap of native C++ class, I need to store C++ object in V8 object's internal fields, however since internal fields are not inherited via prototype inheritance, I can not get the wrapped C++ object from derived class's this object without hacks.

    2. Performance

      In order to be able to get the wrapped C++ object from JS object, I have to store the native this object in the JS object somewhere, and then manually find it when I need to lookup the C++ object, which hurts the performance.

    3. Too many hacks in JS

      To make inheritance of native class work as expected, many hacks have to be used, the v8-juice's Wiki explains this very well:
      http://code.google.com/p/v8-juice/wiki/ClassBinderPartTwo#Inheriting_Native_Classes_from_JS

      And generated JS code of Coffee Script doesn't work well with these hacks.

    4. Two this object

      Distinguished from inheritance of pure JS class, which just calls base class's constructor as plain function, inheriting native class requires calling the native class's constructor with new, and also remembering the created native this object, which means we have to work with two this objects, this just make things a mess.

    So my conclusion is, making inheritance of native class from JS work would cause too much troubles and we should not support it.

  5. added a commit that references this issue on May 1, 2018
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