Add "ref-struct" module

This commit is contained in:
Paul Loyd
2013-09-10 21:43:05 +04:00
parent 660cb0bf91
commit 8bbceef0c2
2 changed files with 81 additions and 3 deletions
+23
View File
@@ -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;
}
+58 -3
View File
@@ -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. */
@@ -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;
}