XNSim/XNSimHtml/node_modules/ffi-napi/lib/foreign_function.js

40 lines
1.3 KiB
JavaScript
Raw Normal View History

2025-04-28 12:25:20 +08:00
'use strict';
/**
* Module dependencies.
*/
const CIF = require('./cif');
const _ForeignFunction = require('./_foreign_function');
const debug = require('debug')('ffi:ForeignFunction');
const assert = require('assert');
const ref = require('ref-napi');
/**
* Represents a foreign function in another library. Manages all of the aspects
* of function execution, including marshalling the data parameters for the
* function into native types and also unmarshalling the return from function
* execution.
*/
function ForeignFunction (funcPtr, returnType, argTypes, abi) {
debug('creating new ForeignFunction', funcPtr);
// check args
assert(Buffer.isBuffer(funcPtr), 'expected Buffer as first argument');
assert(!!returnType, 'expected a return "type" object as the second argument');
assert(Array.isArray(argTypes), 'expected Array of arg "type" objects as the third argument');
// normalize the "types" (they could be strings,
// so turn into real type instances)
returnType = ref.coerceType(returnType);
argTypes = argTypes.map(ref.coerceType);
// create the `ffi_cif *` instance
const cif = CIF(returnType, argTypes, abi);
// create and return the JS proxy function
return _ForeignFunction(cif, funcPtr, returnType, argTypes);
}
module.exports = ForeignFunction;