aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src/sloth_std/misc.rs
blob: 54d6e3702a5e5002b936ffac0c3f74dd147b1564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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`",
    ),
};