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
object also provides numerous predefined types that correspond to primitives and common typedefs in C.ctypes
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
ctypes.PointerType(type)
and the.ptr
property, which creates a new type describing a pointer to an existing type.ctypes.ArrayType(type, [length])
and the.array()
method, which creates a new type describing an array of objects of an existing type.ctypes.StructType()
, which creates a new type describing a C struct.ctypes.FunctionType(abi, returnType, argType1 [, argType2 ...])
, which creates a new type describing a C function.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论