-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
264 lines (224 loc) · 6.13 KB
/
mod.rs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
mod alloc;
mod fns;
mod heap;
mod heap_wrap;
use std::{
any::Any,
collections::HashMap,
fmt::Debug,
future::Future,
marker::PhantomData,
ops::{Deref, DerefMut},
pin::Pin,
task::{Context, Poll},
};
pub use alloc::*;
pub use fns::*;
pub use heap::*;
pub use heap_wrap::*;
use tokio::{runtime::Handle, sync::mpsc::UnboundedReceiver, task::JoinHandle};
use crate::runtime::RuntimeValue;
pub enum RetBufValue {
Future(Pin<Box<dyn Future<Output = BufValue> + Send>>),
Heap(BufValue)
}
impl From<BufValue> for RetBufValue {
fn from(item: BufValue) -> Self {
Self::Heap(item)
}
}
pub struct Options {
pub pre: *const str,
pub r_val: Option<RetBufValue>,
pub runtime: *const Handle,
r_runtime: Option<RuntimeValue>,
}
impl Debug for Options {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("Options {{ <inner> }}"))
}
}
impl Options {
pub fn new(rt: *const Handle) -> Self {
Self {
pre: "" as _,
r_val: None,
r_runtime: None,
runtime: rt,
}
}
fn spawn(&self) -> &Handle {
unsafe { &*self.runtime }
}
pub fn set_return_future(&mut self, val: Pin<Box<dyn Future<Output = BufValue> + Send>>) {
self.r_val = Some(RetBufValue::Future(val));
}
pub fn set_return_val(&mut self, val: BufValue) {
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(crate) fn rem_r_runtime(&mut self) -> Option<RuntimeValue> {
let mut rt = self.r_runtime.take()?;
rt.r#type = format!("{}/{}", unsafe { &*self.pre }, rt.r#type);
Some(rt)
}
pub fn set_r_runtime(&mut self, val: RuntimeValue) {
self.r_runtime = Some(val);
}
}
#[derive(Debug)]
pub struct AnyWrapper(pub Box<dyn Any>);
impl Deref for AnyWrapper {
type Target = dyn Any;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl PartialEq for AnyWrapper {
fn eq(&self, _other: &Self) -> bool {
false
}
}
#[allow(non_camel_case_types)]
#[derive(PartialEq, Debug)]
pub enum BufValue {
Int(i64),
U_Int(u64),
Float(f64),
Str(String),
Bool(bool),
Array(Vec<Self>),
Object(HashMap<String, Box<Self>>),
Faillable(Result<Box<Self>, String>),
Pointer(*const Self),
PointerMut(*mut Self),
Runtime(AnyWrapper),
AsyncTask(AppliesEq<JoinHandle<Self>>),
Listener(AppliesEq<UnboundedReceiver<Self>>),
RuntimeRaw(&'static str, AppliesEq<RawRTValue>),
}
unsafe impl Send for BufValue {}
unsafe impl Sync for BufValue {}
pub struct UnsafeSend<F> {
pub future: F,
pub _marker: PhantomData<*const ()>, // Ensures this type is `!Send` unless we implement `Send`
}
unsafe impl<F> Send for UnsafeSend<F> {}
unsafe impl<F> Sync for UnsafeSend<F> {}
impl<F: Future> Future for UnsafeSend<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe { self.map_unchecked_mut(|s| &mut s.future).poll(cx) }
}
}
pub fn make_unsafe_send_future<F>(fut: F) -> UnsafeSend<F>
where
F: Future,
{
UnsafeSend {
future: fut,
_marker: PhantomData,
}
}
#[derive(Debug)]
pub struct AppliesEq<T>(pub T);
unsafe impl<T> Send for AppliesEq<T> {}
unsafe impl<T> Sync for AppliesEq<T> {}
impl<T> PartialEq for AppliesEq<T> {
fn eq(&self, _: &Self) -> bool {
false
}
}
impl<T> Deref for AppliesEq<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for AppliesEq<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl BufValue {
pub fn type_of(&self) -> String {
match &self {
BufValue::Array(_) => "array".to_string(),
BufValue::Bool(_) => "bool".to_string(),
BufValue::Float(_) => "float".to_string(),
BufValue::Int(_) => "int".to_string(),
BufValue::U_Int(_) => "u_int".to_string(),
BufValue::Object(_) => "object".to_string(),
BufValue::Str(_) => "string".to_string(),
BufValue::Faillable(res) => match res {
Ok(t) => format!("<success {}>", t.type_of()),
Err(t) => format!("<err {}>", &t),
},
BufValue::Runtime(d) => format!("<runtime {:?}>", d.type_id()),
BufValue::Pointer(ptr) => {
if ptr.is_null() {
return "<ptr *ref NULL>".into();
}
unsafe { &**ptr }.type_of()
}
BufValue::PointerMut(ptr) => {
if ptr.is_null() {
return "<ptr *mut NULL>".into();
}
unsafe { &**ptr }.type_of()
}
BufValue::Listener(_) => "<listener ?event>".into(),
BufValue::AsyncTask(t) => {
if t.is_finished() {
"<async recv...\\0>".into()
} else {
"<async pending...>".into()
}
}
BufValue::RuntimeRaw(_, _) => "<runtime rt>".into(),
}
}
pub fn get_vec_mut(&mut self) -> Option<&mut Vec<BufValue>> {
match self {
BufValue::Array(a) => Some(a),
_ => None,
}
}
pub fn gt(&self, other: &BufValue) -> bool {
match (self, other) {
(BufValue::Int(a), BufValue::Int(b)) => a > b,
(BufValue::Int(a), BufValue::U_Int(b)) => (*a as i128) > (*b as i128),
(BufValue::U_Int(a), BufValue::U_Int(b)) => a > b,
(BufValue::U_Int(a), BufValue::Int(b)) => (*a as i128) > (*b as i128),
(BufValue::Float(a), BufValue::Float(b)) => a > b,
_ => false,
}
}
pub fn lt(&self, other: &BufValue) -> bool {
match (self, other) {
(BufValue::Int(a), BufValue::Int(b)) => a < b,
(BufValue::Int(a), BufValue::U_Int(b)) => (*a as i128) < (*b as i128),
(BufValue::U_Int(a), BufValue::U_Int(b)) => a < b,
(BufValue::U_Int(a), BufValue::Int(b)) => (*a as i128) < (*b as i128),
(BufValue::Float(a), BufValue::Float(b)) => a < b,
_ => false,
}
}
pub fn eq(&self, other: &BufValue) -> bool {
match (self, other) {
(BufValue::Int(a), BufValue::U_Int(b)) => (*a as i128) == (*b as i128),
(BufValue::U_Int(a), BufValue::Int(b)) => (*a as i128) == (*b as i128),
_ => self == other,
}
}
}