tvcutsem / harmony-reflect

ES5 shim for ES6 Reflect and Proxy objects
http://www.ecma-international.org/ecma-262/6.0/#sec-reflection
Other
478 stars 48 forks source link

get handler with Symbol #57

Open soubok opened 9 years ago

soubok commented 9 years ago

I fail to use the Proxy get handler with a ES6 Symbol as property name:

var Reflect = require('harmony-reflect');

var p = new Proxy(Object.create(null), {

    get: function(target, property) {

        return 'bar';
    }
});

console.log( p['foo'] );
console.log( p[Symbol('fooSym')] );
tvcutsem commented 9 years ago

Symbols are not supported by this shim, as they are not available in ES5.

Can you provide a little more info about your environment? Are you using a tool like babeljs to compile to ES5 or a runtime that already provides support for Symbols?

soubok commented 9 years ago

I am using iojs v2.3.4 (--harmony --harmony_arrow_functions --harmony_proxies) and I haven't tried babeljs yet but I don't see any information about proxies support on their web site.

tvcutsem commented 9 years ago

I'll try to replicate and see what the behavior is. If v8's Symbol implementation is incompatible with its Proxy implementation, there is little this shim can do. But perhaps the issue lies in my library code and can be fixed.

Babeljs indeed does not support proxies, but it does support Symbols. It could have been the case that you were using Babeljs Symbols, compiled down to ES5 and then using node or iojs --harmony_proxies to run that code. I'm not sure how Babeljs compiles symbols to ES5 but I suspect it would not lead to the desired semantics.

zloirock commented 9 years ago

I'm not sure how Babeljs compiles symbols to ES5

Babel uses core-js - setters in Object.prototype and wrappers for some methods.

tvcutsem commented 9 years ago

As I had expected, it seems V8's Symbols and built-in Proxies don't play well together:

I tested the following using iojs --harmony_proxies (iojs v2.3.0)

var p = Proxy.create({
  get: function(receiver, name) {
    console.log(typeof name);
    return name;
  },
  set: function(receiver, name, val) {
    console.log(typeof name);
  }
});

var s = Symbol("x");
p[s] = 42;
console.log(p[s]);

Note that this code doesn't even use this library but just uses the old pre-ES6 Proxy API directly.

The above code doesn't even trigger the Proxy get/set traps, and the last line just logs undefined. Seems like this is a V8 bug.

I'm not sure to what extent my shim can add support for Symbols if built-in Proxies don't work with them. For now, I'll note the incompatibility in the README.

Thanks for reporting.

deian commented 8 years ago

Not sure if folks are still interested in this, but I filed a v8 bug on this (https://code.google.com/p/v8/issues/detail?id=4537&thanks=4537&ts=1446787325)