github.com/goplus/llgo@v0.8.3/internal/runtime/z_iface.go (about) 1 /* 2 * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package runtime 18 19 import ( 20 "unsafe" 21 22 "github.com/goplus/llgo/internal/abi" 23 ) 24 25 // ----------------------------------------------------------------------------- 26 27 type InterfaceType = abi.InterfaceType 28 29 var ( 30 TyAny = &InterfaceType{} 31 ) 32 33 // ----------------------------------------------------------------------------- 34 35 type Interface = iface 36 37 func MakeAnyInt(typ *Type, data uintptr) Interface { 38 tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} 39 return Interface{ 40 tab: tab, data: unsafe.Pointer(data), 41 } 42 } 43 44 func MakeAnyString(data string) Interface { 45 typ := basicTypes[abi.String] 46 tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} 47 return Interface{ 48 tab: tab, data: unsafe.Pointer(&data), 49 } 50 } 51 52 func MakeAny(typ *Type, data unsafe.Pointer) Interface { 53 tab := &itab{inter: TyAny, _type: typ, hash: 0, fun: [1]uintptr{0}} 54 return Interface{ 55 tab: tab, data: data, 56 } 57 } 58 59 func MakeInterface(inter *InterfaceType, typ *Type, data unsafe.Pointer) Interface { 60 tab := &itab{inter: inter, _type: typ, hash: 0, fun: [1]uintptr{0}} 61 return Interface{ 62 tab: tab, data: data, 63 } 64 } 65 66 func I2Int(v Interface, t *Type) uintptr { 67 if v.tab._type == t { 68 return uintptr(v.data) 69 } 70 panic("I2Int: type mismatch") 71 } 72 73 func CheckI2Int(v Interface, t *Type) (uintptr, bool) { 74 if v.tab._type == t { 75 return uintptr(v.data), true 76 } 77 return 0, false 78 } 79 80 func I2String(v Interface, t *Type) string { 81 if v.tab._type == t { 82 return *(*string)(v.data) 83 } 84 panic("I2String: type mismatch") 85 } 86 87 func CheckI2String(v Interface, t *Type) (string, bool) { 88 if v.tab._type == t { 89 return *(*string)(v.data), true 90 } 91 return "", false 92 } 93 94 // -----------------------------------------------------------------------------