Permalink
Browse files

don't export the C++ bindings directly

We're going to selectively export only the public API at this point...
  • Loading branch information...
1 parent 75b2c01 commit e6aadc83269bb7fe3636da2eec412f069adb78ad @TooTallNate TooTallNate committed May 30, 2012
Showing with 59 additions and 41 deletions.
  1. +4 −3 lib/_foreign_function.js
  2. +2 −0 lib/bindings.js
  3. +2 −1 lib/callback.js
  4. +8 −6 lib/cif.js
  5. +8 −6 lib/cif_var.js
  6. +5 −4 lib/dynamic_library.js
  7. +25 −17 lib/ffi.js
  8. +2 −1 lib/foreign_function_var.js
  9. +2 −2 test/cif.js
  10. +1 −1 test/objc.js
@@ -3,8 +3,9 @@ var ffi = require('./ffi')
, assert = require('assert')
, debug = require('debug')('ffi:ForeignFunction')
, ref = require('ref')
+ , bindings = require('./bindings')
, POINTER_SIZE = ref.sizeof.pointer
- , FFI_ARG_SIZE = ffi.Bindings.FFI_ARG_SIZE
+ , FFI_ARG_SIZE = bindings.FFI_ARG_SIZE
function ForeignFunction (cif, funcPtr, returnType, argTypes) {
@@ -53,7 +54,7 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
}
// invoke the `ffi_call()` function
- ffi.Bindings.ffi_call(cif, funcPtr, result, argsList)
+ bindings.ffi_call(cif, funcPtr, result, argsList)
result.type = returnType
return result.deref()
@@ -98,7 +99,7 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
}
// invoke the `ffi_call()` function asynchronously
- ffi.Bindings.ffi_call_async(cif, funcPtr, result, argsList, function (err) {
+ bindings.ffi_call_async(cif, funcPtr, result, argsList, function (err) {
if (err) {
callback(err)
} else {
View
@@ -0,0 +1,2 @@
+
+module.exports = require('bindings')('ffi_bindings.node')
View
@@ -3,6 +3,7 @@ var ffi = require('./ffi')
, assert = require('assert')
, ref = require('ref')
, debug = require('debug')('ffi:Callback')
+ , _Callback = require('./bindings').Callback
, POINTER_SIZE = ref.sizeof.pointer
/**
@@ -33,7 +34,7 @@ function Callback (retType, types, abi, func) {
var cif = ffi.CIF(retType, types, abi)
var argc = types.length
- return ffi.Bindings.Callback(cif, retType.size, argc, function (retval, params) {
+ return _Callback(cif, retType.size, argc, function (retval, params) {
debug('Callback function being invoked')
var args = []
View
@@ -3,13 +3,15 @@ var ffi = require('./ffi')
, assert = require('assert')
, debug = require('debug')('ffi:cif')
, ref = require('ref')
+ , bindings = require('./bindings')
, POINTER_SIZE = ref.sizeof.pointer
- , FFI_CIF_SIZE = ffi.Bindings.FFI_CIF_SIZE
- , FFI_DEFAULT_ABI = ffi.Bindings.FFI_DEFAULT_ABI
+ , ffi_prep_cif = bindings.ffi_prep_cif
+ , FFI_CIF_SIZE = bindings.FFI_CIF_SIZE
+ , FFI_DEFAULT_ABI = bindings.FFI_DEFAULT_ABI
// status codes
- , FFI_OK = ffi.Bindings.FFI_OK
- , FFI_BAD_TYPEDEF = ffi.Bindings.FFI_BAD_TYPEDEF
- , FFI_BAD_ABI = ffi.Bindings.FFI_BAD_ABI
+ , FFI_OK = bindings.FFI_OK
+ , FFI_BAD_TYPEDEF = bindings.FFI_BAD_TYPEDEF
+ , FFI_BAD_ABI = bindings.FFI_BAD_ABI
/**
* JS wrapper for the `ffi_prep_cif` function.
@@ -46,7 +48,7 @@ function CIF (rtype, types, abi) {
abi = FFI_DEFAULT_ABI
}
- var status = ffi.Bindings.ffi_prep_cif(cif, numArgs, _rtypeptr, _argtypesptr, abi)
+ var status = ffi_prep_cif(cif, numArgs, _rtypeptr, _argtypesptr, abi)
if (status !== FFI_OK) {
switch (status) {
View
@@ -3,13 +3,15 @@ var ffi = require('./ffi')
, assert = require('assert')
, debug = require('debug')('ffi:cif_var')
, ref = require('ref')
+ , bindings = require('./bindings')
, POINTER_SIZE = ref.sizeof.pointer
- , FFI_CIF_SIZE = ffi.Bindings.FFI_CIF_SIZE
- , FFI_DEFAULT_ABI = ffi.Bindings.FFI_DEFAULT_ABI
+ , ffi_prep_cif_var = bindings.ffi_prep_cif_var
+ , FFI_CIF_SIZE = bindings.FFI_CIF_SIZE
+ , FFI_DEFAULT_ABI = bindings.FFI_DEFAULT_ABI
// status codes
- , FFI_OK = ffi.Bindings.FFI_OK
- , FFI_BAD_TYPEDEF = ffi.Bindings.FFI_BAD_TYPEDEF
- , FFI_BAD_ABI = ffi.Bindings.FFI_BAD_ABI
+ , FFI_OK = bindings.FFI_OK
+ , FFI_BAD_TYPEDEF = bindings.FFI_BAD_TYPEDEF
+ , FFI_BAD_ABI = bindings.FFI_BAD_ABI
/**
* JS wrapper for the `ffi_prep_cif_var` function.
@@ -45,7 +47,7 @@ function CIF_var (rtype, types, numFixedArgs, abi) {
abi = FFI_DEFAULT_ABI
}
- var status = ffi.Bindings.ffi_prep_cif_var(cif, numFixedArgs, numTotalArgs, _rtypeptr, _argtypesptr, abi)
+ var status = ffi_prep_cif_var(cif, numFixedArgs, numTotalArgs, _rtypeptr, _argtypesptr, abi)
if (status !== FFI_OK) {
switch (status) {
@@ -1,17 +1,18 @@
var ffi = require('./ffi')
, assert = require('assert')
, debug = require('debug')('ffi:DynamicLibrary')
+ , bindings = require('./bindings')
, ref = require('ref')
, read = require('fs').readFileSync
var int = ref.types.int
, charPtr = ref.refType(ref.types.char)
, voidPtr = ref.refType(ref.types.void)
-var dlopen = ffi.ForeignFunction(ffi.Bindings.StaticFunctions.dlopen, voidPtr, [ charPtr, int ])
- , dlclose = ffi.ForeignFunction(ffi.Bindings.StaticFunctions.dlclose, int, [ voidPtr ])
- , dlsym = ffi.ForeignFunction(ffi.Bindings.StaticFunctions.dlsym, voidPtr, [ voidPtr, charPtr ])
- , dlerror = ffi.ForeignFunction(ffi.Bindings.StaticFunctions.dlerror, charPtr, [ ])
+var dlopen = ffi.ForeignFunction(bindings.StaticFunctions.dlopen, voidPtr, [ charPtr, int ])
+ , dlclose = ffi.ForeignFunction(bindings.StaticFunctions.dlclose, int, [ voidPtr ])
+ , dlsym = ffi.ForeignFunction(bindings.StaticFunctions.dlsym, voidPtr, [ voidPtr, charPtr ])
+ , dlerror = ffi.ForeignFunction(bindings.StaticFunctions.dlerror, charPtr, [ ])
/**
* `DynamicLibrary` loads and fetches function pointers for dynamic libraries
View
@@ -3,11 +3,8 @@ var ref = require('ref')
var assert = require('assert')
var debug = require('debug')('ffi:ffi')
var Struct = require('ref-struct')
-
-var ffi = module.exports
-
-ffi.Bindings = require('bindings')('ffi_bindings.node')
-
+var bindings = require('./bindings')
+var ffi = exports
/**
* The extension to use on libraries.
@@ -29,35 +26,46 @@ Object.defineProperty(ffi, 'LIB_EXT', {
}[process.platform]
})
+;['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)
+ }
+ Object.defineProperty(ffi, prop, Object.getOwnPropertyDescriptor(bindings, prop))
+})
+
/**
* Set the `ffi_type` property on the built-in types.
*/
-Object.keys(ffi.Bindings.FFI_TYPES).forEach(function (name) {
- if (name === 'pointer') return
- var ffi_type = ffi.Bindings.FFI_TYPES[name]
- ref.types[name].ffi_type = ffi_type
+Object.keys(bindings.FFI_TYPES).forEach(function (name) {
+ var 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 = ref.cloneType(ref.types.size_t)
-ref.types.size_t.ffi_type = ffi.Bindings.FFI_TYPES.pointer
+ref.types.size_t.ffi_type = bindings.FFI_TYPES.pointer
// make `Utf8String` use "ffi_type_pointer"
-ref.types.Utf8String.ffi_type = ffi.Bindings.FFI_TYPES.pointer
+ref.types.Utf8String.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 = ffi.Bindings.FFI_TYPES.uint32
- ref.types.long.ffi_type = ffi.Bindings.FFI_TYPES.int32
+ 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 = ffi.Bindings.FFI_TYPES.uint64
- ref.types.long.ffi_type = ffi.Bindings.FFI_TYPES.int64
+ 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)
@@ -76,7 +84,7 @@ ffi.ffiType = function ffiType (type) {
if (type.indirection === 1) {
rtn = type.ffi_type
} else {
- rtn = ffi.Bindings.FFI_TYPES.pointer
+ rtn = bindings.FFI_TYPES.pointer
}
if (!rtn && type.fields) {
@@ -128,4 +136,4 @@ ffi.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.FFI_TYPE))
ffi.FFI_TYPE.defineProperty('elements', ffi_type_ptr_array)
-assert.equal(ffi.Bindings.FFI_TYPE_SIZE, ffi.FFI_TYPE.size)
+assert.equal(bindings.FFI_TYPE_SIZE, ffi.FFI_TYPE.size)
@@ -4,8 +4,9 @@ var ffi = require('./ffi')
, assert = require('assert')
, debug = require('debug')('ffi:ForeignFunction')
, ref = require('ref')
+ , bindings = require('./bindings')
, POINTER_SIZE = ref.sizeof.pointer
- , FFI_ARG_SIZE = ffi.Bindings.FFI_ARG_SIZE
+ , FFI_ARG_SIZE = bindings.FFI_ARG_SIZE
/**
* For when you want to call to a C function with variable amount of arguments.
View
@@ -25,7 +25,7 @@ describe('ffi_cif', function () {
}, function (err) {
assert(err.message.indexOf('FFI_BAD_TYPEDEF') !== -1)
assert.equal('FFI_BAD_TYPEDEF', err.code)
- assert.equal(ffi.Bindings.FFI_BAD_TYPEDEF, err.errno)
+ assert.equal(ffi.FFI_BAD_TYPEDEF, err.errno)
return true
})
})
@@ -36,7 +36,7 @@ describe('ffi_cif', function () {
}, function (err) {
assert(err.message.indexOf('FFI_BAD_ABI') !== -1)
assert.equal('FFI_BAD_ABI', err.code)
- assert.equal(ffi.Bindings.FFI_BAD_ABI, err.errno)
+ assert.equal(ffi.FFI_BAD_ABI, err.errno)
return true
})
})
View
@@ -11,7 +11,7 @@ var id = voidPtr
, SEL = voidPtr
, Class = voidPtr
-if (ffi.Bindings.HAS_OBJC) {
+if (ffi.HAS_OBJC) {
describe('@try / @catch', function () {

0 comments on commit e6aadc8

Please sign in to comment.