GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How to inherit from new Buffer implementation · Issue #2882 · nodejs/node

How to inherit from new Buffer implementation #2882

New issue
Copy link
New issue
Copy link
Closed
Closed
How to inherit from new Buffer implementation#2882
Copy link
Assignees
trevnorris
Labels
bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to Node.js documentation.questionIssues asking questions about Node.js.

Description

@corentingurtner
corentingurtner
opened on Sep 15, 2015
Issue body actions

Hi,
I would like to know how a class could inherit from the new Buffer implementation.

I'm trying to upgrade the node-ogg module for node v4 and nan v2.
(Here is my work so far)

I tried this classic inheritance model:

function ogg_packet (buffer) {
  if (!Buffer.isBuffer(buffer)) {
    Buffer.call(this, binding.sizeof_ogg_packet);
  } else {
    Buffer.call(this, buffer);
  }
  if (this.length != binding.sizeof_ogg_packet) {
    throw new Error('"buffer.length" = ' + this.length + ', expected ' + binding.sizeof_ogg_packet);
  }
}
inherits(ogg_packet, Buffer);

But I get this error when trying to access this.length :

Uncaught TypeError: Method Uint8Array.length called on incompatible receiver [object Object]

Thanks

Note: I also asked the question on SO:
http://stackoverflow.com/questions/32555714

