| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package objc |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"reflect" |
| 9 |
"sync" |
| 10 |
"unsafe" |
| 11 |
|
| 12 |
"github.com/ebitengine/purego" |
| 13 |
"github.com/ebitengine/purego/internal/xreflect" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
blockBaseClass = "__NSMallocBlock__" |
| 23 |
|
| 24 |
blockFlags = blockHasCopyDispose | blockHasSignature |
| 25 |
|
| 26 |
|
| 27 |
blockHasCopyDispose = 1 << 25 |
| 28 |
|
| 29 |
|
| 30 |
blockHasSignature = 1 << 30 |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
type blockDescriptor struct { |
| 38 |
_ uintptr |
| 39 |
size uintptr |
| 40 |
_ uintptr |
| 41 |
dispose uintptr |
| 42 |
signature *uint8 |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
type blockLayout struct { |
| 51 |
isa Class |
| 52 |
flags uint32 |
| 53 |
_ uint32 |
| 54 |
invoke uintptr |
| 55 |
descriptor *blockDescriptor |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
type blockFunctionCache struct { |
| 64 |
mutex sync.RWMutex |
| 65 |
functions map[Block]reflect.Value |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (b *blockFunctionCache) Load(key Block) reflect.Value { |
| 70 |
b.mutex.RLock() |
| 71 |
defer b.mutex.RUnlock() |
| 72 |
return b.functions[key] |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
func (b *blockFunctionCache) Store(key Block, value reflect.Value) Block { |
| 77 |
b.mutex.Lock() |
| 78 |
defer b.mutex.Unlock() |
| 79 |
b.functions[key] = value |
| 80 |
return key |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (b *blockFunctionCache) Delete(key Block) { |
| 85 |
b.mutex.Lock() |
| 86 |
defer b.mutex.Unlock() |
| 87 |
delete(b.functions, key) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func newBlockFunctionCache() *blockFunctionCache { |
| 92 |
return &blockFunctionCache{functions: map[Block]reflect.Value{}} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
type blockCache struct { |
| 101 |
sync.Mutex |
| 102 |
descriptorTemplate blockDescriptor |
| 103 |
layoutTemplate blockLayout |
| 104 |
layouts map[reflect.Type]blockLayout |
| 105 |
Functions *blockFunctionCache |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func (*blockCache) encode(typ reflect.Type) *uint8 { |
| 110 |
|
| 111 |
|
| 112 |
if typ == nil || typ.Kind() != reflect.Func { |
| 113 |
panic("objc: not a function") |
| 114 |
} |
| 115 |
|
| 116 |
var encoding string |
| 117 |
switch typ.NumOut() { |
| 118 |
case 0: |
| 119 |
encoding = encVoid |
| 120 |
default: |
| 121 |
returnType, err := encodeType(typ.Out(0), false) |
| 122 |
if err != nil { |
| 123 |
panic(fmt.Sprintf("objc: %v", err)) |
| 124 |
} |
| 125 |
encoding = returnType |
| 126 |
} |
| 127 |
|
| 128 |
if typ.NumIn() == 0 || typ.In(0) != reflect.TypeOf(Block(0)) { |
| 129 |
panic(fmt.Sprintf("objc: A Block implementation must take a Block as its first argument; got %v", typ.String())) |
| 130 |
} |
| 131 |
|
| 132 |
encoding += encId |
| 133 |
for i := 1; i < typ.NumIn(); i++ { |
| 134 |
argType, err := encodeType(typ.In(i), false) |
| 135 |
if err != nil { |
| 136 |
panic(fmt.Sprintf("objc: %v", err)) |
| 137 |
} |
| 138 |
encoding = fmt.Sprint(encoding, argType) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
return &append([]uint8(encoding), 0)[0] |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
func (b *blockCache) getLayout(typ reflect.Type) blockLayout { |
| 148 |
b.Lock() |
| 149 |
defer b.Unlock() |
| 150 |
|
| 151 |
|
| 152 |
if layout, ok := b.layouts[typ]; ok { |
| 153 |
return layout |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
layout := b.layoutTemplate |
| 158 |
layout.descriptor = &blockDescriptor{} |
| 159 |
*layout.descriptor = b.descriptorTemplate |
| 160 |
|
| 161 |
|
| 162 |
layout.descriptor.signature = b.encode(typ) |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
layout.invoke = purego.NewCallback( |
| 168 |
reflect.MakeFunc( |
| 169 |
typ, |
| 170 |
func(args []reflect.Value) (results []reflect.Value) { |
| 171 |
block, ok := xreflect.TypeAssert[Block](args[0]) |
| 172 |
if !ok { |
| 173 |
panic(fmt.Sprintf("objc: block argument is not a block but %s", args[0].Type().String())) |
| 174 |
} |
| 175 |
return b.Functions.Load(block).Call(args) |
| 176 |
}, |
| 177 |
).Interface(), |
| 178 |
) |
| 179 |
|
| 180 |
|
| 181 |
b.layouts[typ] = layout |
| 182 |
return layout |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
func newBlockCache() *blockCache { |
| 188 |
cache := &blockCache{ |
| 189 |
descriptorTemplate: blockDescriptor{ |
| 190 |
size: unsafe.Sizeof(blockLayout{}), |
| 191 |
}, |
| 192 |
layoutTemplate: blockLayout{ |
| 193 |
isa: GetClass(blockBaseClass), |
| 194 |
flags: blockFlags, |
| 195 |
}, |
| 196 |
layouts: map[reflect.Type]blockLayout{}, |
| 197 |
Functions: newBlockFunctionCache(), |
| 198 |
} |
| 199 |
cache.descriptorTemplate.dispose = purego.NewCallback(cache.Functions.Delete) |
| 200 |
return cache |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
var theBlocksCache *blockCache |
| 205 |
|
| 206 |
|
| 207 |
type Block ID |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func (b Block) Copy() Block { |
| 212 |
return _Block_copy(b) |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
func (b Block) Invoke(args ...any) { |
| 217 |
fn := theBlocksCache.Functions.Load(b) |
| 218 |
|
| 219 |
reflectedArgs := make([]reflect.Value, len(args)+1) |
| 220 |
reflectedArgs[0] = reflect.ValueOf(b) |
| 221 |
for i := range args { |
| 222 |
reflectedArgs[i+1] = reflect.ValueOf(args[i]) |
| 223 |
} |
| 224 |
|
| 225 |
fn.Call(reflectedArgs) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
func (b Block) Release() { |
| 230 |
_Block_release(b) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
func NewBlock(fn any) Block { |
| 238 |
|
| 239 |
layout := theBlocksCache.getLayout(reflect.TypeOf(fn)) |
| 240 |
|
| 241 |
block := Block(unsafe.Pointer(&layout)).Copy() |
| 242 |
|
| 243 |
return theBlocksCache.Functions.Store(block, reflect.ValueOf(fn)) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
func InvokeBlock[T any](block Block, args ...any) (result T, err error) { |
| 249 |
block = block.Copy() |
| 250 |
defer block.Release() |
| 251 |
|
| 252 |
fn := theBlocksCache.Functions.Load(block) |
| 253 |
if fn.Type().NumIn() != len(args)+1 { |
| 254 |
return result, fmt.Errorf("objc: block callback expects %d arguments, got %d", fn.Type().NumIn()-1, len(args)) |
| 255 |
} |
| 256 |
|
| 257 |
reflectedArgs := make([]reflect.Value, len(args)+1) |
| 258 |
reflectedArgs[0] = reflect.ValueOf(block) |
| 259 |
for i := range args { |
| 260 |
reflectedArgs[i+1] = reflect.ValueOf(args[i]) |
| 261 |
} |
| 262 |
|
| 263 |
callResult := fn.Call(reflectedArgs) |
| 264 |
|
| 265 |
var ok bool |
| 266 |
result, ok = xreflect.TypeAssert[T](callResult[0]) |
| 267 |
if !ok { |
| 268 |
return result, fmt.Errorf("objc: the returned value type %s was not %T", callResult[0].Type().String(), result) |
| 269 |
} |
| 270 |
|
| 271 |
return result, nil |
| 272 |
} |