diff --git a/node-ffi/node-ffi-tests.ts b/node-ffi/node-ffi-tests.ts index a29d8dbd6d..06aa2a2ee7 100644 --- a/node-ffi/node-ffi-tests.ts +++ b/node-ffi/node-ffi-tests.ts @@ -2,6 +2,7 @@ import ffi = require('ffi'); import ref = require('ref'); +import Struct = require('ref-struct'); { var sqlite3 = ref.types.void; @@ -54,3 +55,25 @@ import ref = require('ref'); var buf = ref.alloc('int64'); ref.writeInt64LE(buf, 0, '9223372036854775807'); } +{ + var S1 = Struct({ a: ref.types.int }); + var S2 = new Struct({ a: 'int' }); +} +{ + var P = new Struct; + P.defineProperty('a', ref.types.int); + P.defineProperty('d', 'long'); +} +{ + var SimpleStruct = Struct({ + first : ref.types.byte, + last : ref.types.byte + }); + + var ss = new SimpleStruct({ first: 50, last: 100 }); + ss.first += 200; +} +{ + var ST = Struct(); + var test: ffi.Type = ST.fields['t'].type; +} diff --git a/node-ffi/node-ffi.d.ts b/node-ffi/node-ffi.d.ts index 9b4987a427..2ec08e8131 100644 --- a/node-ffi/node-ffi.d.ts +++ b/node-ffi/node-ffi.d.ts @@ -6,9 +6,7 @@ /// declare module "ffi" { - // import StructType = require('ref-struct'); - - export interface StructType {} + import StructType = require('ref-struct'); export interface Type { /** The size in bytes required to hold this datatype. */ @@ -435,3 +433,60 @@ interface NodeBuffer { */ inspect(): string; } + +declare module "ref-struct" { + import ffi = require('ffi'); + + /** + * This is the `constructor` of the Struct type that gets returned. + * + * Invoke it with `new` to create a new Buffer instance backing the struct. + * Pass it an existing Buffer instance to use that as the backing buffer. + * Pass in an Object containing the struct fields to auto-populate the + * struct with the data. + * + * @constructor + */ + interface StructType extends ffi.Type { + /** Pass it an existing Buffer instance to use that as the backing buffer. */ + new (arg: NodeBuffer, data?: {}): any; + new (data?: {}): any; + /** Pass it an existing Buffer instance to use that as the backing buffer. */ + (arg: NodeBuffer, data?: {}): any; + (data?: {}): any; + + fields: {[key: string]: {type: ffi.Type}}; + + /** + * Adds a new field to the struct instance with the given name and type. + * Note that this function will throw an Error if any instances of the struct + * type have already been created, therefore this function must be called at the + * beginning, before any instances are created. + */ + defineProperty(name: string, type: ffi.Type): void; + + /** + * Adds a new field to the struct instance with the given name and type. + * Note that this function will throw an Error if any instances of the struct + * type have already been created, therefore this function must be called at the + * beginning, before any instances are created. + */ + defineProperty(name: string, type: string): void; + + /** + * Custom for struct type instances. + * @override + */ + toString(): string; + } + + /** The struct type meta-constructor. */ + var StructType: { + new (fields?: {}): StructType; + new (fields?: any[]): StructType; + (fields?: {}): StructType; + (fields?: any[]): StructType; + } + + export = StructType; +}