prepack icon indicating copy to clipboard operation
prepack copied to clipboard

Strange optimization function optimization

Open NTillmann opened this issue 6 years ago • 0 comments

We have the following test case.

(function() {
    function f(g) {
        let x = 23;
        function incrementX() {
            x = x + 42;
        }
        global.__optimize && __optimize(incrementX);
        g(incrementX);
        return x;
    }
    global.__optimize && __optimize(f);
    global.inspect = function() {
        return f(g => g());
    };
})();

which Prepacks to

(function () {
  var _$6 = this;

  var _2 = function (g) {
    var __leaked_0;

    var _6 = function () {
      var _$2 = __leaked_0;

      var _$3 = _$2 + 42;

      __leaked_0 = _$3;
      return void 0;
    };

    __leaked_0 = 23;

    var _$0 = g(_6);

    var _$1 = __leaked_0;
    return _$1;
  };

  var _1 = function () {
    return _2(g => g());
  };

  _$6.inspect = _1;
}).call(this);

Okay. But if you move up the definition of x as follows...

(function() {
    let x = 23;
    function f(g) {
        function incrementX() {
            x = x + 42;
        }
        global.__optimize && __optimize(incrementX);
        g(incrementX);
        return x;
    }
    global.__optimize && __optimize(f);
    global.inspect = function() {
        return f(g => g());
    };
})();

then we get

(function () {
  var _$2 = this;

  var _2 = function (g) {
    var _5 = function () {
      return void 0;
    };

    var _$0 = g(_5);

    return 23;
  };

  var _1 = function () {
    return _2(g => g());
  };

  _$2.inspect = _1;
}).call(this);

So in this case, incoming x is not leaking, but treated as a constant, as least as far as the return value of f is concerned. But somehow incrementX treats x as undefined? That seems wrong.

NTillmann avatar Aug 03 '18 22:08 NTillmann