ctypes

This article is in need of a technical review.

Draft
This page is not complete.

The ctypes object contains methods and predefined data types used to create and manage a library.

Method overview

CType ArrayType(type[, length]);
CData cast(data, type);
CType FunctionType(abi, returnType[, argType1, ...]);
CData Int64(n);
String libraryName(name);
Library open(libSpec);
CType PointerType(typeSpec);
CType StructType(name[, fields]);
CData UInt64(n);

Properties

Property Type Description
errno Number The value of the latest system error. Similar to errno in libc, available on all platforms. Cannot be set.
winLastError Number|undefined The value of the latest Windows error. Similar to GetLastError under Windows, available only for Windows. Cannot be set.

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. You also use this for all system calls on Mac OS X and Linux.
stdcall_abi Used for calling functions declared with stdcall on Windows. These functions' names are automatically mangled for you by js-ctypes.
winapi_abi Used for calling Windows system functions. These are declared as stdcall on Windows, but do not have mangled names like those used by stdcall_abi above.

Predefined data types

Primitive types

These types behave the same on all platforms.

Type 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 integers for details.

Types that act like specific C types

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

Type 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.
long_long A integer type at least 64 bits wide.
unsigned_long_long An unsigned integer type at least 64 bits wide.
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.

Type 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 ctypes.Int64 or ctypes.UInt64 JavaScript objects; see 64-bit integers for details.

Type 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.

Type 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.

Type 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.

Large integer types

Because JavaScript doesn't support large (64-bit) integers, js-ctypes provides two data types allowing access to 64-bit integer data.

Type Description
Int64 A JavaScript object representing a 64-bit signed integer.
UInt64 A JavaScript object representing a 64-bit unsigned integer.

Methods

ArrayType()

Returns a new CType representing an array data type.

CType ArrayType(
  type[,
  length]
);
Parameters
type
The type of the elements in the array type to create.
length
The number of entries the array type should be able to contain. If you don't specify this parameter, the array's length is unspecified.
Return value

A CType representing the newly declared array type.

Exceptions thrown
TypeError
type is not a CType, or type.size is undefined. If length is specified but is not a valid array length, this can also be thrown.
RangeError
The size of the resulting array can't be represented as both a size_t and as a JavaScript number.

cast()

Casts the specified CData object to a new type, returning a new CData object representing the value in the new type. See Type casting for details.

CData cast(
  data,
  type
);
Parameters
data
The CData object to type cast.
type
The type to which to cast the value. This can be one of the Predefined data types, or any user-defined types.
Return value

A new CData object representing the same data, but with the specified type.

FunctionType()

Returns a new CType object describing a C function.

CType FunctionType(
  abi,
  returnType[,
  argType1, ...]
);
Parameters
abi
The ABI type for the function; this is one of the ABI constants.
returnType
CType indicating the type of the value returned by the function.
argType1... argTypeN
Zero or more CType objects indicating the types of each of the parameters passed into the C function.
Return value

A CType describing the function type. The equivalent C function type declaration would be:

returnType (*) ([argType1, ..., argTypeN]);
Exceptions thrown
TypeError
ABI is not a valid ABI type, or returnType or any of the argument types are not valid CType objects.

libraryName()

Returns the correct platform-specific filename for a given library name (e.g. libnss3.dylib for nss3 on OS X.)

String libraryName(
  name
);
Parameters
name
The name of the library.
Return value

String containing the platform-specific filename of the library.

open()

Opens a library, specified by a pathname. The library is loaded from the specified full path, or, if a partial path is specified, from the appropriate library directory based on the platform on which the application is running. See Library search paths for more details.

Library open(
  libSpec
);
Parameters
libSpec
The native library to open, specified as a pathname string.
Return value

A Library object representing the opened library.

PointerType()

Returns a CType object describing a new pointer data type.

CType PointerType(
  typeSpec
);
Parameters
typeSpec
Specifies the type to which the pointer type points. This can be any CType object to declare the new type as a pointer to that type.
Return value

A CType object describing the newly created data type.

Exceptions thrown
TypeError
Thrown if the parameter isn't either a string or a CType.
Examples

Creating a type "pointer to 32-bit integer" looks like this:

var intPtrType = new ctypes.PointerType(ctypes.int32_t);

StructType()

Returns a CType object describing a new structure data type. This data type provides the ability to define and manipulate values of the C struct type.

Note: At this time, there isn't a way to denote a packed structure (that is, one created in C using #pragma pack).
CType StructType(
  name[,
  fields]
);
Parameters
name
The name of the structure. Must be a string.
fields Optional
An array of field descriptors, describing all the entries in the structure. If this isn't specified, the structure is considered opaque and you can't access its contents directly. You can call the resulting type's define() method to assign it a non-opaque type later. See Opaque structures for further explanation and an example.
Return value

A CType object describing the newly created struct data type.

Exceptions thrown
TypeError
The name is not a string, or one or more of the fields does not have a defined size.
RangeError
The size of the structure, in bytes, cannot be represented both as a size_t and as a JavaScript Number.
Describing fields

The fields array is comprised of field descriptors, one for each field in the structure. Each field descriptor contains the field's name and its type, such as {'serialNumber': ctypes.int}. A complete field descriptor list might look like this:

[ {'serialNumber': ctypes.int}, {'userName': ctypes.char.ptr} ]

See also

Document Tags and Contributors

Contributors to this page: Sheppy, Shaver, vasi, MattBrubeck, arai, kscarfone, Yoric, philikon
Last updated by: arai,