Optimize type of params by Union Types (#20680)

* Optimize type of params by Union Types

* Avoid variable name of parameter clashes with keyword/type

writeCString()

* Add tslint.json

* tsconfig: turn strictNullChecks on

* Use const instead of var

* Use arrow function

* Update by new-parens

* Update for object-literal-key-quotes

* Update semicolon

* types: use object instead of Object

* types: use object instead of Object

* types: merged by union types

* Use const instead of var

* tsling: no-consecutive-blank-lines max 3

* Fix dt-header by 0.1

* Add '// TypeScript Version: 2.2' under header

* Fix for trim-file

* Update for prefer-declare-function

* tsling: turn ban-types off for use of 'Function'

* Update definition of Library

* Remove `@overide` tag
This commit is contained in:
waiting 2018-01-02 19:32:22 -06:00 committed by Mohamed Hegazy
parent 8e80f13320
commit 6fb134d9fc
5 changed files with 103 additions and 191 deletions

View File

@ -5,102 +5,102 @@ import Union = require('ref-union');
import TArray = require('ref-array');
{
var sqlite3 = ref.types.void;
var sqlite3Ptr = ref.refType(sqlite3);
var sqlite3PtrPtr = ref.refType(sqlite3Ptr);
var stringPtr = ref.refType(ref.types.CString);
const sqlite3 = ref.types.void;
const sqlite3Ptr = ref.refType(sqlite3);
const sqlite3PtrPtr = ref.refType(sqlite3Ptr);
const stringPtr = ref.refType(ref.types.CString);
var libsqlite3 = ffi.Library('libsqlite3', {
'sqlite3_open': [ 'int', [ 'string', sqlite3PtrPtr ] ],
'sqlite3_close': [ 'int', [ sqlite3PtrPtr ] ],
'sqlite3_exec': [ 'int', [ sqlite3PtrPtr, 'string', 'pointer', 'pointer', stringPtr ] ],
'sqlite3_changes': [ 'int', [ sqlite3PtrPtr ]]
const libsqlite3 = ffi.Library('libsqlite3', {
sqlite3_open: [ 'int', [ 'string', sqlite3PtrPtr ] ],
sqlite3_close: [ 'int', [ sqlite3PtrPtr ] ],
sqlite3_exec: [ 'int', [ sqlite3PtrPtr, 'string', 'pointer', 'pointer', stringPtr ] ],
sqlite3_changes: [ 'int', [ sqlite3PtrPtr ]]
});
var dbPtrPtr = ref.alloc(sqlite3PtrPtr);
const dbPtrPtr = ref.alloc(sqlite3PtrPtr);
libsqlite3.sqlite3_open("test.sqlite3", dbPtrPtr);
}
{
var func = ffi.ForeignFunction(new Buffer(10), 'int', [ 'int' ]);
const func = ffi.ForeignFunction(new Buffer(10), 'int', [ 'int' ]);
func(-5);
func.async(-5, function(err: any, res: any) {});
func.async(-5, (err: any, res: any) => {});
}
{
var funcPtr = ffi.Callback('int', [ 'int' ], Math.abs);
var func = ffi.ForeignFunction(funcPtr, 'int', [ 'int' ]);
const funcPtr = ffi.Callback('int', [ 'int' ], Math.abs);
const func = ffi.ForeignFunction(funcPtr, 'int', [ 'int' ]);
}
{
var printfPointer = ffi.DynamicLibrary().get('printf');
var printfGen = ffi.VariadicForeignFunction(printfPointer, 'void', [ 'string' ]);
const printfPointer = ffi.DynamicLibrary().get('printf');
const printfGen = ffi.VariadicForeignFunction(printfPointer, 'void', [ 'string' ]);
printfGen()('Hello World!\n');
printfGen('int')('This is an int: %d\n', 10);
printfGen('string')('This is a string: %s\n', 'hello');
}
{
ref.address(new Buffer(1));
var intBuf = ref.alloc(ref.types.int);
var intWith4 = ref.alloc(ref.types.int, 4);
var buf0 = ref.allocCString('hello world');
var type = ref.coerceType('int **');
var val = ref.deref(intBuf);
const intBuf = ref.alloc(ref.types.int);
const intWith4 = ref.alloc(ref.types.int, 4);
const buf0 = ref.allocCString('hello world');
const type = ref.coerceType('int **');
const val = ref.deref(intBuf);
}
{
ref.isNull(new Buffer(1));
}
{
var str = ref.readCString(new Buffer('hello\0world\0'), 0);
var buf = ref.alloc('int64');
const str = ref.readCString(new Buffer('hello\0world\0'), 0);
const buf = ref.alloc('int64');
ref.writeInt64BE(buf, 0, '9223372036854775807');
var val = ref.readInt64BE(buf, 0)
const val = ref.readInt64BE(buf, 0);
}
{
var voidPtrType = ref.refType(ref.types.void);
var buf = ref.alloc('int64');
const voidPtrType = ref.refType(ref.types.void);
const buf = ref.alloc('int64');
ref.writeInt64LE(buf, 0, '9223372036854775807');
}
{
var S1 = Struct({ a: ref.types.int });
var S2 = new Struct({ a: 'int' });
const S1 = Struct({ a: ref.types.int });
const S2 = new Struct({ a: 'int' });
}
{
var P = new Struct;
const P = new Struct();
P.defineProperty('a', ref.types.int);
P.defineProperty('d', 'long');
}
{
var SimpleStruct = Struct({
const SimpleStruct = Struct({
first : ref.types.byte,
last : ref.types.byte
});
var ss = new SimpleStruct({ first: 50, last: 100 });
const ss = new SimpleStruct({ first: 50, last: 100 });
ss.first += 200;
}
{
var ST = Struct();
var test: ref.Type = ST.fields['t'].type;
const ST = Struct();
const test: ref.Type = ST.fields['t'].type;
}
{
var CharArray = TArray('char');
var b = new Buffer('hello', 'ascii');
var a = new CharArray(b);
const CharArray = TArray('char');
const b = new Buffer('hello', 'ascii');
const a = new CharArray(b);
}
{
var Int32Array = TArray(ref.types.int32);
var input = [1, 4, 91, 123123, 5123512, 0, -1];
var a = new Int32Array(input);
const Int32Array = TArray(ref.types.int32);
const input = [1, 4, 91, 123123, 5123512, 0, -1];
const a = new Int32Array(input);
}
{
var int = ref.types.int;
var IntArray = TArray(int);
const int = ref.types.int;
const IntArray = TArray(int);
var buf = new Buffer(int.size * 3);
const buf = new Buffer(int.size * 3);
int.set(buf, int.size * 0, 5);
int.set(buf, int.size * 1, 8);
int.set(buf, int.size * 2, 0);
var array = IntArray.untilZeros(buf);
const array = IntArray.untilZeros(buf);
}
{
var refCharArr = TArray('char')([1, 3, 5], 2).ref();
const refCharArr = TArray('char')([1, 3, 5], 2).ref();
}

98
types/ffi/index.d.ts vendored
View File

@ -1,4 +1,4 @@
// Type definitions for node-ffi
// Type definitions for node-ffi 0.1
// Project: https://github.com/rbranson/node-ffi
// Definitions by: Paul Loyd <https://github.com/loyd>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@ -11,7 +11,7 @@ import ref = require('ref');
import StructType = require('ref-struct');
/** Provides a friendly API on-top of `DynamicLibrary` and `ForeignFunction`. */
export var Library: {
export interface Library {
/** The extension to use on libraries. */
EXT: string;
@ -20,15 +20,16 @@ export var Library: {
* @param funcs hash of [retType, [...argType], opts?: {abi?, async?, varargs?}]
* @param lib hash that will be extended
*/
new (libFile: string, funcs?: {[key: string]: any[]}, lib?: Object): any;
new (libFile: string, funcs?: {[key: string]: any[]}, lib?: object): any;
/**
* @param libFile name of library
* @param funcs hash of [retType, [...argType], opts?: {abi?, async?, varargs?}]
* @param lib hash that will be extended
*/
(libFile: string, funcs?: {[key: string]: any[]}, lib?: Object): any;
};
(libFile: string, funcs?: {[key: string]: any[]}, lib?: object): any;
}
export const Library: Library;
/** Get value of errno. */
export function errno(): number;
@ -49,11 +50,9 @@ export interface Function extends ref.Type {
}
/** Creates and returns a type for a C function pointer. */
export var Function: {
new (retType: ref.Type, argTypes: any[], abi?: number): Function;
new (retType: string, argTypes: any[], abi?: number): Function;
(retType: ref.Type, argTypes: any[], abi?: number): Function;
(retType: string, argTypes: any[], abi?: number): Function;
export const Function: {
new (retType: ref.Type | string, argTypes: any[], abi?: number): Function;
(retType: ref.Type | string, argTypes: any[], abi?: number): Function;
};
export interface ForeignFunction {
@ -67,12 +66,10 @@ export interface ForeignFunction {
* function into native types and also unmarshalling the return from function
* execution.
*/
export var ForeignFunction: {
new (ptr: Buffer, retType: ref.Type, argTypes: any[], abi?: number): ForeignFunction;
new (ptr: Buffer, retType: string, argTypes: any[], abi?: number): ForeignFunction;
(ptr: Buffer, retType: ref.Type, argTypes: any[], abi?: number): ForeignFunction;
(ptr: Buffer, retType: string, argTypes: any[], abi?: number): ForeignFunction;
}
export const ForeignFunction: {
new (ptr: Buffer, retType: ref.Type | string, argTypes: any[], abi?: number): ForeignFunction;
(ptr: Buffer, retType: ref.Type | string, argTypes: any[], abi?: number): ForeignFunction;
};
export interface VariadicForeignFunction {
/**
@ -96,11 +93,9 @@ export interface VariadicForeignFunction {
* This function takes care of caching and reusing `ForeignFunction` instances that
* contain the same ffi_type argument signature.
*/
export var VariadicForeignFunction: {
new (ptr: Buffer, ret: ref.Type, fixedArgs: any[], abi?: number): VariadicForeignFunction;
new (ptr: Buffer, ret: string, fixedArgs: any[], abi?: number): VariadicForeignFunction;
(ptr: Buffer, ret: ref.Type, fixedArgs: any[], abi?: number): VariadicForeignFunction;
(ptr: Buffer, ret: string, fixedArgs: any[], abi?: number): VariadicForeignFunction;
export const VariadicForeignFunction: {
new (ptr: Buffer, ret: ref.Type | string, fixedArgs: any[], abi?: number): VariadicForeignFunction;
(ptr: Buffer, ret: ref.Type | string, fixedArgs: any[], abi?: number): VariadicForeignFunction;
};
export interface DynamicLibrary {
@ -119,7 +114,7 @@ export interface DynamicLibrary {
* call `get___` on the pointer to dereference it into its actual value, or
* turn the pointer into a callable function with `ForeignFunction`.
*/
export var DynamicLibrary: {
export const DynamicLibrary: {
FLAGS: {
RTLD_LAZY: number;
RTLD_NOW: number;
@ -140,46 +135,44 @@ export var DynamicLibrary: {
* The function pointer may be used in other C functions that
* accept C callback functions.
*/
export var Callback: {
export const Callback: {
new (retType: any, argTypes: any[], abi: number, fn: any): Buffer;
new (retType: any, argTypes: any[], fn: any): Buffer;
(retType: any, argTypes: any[], abi: number, fn: any): Buffer;
(retType: any, argTypes: any[], fn: any): Buffer;
}
};
export var ffiType: {
export const ffiType: {
/** Get a `ffi_type *` Buffer appropriate for the given type. */
(type: ref.Type): Buffer
/** Get a `ffi_type *` Buffer appropriate for the given type. */
(type: string): Buffer
(type: ref.Type | string): Buffer
FFI_TYPE: StructType;
}
};
export var CIF: (retType: any, types: any[], abi?: any) => Buffer
export var CIF_var: (retType: any, types: any[], numFixedArgs: number, abi?: any) => Buffer;
export var HAS_OBJC: boolean;
export var FFI_TYPES: {[key: string]: Buffer};
export var FFI_OK: number;
export var FFI_BAD_TYPEDEF: number;
export var FFI_BAD_ABI: number;
export var FFI_DEFAULT_ABI: number;
export var FFI_FIRST_ABI: number;
export var FFI_LAST_ABI: number;
export var FFI_SYSV: number;
export var FFI_UNIX64: number;
export var RTLD_LAZY: number;
export var RTLD_NOW: number;
export var RTLD_LOCAL: number;
export var RTLD_GLOBAL: number;
export var RTLD_NOLOAD: number;
export var RTLD_NODELETE: number;
export var RTLD_NEXT: Buffer;
export var RTLD_DEFAULT: Buffer;
export var LIB_EXT: string;
export var FFI_TYPE: StructType;
export function CIF(retType: any, types: any[], abi?: any): Buffer;
export function CIF_var(retType: any, types: any[], numFixedArgs: number, abi?: any): Buffer;
export const HAS_OBJC: boolean;
export const FFI_TYPES: {[key: string]: Buffer};
export const FFI_OK: number;
export const FFI_BAD_TYPEDEF: number;
export const FFI_BAD_ABI: number;
export const FFI_DEFAULT_ABI: number;
export const FFI_FIRST_ABI: number;
export const FFI_LAST_ABI: number;
export const FFI_SYSV: number;
export const FFI_UNIX64: number;
export const RTLD_LAZY: number;
export const RTLD_NOW: number;
export const RTLD_LOCAL: number;
export const RTLD_GLOBAL: number;
export const RTLD_NOLOAD: number;
export const RTLD_NODELETE: number;
export const RTLD_NEXT: Buffer;
export const RTLD_DEFAULT: Buffer;
export const LIB_EXT: string;
export const FFI_TYPE: StructType;
/** Default types. */
export var types: {
export const types: {
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;
@ -190,4 +183,3 @@ export var types: {
int32: ref.Type; uchar: ref.Type; size_t: ref.Type;
uint32: ref.Type; short: ref.Type;
};

View File

@ -28,32 +28,23 @@ interface Buffer {
/** Shorthand for `ref.reinterpretUntilZeros`. */
reinterpretUntilZeros(size: number, offset?: number): Buffer;
/** Shorthand for `ref.writeCString`. */
writeCString(offset: number, string: string, encoding?: string): void;
writeCString(offset: number, input: string, encoding?: string): void;
/** Shorthand for `ref.writeInt64BE`. */
writeInt64BE(offset: number, input: number): any;
/** Shorthand for `ref.writeInt64BE`. */
writeInt64BE(offset: number, input: string): any;
writeInt64BE(offset: number, input: number | string): any;
/** Shorthand for `ref.writeInt64LE`. */
writeInt64LE(offset: number, input: number): any;
/** Shorthand for `ref.writeInt64LE`. */
writeInt64LE(offset: number, input: string): any;
writeInt64LE(offset: number, input: number | string): any;
/** Shorthand for `ref.writeObject`. */
writeObject(offset: number, object: Object): void;
writeObject(offset: number, object: object): void;
/** Shorthand for `ref.writePointer`. */
writePointer(offset: number, pointer: Buffer): void;
/** Shorthand for `ref.writeUInt64BE`. */
writeUInt64BE(offset: number, input: number): any;
/** Shorthand for `ref.writeUInt64BE`. */
writeUInt64BE(offset: number, input: string): any;
writeUInt64BE(offset: number, input: number | string): any;
/** Shorthand for `ref.writeUInt64LE`. */
writeUInt64LE(offset: number, input: number): any;
/** Shorthand for `ref.writeUInt64LE`. */
writeUInt64LE(offset: number, input: string): any;
writeUInt64LE(offset: number, input: number | string): any;
/**
* Generate string for inspecting.
* String includes the hex-encoded memory address of the Buffer instance.
* @override
*/
inspect(): string;
}

View File

@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
@ -21,4 +21,4 @@
"node-ffi-buffer.d.ts",
"ffi-tests.ts"
]
}
}

View File

@ -1,79 +1,8 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
"extends": "dtslint/dt.json",
"rules": {
"no-consecutive-blank-lines": [true, 3],
"ban-types": [false]
}
}