You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error object property "name" should be writable #144
I give Error objects names (i.e. setting nameproperty, sometimes replacing an existing message property too) because I prefer this over sub-classing Errors, which only is simple on ES 2015 and with a functional style I don't have a single "class", "this", "bind" in any of my code, it's "just functions".
This is perfectly legal JS. It should just be a normal object.
EDIT: Am I confused? message apparently can be set, with or without the above change. It's only name that is not writable. I guess this comment is useless :-(
Time to change the issue title...
I can't see why a name property cannot be assigned to Error instances :(
EDIT²: Now I'm really confused. In my project code message is not writable either, but it worked in the small example above.
I think name is in the Error constructor's Prototype object. When I assign to an Error instance name property it should create a new property on the instance. Still, even the original name property on the prototype is writable, even if nobody should do that.
I already know you are freezing prototypes. However, the way this works and is supposed to work, looking at other runtimes, is that a new property in the instance is created. If the measure you took prevent adding an instance property this is not just "freezing the prototype".
I read the link again, I had already read it some time ago, just to be sure I did not misread.
The example there explicitly directly changes the prototype object. This has nothing to do with this issue here. Assigning to name does not modify the prototype object in any way, it creates a new property directly on the instance object. The instance is in RAM and should be writable.
It is a consequence of Error.prototype being frozen. The name property is on the prototype as per section 19.5.3.3 of the ECMAScript language specification. When an object is frozen, writes to existing properties fail, including those on instances that use the frozen prototype.
Activity
lll000111 commentedon Mar 10, 2019
It seems to me that this is due to xs/sources/xsError.c setting
XS_GET_ONLY.I set this to
XS_INTERNAL_FLAG | XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAGand then I could set a message in JS code.I also still cannot set the
nameproperty even though as far as I can see it only hasXS_DONT_ENUM_FLAGset.EDIT: Am I confused?
messageapparently can be set, with or without the above change. It's onlynamethat is not writable. I guess this comment is useless :-(Time to change the issue title...
I can't see why a
nameproperty cannot be assigned to Error instances :(EDIT²: Now I'm really confused. In my project code
messageis not writable either, but it worked in the small example above.I think
nameis in the Error constructor'sPrototypeobject. When I assign to an Error instancenameproperty it should create a new property on the instance. Still, even the originalnameproperty on the prototype is writable, even if nobody should do that.From the ECMAscript standard
No write protection, no special object.
MDN even has this example "Throwing a custom error":
[-]Error objects should be writable[/-][+]Error object property "name" should be writable[/+]lll000111 commentedon Mar 11, 2019
phoddie commentedon Mar 11, 2019
This is a consequence of freezing of the
Error.prototypeduring preload. You can read more about that here.lll000111 commentedon Mar 11, 2019
I already know you are freezing prototypes. However, the way this works and is supposed to work, looking at other runtimes, is that a new property in the instance is created. If the measure you took prevent adding an instance property this is not just "freezing the prototype".
lll000111 commentedon Mar 11, 2019
I read the link again, I had already read it some time ago, just to be sure I did not misread.
The example there explicitly directly changes the prototype object. This has nothing to do with this issue here. Assigning to
namedoes not modify the prototype object in any way, it creates a new property directly on the instance object. The instance is in RAM and should be writable.phoddie commentedon Mar 11, 2019
It is a consequence of
Error.prototypebeing frozen. Thenameproperty is on the prototype as per section 19.5.3.3 of the ECMAScript language specification. When an object is frozen, writes to existing properties fail, including those on instances that use the frozen prototype.lll000111 commentedon Mar 11, 2019
Okay, they indeed do. TIL.
lll000111 commentedon Mar 11, 2019
Interesting that Object.defineProperty() still works.