js-ctypes reference

This doc is a work in progress.

ctypes Reference

The ctypes object is the base of the ctypes API. It is obtained by by loading the ctypes module:

Components.utils.import("resource://gre/modules/ctypes.jsm");

You can use the ctypes object to load libraries, construct types, and perform miscellaneous tasks such as type casting. The ctypes object also provides numerous predefined types that correspond to primitives and common typedefs in C.

Working with libraries

To load a Library, use ctypes.open().

The Library object is used mostly to declare native functions provided by the library so that js-ctypes knows how to call them. See Library.declare() for instructions on how to declare these functions.

Once you have finished working with a Library, call Library.close().

Calling conventions

See ABI.

Types and data

To use js-ctypes effectively, it is important to understand the different kinds of objects that the module provides. There are three major categories:

Types

These represent a type in C, and are thus represented by CType objects. These objects have two main purposes. First, they provide a concrete representation of different data types, allowing the programmer to describe the arguments and return type of a native function (see Library.declare()).  Second, they serve as constructors to create concrete instances of the given type, i.e. CData objects.

Out of the box, js-ctypes supports a number of predefined types.

Type constructors

These are functions that define new types, and thus return a CType object. These include

Data

These are instances of types, represented by CData objects and instantiated by calling CType objects as functions.

These distinctions are crucial, and understanding them will alleviate much of the confusion surrounding js-ctypes. Calling a Type Constructor will return a CType. Calling a CType will return a CData. You declare the arguments and return value of a native function with CType objects. You call a native function with CData objects.

Other Features

Error-handling

js-ctypes supports both errno (on all platforms) and GetLastError (on Windows platforms).

Callbacks

You can pass regular JavaScript functions as callbacks to native functions. See the Callbacks page for details.

64-Bit integer handling

Because JavaScript Number objects can't directly represent every possible 64-bit integer value, js-ctypes provides new types for these. See Int64 and UInt64.

Document Tags and Contributors

 Contributors to this page: arai, Noitidart, fscholz, Yoric, bsmedberg, bholley, Sheppy
 Last updated by: arai,