Skip to content

Commit

Permalink
Add "ref-struct" module
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Sep 10, 2013
1 parent 660cb0b commit 8bbceef
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
23 changes: 23 additions & 0 deletions node-ffi/node-ffi-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ffi = require('ffi');
import ref = require('ref');
import Struct = require('ref-struct');

{
var sqlite3 = ref.types.void;
Expand Down Expand Up @@ -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;
}
61 changes: 58 additions & 3 deletions node-ffi/node-ffi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
/// <reference path="../node/node.d.ts" />

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. */
Expand Down Expand Up @@ -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;
}

0 comments on commit 8bbceef

Please sign in to comment.