Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error object property "name" should be writable #144

Closed
lll000111 opened this issue Mar 10, 2019 · 8 comments
Closed

Error object property "name" should be writable #144

lll000111 opened this issue Mar 10, 2019 · 8 comments

Comments

@lll000111
Copy link

lll000111 commented Mar 10, 2019

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.

Code

const e = new Error('test');
e.name = 'TestError';
trace(e.name + ' ' + e.message);

Result

Error: set name: not writable!

image

@lll000111
Copy link
Author

lll000111 commented 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_FLAG and then I could set a message in JS code.

I also still cannot set the name property even though as far as I can see it only has XS_DONT_ENUM_FLAG set.


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.

From the ECMAscript standard

Error instances are ordinary objects that inherit properties from the Error prototype object

No write protection, no special object.

MDN even has this example "Throwing a custom error":

var e = new Error('Malformed input'); // e.name is 'Error'
e.name = 'ParseError';
throw e;
// e.toString() would return 'ParseError: Malformed input'

@lll000111 lll000111 changed the title Error objects should be writable Error object property "name" should be writable Mar 10, 2019
@lll000111

This comment has been minimized.

@phoddie
Copy link
Collaborator

phoddie commented Mar 11, 2019

This is a consequence of freezing of the Error.prototype during preload. You can read more about that here.

@phoddie phoddie closed this as completed Mar 11, 2019
@lll000111
Copy link
Author

lll000111 commented 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
Copy link
Author

lll000111 commented 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 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.

@phoddie
Copy link
Collaborator

phoddie commented Mar 11, 2019

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.

@lll000111
Copy link
Author

Okay, they indeed do. TIL.

@lll000111
Copy link
Author

lll000111 commented Mar 11, 2019

Interesting that Object.defineProperty() still works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants