js-ctypes reference

  • Revision slug: Mozilla/js-ctypes/js-ctypes_reference
  • Revision title: js-ctypes reference
  • Revision id: 4063
  • Created:
  • Creator: Yoric
  • Is current revision? No
  • Comment 80 words added, 374 words removed

Revision Content

{{ gecko_minversion_header("2.0") }}

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

Loading a library with ctypes.open() returns a Library object. This 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.

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:

Type constructors

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

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.

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.

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.

Revision Source

<p>{{ gecko_minversion_header("2.0") }}</p>
<p>This doc is a work in progress.</p>
<h2>ctypes Reference</h2>
<p>The <code>ctypes</code> object is the base of the ctypes API. It is obtained by by loading the ctypes module:</p>
<p><code>Components.utils.import("<a class=" external" href="resource://gre/modules/ctypes.jsm" rel="freelink">resource://gre/modules/ctypes.jsm</a>");</code></p>
<p>You can use the <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes" title="en/js-ctypes/js-ctypes reference/ctypes"><code>ctypes</code></a> object to load libraries, construct types, and perform miscellaneous tasks such as type casting. The <code><code>ctypes</code></code> object also provides numerous <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes#Predefined_data_types" title="en/js-ctypes/js-ctypes reference/ctypes#Predefined data types">predefined types</a> that correspond to primitives and common typedefs in C.</p>
<h2>Working with libraries</h2>
<p>Loading a library with <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes#open()" title="en/js-ctypes/js-ctypes reference/ctypes#open()">ctypes.open()</a></code> returns a <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/Library" title="en/js-ctypes/js-ctypes reference/Library">Library</a></code> object. This is used mostly to declare native functions provided by the library so that js-ctypes knows how to call them.See <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/Library#declare()" title="Library.declare()">Library.declare()</a></code> for instructions on how to declare these functions.</p>
<h2>Types and data</h2>
<p>To use js-ctypes effectively, it is important to understand the different kinds of objects that the module provides. There are three major categories:</p>
<h3>Type constructors</h3>
<p>These are functions that define new types, and thus return a <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/CType" title="en/js-ctypes/js-ctypes reference/CType">CType</a></code> object. These include</p>
<ul> <li><code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes#PointerType()" title="en/js-ctypes/js-ctypes reference/ctypes#PointerType()">ctypes.PointerType(type) </a></code>and the <code>.ptr</code> property, which creates a new type describing a pointer to an existing type.</li> <li><code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes#ArrayType()" title="en/js-ctypes/js-ctypes reference/ctypes#ArrayType()">ctypes.ArrayType(type, [length])</a></code> and the <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/CType#array()" title="en/js-ctypes/js-ctypes reference/CType#array()"><code>.array()</code></a> method, which creates a new type describing an array of objects of an existing type.</li> <li><code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes#StructType()" title="en/js-ctypes/js-ctypes reference/ctypes#StructType()">ctypes.StructType()</a>, </code>which creates a new type describing a C struct.</li> <li><code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/ctypes#FunctionType()" title="en/js-ctypes/js-ctypes reference/ctypes#FunctionType()">ctypes.FunctionType(abi, returnType, argType1 [, argType2 ...])</a></code><code>, </code>which creates a new type describing a C function.</li>
</ul>
<h3>Types</h3>
<p>These represent a type in C, and are thus represented by <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/CType" title="en/js-ctypes/js-ctypes reference/CType">CType</a></code> 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 <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/Library#declare()" title="Library.declare()">Library.declare()</a></code>. Second, they serve as constructors to create concrete instances of the given type, i.e. <code><a href="/en/Mozilla/js-ctypes/js-ctypes_reference/CData" title="en/js-ctypes/js-ctypes reference/CData">CData</a></code> objects.</p>
<h3>Data</h3>
<p>These are instances of types, represented by <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/CData" title="en/js-ctypes/js-ctypes reference/CData"><code>CData</code></a> objects and instantiated by calling <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/CType" title="en/js-ctypes/js-ctypes reference/CType"><code>CType</code></a> objects as functions.<span id="1314129036225E" style="display: none;"> </span></p>
<p>These distinctions are crucial, and understanding them will alleviate much of the confusion surrounding js-ctypes. Calling a Type Constructor will return a <code>CType</code>. Calling a <code>CType</code> will return a <code>CData</code>. You <em>declare</em> the arguments and return value of a native function with <code>CType</code> objects. You <em>call</em> a native function with <code>CData</code> objects.</p>
<h2>Callbacks</h2>
<p>You can pass regular JavaScript functions as callbacks to native functions. See the <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/Callbacks" title="en/js-ctypes/js-ctypes reference/Callbacks">Callbacks</a> page for details.</p>
<h2>64-Bit integer handling</h2>
<p>Because JavaScript <a href="/en/JavaScript/Reference/Global_Objects/Number" title="en/JavaScript/Reference/Global Objects/Number"><code>Number</code></a> objects can't directly represent every possible 64-bit integer value, js-ctypes provides new types for these. See <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/Int64" title="en/js-ctypes/js-ctypes reference/Int64">Int64</a> and <a href="/en/Mozilla/js-ctypes/js-ctypes_reference/UInt64" title="en/js-ctypes/js-ctypes reference/UInt64">UInt64</a>.</p>
Revert to this revision