testDefineGetterSetterNonEnumerable.cpp (1864B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 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/. */ 7 8 #include "js/PropertyAndElement.h" // JS_DefineProperty 9 #include "js/PropertyDescriptor.h" // JS_GetOwnPropertyDescriptor 10 #include "jsapi-tests/tests.h" 11 12 static bool NativeGetterSetter(JSContext* cx, unsigned argc, JS::Value* vp) { 13 return true; 14 } 15 16 BEGIN_TEST(testDefineGetterSetterNonEnumerable) { 17 static const char PROPERTY_NAME[] = "foo"; 18 19 JS::RootedValue vobj(cx); 20 JS::RootedObject obj(cx, JS_NewPlainObject(cx)); 21 CHECK(obj); 22 vobj.setObject(*obj); 23 24 JSFunction* funGet = JS_NewFunction(cx, NativeGetterSetter, 0, 0, "get"); 25 CHECK(funGet); 26 JS::RootedObject funGetObj(cx, JS_GetFunctionObject(funGet)); 27 JS::RootedValue vget(cx, JS::ObjectValue(*funGetObj)); 28 29 JSFunction* funSet = JS_NewFunction(cx, NativeGetterSetter, 1, 0, "set"); 30 CHECK(funSet); 31 JS::RootedObject funSetObj(cx, JS_GetFunctionObject(funSet)); 32 JS::RootedValue vset(cx, JS::ObjectValue(*funSetObj)); 33 34 JS::RootedObject vObject(cx, vobj.toObjectOrNull()); 35 CHECK(JS_DefineProperty(cx, vObject, PROPERTY_NAME, funGetObj, funSetObj, 36 JSPROP_ENUMERATE)); 37 38 CHECK(JS_DefineProperty(cx, vObject, PROPERTY_NAME, funGetObj, funSetObj, 39 JSPROP_PERMANENT)); 40 41 JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> desc(cx); 42 CHECK(JS_GetOwnPropertyDescriptor(cx, vObject, PROPERTY_NAME, &desc)); 43 CHECK(desc.isSome()); 44 CHECK(desc->hasGetter()); 45 CHECK(desc->hasSetter()); 46 CHECK(!desc->configurable()); 47 CHECK(!desc->enumerable()); 48 49 return true; 50 } 51 END_TEST(testDefineGetterSetterNonEnumerable)