Data type declarations

Constants

ABI constants

These constants are used to specify the ABI used to call a native function in the library.

Constant

Description

default_abi

Corresponds to cdecl; standard libraries use this ABI.

stdcall_abi

Used for calling Windows API functions.

Predefined data types

Primitive types

These types behave the same on all platforms.

Constant

Description

int8_t

Signed 8-bit integer.

uint8_t

Unsigned 8-bit integer.

int16_t

Signed 16-bit integer.

uint16_t

Unsigned 16-bit integer.

int32_t

Signed 32-bit integer.

uint32_t

Unsigned 32-bit integer.

int64_t

Signed 64-bit integer.

uint64_t

Unsigned 64-bit integer.

float32_t

32-bit floating-point value.

float64_t

64-bit floating-point value.

Note: Some 64-bit values are outside the range of numeric values supported by JavaScript. Because of this, ctypes.int64 and ctypes.uint64 do not automatically convert to JavaScript numbers. Instead, they convert to objects of the wrapper types ctypes.Int64 and ctypes.UInt64, which are JavaScript objects rather than CData objects. See 64-bit integer objects for details.

Types that act like specific C types

These types are designed to work exactly like the corresponding type on the native platform.

Constant

Description

bool

A Boolean type that behaves like the corresponding C type on the platform.

short

A short integer type that behaves like the corresponding C type on the platform.

unsigned_short

An unsigned short integer type that behaves like the corresponding C type on the platform.

int

An integer type that behaves like the int C type on the platform.

unsigned_int

An unsigned integer type that behaves like the unsigned int C type on the platform.

long

A long integer type that behaves like the corresponding C type on the platform.

Note: This automatically converts to an Int64 JavaScript object on all platforms, since it's unknown whether this is a 32-bit or 64-bit value. This is done for compatibility's sake.

unsigned long

An unsigned long integer type that behaves like the corresponding C type on the platform.

Note: This automatically converts to a UInt64 JavaScript object on all platforms, since it's unknown whether this is a 32-bit or 64-bit value. This is done for compatibility's sake.

float

A floating point value that behaves like the float type on the platform.

double

A double-precision floating point value that behaves like the double type on the platform.

Character types

Character types are 8-bit values that behave like their C counterparts. They're similar to the int8_t and uint8_t types, except conversion is handled differently.

For example, ctypes.char.array(30)(str) converts the string str to UTF-8 and returns a new CData object of array type.

Constant

Description

char

A character type that behaves like the char C type on the platform.

signed_char

A signed character that behaves like the char type on the platform.

unsigned_char

An unsigned character that behaves like the unsigned char type on the platform.

Types whose size varies depending on platform

Because it's unknown whether these values will be 32-bit or 64-bit, they are not automatically converted into JavaScript numbers. Instead, these convert into Int64 or UInt64 JavaScript objects; see 64-bit integers for details.

Constant

Description

size_t

A platform-dependent size type.

ssize_t

A platform-dependent size type.

intptr_t

A platform-dependent integer representation of a pointer.

uintptr_t

A platform-dependent unsigned integer representation of a pointer.

JavaScript characters

16-bit C Unicode characters are handled by js-ctypes using the jschar type.

Constant

Description

jschar

A 16-bit unsigned character. This is different from uint16_t in that C jschar values are automatically converted into 1-character JavaScript strings. These are Unicode characters.

Special C types

These types represent C types that have special meanings.

Constant

Description

void_t

The special C type void. This can only be used as a return value type.

Note: You must use void_t, not void, since void is a keyword in JavaScript.

voidptr_t

The C type void *. This is a pointer to an indeterminate type of data.

Parameters

Return value

PointerType()

Creates a new pointer type, returning a CType object describing the new type.

CType PointerType(
type
);

Parameters

type

If this is a type (either a user-defined type or one of the Predefined data types), the returned type is "pointer to type". If this is a string, the returned type is an opaque pointer type named "type".

Return value

A pointer data type which can be used when declaring functions or other types, including structures.

StructType()

Creates a new structure data type, returning a CType object describing the new type.

Note: Currently, there is no way to represent a packed structure; that is, one that in C is defined using #pragma pack.

CType StructType(
name[,
fields
]);

Parameters

name

The name of the structure.

fields

An array of field descriptors. Each field is described using a name and a type, which can be a user-defined type or one of the Predefined data types. If this parameter isn't included, the structure is opaque.

Return value

A data type representing the corresponding C struct.

Exceptions thrown

RangeError

The size of the structure, in bytes, can't be exactly represented both as a size_t and as a JavaScript number.

TypeError

name isn't a string, or one of the fields is of a type with undefined size.