Skip to content

napi_define_properties should create ECMAScript-compliant accessors. #26551

@iamstolis

Description

@iamstolis
  • Version: v10.15.2
  • Platform: Linux stola-ThinkPad 3.16.0-38-generic io.js on The Changelog! #52~14.04.1-Ubuntu SMP Fri May 8 09:43:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: napi

Append the following lines to addons-napi/test_properties test-case

console.log(Object.getOwnPropertyDescriptor(test_object, 'readwriteAccessor1'));
var anotherObject = Object.create(test_object);
anotherObject.readwriteAccessor1 = 42;
console.log(anotherObject.hasOwnProperty('readwriteAccessor1'));
console.log(test_object.readwriteAccessor1);

and execute it. You will get the following output

{ value: 2,
  writable: true,
  enumerable: false,
  configurable: false }
false
42

You can see that readwriteAccessor1 looks like a data property (according to its property descriptor) despite being created with setter and getter. On the other hand, it behaves like an accessor property because anotherObject.readwriteAccessor1 = 42 does not create a data property on anotherObject but invokes the setter in the prototype chain (as you can see from the last two lines of the output).

This strange behaviour is not possible in ECMAScript-compliant world. It is some weird legacy behaviour of v8::Object:SetAccessor (used in the implementation of napi_define_properties) and should be avoided. It would be unfortunate to promote this off-spec behaviour in newly arising N-API.

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