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 "jsapi-tests/tests.h"
9 
NativeGetterSetter(JSContext * cx,unsigned argc,JS::Value * vp)10 static bool NativeGetterSetter(JSContext* cx, unsigned argc, JS::Value* vp) {
11   return true;
12 }
13 
BEGIN_TEST(testDefineGetterSetterNonEnumerable)14 BEGIN_TEST(testDefineGetterSetterNonEnumerable) {
15   static const char PROPERTY_NAME[] = "foo";
16 
17   JS::RootedValue vobj(cx);
18   JS::RootedObject obj(cx, JS_NewPlainObject(cx));
19   CHECK(obj);
20   vobj.setObject(*obj);
21 
22   JSFunction* funGet = JS_NewFunction(cx, NativeGetterSetter, 0, 0, "get");
23   CHECK(funGet);
24   JS::RootedObject funGetObj(cx, JS_GetFunctionObject(funGet));
25   JS::RootedValue vget(cx, JS::ObjectValue(*funGetObj));
26 
27   JSFunction* funSet = JS_NewFunction(cx, NativeGetterSetter, 1, 0, "set");
28   CHECK(funSet);
29   JS::RootedObject funSetObj(cx, JS_GetFunctionObject(funSet));
30   JS::RootedValue vset(cx, JS::ObjectValue(*funSetObj));
31 
32   JS::RootedObject vObject(cx, vobj.toObjectOrNull());
33   CHECK(JS_DefineProperty(cx, vObject, PROPERTY_NAME, funGetObj, funSetObj,
34                           JSPROP_GETTER | JSPROP_SETTER | JSPROP_ENUMERATE));
35 
36   CHECK(JS_DefineProperty(cx, vObject, PROPERTY_NAME, funGetObj, funSetObj,
37                           JSPROP_GETTER | JSPROP_SETTER | JSPROP_PERMANENT));
38 
39   JS::Rooted<JS::PropertyDescriptor> desc(cx);
40   CHECK(JS_GetOwnPropertyDescriptor(cx, vObject, PROPERTY_NAME, &desc));
41   CHECK(desc.object());
42   CHECK(desc.hasGetterObject());
43   CHECK(desc.hasSetterObject());
44   CHECK(!desc.configurable());
45   CHECK(!desc.enumerable());
46 
47   return true;
48 }
49 END_TEST(testDefineGetterSetterNonEnumerable)
50