Library 编辑

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:82 次

字数:6297

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文