CType

  • Revision slug: Mozilla/js-ctypes/js-ctypes_reference/CType
  • Revision title: CType
  • Revision id: 72705
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment checkpoint; starting to break out the subclasses; 56 words added, 58 words removed

Revision Content

{{ gecko_minversion_header("2.0") }}

{{ draft() }}

Data types declared using the js-ctypes API are represented by CType objects. These objects have assorted methods and properties that let you create objects of these types, find out information about them, and so forth.

Available types

Primitive types
These are typical unstructured data, such as the predefined types listed in Predefined data types.
Big integer types
The Int64 and UInt64 types provide access to 64-bit integer values, which JavaScript doesn't currently support.
PointerType
This subclass of CType represents C pointers to native data.
FunctionType
This subclass of CType represents callable C functions.
StructType
This subclass of CType represents C structures.
ArrayType
This subclass of CType represents C arrays.

Method overview

CType array([n])
String toSource()
String toString()

Properties

These properties are available on all CType objects.

Property Type Description
name String

The type's name.

For primitive types, this is just the name of the corresponding C type. For structure and opaque pointer types, this is simply the string that was passed to the constructor. For other function, pointer, and array types, this should be a valid C type expression.

ptr CType Returns a CType representing the data type "pointer to this type". This is the result of calling ctypes.PointerType(the_type).
size Number

The size of the type, in bytes. This is the same value as the C sizeof.

Note: ctypes.void_t.size is undefined.

Properties of pointer types

These properties are only available on pointer types.

Property Type Description
targetType CType The type of object the pointer points to, or null if it's an opaque pointer type. Read only.

Properties of function types

These properties are only available on function types.

Property Type Description
abi One of the ABI constants The ABI of the function. Read only.
argTypes
CType[] A sealed array of the argument types. Read only.
returnType
CType The return type of the function. Read only.

Properties of struct types

These properties are only available on struct types.

Property Type Description
fields CType[] A sealed array of field descriptors. Read only.

Properties of array types

These properties are only available on array types.

Property Type Description
elementType CType The data type of the elements in an array of this type. Read only.
length Number The number of elements in the array, or undefined if the array type doesn't have a specified length. Read only.

Methods available on all CType objects

array()

Returns a new CType representing an array of elements of the type on which it was called.

CType array(
  [n]
}; 
Parameters
n {{ optional_inline() }}
The number of elements in the declared array type. If this parameter is left out, the array's size is indeterminate.
Return value

A CType representing the data type "array of this type". This is the same as calling ctypes.ArrayType(the_type[, n]).

toSource()

Returns a JavaScript expression that evaluates to a CType describing the same C type as this object.

String toSource();
Parameters

None.

Return value

A JavaScript expression that evaluates to a CType describing the same C type as this object.

Example

ctypes.uint32_t.toSource() returns "ctypes.uint32_t".

toString()

Returns a string identifying the type. The format of this string is "type " + name.

String toString();
Parameters

None.

Return value

A string identifying the data type.

Revision Source

