michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: BEGIN_TEST(testSetProperty_NativeGetterStubSetter) michael@0: { michael@0: JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: CHECK(obj); michael@0: michael@0: CHECK(JS_DefineProperty(cx, global, "globalProp", obj, JSPROP_ENUMERATE, michael@0: JS_PropertyStub, JS_StrictPropertyStub)); michael@0: michael@0: CHECK(JS_DefineProperty(cx, obj, "prop", JS::UndefinedHandleValue, JSPROP_SHARED, michael@0: NativeGet, JS_StrictPropertyStub)); michael@0: michael@0: EXEC("'use strict'; \n" michael@0: "var error, passed = false; \n" michael@0: "try \n" michael@0: "{ \n" michael@0: " this.globalProp.prop = 42; \n" michael@0: " throw new Error('setting property succeeded!'); \n" michael@0: "} \n" michael@0: "catch (e) \n" michael@0: "{ \n" michael@0: " error = e; \n" michael@0: " if (e instanceof TypeError) \n" michael@0: " passed = true; \n" michael@0: "} \n" michael@0: " \n" michael@0: "if (!passed) \n" michael@0: " throw error; \n"); michael@0: michael@0: EXEC("var error, passed = false; \n" michael@0: "try \n" michael@0: "{ \n" michael@0: " this.globalProp.prop = 42; \n" michael@0: " if (this.globalProp.prop === 17) \n" michael@0: " passed = true; \n" michael@0: " else \n" michael@0: " throw new Error('bad value after set!'); \n" michael@0: "} \n" michael@0: "catch (e) \n" michael@0: "{ \n" michael@0: " error = e; \n" michael@0: "} \n" michael@0: " \n" michael@0: "if (!passed) \n" michael@0: " throw error; \n"); michael@0: michael@0: return true; michael@0: } michael@0: static bool michael@0: NativeGet(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp) michael@0: { michael@0: vp.set(INT_TO_JSVAL(17)); michael@0: return true; michael@0: } michael@0: END_TEST(testSetProperty_NativeGetterStubSetter) michael@0: michael@0: BEGIN_TEST(testSetProperty_InheritedGlobalSetter) michael@0: { michael@0: // This is a JSAPI test because jsapi-test globals do not have a resolve michael@0: // hook and therefore can use the property cache in some cases where the michael@0: // shell can't. michael@0: JS_ASSERT(JS_GetClass(global)->resolve == &JS_ResolveStub); michael@0: michael@0: CHECK(JS_DefineProperty(cx, global, "HOTLOOP", 8, 0)); michael@0: EXEC("var n = 0;\n" michael@0: "var global = this;\n" michael@0: "function f() { n++; }\n" michael@0: "Object.defineProperty(Object.prototype, 'x', {set: f});\n" michael@0: "for (var i = 0; i < HOTLOOP; i++)\n" michael@0: " global.x = i;\n"); michael@0: EXEC("if (n != HOTLOOP)\n" michael@0: " throw 'FAIL';\n"); michael@0: return true; michael@0: } michael@0: END_TEST(testSetProperty_InheritedGlobalSetter) michael@0: