Skip to content

Commit

Permalink
trying out lazy cell?
Browse files Browse the repository at this point in the history
  • Loading branch information
soohoonc committed May 13, 2024
1 parent 77c428f commit 84871cc
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 62 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion src/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ bench = true
wasm-bindgen = "0.2.83"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = { version = "1.0.108" }
web-sys = { version = "0.3.66", features=["console"]}
web-sys = { version = "0.3.66", features=["console"]}
include_dir = "0.7.3"
75 changes: 45 additions & 30 deletions src/wasm/src/filesystem/create.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,65 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::path::Path;
use std::sync::LazyLock;

use crate::filesystem::node::Node;
use crate::filesystem::node::NodeType;

use web_sys::console;

static ROOT_NODE: LazyLock<Rc<RefCell<Node>>> = LazyLock::new(|| {
let root_path = Path::new("../../assets/fs");
create_fs(root_path)
});

pub fn get_root_node() -> Rc<RefCell<Node>> {
ROOT_NODE.lock().unwrap().clone()
}

fn traverse(path: &Path, node: Rc<RefCell<Node>>) {
// read files in path and create a filesystem structure

console::log_1(&path.display().to_string().into());

let children = match path.read_dir() {
Ok(children) => {
console::log_1(&"children".into());
children
},
Err(_) => return,
Ok(children) => {
console::log_1(&"children".into());
children
},
Err(_) => {
console::log_1(&"error".into());
return;

},
};
for child in children {
let child = match child {
Ok(child) => child,
Err(_) => continue,
};
let child_path = child.path();
let child_name = child_path.file_name().unwrap().to_str().unwrap().to_string();
let child_node;
if child_path.is_dir() {
child_node = Rc::new(RefCell::new(Node::new(child_name, NodeType::Directory, Some(node.clone()))));
traverse(&child_path, Rc::clone(&child_node));
} else if child_path.is_file() {
child_node = Rc::new(RefCell::new(Node::new(child_name, NodeType::File, Some(node.clone()))));
} else {
continue;
}
match node.borrow().get_node_type() {
NodeType::File => continue,
NodeType::Directory => {
node.borrow_mut().add_child(Rc::clone(&child_node));

for child in children.filter_map(|e| e.ok()) {
let child_path = child.path();
let child_name = child_path.file_name().unwrap().to_str().unwrap().to_string();
let child_node;
if child_path.is_dir() {
console::log_1(&"directory".into());
child_node = Rc::new(RefCell::new(Node::new(child_name, NodeType::Directory, Some(node.clone()))));
traverse(&child_path, Rc::clone(&child_node));
} else if child_path.is_file() {
console::log_1(&"file".into());
child_node = Rc::new(RefCell::new(Node::new(child_name, NodeType::File, Some(node.clone()))));
} else {
continue;
}

match node.borrow().get_node_type() {
NodeType::File => continue,
NodeType::Directory => {
node.borrow_mut().add_child(Rc::clone(&child_node));
}
}
}
}
}

pub fn create_fs (root_path: &Path) -> Rc<RefCell<Node>> {
fn create_fs (root_path: &Path) -> Rc<RefCell<Node>> {
let root = Rc::new(RefCell::new(Node::new("root".to_string(), NodeType::Directory, None)));
// read files in root_path and create a filesystem structure, traverse directories
traverse(root_path, Rc::clone(&root));
// read files in root_path and create a filesystem structure, traverse directories
traverse(root_path, Rc::clone(&root));
root
}
54 changes: 27 additions & 27 deletions src/wasm/src/filesystem/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ pub struct Directory {
}

impl Directory {
pub fn new(name: String, node: Rc<RefCell<Node>>) -> Directory {
Directory {
name,
node,
}
}
// pub fn new(name: String, node: Rc<RefCell<Node>>) -> Directory {
// Directory {
// name,
// node,
// }
// }

pub fn get_name(&self) -> String {
self.name.clone()
}
// pub fn get_name(&self) -> String {
// self.name.clone()
// }

pub fn parent_name(&self) -> Option<String> {
match &self.node.borrow().get_parent() {
Some(parent) => Some(parent.borrow().get_name()),
None => None,
}
}
// pub fn parent_name(&self) -> Option<String> {
// match &self.node.borrow().get_parent() {
// Some(parent) => Some(parent.borrow().get_name()),
// None => None,
// }
// }

pub fn get_parent(&self) -> Option<Rc<RefCell<Node>>> {
match &self.node.borrow().get_parent() {
Some(parent) => Some(Rc::clone(parent)),
None => None,
}
}
// pub fn get_parent(&self) -> Option<Rc<RefCell<Node>>> {
// match &self.node.borrow().get_parent() {
// Some(parent) => Some(Rc::clone(parent)),
// None => None,
// }
// }

pub fn get_children(&self) -> Vec<Rc<RefCell<Node>>> {
self.node.borrow().get_children().clone()
}
// pub fn get_children(&self) -> Vec<Rc<RefCell<Node>>> {
// self.node.borrow().get_children().clone()
// }

pub fn add_child(&mut self, child: Rc<RefCell<Node>>) {
self.node.borrow_mut().get_children().push(Rc::clone(&child));
}
// pub fn add_child(&mut self, child: Rc<RefCell<Node>>) {
// self.node.borrow_mut().get_children().push(Rc::clone(&child));
// }
}
4 changes: 2 additions & 2 deletions src/wasm/src/filesystem/node.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::rc::Rc;
use std::cell::RefCell;

#[derive(Clone)]
#[derive(Clone, Send)]
pub enum NodeType {
Directory,
File,
}


#[derive(Clone, Send)]
pub struct Node {
name: String,
node_type: NodeType,
Expand Down
6 changes: 4 additions & 2 deletions src/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::rc::Rc;
use std::cell::RefCell;

use wasm_bindgen::prelude::*;
// use web_sys::console;
use web_sys::console;
use serde_json;

mod shell;
Expand All @@ -14,6 +14,7 @@ use filesystem::node::Node;
use filesystem::create::create_fs;
// use filesystem::pipe::Pipe;


#[wasm_bindgen]
pub struct Shell {
lexer: Lexer,
Expand All @@ -30,7 +31,8 @@ impl Shell {
#[wasm_bindgen(constructor)]
pub fn new(user: String, hostname: String) -> Shell {
// hardcoded for now
let root = create_fs(std::path::Path::new("."));
console::log_1(&"Creating filesystem".into());
let root = get_root_node();
// let current = root.borrow().get("/user/guest").unwrap();
Shell {
lexer: Lexer::new(),
Expand Down

0 comments on commit 84871cc

Please sign in to comment.