js/src/jsapi-tests/testSetProperty.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: set ts=8 sts=4 et sw=4 tw=99:
     3  */
     4 /* This Source Code Form is subject to the terms of the Mozilla Public
     5  * License, v. 2.0. If a copy of the MPL was not distributed with this
     6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     8 #include "jsapi-tests/tests.h"
    10 BEGIN_TEST(testSetProperty_NativeGetterStubSetter)
    11 {
    12     JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
    13     CHECK(obj);
    15     CHECK(JS_DefineProperty(cx, global, "globalProp", obj, JSPROP_ENUMERATE,
    16                             JS_PropertyStub, JS_StrictPropertyStub));
    18     CHECK(JS_DefineProperty(cx, obj, "prop", JS::UndefinedHandleValue, JSPROP_SHARED,
    19                             NativeGet, JS_StrictPropertyStub));
    21     EXEC("'use strict';                                     \n"
    22          "var error, passed = false;                        \n"
    23          "try                                               \n"
    24          "{                                                 \n"
    25          "  this.globalProp.prop = 42;                      \n"
    26          "  throw new Error('setting property succeeded!'); \n"
    27          "}                                                 \n"
    28          "catch (e)                                         \n"
    29          "{                                                 \n"
    30          "  error = e;                                      \n"
    31          "  if (e instanceof TypeError)                     \n"
    32          "    passed = true;                                \n"
    33          "}                                                 \n"
    34          "                                                  \n"
    35          "if (!passed)                                      \n"
    36          "  throw error;                                    \n");
    38     EXEC("var error, passed = false;                        \n"
    39          "try                                               \n"
    40          "{                                                 \n"
    41          "  this.globalProp.prop = 42;                      \n"
    42          "  if (this.globalProp.prop === 17)                \n"
    43          "    passed = true;                                \n"
    44          "  else                                            \n"
    45          "    throw new Error('bad value after set!');      \n"
    46          "}                                                 \n"
    47          "catch (e)                                         \n"
    48          "{                                                 \n"
    49          "  error = e;                                      \n"
    50          "}                                                 \n"
    51          "                                                  \n"
    52          "if (!passed)                                      \n"
    53          "  throw error;                                    \n");
    55     return true;
    56 }
    57 static bool
    58 NativeGet(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
    59 {
    60     vp.set(INT_TO_JSVAL(17));
    61     return true;
    62 }
    63 END_TEST(testSetProperty_NativeGetterStubSetter)
    65 BEGIN_TEST(testSetProperty_InheritedGlobalSetter)
    66 {
    67     // This is a JSAPI test because jsapi-test globals do not have a resolve
    68     // hook and therefore can use the property cache in some cases where the
    69     // shell can't.
    70     JS_ASSERT(JS_GetClass(global)->resolve == &JS_ResolveStub);
    72     CHECK(JS_DefineProperty(cx, global, "HOTLOOP", 8, 0));
    73     EXEC("var n = 0;\n"
    74          "var global = this;\n"
    75          "function f() { n++; }\n"
    76          "Object.defineProperty(Object.prototype, 'x', {set: f});\n"
    77          "for (var i = 0; i < HOTLOOP; i++)\n"
    78          "    global.x = i;\n");
    79     EXEC("if (n != HOTLOOP)\n"
    80          "    throw 'FAIL';\n");
    81     return true;
    82 }
    83 END_TEST(testSetProperty_InheritedGlobalSetter)

mercurial