Skip to content

Commit 1568637

Browse files
committed
Merge branch 'master' into coolreader18/wasm-cleanup1
2 parents 7ca3b77 + d64554d commit 1568637

2 files changed

Lines changed: 50 additions & 20 deletions

File tree

vm/src/dictdatatype.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ use num_traits::ToPrimitive;
99
/// And: http://code.activestate.com/recipes/578375/
1010
use std::collections::HashMap;
1111

12-
#[derive(Default)]
13-
pub struct Dict {
12+
pub struct Dict<T = PyObjectRef> {
1413
size: usize,
1514
indices: HashMap<usize, usize>,
16-
entries: Vec<Option<DictEntry>>,
15+
entries: Vec<Option<DictEntry<T>>>,
1716
}
1817

19-
struct DictEntry {
18+
impl<T> Default for Dict<T> {
19+
fn default() -> Self {
20+
Dict {
21+
size: 0,
22+
indices: HashMap::new(),
23+
entries: Vec::new(),
24+
}
25+
}
26+
}
27+
28+
struct DictEntry<T> {
2029
hash: usize,
2130
key: PyObjectRef,
22-
value: PyObjectRef,
31+
value: T,
2332
}
2433

25-
impl Dict {
34+
impl<T: Clone> Dict<T> {
2635
pub fn new() -> Self {
2736
Dict {
2837
size: 0,
@@ -32,12 +41,7 @@ impl Dict {
3241
}
3342

3443
/// Store a key
35-
pub fn insert(
36-
&mut self,
37-
vm: &VirtualMachine,
38-
key: &PyObjectRef,
39-
value: PyObjectRef,
40-
) -> PyResult<()> {
44+
pub fn insert(&mut self, vm: &VirtualMachine, key: &PyObjectRef, value: T) -> PyResult<()> {
4145
match self.lookup(vm, key)? {
4246
LookupResult::Existing(index) => {
4347
// Update existing key
@@ -76,7 +80,7 @@ impl Dict {
7680
}
7781

7882
/// Retrieve a key
79-
pub fn get(&self, vm: &VirtualMachine, key: &PyObjectRef) -> PyResult<Option<PyObjectRef>> {
83+
pub fn get(&self, vm: &VirtualMachine, key: &PyObjectRef) -> PyResult<Option<T>> {
8084
if let LookupResult::Existing(index) = self.lookup(vm, key)? {
8185
if let Some(entry) = &self.entries[index] {
8286
Ok(Some(entry.value.clone()))
@@ -114,7 +118,7 @@ impl Dict {
114118
self.len() == 0
115119
}
116120

117-
pub fn get_items(&self) -> Vec<(PyObjectRef, PyObjectRef)> {
121+
pub fn get_items(&self) -> Vec<(PyObjectRef, T)> {
118122
self.entries
119123
.iter()
120124
.filter(|e| e.is_some())

vm/src/stdlib/os.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,35 @@ fn os_write(fd: PyIntRef, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
142142
Ok(vm.ctx.new_int(written))
143143
}
144144

145-
fn os_remove(path: PyStringRef, vm: &VirtualMachine) -> PyResult {
146-
match fs::remove_file(&path.value) {
147-
Ok(_) => (),
148-
Err(s) => return Err(vm.new_os_error(s.to_string())),
149-
}
145+
fn os_remove(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
146+
fs::remove_file(&path.value).map_err(|s| vm.new_os_error(s.to_string()))
147+
}
150148

151-
Ok(vm.get_none())
149+
fn os_mkdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
150+
fs::create_dir(&path.value).map_err(|s| vm.new_os_error(s.to_string()))
151+
}
152+
153+
fn os_mkdirs(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
154+
fs::create_dir_all(&path.value).map_err(|s| vm.new_os_error(s.to_string()))
155+
}
156+
157+
fn os_rmdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> {
158+
fs::remove_dir(&path.value).map_err(|s| vm.new_os_error(s.to_string()))
159+
}
160+
161+
fn os_listdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult {
162+
match fs::read_dir(&path.value) {
163+
Ok(iter) => {
164+
let res: PyResult<Vec<PyObjectRef>> = iter
165+
.map(|entry| match entry {
166+
Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())),
167+
Err(s) => Err(vm.new_os_error(s.to_string())),
168+
})
169+
.collect();
170+
Ok(vm.ctx.new_list(res?))
171+
}
172+
Err(s) => Err(vm.new_os_error(s.to_string())),
173+
}
152174
}
153175

154176
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
@@ -168,6 +190,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
168190
"write" => ctx.new_rustfunc(os_write),
169191
"remove" => ctx.new_rustfunc(os_remove),
170192
"unlink" => ctx.new_rustfunc(os_remove),
193+
"mkdir" => ctx.new_rustfunc(os_mkdir),
194+
"mkdirs" => ctx.new_rustfunc(os_mkdirs),
195+
"rmdir" => ctx.new_rustfunc(os_rmdir),
196+
"listdir" => ctx.new_rustfunc(os_listdir),
171197
"name" => ctx.new_str(os_name),
172198
"O_RDONLY" => ctx.new_int(0),
173199
"O_WRONLY" => ctx.new_int(1),

0 commit comments

Comments
 (0)