Skip to content

Instantly share code, notes, and snippets.

@Mefistophell
Created August 13, 2020 11:29
Show Gist options
  • Save Mefistophell/62cf2a2b03c62daf7ed12dd7e312c64e to your computer and use it in GitHub Desktop.
Save Mefistophell/62cf2a2b03c62daf7ed12dd7e312c64e to your computer and use it in GitHub Desktop.
[Rust ⭤ Node.js cloud function] communication via strings
[package]
name = "embed"
version = "0.1.0"
authors = ["Yevhen Blotskyi <yebl@boozt.com>"]
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"]
var ffi = require('ffi');
var lib = ffi.Library('target/release/libembed', {
'process': ['string', ['string']]
});
const r = lib.process('Nill');
console.log('From rust:', r);
// 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