Skip to content

Commit

Permalink
Merge pull request #10 from Xila-Project/Feature/Tasks
Browse files Browse the repository at this point in the history
Feature/tasks
  • Loading branch information
AlixANNERAUD authored Jun 29, 2024
2 parents 9327507 + cc8b965 commit 533643a
Show file tree
Hide file tree
Showing 64 changed files with 4,586 additions and 1,235 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
"Modules/Shared",
"Modules/Virtual_machine",
"Modules/Virtual_machine/Tests/WASM_test",
"Modules/Bindings/Tests/WASM_test", "Modules/Graphics", "Modules/Screen", "Modules/Task",
"Modules/Bindings/Tests/WASM_test", "Modules/Graphics", "Modules/Screen", "Modules/Task", "Modules/Users",
]

[package]
Expand Down
2 changes: 2 additions & 0 deletions Modules/Bindings/Build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{env, process::Command};

fn main() -> Result<(), ()> {
println!("cargo:rerun-if-changed=Tests/WASM_test/src/main.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/src/File_system.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/src/Task.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/Cargo.toml");

// TODO : Add a check for test mode
Expand Down
6 changes: 6 additions & 0 deletions Modules/Bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ File_system = { path = "../File_system" }
Virtual_machine = { path = "../Virtual_machine" }
Binding_tool = { path = "../../../Binding_tool", features = ["Native"] }
Shared = { path = "../Shared" }
Users = { path = "../Users" }
Task = { path = "../Task" }

[[test]]
name = "File_system_bindings_tests"
path = "Tests/File_system.rs"

[[test]]
name = "Task_tests"
path = "Tests/Task.rs"
26 changes: 19 additions & 7 deletions Modules/Bindings/Tests/File_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#![allow(non_upper_case_globals)]

use Bindings::File_system_bindings;
use File_system::{Drivers::Native::File_system_type, Prelude::File_system_traits};
use File_system::{
Drivers::Native::File_system_type,
Prelude::{Path_type, Virtual_file_system_type},
};
use Virtual_machine::{Data_type, Instantiate_test_environment, WasmValue};

#[test]
Expand All @@ -12,14 +15,23 @@ fn Integration_test() {
"../../../target/wasm32-unknown-unknown/release/File_system_bindings_WASM_test.wasm"
);

let mut Native_file_system = File_system_type::New();
let Virtual_file_system =
Virtual_file_system_type::New(Task::Manager_type::New(), Users::Manager_type::New());

Native_file_system.Initialize().unwrap();
let Native_file_system = File_system_type::New().expect("Failed to create file system");

let User_data = Data_type::New(&Native_file_system);
let _ = Virtual_file_system.Mount(Box::new(Native_file_system), Path_type::Get_root());

let (_Runtime, _Module, Instance) =
Instantiate_test_environment(Binary_buffer, File_system_bindings {}, &User_data);
let (_Runtime, _Module, Instance) = Instantiate_test_environment(
Binary_buffer,
File_system_bindings::New(Virtual_file_system.clone()),
&Data_type::New(),
);

assert_eq!(Instance.Call_main(&vec![]).unwrap(), WasmValue::I32(0))
assert_eq!(
Instance
.Call_export_function("Test_file_system", &vec![])
.unwrap(),
WasmValue::I32(0)
)
}
30 changes: 30 additions & 0 deletions Modules/Bindings/Tests/Task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

use Bindings::{File_system_bindings, Task_bindings};
use File_system::{
Drivers::Native::File_system_type,
Prelude::{Path_type, Virtual_file_system_type},
};
use Virtual_machine::{Data_type, Instantiate_test_environment, WasmValue};

#[test]
fn Integration_test() {
let Binary_buffer = include_bytes!(
"../../../target/wasm32-unknown-unknown/release/File_system_bindings_WASM_test.wasm"
);

let Task_manager = Task::Manager_type::New();

let (_Runtime, _Module, Instance) = Instantiate_test_environment(
Binary_buffer,
Task_bindings::New(Task_manager.clone()),
&Data_type::New(),
);

assert_eq!(
Instance.Call_export_function("Test_task", &vec![]).unwrap(),
WasmValue::I32(42)
)
}
1 change: 0 additions & 1 deletion Modules/Bindings/Tests/WASM_test/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ rustflags = [
"-C", "link-arg=--export=__heap_base",
"-C", "link-arg=--export=__data_end",
"-C", "link-arg=--strip-all",
"-C", "lto=yes",
]
3 changes: 1 addition & 2 deletions Modules/Bindings/Tests/WASM_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Binding_tool = { path = "../../../../../Binding_tool", features = ["WASM"] }
wee_alloc = "0.4.5"
Binding_tool = { path = "../../../../../Binding_tool", features = ["WASM"] }
98 changes: 98 additions & 0 deletions Modules/Bindings/Tests/WASM_test/src/File_system.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::num::NonZeroU32;

