Skip to content
Browse files

changes

  • Loading branch information...
1 parent 466c543 commit bb432424ef23bebf6ec15078f36c4afad1f2420e @justinfreitag justinfreitag committed
Showing with 109 additions and 52 deletions.
  1. +3 −3 lib/cif.js
  2. +3 −3 lib/cif_var.js
  3. +44 −42 lib/{types.js → ffi.js}
  4. +4 −4 lib/foreign_function_var.js
  5. +55 −0 lib/type.js
View
6 lib/cif.js
@@ -1,5 +1,5 @@
-var Types = require('./types')
+var Type = require('./type')
, assert = require('assert')
, debug = require('debug')('ffi:cif')
, ref = require('ref')
@@ -30,11 +30,11 @@ function CIF (rtype, types, abi) {
var numArgs = types.length
var _argtypesptr = new Buffer(numArgs * POINTER_SIZE)
- var _rtypeptr = Types.ffiType(rtype)
+ var _rtypeptr = Type(rtype)
for (var i = 0; i < numArgs; i++) {
var type = types[i]
- var ffiType = Types.ffiType(type)
+ var ffiType = Type(type)
_argtypesptr.writePointer(ffiType, i * POINTER_SIZE)
}
View
6 lib/cif_var.js
@@ -1,5 +1,5 @@
-var Types = require('./types')
+var Type = require('./type')
, assert = require('assert')
, debug = require('debug')('ffi:cif_var')
, ref = require('ref')
@@ -31,10 +31,10 @@ function CIF_var (rtype, types, numFixedArgs, abi) {
var numTotalArgs = types.length
var _argtypesptr = new Buffer(numTotalArgs * POINTER_SIZE)
- var _rtypeptr = Types.ffiType(rtype)
+ var _rtypeptr = Type(rtype)
for (var i = 0; i < numTotalArgs; i++) {
- var ffiType = Types.ffiType(types[i])
+ var ffiType = Type(types[i])
_argtypesptr.writePointer(ffiType, i * POINTER_SIZE)
}
View
86 lib/types.js → lib/ffi.js
@@ -7,6 +7,41 @@ var bindings = require('./bindings')
var ffi = exports
/**
+ * The extension to use on libraries.
+ * i.e. libm -> libm.so on linux
+ */
+
+Object.defineProperty(ffi, 'LIB_EXT', {
+ configurable: true
+ , enumerable: true
+ , writable: false
+ , value: {
+ 'linux': '.so'
+ , 'linux2': '.so'
+ , 'sunos': '.so'
+ , 'solaris':'.so'
+ , 'darwin': '.dylib'
+ , 'mac': '.dylib'
+ , 'win32': '.dll'
+ }[process.platform]
+})
+
+/**
+ * Export some of the properties from the "bindings" file.
+ */
+
+;['HAS_OBJC', 'FFI_TYPES',
+, 'FFI_OK', 'FFI_BAD_TYPEDEF', 'FFI_BAD_ABI'
+, 'FFI_DEFAULT_ABI', 'FFI_LAST_ABI', 'FFI_SYSV', 'FFI_UNIX64', 'FFI_WIN64'
+, 'FFI_STDCALL', 'FFI_THISCALL', 'FFI_FASTCALL', 'FFI_MS_CDECL'].forEach(function (prop) {
+ if (!bindings.hasOwnProperty(prop)) {
+ return debug('skipping exporting of non-existant property: %s', prop)
+ }
+ var desc = Object.getOwnPropertyDescriptor(bindings, prop)
+ Object.defineProperty(ffi, prop, desc)
+})
+
+/**
* Set the `ffi_type` property on the built-in types.
*/
@@ -49,48 +84,15 @@ switch (ref.sizeof.long) {
ffi.types = ref.types
-
-/**
- * Returns a `ffi_type *` Buffer appropriate for the given "type".
- */
-
-ffi.ffiType = function ffiType (type) {
- debug('ffiType()', type.name || type)
- type = ref.coerceType(type)
- assert(type.indirection >= 1, 'invalid "type" given: ' + (type.name || type))
- var rtn
- if (type.indirection === 1) {
- rtn = type.ffi_type
- } else {
- rtn = bindings.FFI_TYPES.pointer
- }
-
- if (!rtn && type.fields) {
- // got a "ref-struct" type
- // need to create the `ffi_type` instance manually
- debug('creating an `ffi_type` for given "ref-struct" type')
- var props = type.fields
- , propNames = Object.keys(props)
- , numProps = propNames.length
- var t = new ffi.FFI_TYPE
- t.size = 0
- t.alignment = 0
- t.type = 13 // FFI_TYPE_STRUCT
- var elementsSize = ref.sizeof.pointer * (numProps + 1) // +1 because of the NULL terminator
- var elements = t.elements = new Buffer(elementsSize)
- for (var i = 0; i < numProps; i++) {
- var prop = props[propNames[i]]
- elements.writePointer(ffi.ffiType(prop.type), i * ref.sizeof.pointer)
- }
- // final NULL pointer to terminate the Array
- elements.writePointer(ref.NULL, i * ref.sizeof.pointer)
- // also set the `ffi_type` property to that it's cached for next time
- rtn = type.ffi_type = t.ref()
- }
- assert(rtn, 'Could not determine the `ffi_type` instance for type: ' + (type.name || type))
- debug('returning `ffi_type`', rtn.name)
- return rtn
-}
+// Include our other modules
+ffi.CIF = require('./cif')
+ffi.CIF_var = require('./cif_var')
+ffi.ForeignFunction = require('./foreign_function')
+ffi.VariadicForeignFunction = require('./foreign_function_var')
+ffi.DynamicLibrary = require('./dynamic_library')
+ffi.Library = require('./library')
+ffi.Callback = require('./callback')
+ffi.errno = require('./errno')
/**
* Define the `ffi_type` struct (see deps/libffi/include/ffi.h) for use in JS.
View
8 lib/foreign_function_var.js
@@ -1,6 +1,6 @@
var CIF_var = require('./cif_var')
- , Types = require('./types')
+ , Type = require('./type')
, _ForeignFunction = require('./_foreign_function')
, assert = require('assert')
, debug = require('debug')('ffi:VariadicForeignFunction')
@@ -37,7 +37,7 @@ function VariadicForeignFunction (funcPtr, returnType, fixedArgTypes, abi) {
// get the names of the fixed arg types
var fixedKey = fixedArgTypes.map(function (type) {
- var ffi_type = Types.ffiType(type)
+ var ffi_type = Type(type)
assert(ffi_type.name)
return ffi_type.name
})
@@ -56,14 +56,14 @@ function VariadicForeignFunction (funcPtr, returnType, fixedArgTypes, abi) {
var type = ref.coerceType(arguments[i])
argTypes.push(type)
- var ffi_type = Types.ffiType(type)
+ var ffi_type = Type(type)
assert(ffi_type.name)
key.push(ffi_type.name)
}
// now figure out the return type
var rtnType = ref.coerceType(variadic_function_generator.returnType)
- var rtnName = Types.ffiType(rtnType).name
+ var rtnName = Type(rtnType).name
assert(rtnName)
// first let's generate the key and see if we got a cache-hit
View
55 lib/type.js
@@ -0,0 +1,55 @@
+var ref = require('ref')
+var assert = require('assert')
+var debug = require('debug')('ffi:types')
+var Struct = require('ref-struct')
+var bindings = require('./bindings')
+
+FFI_TYPE = Struct()
+FFI_TYPE.defineProperty('size', ref.types.size_t)
+FFI_TYPE.defineProperty('alignment', ref.types.ushort)
+FFI_TYPE.defineProperty('type', ref.types.ushort)
+// this last prop is a C Array of `ffi_type *` elements, so this is `ffi_type **`
+var ffi_type_ptr_array = ref.refType(ref.refType(FFI_TYPE))
+FFI_TYPE.defineProperty('elements', ffi_type_ptr_array)
+assert.equal(bindings.FFI_TYPE_SIZE, FFI_TYPE.size)
+
+function Type (type) {
+ debug('Type()', type.name || type)
+ type = ref.coerceType(type)
+ assert(type.indirection >= 1, 'invalid "type" given: ' + (type.name || type))
+ var rtn
+ if (type.indirection === 1) {
+ rtn = type.ffi_type
+ } else {
+ rtn = bindings.FFI_TYPES.pointer
+ }
+
+ if (!rtn && type.fields) {
+ // got a "ref-struct" type
+ // need to create the `ffi_type` instance manually
+ debug('creating an `ffi_type` for given "ref-struct" type')
+ var props = type.fields
+ , propNames = Object.keys(props)
+ , numProps = propNames.length
+ var t = new FFI_TYPE
+ t.size = 0
+ t.alignment = 0
+ t.type = 13 // FFI_TYPE_STRUCT
+ var elementsSize = ref.sizeof.pointer * (numProps + 1) // +1 because of the NULL terminator
+ var elements = t.elements = new Buffer(elementsSize)
+ for (var i = 0; i < numProps; i++) {
+ var prop = props[propNames[i]]
+ elements.writePointer(Type(prop.type), i * ref.sizeof.pointer)
+ }
+ // final NULL pointer to terminate the Array
+ elements.writePointer(ref.NULL, i * ref.sizeof.pointer)
+ // also set the `ffi_type` property to that it's cached for next time
+ rtn = type.ffi_type = t.ref()
+ }
+ assert(rtn, 'Could not determine the `ffi_type` instance for type: ' + (type.name || type))
+ debug('returning `ffi_type`', rtn.name)
+ return rtn
+}
+
+module.exports = Type
+

0 comments on commit bb43242

Please sign in to comment.
Something went wrong with that request. Please try again.