fix: thunk & function

This commit is contained in:
2025-05-15 11:11:11 +08:00
parent bcb6c48cfa
commit 2293b9e2de
10 changed files with 76 additions and 112 deletions

View File

@@ -1,14 +1,15 @@
use std::cell::OnceCell;
use std::rc::Rc;
use ecow::EcoString;
use itertools::Itertools;
use rpds::HashTrieMap;
use derive_more::Constructor;
use crate::bytecode::OpCodes;
use crate::bytecode::Func as BFunc;
use crate::error::Result;
use crate::ir;
use crate::ty::internal::Value;
use crate::vm::{CapturedEnv, Env, VM};
use crate::ty::internal::{Thunk, Value};
use crate::vm::{Env, VM};
#[derive(Debug, Clone)]
pub enum Param {
@@ -40,24 +41,23 @@ impl From<ir::Param> for Param {
}
}
pub type JITFunc<'vm> = unsafe extern "C" fn(vm: *mut VM<'vm, '_>, *mut Env<'vm>, *mut Value<'vm>) -> Value<'vm>;
pub type JITFunc<'vm> = unsafe extern "C" fn(vm: *mut VM<'_>, *mut Env<'vm>, *mut Value<'vm>) -> Value<'vm>;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Constructor)]
pub struct Func<'vm> {
pub env: OnceCell<CapturedEnv<'vm>>,
pub param: Param,
pub opcodes: OpCodes,
pub func: &'vm BFunc,
pub env: Rc<Env<'vm>>,
pub compiled: Option<JITFunc<'vm>>
}
impl<'vm> Func<'vm> {
pub fn call(&'vm self, vm: &VM<'vm, '_>, arg: Value<'vm>) -> Result<Value<'vm>> {
pub fn call(self, vm: &'vm VM<'_>, arg: Value<'vm>) -> Result<Value<'vm>> {
use Param::*;
let env = self.env.get().unwrap().clone().released();
let env = Rc::new(self.env.as_ref().clone());
match &self.param {
Ident(ident) => env.enter(HashTrieMap::new().insert(ident.clone().into(), arg)),
match self.func.param.clone() {
Ident(ident) => env.enter(HashTrieMap::new().insert(ident.into(), arg)),
Formals {
formals,
ellipsis,
@@ -79,7 +79,7 @@ impl<'vm> Func<'vm> {
let formal = formal.clone().into();
let arg = arg
.select(&formal)
.or_else(|| default.map(|idx| Value::ThunkRef(vm.get_thunk(idx))))
.or_else(|| default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx)))))
.unwrap();
new.insert_mut(formal, arg);
}
@@ -90,7 +90,7 @@ impl<'vm> Func<'vm> {
}
}
vm.eval(self.opcodes.clone(), env)
vm.eval(self.func.opcodes.clone(), env)
}
}