NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
2k stars 353 forks source link

hi, getter of interpreter.js doesn't work #162

Closed wubolin closed 5 years ago

wubolin commented 5 years ago

when i write a getter, e.g
var o ={ get m() { return "hi"}; alert(o.m) . it return null.

i trace de source, find the bug in line 3650: this.setProperty(state.object_, key, null, descriptor);

when i modify it as this.setProperty(state.object_, key, Interpreter.VALUE_IN_DESCRIPTOR, descriptor);

it's ok . above example, return the correct value "hi"

NeilFraser commented 5 years ago

Confirming. The following (using defineProperty) works as expected:

var o = {};
Object.defineProperty(o, 'foo', {
  get: function() { alert('Get'); return 2; },
  set: function(x) { alert('Setting: ' + x); }
});
alert(o.foo);
o.foo = 4;

(alerts 'Get', '2', and 'Setting: 4')

However, the equivalent using the get or set syntax does not work:

var o = {
  get foo() { alert('Get'); return 2; },
  set foo(x) { alert('Setting: ' + x); }
};
alert(o.foo);
o.foo = 4;

(alerts 'null', neither get nor set functions are called.)

NeilFraser commented 5 years ago

Your analysis is exactly correct. Interpreter.VALUE_IN_DESCRIPTOR is the correct fix for this issue. Pushed a new version. Thanks!