Skip to content

Commit

Permalink
wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
soohoonc committed Aug 23, 2024
1 parent f0a1cf7 commit f8d44fc
Show file tree
Hide file tree
Showing 11 changed files with 855 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

target
197 changes: 197 additions & 0 deletions src/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/wasm/Cargo.toml
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"
16 changes: 16 additions & 0 deletions src/wasm/src/fs/directory.rs
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 }
}
}
17 changes: 17 additions & 0 deletions src/wasm/src/fs/file.rs
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;
}
}
3 changes: 3 additions & 0 deletions src/wasm/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod directory;
pub mod file;
pub mod node;
70 changes: 70 additions & 0 deletions src/wasm/src/fs/node.rs
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));
}
}
65 changes: 65 additions & 0 deletions src/wasm/src/lib.rs
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
}
}
Loading

0 comments on commit f8d44fc

Please sign in to comment.