How to return string value from a Rust FFI function in NodeJS?

穿精又带淫゛_ 提交于 2019-12-01 22:59:42
Hunter Lester

Thank you Wesley Wiser for giving me a big clue with CString. I found the answer in The Rust FFI Omnibus.

The memory for my expected JSON string was being deallocated before my NodeJS program could access it, whether I returned the JSON string or returned a CString.

Here's my solution based on that article. To other novice programmers, please keep in mind that my solution may or may not be ideal:

Rust

extern crate rand;
extern crate rustc_serialize;
extern crate libc;

use libc::c_char;
use rand::{OsRng, Rng};
use std::ffi::CString;
use rustc_serialize::json;

#[no_mangle]
pub extern "C" fn generate() -> *mut c_char {
    let choices: [u8; 6] = [1, 2, 3, 4, 5, 6];

    let mut rand_vec: Vec<u8> = Vec::new();

    let mut rng = match OsRng::new() {
        Ok(t) => t,
        Err(e) => panic!("Failed to create OsRng!, {}", e),
    };

    for _ in 0..6 {
        rand_vec.push(*rng.choose(&choices).unwrap());
    }

    let json_string = CString::new(json::encode(&rand_vec).unwrap()).unwrap();

    json_string.into_raw()
}

#[no_mangle]
pub extern "C" fn free_memory(pointer: *mut c_char) {
    unsafe {
        if pointer.is_null() {
            return;
        }
        CString::from_raw(pointer)
    };
}

NodeJS

var ffi = require('ffi');
var path = require('path');

var lib = ffi.Library(path.join(__dirname,
  './ffi/generate_6_rand.dll'), {
    generate: [ 'char *' , [ ]],
    free_memory: ['void', ['char *']]
  });

var json_string = lib.generate();

var save_json = JSON.parse(json_string.readCString());

console.log( json_string.readCString()); // Output: [6,1,6,4,1,4]
lib.free_memory(json_string);
console.log(json_string.readCString()); // Output: ��x�

I set up two console.logs to show what the output before and after deallocation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!