ffi

package module
v0.0.0-...-d38ff63 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2016 License: MIT Imports: 6 Imported by: 0

README

go-ffi Build Status Coverage Status

Go bindings to libffi

Calling C Functions

go-ffi is interesting when a program needs to call C functions in a way that is not yet supported by cgo. For example if the function pointer was obtained dynamically or if the function as a variadic signature.

Here's an example showing how to call printf from Go using the go-ffi package:

package main

// #include <stdio.h>
//
// const void *printf__ = (void *) printf;
import "C"
import "github.com/achille-roussel/go-ffi"

func main() {
     ffi.Call(C.printf__, nil, "%s %s!\n", "Hello", "World")
}
Hello World!

Calling Go Functions

go-ffi also provides a mechanism for generating pointers that can be used to call Go functions from compiled C code.
Here's an example showing how this is done:

package main

import (
       "fmt"
       "strconv"
       "unsafe"

       "github.com/achille-roussel/go-ffi"
)

func main() {
     itoa := ffi.Closure(strconv.Itoa)
     fptr := itoa.Pointer()
     repr := ""

     ffi.Call(unsafe.Pointer(fptr), &repr, 42)

     fmt.Println(repr)
}
42

Type Conversions

go-ffi automatically converts between C and Go types when using the high-level interfaces.
The following table exposes what conversions are supported:

Go C
int int
int8 int8_t
int16 int16_t
int32 int32_t
int64 int64_t
uint unsigned int
uint8 uint8_t
uint16 uint16_t
uint32 uint32_t
uintptr size_t
float32 float
float64 double
string char *
unsafe.Pointer void *

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(fptr unsafe.Pointer, ret interface{}, args ...interface{}) (err error)

func GoClosureCallback

func GoClosureCallback(cif *C.ffi_cif, ret unsafe.Pointer, args *unsafe.Pointer, data unsafe.Pointer)

Types

type Function

type Function interface {
	Call(unsafe.Pointer, ...unsafe.Pointer) error

	Pointer() uintptr
}

func Closure

func Closure(v interface{}) Function

type Interface

type Interface struct {
	// contains filtered or unexported fields
}

func Prepare

func Prepare(ret Type, args ...Type) (cif Interface)

func (Interface) Call

func (cif Interface) Call(fptr unsafe.Pointer, ret unsafe.Pointer, args ...unsafe.Pointer) (err error)

func (Interface) Format

func (self Interface) Format(f fmt.State, r rune)

func (Interface) String

func (self Interface) String() string

type Status

type Status int
const (
	OK         Status = Status(C.FFI_OK)
	BadTypedef Status = Status(C.FFI_BAD_TYPEDEF)
	BadABI     Status = Status(C.FFI_BAD_ABI)
)

func (Status) Error

func (s Status) Error() string

func (Status) String

func (s Status) String() string

type Type

type Type struct {
	// contains filtered or unexported fields
}
var (
	Void Type = Type{&C.ffi_type_void, "void"}

	UChar  Type = Type{&C.ffi_type_uchar, "unsigned char"}
	UShort Type = Type{&C.ffi_type_ushort, "unsigned short"}
	UInt   Type = Type{&C.ffi_type_uint, "unsigned int"}
	ULong  Type = Type{&C.ffi_type_ulong, "unsigned long"}

	UInt8  Type = Type{&C.ffi_type_uint8, "uint8_t"}
	UInt16 Type = Type{&C.ffi_type_uint16, "uint16_t"}
	UInt32 Type = Type{&C.ffi_type_uint32, "uint32_t"}
	UInt64 Type = Type{&C.ffi_type_uint64, "uint64_t"}

	Char  Type = Type{&C.ffi_type_schar, "char"}
	Short Type = Type{&C.ffi_type_sshort, "short"}
	Int   Type = Type{&C.ffi_type_sint, "int"}
	Long  Type = Type{&C.ffi_type_slong, "long"}

	Int8  Type = Type{&C.ffi_type_sint8, "int8_t"}
	Int16 Type = Type{&C.ffi_type_sint16, "int16_t"}
	Int32 Type = Type{&C.ffi_type_sint32, "int32_t"}
	Int64 Type = Type{&C.ffi_type_sint64, "int64_t"}

	Float  Type = Type{&C.ffi_type_float, "float"}
	Double Type = Type{&C.ffi_type_double, "double"}

	Pointer Type = Type{&C.ffi_type_pointer, "void *"}
)

func (Type) String

func (t Type) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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