js/src/jsapi-tests/testSetProperty.cpp
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Wed, 29 Jun 2022 07:00:16 +0000
changeset 622411 8e5247451c9a83abe6f877f5f2c62332a84c9aaf
parent 611050 cb11b00621a21a44e2cbafab4599ca7a41427e35
permissions -rw-r--r--
no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD es-MX -> 9a31632cf87a851a1098be4b8f4124daab607967 ko -> 217a9bde95e1cfafd86cf9e09599e5fe838da2c6 sl -> 67cecc7d62a21ddc3b8942f1e3741df67933c53b zh-TW -> 4955eee5c0cc3d8df3b5e06065319552c908f981

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "js/Object.h"              // JS::GetClass
#include "js/PropertyAndElement.h"  // JS_DefineProperty
#include "jsapi-tests/tests.h"

BEGIN_TEST(testSetProperty_InheritedGlobalSetter) {
  // This is a JSAPI test because jsapi-test globals can be set up to not have
  // a resolve hook and therefore can use the property cache in some cases
  // where the shell can't.
  MOZ_RELEASE_ASSERT(!JS::GetClass(global)->getResolve());

  CHECK(JS::InitRealmStandardClasses(cx));

  CHECK(JS_DefineProperty(cx, global, "HOTLOOP", 8, 0));
  EXEC(
      "var n = 0;\n"
      "var global = this;\n"
      "function f() { n++; }\n"
      "Object.defineProperty(Object.prototype, 'x', {set: f});\n"
      "for (var i = 0; i < HOTLOOP; i++)\n"
      "    global.x = i;\n");
  EXEC(
      "if (n != HOTLOOP)\n"
      "    throw 'FAIL';\n");
  return true;
}

const JSClass* getGlobalClass(void) override {
  static const JSClassOps noResolveGlobalClassOps = {
      nullptr,                   // addProperty
      nullptr,                   // delProperty
      nullptr,                   // enumerate
      nullptr,                   // newEnumerate
      nullptr,                   // resolve
      nullptr,                   // mayResolve
      nullptr,                   // finalize
      nullptr,                   // call
      nullptr,                   // construct
      JS_GlobalObjectTraceHook,  // trace
  };

  static const JSClass noResolveGlobalClass = {
      "testSetProperty_InheritedGlobalSetter_noResolveGlobalClass",
      JSCLASS_GLOBAL_FLAGS, &noResolveGlobalClassOps};

  return &noResolveGlobalClass;
}
END_TEST(testSetProperty_InheritedGlobalSetter)