1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 16/* 17 * @tc.name:instanceofic 18 * @tc.desc:test instanceofic 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22 23class F { 24 static [Symbol.hasInstance](instance) { 25 return Array.isArray(instance); 26 } 27}; 28let res; 29 30for (let i = 300; i > 0; --i) { 31 res = [] instanceof F; 32 if (i == 150) { 33 if (res) { 34 print("instanceoficsuccess"); 35 } else { 36 print("instanceoficfailse"); 37 } 38 F[Symbol.hasInstance] = function() { return false }; 39 } 40 if (i == 140) { 41 if (!res) { 42 print("instanceoficsuccess"); 43 } else { 44 print("instanceoficfailse"); 45 } 46 } 47} 48 49let e = new Error("instanceof ic failed") 50for (let index = 0; index <= 30; index++) { 51 if ((e instanceof URIError) === true){ 52 continue 53 } 54} 55 56print("test success") 57 58for (let index = 0; index <= 30; index++) { 59 function foo() { 60 try { 61 const obj = {}; 62 obj instanceof {}; 63 } catch (e) { 64 65 } 66 } 67 foo(); 68} 69print("test success") 70 71// Test instanceof when hasInstance is modified uncallable 72var instanceObj = { 73 [Symbol.hasInstance]: 1.1 74} 75try { 76 print({} instanceof instanceObj); 77} catch (e) { 78 print("CallbackFn is not callable"); 79} 80 81// Test instanceof if constructor's prototype has been modified 82function function_prototype_changed() { 83 84} 85try { 86 function_prototype_changed.prototype = 5; 87 Array instanceof function_prototype_changed; 88} catch (e) { 89 print(e.name) 90} 91 92var A = {}; 93function Func() { } 94Func.prototype = A; 95print(A instanceof Func); 96 97var proto_desc = Object.getOwnPropertyDescriptor(RegExp, "prototype"); 98var proto = proto_desc.value; 99print(proto instanceof RegExp); 100