Skip to content

Commit

Permalink
very basic version
Browse files Browse the repository at this point in the history
  • Loading branch information
soohoonc committed Jun 9, 2024
1 parent 8fc610f commit 7de5668
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 163 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"build:wasm": "wasm-pack build ./src/wasm --target web",
"start": "next start",
"lint": "next lint",
"format": "bunx prettier --write .",
Expand Down
43 changes: 9 additions & 34 deletions src/wasm/src/filesystem/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,17 @@ use std::rc::Rc;
// use std::sync::{Arc, Mutex};
use crate::filesystem::node::Node;

#[derive(Clone, PartialEq)]
pub struct Directory {
name: String,
node: Rc<RefCell<Node>>,
pub name: String,
children: Vec<Rc<RefCell<Node>>>,
}

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

// 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 get_parent(&self) -> Option<Arc<Mutex<Node>>> {
// match &self.node.borrow().get_parent() {
// Some(parent) => Some(Rc::clone(parent)),
// None => None,
// }
// }

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

// pub fn add_child(&mut self, child: Arc<Mutex<Node>>) {
// self.node.borrow_mut().get_children().push(Rc::clone(&child));
// }
pub fn new(name: String, children: Vec<Rc<RefCell<Node>>>) -> Directory {
Directory {
name,
children
}
}
}
28 changes: 9 additions & 19 deletions src/wasm/src/filesystem/file.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
// use std::{cell::RefCell, rc::Rc};
use std::sync::{Arc, Mutex};

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


#[derive(Clone, PartialEq)]
pub struct File {
name: String,
pub name: String,
data: String,
node: Arc<Mutex<Node>>
}

impl File {
pub fn new(name: String, data: String, node: Arc<Mutex<Node>>) -> File {
pub fn new(name: String, data: String) -> File {
File {
name,
data,
node,
}
}

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

pub fn read(&self) -> String {
self.data.clone()
}
pub fn write(&mut self, data: String) {
self.data = data;
}
// pub fn read(&self) -> String {
// self.data.clone()
// }
// pub fn write(&mut self, data: String) {
// self.data = data;
// }
}
37 changes: 28 additions & 9 deletions src/wasm/src/filesystem/node.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::rc::Rc;
use std::cell::RefCell;

use super::directory::Directory;
use super::file::File;
// use std::sync::{Arc, Mutex};

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

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct Node {
name: String,
node_type: NodeType,
Expand All @@ -22,10 +25,26 @@ impl Node {
name,
node_type,
parent,
children: Vec::<Rc<RefCell<Node>>>::new(),
children: Vec::new(),
}
}

pub fn as_directory(&self) -> Option<&Directory> {
if let NodeType::Directory(ref dir) = self.node_type {
Some(dir)
} else {
None
}
}

// pub fn as_file(&self) -> Option<&File> {
// if let NodeType::File(ref file) = self.node_type {
// Some(file)
// } else {
// None
// }
// }

pub fn get_node_type(&self) -> NodeType {
self.node_type.clone()
}
Expand All @@ -41,11 +60,11 @@ impl Node {
}
}

pub fn add_child(&mut self, child: Rc<RefCell<Node>>) {
self.children.push(child);
}

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

pub fn add_child(&mut self, child: Rc<RefCell<Node>>) {
self.children.push(Rc::clone(&child));
}
}
25 changes: 16 additions & 9 deletions src/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ 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;
mod filesystem;

use shell::lexer::Lexer;
use shell::exec::Exec;
use filesystem::node::Node;
use filesystem::node::{Node, NodeType};
use filesystem::directory::Directory;
// use filesystem::pipe::Pipe;


#[wasm_bindgen]
pub struct Shell {
lexer: Lexer,
exec: Exec,
root: Rc<RefCell<Node>>,
// root: Rc<RefCell<Node>>,
current: Rc<RefCell<Node>>,
// pipe: Pipe,
user: String,
Expand All @@ -30,15 +31,17 @@ impl Shell {
#[wasm_bindgen(constructor)]
pub fn new(user: String, hostname: String) -> Shell {
let root = Rc::new(RefCell::new(
Node::new("/".to_string(), filesystem::node::NodeType::Directory, None)
Node::new("".to_string(), NodeType::Directory(Directory::new(
"".to_string(),
Vec::new()
)), None)
));

Shell {
lexer: Lexer::new(),
exec: Exec::new(Rc::clone(&root), Rc::clone(&root)),
// current,
current: Rc::clone(&root),
root,
// root,
// pipe: Pipe::new(""),
user,
hostname,
Expand All @@ -50,11 +53,15 @@ impl Shell {
let statement = self.lexer.lex(input);
let result = self.exec.execute(statement);
// console::log_1(&result.clone().into());
self.current = Rc::clone(&self.exec.get_current());
self.user = self.exec.get_user();
self.hostname = self.exec.get_hostname();

let output = serde_json::json!({
"result": result,
"user": self.user,
"host": self.hostname,
"path": self.current.borrow().get_name(),
"user": self.exec.get_user(),
"host": self.exec.get_hostname(),
"path": self.exec.get_current().borrow().get_name(),
});

let output_str = serde_json::to_string(&output).unwrap();
Expand Down
Loading

0 comments on commit 7de5668

Please sign in to comment.