From 94ef78c6703bba026fe7cfc06698d66e83a130ba Mon Sep 17 00:00:00 2001 From: Akshanabha Chakraborty Date: Sun, 2 Mar 2025 21:49:45 +0530 Subject: [PATCH] new intl --- interpreter/src/ipreter.rs | 9 ++++---- interpreter/src/lib.rs | 2 +- interpreter/src/types/mod.rs | 40 ++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/interpreter/src/ipreter.rs b/interpreter/src/ipreter.rs index 0a7c7c0..2815d4c 100644 --- a/interpreter/src/ipreter.rs +++ b/interpreter/src/ipreter.rs @@ -155,7 +155,8 @@ pub(crate) unsafe fn tok_parse<'a>( heap .remove(var) .expect("Cannot find variable") - .expect("Cannot find variable"), + .expect("Cannot find variable") + .into(), ); None @@ -211,7 +212,7 @@ pub(crate) unsafe fn tok_parse<'a>( let runt = opt.rem_r_runtime(); if val_type && opt.r_val.is_some() { - let _ = heap.set(Cow::Borrowed(to_set), opt.r_val.unwrap()); + let _ = heap.set(Cow::Borrowed(to_set), opt.r_val()); } else if val_type && runt.is_some() { let _ = set_runtime_val( heap, @@ -230,7 +231,7 @@ pub(crate) unsafe fn tok_parse<'a>( let runt = opt.rem_r_runtime(); if val_type && opt.r_val.is_some() { - let _ = heap.set(Cow::Borrowed(to_set), opt.r_val.unwrap()); + let _ = heap.set(Cow::Borrowed(to_set), opt.r_val()); } else if val_type && runt.is_some() { let _ = set_runtime_val( heap, @@ -264,7 +265,7 @@ pub(crate) unsafe fn tok_parse<'a>( let runt = opt.rem_r_runtime(); if val_type && opt.r_val.is_some() { - let _ = heap.set(Cow::Borrowed(to_set), opt.r_val.unwrap()); + let _ = heap.set(Cow::Borrowed(to_set), opt.r_val()); } else if val_type && runt.is_some() { let _ = set_runtime_val( heap, diff --git a/interpreter/src/lib.rs b/interpreter/src/lib.rs index 959addc..d93d76f 100644 --- a/interpreter/src/lib.rs +++ b/interpreter/src/lib.rs @@ -28,7 +28,7 @@ pub use tokio::*; pub use lealang_chalk_rs::Chalk; -pub static VERSION_INT: u16 = 4; +pub static VERSION_INT: u16 = 5; pub trait Package { fn name(&self) -> &'static [u8]; diff --git a/interpreter/src/types/mod.rs b/interpreter/src/types/mod.rs index 8e2d2e1..9a1e1bc 100644 --- a/interpreter/src/types/mod.rs +++ b/interpreter/src/types/mod.rs @@ -21,16 +21,27 @@ use tokio::{runtime::Handle, sync::mpsc::UnboundedReceiver, task::JoinHandle}; use crate::runtime::RuntimeValue; +pub enum RetBufValue { + Future(Pin + Send>>), + Heap(BufValue) +} + +impl From for RetBufValue { + fn from(item: BufValue) -> Self { + Self::Heap(item) + } +} + pub struct Options { pub pre: *const str, - pub r_val: Option, + pub r_val: Option, pub runtime: *const Handle, r_runtime: Option, } impl Debug for Options { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("Options {{ r_val: {:?} }}", &self.r_val)) + f.write_fmt(format_args!("Options {{ }}")) } } @@ -44,15 +55,32 @@ impl Options { } } - pub fn spawn + Send + 'static>(&self, future: F) -> JoinHandle { - unsafe { &*self.runtime }.spawn(future) + fn spawn(&self) -> &Handle { + unsafe { &*self.runtime } + } + + pub fn set_return_future(&mut self, val: Pin + Send>>) { + self.r_val = Some(RetBufValue::Future(val)); } pub fn set_return_val(&mut self, val: BufValue) { - self.r_val = Some(val); + self.r_val = Some(RetBufValue::Heap(val)); + } + + pub(crate) fn r_val(self) -> BufValue { + let rt = self.spawn(); + let rt = rt.clone(); + let val = self.r_val; + + match val.expect("Error") { + RetBufValue::Future(x) => { + rt.block_on(x) + }, + RetBufValue::Heap(x) => x + } } - pub fn rem_r_runtime(&mut self) -> Option { + pub(crate) fn rem_r_runtime(&mut self) -> Option { let mut rt = self.r_runtime.take()?; rt.r#type = format!("{}/{}", unsafe { &*self.pre }, rt.r#type);