Permalink
Browse files

removed circular module references

  • Loading branch information...
1 parent a22de4f commit 466c543ecc40cddacd6e356b61231b850183d5cc @justinfreitag justinfreitag committed Jun 27, 2012
@@ -1,6 +1,5 @@
-var ffi = require('./ffi')
- , assert = require('assert')
+var assert = require('assert')
, debug = require('debug')('ffi:_ForeignFunction')
, ref = require('ref')
, bindings = require('./bindings')
View
@@ -1,5 +1,5 @@
-var ffi = require('./ffi')
+var CIF = require('./cif')
, assert = require('assert')
, ref = require('ref')
, debug = require('debug')('ffi:Callback')
@@ -31,7 +31,7 @@ function Callback (retType, types, abi, func) {
types = types.map(ref.coerceType)
// create the `ffi_cif *` instance
- var cif = ffi.CIF(retType, types, abi)
+ var cif = CIF(retType, types, abi)
var argc = types.length
return _Callback(cif, retType.size, argc, function (retval, params) {
View
@@ -1,5 +1,5 @@
-var ffi = require('./ffi')
+var Types = require('./types')
, 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 = ffi.ffiType(rtype)
+ var _rtypeptr = Types.ffiType(rtype)
for (var i = 0; i < numArgs; i++) {
var type = types[i]
- var ffiType = ffi.ffiType(type)
+ var ffiType = Types.ffiType(type)
_argtypesptr.writePointer(ffiType, i * POINTER_SIZE)
}
View
@@ -1,5 +1,5 @@
-var ffi = require('./ffi')
+var Types = require('./types')
, 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 = ffi.ffiType(rtype)
+ var _rtypeptr = Types.ffiType(rtype)
for (var i = 0; i < numTotalArgs; i++) {
- var ffiType = ffi.ffiType(types[i])
+ var ffiType = Types.ffiType(types[i])
_argtypesptr.writePointer(ffiType, i * POINTER_SIZE)
}
@@ -1,4 +1,4 @@
-var ffi = require('./ffi')
+var ForeignFunction = require('./foreign_function')
, assert = require('assert')
, debug = require('debug')('ffi:DynamicLibrary')
, bindings = require('./bindings')
@@ -9,10 +9,10 @@ var int = ref.types.int
, charPtr = ref.refType(ref.types.char)
, voidPtr = ref.refType(ref.types.void)
-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, [ ])
+var dlopen = ForeignFunction(bindings.StaticFunctions.dlopen, voidPtr, [ charPtr, int ])
+ , dlclose = ForeignFunction(bindings.StaticFunctions.dlclose, int, [ voidPtr ])
+ , dlsym = ForeignFunction(bindings.StaticFunctions.dlsym, voidPtr, [ voidPtr, charPtr ])
+ , dlerror = ForeignFunction(bindings.StaticFunctions.dlerror, charPtr, [ ])
/**
* `DynamicLibrary` loads and fetches function pointers for dynamic libraries
View
@@ -6,20 +6,21 @@
* On Windows it's a method execution called `_errno`.
*/
-var ffi = require('./ffi')
+var DynamicLibrary = require('./dynamic_library')
+ , ForeignFunction = require('./foreign_function')
, ref = require('ref')
, errnoPtr = null
, int = ref.types.int
, intPtr = ref.refType(int)
if (process.platform == 'darwin' || process.platform == 'mac') {
- var __error = ffi.DynamicLibrary().get('__error')
- errnoPtr = ffi.ForeignFunction(__error, intPtr, [])
+ var __error = DynamicLibrary().get('__error')
+ errnoPtr = ForeignFunction(__error, intPtr, [])
} else if (process.platform == 'win32') {
- var _errno = ffi.DynamicLibrary('msvcrt.dll').get('_errno')
- errnoPtr = ffi.ForeignFunction(_errno, intPtr, [])
+ var _errno = DynamicLibrary('msvcrt.dll').get('_errno')
+ errnoPtr = ForeignFunction(_errno, intPtr, [])
} else { // linux, sunos, etc.
- var errnoGlobal = ffi.DynamicLibrary().get('errno').reinterpret(int.size)
+ var errnoGlobal = DynamicLibrary().get('errno').reinterpret(int.size)
errnoPtr = function () { return errnoGlobal }
// set the errno type
errnoGlobal.type = int
@@ -1,5 +1,5 @@
-var ffi = require('./ffi')
+var CIF = require('./cif')
, _ForeignFunction = require('./_foreign_function')
, debug = require('debug')('ffi:ForeignFunction')
, assert = require('assert')
@@ -26,7 +26,7 @@ function ForeignFunction (funcPtr, returnType, argTypes, abi) {
argTypes = argTypes.map(ref.coerceType)
// create the `ffi_cif *` instance
- var cif = ffi.CIF(returnType, argTypes, abi)
+ var cif = CIF(returnType, argTypes, abi)
// create and return the JS proxy function
return _ForeignFunction(cif, funcPtr, returnType, argTypes)
@@ -1,5 +1,6 @@
-var ffi = require('./ffi')
+var CIF_var = require('./cif_var')
+ , Types = require('./types')
, _ForeignFunction = require('./_foreign_function')
, assert = require('assert')
, debug = require('debug')('ffi:VariadicForeignFunction')
@@ -36,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 = ffi.ffiType(type)
+ var ffi_type = Types.ffiType(type)
assert(ffi_type.name)
return ffi_type.name
})
@@ -55,14 +56,14 @@ function VariadicForeignFunction (funcPtr, returnType, fixedArgTypes, abi) {
var type = ref.coerceType(arguments[i])
argTypes.push(type)
- var ffi_type = ffi.ffiType(type)
+ var ffi_type = Types.ffiType(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 = ffi.ffiType(rtnType).name
+ var rtnName = Types.ffiType(rtnType).name
assert(rtnName)
// first let's generate the key and see if we got a cache-hit
@@ -74,7 +75,7 @@ function VariadicForeignFunction (funcPtr, returnType, fixedArgTypes, abi) {
} else {
// create the `ffi_cif *` instance
debug('creating the variadic ffi_cif instance for key:', key)
- var cif = ffi.CIF_var(returnType, argTypes, numFixedArgs, abi)
+ var cif = CIF_var(returnType, argTypes, numFixedArgs, abi)
func = cache[key] = _ForeignFunction(cif, funcPtr, rtnType, argTypes)
}
return func
View
@@ -1,8 +1,22 @@
+/**
+ * The extension to use on libraries.
+ * i.e. libm -> libm.so on linux
+ */
-var ffi = require('./ffi')
+var DynamicLibrary = require('./dynamic_library')
+ , ForeignFunction = require('./foreign_function')
+ , VariadicForeignFunction = require('./foreign_function_var')
, debug = require('debug')('ffi:Library')
- , EXT = ffi.LIB_EXT
- , RTLD_NOW = ffi.DynamicLibrary.FLAGS.RTLD_NOW
+ , RTLD_NOW = DynamicLibrary.FLAGS.RTLD_NOW
+ , EXT = {
+ 'linux': '.so'
+ , 'linux2': '.so'
+ , 'sunos': '.so'
+ , 'solaris':'.so'
+ , 'darwin': '.dylib'
+ , 'mac': '.dylib'
+ , 'win32': '.dll'
+ }[process.platform]
/**
* Provides a friendly abstraction/API on-top of DynamicLibrary and
@@ -18,7 +32,7 @@ function Library (libfile, funcs) {
}
var lib = {}
- var dl = new ffi.DynamicLibrary(libfile || null, RTLD_NOW)
+ var dl = new DynamicLibrary(libfile || null, RTLD_NOW)
Object.keys(funcs || {}).forEach(function (func) {
debug('defining function', func)
@@ -39,9 +53,9 @@ function Library (libfile, funcs) {
, varargs = fopts && fopts.varargs
if (varargs) {
- lib[func] = ffi.VariadicForeignFunction(fptr, resultType, paramTypes, abi)
+ lib[func] = VariadicForeignFunction(fptr, resultType, paramTypes, abi)
} else {
- var ff = ffi.ForeignFunction(fptr, resultType, paramTypes, abi)
+ var ff = ForeignFunction(fptr, resultType, paramTypes, abi)
lib[func] = async ? ff.async : ff
}
})
@@ -7,41 +7,6 @@ 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.
*/
@@ -127,17 +92,6 @@ ffi.ffiType = function ffiType (type) {
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.
* This struct type is used internally to define custom struct rtn/arg types.
View
@@ -1,35 +1,36 @@
var assert = require('assert')
, ref = require('ref')
- , ffi = require('../')
+ , Callback = require('../lib/callback')
+ , ForeignFunction = require('../lib/foreign_function')
, int = ref.types.int
describe('Callback', function () {
afterEach(gc)
it('should create a C function pointer from a JS function', function () {
- var callback = ffi.Callback('void', [ ], function (val) { })
+ var callback = Callback('void', [ ], function (val) { })
assert(Buffer.isBuffer(callback))
})
it('should be invokable by an ffi\'d ForeignFunction', function () {
- var funcPtr = ffi.Callback(int, [ int ], Math.abs)
- var func = ffi.ForeignFunction(funcPtr, int, [ int ])
+ var funcPtr = Callback(int, [ int ], Math.abs)
+ var func = ForeignFunction(funcPtr, int, [ int ])
assert.equal(1234, func(-1234))
})
it('should work with a "void" return type', function () {
- var funcPtr = ffi.Callback('void', [ ], function (val) { })
- var func = ffi.ForeignFunction(funcPtr, 'void', [ ])
+ var funcPtr = Callback('void', [ ], function (val) { })
+ var func = ForeignFunction(funcPtr, 'void', [ ])
assert.strictEqual(null, func())
})
describe('async', function () {
it('should be invokable asynchronously by an ffi\'d ForeignFunction', function () {
- var funcPtr = ffi.Callback(int, [ int ], Math.abs)
- var func = ffi.ForeignFunction(funcPtr, int, [ int ])
+ var funcPtr = Callback(int, [ int ], Math.abs)
+ var func = ForeignFunction(funcPtr, int, [ int ])
func.async(-9999, function (err, res) {
assert.equal(null, err)
assert.equal(9999, res)
View
@@ -1,42 +1,44 @@
var assert = require('assert')
, ref = require('ref')
- , ffi = require('../')
+ , CIF = require('../lib/cif')
+ , Types = require('../lib/types')
+ , bindings = require('../lib/bindings')
describe('ffi_cif', function () {
afterEach(gc)
it('should return a Buffer representing the `ffi_cif` instance', function () {
- var cif = ffi.CIF(ref.types.void, [ ])
+ var cif = CIF(ref.types.void, [ ])
assert(Buffer.isBuffer(cif))
})
it('should throw an Error when given an invalid "type"', function () {
- var ffi_type = new ffi.FFI_TYPE
+ var ffi_type = new Types.FFI_TYPE
ffi_type.size = 0
ffi_type.alignment = 0
ffi_type.type = 0
ffi_type.elements = ref.NULL
var bad_type = { size: 1, indirection: 1, ffi_type: ffi_type.ref() }
assert.throws(function () {
- ffi.CIF(bad_type, [])
+ CIF(bad_type, [])
}, function (err) {
assert(err.message.indexOf('FFI_BAD_TYPEDEF') !== -1)
assert.equal('FFI_BAD_TYPEDEF', err.code)
- assert.equal(ffi.FFI_BAD_TYPEDEF, err.errno)
+ assert.equal(bindings.FFI_BAD_TYPEDEF, err.errno)
return true
})
})
it('should throw an Error when given an invalid ABI argument', function () {
assert.throws(function () {
- ffi.CIF(ref.types.void, [], -1)
+ CIF(ref.types.void, [], -1)
}, function (err) {
assert(err.message.indexOf('FFI_BAD_ABI') !== -1)
assert.equal('FFI_BAD_ABI', err.code)
- assert.equal(ffi.FFI_BAD_ABI, err.errno)
+ assert.equal(bindings.FFI_BAD_ABI, err.errno)
return true
})
})
View
@@ -1,8 +1,8 @@
var assert = require('assert')
, ref = require('ref')
- , ffi = require('../')
- , errno = ffi.errno
+ , errno = require('../lib/errno')
+ , Library = require('../lib/library')
, charPtr = ref.refType(ref.types.char)
describe('errno()', function () {
@@ -15,7 +15,7 @@ describe('errno()', function () {
it('should set the errno with out-of-range "strtoul" value', function () {
var lib = process.platform == 'win32' ? 'msvcrt' : 'libc'
- var strtoul = new ffi.Library(lib, {
+ var strtoul = new Library(lib, {
'strtoul': [ 'ulong', [ 'string', charPtr, 'int' ] ]
}).strtoul
var before = errno()
Oops, something went wrong.

0 comments on commit 466c543

Please sign in to comment.