diff --git a/.gitignore b/.gitignore index fa8d85a..691a001 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Cargo.lock target +node_modules diff --git a/node-to-rust/Cargo.toml b/node-to-rust/Cargo.toml new file mode 100644 index 0000000..f318de7 --- /dev/null +++ b/node-to-rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "node-to-rust" +version = "0.1.0" +authors = ["Alex Crichton "] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/node-to-rust/Makefile b/node-to-rust/Makefile new file mode 100644 index 0000000..b8898b2 --- /dev/null +++ b/node-to-rust/Makefile @@ -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 diff --git a/node-to-rust/src/lib.rs b/node-to-rust/src/lib.rs new file mode 100644 index 0000000..bf573d1 --- /dev/null +++ b/node-to-rust/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern fn double_input(input: i32) -> i32 { + input * 2 +} diff --git a/node-to-rust/src/main.js b/node-to-rust/src/main.js new file mode 100644 index 0000000..4ffec4b --- /dev/null +++ b/node-to-rust/src/main.js @@ -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);