<p>{{ gecko_minversion_header("2.0") }}</p>
<p>{{ draft() }}</p>
<p>Data types declared using the js-ctypes API are represented by <code>CType</code> objects. These objects have assorted methods and properties that let you create objects of these types, find out information about them, and so forth.</p>
<h2>Available types</h2>
<dl> <dt>Primitive types</dt> <dd>These are typical unstructured data, such as the predefined types listed in <a href="/en/js-ctypes/js-ctypes_reference/ctypes#Predefined_data_types" title="en/js-ctypes/js-ctypes reference/ctypes#Predefined data types">Predefined data types</a>.</dd> <dt>Big integer types</dt> <dd>The <code>Int64</code> and <code>UInt64</code> types provide access to 64-bit integer values, which JavaScript doesn't currently support.</dd> <dt><a href="/en/js-ctypes/js-ctypes_reference/CType/PointerType" title="en/js-ctypes/js-ctypes reference/CType/PointerType"><code>PointerType</code></a></dt> <dd>This subclass of <code>CType</code> represents C pointers to native data.</dd> <dt><a href="/en/js-ctypes/js-ctypes_reference/CType/FunctionType" title="en/js-ctypes/js-ctypes reference/CType/FunctionType"><code>FunctionType</code></a></dt> <dd>This subclass of <code>CType</code> represents callable C functions.</dd> <dt><a href="/en/js-ctypes/js-ctypes_reference/CType/StructType" title="en/js-ctypes/js-ctypes reference/CType/StructType"><code>StructType</code></a></dt> <dd>This subclass of <code>CType</code> represents C structures.</dd> <dt><a href="/en/js-ctypes/js-ctypes_reference/CType/ArrayType" title="en/js-ctypes/js-ctypes reference/CType/ArrayType"><code>ArrayType</code></a></dt> <dd>This subclass of <code>CType</code> represents C arrays.</dd>
</dl>
<h2>Method overview</h2>
<table class="standard-table"> <tbody> <tr> <td><span style="font-family: monospace;">CType <a href="/en/js-ctypes/js-ctypes_reference#array()" title="en/js-ctypes/js-ctypes reference#array()">array</a>([n])</span><code><br> </code></td> </tr> <tr> <td><code>String <a href="/en/js-ctypes/js-ctypes_reference#toSource()" title="en/js-ctypes/js-ctypes reference#toSource()">toSource</a>()</code></td> </tr> <tr> <td><code>String <a href="/en/js-ctypes/js-ctypes_reference#toString()" title="en/js-ctypes/js-ctypes reference#toString()">toString</a>()</code></td> </tr> </tbody>
</table>
<h2>Properties</h2>
<p>These properties are available on all <code>CType</code> objects.</p>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Property</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>name</code></td> <td><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/String" title="en/Core JavaScript 1.5 Reference/Global Objects/String"><code>String</code></a></td> <td> <p>The type's name.</p> <p>For primitive types, this is just the name of the corresponding C type. For structure and opaque pointer types, this is simply the string that was passed to the constructor. For other function, pointer, and array types, this should be a valid C type expression.</p> </td> </tr> <tr> <td><code>ptr</code></td> <td><code>CType</code></td> <td>Returns a <code>CType</code> representing the data type "pointer to this type". This is the result of calling <code>ctypes.PointerType(the_type)</code>.</td> </tr> <tr> <td><code>size</code></td> <td><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Number" title="en/Core JavaScript 1.5 Reference/Global Objects/Number"><code>Number</code></a></td> <td> <p>The size of the type, in bytes. This is the same value as the C <code>sizeof</code>.</p> <div class="note"><strong>Note:</strong> <code>ctypes.void_t.size</code> is undefined.</div> </td> </tr> </tbody>
</table>
<h3>Properties of pointer types</h3>
<p>These properties are only available on pointer types.</p>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Property</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>targetType</code></td> <td><code>CType</code></td> <td>The type of object the pointer points to, or <code>null</code> if it's an opaque pointer type. <strong>Read only.</strong></td> </tr> </tbody>
</table>
<h3>Properties of function types</h3>
<p>These properties are only available on function types.</p>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Property</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>abi</code></td> <td>One of the <a href="/en/js-ctypes/js-ctypes_reference/ctypes#ABI_constants" title="en/js-ctypes/js-ctypes reference/ctypes#ABI constants">ABI constants</a></td> <td>The ABI of the function. <strong>Read only.</strong></td> </tr> <tr> <td><code>argTypes<br> </code></td> <td><code>CType[]</code></td> <td>A sealed array of the argument types. <strong>Read only.</strong></td> </tr> <tr> <td><code>returnType<br> </code></td> <td><code>CType</code></td> <td>The return type of the function. <strong>Read only.</strong></td> </tr> </tbody>
</table>
<h3>Properties of struct types</h3>
<p>These properties are only available on struct types.</p>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Property</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>fields</code></td> <td><code>CType[]</code></td> <td>A sealed array of field descriptors. <strong>Read only.</strong></td> </tr> </tbody>
</table>
<h3>Properties of array types</h3>
<p>These properties are only available on array types.</p>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Property</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>elementType</code></td> <td><code>CType</code></td> <td>The data type of the elements in an array of this type. <strong>Read only.</strong></td> </tr> <tr> <td><code>length</code></td> <td><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Number" title="en/Core JavaScript 1.5 Reference/Global Objects/Number"><code>Number</code></a></td> <td>The number of elements in the array, or <code>undefined</code> if the array type doesn't have a specified length. <strong>Read only.</strong></td> </tr> </tbody>
</table>
<h2>Methods available on all CType objects</h2>
<h3>array()</h3>
<p>Returns a new <code>CType</code> representing an array of elements of the type on which it was called.</p>
<pre>CType array(
  [n]
}; 
</pre>
<h6>Parameters</h6>
<dl> <dt>n {{ optional_inline() }}</dt> <dd>The number of elements in the declared array type. If this parameter is left out, the array's size is indeterminate.</dd>
</dl>
<h6>Return value</h6>
<p>A <code>CType</code> representing the data type "array of this type". This is the same as calling <code>ctypes.ArrayType(the_type[, n])</code>.</p>
<h3>toSource()</h3>
<p>Returns a JavaScript expression that evaluates to a <code>CType</code> describing the same C type as this object.</p>
<pre>String toSource();
</pre>
<h6>Parameters</h6>
<p>None.</p>
<h6>Return value</h6>
<p>A JavaScript expression that evaluates to a <code>CType</code> describing the same C type as this object.</p>
<h6>Example</h6>
<p><code>ctypes.uint32_t.toSource()</code> returns "ctypes.uint32_t".</p>
<h3>toString()</h3>
<p>Returns a string identifying the type. The format of this string is "type " + <code>name</code>.</p>
<pre>String toString();
</pre>
<h6>Parameters</h6>
<p>None.</p>
<h6>Return value</h6>
<p>A string identifying the data type.</p>
Revert to this revision