qtdeclarative/tests/auto/qml/qjsengine
Ivan Tkachenko d7602cb315 Set correct `this` value in JSON.stringify replacer scope
Long story short, JSON.stringify takes optional second argument which
can be a replacer function. It is supposed to be called for each
encountered key-value during serialization. While replacer's second
argument is already serialized (so you could just return it), just in
case you are not satisfied with default serialization — the original
value is still passed in together with the whole object as a value of
JavaScript `this`.

Sadly, there is no test in the whole ECMA test suite to catch this bug.
This is quite a breaking change for code which already uses Qt-specific
workarounds, so it would be better not to cherry-pick it as a hot-fix
onto existing (released) branches.

Sample use case: serialize dates as a number of seconds since epoch.

  function replacer(k, v) {
    if (this[k] instanceof Date) {
      return Math.floor(this[k].getTime() / 1000.0);
    }
    return v;
  }
  const str = JSON.stringify(obj, replacer);

[ChangeLog][QML][Important Behavior Changes] Set correct `this` value in
JSON.stringify replacer scope, so that the value for current key in
current object is no longer pre-stringified, and can actually be used to
implement custom object serialization logic.

Fixes: QTBUG-95324
Change-Id: I618a533e8eba7999d5416aca14f4971093a83f7a
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
(cherry picked from commit 4c930247e6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2023-02-22 17:38:01 +00:00
..
script/com
translations Avoid context truncation when filename has multiple dots 2022-09-28 21:46:24 +00:00
CMakeLists.txt Change the license of all CMakeLists.txt and *.cmake files to BSD 2022-09-07 17:01:30 +02:00
dummy_imports.qml Rename android_dummy_imports.qml to dummy_imports.qml 2022-03-05 15:23:48 +00:00
exporterror1.mjs Fix error reporting when imports or re-exports in modules fail 2018-10-11 14:40:18 +00:00
idtranslatable-unicode.js
idtranslatable.js
importerror1.mjs Fix error reporting when imports or re-exports in modules fail 2018-10-11 14:40:18 +00:00
modulewithlexicals.mjs Fix use of lexically scoped variables in modules 2018-09-23 20:27:40 +00:00
testmodule.mjs Add API to QJSEngine for importing ECMAScript modules 2018-08-16 11:05:28 +00:00
testregister.mjs Add QJSEngine::registerModule 2021-05-01 17:26:00 -04:00
testregister2.mjs Add QJSEngine::registerModule 2021-05-01 17:26:00 -04:00
testregister3.mjs Add QJSEngine::registerModule 2021-05-01 17:26:00 -04:00
translatable-unicode.js
translatable.1.0.js Avoid context truncation when filename has multiple dots 2022-09-28 21:46:24 +00:00
translatable.js
translatable2.js
tst_qjsengine.cpp Set correct `this` value in JSON.stringify replacer scope 2023-02-22 17:38:01 +00:00