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
265
266
//! # Language expressions;
//!
//! These are used for condition expressions and scripts
use crate::bpmn::schema::{FormalExpression, ScriptTask};
use crate::data_object::DataObject;
use crate::sys::task;
use async_trait::async_trait;
use thiserror::Error;

#[cfg(feature = "rhai")]
pub mod rhai;

/// Information about the engine
pub trait EngineInfo {
    /// Returns language's namespace
    fn namespace(&self) -> Option<&str> {
        None
    }
    /// Returns language's mime type
    fn mime_type(&self) -> Option<&str> {
        None
    }
}

/// Engine context provider
pub trait EngineContextProvider {
    /// Context type
    type Context: EngineContext;

    /// Returns a newly initialized context
    fn new_context(&self) -> Self::Context;
}

/// Engine execution context
pub trait EngineContext {
    /// Clears the context
    fn clear(&mut self) -> &mut Self;

    /// Sets a value in the context
    fn set(&mut self, name: &str, value: Box<dyn DataObject>) -> &mut Self;
}

/// Language for expressions and/or scripts
#[async_trait]
pub trait Engine<Expr>: EngineInfo + EngineContextProvider + Send + Sync
where
    Expr: Send + Sync + Unpin + 'static,
{
    /// Evaluates a program using an engine for a given language
    async fn eval<T>(
        &self,
        code: &Expr,
        context: &mut <Self as EngineContextProvider>::Context,
    ) -> Result<T, EvaluationError>
    where
        T: Send + Sync + Clone + 'static;
}

/// Evaluation error
#[derive(Error, Debug)]
pub enum EvaluationError {
    /// Given language is not supported
    #[error("unsupported language {language:?}")]
    UnsupportedLanguage { language: Option<String> },
    #[error("evaluation error {error:?}")]
    /// Error during evaluation
    EvaluationError {
        #[from]
        error: Box<dyn std::error::Error + Send>,
    },
    /// Result type mismatch
    #[error("result type mismatch, expected {expected}, got {got}")]
    ResultTypeError { expected: String, got: String },
    /// Empty expression or script
    #[error("empty expression or script")]
    Empty,
    #[error("execution error {0:?}")]
    ExecutionError(ExecutionError),
}

#[derive(Debug)]
pub enum ExecutionError {
    Cancelled,
    Panicked,
    UnknownCause,
}

#[cfg(not(target_arch = "wasm32"))]
impl From<task::JoinError> for EvaluationError {
    fn from(err: task::JoinError) -> Self {
        if err.is_cancelled() {
            return Self::ExecutionError(ExecutionError::Cancelled);
        }
        if err.is_panic() {
            return Self::ExecutionError(ExecutionError::Panicked);
        }
        Self::ExecutionError(ExecutionError::UnknownCause)
    }
}

#[cfg(target_arch = "wasm32")]
impl From<task::JoinError> for EvaluationError {
    fn from(_err: task::JoinError) -> Self {
        Self::ExecutionError(ExecutionError::UnknownCause)
    }
}

pub struct MultiLanguageEngine {
    default_namespace: Option<String>,
    default_mime_type: Option<String>,
    #[cfg(feature = "rhai")]
    pub rhai: rhai::Rhai,
}

impl MultiLanguageEngine {
    /// Creates a new default engine
    pub fn new() -> Self {
        Self {
            #[cfg(not(feature = "rhai"))]
            default_namespace: None,
            #[cfg(feature = "rhai")]
            default_namespace: Some(rhai::RHAI_URI.to_owned()),
            #[cfg(not(feature = "rhai"))]
            default_mime_type: None,
            #[cfg(feature = "rhai")]
            default_mime_type: Some(rhai::RHAI_MIME.to_owned()),
            #[cfg(feature = "rhai")]
            rhai: Default::default(),
        }
    }

    /// Sets default namespace
    pub fn set_default_namespace<S: Into<String>>(&mut self, ns: S) {
        self.default_namespace.replace(ns.into());
    }

    /// Sets default mime type
    pub fn set_default_mime_type<S: Into<String>>(&mut self, mime_type: S) {
        self.default_mime_type.replace(mime_type.into());
    }
}

impl EngineInfo for MultiLanguageEngine {}

#[async_trait]
impl Engine<FormalExpression> for MultiLanguageEngine {
    async fn eval<T>(
        &self,
        code: &FormalExpression,
        context: &mut <Self as EngineContextProvider>::Context,
    ) -> Result<T, EvaluationError>
    where
        T: Send + Sync + Clone + 'static,
    {
        let language = match code.language.as_ref() {
            None => self.default_namespace.as_ref(),
            Some(language) => Some(language),
        };
        match language {
            None => return Err(EvaluationError::UnsupportedLanguage { language: None }),
            Some(language) => {
                #[cfg(feature = "rhai")]
                if let Some(ns) = self.rhai.namespace() {
                    if ns == language {
                        return self.rhai.eval(code, &mut context.rhai_context).await;
                    }
                }
                return Err(EvaluationError::UnsupportedLanguage {
                    language: Some(language.into()),
                });
            }
        }
    }
}

#[async_trait]
impl Engine<ScriptTask> for MultiLanguageEngine {
    async fn eval<T>(
        &self,
        code: &ScriptTask,
        context: &mut <Self as EngineContextProvider>::Context,
    ) -> Result<T, EvaluationError>
    where
        T: Send + Sync + Clone + 'static,
    {
        let language = match code.script_format.as_ref() {
            None => self.default_mime_type.as_ref(),
            Some(language) => Some(language),
        };
        match language {
            None => return Err(EvaluationError::UnsupportedLanguage { language: None }),
            Some(language) => {
                #[cfg(feature = "rhai")]
                if let Some(t) = self.rhai.mime_type() {
                    if t == language {
                        return self.rhai.eval(code, &mut context.rhai_context).await;
                    }
                }
                return Err(EvaluationError::UnsupportedLanguage {
                    language: Some(language.into()),
                });
            }
        }
    }
}

pub struct MultiLanguageEngineContext {
    #[cfg(feature = "rhai")]
    rhai_context: rhai::Context,
}

impl EngineContext for MultiLanguageEngineContext {
    fn clear(&mut self) -> &mut Self {
        #[cfg(feature = "rhai")]
        self.rhai_context.clear();
        self
    }

    /// Sets a value in the context
    fn set(&mut self, name: &str, value: Box<dyn DataObject>) -> &mut Self {
        #[cfg(feature = "rhai")]
        self.rhai_context.set(name, dyn_clone::clone_box(&*value));
        self
    }
}

impl EngineContextProvider for MultiLanguageEngine {
    type Context = MultiLanguageEngineContext;

    fn new_context(&self) -> Self::Context {
        MultiLanguageEngineContext {
            #[cfg(feature = "rhai")]
            rhai_context: self.rhai.new_context(),
        }
    }
}

impl Default for MultiLanguageEngine {
    fn default() -> Self {
        MultiLanguageEngine::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bpxe_internal_macros as bpxe_im;

    use crate::language::rhai::*;

    #[bpxe_im::test]
    async fn dispatch_to_rhai_evaluation() {
        let e = MultiLanguageEngine::new();
        assert!(e
            .eval::<bool>(
                &FormalExpression {
                    language: Some(RHAI_URI.to_string()),
                    content: Some("true".into()),
                    ..Default::default()
                },
                &mut e.new_context()
            )
            .await
            .unwrap());
    }
}