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

"Non-POJO" check is slightly naive #5

Closed
Conduitry opened this issue Mar 26, 2018 · 1 comment · Fixed by #7
Closed

"Non-POJO" check is slightly naive #5

Conduitry opened this issue Mar 26, 2018 · 1 comment · Fixed by #7

Comments

@Conduitry
Copy link
Contributor

Conduitry commented Mar 26, 2018

First, a disclaimer: This issue is niche as hell. I understand that. But:

If a POJO was created in a different context, checking whether its prototype is Object.prototype will fail, because its prototype is actually a different Object.prototype. This actually did come up for me, when I was trying to stringify a value created by Node's vm module. You can easily reproduce this by attempting to run devalue(vm.runInNewContext('({})')).

Now. I don't really know what a good solution to this is, and it seems it's likely that I need to send normal object to devalue more so than that devalue needs to handle weird ones.

One heavy-handed way would be to have an option that forces devalue to stringify objects, regardless of whether it perceives them to be non-POJOs.

A less heavy-handed way (but kind of a weird one) would be to have an option that can be passed to devalue that tells it what Object or Object.prototype object to use.


In the other sort of direction, devalue currently silently ignores any keys on objects that are non-enumerable or that are a symbol and not a string. By their very nature, there's no way to handle symbol keys - and it might be possible to deal with non-enumerable ones but it's likely to not be worth the hassle. Anyway, perhaps these should throw exceptions rather than just being ignored.

@Conduitry
Copy link
Contributor Author

Posted this in the svelte gitter, but here would have really been a better place:

My current 'probably will work most of the time unless someone is trying to be sneaky' proposal to detect cross-realm POJOs is to compare the arrays Object.getOwnPropertyKeys(Object.getPrototypeOf(obj)) and Object.getOwnPropertyKeys(Object.prototype).

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

Successfully merging a pull request may close this issue.

1 participant