How to use ref-napi - 10 common examples

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 ibm-messaging / mq-mqi-nodejs / lib / mqi.js View on Github external
function put1Internal() {

    if (isLocked(jsQueueManager._hConn)) {
      return setImmediate(put1Internal);
    } else {
      lock(jsQueueManager._hConn);
    }

    var mqRc = ref.alloc(MQT.LONG);
    var mqCc = ref.alloc(MQT.LONG);

    var mqMd = MQMD._copyMDtoC(jsmd);
    var mqOd = MQOD._copyODtoC(jsod);
    var mqPmo = MQPMO._copyPMOtoC(jspmo);

    // If it's a string, then we need to create a Buffer from it.
    // We also set the MQMD Format
    // TODO: Are there any string codepage conversions we should look at here?
    if (buf) {
      if (typeof buf == "string") {
        buf = Buffer.from(buf);
        u.setMQIString(mqMd.Format,"MQSTR");
      } else {
        checkParam(buf,Buffer,'Buffer');
      }
    }
github ibm-messaging / mq-mqi-nodejs / lib / mqi.js View on Github external
function putInternal() {

    if (isLocked(jsObject._mqQueueManager._hConn)) {
      return setImmediate(putInternal);
    } else {
      lock(jsObject._mqQueueManager._hConn);
    }

    var mqRc = ref.alloc(MQT.LONG);
    var mqCc = ref.alloc(MQT.LONG);

    var mqMd = MQMD._copyMDtoC(jsmd);
    var mqPmo = MQPMO._copyPMOtoC(jspmo);

    // If it's a string, then we need to create a Buffer from it.
    // We also set the MQMD Format
    // TODO: Are there any string codepage conversions we should look at here?
    //       Do we need to set the CCSID in some way?
    if (buf) {
      if (typeof buf == "string") {
        buf = Buffer.from(buf);
        u.setMQIString(mqMd.Format,"MQSTR");
      } else {
        checkParam(buf,Buffer,'Buffer');
      }
    }
github ibm-messaging / mq-mqi-nodejs / lib / mqi.js View on Github external
function openInternal () {
    // check if hConn is already being used
    if (isLocked(jsQueueManager._hConn)) {
      return setImmediate(openInternal);
    } else {
      lock(jsQueueManager._hConn);
    }

    var mqHObj  = ref.alloc(MQT.HOBJ);
    var mqRc = ref.alloc(MQT.LONG);
    var mqCc = ref.alloc(MQT.LONG);
    var mqOd = MQOD._copyODtoC(jsod);
    var err;

    if (useAsync) {
      libmqm.MQOPEN.async(jsQueueManager._hConn,mqOd.ref(),jsOpenOptions,mqHObj,mqCc,mqRc, function(error,resp) {
        unlock(jsQueueManager._hConn);

        MQOD._copyODfromC(mqOd,jsod);

        var jsHObj = mqHObj.deref();
        var jsRc = mqRc.deref();
        var jsCc = mqCc.deref();

        var jsObject = new MQObject(jsHObj,jsQueueManager,jsod.ObjectName);
        err = new MQError(jsCc,jsRc,"OPEN");
github ibm-messaging / mq-mqi-nodejs / lib / mqi.js View on Github external
exports.Inq = function(jsObject,jsSelectors,cb) {
  checkParamCB(cb);
  checkParam(jsObject, MQObject, 'MQObject',true);
  checkArray(jsSelectors);

  var mqRc = ref.alloc(MQT.LONG);
  var mqCc = ref.alloc(MQT.LONG);
  var jsRc;
  var jsCc;
  var i;
  var charAttrLen = 0;
  var charLen;
  var badSelector = false;
  var selector;

  var mqSelectors = new mqLongArray(jsSelectors.length);
  for (i=0;i= MQC.MQIA_FIRST && selector <= MQC.MQIA_LAST) {
      // Do nothing.
    } else if (selector >= MQC.MQCA_FIRST && selector <= MQC.MQCA_LAST) {
github ibm-messaging / mq-mqi-nodejs / lib / mqi.js View on Github external
exports.SetMp = function(jsQueueManager,jsHMsg,jssmpo,name,jspd,value,cb) {
  checkParamCB(cb);
  checkParam(jsQueueManager, MQQueueManager, 'MQQueueManager');
  checkParam(jssmpo, MQMPO.MQSMPO, 'MQSMPO');
  checkParam(jspd  , MQMPO.MQPD  , 'MQPD');
  checkParam(name, 'string', 'string');

  var mqHMsg  = jsHMsg;
  var mqRc = ref.alloc(MQT.LONG);
  var mqCc = ref.alloc(MQT.LONG);
  var mqSmpo = MQMPO._copySMPOtoC(jssmpo);
  var mqPd   = MQMPO._copyPDtoC(jspd);

  var vsName = new MQT.CHARV();
  var ptr;
  var len;
  var type;

  u.defaultCharV(vsName);
  u.setMQICharV(vsName,name);

  // TODO: Handle all the possible MQTYPE values including FLOAT and
  //       different sized ints
  if (typeof value == 'string') {
    ptr =  Buffer.from(value,'utf8');
    len = value.length;
github sindresorhus / active-win / lib / windows.js View on Github external
/* eslint-disable new-cap */
'use strict';
const path = require('path');
const ffi = require('ffi-napi');
const wchar = require('ref-wchar-napi');
const ref = require('ref-napi');
const struct = require('ref-struct-napi');

// Create the struct required to save the window bounds
const Rect = struct({
	left: 'long',
	top: 'long',
	right: 'long',
	bottom: 'long'
});
const RectPointer = ref.refType(Rect);

// Required by QueryFullProcessImageName
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;

// Create FFI declarations for the C++ library and functions needed (User32.dll), using their "Unicode" (UTF-16) version
const user32 = new ffi.Library('User32.dll', {
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
	GetForegroundWindow: ['pointer', []],
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx
	GetWindowTextW: ['int', ['pointer', 'pointer', 'int']],
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633521(v=vs.85).aspx
	GetWindowTextLengthW: ['int', ['pointer']],
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
	GetWindowThreadProcessId: ['uint32', ['pointer', 'uint32 *']],
	// Get window bounds function
github node-ffi-napi / node-ffi-napi / lib / dynamic_library.js View on Github external
'use strict';
/**
 * Module dependencies.
 */

const ForeignFunction = require('./foreign_function');
const assert = require('assert');
const debug = require('debug')('ffi:DynamicLibrary');
const bindings = require('./bindings');
const funcs = bindings.StaticFunctions;
const ref = require('ref-napi');
const read  = require('fs').readFileSync;

// typedefs
const int = ref.types.int;
const voidPtr = ref.refType(ref.types.void);

const dlopen  = ForeignFunction(funcs.dlopen,  voidPtr, [ 'string', int ]);
const dlclose = ForeignFunction(funcs.dlclose, int,     [ voidPtr ]);
const dlsym   = ForeignFunction(funcs.dlsym,   voidPtr, [ voidPtr, 'string' ]);
const dlerror = ForeignFunction(funcs.dlerror, 'string', [ ]);

/**
 * `DynamicLibrary` loads and fetches function pointers for dynamic libraries
 * (.so, .dylib, etc). After the libray's function pointer is acquired, then you
 * call `get(symbol)` to retreive a pointer to an exported symbol. You need to
 * call `get___()` on the pointer to dereference it into its actual value, or
 * turn the pointer into a callable function with `ForeignFunction`.
 */

function DynamicLibrary (path, mode) {
  if (!(this instanceof DynamicLibrary)) {
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);
}