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:
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"
13 BEGIN_TEST(testForwardSetProperty
) {
17 "var obj1 = { set prop(val) { foundValue = this; } }; \n"
23 "var obj2 = Object.create(obj1); \n"
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
);
43 CHECK(JS_ValueToId(cx
, propkey
, &prop
));
46 "function assertEq(a, b, msg) \n"
48 " if (!Object.is(a, b)) \n"
49 " throw new Error('Assertion failure: ' + msg); \n"
54 JS::ObjectOpResult result
;
55 CHECK(JS_ForwardSetPropertyTo(cx
, obj2
, prop
, setval
, v3
, result
));
58 EXEC("assertEq(foundValue, obj3, 'wrong receiver passed to setter');");
60 CHECK(JS_ForwardSetPropertyTo(cx
, obj2
, prop
, setval
, setval
, result
));
64 "assertEq(typeof foundValue === 'object', true, \n"
65 " 'passing 42 as receiver to non-strict setter ' + \n"
69 "assertEq(foundValue instanceof Number, true, \n"
70 " 'passing 42 as receiver to non-strict setter ' + \n"
71 " 'must box to a Number');");
74 "assertEq(foundValue.valueOf(), 42, \n"
75 " 'passing 42 as receiver to non-strict setter ' + \n"
76 " 'must box to new Number(42)');");
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
));
87 EXEC("assertEq(foundValue, obj3, 'wrong receiver passed to strict setter');");
89 CHECK(JS_ForwardSetPropertyTo(cx
, obj4
, prop
, setval
, setval
, result
));
93 "assertEq(foundValue, 42, \n"
94 " '42 passed as receiver to strict setter was mangled');");
98 END_TEST(testForwardSetProperty
)