Skip to content

Commit

Permalink
Merge 6cf7bf5 into cd8c58b
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Nov 28, 2020
2 parents cd8c58b + 6cf7bf5 commit ddd4995
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/dynamic_library.js
Expand Up @@ -38,6 +38,7 @@ function DynamicLibrary (path, mode) {
mode = DynamicLibrary.FLAGS.RTLD_LAZY;
}

this._path = path;
this._handle = dlopen(path, mode);
assert(Buffer.isBuffer(this._handle), 'expected a Buffer instance to be returned from `dlopen()`');

Expand Down Expand Up @@ -120,8 +121,14 @@ DynamicLibrary.prototype.get = function (symbol) {
/**
* Returns the result of the dlerror() system function
*/

DynamicLibrary.prototype.error = function error () {
debug('dlerror()');
return dlerror();
}

/**
* Returns the path originally passed to the constructor
*/
DynamicLibrary.prototype.path = function error () {
return this._path;
}
11 changes: 8 additions & 3 deletions lib/library.js
Expand Up @@ -34,15 +34,20 @@ const EXT = Library.EXT = {
function Library (libfile, funcs, lib) {
debug('creating Library object for', libfile);

if (libfile && libfile.indexOf(EXT) === -1) {
if (libfile && typeof libfile === 'string' && libfile.indexOf(EXT) === -1) {
debug('appending library extension to library name', EXT);
libfile += EXT;
}

if (!lib) {
lib = {};
}
const dl = new DynamicLibrary(libfile || null, RTLD_NOW);
let dl;
if (typeof libfile === 'string' || !libfile) {
dl = new DynamicLibrary(libfile || null, RTLD_NOW);
} else {
dl = libfile;
}

Object.keys(funcs || {}).forEach(function (func) {
debug('defining function', func);
Expand All @@ -51,7 +56,7 @@ function Library (libfile, funcs, lib) {
const info = funcs[func];

if (fptr.isNull()) {
throw new Error('Library: "' + libfile
throw new Error('Library: "' + dl.path()
+ '" returned NULL function pointer for "' + func + '"');
}

Expand Down
10 changes: 10 additions & 0 deletions test/library.js
Expand Up @@ -3,6 +3,7 @@ const assert = require('assert');
const ref = require('ref-napi');
const Struct = require('ref-struct-di')(ref);
const ffi = require('../');
const DynamicLibrary = ffi.DynamicLibrary;
const Library = ffi.Library;

describe('Library', function () {
Expand Down Expand Up @@ -52,6 +53,15 @@ describe('Library', function () {
assert(libm.ceil(1.1) === 2);
})

it('should accept a DynamicLibrary instance as the first argument', function () {
const lib = process.platform == 'win32' ? 'msvcrt' : 'libm';
const libm = new Library(new DynamicLibrary(lib + ffi.LIB_EXT, DynamicLibrary.FLAGS.RTLD_NOW), {
'ceil': [ 'double', [ 'double' ] ]
});
assert(typeof libm.ceil === 'function');
assert(libm.ceil(1.1) === 2);
})

it('should accept a lib name with file extension', function() {
const lib = process.platform == 'win32'
? 'msvcrt.dll'
Expand Down

0 comments on commit ddd4995

Please sign in to comment.