|
| 1 | +// Type definitions for node-ffi, ref, ref-array, ref-struct and ref-union |
| 2 | +// Project: https://github.com/rbranson/node-ffi |
| 3 | +// Definitions by: Paul Loyd <https://github.com/loyd> |
| 4 | +// Definitions: https://github.com/borisyankov/DefinitelyTyped |
| 5 | + |
| 6 | +/// <reference path="../node/node.d.ts" /> |
| 7 | + |
| 8 | +declare module "ffi" {} |
| 9 | + |
| 10 | +declare module "ref" { |
| 11 | + export interface Type { |
| 12 | + /** The size in bytes required to hold this datatype. */ |
| 13 | + size: number; |
| 14 | + /** The current level of indirection of the buffer. */ |
| 15 | + indirection: number; |
| 16 | + /** To invoke when ref.get() is invoked on a buffer of this type. */ |
| 17 | + get(buffer: NodeBuffer, offset: number): any; |
| 18 | + /** To invoke when ref.set() is invoked on a buffer of this type. */ |
| 19 | + set(buffer: NodeBuffer, offset: number, value): void; |
| 20 | + /** The name to use during debugging for this datatype. */ |
| 21 | + name?: string; |
| 22 | + /** The alignment of this datatype when placed inside a struct. */ |
| 23 | + alignment?: number; |
| 24 | + } |
| 25 | + |
| 26 | + /** A Buffer that references the C NULL pointer. */ |
| 27 | + export var NULL: NodeBuffer; |
| 28 | + /** A pointer-sized Buffer instance pointing to NULL. */ |
| 29 | + export var NULL_POINTER: NodeBuffer; |
| 30 | + /** Get the memory address of buffer. */ |
| 31 | + export function address(buffer: NodeBuffer): number; |
| 32 | + /** Allocate the memory with the given value written to it. */ |
| 33 | + export function alloc(type: Type, value?): NodeBuffer; |
| 34 | + /** Allocate the memory with the given value written to it. */ |
| 35 | + export function alloc(type: string, value?): NodeBuffer; |
| 36 | + |
| 37 | + /** |
| 38 | + * Allocate the memory with the given string written to it with the given |
| 39 | + * encoding (defaults to utf8). The buffer is 1 byte longer than the |
| 40 | + * string itself, and is NULL terminated. |
| 41 | + */ |
| 42 | + export function allocCString(string: string, encoding?: string): NodeBuffer; |
| 43 | + |
| 44 | + /** Coerce a type.*/ |
| 45 | + export function coerceType(type: Type): Type; |
| 46 | + /** Coerce a type. String are looked up from the ref.types object. */ |
| 47 | + export function coerceType(type: string): Type; |
| 48 | + |
| 49 | + /** |
| 50 | + * Get value after dereferencing buffer. |
| 51 | + * That is, first it checks the indirection count of buffer's type, and |
| 52 | + * if it's greater than 1 then it merely returns another Buffer, but with |
| 53 | + * one level less indirection. |
| 54 | + */ |
| 55 | + export function deref(buffer: NodeBuffer): any; |
| 56 | + |
| 57 | + /** Create clone of the type, with decremented indirection level by 1. */ |
| 58 | + export function derefType(type: Type): Type; |
| 59 | + /** Create clone of the type, with decremented indirection level by 1. */ |
| 60 | + export function derefType(type: string): Type; |
| 61 | + /** Represents the native endianness of the processor ("LE" or "BE"). */ |
| 62 | + export var endianness: string; |
| 63 | + /** Check the indirection level and return a dereferenced when necessary. */ |
| 64 | + export function get(buffer: NodeBuffer, offset?: number, type?: Type): any; |
| 65 | + /** Check the indirection level and return a dereferenced when necessary. */ |
| 66 | + export function get(buffer: NodeBuffer, offset?: number, type?: string): any; |
| 67 | + /** Get type of the buffer. Create a default type when none exists. */ |
| 68 | + export function getType(buffer: NodeBuffer): Type; |
| 69 | + /** Check the NULL. */ |
| 70 | + export function isNull(buffer: NodeBuffer): boolean; |
| 71 | + /** Read C string until the first NULL. */ |
| 72 | + export function readCString(buffer: NodeBuffer, offset?: number): string; |
| 73 | + |
| 74 | + /** |
| 75 | + * Read a big-endian signed 64-bit int |
| 76 | + * If there is losing precision, then return a string, otherwise a number. |
| 77 | + * @return {number|string} |
| 78 | + */ |
| 79 | + export function readInt64BE(buffer: NodeBuffer, offset?: number): any; |
| 80 | + |
| 81 | + /** |
| 82 | + * Read a little-endian signed 64-bit int |
| 83 | + * If there is losing precision, then return a string, otherwise a number. |
| 84 | + * @return {number|string} |
| 85 | + */ |
| 86 | + export function readInt64LE(buffer: NodeBuffer, offset?: number): any; |
| 87 | + |
| 88 | + /** Read a JS Object that has previously been written. */ |
| 89 | + export function readObject(buffer: NodeBuffer, offset?: number): Object; |
| 90 | + /** Read data from the pointer. */ |
| 91 | + export function readPointer(buffer: NodeBuffer, offset?: number, |
| 92 | + length?: number): NodeBuffer; |
| 93 | + /** |
| 94 | + * Read a big-endian unsigned 64-bit int |
| 95 | + * If there is losing precision, then return a string, otherwise a number. |
| 96 | + * @return {number|string} |
| 97 | + */ |
| 98 | + export function readUInt64BE(buffer: NodeBuffer, offset?: number): any; |
| 99 | + |
| 100 | + /** |
| 101 | + * Read a little-endian unsigned 64-bit int |
| 102 | + * If there is losing precision, then return a string, otherwise a number. |
| 103 | + * @return {number|string} |
| 104 | + */ |
| 105 | + export function readUInt64LE(buffer: NodeBuffer, offset?: number): any; |
| 106 | + |
| 107 | + /** Create pointer to buffer. */ |
| 108 | + export function ref(buffer: NodeBuffer): NodeBuffer; |
| 109 | + /** Create clone of the type, with incremented indirection level by 1. */ |
| 110 | + export function refType(type: Type): Type; |
| 111 | + /** Create clone of the type, with incremented indirection level by 1. */ |
| 112 | + export function refType(type: string): Type; |
| 113 | + |
| 114 | + /** |
| 115 | + * Create buffer with the specified size, with the same address as source. |
| 116 | + * This function "attaches" source to the returned buffer to prevent it from |
| 117 | + * being garbage collected. |
| 118 | + */ |
| 119 | + export function reinterpret(buffer: NodeBuffer, size: number, |
| 120 | + offset?: number): NodeBuffer; |
| 121 | + /** |
| 122 | + * Scan past the boundary of the Buffer's length until it finds size number |
| 123 | + * of aligned NULL bytes. |
| 124 | + */ |
| 125 | + export function reinterpretUntilZeros(buffer: NodeBuffer, size: number, |
| 126 | + offset?: number): NodeBuffer; |
| 127 | + |
| 128 | + /** Write pointer if the indirection is 1, otherwise write value. */ |
| 129 | + export function set(buffer: NodeBuffer, offset: number, value, type?: Type): void; |
| 130 | + /** Write pointer if the indirection is 1, otherwise write value. */ |
| 131 | + export function set(buffer: NodeBuffer, offset: number, value, type?: string): void; |
| 132 | + /** Write the string as a NULL terminated. Default encoding is utf8. */ |
| 133 | + export function writeCString(buffer: NodeBuffer, offset: number, |
| 134 | + string: string, encoding?: string): void; |
| 135 | + /** Write a big-endian signed 64-bit int. */ |
| 136 | + export function writeInt64BE(buffer: NodeBuffer, offset: number, input: number): void; |
| 137 | + /** Write a big-endian signed 64-bit int. */ |
| 138 | + export function writeInt64BE(buffer: NodeBuffer, offset: number, input: string): void; |
| 139 | + /** Write a little-endian signed 64-bit int. */ |
| 140 | + export function writeInt64LE(buffer: NodeBuffer, offset: number, input: number): void; |
| 141 | + /** Write a little-endian signed 64-bit int. */ |
| 142 | + export function writeInt64LE(buffer: NodeBuffer, offset: number, input: string): void; |
| 143 | + |
| 144 | + /** |
| 145 | + * Write the JS Object. This function "attaches" object to buffer to prevent |
| 146 | + * it from being garbage collected. |
| 147 | + */ |
| 148 | + export function writeObject(buffer: NodeBuffer, offset: number, object: Object): void; |
| 149 | + |
| 150 | + /** |
| 151 | + * Write the memory address of pointer to buffer at the specified offset. This |
| 152 | + * function "attaches" object to buffer to prevent it from being garbage collected. |
| 153 | + */ |
| 154 | + export function writePointer(buffer: NodeBuffer, offset: number, |
| 155 | + pointer: NodeBuffer): void; |
| 156 | + |
| 157 | + /** Write a little-endian unsigned 64-bit int. */ |
| 158 | + export function writeUInt64BE(buffer: NodeBuffer, offset: number, input: number): void; |
| 159 | + /** Write a little-endian unsigned 64-bit int. */ |
| 160 | + export function writeUInt64BE(buffer: NodeBuffer, offset: number, input: string): void; |
| 161 | + |
| 162 | + /** |
| 163 | + * Attach object to buffer such. |
| 164 | + * It prevents object from being garbage collected until buffer does. |
| 165 | + */ |
| 166 | + export function _attach(buffer: NodeBuffer, object: Object); |
| 167 | + |
| 168 | + /** Same as ref.reinterpret, except that this version does not attach buffer. */ |
| 169 | + export function _reinterpret(buffer: NodeBuffer, size: number, |
| 170 | + offset?: number): NodeBuffer; |
| 171 | + /** Same as ref.reinterpretUntilZeros, except that this version does not attach buffer. */ |
| 172 | + export function _reinterpretUntilZeros(buffer: NodeBuffer, size: number, |
| 173 | + offset?: number): NodeBuffer; |
| 174 | + /** Same as ref.writePointer, except that this version does not attach pointer. */ |
| 175 | + export function _writePointer(buffer: NodeBuffer, offset: number, |
| 176 | + pointer: NodeBuffer): void; |
| 177 | + /** Same as ref.writeObject, except that this version does not attach object. */ |
| 178 | + export function _writeObject(buffer: NodeBuffer, offset: number, object: Object): void; |
| 179 | + |
| 180 | + export var types: { |
| 181 | + void: Type; int64: Type; ushort: Type; |
| 182 | + int: Type; uint64: Type; float: Type; |
| 183 | + uint: Type; long: Type; double: Type; |
| 184 | + int8: Type; ulong: Type; Object: Type; |
| 185 | + uint8: Type; longlong: Type; CString: Type; |
| 186 | + int16: Type; ulonglong: Type; bool: Type; |
| 187 | + uint16: Type; char: Type; byte: Type; |
| 188 | + int32: Type; uchar: Type; size_t: Type; |
| 189 | + uint32: Type; short: Type; |
| 190 | + }; |
| 191 | +} |
| 192 | + |
| 193 | +interface NodeBuffer { |
| 194 | + /** Shorthand for ref.address. */ |
| 195 | + address(): number; |
| 196 | + /** Shorthand for ref.deref. */ |
| 197 | + deref(): any; |
| 198 | + /** Shorthand for ref.isNull. */ |
| 199 | + isNull(): boolean; |
| 200 | + /** Shorthand for ref.readCString. */ |
| 201 | + readCString(offset?: number): string; |
| 202 | + /** Shorthand for ref.readInt64BE. */ |
| 203 | + readInt64BE(offset?: number): string; |
| 204 | + /** Shorthand for ref.readInt64LE. */ |
| 205 | + readInt64LE(offset?: number): string; |
| 206 | + /** Shorthand for ref.readObject. */ |
| 207 | + readObject(offset?: number): string; |
| 208 | + /** Shorthand for ref.readPointer. */ |
| 209 | + readPointer(offset?: number): string; |
| 210 | + /** Shorthand for ref.readUInt64BE. */ |
| 211 | + readUInt64BE(offset?: number): string; |
| 212 | + /** Shorthand for ref.readUInt64LE. */ |
| 213 | + readUInt64LE(offset?: number): string; |
| 214 | + /** Shorthand for ref.ref. */ |
| 215 | + ref(): NodeBuffer; |
| 216 | + /** Shorthand for ref.reinterpret. */ |
| 217 | + reinterpret(size: number, offset?: number): NodeBuffer; |
| 218 | + /** Shorthand for ref.reinterpretUntilZeros. */ |
| 219 | + reinterpretUntilZeros(size: number, offset?: number): NodeBuffer; |
| 220 | + /** Shorthand for ref.writeCString. */ |
| 221 | + writeCString(offset: number, string: string, encoding?: string): void; |
| 222 | + /** Shorthand for ref.writeInt64BE. */ |
| 223 | + writeInt64BE(offset: number, input: number): any; |
| 224 | + /** Shorthand for ref.writeInt64BE. */ |
| 225 | + writeInt64BE(offset: number, input: string): any; |
| 226 | + /** Shorthand for ref.writeInt64LE. */ |
| 227 | + writeInt64LE(offset: number, input: number): any; |
| 228 | + /** Shorthand for ref.writeInt64LE. */ |
| 229 | + writeInt64LE(offset: number, input: string): any; |
| 230 | + /** Shorthand for ref.writeObject. */ |
| 231 | + writeObject(offset: number, object: Object): void; |
| 232 | + /** Shorthand for ref.writePointer. */ |
| 233 | + writePointer(offset: number, pointer: NodeBuffer): void; |
| 234 | + /** Shorthand for ref.writeUInt64BE. */ |
| 235 | + writeUInt64BE(offset: number, input: number): any; |
| 236 | + /** Shorthand for ref.writeUInt64BE. */ |
| 237 | + writeUInt64BE(offset: number, input: string): any; |
| 238 | + /** Shorthand for ref.writeUInt64LE. */ |
| 239 | + writeUInt64LE(offset: number, input: number): any; |
| 240 | + /** Shorthand for ref.writeUInt64LE. */ |
| 241 | + writeUInt64LE(offset: number, input: string): any; |
| 242 | + |
| 243 | + /** |
| 244 | + * Generate string for inspecting. |
| 245 | + * String includes the hex-encoded memory address of the Buffer instance. |
| 246 | + * @override |
| 247 | + */ |
| 248 | + inspect(): string; |
| 249 | +} |
0 commit comments