I am confused with 6.2.5.4 FromPropertyDescriptor

To understand details, I followed whole steps.
Then, I faced the problem:

6.2.5.4 FromPropertyDescriptor
3. Assert: obj is an extensible ordinary object with no own properties.

  1. If Desc has a [[Writable]] field, then
    a. Perform ! CreateDataPropertyOrThrow(obj, "writable", Desc.[[Writable]]).

and...following steps:

CreateDataPropertyOrThrow(obj, "writable", Desc.[[Writable]]).

newDesc = PropertyDescriptor { [[Value]]: Desc.[[Writable]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.

obj.[[DefineOwnProperty]]("writable", newDesc).

OrdinaryDefineOwnProperty ( obj, "writable", newDesc )

  1. Let current be ? O.[GetOwnProperty].

obj.[GetOwnProperty]

-2. If O does not have an own property with key P, return undefined.

(==obj does not have an own property with key "writable" )

Therefore, current be undefined

  1. Let extensible be ? IsExtensible(O).

(==assume as true because of 6.2.5.4.3)

ValidateAndApplyPropertyDescriptor ( obj, "writable", true|false, newDesc , undefined )

  1. Assert: If O is not undefined, then IsPropertyKey(P) is true.

(==Therefore, It’s true)

  1. If current is undefined, then
  • a. If extensible is false, return false. [No]
  • b. Assert: extensible is true. [Yes, assumed as true]
  • c. If IsGenericDescriptor(Desc) is true or IsDataDescriptor(Desc) is true, then
    • i. If O is not undefined, create an own data property named P of object O whose [[Value]], [[Writable]], [[Enumerable]], and [[Configurable]] attribute values are described by Desc. If the value of an attribute field of Desc is absent, the attribute of the newly created property is set to its default value.
    • Because IsDataDescriptor(newDesc) is true, do above.
  • e. Return true.

conclusion...

The result would be

obj = {“writable”: true}

And getOwnDescriptor of it would be newDesc

What 's the fault? I am doubtful that I was really mistaken.

Maybe comparing to engine-262 will help?

1 Like

It might be confusing as there is a mix of the internal property descriptors and creating a JS object representation of a property descriptor by using property descriptors. Very inception.

function FromPropertyDescriptor(descriptor)  {
  if (descriptor === undefined) {
    return undefined;
  }
  let obj = {};
  for (const [internalSlot, publicName] of [
    ['[[Value]]',        'value'],
    ['[[Writable]]',     'writable'],
    ['[[Get]]',          'get'],
    ['[[Set]]',          'set'],
    ['[[Enumerable]]',   'enumerable'],
    ['[[Configurable]]', 'configurable']
  ]) {
    if (internalSlot in descriptor) {
      Object.defineProperty(obj, publicName, {
        value: descriptor[internalSlot],
        writable: true,
        enumerable: true,
        configurable: true
      })
    }
  }
  return obj;
}
1 Like

Additionally, es-abstract: https://github.com/ljharb/es-abstract/blob/144955764b80341bee97c1773f89142b7e507840/2020/FromPropertyDescriptor.js#L9-L36

2 Likes

TIL!

1 Like

Thank you all! I was impressed.

Since now I would learn through engine262 and associated packages rather than docs :smiley: