objc

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Rendered for darwin/amd64

Overview

Package objc is a low-level pure Go objective-c runtime. This package is easy to use incorrectly, so it is best to use a wrapper that provides the functionality you need in a safer way.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send[T any](id ID, sel SEL, args ...any) T

Send is a convenience method for sending messages to objects that can return any type. This function takes a SEL instead of a string since RegisterName grabs the global Objective-C lock. It is best to cache the result of RegisterName.

func SendSuper

func SendSuper[T any](id ID, sel SEL, args ...any) T

SendSuper is a convenience method for sending message to object's super that can return any type. This function takes a SEL instead of a string since RegisterName grabs the global Objective-C lock. It is best to cache the result of RegisterName.

Types

type Class

type Class uintptr

Class is an opaque type that represents an Objective-C class.

func AllocateClassPair deprecated

func AllocateClassPair(super Class, name string, extraBytes uintptr) Class

AllocateClassPair creates a new class and metaclass. Then returns the new class, or Nil if the class could not be created

Deprecated: use RegisterClass instead

Example
package main

import (
	"fmt"

	"github.com/FaceChainTeam/purego/objc"
)

func main() {
	class := objc.AllocateClassPair(objc.GetClass("NSObject"), "FooObject", 0)
	class.AddMethod(objc.RegisterName("run"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) {
		fmt.Println("Hello World!")
	}), "v@:")
	class.Register()

	fooObject := objc.ID(class).Send(objc.RegisterName("new"))
	fooObject.Send(objc.RegisterName("run"))
}
Output:

Hello World!

func GetClass

func GetClass(name string) Class

GetClass returns the Class object for the named class, or nil if the class is not registered with the Objective-C runtime.

func RegisterClass

func RegisterClass(name string, superClass Class, protocols []*Protocol, ivars []FieldDef, methods []MethodDef) (Class, error)

RegisterClass takes the name of the class to create, the superclass, a list of protocols this class implements, a list of fields this class has and a list of methods. It returns the created class or an error describing what went wrong.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/FaceChainTeam/purego/objc"
)

func main() {
	var (
		sel_new    = objc.RegisterName("new")
		sel_init   = objc.RegisterName("init")
		sel_setBar = objc.RegisterName("setBar:")
		sel_bar    = objc.RegisterName("bar")

		BarInit = func(id objc.ID, cmd objc.SEL) objc.ID {
			return id.SendSuper(cmd)
		}
	)

	class, err := objc.RegisterClass(
		"BarObject",
		objc.GetClass("NSObject"),
		[]*objc.Protocol{
			objc.GetProtocol("NSDelegateWindow"),
		},
		[]objc.FieldDef{
			{
				Name:      "bar",
				Type:      reflect.TypeOf(int(0)),
				Attribute: objc.ReadWrite,
			},
			{
				Name:      "foo",
				Type:      reflect.TypeOf(false),
				Attribute: objc.ReadWrite,
			},
		},
		[]objc.MethodDef{
			{
				Cmd: sel_init,
				Fn:  BarInit,
			},
		},
	)
	if err != nil {
		panic(err)
	}

	object := objc.ID(class).Send(sel_new)
	object.Send(sel_setBar, 123)
	bar := int(object.Send(sel_bar))
	fmt.Println(bar)
}
Output:

123

func (Class) AddIvar deprecated

func (c Class) AddIvar(name string, ty interface{}, types string) bool

AddIvar adds a new instance variable to a class. It may only be called after AllocateClassPair and before Register. Adding an instance variable to an existing class is not supported. The class must not be a metaclass. Adding an instance variable to a metaclass is not supported. It takes the instance of the type of the Ivar and a string representing the type.

Deprecated: use RegisterClass instead

func (Class) AddMethod

func (c Class) AddMethod(name SEL, imp IMP, types string) bool

AddMethod adds a new method to a class with a given name and implementation. The types argument is a string containing the mapping of parameters and return type. Since the function must take at least two arguments—self and _cmd, the second and third characters must be “@:” (the first character is the return type).

func (Class) AddProtocol

func (c Class) AddProtocol(protocol *Protocol) bool

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 (Class) InstanceSize

func (c Class) InstanceSize() uintptr

InstanceSize returns the size in bytes of instances of the class or 0 if cls is nil

func (Class) InstanceVariable

func (c Class) InstanceVariable(name string) Ivar

InstanceVariable returns an Ivar data structure containing information about the instance variable specified by name.

func (Class) Register deprecated

func (c Class) Register()

Register registers a class that was allocated using AllocateClassPair. It can now be used to make objects by sending it either alloc and init or new.

Deprecated: use RegisterClass instead

func (Class) SuperClass

func (c Class) SuperClass() Class

SuperClass returns the superclass of a class. You should usually use NSObject‘s superclass method instead of this function.

type FieldDef

