diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-18 09:08:49 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-18 09:08:49 -0500 |
| commit | b0ffef5124c88d52d81cbd2f8c96c019ce84ad47 (patch) | |
| tree | 2c386ab8d561459723d10f4961cc8a8f6772565e /crates/sloth_vm/src/sloth_std/misc.rs | |
| parent | 19fefeb732d559195edb01ebc36170c0cf9a0308 (diff) | |
| download | sloth-b0ffef5124c88d52d81cbd2f8c96c019ce84ad47.tar.gz | |
Need to write tests for it, but hopefully function docs work now
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/misc.rs')
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/misc.rs | 39 |
1 files changed, 39 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..72be48f --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -0,0 +1,39 @@ +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: None, +}; |
