Skip to content

Commit b879b9c

Browse files
committed
Implement the logic for the rs_loader_impl_load_from_file and implement a basic functionality for metacall_registrator
1 parent 560ab63 commit b879b9c

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

source/loaders/rs_loader/rust/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ rustc_private = true
1414
opt-level = "z"
1515

1616
[workspace]
17-
members = ["parser"]
17+
members = ["parser", "metacall_registrator"]
1818

1919
[dependencies]
20-
parser = { path = "./parser" }
20+
parser = { path = "./parser" }
21+
metacall_registrator = { path = "metacall_registrator" }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "metacall_registrator"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
cargo_toml = "0.8.1"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::{ffi::c_void, path::PathBuf};
2+
3+
pub struct CargoCdylibProject {
4+
path_to_project: PathBuf,
5+
path_to_project_librs: PathBuf,
6+
}
7+
impl CargoCdylibProject {
8+
pub fn new(
9+
path_buf: PathBuf,
10+
loader_impl: *mut c_void,
11+
) -> Result<CargoCdylibProject, &'static str> {
12+
Err("Basic implementation")
13+
}
14+
15+
pub fn register_the_project_in_metacall(&self) -> Result<(), &'static str> {
16+
Err("Basic implementation")
17+
}
18+
}

source/loaders/rs_loader/rust/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pub use lifecycle::rs_loader_impl_load_from_memory;
1818
pub use lifecycle::rs_loader_impl_load_from_package;
1919

2020
pub struct LoaderLifecycleState {
21-
paths: Vec<PathBuf>,
21+
execution_paths: Vec<PathBuf>,
2222
}
2323
impl LoaderLifecycleState {
24-
pub fn new(paths: Vec<PathBuf>) -> LoaderLifecycleState {
25-
LoaderLifecycleState { paths }
24+
pub fn new(execution_paths: Vec<PathBuf>) -> LoaderLifecycleState {
25+
LoaderLifecycleState { execution_paths }
2626
}
2727
}

source/loaders/rs_loader/rust/src/lifecycle/execution_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub extern "C" fn rs_loader_impl_execution_path(
1313

1414
unsafe {
1515
(*(loader_lifecycle_state))
16-
.paths
16+
.execution_paths
1717
.push(PathBuf::from(path_slice));
1818
}
1919

source/loaders/rs_loader/rust/src/lifecycle/load_from_file.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,69 @@
1-
use crate::{c_char, c_int, c_void, CStr};
1+
use crate::{bridge_api, c_char, c_int, c_void, CStr, PathBuf};
2+
3+
use metacall_registrator;
4+
5+
fn rs_loader_impl_load_from_file_on_error(error: &str) -> *mut c_void {
6+
eprintln!("{}", error);
7+
8+
0 as c_int as *mut c_void
9+
}
210

311
#[no_mangle]
412
pub extern "C" fn rs_loader_impl_load_from_file(
5-
_loader_impl: *mut c_void,
13+
loader_impl: *mut c_void,
614
paths: *const *mut c_char,
715
size: usize,
816
) -> *mut c_void {
17+
let loader_lifecycle_state = bridge_api::get_loader_lifecycle_state(loader_impl);
18+
let mut execution_paths_iterator = unsafe { (*loader_lifecycle_state).execution_paths.iter() };
19+
920
for i in 0..size {
1021
let path: *const c_char = paths.wrapping_offset(i as isize) as *const c_char;
22+
let path_slice = unsafe { CStr::from_ptr(path) }.to_str().unwrap();
23+
let mut path_buf = PathBuf::from(path_slice);
24+
25+
if !path_buf.is_absolute() {
26+
let execution_path = execution_paths_iterator.next();
27+
28+
match execution_path {
29+
Some(execution_path) => {
30+
let mut execution_path = execution_path.clone();
31+
let mut execution_path_as_str = execution_path.to_str().unwrap();
32+
33+
if !execution_path_as_str.ends_with("/") {
34+
execution_path = PathBuf::from(format!("{}{}", execution_path_as_str, "/"));
35+
36+
//Reassign the execution_path_as_str since the execution_path got changed
37+
execution_path_as_str = execution_path.to_str().unwrap();
38+
}
1139

12-
let c_path: &CStr = unsafe { CStr::from_ptr(path) };
40+
path_buf = PathBuf::from(format!(
41+
"{}{}",
42+
execution_path_as_str,
43+
path_buf.to_str().unwrap()
44+
));
45+
}
46+
None => {
47+
return rs_loader_impl_load_from_file_on_error(
48+
"Execution path's length is less than non-absolute path's length",
49+
)
50+
}
51+
};
52+
}
53+
if !path_buf.is_file() {
54+
return rs_loader_impl_load_from_file_on_error("Not a valid path to a file");
55+
}
1356

14-
let path_slice: &str = c_path.to_str().unwrap();
57+
let cargo_cdylib_project =
58+
match metacall_registrator::CargoCdylibProject::new(path_buf, loader_impl) {
59+
Ok(cargo_cdylib_project) => cargo_cdylib_project,
60+
Err(error) => return rs_loader_impl_load_from_file_on_error(error),
61+
};
1562

16-
println!("Path slice given from Rust: {}", path_slice);
63+
match cargo_cdylib_project.register_the_project_in_metacall() {
64+
Ok(_) => (),
65+
Err(error) => return rs_loader_impl_load_from_file_on_error(error),
66+
};
1767
}
1868

1969
1 as c_int as *mut c_void

0 commit comments

Comments
 (0)