use Binding_tool::Bind_function_WASM;

#[Bind_function_WASM]
fn Open(Path: &str, Flags: u32, File_identifier: &mut u32) -> Result<(), NonZeroU32> {}

#[Bind_function_WASM]
fn Read(File_identifier: u32, Buffer: &mut [u8], Read_size: &mut u64) -> Result<(), NonZeroU32> {}

#[Bind_function_WASM]
fn Create_file(Path: &str) -> Result<(), NonZeroU32> {}

#[Bind_function_WASM]
fn Write(File_identifier: u32, Buffer: &[u8], Write_size: &mut u64) -> Result<(), NonZeroU32> {}

#[Bind_function_WASM]
fn Exists(Path: &str, Exists: &mut bool) -> Result<(), NonZeroU32> {}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
#[allow(dead_code)]
pub enum Position_type {
Start(u64),
Current(i64),
End(i64),
}

#[Bind_function_WASM]
fn Set_position(
File_identifier: u32,
Position: &Position_type,
Result_value: &mut u64,
) -> Result<(), NonZeroU32> {
}

#[Bind_function_WASM]
fn Delete(Path: &str, Recursive: bool) -> Result<(), NonZeroU32> {}

#[no_mangle]
fn Test_file_system() -> u32 {
let mut File_identifier = 0;

let mut File_exists: bool = true;

Exists("/wasm.txt", &mut File_exists).expect("Failed to check if file exists");

if File_exists {
Delete("/wasm.txt", false).expect("Failed to delete file");
}

Create_file("/wasm.txt").expect("Failed to create file");

Exists("/wasm.txt", &mut File_exists).expect("Failed to check if file exists");

if !File_exists {
return 2;
}

let Flags = 0b11 << 4;

Open("/wasm.txt", Flags, &mut File_identifier).expect("Failed to open file");

let mut Write_size = 0;

let Message = "Hello world from WASM !";

Write(File_identifier, Message.as_bytes(), &mut Write_size).expect("Failed to write file");

if Write_size != Message.len() as u64 {
return 4;
}

let mut Result_position = 0;

Set_position(
File_identifier,
&Position_type::Start(0),
&mut Result_position,
)
.expect("Failed to set position");

let mut Buffer = [0; 64];

let mut Read_size = 0;

Read(File_identifier, &mut Buffer, &mut Read_size).expect("Failed to read file");

let String = String::from_utf8_lossy(&Buffer[..Read_size as usize]);

if String != Message {
return 6;
}

Delete("/wasm.txt", false).expect("Failed to delete file");

0
}
26 changes: 26 additions & 0 deletions Modules/Bindings/Tests/WASM_test/src/Task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![allow(non_upper_case_globals)]

use std::{num::NonZeroU32, sync::RwLock};

use Binding_tool::Bind_function_WASM;

#[Bind_function_WASM]
fn New_task(Name: &str, Stack_size: u32, Function: u32) -> Result<(), NonZeroU32> {}

#[Bind_function_WASM]
fn Sleep(Duration: u64) {}

fn Test_function() {
*Test_variable.write().unwrap() = 42;
}

static Test_variable: RwLock<u32> = RwLock::new(0);

#[no_mangle]
fn Test_task() -> u32 {
let _ = New_task("Test", 4096, Test_function as usize as u32);

Sleep(1);

*Test_variable.read().unwrap()
}
44 changes: 4 additions & 40 deletions Modules/Bindings/Tests/WASM_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![no_main]

use Binding_tool::Bind_function_WASM;
mod File_system;

#[Bind_function_WASM]
fn Open_file(Path: &str, Mode: u32, File_identifier: &mut u16) -> u32 {}

#[Bind_function_WASM]
fn Read_file(File_identifier: u16, Buffer: &mut [u8], Read_size: &mut u32) -> u32 {}

extern crate wee_alloc;

// Use `wee_alloc` as the global allocator saving kilobytes of code size
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

fn main() -> Result<(), ()> {
let mut File_identifier = 1;

let Result = Open_file("test.txt", 1, &mut File_identifier);

if Result != 0 {
return Err(());
}

let mut Buffer = [0; 1024];

let mut Read_size = 0;

let Result = Read_file(File_identifier, &mut Buffer, &mut Read_size);

if Result != 0 {
return Err(());
}

let String = String::from_utf8_lossy(&Buffer[..Read_size as usize]);

if String != "Hello World!" {
return Err(());
}

Ok(())
}
mod Task;
1 change: 0 additions & 1 deletion Modules/Bindings/Xila/test.txt

This file was deleted.

Loading

0 comments on commit 533643a

Please sign in to comment.