Skip to content
This repository
Browse code

Added factorial C library example

  • Loading branch information...
commit e855c8811e413e582cdbe73845e8a6ed2556e9f8 1 parent 8f54311
Rick Branson authored November 18, 2010
4  README.md
Source Rendered
@@ -5,7 +5,9 @@ http://github.com/rbranson/node-ffi
5 5
 
6 6
 # DESCRIPTION
7 7
 
8  
-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.
  8
+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. 
  9
+
  10
+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.
9 11
 
10 12
 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.
11 13
 
6  example/factorial/README
... ...
@@ -0,0 +1,6 @@
  1
+If you've already got node installed, this can be used:
  2
+
  3
+    $ node-waf build
  4
+    ...
  5
+    $ node factorial.js 35
  6
+    Your output: 6399018521010896896
11  example/factorial/factorial.c
... ...
@@ -0,0 +1,11 @@
  1
+#include <stdint.h>
  2
+
  3
+uint64_t factorial(int max) {
  4
+    int i           = max;
  5
+    uint64_t result = 1;
  6
+    
  7
+    while (i >= 2)
  8
+        result *= i--;
  9
+    
  10
+    return result;
  11
+}
14  example/factorial/factorial.js
... ...
@@ -0,0 +1,14 @@
  1
+var FFI = require("../../lib/ffi");
  2
+
  3
+var libfactorial = new FFI.Library("libfactorial", {
  4
+    "factorial": [ "long", [ "uint64" ] ]
  5
+});
  6
+
  7
+if (process.argv.length < 3) {
  8
+    console.log("Arguments: " + process.argv[0] + " " + process.argv[1] + " <max>");
  9
+    process.exit();
  10
+}
  11
+
  12
+var output = libfactorial.factorial(parseInt(process.argv[2]))
  13
+
  14
+console.log("Your output: " + output);
24  example/factorial/wscript
... ...
@@ -0,0 +1,24 @@
  1
+import Options
  2
+from os import unlink, symlink, popen
  3
+from os.path import exists 
  4
+from logging import fatal
  5
+
  6
+srcdir = '.'
  7
+blddir = 'build'
  8
+
  9
+def set_options(opt):
  10
+  opt.tool_options('compiler_cc')
  11
+
  12
+def configure(conf):
  13
+  conf.check_tool('compiler_cc')
  14
+
  15
+def build(bld):
  16
+  obj = bld.new_task_gen('cc', 'shlib')
  17
+  obj.target = 'factorial'
  18
+  obj.source = 'factorial.c'
  19
+
  20
+def shutdown():
  21
+  if exists("build/default/libfactorial.dylib") and not exists("libfactorial.dylib"):
  22
+    symlink("build/default/libfactorial.dylib", "libfactorial.dylib")
  23
+  if exists("build/default/libfactorial.so") and not exists("libfactorial.so"):
  24
+    symlink("build/default/libfactorial.so", "libfactorial.so")

0 notes on commit e855c88

Please sign in to comment.
Something went wrong with that request. Please try again.