Bug 1716030 [wpt PR 29346] - Update wpt metadata, a=testonly
[gecko.git] / js / src / jsapi-tests / testWindowNonConfigurable.cpp
blob071b9e01593075d2fb8fe2b26efdd125f465449a
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/. */
8 #include "js/PropertyAndElement.h" // JS_DefineProperty
9 #include "jsapi-tests/tests.h"
11 static bool windowProxy_defineProperty(JSContext* cx, JS::HandleObject obj,
12 JS::HandleId id,
13 JS::Handle<JS::PropertyDescriptor> desc,
14 JS::ObjectOpResult& result) {
15 if (desc.hasConfigurable() && !desc.configurable()) {
16 result.failCantDefineWindowNonConfigurable();
17 return true;
20 return NativeDefineProperty(cx, obj.as<js::NativeObject>(), id, desc, result);
23 static const js::ObjectOps windowProxy_objectOps = {
24 nullptr, // lookupProperty
25 windowProxy_defineProperty, // defineProperty
26 nullptr, // hasProperty
27 nullptr, // getProperty
28 nullptr, // setProperty
29 nullptr, // getOwnPropertyDescriptor
30 nullptr, // deleteProperty
31 nullptr, // getElements
32 nullptr, // funToString
35 static const JSClass windowProxy_class = {
36 "WindowProxy", 0, nullptr, nullptr, nullptr, &windowProxy_objectOps};
38 BEGIN_TEST(testWindowNonConfigurable) {
39 JS::RootedObject obj(cx, JS_NewObject(cx, &windowProxy_class));
40 CHECK(obj);
41 CHECK(JS_DefineProperty(cx, global, "windowProxy", obj, 0));
42 JS::RootedValue v(cx);
43 EVAL(
44 "Object.defineProperty(windowProxy, 'bar', {value: 1, configurable: "
45 "false})",
46 &v);
47 CHECK(v.isNull()); // This is the important bit!
48 EVAL(
49 "Object.defineProperty(windowProxy, 'bar', {value: 1, configurable: "
50 "true})",
51 &v);
52 CHECK(&v.toObject() == obj);
53 EVAL(
54 "Reflect.defineProperty(windowProxy, 'foo', {value: 1, configurable: "
55 "false})",
56 &v);
57 CHECK(v.isFalse());
58 EVAL(
59 "Reflect.defineProperty(windowProxy, 'foo', {value: 1, configurable: "
60 "true})",
61 &v);
62 CHECK(v.isTrue());
64 return true;
66 END_TEST(testWindowNonConfigurable)