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

Hoek.clone() issues #228

Closed
kanongil opened this issue Jan 30, 2018 · 10 comments
Closed

Hoek.clone() issues #228

kanongil opened this issue Jan 30, 2018 · 10 comments
Labels
bug Bug or defect
Milestone

Comments

@kanongil
Copy link
Contributor

kanongil commented Jan 30, 2018

I happened to look into the clone() implementation, and found several critical issues in my review:

  1. Cloned Map / Set values are completely broken.
  2. Does not clone Symbol() properties on object. Can be found using Object.getOwnPropertySymbols().
  3. Non-enumerable properties are enumerable on cloned object.
  4. Cloned Promise values are broken.
@kanongil kanongil added the bug Bug or defect label Jan 30, 2018
@kanongil
Copy link
Contributor Author

Another type that could use special handling is Error.

I know that due to lazy evaluation, reading the stack property is extremely slow in V8, and should be avoided. I would probably cheat a little here, and install a getter instead, which can then retrieve the original stack value.

Object.defineProperty(newObj, 'stack', {
    configurable: true,
    get() {

        return obj.stack;
    },
    set(value) {

        Object.defineProperty(newObj, 'stack', {
            configurable: true,
            writable: true,
            value
        });
    }
});

See https://bugs.chromium.org/p/v8/issues/detail?id=5962 for a bit of background.

@nlf
Copy link
Member

nlf commented Jan 30, 2018

yup, clone hasn't been updated for newer types (even as new as Promise)

good catch on cloned non-enumerable properties becoming enumerable though!

@robmcguinness
Copy link

For completeness: Hoek.applyToDefaults also doesn't take into account Symbol properties of an object.

@SimonSchick
Copy link

It's also problematic for complex types such as sockets, I ran into this this catbox-redis just now.

@kanongil
Copy link
Contributor Author

@SimonSchick Yeah, any object with hidden state won't work unless explicitly added. It's only designed to work for "simple" objects.

@hueniverse
Copy link
Contributor

@kanongil any plans on fixing this?

@kanongil
Copy link
Contributor Author

The enumerable issue is fixed with 6f02157.

@kanongil
Copy link
Contributor Author

The Symbol issue should be fixed with #281.

@shijiebei2014
Copy link

const Hoek = require('hoek')
var map = new Map([
['1', new Object()]
])
console.log(Hoek.clone(map))

The result is : Map {}

@hueniverse hueniverse added this to the 6.2.3 milestone May 17, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

6 participants