From e855c8811e413e582cdbe73845e8a6ed2556e9f8 Mon Sep 17 00:00:00 2001 From: Rick Branson Date: Thu, 18 Nov 2010 15:48:11 -0600 Subject: [PATCH] Added factorial C library example --- README.md | 4 +++- example/factorial/README | 6 ++++++ example/factorial/factorial.c | 11 +++++++++++ example/factorial/factorial.js | 14 ++++++++++++++ example/factorial/wscript | 24 ++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 example/factorial/README create mode 100644 example/factorial/factorial.c create mode 100644 example/factorial/factorial.js create mode 100644 example/factorial/wscript diff --git a/README.md b/README.md index 27184149..54b9497c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,9 @@ http://github.com/rbranson/node-ffi # DESCRIPTION -node-ffi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code. +node-ffi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code. + +It also simplifies the augmentation of node.js with C code as it takes care of handling the translation of types across JavaScript and C, which can add reams of boilerplate code to your otherwise simple C. See the `examples/factorial` for an example of this use case. WARNING: node-ffi assumes you know what you're doing. You can pretty easily create situations where you will segfault the interpreter and unless you've got C debugger skills, you probably won't know what's going on. diff --git a/example/factorial/README b/example/factorial/README new file mode 100644 index 00000000..b7b3f86b --- /dev/null +++ b/example/factorial/README @@ -0,0 +1,6 @@ +If you've already got node installed, this can be used: + + $ node-waf build + ... + $ node factorial.js 35 + Your output: 6399018521010896896 \ No newline at end of file diff --git a/example/factorial/factorial.c b/example/factorial/factorial.c new file mode 100644 index 00000000..9c1c42e4 --- /dev/null +++ b/example/factorial/factorial.c @@ -0,0 +1,11 @@ +#include + +uint64_t factorial(int max) { + int i = max; + uint64_t result = 1; + + while (i >= 2) + result *= i--; + + return result; +} diff --git a/example/factorial/factorial.js b/example/factorial/factorial.js new file mode 100644 index 00000000..4d89bcf5 --- /dev/null +++ b/example/factorial/factorial.js @@ -0,0 +1,14 @@ +var FFI = require("../../lib/ffi"); + +var libfactorial = new FFI.Library("libfactorial", { + "factorial": [ "long", [ "uint64" ] ] +}); + +if (process.argv.length < 3) { + console.log("Arguments: " + process.argv[0] + " " + process.argv[1] + " "); + process.exit(); +} + +var output = libfactorial.factorial(parseInt(process.argv[2])) + +console.log("Your output: " + output); \ No newline at end of file diff --git a/example/factorial/wscript b/example/factorial/wscript new file mode 100644 index 00000000..eed55663 --- /dev/null +++ b/example/factorial/wscript @@ -0,0 +1,24 @@ +import Options +from os import unlink, symlink, popen +from os.path import exists +from logging import fatal + +srcdir = '.' +blddir = 'build' + +def set_options(opt): + opt.tool_options('compiler_cc') + +def configure(conf): + conf.check_tool('compiler_cc') + +def build(bld): + obj = bld.new_task_gen('cc', 'shlib') + obj.target = 'factorial' + obj.source = 'factorial.c' + +def shutdown(): + if exists("build/default/libfactorial.dylib") and not exists("libfactorial.dylib"): + symlink("build/default/libfactorial.dylib", "libfactorial.dylib") + if exists("build/default/libfactorial.so") and not exists("libfactorial.so"): + symlink("build/default/libfactorial.so", "libfactorial.so") \ No newline at end of file