Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

restored public api

  • Loading branch information...
commit 078008a2819991c71fcb763357e3f2fab37f0970 1 parent 765a7ac
@justinfreitag justinfreitag authored
View
BIN  lib/.cif_var.js.swp
Binary file not shown
View
2  lib/cif.js
@@ -1,5 +1,5 @@
-var Type = require('./type')
+var Type = require('./type').Type
, assert = require('assert')
, debug = require('debug')('ffi:cif')
, ref = require('ref')
View
2  lib/cif_var.js
@@ -1,5 +1,5 @@
-var Type = require('./type')
+var Type = require('./type').Type
, assert = require('assert')
, debug = require('debug')('ffi:cif_var')
, ref = require('ref')
View
15 lib/ffi.js
@@ -93,17 +93,6 @@ ffi.DynamicLibrary = require('./dynamic_library')
ffi.Library = require('./library')
ffi.Callback = require('./callback')
ffi.errno = require('./errno')
+ffi.ffiType = require('./type').Type
+ffi.FFI_TYPE = require('./type').FFI_TYPE
-/**
- * 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.
- */
-
-ffi.FFI_TYPE = Struct()
-ffi.FFI_TYPE.defineProperty('size', ref.types.size_t)
-ffi.FFI_TYPE.defineProperty('alignment', ref.types.ushort)
-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(bindings.FFI_TYPE_SIZE, ffi.FFI_TYPE.size)
View
2  lib/foreign_function_var.js
@@ -1,6 +1,6 @@
var CIF_var = require('./cif_var')
- , Type = require('./type')
+ , Type = require('./type').Type
, _ForeignFunction = require('./_foreign_function')
, assert = require('assert')
, debug = require('debug')('ffi:VariadicForeignFunction')
View
6 lib/type.js
@@ -4,7 +4,7 @@ var debug = require('debug')('ffi:types')
var Struct = require('ref-struct')
var bindings = require('./bindings')
-FFI_TYPE = Struct()
+exports.FFI_TYPE = 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)
@@ -13,7 +13,7 @@ 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) {
+exports.Type = function Type (type) {
debug('Type()', type.name || type)
type = ref.coerceType(type)
assert(type.indirection >= 1, 'invalid "type" given: ' + (type.name || type))
@@ -51,5 +51,3 @@ function Type (type) {
return rtn
}
-module.exports = Type
-
View
BIN  test/.variadic.js.swp
Binary file not shown
View
17 test/callback.js
@@ -1,8 +1,7 @@
var assert = require('assert')
, ref = require('ref')
- , Callback = require('../lib/callback')
- , ForeignFunction = require('../lib/foreign_function')
+ , ffi = require('../')
, int = ref.types.int
describe('Callback', function () {
@@ -10,27 +9,27 @@ describe('Callback', function () {
afterEach(gc)
it('should create a C function pointer from a JS function', function () {
- var callback = Callback('void', [ ], function (val) { })
+ var callback = ffi.Callback('void', [ ], function (val) { })
assert(Buffer.isBuffer(callback))
})
it('should be invokable by an ffi\'d ForeignFunction', function () {
- var funcPtr = Callback(int, [ int ], Math.abs)
- var func = ForeignFunction(funcPtr, int, [ int ])
+ var funcPtr = ffi.Callback(int, [ int ], Math.abs)
+ var func = ffi.ForeignFunction(funcPtr, int, [ int ])
assert.equal(1234, func(-1234))
})
it('should work with a "void" return type', function () {
- var funcPtr = Callback('void', [ ], function (val) { })
- var func = ForeignFunction(funcPtr, 'void', [ ])
+ var funcPtr = ffi.Callback('void', [ ], function (val) { })
+ var func = ffi.ForeignFunction(funcPtr, 'void', [ ])
assert.strictEqual(null, func())
})
describe('async', function () {
it('should be invokable asynchronously by an ffi\'d ForeignFunction', function () {
- var funcPtr = Callback(int, [ int ], Math.abs)
- var func = ForeignFunction(funcPtr, int, [ int ])
+ var funcPtr = ffi.Callback(int, [ int ], Math.abs)
+ var func = ffi.ForeignFunction(funcPtr, int, [ int ])
func.async(-9999, function (err, res) {
assert.equal(null, err)
assert.equal(9999, res)
View
16 test/cif.js
@@ -1,21 +1,19 @@
var assert = require('assert')
, ref = require('ref')
- , CIF = require('../lib/cif')
- , Types = require('../lib/types')
- , bindings = require('../lib/bindings')
+ , ffi = require('../')
describe('ffi_cif', function () {
afterEach(gc)
it('should return a Buffer representing the `ffi_cif` instance', function () {
- var cif = CIF(ref.types.void, [ ])
+ var cif = ffi.CIF(ref.types.void, [ ])
assert(Buffer.isBuffer(cif))
})
it('should throw an Error when given an invalid "type"', function () {
- var ffi_type = new Types.FFI_TYPE
+ var ffi_type = new ffi.FFI_TYPE
ffi_type.size = 0
ffi_type.alignment = 0
ffi_type.type = 0
@@ -23,22 +21,22 @@ describe('ffi_cif', function () {
var bad_type = { size: 1, indirection: 1, ffi_type: ffi_type.ref() }
assert.throws(function () {
- CIF(bad_type, [])
+ ffi.CIF(bad_type, [])
}, function (err) {
assert(err.message.indexOf('FFI_BAD_TYPEDEF') !== -1)
assert.equal('FFI_BAD_TYPEDEF', err.code)
- assert.equal(bindings.FFI_BAD_TYPEDEF, err.errno)
+ assert.equal(ffi.FFI_BAD_TYPEDEF, err.errno)
return true
})
})
it('should throw an Error when given an invalid ABI argument', function () {
assert.throws(function () {
- CIF(ref.types.void, [], -1)
+ ffi.CIF(ref.types.void, [], -1)
}, function (err) {
assert(err.message.indexOf('FFI_BAD_ABI') !== -1)
assert.equal('FFI_BAD_ABI', err.code)
- assert.equal(bindings.FFI_BAD_ABI, err.errno)
+ assert.equal(ffi.FFI_BAD_ABI, err.errno)
return true
})
})
View
6 test/errno.js
@@ -1,8 +1,8 @@
var assert = require('assert')
, ref = require('ref')
- , errno = require('../lib/errno')
- , Library = require('../lib/library')
+ , ffi = require('../')
+ , errno = ffi.errno
, 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 Library(lib, {
+ var strtoul = new ffi.Library(lib, {
'strtoul': [ 'ulong', [ 'string', charPtr, 'int' ] ]
}).strtoul
var before = errno()
View
20 test/foreign_function.js
@@ -3,7 +3,7 @@ var expect = require('expect.js')
, assert = require('assert')
, ref = require('ref')
, Struct = require('ref-struct')
- , ForeignFunction = require('../lib/foreign_function')
+ , ffi = require('../')
, bindings = require('./build/Release/ffi_tests')
describe('ForeignFunction', function () {
@@ -18,20 +18,20 @@ describe('ForeignFunction', function () {
it('should call the static "abs" bindings', function () {
var _abs = bindings.abs
- var abs = ForeignFunction(_abs, 'int', [ 'int' ])
+ var abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ])
expect(abs).to.be.a('function')
expect(abs(-1234)).to.equal(1234)
})
it('should call the static "atoi" bindings', function () {
var _atoi = bindings.atoi
- var atoi = ForeignFunction(_atoi, 'int', [ 'string' ])
+ var atoi = ffi.ForeignFunction(_atoi, 'int', [ 'string' ])
expect(atoi).to.be.a('function')
expect(atoi('1234')).to.equal(1234)
})
it('should call the static "double_box" bindings', function () {
- var double_box = ForeignFunction(bindings.double_box, box, [ box ])
+ var double_box = ffi.ForeignFunction(bindings.double_box, box, [ box ])
var b = new box
assert(b instanceof box)
b.width = 4
@@ -49,7 +49,7 @@ describe('ForeignFunction', function () {
it('should call the static "double_box_ptr" bindings', function () {
var boxPtr = ref.refType(box)
- var double_box_ptr = ForeignFunction(bindings.double_box_ptr, box, [ boxPtr ])
+ var double_box_ptr = ffi.ForeignFunction(bindings.double_box_ptr, box, [ boxPtr ])
var b = new box
b.width = 4
b.height = 5
@@ -65,7 +65,7 @@ describe('ForeignFunction', function () {
})
it('should call the static "area_box" bindings', function () {
- var area_box = ForeignFunction(bindings.area_box, ref.types.int, [ box ])
+ var area_box = ffi.ForeignFunction(bindings.area_box, ref.types.int, [ box ])
var b = new box({ width: 5, height: 20 })
var rtn = area_box(b)
assert.equal('number', typeof rtn)
@@ -74,7 +74,7 @@ describe('ForeignFunction', function () {
it('should call the static "area_box_ptr" bindings', function () {
var boxPtr = ref.refType(box)
- var area_box = ForeignFunction(bindings.area_box_ptr, ref.types.int, [ boxPtr ])
+ var area_box = ffi.ForeignFunction(bindings.area_box_ptr, ref.types.int, [ boxPtr ])
var b = new box({ width: 5, height: 20 })
var rtn = area_box(b.ref())
assert.equal('number', typeof rtn)
@@ -82,7 +82,7 @@ describe('ForeignFunction', function () {
})
it('should call the static "create_box" bindings', function () {
- var create_box = ForeignFunction(bindings.create_box, box, [ 'int', 'int' ])
+ var create_box = ffi.ForeignFunction(bindings.create_box, box, [ 'int', 'int' ])
var rtn = create_box(1, 2)
assert(rtn instanceof box)
assert.equal(1, rtn.width)
@@ -96,7 +96,7 @@ describe('ForeignFunction', function () {
box.set(boxes, box.size * 1, { width: 2, height: 20 })
box.set(boxes, box.size * 2, { width: 3, height: 30 })
var boxPtr = ref.refType(box)
- var add_boxes = ForeignFunction(bindings.add_boxes, box, [ boxPtr, 'int' ])
+ var add_boxes = ffi.ForeignFunction(bindings.add_boxes, box, [ boxPtr, 'int' ])
var rtn = add_boxes(boxes, count)
assert(rtn instanceof box)
assert.equal(6, rtn.width)
@@ -107,7 +107,7 @@ describe('ForeignFunction', function () {
it('should call the static "abs" bindings asynchronously', function (done) {
var _abs = bindings.abs
- var abs = ForeignFunction(_abs, 'int', [ 'int' ])
+ var abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ])
expect(abs).to.be.a('function')
// invoke asynchronously
View
14 test/library.js
@@ -3,16 +3,8 @@ var expect = require('expect.js')
, assert = require('assert')
, ref = require('ref')
, Struct = require('ref-struct')
- , Library = require('../lib/library')
- , LIB_EXT = {
- 'linux': '.so'
- , 'linux2': '.so'
- , 'sunos': '.so'
- , 'solaris':'.so'
- , 'darwin': '.dylib'
- , 'mac': '.dylib'
- , 'win32': '.dll'
- }[process.platform]
+ , ffi = require('../')
+ , Library = ffi.Library
describe('Library', function () {
@@ -50,7 +42,7 @@ describe('Library', function () {
it('should accept a lib name with file extension', function() {
var lib = process.platform == 'win32'
? 'msvcrt.dll'
- : 'libm' + LIB_EXT
+ : 'libm' + ffi.LIB_EXT
var libm = new Library(lib, {
'ceil': [ 'double', ['double'] ]
})
View
7 test/objc.js
@@ -1,8 +1,7 @@
var expect = require('expect.js')
, ref = require('ref')
- , bindings = require('../lib/bindings')
- , Library = require('../lib/library')
+ , ffi = require('../')
, voidPtr = ref.refType(ref.types.void)
// these are "opaque" pointer types, so we only care about the memory addess,
@@ -12,13 +11,13 @@ var id = voidPtr
, SEL = voidPtr
, Class = voidPtr
-if (bindings.HAS_OBJC) {
+if (ffi.HAS_OBJC) {
describe('@try / @catch', function () {
afterEach(gc)
- var objcLib = new Library('libobjc', {
+ var objcLib = new ffi.Library('libobjc', {
'objc_msgSend': [ id, [ id, SEL ] ]
, 'objc_getClass': [ Class, [ 'string' ] ]
, 'sel_registerName': [ SEL, [ 'string' ] ]
View
4 test/types.js
@@ -1,7 +1,7 @@
var assert = require('assert')
, ref = require('ref')
- , Types = require('../lib/types')
+ , ffi = require('../')
describe('types', function () {
@@ -10,7 +10,7 @@ describe('types', function () {
Object.keys(ref.types).forEach(function (name) {
it('should match a valid `ffi_type` for "' + name + '"', function () {
var type = ref.types[name]
- var ffi_type = Types.ffiType(type)
+ var ffi_type = ffi.ffiType(type)
assert(Buffer.isBuffer(ffi_type))
})
})
View
6 test/variadic.js
@@ -1,7 +1,7 @@
var assert = require('assert')
, ref = require('ref')
- , VariadicForeignFunction = require('../lib/foreign_function_var')
+ , ffi = require('../')
, snprintfPtr = require('./build/Release/ffi_tests').snprintf
describe('variadic arguments', function () {
@@ -10,7 +10,7 @@ describe('variadic arguments', function () {
it('should work with vararg C functions', function () {
var buf = new Buffer(100)
- var snprintfGen = VariadicForeignFunction(snprintfPtr, 'int', [ 'pointer', 'size_t', 'string' ])
+ var snprintfGen = ffi.VariadicForeignFunction(snprintfPtr, 'int', [ 'pointer', 'size_t', 'string' ])
snprintfGen()(buf, buf.length, 'hello world!')
assert.equal(buf.readCString(), 'hello world!')
@@ -26,7 +26,7 @@ describe('variadic arguments', function () {
})
it('should return the same Function instance when the same arguments are used', function () {
- var snprintfGen = VariadicForeignFunction(snprintfPtr, 'int', [ 'pointer', 'size_t', 'string' ])
+ var snprintfGen = ffi.VariadicForeignFunction(snprintfPtr, 'int', [ 'pointer', 'size_t', 'string' ])
var one = snprintfGen('int')
var two = snprintfGen(ref.types.int)
Please sign in to comment.
Something went wrong with that request. Please try again.