Skip to content

Instantly share code, notes, and snippets.

@fgalan
Created March 25, 2024 17:33
Show Gist options
  • Save fgalan/4da64b964a93d29995d6b7c57069add6 to your computer and use it in GitHub Desktop.
Save fgalan/4da64b964a93d29995d6b7c57069add6 to your computer and use it in GitHub Desktop.
rustc --crate-type=staticlib jexl_evaluator.rs
g++ main.cpp -L. -ljexl_evaluator -o main
./main
// jexl_evaluator.rs
use std::ffi::{CString, CStr};
use std::os::raw::c_char;
#[no_mangle]
pub extern "C" fn evaluate_jexl(exp: *const c_char, ctx: *const c_char) -> *mut c_char {
// Convert C string to Rust strings
let c_exp = unsafe { CStr::from_ptr(exp) };
let c_ctx = unsafe { CStr::from_ptr(ctx) };
let exp = c_exp.to_str().unwrap();
let ctx = c_ctx.to_str().unwrap();
// Concatenate them
let result = format!("Expression: {}, Context: {}", exp, ctx);
// Convert the result to a C string and return it
CString::new(result).unwrap().into_raw()
}
// main.cpp
#include <iostream>
extern "C" {
char* evaluate_jexl(const char* exp, const char* ctx);
}
int main() {
char* result = evaluate_jexl("x|uppercase", "{\"x\": \"holaaa\"}");
printf("Result: %s\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment