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 NoParent () { } 17 18Object.defineProperty (NoParent, Symbol.hasInstance, { 19 value: function (arg) { return false; } 20}); 21 22var obj = new NoParent (); 23 24assert ((obj instanceof NoParent) === false); 25 26try { 27 Object.defineProperty (NoParent, Symbol.hasInstance, { 28 value: function (arg) { return true; } 29 }); 30 assert (false) 31} catch (ex) { 32 assert (ex instanceof TypeError); 33} 34 35 36function PositiveNumber () { } 37 38Object.defineProperty (PositiveNumber, Symbol.hasInstance, { 39 value: function (arg) { return (arg instanceof Number) && (arg >= 0); } 40}) 41 42var num_a = new Number (33); 43var num_b = new Number (-50); 44 45assert ((num_a instanceof PositiveNumber) === true); 46assert ((num_b instanceof PositiveNumber) === false); 47 48 49function ErrorAlways () { } 50 51Object.defineProperty (ErrorAlways, Symbol.hasInstance, { 52 value: function (arg) { throw new URIError ("ErrorAlways"); } 53}) 54 55try { 56 (new Object ()) instanceof ErrorAlways; 57 assert (false); 58} catch (ex) { 59 assert (ex instanceof URIError); 60} 61 62 63function NonCallable () { } 64 65Object.defineProperty (NonCallable, Symbol.hasInstance, { value: 11 }); 66 67try { 68 (new Object ()) instanceof NonCallable; 69 assert (false); 70} catch (ex) { 71 assert (ex instanceof TypeError); 72} 73 74 75function ErrorGenerator () { } 76 77Object.defineProperty (ErrorGenerator, Symbol.hasInstance, { 78 get: function () { throw new URIError ("ErrorGenerator"); } 79}); 80 81try { 82 (new Object ()) instanceof ErrorGenerator; 83 assert (false); 84} catch (ex) { 85 assert (ex instanceof URIError); 86} 87