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

Strange 'illegal access' error in stack trace #12

Closed sricc closed 11 years ago

sricc commented 11 years ago

I noticed that when using your attributes.js example I get a strange error in the stack trace of any method that I call when the class is wrapped in AttributeProxy. It doesn't "seem" to affect anything, but was wondering if this is normal? Thanks.

In node.js:

require('harmony-reflect');

function AttributeProxy(target) {
  return Proxy(target, {
    get: function(target, name, receiver) {
      if (name in target) { return target[name]; }
      // everything not found on the target object itself should
      // be looked up in the target's _attributes map
      return target._attributes[name];
    },
    set: function(target, name, val, receiver) {
      if (name in target) {
        target[name] = val;
        return true;
      }
      // everything not found on the target object itself should
      // be added to the target's _attributes map
      target._attributes[name] = val;
      return true;
    }
  });
}

var Person = function() {
   this._attributes = {};
   return AttributeProxy(this);
};

Person.prototype.walk = function() {
  console.log('Person is walking');
  console.log(new Error().stack);
};

var Female = function() {
  // call "super" constructor
  return Person.call(this);
}
// make Female inherit from Person
Female.prototype = Object.create(Person.prototype);
Female.prototype.shop = function() {
  console.log('Female is shopping');
  console.log(new Error().stack);
}

console.log(new Error().stack);

// tests
var person = new Person();
person.hair = 'black';
person.walk(); // methods are called normally

var female = new Female();
female.hair = 'blonde';
female.walk(); // methods are called normally
female.shop(); // methods are called normally

Output:

Error: Regular Stack Trace
    at Object.<anonymous> (/var/guestweb/new.jump.omnitarget.com/attributes_test.js:45:13)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Person is walking
Error: Person.walk
    at <error: illegal access>
    at Object.<anonymous> (/var/guestweb/new.jump.omnitarget.com/attributes_test.js:50:8)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Person is walking
Error: Person.walk
    at <error: illegal access>
    at Object.<anonymous> (/var/guestweb/new.jump.omnitarget.com/attributes_test.js:54:8)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Female is shopping
Error: Female.shop
    at <error: illegal access>
    at Object.<anonymous> (/var/guestweb/new.jump.omnitarget.com/attributes_test.js:55:8)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
tvcutsem commented 11 years ago

I don't think this is anything to worry about. Probably also relatively implementation-dependent. I ran the code through v8 and spidermonkey and they show different results:

In v8 itself:

v8 --harmony stacktest.js 
Error
    at stacktest.js:50:13
Person is walking
Error
    at <error: illegal access>
    at stacktest.js:55:8
Person is walking
Error
    at <error: illegal access>
    at stacktest.js:59:8
Female is shopping
Error
    at <error: illegal access>
    at stacktest.js:60:8

Not surprisingly, this is the same behavior as NodeJS.

In tracemonkey:

js stacktest.js 
@stacktest.js:50

Person is walking
()@stacktest.js:36
@stacktest.js:55

Person is walking
()@stacktest.js:36
@stacktest.js:59

Female is shopping
()@stacktest.js:47
@stacktest.js:60

Here the stack trace is just empty.

AFAICT, stack traces are very platform-dependent. How proxies should interact with / show up in stack traces is not specified anywhere. Unless you're stuck on something due to this issue, I wouldn't worry about it.

sricc commented 11 years ago

Thanks for looking into it. Currently it's not causing a problem, that I am aware of, but I just wanted to let you know it was happening in case it does affect something down the road.