js-ctypes reference 编辑

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

To load a Library, use ctypes.open().

The Library object 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.

Once you have finished working with a Library, call Library.close().

Calling conventions

See ABI.

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:

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.

Out of the box, js-ctypes supports a number of predefined types.

Type constructors

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

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.

Other Features

Error-handling

js-ctypes supports both errno (on all platforms) and GetLastError (on Windows platforms).

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.

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

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

发布评论

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

词条统计

浏览:61 次

字数:11736

最后编辑:6 年前

编辑次数:0 次

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