Git Product home page Git Product logo

Comments (3)

zcbenz avatar zcbenz commented on May 5, 2024

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.

from electron.

zcbenz avatar zcbenz commented on May 5, 2024

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

from electron.

zcbenz avatar zcbenz commented on May 5, 2024

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.

from electron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.