type FieldDef struct {
	Name      string
	Type      reflect.Type
	Attribute IvarAttrib
}

FieldDef is a definition of a field to add to an Objective-C class. The name of the field is what will be used to access it through the Ivar. If the type is bool the name cannot start with `is` since a getter will be generated with the name `isBoolName`. The name also cannot contain any spaces. The type is the Go equivalent type of the Ivar. Attribute determines if a getter and or setter method is generated for this field.

type ID

type ID uintptr

ID is an opaque pointer to some Objective-C object

func (ID) Class

func (id ID) Class() Class

Class returns the class of the object.

func (ID) GetIvar

func (id ID) GetIvar(ivar Ivar) ID

GetIvar reads the value of an instance variable in an object.

func (ID) Send

func (id ID) Send(sel SEL, args ...interface{}) ID

Send is a convenience method for sending messages to objects. This function takes a SEL instead of a string since RegisterName grabs the global Objective-C lock. It is best to cache the result of RegisterName.

func (ID) SendSuper

func (id ID) SendSuper(sel SEL, args ...interface{}) ID

SendSuper is a convenience method for sending message to object's super. This function takes a SEL instead of a string since RegisterName grabs the global Objective-C lock. It is best to cache the result of RegisterName.

Example
package main

import (
	"fmt"

	"github.com/FaceChainTeam/purego/objc"
)

func main() {
	super := objc.AllocateClassPair(objc.GetClass("NSObject"), "SuperObject", 0)
	super.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) {
		fmt.Println("In Super!")
	}), "v@:")
	super.Register()

	child := objc.AllocateClassPair(super, "ChildObject", 0)
	child.AddMethod(objc.RegisterName("doSomething"), objc.NewIMP(func(self objc.ID, _cmd objc.SEL) {
		fmt.Println("In Child")
		self.SendSuper(_cmd)
	}), "v@:")
	child.Register()

	objc.ID(child).Send(objc.RegisterName("new")).Send(objc.RegisterName("doSomething"))

}
Output:

In Child
In Super!

func (ID) SetIvar

func (id ID) SetIvar(ivar Ivar, value ID)

SetIvar sets the value of an instance variable in an object.

type IMP

type IMP uintptr

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

Example
package main

import (
	"fmt"

	"github.com/FaceChainTeam/purego"
	"github.com/FaceChainTeam/purego/objc"
)

func main() {
	imp := objc.NewIMP(func(self objc.ID, _cmd objc.SEL, a3, a4, a5, a6, a7, a8, a9 int) {
		fmt.Println("IMP:", self, _cmd, a3, a4, a5, a6, a7, a8, a9)
	})

	purego.SyscallN(uintptr(imp), 105, 567, 9, 2, 3, ^uintptr(4), 4, 8, 9)

}
Output:

IMP: 105 567 9 2 3 -5 4 8 9

func NewIMP

func NewIMP(fn interface{}) IMP

NewIMP takes a Go function that takes (ID, SEL) as its first two arguments. It returns an IMP function pointer that can be called by Objective-C code. The function panics if an error occurs. The function pointer is never deallocated.

type Ivar

type Ivar uintptr

Ivar an opaque type that represents an instance variable.

func (Ivar) Name

func (i Ivar) Name() string

func (Ivar) Offset

func (i Ivar) Offset() uintptr

Offset returns the offset of an instance variable that can be used to assign and read the Ivar's value.

For instance variables of type ID or other object types, call Ivar and SetIvar instead of using this offset to access the instance variable data directly.

type IvarAttrib

type IvarAttrib int

IvarAttrib is the attribute that an ivar has. It affects if and which methods are automatically generated when creating a class with RegisterClass. See Apple Docs for an understanding of these attributes. The fields are still accessible using objc.GetIvar and objc.SetIvar regardless of the value of IvarAttrib.

Take for example this Objective-C code:

@property (readwrite) float value;

In Go, the functions can be accessed as followed:

var value = purego.Send[float32](id, purego.RegisterName("value"))
id.Send(purego.RegisterName("setValue:"), 3.46)
const (
	ReadOnly IvarAttrib = 1 << iota
	ReadWrite
)

type MethodDef

type MethodDef struct {
	Cmd SEL
	Fn  any
}

MethodDef represents the Go function and the selector that ObjC uses to access that function.

type Protocol

type Protocol [0]func()

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

func GetProtocol

func GetProtocol(name string) *Protocol

GetProtocol returns the protocol for the given name or nil if there is no protocol by that name.

func (*Protocol) Equals

func (p *Protocol) Equals(p2 *Protocol) bool

Equals return true if the two protocols are the same.

type SEL

type SEL uintptr

SEL is an opaque type that represents a method selector

func RegisterName

func RegisterName(name string) SEL

RegisterName registers a method with the Objective-C runtime system, maps the method name to a selector, and returns the selector value. This function grabs the global Objective-c lock. It is best the cache the result of this function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL