Move Type interface from "ffi" module into "ref" module

This commit is contained in:
Paul Loyd
2013-09-10 21:43:05 +04:00
parent 1bcbd4e538
commit 545340a970
2 changed files with 67 additions and 68 deletions
+1 -1
View File
@@ -77,7 +77,7 @@ import TArray = require('ref-array');
}
{
var ST = Struct();
var test: ffi.Type = ST.fields['t'].type;
var test: ref.Type = ST.fields['t'].type;
}
{
var CharArray = TArray('char');
+66 -67
View File
@@ -6,23 +6,9 @@
/// <reference path="../node/node.d.ts" />
declare module "ffi" {
import ref = require('ref');
import StructType = require('ref-struct');
export interface Type {
/** The size in bytes required to hold this datatype. */
size: number;
/** The current level of indirection of the buffer. */
indirection: number;
/** To invoke when `ref.get` is invoked on a buffer of this type. */
get(buffer: NodeBuffer, offset: number): any;
/** To invoke when `ref.set` is invoked on a buffer of this type. */
set(buffer: NodeBuffer, offset: number, value): void;
/** The name to use during debugging for this datatype. */
name?: string;
/** The alignment of this datatype when placed inside a struct. */
alignment?: number;
}
/** Provides a friendly API on-top of `DynamicLibrary` and `ForeignFunction`. */
export var Library: {
/** The extension to use on libraries. */
@@ -46,11 +32,11 @@ declare module "ffi" {
/** Get value of errno. */
export function errno(): number;
export interface Function extends Type {
export interface Function extends ref.Type {
/** The type of return value. */
retType: Type;
retType: ref.Type;
/** The type of arguments. */
argTypes: Type[];
argTypes: ref.Type[];
/** Is set for node-ffi functions. */
ffi_type: NodeBuffer;
abi: number;
@@ -63,9 +49,9 @@ declare module "ffi" {
/** Creates and returns a type for a C function pointer. */
export var Function: {
new (retType: Type, argTypes: any[], abi?: number): Function;
new (retType: ref.Type, argTypes: any[], abi?: number): Function;
new (retType: string, argTypes: any[], abi?: number): Function;
(retType: Type, argTypes: any[], abi?: number): Function;
(retType: ref.Type, argTypes: any[], abi?: number): Function;
(retType: string, argTypes: any[], abi?: number): Function;
};
@@ -81,9 +67,9 @@ declare module "ffi" {
* execution.
*/
export var ForeignFunction: {
new (ptr: NodeBuffer, retType: Type, argTypes: any[], abi?: number): ForeignFunction;
new (ptr: NodeBuffer, retType: ref.Type, argTypes: any[], abi?: number): ForeignFunction;
new (ptr: NodeBuffer, retType: string, argTypes: any[], abi?: number): ForeignFunction;
(ptr: NodeBuffer, retType: Type, argTypes: any[], abi?: number): ForeignFunction;
(ptr: NodeBuffer, retType: ref.Type, argTypes: any[], abi?: number): ForeignFunction;
(ptr: NodeBuffer, retType: string, argTypes: any[], abi?: number): ForeignFunction;
}
@@ -110,9 +96,9 @@ declare module "ffi" {
* contain the same ffi_type argument signature.
*/
export var VariadicForeignFunction: {
new (ptr: NodeBuffer, ret: Type, fixedArgs: any[], abi?: number): VariadicForeignFunction;
new (ptr: NodeBuffer, ret: ref.Type, fixedArgs: any[], abi?: number): VariadicForeignFunction;
new (ptr: NodeBuffer, ret: string, fixedArgs: any[], abi?: number): VariadicForeignFunction;
(ptr: NodeBuffer, ret: Type, fixedArgs: any[], abi?: number): VariadicForeignFunction;
(ptr: NodeBuffer, ret: ref.Type, fixedArgs: any[], abi?: number): VariadicForeignFunction;
(ptr: NodeBuffer, ret: string, fixedArgs: any[], abi?: number): VariadicForeignFunction;
};
@@ -162,7 +148,7 @@ declare module "ffi" {
export var ffiType: {
/** Get a `ffi_type *` Buffer appropriate for the given type. */
(type: Type): NodeBuffer
(type: ref.Type): NodeBuffer
/** Get a `ffi_type *` Buffer appropriate for the given type. */
(type: string): NodeBuffer
FFI_TYPE: StructType;
@@ -193,20 +179,33 @@ declare module "ffi" {
/** Default types. */
export var types: {
void: Type; int64: Type; ushort: Type;
int: Type; uint64: Type; float: Type;
uint: Type; long: Type; double: Type;
int8: Type; ulong: Type; Object: Type;
uint8: Type; longlong: Type; CString: Type;
int16: Type; ulonglong: Type; bool: Type;
uint16: Type; char: Type; byte: Type;
int32: Type; uchar: Type; size_t: Type;
uint32: Type; short: Type;
void: ref.Type; int64: ref.Type; ushort: ref.Type;
int: ref.Type; uint64: ref.Type; float: ref.Type;
uint: ref.Type; long: ref.Type; double: ref.Type;
int8: ref.Type; ulong: ref.Type; Object: ref.Type;
uint8: ref.Type; longlong: ref.Type; CString: ref.Type;
int16: ref.Type; ulonglong: ref.Type; bool: ref.Type;
uint16: ref.Type; char: ref.Type; byte: ref.Type;
int32: ref.Type; uchar: ref.Type; size_t: ref.Type;
uint32: ref.Type; short: ref.Type;
};
}
declare module "ref" {
import ffi = require('ffi');
export interface Type {
/** The size in bytes required to hold this datatype. */
size: number;
/** The current level of indirection of the buffer. */
indirection: number;
/** To invoke when `ref.get` is invoked on a buffer of this type. */
get(buffer: NodeBuffer, offset: number): any;
/** To invoke when `ref.set` is invoked on a buffer of this type. */
set(buffer: NodeBuffer, offset: number, value): void;
/** The name to use during debugging for this datatype. */
name?: string;
/** The alignment of this datatype when placed inside a struct. */
alignment?: number;
}
/** A Buffer that references the C NULL pointer. */
export var NULL: NodeBuffer;
@@ -215,7 +214,7 @@ declare module "ref" {
/** Get the memory address of buffer. */
export function address(buffer: NodeBuffer): number;
/** Allocate the memory with the given value written to it. */
export function alloc(type: ffi.Type, value?): NodeBuffer;
export function alloc(type: Type, value?): NodeBuffer;
/** Allocate the memory with the given value written to it. */
export function alloc(type: string, value?): NodeBuffer;
@@ -227,9 +226,9 @@ declare module "ref" {
export function allocCString(string: string, encoding?: string): NodeBuffer;
/** Coerce a type.*/
export function coerceType(type: ffi.Type): ffi.Type;
export function coerceType(type: Type): Type;
/** Coerce a type. String are looked up from the ref.types object. */
export function coerceType(type: string): ffi.Type;
export function coerceType(type: string): Type;
/**
* Get value after dereferencing buffer.
@@ -240,17 +239,17 @@ declare module "ref" {
export function deref(buffer: NodeBuffer): any;
/** Create clone of the type, with decremented indirection level by 1. */
export function derefType(type: ffi.Type): ffi.Type;
export function derefType(type: Type): Type;
/** Create clone of the type, with decremented indirection level by 1. */
export function derefType(type: string): ffi.Type;
export function derefType(type: string): Type;
/** Represents the native endianness of the processor ("LE" or "BE"). */
export var endianness: string;
/** Check the indirection level and return a dereferenced when necessary. */
export function get(buffer: NodeBuffer, offset?: number, type?: ffi.Type): any;
export function get(buffer: NodeBuffer, offset?: number, type?: Type): any;
/** Check the indirection level and return a dereferenced when necessary. */
export function get(buffer: NodeBuffer, offset?: number, type?: string): any;
/** Get type of the buffer. Create a default type when none exists. */
export function getType(buffer: NodeBuffer): ffi.Type;
export function getType(buffer: NodeBuffer): Type;
/** Check the NULL. */
export function isNull(buffer: NodeBuffer): boolean;
/** Read C string until the first NULL. */
@@ -292,9 +291,9 @@ declare module "ref" {
/** Create pointer to buffer. */
export function ref(buffer: NodeBuffer): NodeBuffer;
/** Create clone of the type, with incremented indirection level by 1. */
export function refType(type: ffi.Type): ffi.Type;
export function refType(type: Type): Type;
/** Create clone of the type, with incremented indirection level by 1. */
export function refType(type: string): ffi.Type;
export function refType(type: string): Type;
/**
* Create buffer with the specified size, with the same address as source.
@@ -311,7 +310,7 @@ declare module "ref" {
offset?: number): NodeBuffer;
/** Write pointer if the indirection is 1, otherwise write value. */
export function set(buffer: NodeBuffer, offset: number, value, type?: ffi.Type): void;
export function set(buffer: NodeBuffer, offset: number, value, type?: Type): void;
/** Write pointer if the indirection is 1, otherwise write value. */
export function set(buffer: NodeBuffer, offset: number, value, type?: string): void;
/** Write the string as a NULL terminated. Default encoding is utf8. */
@@ -364,15 +363,15 @@ declare module "ref" {
/** Default types. */
export var types: {
void: ffi.Type; int64: ffi.Type; ushort: ffi.Type;
int: ffi.Type; uint64: ffi.Type; float: ffi.Type;
uint: ffi.Type; long: ffi.Type; double: ffi.Type;
int8: ffi.Type; ulong: ffi.Type; Object: ffi.Type;
uint8: ffi.Type; longlong: ffi.Type; CString: ffi.Type;
int16: ffi.Type; ulonglong: ffi.Type; bool: ffi.Type;
uint16: ffi.Type; char: ffi.Type; byte: ffi.Type;
int32: ffi.Type; uchar: ffi.Type; size_t: ffi.Type;
uint32: ffi.Type; short: ffi.Type;
void: Type; int64: Type; ushort: Type;
int: Type; uint64: Type; float: Type;
uint: Type; long: Type; double: Type;
int8: Type; ulong: Type; Object: Type;
uint8: Type; longlong: Type; CString: Type;
int16: Type; ulonglong: Type; bool: Type;
uint16: Type; char: Type; byte: Type;
int32: Type; uchar: Type; size_t: Type;
uint32: Type; short: Type;
};
}
@@ -435,12 +434,12 @@ interface NodeBuffer {
}
declare module "ref-array" {
import ffi = require('ffi');
import ref = require('ref');
interface ArrayType extends ffi.Type {
interface ArrayType extends ref.Type {
BYTES_PER_ELEMENT: number;
/** The reference to the base type. */
type: ffi.Type;
type: ref.Type;
/**
* Accepts a Buffer instance that should be an already-populated with data
@@ -471,9 +470,9 @@ declare module "ref-array" {
* TypedArray API.
*/
var ArrayType: {
new (type: ffi.Type, length?: number): ArrayType;
new (type: ref.Type, length?: number): ArrayType;
new (type: string, length?: number): ArrayType;
(type: ffi.Type, length?: number): ArrayType;
(type: ref.Type, length?: number): ArrayType;
(type: string, length?: number): ArrayType;
};
@@ -481,7 +480,7 @@ declare module "ref-array" {
}
declare module "ref-struct" {
import ffi = require('ffi');
import ref = require('ref');
/**
* This is the `constructor` of the Struct type that gets returned.
@@ -493,7 +492,7 @@ declare module "ref-struct" {
*
* @constructor
*/
interface StructType extends ffi.Type {
interface StructType extends ref.Type {
/** Pass it an existing Buffer instance to use that as the backing buffer. */
new (arg: NodeBuffer, data?: {}): any;
new (data?: {}): any;
@@ -501,7 +500,7 @@ declare module "ref-struct" {
(arg: NodeBuffer, data?: {}): any;
(data?: {}): any;
fields: {[key: string]: {type: ffi.Type}};
fields: {[key: string]: {type: ref.Type}};
/**
* Adds a new field to the struct instance with the given name and type.
@@ -509,7 +508,7 @@ declare module "ref-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;
defineProperty(name: string, type: ref.Type): void;
/**
* Adds a new field to the struct instance with the given name and type.
@@ -538,7 +537,7 @@ declare module "ref-struct" {
}
declare module "ref-union" {
import ffi = require('ffi');
import ref = require('ref');
/**
* This is the `constructor` of the Struct type that gets returned.
@@ -550,7 +549,7 @@ declare module "ref-union" {
*
* @constructor
*/
interface UnionType extends ffi.Type {
interface UnionType extends ref.Type {
/** Pass it an existing Buffer instance to use that as the backing buffer. */
new (arg: NodeBuffer, data?: {}): any;
new (data?: {}): any;
@@ -558,7 +557,7 @@ declare module "ref-union" {
(arg: NodeBuffer, data?: {}): any;
(data?: {}): any;
fields: {[key: string]: {type: ffi.Type}};
fields: {[key: string]: {type: ref.Type}};
/**
* Adds a new field to the union instance with the given name and type.
@@ -566,7 +565,7 @@ declare module "ref-union" {
* 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;
defineProperty(name: string, type: ref.Type): void;
/**
* Adds a new field to the union instance with the given name and type.