-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
855 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "soohoonc_wasm" | ||
version = "0.0.1" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[lib] | ||
name = "soohoonc_wasm" | ||
crate-type = ["cdylib"] | ||
bench = true | ||
|
||
[dependencies] | ||
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"] } | ||
once_cell = "1.19.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
// use std::sync::{Arc, Mutex}; | ||
use super::node::Node; | ||
|
||
#[derive(Clone, PartialEq)] | ||
pub struct Directory { | ||
pub name: String, | ||
children: Vec<Rc<RefCell<Node>>>, | ||
} | ||
|
||
impl Directory { | ||
pub fn new(name: String, children: Vec<Rc<RefCell<Node>>>) -> Directory { | ||
Directory { name, children } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#[derive(Clone, PartialEq)] | ||
pub struct File { | ||
pub name: String, | ||
data: String, | ||
} | ||
|
||
impl File { | ||
pub fn new(name: String, data: String) -> File { | ||
File { name, data } | ||
} | ||
pub fn read(&self) -> String { | ||
self.data.clone() | ||
} | ||
pub fn write(&mut self, data: String) { | ||
self.data = data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod directory; | ||
pub mod file; | ||
pub mod node; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
use super::directory::Directory; | ||
use super::file::File; | ||
// use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Clone, PartialEq)] | ||
pub enum NodeType { | ||
Directory(Directory), | ||
File(File), | ||
} | ||
|
||
#[derive(Clone, PartialEq)] | ||
pub struct Node { | ||
name: String, | ||
node_type: NodeType, | ||
parent: Option<Rc<RefCell<Node>>>, | ||
children: Vec<Rc<RefCell<Node>>>, | ||
} | ||
|
||
impl Node { | ||
pub fn new(name: String, node_type: NodeType, parent: Option<Rc<RefCell<Node>>>) -> Node { | ||
Node { | ||
name, | ||
node_type, | ||
parent, | ||
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() | ||
} | ||
|
||
pub fn get_name(&self) -> String { | ||
self.name.clone() | ||
} | ||
|
||
pub fn get_parent(&self) -> Option<Rc<RefCell<Node>>> { | ||
match &self.parent { | ||
Some(parent) => Some(Rc::clone(parent)), | ||
None => None, | ||
} | ||
} | ||
|
||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
use wasm_bindgen::prelude::*; | ||
// use web_sys::console; | ||
use serde_json; | ||
|
||
mod fs; | ||
mod shell; | ||
|
||
use fs::directory::Directory; | ||
use fs::node::{Node, NodeType}; | ||
use shell::exec::Exec; | ||
use shell::lexer::Lexer; | ||
// use filesystem::pipe::Pipe; | ||
|
||
#[wasm_bindgen] | ||
pub struct Shell { | ||
lexer: Lexer, | ||
exec: Exec, | ||
root: Rc<RefCell<Node>>, | ||
current: Rc<RefCell<Node>>, | ||
// pipe: Pipe, | ||
user: Rc<RefCell<String>>, | ||
hostname: Rc<RefCell<String>>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl Shell { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(user: String, hostname: String) -> Shell { | ||
let root = Rc::new(RefCell::new(Node::new( | ||
"".to_string(), | ||
NodeType::Directory(Directory::new("".to_string(), Vec::new())), | ||
None, | ||
))); | ||
|
||
Shell { | ||
lexer: Lexer::new(), | ||
exec: Exec::new(), | ||
current: Rc::clone(&root), | ||
root, | ||
// pipe: Pipe::new(""), | ||
user: Rc::new(RefCell::new(user)), | ||
hostname: Rc::new(RefCell::new(hostname)), | ||
} | ||
} | ||
|
||
/** Only one statement for now */ | ||
pub fn run(&mut self, input: &str) -> String { | ||
let statement = self.lexer.lex(input); | ||
let result = self.exec.execute( | ||
statement, | ||
&self.root, | ||
&self.current, | ||
&self.user, | ||
&self.hostname, | ||
); | ||
// console::log_1(&result.clone().into()); | ||
|
||
let output_str = serde_json::to_string(&result).unwrap(); | ||
// console::log_1(&output_str.clone().into()); | ||
output_str | ||
} | ||
} |
Oops, something went wrong.