Last active
October 24, 2024 20:29
-
-
Save PsichiX/b2dba785e1c4254b56a3bd0533cbfc72 to your computer and use it in GitHub Desktop.
FFI support for Intuicio scripting platform
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
let registry = Registry::default().with_basic_types(); | |
let mut context = Context::new(10240, 10240); | |
let mut lib = FfiLibrary::new("../../target/debug/ffi").unwrap(); | |
let ffi_add = lib | |
.function(function_signature!(®istry => fn add(a: i32, b: i32) -> (result: i32))) | |
.unwrap(); | |
let ffi_ensure = lib | |
.function(function_signature!(®istry => fn ensure_42(v: i32) -> ())) | |
.unwrap(); | |
context.stack().push(2i32); | |
context.stack().push(40i32); | |
unsafe { | |
ffi_add.call(&mut context).unwrap(); | |
ffi_ensure.call(&mut context).unwrap(); | |
} |
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
extern "C" fn add(a: i32, b: i32) -> i32 { | |
a + b | |
} | |
extern "C" fn ensure_42(v: i32) { | |
assert_eq!(v, 42); | |
} | |
let registry = Registry::default().with_basic_types(); | |
let mut context = Context::new(10240, 10240); | |
let i32_type = registry.find_type(TypeQuery::of::<i32>()).unwrap(); | |
let ffi_add = Ffi::new(FfiCodePtr(add as *mut _)) | |
.with_argument(i32_type.clone()) | |
.with_argument(i32_type.clone()) | |
.with_result(i32_type.clone()); | |
let ffi_ensure = Ffi::new(FfiCodePtr(ensure_42 as *mut _)).with_argument(i32_type.clone()); | |
context.stack().push(2i32); | |
context.stack().push(40i32); | |
unsafe { | |
ffi_add.call(&mut context).unwrap(); | |
ffi_ensure.call(&mut context).unwrap(); | |
} | |
let ffi_add = Ffi::from_function_signature( | |
FfiCodePtr(add as *mut _), | |
&function_signature!(®istry => fn add(a: i32, b: i32) -> (result: i32)), | |
); | |
let ffi_ensure = Ffi::from_function_signature( | |
FfiCodePtr(ensure_42 as *mut _), | |
&function_signature!(®istry => fn ensure_42(v: i32) -> ()), | |
); | |
context.stack().push(2i32); | |
context.stack().push(40i32); | |
unsafe { | |
ffi_add.call(&mut context).unwrap(); | |
ffi_ensure.call(&mut context).unwrap(); | |
} | |
let ffi_add = Ffi::build_function( | |
FfiCodePtr(add as *mut _), | |
function_signature!(®istry => fn add(a: i32, b: i32) -> (result: i32)), | |
); | |
let ffi_ensure = Ffi::build_function( | |
FfiCodePtr(ensure_42 as *mut _), | |
function_signature!(®istry => fn ensure_42(v: i32) -> ()), | |
); | |
context.stack().push(2i32); | |
context.stack().push(40i32); | |
ffi_add.invoke(&mut context, ®istry); | |
ffi_ensure.invoke(&mut context, ®istry); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment