Giter Club home page Giter Club logo

Comments (11)

sieira avatar sieira commented on May 9, 2024 3

Regarding the JSON.stringify, a workaround is to do this:

  var proxy = new Proxy(obj, {
    get: function(target, property, receiver) {
      if (property === 'toJSON') {
        return () => target;
      }
 });

from harmony-reflect.

billmark avatar billmark commented on May 9, 2024

I just checked these issues against the most recent version of v8 (3.17.16.2). Issue 1 from above (JSON.stringify dying on proxies) is fixed. Issue 3 from above (Arrays returning "object Object") is still there, as the following d8 console session shows:

V8 version 3.17.16.2 [console: readline]
d8> load("/home/parallels/git/experiments/node_modules/harmony-reflect/reflect.js");
undefined
d8> a = ["a","b","c"];
["a", "b", "c"]
d8> Object.prototype.toString.apply(a);
"[object Array]"
d8> var p = Proxy(a, {});
undefined
d8> print(p);
a,b,c
undefined
d8> print(a);
a,b,c
undefined
d8> Object.prototype.toString.apply(p);
"[object Object]"

from harmony-reflect.

metamatt avatar metamatt commented on May 9, 2024

Thanks for clarifying. I'd meant to report these results using that same Node version but had invoked the wrong one. Whoops.

I see the same thing as @billmark, which is nice because the JSON.stringify issue I complained about as (1) above actually is fixed, but now that it works, it turns out that it, just like JSON2.stringify and util.inspect, does not think that proxy-to-array is an array.

So scratch complaint (1), but (2) and (3) turn out to be even truer than I thought.

from harmony-reflect.

billmark avatar billmark commented on May 9, 2024

More details: I also checked against the slightly more recent "bleeding_edge" version of v8, "3.18.0 (candidate)", and it still has the issue that calling Object.prototype.toString on a proxied array returns "[object Object]" rather than "[object Array]".

from harmony-reflect.

tvcutsem avatar tvcutsem commented on May 9, 2024

I just committed a patch so that Array.isArray and Object.prototype.toString automatically unwrap proxies. Those methods now return the expected result on proxies for arrays:

var p = Proxy([] , {});
Object.prototype.toString.call(p) // [object Array]
Array.isArray(p) // true

However, JSON.stringify still serializes proxies-for-arrays as objects. This is because that algorithm internally uses its own isArray check, which my library cannot intercept. AFAICT, this is something that can be solved only by proxies directly provided by the engine. The alternative is to re-implement JSON.stringify in JavaScript itself, which doesn't seem to be a good alternative.

from harmony-reflect.

billmark avatar billmark commented on May 9, 2024

Thanks, Tom.

from harmony-reflect.

coreybutler avatar coreybutler commented on May 9, 2024

Has any of this made it's way to the npm module?

from harmony-reflect.

tvcutsem avatar tvcutsem commented on May 9, 2024

Not yet. Thanks for bringing it to my attention. I'll upload a new npm package shortly.

from harmony-reflect.

tvcutsem avatar tvcutsem commented on May 9, 2024

npm module v0.0.5 now contains the patches to Array.isArray and Object.prototype.toString to unwrap proxies.

from harmony-reflect.

billmark avatar billmark commented on May 9, 2024

Thanks, Tom!

from harmony-reflect.

coreybutler avatar coreybutler commented on May 9, 2024

Thanks!

from harmony-reflect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.