diff options
| author | Cody <cody@codyq.dev> | 2023-05-24 00:23:13 -0500 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-05-24 00:23:13 -0500 |
| commit | 9c41dd96cd3652ce9c46307184e8055704655338 (patch) | |
| tree | 07071d74da7398593fc5b9cfe5c4df0229ee4eb0 /crates/sloth_vm/src/sloth_std/file.rs | |
| parent | 2418d68631f6e338251f2d988de2d3fde206982b (diff) | |
| parent | df00e9ff2c2b563c79beb71ba8c510233b04f3bf (diff) | |
| download | sloth-9c41dd96cd3652ce9c46307184e8055704655338.tar.gz | |
Merge branch 'master' into compiler
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/file.rs')
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/file.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/crates/sloth_vm/src/sloth_std/file.rs b/crates/sloth_vm/src/sloth_std/file.rs new file mode 100644 index 0000000..b0b476a --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/file.rs @@ -0,0 +1,83 @@ +use std::fs; + +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::{Object, ObjectType, Primitive}; +use crate::VM; + +fn file_read(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let Some(Primitive::Object(ptr)) = args.get(0).cloned() else { + return Err(native::Error::InvalidArgument); + }; + + let object = vm + .objects() + .get(ptr as usize) + .ok_or(native::Error::InvalidArgument)?; + + let ObjectType::String(str) = &object.typ else { + return Err(native::Error::InvalidArgument); + }; + + let contents = fs::read_to_string(str).expect("IO Error: Failed to read file!"); + + let object = Object::new(ObjectType::String(contents)); + let ptr = vm.objects_mut().allocate(object); + + Ok(Primitive::Object(ptr as u32)) +} + +pub const FILE_READ: NativeFunction = NativeFunction { + name: "file$read", + function: file_read, + arity: 1, + returns_value: true, + doc: Some( + "NativeFunction file$read: \n\targs: path (str)\n\tdesc: Returns the contents of a file \ + at <path>\n\tExample: `var todo = file$read('/home/sloth/todo.txt'); # Assuming the \ + contents of todo.txt are 'Take a nap' then todo = 'Take a nap'`", + ), +}; + +fn file_write(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let Some(Primitive::Object(path_ptr)) = args.get(0).cloned() else { + return Err(native::Error::InvalidArgument); + }; + + let path_object = vm + .objects() + .get(path_ptr as usize) + .ok_or(native::Error::InvalidArgument)?; + + let ObjectType::String(path) = &path_object.typ else { + return Err(native::Error::InvalidArgument); + }; + + let Some(Primitive::Object(content_ptr)) = args.get(1).cloned() else { + return Err(native::Error::InvalidArgument); + }; + + let content_object = vm + .objects() + .get(content_ptr as usize) + .ok_or(native::Error::InvalidArgument)?; + + let ObjectType::String(content) = &content_object.typ else { + return Err(native::Error::InvalidArgument); + }; + + let _ = fs::write(path, content); + + Ok(Primitive::Empty) +} + +pub const FILE_WRITE: NativeFunction = NativeFunction { + name: "file$write", + function: file_write, + arity: 2, + returns_value: false, + doc: Some( + "NativeFunction file$write: \n\targs: path (str), content (str)\n\tdesc: Writes <content> \ + to file at <path>\n\tExample: `file$write('/home/sloth/todo.txt', 'Take a nap'); # \ + todo.txt now contains the string 'Take a nap'`", + ), +}; |
