How to use the ref-napi.types function in ref-napi

To help you get started, we’ve selected a few ref-napi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github node-ffi-napi / node-ffi-napi / test / library.js View on Github external
describe('Library', function () {
  const charPtr = ref.refType(ref.types.char);

  afterEach(global.gc);

  it('should be a function', function () {
    assert(typeof Library === 'function');
  });

  it('should work with the `new` operator', function () {
    const l = new Library();
    assert(typeof l === 'object');
  });

  it('should accept `null` as a first argument', function () {
    if (process.platform == 'win32') {
      // On Windows, null refers to just the main executable (e.g. node.exe).
      // Windows never searches for symbols across multiple DLL's.
github node-ffi-napi / node-ffi-napi / test / foreign_function.js View on Github external
describe('ForeignFunction', function () {
  afterEach(global.gc);

  // these structs are also defined in ffi_tests.cc
  const box = Struct({
    width: ref.types.int,
    height: ref.types.int
  });

  const arst = Struct({
    num: 'int',
    array: Array('double', 20)
  });

  it('should call the static "abs" bindings', function () {
    const _abs = bindings.abs;
    const abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ]);
    assert.strictEqual('function', typeof abs);
    assert.strictEqual(1234, abs(-1234));
  })

  it('should throw an Error with a meaningful message when type\'s `set()` throws', function () {
github node-ffi-napi / node-ffi-napi / lib / ffi.js View on Github external
const type = bindings.FFI_TYPES[name];
  type.name = name;
  if (name === 'pointer')
    return; // there is no "pointer" type...
  ref.types[name].ffi_type = type;
});

// make `size_t` use the "ffi_type_pointer"
ref.types.size_t.ffi_type = bindings.FFI_TYPES.pointer;

// make `Utf8String` use "ffi_type_pointer"
const CString = ref.types.CString || ref.types.Utf8String;
CString.ffi_type = bindings.FFI_TYPES.pointer;

// make `Object` use the "ffi_type_pointer"
ref.types.Object.ffi_type = bindings.FFI_TYPES.pointer;

// libffi is weird when it comes to long data types (defaults to 64-bit),
// so we emulate here, since some platforms have 32-bit longs and some
// platforms have 64-bit longs.
switch (ref.sizeof.long) {
  case 4:
    ref.types.ulong.ffi_type = bindings.FFI_TYPES.uint32;
    ref.types.long.ffi_type = bindings.FFI_TYPES.int32;
    break;
  case 8:
    ref.types.ulong.ffi_type = bindings.FFI_TYPES.uint64;
    ref.types.long.ffi_type = bindings.FFI_TYPES.int64;
    break;
  default:
    throw new Error('unsupported "long" size: ' + ref.sizeof.long);
}
github Bannerets / tdl / src / tdlib-ffi.js View on Github external
const buildQuery = (query: Object) => {
  const buffer = Buffer.from(JSON.stringify(query) + '\0', 'utf-8')
  // $FlowOff
  buffer.type = ref.types.CString
  return buffer
}
github ibm-messaging / mq-mqi-nodejs / lib / mqitypes.js View on Github external
Contributors:
     Mark Taylor - Initial Contribution
*/

/*
 * This module defines basic MQI types that will be used when
 * constructing the buffer structures to be passed across the FFI.
 */

// Import packages for handling structures
var ref        = require('ref-napi');
var StructType = require('ref-struct-di')(ref);
var ArrayType  = require('ref-array-di')(ref);

exports.CHAR    = ref.types.char;
exports.BYTE    = ref.types.uchar;
exports.CHAR4   = ArrayType(exports.CHAR,4);
exports.CHAR8   = ArrayType(exports.CHAR,8);
exports.CHAR12  = ArrayType(exports.CHAR,12);
exports.CHAR20  = ArrayType(exports.CHAR,20);
exports.CHAR28  = ArrayType(exports.CHAR,28);
exports.CHAR32  = ArrayType(exports.CHAR,32);
exports.CHAR40  = ArrayType(exports.CHAR,40);
exports.CHAR48  = ArrayType(exports.CHAR,48);
exports.CHAR64  = ArrayType(exports.CHAR,64);
exports.CHAR128 = ArrayType(exports.CHAR,128);
exports.CHAR256 = ArrayType(exports.CHAR,256);
exports.CHAR264 = ArrayType(exports.CHAR,264);

exports.BYTE4   = ArrayType(exports.BYTE,4);
exports.BYTE8   = ArrayType(exports.BYTE,8);
github node-ffi-napi / node-ffi-napi / lib / ffi.js View on Github external
CString.ffi_type = bindings.FFI_TYPES.pointer;

// make `Object` use the "ffi_type_pointer"
ref.types.Object.ffi_type = bindings.FFI_TYPES.pointer;

// libffi is weird when it comes to long data types (defaults to 64-bit),
// so we emulate here, since some platforms have 32-bit longs and some
// platforms have 64-bit longs.
switch (ref.sizeof.long) {
  case 4:
    ref.types.ulong.ffi_type = bindings.FFI_TYPES.uint32;
    ref.types.long.ffi_type = bindings.FFI_TYPES.int32;
    break;
  case 8:
    ref.types.ulong.ffi_type = bindings.FFI_TYPES.uint64;
    ref.types.long.ffi_type = bindings.FFI_TYPES.int64;
    break;
  default:
    throw new Error('unsupported "long" size: ' + ref.sizeof.long);
}

/**
 * Alias the "ref" types onto ffi's exports, for convenience...
 */

exports.types = ref.types;

// Include our other modules
exports.version = bindings.version;
exports.CIF = require('./cif');
exports.CIF_var = require('./cif_var');
exports.Function = require('./function');
github node-ffi-napi / node-ffi-napi / lib / ffi.js View on Github external
const CString = ref.types.CString || ref.types.Utf8String;
CString.ffi_type = bindings.FFI_TYPES.pointer;

// make `Object` use the "ffi_type_pointer"
ref.types.Object.ffi_type = bindings.FFI_TYPES.pointer;

// libffi is weird when it comes to long data types (defaults to 64-bit),
// so we emulate here, since some platforms have 32-bit longs and some
// platforms have 64-bit longs.
switch (ref.sizeof.long) {
  case 4:
    ref.types.ulong.ffi_type = bindings.FFI_TYPES.uint32;
    ref.types.long.ffi_type = bindings.FFI_TYPES.int32;
    break;
  case 8:
    ref.types.ulong.ffi_type = bindings.FFI_TYPES.uint64;
    ref.types.long.ffi_type = bindings.FFI_TYPES.int64;
    break;
  default:
    throw new Error('unsupported "long" size: ' + ref.sizeof.long);
}

/**
 * Alias the "ref" types onto ffi's exports, for convenience...
 */

exports.types = ref.types;

// Include our other modules
exports.version = bindings.version;
exports.CIF = require('./cif');
exports.CIF_var = require('./cif_var');
github node-ffi-napi / node-ffi-napi / test / variadic.js View on Github external
it('should return the same Function instance when the same arguments are used', function () {
    var sprintfGen = ffi.VariadicForeignFunction(sprintfPtr, 'int', [ 'pointer', 'string' ]);

    var one = sprintfGen('int');
    var two = sprintfGen(ref.types.int);

    assert.strictEqual(one, two);
  });
});
github hertzg / node-net-keepalive / lib / index.js View on Github external
function getKeepAliveProbes(socket) {
  Assert.strictEqual(arguments.length, 1,
    'getKeepAliveProbes requires one arguments')
  Assert(_isSocket(socket),
    'getKeepAliveProbes expects an instance of socket as its first ' +
    'argument')

  const fd = _getSocketFD(socket)
    , cntVal = Ref.alloc(Ref.types.int)
    , cntValLn = Ref.alloc(Ref.types.int, cntVal.type.size)

  FFIBindings.getsockopt(fd, Constants.SOL_TCP,
    Constants.TCP_KEEPCNT, cntVal, cntValLn)

  return cntVal.deref()
}
github ibm-messaging / mq-mqi-nodejs / lib / mqod.js View on Github external
* _MQODffi_t is the definition directly matching the C structure
 * for the MQOD so it can be used in the FFI call to the MQI.
 * This is not meant to be used publicly.
 */
var _MQODffi_t = StructType({
  StrucId            : MQT.CHAR4       ,
  Version            : ref.types.int32 ,
  ObjectType         : ref.types.int32 ,
  ObjectName         : MQT.CHAR48      ,
  ObjectQMgrName     : MQT.CHAR48      ,
  DynamicQName       : MQT.CHAR48      ,
  AlternateUserId    : MQT.CHAR12      ,

  RecsPresent        : ref.types.int32 ,
  KnownDestCount     : ref.types.int32 ,
  UnknownDestCount   : ref.types.int32 ,
  InvalidDestCount   : ref.types.int32 ,
  ObjectRecOffset    : ref.types.int32 ,
  ResponseRecOffset  : ref.types.int32 ,

  ObjectRecPtr       : MQT.PTR         ,
  ResponseRecPtr     : MQT.PTR         ,

  AlternateSecurityId: MQT.CHAR40      ,
  ResolvedQName      : MQT.CHAR48      ,
  ResolvedQMgrName   : MQT.CHAR48      ,

  ObjectString      : MQT.CHARV        ,
  SelectionString   : MQT.CHARV        ,
  ResObjectString   : MQT.CHARV        ,
  ResolvedType      : ref.types.int32