1/* 2 * Copyright (c) 2024 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 16import type { D_Sft, D_Nft } from './sendable_function_dependencie'; 17import { 18 D_sf, Dv_sf, D_nf, Dv_nf, Dv_Sft, Dv_Nft, D_Sc, D_Nc, 19 D_sf as AS_D_sf, Dv_sf as AS_Dv_sf, D_nf as AS_D_nf, Dv_nf as AS_Dv_nf, 20 D_Sft as AS_D_Sft, Dv_Sft as AS_Dv_Sft, D_Nft as AS_D_Nft, Dv_Nft as AS_Dv_Nft, 21 D_Sc as AS_D_Sc, D_Nc as AS_D_Nc 22} from './sendable_function_dependencie'; 23 24// -------------------- check sendable-function declaration -------------------- // 25@Sendable 26function sf():void {} // OK 27@Sendable 28function stf<T>(p:T):T { 29 return p; 30} // OK 31@Sendable 32@Other // ERROR 33function sf2():void {} 34 35 36// check overloading 37@Sendable 38function sf3():void; // OK 39@Sendable 40function sf3():void; 41 42@Sendable 43function sf3():void {} 44 45@Sendable 46function sf4():void; 47function sf4():void; // ERROR 48@Sendable 49function sf4():void {} 50 51@Sendable 52function sf5():void; 53 54@Sendable 55@Other // ERROR 56function sf5():void; 57 58@Sendable 59function sf5():void {} 60 61function nf():void {} 62 63// -------------------- check sendable-typealias declaration -------------------- // 64@Sendable 65type Sft = () => void; // OK 66@Sendable 67type Sft2<T> = () => void; // OK 68@Sendable 69type Sft3 = <T>(p:T) => T; // OK 70@Sendable 71@Other // ERROR 72type Sft4 = () => void; 73@Sendable 74type Sft5 = number; // ERROR 75@Sendable 76type Sft6 = Sft; // ERROR 77type Sft7 = () => void; 78 79@Sendable 80type Sft8 = Sft4; // ERROR 81 82type Nft = ()=>void; 83 84// -------------------- check sendable-function closure -------------------- // 85@Sendable 86class Sc {} 87class Nc {} 88 89@Sendable 90class SendableClassClosure { 91 constructor() { 92 const a1:Sft = sf; // OK 93 const a2:Nft = nf; // ERROR 94 const a3:Sc = new Sc(); // OK 95 const a4:Nc = new Nc(); // ERROR 96 97 const b1:D_Sft = D_sf; // OK 98 const b2:D_Nft = D_nf; // OK 99 const b3:D_Sc = new D_Sc(); // OK 100 const b4:D_Nc = new D_Nc(); // OK 101 } 102 103 @nf 104 handle() {} 105} 106 107@Sendable 108@nf 109function SendableFunctionClosure() { 110 const a1:Sft = sf; // OK 111 const a2:Nft = nf; // ERROR 112 const a3:Sc = new Sc(); // OK 113 const a4:Nc = new Nc(); // ERROR 114 const b1:D_Sft = D_sf; // OK 115 const b2:D_Nft = D_nf; // OK 116 const b3:D_Sc = new D_Sc(); // OK 117 const b4:D_Nc = new D_Nc(); // OK 118} 119 120namespace ns { 121 @Sendable 122 function sf():void; 123 @Sendable 124 function sf():void {} 125} 126 127 128 129// -------------------- check sendable-typealias is sendaboe-data-type -------------------- // 130 131 132// check property 133@Sendable 134class Sc2<T> { 135 p1: Sft = sf; // OK 136 p2: D_Nft = sf; // ERROR 137} 138// check genericity 139new Sc2<Sft>(); 140new Sc2<D_Nft>(); // ERROR 141 142// -------------------- check sendable-function cannot operation property --------------------// 143sf.prop = 1; // ERROR 144D_nf.prop = 1; // OK 145D_sf.prop = 1; // ERROR 146AS_D_sf.prop = 1;// ERROR 147 148// -------------------- check sendable-function assignment --------------------// 149 150@Sendable 151function stf1<T>(p:T):T {return p;}; 152function ntf1<T>(p:T):T {return p}; 153// 154const of1 = ()=>{}; 155// 156@Sendable 157type Stft1 = <T>(p:T)=>T; 158@Sendable 159type Stft2<T> = ()=>void; 160// 161type Nft1 = ()=>void; 162type Nft2 = ()=>number; 163type Ntft1 = <T>(p:T)=>T; 164type Ntft2<T> = ()=>void; 165// 166type U_Sft1 = Sft | D_Sft; 167type U_Sft2 = Sft | Stft1; 168type U_Nft1 = Nft1 | Nft2; 169type U_Nft2 = Nft1 | Ntft1; 170type U_ft1 = Sft | Nft1; 171type U_ft2 = Sft | Stft1| Nft1 | Ntft1; 172// 173type TU_Sft1<T> = Sft | D_Sft; 174type TU_Sft2<T> = Sft | Stft1 | Stft1 ; 175type TU_Nft1<T> = Nft1 | Nft2; 176type TU_Nft2<T> = Nft1 | Ntft1| Ntft2<string>; 177type TU_ft1<T> = Sft | Stft1 | Nft1 | Ntft1; 178// 179type DU_Sft1 = U_Sft1 | U_Sft2; 180type DU_Sft2<T> = DU_Sft1 | TU_Sft1<T>; 181type DU_Sft3<T> = DU_Sft2<T> | TU_Sft2<T>; 182type DU_Nft<T> = DU_Sft3<T> | TU_Nft2<T>; 183// normal 184const a1: Sft = sf; // OK 185const a2: Sft = nf; // ERROR 186const a3: Sft = of1; // ERROR 187const a4: Nft1 = nf; // OK 188const a5: Sft = a1; // OK 189const a6: Sft = a4; // ERROR 190// generic 191const b1: Stft1 = stf1; // OK 192const b2: Stft1 = ntf1; // ERROR 193const b3: Ntft1 = ntf1; // OK 194const b4: Stft1 = b1; // OK 195const b5: Stft1 = b3; // ERROR 196// unite 197const f1: U_ft1 = sf; // OK 198const f2: U_ft2 = stf1; // OK 199const f3: U_ft1 = nf; // ERROR 200const f4: U_ft2 = ntf1; // ERROR 201const d1: U_Sft1 = sf; // OK 202const d2: U_Sft1 = nf; // ERROR 203const d3: U_Sft1 = of1; // ERROR 204const d4: U_Nft1 = sf; // OK 205const d5: U_Nft1 = nf; // OK 206const d6: U_Nft1 = of1; // OK 207const d7: U_Sft1 = d1; // OK 208const d18: U_Sft1 = d4; // ERROR 209const d9: U_Sft1 = f1; // ERROR 210// const d10: U_Sft1 = f2; // ERROR 211const e1: U_Sft2 = stf1; // OK 212const e2: U_Sft2 = ntf1; // ERROR 213const e3: U_Nft2 = ntf1; // OK 214const e4: U_Sft2 = e1; // OK 215const e5: U_Sft2 = e3; // ERROR 216const e6: U_Sft2 = f1; // ERROR 217const e7: U_Sft2 = f2; // ERROR 218// unite & generic 219const g1: TU_ft1<number> = sf; // OK 220const g2: TU_ft1<number> = stf1; // OK 221const g3: TU_ft1<number> = nf; // ERROR 222const h1: TU_Sft1<number> = sf; // OK 223const h2: TU_Sft1<number> = nf; // ERROR 224const h3: TU_Sft1<number> = of1; // ERROR 225const h4: TU_Nft1<number> = nf; // OK 226const h5: TU_Sft1<number> = h1; // OK 227const h6: TU_Sft1<number> = h4; // ERROR 228const h7: TU_Sft2<number> = stf1; // OK 229const h8: TU_Sft2<number> = ntf1; // ERROR 230