Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-10313

[Reg] parentContext->activationObject() can return the wrong object when called from a native QtScript function

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • P2: Important
    • None
    • 4.6.0
    • Qt Script
    • None

    Description

      In Qt 4.5, the following would print "123":

      static QScriptValue getParentAct(QScriptContext *ctx, QScriptEngine *)

      { return ctx->parentContext()->activationObject(); }

      ...

      QScriptEngine eng;
      eng.globalObject().setProperty("getParentAct", eng.newFunction(getParentAct));
      qDebug() << eng.evaluate("(function()

      { return getParentAct(); }

      )(123).arguments[0]").toNumber();

      Since 4.6, it prints "nan".
      Indeed, the object returned from getParentAct() will be the global object, not the activation object associated with the JS anonymous function call.
      This is because JavaScriptCore optimizes away the activation object, because it doesn't think it is needed (since none of the JS code requires it).

      In order to guarantee the 4.5 behavior we would have to turn off the optimization, which would hurt performance in the common case (most native functions don't need to access the caller's activation).
      An alternative would be to make it possible to turn it on/off explicitly.

      One would think that this could be worked around by creating a closure in the caller, since that forces JSC to create the activation object. E.g. changing it to

      qDebug() << eng.evaluate("(function() { var tmp = function() {}; return getParentAct(); })(123).arguments[0]").toNumber();

      But this prints a junk value.

      That can be worked around by "touching" the arguments object from JS:

      qDebug() << eng.evaluate("(function() { var tmp = function() {}; arguments[0]; return getParentAct(); })(123).arguments[0]").toNumber(); // "123"

      So it looks like the native version is not "detaching" the arguments object from the JS stack when the function returns.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              Unassigned Unassigned
              kenthans Kent Hansen (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes