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

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

mercurial