diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-27 17:19:54 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-27 17:19:54 -0500 |
| commit | ac8c9abab30d66ff208a623d0a6de8869373a554 (patch) | |
| tree | 8d2d20d9a5312a1e2fad73d9a6ee7294b0c27dc2 /crates/sloth_vm/src/sloth_std/misc.rs | |
| parent | 757e804671ef3dcb2e0f1295e385780b5feae2ca (diff) | |
| parent | af02ccd056754c60131d13d74b9fac0e23b2af31 (diff) | |
| download | sloth-ac8c9abab30d66ff208a623d0a6de8869373a554.tar.gz | |
Merge branch 'standard-library' of github.com-Nic:slothlang/sloth into standard-library
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/misc.rs')
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/misc.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs new file mode 100644 index 0000000..54d6e37 --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -0,0 +1,43 @@ +use super::NATIVE_LIBRARY; +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::{Object, ObjectType, Primitive}; +use crate::VM; + +fn get_doc(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 docs = NATIVE_LIBRARY + .get(str.as_str()) + .ok_or(native::Error::InvalidArgument)? + .doc + .ok_or(native::Error::InvalidArgument)? + .to_string(); + + let object = Object::new(ObjectType::String(docs)); + let ptr = vm.objects_mut().allocate(object); + + Ok(Primitive::Object(ptr as u32)) +} + +pub const GET_DOC: NativeFunction = NativeFunction { + name: "get$doc", + function: get_doc, + arity: 1, + returns_value: true, + doc: Some( + "NativeFunction get_doc: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ + function with name <str>\n\tExample: `var doc = get_doc('wait'); # Returns the \ + documentation of the 'wait' function to doc`", + ), +}; |
