Add node to Rust example

This commit is contained in:
Alex Crichton
2015-04-19 10:48:56 -07:00
parent e37bd54df4
commit 9d2ba106f9
5 changed files with 31 additions and 0 deletions

8
node-to-rust/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "node-to-rust"
version = "0.1.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
[lib]
name = "double_input"
crate-type = ["dylib"]

9
node-to-rust/Makefile Normal file
View File

@@ -0,0 +1,9 @@
all: target/libdouble_input.so
node src/main.js
target/libdouble_input.so: src/lib.rs Cargo.toml
cargo build
(cd target && ln -nsf debug/libdouble_input-* libdouble_input.so)
clean:
rm -rf target

4
node-to-rust/src/lib.rs Normal file
View File

@@ -0,0 +1,4 @@
#[no_mangle]
pub extern fn double_input(input: i32) -> i32 {
input * 2
}

9
node-to-rust/src/main.js Normal file
View File

@@ -0,0 +1,9 @@
var ffi = require('ffi');
var lib = ffi.Library('target/libdouble_input.so', {
'double_input': [ 'int', [ 'int' ] ]
});
var input = 4;
var output = lib.double_input(input);
console.log(input + " * 2 = " + output);