Bug 1931736. Add missing braces around if/loop statements in layout/xul/. r=layout...
[gecko.git] / js / src / jsapi-tests / testForwardSetProperty.cpp
blob0ab221a0c1ede3911848b63cab3c3f32f0fdf332
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_ForwardSetPropertyTo
9 #include "jsapi-tests/tests.h"
11 using namespace JS;
13 BEGIN_TEST(testForwardSetProperty) {
14 RootedValue v1(cx);
15 EVAL(
16 "var foundValue; \n"
17 "var obj1 = { set prop(val) { foundValue = this; } }; \n"
18 "obj1;",
19 &v1);
21 RootedValue v2(cx);
22 EVAL(
23 "var obj2 = Object.create(obj1); \n"
24 "obj2;",
25 &v2);
27 RootedValue v3(cx);
28 EVAL(
29 "var obj3 = {}; \n"
30 "obj3;",
31 &v3);
33 RootedObject obj1(cx, &v1.toObject());
34 RootedObject obj2(cx, &v2.toObject());
35 RootedObject obj3(cx, &v3.toObject());
37 RootedValue setval(cx, Int32Value(42));
39 RootedValue propkey(cx);
40 EVAL("'prop';", &propkey);
42 RootedId prop(cx);
43 CHECK(JS_ValueToId(cx, propkey, &prop));
45 EXEC(
46 "function assertEq(a, b, msg) \n"
47 "{ \n"
48 " if (!Object.is(a, b)) \n"
49 " throw new Error('Assertion failure: ' + msg); \n"
50 "}");
52 // Non-strict setter
54 JS::ObjectOpResult result;
55 CHECK(JS_ForwardSetPropertyTo(cx, obj2, prop, setval, v3, result));
56 CHECK(result);
58 EXEC("assertEq(foundValue, obj3, 'wrong receiver passed to setter');");
60 CHECK(JS_ForwardSetPropertyTo(cx, obj2, prop, setval, setval, result));
61 CHECK(result);
63 EXEC(
64 "assertEq(typeof foundValue === 'object', true, \n"
65 " 'passing 42 as receiver to non-strict setter ' + \n"
66 " 'must box');");
68 EXEC(
69 "assertEq(foundValue instanceof Number, true, \n"
70 " 'passing 42 as receiver to non-strict setter ' + \n"
71 " 'must box to a Number');");
73 EXEC(
74 "assertEq(foundValue.valueOf(), 42, \n"
75 " 'passing 42 as receiver to non-strict setter ' + \n"
76 " 'must box to new Number(42)');");
78 // Strict setter
80 RootedValue v4(cx);
81 EVAL("({ set prop(val) { 'use strict'; foundValue = this; } })", &v4);
82 RootedObject obj4(cx, &v4.toObject());
84 CHECK(JS_ForwardSetPropertyTo(cx, obj4, prop, setval, v3, result));
85 CHECK(result);
87 EXEC("assertEq(foundValue, obj3, 'wrong receiver passed to strict setter');");
89 CHECK(JS_ForwardSetPropertyTo(cx, obj4, prop, setval, setval, result));
90 CHECK(result);
92 EXEC(
93 "assertEq(foundValue, 42, \n"
94 " '42 passed as receiver to strict setter was mangled');");
96 return true;
98 END_TEST(testForwardSetProperty)