Skip to content

Instantly share code, notes, and snippets.

@PsichiX
Last active October 24, 2024 20:29
Show Gist options
  • Save PsichiX/b2dba785e1c4254b56a3bd0533cbfc72 to your computer and use it in GitHub Desktop.
Save PsichiX/b2dba785e1c4254b56a3bd0533cbfc72 to your computer and use it in GitHub Desktop.
FFI support for Intuicio scripting platform
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!(&registry => fn add(a: i32, b: i32) -> (result: i32)))
.unwrap();
let ffi_ensure = lib
.function(function_signature!(&registry => 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();
}
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!(&registry => fn add(a: i32, b: i32) -> (result: i32)),
);
let ffi_ensure = Ffi::from_function_signature(
FfiCodePtr(ensure_42 as *mut _),
&function_signature!(&registry => 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!(&registry => fn add(a: i32, b: i32) -> (result: i32)),
);
let ffi_ensure = Ffi::build_function(
FfiCodePtr(ensure_42 as *mut _),
function_signature!(&registry => fn ensure_42(v: i32) -> ()),
);
context.stack().push(2i32);
context.stack().push(40i32);
ffi_add.invoke(&mut context, &registry);
ffi_ensure.invoke(&mut context, &registry);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment