aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src/sloth_std/misc.rs
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-05-24 00:23:13 -0500
committerCody <cody@codyq.dev>2023-05-24 00:23:13 -0500
commit9c41dd96cd3652ce9c46307184e8055704655338 (patch)
tree07071d74da7398593fc5b9cfe5c4df0229ee4eb0 /crates/sloth_vm/src/sloth_std/misc.rs
parent2418d68631f6e338251f2d988de2d3fde206982b (diff)
parentdf00e9ff2c2b563c79beb71ba8c510233b04f3bf (diff)
downloadsloth-9c41dd96cd3652ce9c46307184e8055704655338.tar.gz
Merge branch 'master' into compiler
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/misc.rs')
-rw-r--r--crates/sloth_vm/src/sloth_std/misc.rs39
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..ca08d1d
--- /dev/null
+++ b/crates/sloth_vm/src/sloth_std/misc.rs
@@ -0,0 +1,39 @@
+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::NativeFunction(fnc) = &object.typ else {
+ return Err(native::Error::InvalidArgument);
+ };
+
+ let docs = fnc
+ .doc
+ .expect("Oopsie Poopsie the stringy no worky")
+ .to_string();
+ let object = Object::new(ObjectType::String(docs));
+ let ptr = vm.objects_mut().allocate(object);
+
+ Ok(Primitive::Object(ptr as u32))
+}
+
+pub const DOCS: NativeFunction = NativeFunction {
+ name: "docs",
+ function: get_doc,
+ arity: 1,
+ returns_value: true,
+ doc: Some(
+ "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a function \
+ with name <str>\n\tExample: `var doc = docs('wait'); # Returns the documentation of the \
+ 'wait' function to doc`",
+ ),
+};