Created
August 13, 2020 11:29
-
-
Save Mefistophell/62cf2a2b03c62daf7ed12dd7e312c64e to your computer and use it in GitHub Desktop.
[Rust ⭤ Node.js cloud function] communication via strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "embed" | |
version = "0.1.0" | |
authors = ["Yevhen Blotskyi <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
libc = "*" | |
[lib] | |
name = "embed" | |
# path = "src/main.rs" | |
crate-type = ["dylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ffi = require('ffi'); | |
var lib = ffi.Library('target/release/libembed', { | |
'process': ['string', ['string']] | |
}); | |
const r = lib.process('Nill'); | |
console.log('From rust:', r); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib.rs | |
extern crate libc; | |
use libc::c_char; | |
use std::ffi::CString; | |
#[no_mangle] | |
pub extern "C" fn process(name: *mut c_char) -> *mut c_char { | |
let s = unsafe { | |
CString::from_raw(name) | |
}; | |
println!("Hello, {:?}", s); | |
s.into_raw() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment