forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
83 lines (72 loc) · 2.82 KB
/
Copy pathmod.rs
File metadata and controls
83 lines (72 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Foreign Function Interface (FFI) core for Nulang.
//!
//! This module provides a registry of dynamically loaded native functions and
//! libraries, plus marshalling between Nulang `Value`s and C ABI types.
//!
//! It intentionally does not touch the language frontend, bytecode compiler, or
//! VM execution engine. The registry is global and thread-safe.
pub mod c_api;
pub mod marshal;
pub mod native;
pub use marshal::{call_native, CType, Signature};
pub use native::{
register_native_function, FfiRegistry, NativeFunction, NativeLibrary, FFI_REGISTRY,
};
#[cfg(test)]
mod tests {
use std::ffi::c_void;
use crate::ffi::marshal::{call_native, CType, Signature};
use crate::ffi::native::{register_native_function, NativeFunction};
use crate::vm::{Value, VM};
extern "C" fn add(a: i64, b: i64) -> i64 {
a + b
}
#[test]
fn test_register_and_call_native() {
let signature = Signature::new(vec![CType::I64, CType::I64], CType::I64);
// SAFETY: pointer matches the provided signature.
unsafe {
register_native_function("add", add as *const c_void, signature).unwrap();
}
let func = NativeFunction {
ptr: add as *const c_void,
signature: Signature::new(vec![CType::I64, CType::I64], CType::I64),
library: None,
symbol: "add".to_string(),
};
// SAFETY: pointer matches signature.
let result = unsafe { call_native(&func, &[Value::int(10), Value::int(32)]).unwrap() };
assert_eq!(result.as_int(), Some(42));
}
extern "C" fn double_int(x: i64) -> i64 {
x * 2
}
fn run_source(source: &str) -> Result<Value, crate::types::NuError> {
let tokens = crate::lexer::Lexer::new(source).lex()?;
let ast = crate::parser::Parser::new(tokens).parse_module()?;
let mut tc = crate::typechecker::TypeChecker::new();
tc.check_module(&ast)?;
let hir = crate::hir_lower::lower_module(&ast, &tc.inferred_decl_types);
let mut mir = crate::mir_lower::lower_module(&hir)?;
let module = crate::mir_codegen::compile_mir(&mut mir, "test")?;
let mut vm = VM::new();
vm.load_module(module);
vm.run()
}
#[test]
fn test_compile_and_run_registered_ffi() {
let signature = Signature::new(vec![CType::I64], CType::I64);
// SAFETY: pointer matches the provided signature.
unsafe {
register_native_function("double_int", double_int as *const c_void, signature).unwrap();
}
let source = r#"
extern "__nulang_registered__" {
fn double_int(x: Int) -> Int
}
double_int(21)
"#;
let result = run_source(source).expect("should compile and call registered FFI");
assert_eq!(result.as_int(), Some(42));
}
}