none
IE11 JIT broken when using class properties RRS feed

  • Question

  • I have run into a strange behavior in IE11 JavaScript JIT.

    Calling Function.name on Classes fails if it is called in the order of base class->child. If called in the child->base class order, it succeeds.

    This example:

    console.log(Animal.name);
    console.log(Snake.name); // returns Animal in IE11
    console.log(Horse.name); // returns Animal in IE11
    However:

    console.log(Snake.name); // returns Snake in IE11
    console.log(Horse.name); // returns Horse in IE11
    console.log(Animal.name);
    Note: changing the order of the children doesn't have any effect in this example.

    I have a simple class hierarchy example which I built using Typescript, which is transpired into es6, then babel'd into es5.

    The sample page includes the babel polyfill and the script.

    I've also tried the JamesMGreene/Function.name polyfill.

    Based on walking the code I don't think that is the problem. This appears to be a flaw in the IE11 JIT.

    I'm looking for any advice on how to handle this besides (1) don't use IE11 or (2) don't use Function.name.

    Sample:

    // Typescript
    class Animal {    
        hello(person: string) {
            console.log(`Hello ${person}`);
        }
    }

    class Snake extends Animal {    
        hello(person: string) {
            console.log("I'm a snake");
            super.hello(person);
        }
    }

    class Horse extends Animal {
        get wow(): string {
            return "wow";
        }
    }

    console.log(Animal.name);
    console.log(Snake.name);
    console.log(Horse.name);

    tsc -t ES2015 with produces this JavaScript:

    class Animal {
        hello(person) {
            console.log(`Hello ${person}`);
        }
    }
    class Snake extends Animal {
        hello(person) {
            console.log("I'm a snake");
            super.hello(person);
        }
    }
    class Horse extends Animal {
        get wow() {
            return "wow";
        }
    }
    console.log(Animal.name);
    console.log(Snake.name);
    console.log(Horse.name);

    Then babel with "presets": ["es2015"] generates the es5 Javascript:

    (I have included a polyfill to make this JS runnable)

    "use strict";

    (function() {

    var fnNameMatchRegex = /^\s*function\s+([^\(\s]*)\s*/;

    function _name() {
      var match, name;
      if (this === Function || this === Function.prototype.constructor) {
        name = "Function";
      }
      else if (this !== Function.prototype) {
        match = ("" + this).match(fnNameMatchRegex);
        name = match && match[1];
      }
      return name || "";
    }

    // Inspect the polyfill-ability of this browser
    var needsPolyfill = !("name" in Function.prototype && "name" in (function x() {}));
    var canDefineProp = typeof Object.defineProperty === "function" &&
      (function() {
        var result;
        try {
          Object.defineProperty(Function.prototype, "_xyz", {
            get: function() {
              return "blah";
            },
            configurable: true
          });
          result = Function.prototype._xyz === "blah";
          delete Function.prototype._xyz;
        }
        catch (e) {
          result = false;
        }
        return result;
      })();
    var canDefineGetter = typeof Object.prototype.__defineGetter__ === "function" &&
      (function() {
        var result;
        try {
          Function.prototype.__defineGetter__("_abc", function() {
            return "foo";
          });
          result = Function.prototype._abc === "foo";
          delete Function.prototype._abc;
        }
        catch (e) {
          result = false;
        }
        return result;
      })();



    // Add the "private" property for testing, even if the real property can be polyfilled
    Function.prototype._name = _name;


    // Polyfill it!
    // For:
    //  * IE >=9 <12
    //  * Chrome <33
    if (needsPolyfill) {
      // For:
      //  * IE >=9 <12
      //  * Chrome >=5 <33
      if (canDefineProp) {
        Object.defineProperty(Function.prototype, "name", {
          get: function() {
            var name = _name.call(this);

            // Since named function definitions have immutable names, also memoize the
            // output by defining the `name` property directly on this Function
            // instance so that this polyfill will not need to be invoked again
            if (this !== Function.prototype) {
              Object.defineProperty(this, "name", {
                value: name,
                configurable: true
              });
            }

            return name;
          },
          configurable: true
        });
      }
      // For:
      //  * Chrome <5
      else if (canDefineGetter) {
        // NOTE:
        // The snippet:
        //
        //     x.__defineGetter__('y', z);
        //
        // ...is essentially equivalent to:
        //
        //     Object.defineProperty(x, 'y', {
        //       get: z,
        //       configurable: true,  // <-- key difference #1
        //       enumerable: true     // <-- key difference #2
        //     });
        //
        Function.prototype.__defineGetter__("name", function() {
          var name = _name.call(this);

          // Since named function definitions have immutable names, also memoize the
          // output by defining the `name` property directly on this Function
          // instance so that this polyfill will not need to be invoked again
          if (this !== Function.prototype) {
            this.__defineGetter__("name", function() { return name; });
          }

          return name;
        });
      }
    }

    })();


    var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

    var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

    function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

    function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var Animal = function () {
        function Animal() {
            _classCallCheck(this, Animal);
        }

        _createClass(Animal, [{
            key: "hello",
            value: function hello(person) {
                console.log("Hello " + person);
            }
        }]);

        return Animal;
    }();

    var Snake = function (_Animal) {
        _inherits(Snake, _Animal);

        function Snake() {
            _classCallCheck(this, Snake);

            return _possibleConstructorReturn(this, (Snake.__proto__ || Object.getPrototypeOf(Snake)).apply(this, arguments));
        }

        _createClass(Snake, [{
            key: "hello",
            value: function hello(person) {
                console.log("I'm a snake");
                _get(Snake.prototype.__proto__ || Object.getPrototypeOf(Snake.prototype), "hello", this).call(this, person);
            }
        }]);

        return Snake;
    }(Animal);

    var Horse = function (_Animal2) {
        _inherits(Horse, _Animal2);

        function Horse() {
            _classCallCheck(this, Horse);

            return _possibleConstructorReturn(this, (Horse.__proto__ || Object.getPrototypeOf(Horse)).apply(this, arguments));
        }

        _createClass(Horse, [{
            key: "wow",
            get: function get() {
                return "wow";
            }
        }]);

        return Horse;
    }(Animal);

    console.log(Animal.name);
    console.log(Snake.name); // returns Animal in IE11
    console.log(Horse.name); // returns Animal in IE11

    Tuesday, February 28, 2017 4:27 PM

All replies

  • Hi,

    As this issue is mainly related to IE JavaScript, I suggest discussing it in our IE development forum. They are the best resource to troubleshoot this issue.

    https://social.msdn.microsoft.com/Forums/ie/en-US/home?category=iedevelopment

    Best Regards,

    Tao


    Please remember to mark the replies as answers if they help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, March 1, 2017 6:52 AM
    Moderator
  • Adding to that forum, thank you.
    Wednesday, March 1, 2017 1:19 PM
  • You are welcome:)

    In addition, you can mark the reply as an answer if you find it is helpful:)


    Please remember to mark the replies as answers if they help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, March 2, 2017 1:17 AM
    Moderator