Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Library

This article needs a technical review. How you can help.

Draft
This page is not complete.

The Library object represents a native library loaded by the ctypes open() method. Its methods let you declare symbols exported by the library, and to manage the library.

Method overview

close();
CData declare(name, [abi, ], returnType[, argType1, ...]);

Methods

close()

Closes the library. You need to call this once you're done using the library.

close();
Parameters

None.

declare()

Declares an API from the native library, allowing it to be used from JavaScript. This can be used both for exported data symbols and for functions.

CData declare(
  name[,
  abi,
  returnType
  argType1,
  ...]
);
Parameters
name
The name of the symbol exported by the native library that is to be declared as usable from JavaScript
abi
The ABI used by the exported function; this will be ctypes.default_abi for most libraries, except for Windows libraries, which will be ctypes.winapi_abi or ctypes.stdcall_abi. See ABI constants. You don't need to provide this for exported data; it's only needed for function declarations.
returnType
The CType type returned by the defined API, if it's a function. This parameter should not be provided if the API is an exported data symbol.
argType1...argTypeN
Zero or more parameter CType may be specified for the parameters of the function being declared. These should not be provided if the API is an exported data symbol rather than a function.
Return value

FunctionType CData object representing the declared API.

Exceptions thrown
TypeError
The return type was specified as an array.

Alternative Syntax

Another use for ctypes.declare is to get non-function/non-methods from libraries. The syntax for this is seen in Firefox codebase here:

https://dxr.mozilla.org/mozilla-central/source/js/src/ctypes/Library.cpp?offset=0#271

This shows that we can also supply only two arguments to the declare function. The first being the same as in the above example, name, which is the name of the symbol to export. And in place of the abi argument, we will pass in the type of this export. For example, from the Objective-C library we can export the symbol which holds the address to an external block. This would be done like this:

var objc = ctypes.open(ctypes.libraryName('objc'));

objc.declare('_NSConcreteGlobalBlock', ctypes.voidptr_t);

See Also

Document Tags and Contributors

 Contributors to this page: arai, Noitidart, kscarfone, Sheppy
 Last updated by: arai,