Skip to content

Instantly share code, notes, and snippets.

@Mefistophell
Created August 13, 2020 11:29
Show Gist options
  • Select an option

  • Save Mefistophell/62cf2a2b03c62daf7ed12dd7e312c64e to your computer and use it in GitHub Desktop.

Select an option

Save Mefistophell/62cf2a2b03c62daf7ed12dd7e312c64e to your computer and use it in GitHub Desktop.

Revisions

  1. Mefistophell created this gist Aug 13, 2020.
    15 changes: 15 additions & 0 deletions Cargo.toml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    [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"]
    9 changes: 9 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    var ffi = require('ffi');

    var lib = ffi.Library('target/release/libembed', {
    'process': ['string', ['string']]
    });


    const r = lib.process('Nill');
    console.log('From rust:', r);
    15 changes: 15 additions & 0 deletions lib.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // 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()
    }