tc39 / proposal-structs

JavaScript Structs: Fixed Layout Objects
http://tc39.es/proposal-structs/
239 stars 5 forks source link

Breaking Proxy? #2

Closed Jack-Works closed 3 years ago

Jack-Works commented 3 years ago

Proxy is designed to be non-distinguishable from normal objects. If structs are typeof === "object", then the Proxy of structs can be detected with the following code.

function isProxyOfStruct(x) {
    try {
        struct class extends x {}
        return true
    } catch {
        return false // Nah, it's a proxy
    }
}
ljharb commented 3 years ago

You can already easily determine if anything is a proxy of any specific builtin, except Error, Function, and Array.

syg commented 3 years ago

Doesn't that example also return false for regular objects?

Jack-Works commented 3 years ago

Doesn't that example also return false for regular objects?

Yes, I mean the "Proxy of structs".

I agree this isn't a problem because of all those Web APIs that reads internal slots directly.

ljharb commented 3 years ago

Not just web; every 262 builtin (besides Error) reads internal slots, and only Function and Array effectively tunnel through proxies.