* Provides a friendly abstraction/API on-top of DynamicLibrary and * ForeignFunction.
(libfile, funcs, lib)
| 32 | */ |
| 33 | |
| 34 | function Library (libfile, funcs, lib) { |
| 35 | debug('creating Library object for', libfile) |
| 36 | |
| 37 | if (libfile && libfile.indexOf(EXT) === -1) { |
| 38 | debug('appending library extension to library name', EXT) |
| 39 | libfile += EXT |
| 40 | } |
| 41 | |
| 42 | if (!lib) { |
| 43 | lib = {} |
| 44 | } |
| 45 | var dl = new DynamicLibrary(libfile || null, RTLD_NOW) |
| 46 | |
| 47 | Object.keys(funcs || {}).forEach(function (func) { |
| 48 | debug('defining function', func) |
| 49 | |
| 50 | var fptr = dl.get(func) |
| 51 | , info = funcs[func] |
| 52 | |
| 53 | if (fptr.isNull()) { |
| 54 | throw new Error('Library: "' + libfile |
| 55 | + '" returned NULL function pointer for "' + func + '"') |
| 56 | } |
| 57 | |
| 58 | var resultType = info[0] |
| 59 | , paramTypes = info[1] |
| 60 | , fopts = info[2] |
| 61 | , abi = fopts && fopts.abi |
| 62 | , async = fopts && fopts.async |
| 63 | , varargs = fopts && fopts.varargs |
| 64 | |
| 65 | if (varargs) { |
| 66 | lib[func] = VariadicForeignFunction(fptr, resultType, paramTypes, abi) |
| 67 | } else { |
| 68 | var ff = ForeignFunction(fptr, resultType, paramTypes, abi) |
| 69 | lib[func] = async ? ff.async : ff |
| 70 | } |
| 71 | }) |
| 72 | |
| 73 | return lib |
| 74 | } |
| 75 | module.exports = Library |
nothing calls this directly
no test coverage detected