Fast Testing
The scratchpad is an excellent tool for running js-ctypes without having to zip up any add-on source files and install them. Code can be run from scratchpad and tested very fast. Here is a screencast demonstrating how to set it up:
https://www.youtube.com/watch?v=oo4STWceGTM
Printing CData and CType
Currently console.log
doesn't show type information of CData.
Running the following code shows only partial value information.
let { ctypes } = Components.utils.import("resource://gre/modules/ctypes.jsm", {}); let i = ctypes.int32_t(10); console.log(i); let Point = ctypes.StructType("Point", [{ x: ctypes.int32_t }, { y: ctypes.int32_t }]) let p = Point(10, 20); console.log(p); let pp = p.address(); console.log(pp);
The result will be as following:
CData { value: 10 } CData { x: 10, y: 20 } CData { contents: CData }
To see more descriptive information, you can use .toSource()
.
let { ctypes } = Components.utils.import("resource://gre/modules/ctypes.jsm", {}); let i = ctypes.int32_t(10); console.log(i.toSource()); let Point = ctypes.StructType("Point", [{ x: ctypes.int32_t }, { y: ctypes.int32_t }]) let p = Point(10, 20); console.log(p.toSource()); let pp = p.address(); console.log(pp.toSource());
The result will be :
ctypes.int32_t(10) Point(10, 20) Point.ptr(ctypes.UInt64("0x15fdafb08"))
To see the complete type information, you can use .constructor.toSource()
, to print the source of CType
.
let { ctypes } = Components.utils.import("resource://gre/modules/ctypes.jsm", {}); let i = ctypes.int32_t(10); console.log(i.constructor.toSource()); let Point = ctypes.StructType("Point", [{ x: ctypes.int32_t }, { y: ctypes.int32_t }]) let p = Point(10, 20); console.log(p.constructor.toSource()); let pp = p.address(); console.log(pp.constructor.toSource());
The result will be as per the following:
ctypes.int32_t ctypes.StructType("Point", [{ "x": ctypes.int32_t }, { "y": ctypes.int32_t }]) ctypes.StructType("Point", [{ "x": ctypes.int32_t }, { "y": ctypes.int32_t }]).ptr