Activity

  1. Fishrock123
    added
    questionIssues asking questions about Node.js.
    on Sep 15, 2015
  2. ChALkeR
    added
    bufferIssues and PRs related to the buffer subsystem.
    on Sep 15, 2015
  3. bnoordhuis commented on Sep 15, 2015

    @bnoordhuis
    bnoordhuis
    on Sep 15, 2015
    Member
    More actions

    ES6 extends should work:

    > class C extends Buffer { constructor(size) { super(size) } }
    [Function: C]
    
    > (new C(32)).length
    32
    
  4. corentingurtner commented on Sep 15, 2015

    @corentingurtner
    corentingurtner
    on Sep 15, 2015
    Author
    More actions

    Thanks @bnoordhuis ,

    I know it can be seen as going backward, but is there a way in ES5 too?

  5. bnoordhuis commented on Sep 15, 2015

    @bnoordhuis
    bnoordhuis
    on Sep 15, 2015
    Member
    More actions

    I think that's going to be less easy for the same reason that it's difficult to inherit from Array or Uint8Array in ES5. You can maybe hack something together through the __proto__ property.

  6. trevnorris commented on Sep 15, 2015

    @trevnorris
    trevnorris
    on Sep 15, 2015
    Contributor
    More actions

    Try something like this:

    function Ogg() {
      const ui = new Uint8Array(size);
      Object.setPrototypeOf(ui, Ogg.prototype);
      return ui;
    }
    
    Ogg.prototype.__proto__ = Buffer.prototype;
    Ogg.__proto__ = Buffer;

    Yeah, ugly as sin.

  7. Fishrock123 commented on Sep 15, 2015

    @Fishrock123
    Fishrock123
    on Sep 15, 2015
    Contributor
    More actions

    This should probably go into the buffer docs.

  8. Fishrock123
    added
    docIssues and PRs related to Node.js documentation.
    on Sep 15, 2015
  9. corentingurtner commented on Sep 15, 2015

    @corentingurtner
    corentingurtner
    on Sep 15, 2015
    Author
    More actions

    Thanks a lot for your answers,

    @trevnorris :
    I succeed to create an instance this way thanks to your help:

    function ogg_packet (buffer) {
      if (!Buffer.isBuffer(buffer)) {
        buffer = new Uint8Array(binding.sizeof_ogg_packet);
      }
    
      if (buffer.length != binding.sizeof_ogg_packet) {
        throw new Error('"buffer.length" = ' + buffer.length + ', expected ' + binding.sizeof_ogg_packet);
      }
    
      Object.setPrototypeOf(buffer, ogg_packet.prototype);
      console.log('BUFFER', buffer.toString());
      return buffer;
    }
    ogg_packet.prototype.__proto__ = Buffer.prototype;
    ogg_packet.__proto__ = Buffer;

    However, it seems that I can't use Buffer methods (at least toString()) since there is a strict equality check of the instance. For example, trying to print buffer.toString() in the constructor function give me that error:

    Uncaught TypeError: argument should be a Buffer
     at TypeError (native)
     at ogg_packet.Buffer.toString (buffer.js:354:23)
    
  10. trevnorris commented on Sep 15, 2015

    @trevnorris
    trevnorris
    on Sep 15, 2015
    Contributor
    More actions

    @corentingurtner That would be from the node::Buffer::HasInstance() check. It's strictly checking the hidden class. Unfortunately there's no way I'm aware of to do proto transversal in the C++ API, similar to instanceof.

    /cc @domenic Know of a way to do this proto transversal check in C++?

    If there's not a quick fix then let's open a new issue for this.

  11. domenic commented on Sep 15, 2015

    @domenic
    domenic
    on Sep 15, 2015
    Contributor
    More actions

    @trevnorris you can use v8::Object::GetPrototype(). Something like objLocal->GetPrototype()->Get(context, v8_str("constructor")) to get the constructor and then check with Equals against the constructor.

  12. domenic commented on Sep 15, 2015

    @domenic
    domenic
    on Sep 15, 2015
    Contributor
    More actions

    What you really want though is to check that the appropriate internal data is set on the object. Which I think just would be val->IsUint8Array(). Otherwise I can fool it with crap like var notABuffer = {}; notABuffer.__proto__ = Buffer.prototype.

  13. trevnorris commented on Sep 15, 2015

    @trevnorris
    trevnorris
    on Sep 15, 2015
    Contributor
    More actions

    @domenic ah yup. sure enough. when I had to reimplement the check I was focused on not allowing users to bypass the instanceof check in JS.

    I agree that the check should be simplified down to checking internal data. All methods will work from that point regardless of whether it's actually a Buffer instance. I'll make a PR.

  14. trevnorris
    self-assigned this
    on Sep 16, 2015
  15. ben-page commented on Sep 21, 2015

    @ben-page
    ben-page
    on Sep 21, 2015
    Contributor
    More actions

    +1 This stops me from migrating to 4.x.

  16. trevnorris commented on Sep 24, 2015

    @trevnorris
    trevnorris
    on Sep 24, 2015
    Contributor
    More actions

    @bnoordhuis using classes won't work:

    'use strict';
    
    class Foo extends Buffer {
      constructor(n) { super(n); }
    
      foo() {
        let cntr = 0;
        for (let i = 0; i < this.length; i++) {
          cntr += this[i];
        }
        return cntr;
      }
    }
    
    let foo = new Foo(17).fill('abc');
    
    console.log(foo.foo());  // TypeError: foo.foo is not a function

    We are overwriting the prototype in the Buffer constructor, and loosing all the child's class methods. Want to examine this a bit more.

  17. domenic commented on Sep 24, 2015

    @domenic
    domenic
    on Sep 24, 2015
    Contributor
    More actions

    This is fixable once new.target ships in V8. You'd use new.target.prototype instead of Buffer.prototype.

  18. targos commented on Sep 24, 2015

    @targos
    targos
    on Sep 24, 2015
    Member
    More actions

    We'll be able to fix this with new.target (only from V8 4.6):

    node [vee-eight-4.6●] % more test.js 
    'use strict';
    
    class A {
      constructor() {
        console.log(new.target);
      }
    }
    
    class B extends A {}
    
    new A();
    new B();
    
    node [vee-eight-4.6●] % ./node test
    [Function: A]
    [Function: B]
    
  19. trevnorris commented on Sep 24, 2015

    @trevnorris
    trevnorris
    on Sep 24, 2015
    Contributor
    More actions

    This still requires that HasInstance() basically becomes an IsUint8Array() check.

  20. trevnorris commented on Sep 24, 2015

    @trevnorris
    trevnorris
    on Sep 24, 2015
    Contributor
    More actions

    @domenic @targos Unless I'm missing something, new.target doesn't help class constructors:

    'use strict';
    
    class A {
      constructor() {
        if (!new.target)
          return { foo: 42 };
      }
    }
    
    let a = A();  // TypeError: Class constructors ...
  21. trevnorris commented on Sep 26, 2015

    @trevnorris
    trevnorris
    on Sep 26, 2015
    Contributor
    More actions

    @domenic The inconsistency must come from from mixing ES5 and ES6 style inheritance together. Take this example:

    class B extends Uint8Array { constructor() { super(0); } }
    class C extends B { constructor() { super(); } }
    
    let c = new C();
    
    console.log(c.constructor == C);
    console.log(c.__proto__ == C.prototype);
    console.log(c.__proto__.__proto__ == B.prototype);
    console.log(c.__proto__.__proto__.__proto__ == Uint8Array.prototype);

    But now doing the same thing with Buffer:

    class C extends Buffer { constructor() { super(0); } }
    
    let c = new C();
    
    console.log(c.constructor == Buffer);
    console.log(c.__proto__ == Buffer.prototype);
    console.log(c.__proto__.__proto__ == Uint8Array.prototype);

    As you can see it completely dropped anything related to C.

    What I'm missing is what new.target could help with.

  22. domenic commented on Sep 26, 2015

    @domenic
    domenic
    on Sep 26, 2015
    Contributor
    More actions

    @trevnorris right now allocate is doing Object.setPrototypeOf(ui8, Buffer.prototype), so even if someone does new C(), you are returning to them something with Buffer.prototype, not C.prototype.

    To fix this, you need to pass new.target from the Buffer constructor definition, to allocate, so that allocate can do Object.setPrototypeOf(ui8, newTarget.prototype).

    Then the returned object will have the correct prototype. In this case it would be C.prototype, since the target of new in new C() is C.

  23. trevnorris
    added a commit that references this issue on Oct 6, 2015

    buffer: Only check if instance is Uint8Array

    b7eb4f1
  24. trevnorris
    closed this as completedin 05d424con Oct 6, 2015
  25. trevnorris
    added a commit that references this issue on Oct 8, 2015

    buffer: only check if instance is Uint8Array

    651a5b5
  26. sjarmak
    added a commit that references this issue on Jul 27, 2026

    buffer: only check if instance is Uint8Array

    05d424c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @trevnorris
    trevnorris

Labels

bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to Node.js documentation.questionIssues asking questions about Node.js.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.