testDifferentNewTargetInvokeConstructor.cpp (1126B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "js/CallAndConstruct.h" // JS::Construct 6 #include "jsapi-tests/tests.h" 7 8 BEGIN_TEST(testDifferentNewTargetInvokeConstructor) { 9 JS::RootedValue func(cx); 10 JS::RootedValue otherFunc(cx); 11 12 EVAL("(function() { /* This is a different new.target function */ })", 13 &otherFunc); 14 15 EVAL( 16 "(function(expected) { if (expected !== new.target) throw new " 17 "Error('whoops'); })", 18 &func); 19 20 JS::RootedValueArray<1> args(cx); 21 args[0].set(otherFunc); 22 23 JS::RootedObject obj(cx); 24 25 JS::RootedObject newTarget(cx, &otherFunc.toObject()); 26 27 CHECK(JS::Construct(cx, func, newTarget, args, &obj)); 28 29 // It should fail, though, if newTarget is not a constructor 30 JS::RootedValue plain(cx); 31 EVAL("({})", &plain); 32 args[0].set(plain); 33 newTarget = &plain.toObject(); 34 CHECK(!JS::Construct(cx, func, newTarget, args, &obj)); 35 36 return true; 37 } 38 END_TEST(testDifferentNewTargetInvokeConstructor)