|
1 | 1 | /// <reference path="node-ffi.d.ts" /> |
2 | 2 |
|
| 3 | +import ffi = require('ffi'); |
3 | 4 | import ref = require('ref'); |
4 | 5 |
|
5 | | -ref.address(new Buffer(1)); |
6 | | -var intBuf = ref.alloc(ref.types.int); |
7 | | -var intWith4 = ref.alloc(ref.types.int, 4); |
8 | | -var buf0 = ref.allocCString('hello world'); |
9 | | -var type = ref.coerceType('int **'); |
10 | | -var val = ref.deref(intBuf); |
11 | | -ref.isNull(new Buffer(1)); |
12 | | -var str = ref.readCString(new Buffer('hello\0world\0'), 0); |
13 | | -var buf1 = ref.alloc('int64'); |
14 | | -ref.writeInt64BE(buf1, 0, '9223372036854775807'); |
15 | | -var val = ref.readInt64BE(buf1, 0) |
16 | | -var voidPtrType = ref.refType(ref.types.void); |
17 | | -var buf2 = ref.alloc('int64'); |
18 | | -ref.writeInt64LE(buf2, 0, '9223372036854775807'); |
| 6 | +{ |
| 7 | + var sqlite3 = ref.types.void; |
| 8 | + var sqlite3Ptr = ref.refType(sqlite3); |
| 9 | + var sqlite3PtrPtr = ref.refType(sqlite3Ptr); |
| 10 | + var stringPtr = ref.refType(ref.types.CString); |
| 11 | + |
| 12 | + var libsqlite3 = ffi.Library('libsqlite3', { |
| 13 | + 'sqlite3_open': [ 'int', [ 'string', sqlite3PtrPtr ] ], |
| 14 | + 'sqlite3_close': [ 'int', [ sqlite3PtrPtr ] ], |
| 15 | + 'sqlite3_exec': [ 'int', [ sqlite3PtrPtr, 'string', 'pointer', 'pointer', stringPtr ] ], |
| 16 | + 'sqlite3_changes': [ 'int', [ sqlite3PtrPtr ]] |
| 17 | + }); |
| 18 | + |
| 19 | + var dbPtrPtr = ref.alloc(sqlite3PtrPtr); |
| 20 | + libsqlite3.sqlite3_open("test.sqlite3", dbPtrPtr); |
| 21 | + var dbHandle = dbPtrPtr.deref(); |
| 22 | +} |
| 23 | +{ |
| 24 | + var func = ffi.ForeignFunction(new Buffer(10), 'int', [ 'int' ]); |
| 25 | + func(-5); |
| 26 | + func.async(-5, function(err, res) {}); |
| 27 | +} |
| 28 | +{ |
| 29 | + var printfPointer = ffi.DynamicLibrary().get('printf'); |
| 30 | + var printfGen = ffi.VariadicForeignFunction(printfPointer, 'void', [ 'string' ]); |
| 31 | + printfGen()('Hello World!\n'); |
| 32 | + printfGen('int')('This is an int: %d\n', 10); |
| 33 | + printfGen('string')('This is a string: %s\n', 'hello'); |
| 34 | +} |
| 35 | +{ |
| 36 | + ref.address(new Buffer(1)); |
| 37 | + var intBuf = ref.alloc(ref.types.int); |
| 38 | + var intWith4 = ref.alloc(ref.types.int, 4); |
| 39 | + var buf0 = ref.allocCString('hello world'); |
| 40 | + var type = ref.coerceType('int **'); |
| 41 | + var val = ref.deref(intBuf); |
| 42 | +} |
| 43 | +{ |
| 44 | + ref.isNull(new Buffer(1)); |
| 45 | +} |
| 46 | +{ |
| 47 | + var str = ref.readCString(new Buffer('hello\0world\0'), 0); |
| 48 | + var buf = ref.alloc('int64'); |
| 49 | + ref.writeInt64BE(buf, 0, '9223372036854775807'); |
| 50 | + var val = ref.readInt64BE(buf, 0) |
| 51 | +} |
| 52 | +{ |
| 53 | + var voidPtrType = ref.refType(ref.types.void); |
| 54 | + var buf = ref.alloc('int64'); |
| 55 | + ref.writeInt64LE(buf, 0, '9223372036854775807'); |
| 56 | +} |
0 commit comments