Created
March 25, 2024 17:33
-
-
Save fgalan/4da64b964a93d29995d6b7c57069add6 to your computer and use it in GitHub Desktop.
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
rustc --crate-type=staticlib jexl_evaluator.rs | |
g++ main.cpp -L. -ljexl_evaluator -o main | |
./main |
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
// 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() | |
} |
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
// 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