testForwardSetProperty.cpp (2687B)
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_ForwardSetPropertyTo 9 #include "jsapi-tests/tests.h" 10 11 using namespace JS; 12 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); 20 21 RootedValue v2(cx); 22 EVAL( 23 "var obj2 = Object.create(obj1); \n" 24 "obj2;", 25 &v2); 26 27 RootedValue v3(cx); 28 EVAL( 29 "var obj3 = {}; \n" 30 "obj3;", 31 &v3); 32 33 RootedObject obj1(cx, &v1.toObject()); 34 RootedObject obj2(cx, &v2.toObject()); 35 RootedObject obj3(cx, &v3.toObject()); 36 37 RootedValue setval(cx, Int32Value(42)); 38 39 RootedValue propkey(cx); 40 EVAL("'prop';", &propkey); 41 42 RootedId prop(cx); 43 CHECK(JS_ValueToId(cx, propkey, &prop)); 44 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 "}"); 51 52 // Non-strict setter 53 54 JS::ObjectOpResult result; 55 CHECK(JS_ForwardSetPropertyTo(cx, obj2, prop, setval, v3, result)); 56 CHECK(result); 57 58 EXEC("assertEq(foundValue, obj3, 'wrong receiver passed to setter');"); 59 60 CHECK(JS_ForwardSetPropertyTo(cx, obj2, prop, setval, setval, result)); 61 CHECK(result); 62 63 EXEC( 64 "assertEq(typeof foundValue === 'object', true, \n" 65 " 'passing 42 as receiver to non-strict setter ' + \n" 66 " 'must box');"); 67 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');"); 72 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)');"); 77 78 // Strict setter 79 80 RootedValue v4(cx); 81 EVAL("({ set prop(val) { 'use strict'; foundValue = this; } })", &v4); 82 RootedObject obj4(cx, &v4.toObject()); 83 84 CHECK(JS_ForwardSetPropertyTo(cx, obj4, prop, setval, v3, result)); 85 CHECK(result); 86 87 EXEC("assertEq(foundValue, obj3, 'wrong receiver passed to strict setter');"); 88 89 CHECK(JS_ForwardSetPropertyTo(cx, obj4, prop, setval, setval, result)); 90 CHECK(result); 91 92 EXEC( 93 "assertEq(foundValue, 42, \n" 94 " '42 passed as receiver to strict setter was mangled');"); 95 96 return true; 97 } 98 END_TEST(testForwardSetProperty)