1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16function null_target () { 17 assert (new.target === undefined); 18} 19 20function demo () { 21 null_target (); 22 return new.target; 23} 24 25assert (demo () === undefined); 26assert ((new demo ()) === demo); 27 28/* new.target is only valid inside functions */ 29try { 30 eval ("new.target"); 31 assert (false); 32} catch (ex) { 33 assert (ex instanceof SyntaxError); 34} 35 36try { 37 var eval_other = eval; 38 eval_other ("new.target"); 39 assert (false); 40} catch (ex) { 41 assert (ex instanceof SyntaxError); 42} 43 44/* test with arrow function */ 45var arrow_called = false; 46function arrow () { 47 assert (new.target === arrow); 48 var mth = () => { return new.target; } 49 assert (mth () === arrow); 50 arrow_called = true; 51} 52 53new arrow (); 54assert (arrow_called === true); 55 56/* test unary operation */ 57var f_called = false; 58function f () { 59 assert (isNaN (-new.target)); 60 f_called = true; 61} 62 63new f (); 64assert (f_called === true); 65 66/* test property access */ 67function fg (callback_object) { 68 callback_object.value = new.target.value; 69} 70 71fg.value = 22; 72 73var test_obj = {}; 74new fg (test_obj); 75 76assert (test_obj.value === 22); 77 78 79/* test new.target with eval */ 80function eval_test () { 81 var target = eval ("new.target"); 82 assert (target === eval_test); 83} 84 85new eval_test (); 86 87function eval_eval_test () { 88 var target = eval ('eval("new.target")'); 89 assert (target === eval_eval_test); 90} 91 92new eval_eval_test (); 93 94/* new.target is only valid in direct eval */ 95function eval_test_2 () { 96 var ev = eval; 97 try { 98 ev ("new.target"); 99 assert (false); 100 } catch (ex) { 101 assert (ex instanceof SyntaxError); 102 } 103} 104 105new eval_test_2 (); 106 107function eval_test_3 () { 108 var ev = eval; 109 try { 110 eval ("ev ('new.target')"); 111 assert (false); 112 } catch (ex) { 113 assert (ex instanceof SyntaxError); 114 } 115} 116 117new eval_test_3 (); 118 119/* test assignment of the "new.target" */ 120function expect_syntax_error (src) 121{ 122 try { 123 eval (src); 124 assert (false); 125 } catch (ex) { 126 assert (ex instanceof SyntaxError); 127 } 128} 129 130expect_syntax_error ("function assign () { new.target = 3; }"); 131expect_syntax_error ("function assign () { new.target += 3; }"); 132expect_syntax_error ("function assign () { new.target *= 3; }"); 133expect_syntax_error ("function assign () { new.target -= 3; }"); 134expect_syntax_error ("function assign () { new.target |= 3; }"); 135expect_syntax_error ("function assign () { new.target &= 3; }"); 136 137expect_syntax_error ("function assign () { new.target++; }"); 138expect_syntax_error ("function assign () { ++new.target; }"); 139expect_syntax_error ("function assign () { new.target--; }"); 140expect_syntax_error ("function assign () { --new.target; }"); 141 142expect_syntax_error ("function synt () { new....target; }"); 143 144function delete_test () { 145 assert ((delete new.target) === true); 146} 147 148new delete_test (); 149 150function binary_test_1 () { 151 /*/ new.target is converted to string */ 152 assert ((new.target + 1) === "function(){/* ecmascript */}1"); 153} 154function binary_test_2 () { assert (isNaN (new.target - 3)); } 155function binary_test_3 () { assert (isNaN (new.target * 2)); } 156function binary_test_4 () { assert (isNaN (new.target / 4)); } 157 158new binary_test_1 (); 159new binary_test_2 (); 160new binary_test_3 (); 161new binary_test_4 (); 162