Skip to content

objc: add protocol support #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions objc/objc_runtime_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ var (
objc_msgSend = purego.Dlsym(objc, "objc_msgSend")
objc_msgSendSuper2 = purego.Dlsym(objc, "objc_msgSendSuper2")
objc_getClass = purego.Dlsym(objc, "objc_getClass")
objc_getProtocol = purego.Dlsym(objc, "objc_getProtocol")
objc_allocateClassPair = purego.Dlsym(objc, "objc_allocateClassPair")
objc_registerClassPair = purego.Dlsym(objc, "objc_registerClassPair")
sel_registerName = purego.Dlsym(objc, "sel_registerName")
class_getSuperclass = purego.Dlsym(objc, "class_getSuperclass")
class_getInstanceVariable = purego.Dlsym(objc, "class_getInstanceVariable")
class_addMethod = purego.Dlsym(objc, "class_addMethod")
class_addIvar = purego.Dlsym(objc, "class_addIvar")
class_addProtocol = purego.Dlsym(objc, "class_addProtocol")
ivar_getOffset = purego.Dlsym(objc, "ivar_getOffset")
object_getClass = purego.Dlsym(objc, "object_getClass")
)
Expand Down Expand Up @@ -189,6 +191,14 @@ func (c Class) AddIvar(name string, ty interface{}, types string) bool {
return byte(ret) != 0
}

// AddProtocol adds a protocol to a class.
// Returns true if the protocol was added successfully, otherwise false (for example,
// the class already conforms to that protocol).
func (c Class) AddProtocol(protocol *Protocol) bool {
ret, _, _ := purego.SyscallN(class_addProtocol, uintptr(c), uintptr(unsafe.Pointer(protocol)))
return byte(ret) != 0
}

// InstanceVariable returns an Ivar data structure containing information about the instance variable specified by name.
func (c Class) InstanceVariable(name string) Ivar {
n := strings.CString(name)
Expand All @@ -215,6 +225,18 @@ func (i Ivar) Offset() uintptr {
return ret
}

// Protocol is a type that declares methods that can be implemented by any class.
type Protocol uintptr

// GetProtocol returns the protocol for the given name or nil if there is no protocol by that name.
func GetProtocol(name string) *Protocol {
n := strings.CString(name)
p, _, _ := purego.SyscallN(objc_getProtocol, uintptr(unsafe.Pointer(n)))
runtime.KeepAlive(n)
// We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer
return *(**Protocol)(unsafe.Pointer(&p))
}

// IMP is a function pointer that can be called by Objective-C code.
type IMP uintptr

Expand Down