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
44
45
46
47
48
49
50
51
52
use core::fmt;
use debris_error::LangResult;
use std::rc::Rc;

use crate::{
    class::{Class, ClassKind, ClassRef},
    impl_class,
    memory::MemoryLayout,
    objects::{
        obj_class::HasClass, obj_function::FunctionContext, obj_function_ref::ObjFunctionRef,
    },
    type_context::TypeContext,
    NativeFunctionId, ObjectPayload, Type,
};

use super::obj_function::FunctionClassRef;

#[derive(Debug, PartialEq, Eq)]
pub struct ObjNativeFunction {
    pub function_id: NativeFunctionId,
    pub(crate) signature: FunctionClassRef,
}

impl_class! {ObjNativeFunction, Type::Function, {
    "compile" => |ctx: &mut FunctionContext, this: &ObjNativeFunction| -> LangResult<ObjFunctionRef> {
        let block_id = ctx.compile_native_function(this.function_id)?;
        Ok(ObjFunctionRef::new(block_id))
    }
}}

impl ObjNativeFunction {}

impl fmt::Display for ObjNativeFunction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.signature, f)
    }
}

impl ObjectPayload for ObjNativeFunction {
    fn memory_layout(&self) -> &MemoryLayout {
        &MemoryLayout::Unsized
    }

    fn create_class(&self, ctx: &TypeContext) -> ClassRef {
        let kind = ClassKind::Function(Rc::clone(&self.signature));
        let class = Class {
            kind,
            properties: Self::create_properties(ctx).into(),
        };
        class.into()
    }
}