mozilla

Revision 73088 of Declaring types

  • Revision slug: Mozilla/js-ctypes/Using_js-ctypes/Declaring_types
  • Revision title: Declaring types
  • Revision id: 73088
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment wip; 37 words added

Revision Content

{{ gecko_minversion_header("2.0") }}{{ draft() }}

The ctypes object offers a number of constructor methods that let you declare types. Every type is represented by a CType object, which, in turn, provides a constructor method you can call to define values of those types.

Types are declared in terms of other, already defined types. At the core of all this is a set of predefined types provided by js-ctypes. These are all primitive types, upon which all other types are eventually based.

Primitive types

Primitive types are those types that represent a single value in memory, as opposed to arrays, structures, or functions. You can define values of these types without declaring new types. For example, to define a new 32-bit integer variable with the value 5:

var i = ctypes.int32_t(5);

You can then pass a pointer to this value to a C function that requires a pointer to a 32-bit integer, like this:

some_c_function(i.address());

Declaring new primitive types

There are times when you want to create new types that are simply new names for existing primitive types. For example, on Windows, you may wish to be able to use the Windows-standard DWORD type name for unsigned 32-bit integers. To declare this type, you can simply do:

const DWORD = ctypes.uint32_t;

After doing this, DWORD is a CType that can then be used to represent 32-bit unsigned integers.

Composite data types

Composite data types, such as structures and arrays, are composed of multiple primitive data types.

Pointers

Revision Source

<p>{{ gecko_minversion_header("2.0") }}{{ draft() }}</p>
<p>The <code><a href="/en/js-ctypes/js-ctypes_reference/ctypes" title="en/js-ctypes/js-ctypes reference/ctypes">ctypes</a></code> object offers a number of constructor methods that let you declare types. Every type is represented by a <a href="/en/js-ctypes/js-ctypes_reference/CType" title="en/js-ctypes/js-ctypes reference/CType"><code>CType</code></a> object, which, in turn, provides a constructor method you can call to define values of those types.</p>
<p>Types are declared in terms of other, already defined types. At the core of all this is a set of <a href="/en/js-ctypes/js-ctypes_reference/ctypes#Predefined_data_types" title="en/js-ctypes/js-ctypes reference/ctypes#Predefined data types">predefined types</a> provided by js-ctypes. These are all primitive types, upon which all other types are eventually based.</p>
<h2>Primitive types</h2>
<p><strong>Primitive types</strong> are those types that represent a single value in memory, as opposed to arrays, structures, or functions. You can define values of these types without declaring new types. For example, to define a new 32-bit integer variable with the value 5:</p>
<pre>var i = ctypes.int32_t(5);
</pre>
<p>You can then pass a pointer to this value to a C function that requires a pointer to a 32-bit integer, like this:</p>
<pre>some_c_function(i.address());
</pre>
<h3>Declaring new primitive types</h3>
<p>There are times when you want to create new types that are simply new names for existing primitive types. For example, on Windows, you may wish to be able to use the Windows-standard <code>DWORD</code> type name for unsigned 32-bit integers. To declare this type, you can simply do:</p>
<pre>const DWORD = ctypes.uint32_t;
</pre>
<p>After doing this, <code>DWORD</code> is a <a href="/en/js-ctypes/js-ctypes_reference/CType" title="en/js-ctypes/js-ctypes reference/CType"><code>CType</code></a> that can then be used to represent 32-bit unsigned integers.</p>
<h2>Composite data types</h2>
<p>Composite data types, such as structures and arrays, are composed of multiple primitive data types.</p>
<h2>Pointers</h2>
Revert to this revision