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
//! Crabzilla provides a _simple_ interface for running JavaScript modules alongside Rust code.
//! <br>
//! # Example
//! ```
//! use crabzilla::*;
//! use std::io::stdin;
//! 
//! #[import_fn(name="read", scope="Stdin")]
//! fn read_from_stdin() -> Value {
//!     let mut buffer = String::new();
//!     println!("Type your name: ");
//!     stdin().read_line(&mut buffer)?;
//!     buffer.pop();
//!     Value::String(buffer)
//! }
//! 
//! #[import_fn(name="sayHello", scope="Stdout")]
//! fn say_hello(args: Vec<Value>) {
//!     if let Some(string) = args.get(0) {
//!         if let Value::String(string) = string {
//!             println!("Hello, {}", string);
//!         }
//!     }
//! }
//! 
//! #[tokio::main]
//! async fn main() {
//!     let mut runtime = runtime! {
//!         read_from_stdin,
//!         say_hello,
//!     };
//!     if let Err(error) = runtime.load_module("./module.js").await {
//!         eprintln!("{}", error);
//!     }
//! }
//! ```
//! In `module.js`:
//! ```
//! const user = Stdin.read();
//! Stdout.sayHello(user);
//! ```
use deno_core::{
    FsModuleLoader,
    JsRuntime,
    ModuleSpecifier,
    OpState,
    OpFn,
    ZeroCopyBuf,
    // json_op_async,
    json_op_sync,
    // BufVec,
};
use std::rc::Rc;
// use std::cell::RefCell;
// use futures::Future;
pub use deno_core::serde_json::value::Value;
pub use deno_core::error::AnyError;
pub use deno_core::error::custom_error;
pub use import_fn::import_fn;

fn get_args(value: &Value) -> Vec<Value> {
    if let Value::Object(map) = &value {
        if let Some(Value::Array(args)) = &map.get("args") {
            return args.to_owned();
        }
    }
    unreachable!();
}

/// Represents an imported Rust function.
pub struct ImportedFn {
    op_fn: Box<OpFn>,
    name: String,
    scope: Option<String>,
    sync: bool,
}

/// Receives a Rust function and returns a structure that can be imported in to a runtime.
pub fn create_sync_fn<F>(imported_fn: F, name: &str, scope: Option<String>) -> ImportedFn
    where
    F: Fn(Vec<Value>) -> Result<Value, AnyError> + 'static,
{
    let op_fn = json_op_sync(
        move |_state: &mut OpState, value: Value, _buffer: &mut [ZeroCopyBuf]| -> Result<Value, AnyError> {
            imported_fn(get_args(&value))
        }
    );
    ImportedFn {
        op_fn,
        name: name.to_string(),
        scope,
        sync: true,
    }
}

// Unsupported until async closures are stable.
// pub fn create_async_fn<F, R>(imported_fn: F, name: &str, scope: Option<String>) -> ImportedFn
//     where
//     F: Fn(Vec<Value>) -> R + 'static,
//     R: Future<Output = Result<Value, AnyError>> + 'static,
// {
//     let op_fn = json_op_async(
//         move |_state: Rc<RefCell<OpState>>, value: Value, _buffer: BufVec| -> R {
//             imported_fn(get_args(&value))
//         }
//     );
//     ImportedFn {
//         op_fn,
//         name: name.to_string(),
//         scope,
//         sync: false,
//     }
// }

struct ImportedName {
    name: String,
    scope: Option<String>,
    sync: bool,
}

/// Represents a JavaScript runtime instance.
pub struct Runtime {
    runtime: JsRuntime,
    imported_names: Vec<ImportedName>,
    scopes: Vec<String>,
}

impl Runtime {
    pub fn new() -> Self {
        let runtime = JsRuntime::new(deno_core::RuntimeOptions{
            module_loader: Some(Rc::new(FsModuleLoader)),
            ..Default::default()
        });
        let imported_names = vec![];
        let scopes = vec![];
        Runtime {
            runtime,
            imported_names,
            scopes,
        }
    }

    pub fn import<F>(&mut self, imported_fn: F) -> ()
    where F: Fn() -> ImportedFn {
        let import_fn = imported_fn();
        if let Some(scope) = &import_fn.scope {
            self.scopes.push(scope.clone());
        }
        self.runtime.register_op(&import_fn.name, import_fn.op_fn);
        self.imported_names.push(ImportedName {
            name: import_fn.name,
            scope: import_fn.scope,
            sync: import_fn.sync,
        });
    }

    pub fn importing_finished(&mut self) {
        let mut scope_definitions = String::new();
        for scope in self.scopes.iter() {
            scope_definitions.push_str(&format!("        window[{:?}] = {{}};\n", scope));
        }
        let mut name_definitions = String::new();
        for import in self.imported_names.iter() {
            let scope = match &import.scope {
                Some(scope) => format!("window[{:?}][{:?}]", scope, import.name),
                None => format!("window[{:?}]", import.name),
            };
            let command = match import.sync {
                true => "jsonOpSync",
                false => "jsonOpAsync",
            };
            name_definitions.push_str(&format!("        {} = (...args) => Deno.core.{}({:?}, {{args}});\n", scope, command, import.name));
        }
        let js_source = format!(r#"
"use strict";
(
    (window) => {{
        Deno.core.ops();
        Deno.core.registerErrorClass("Error", window.Error);
{}{}    }}
)(this);"#,
            scope_definitions,
            name_definitions,
        );
        self.runtime.execute("rust:core.js", &js_source).expect("runtime exporting");
    }

    pub async fn load_module(&mut self, path_str: &str) -> Result<(), AnyError> {
        let specifier = ModuleSpecifier::resolve_path(path_str)?;
        let id = self.runtime.load_module(&specifier, None).await?;
        self.runtime.mod_evaluate(id).await
    }
}

/// Creates a runtime object and imports a list of functions.
///
/// # Example
/// ```
/// #[import_fn]
/// fn foo() {
///   // Do something
/// }
///
/// #[import_fn]
/// fn bar() {
///   // Do something else
/// }
///
/// let mut runtime = runtime! {
///    foo,
///    bar,
///  };
/// ```
#[macro_export]
macro_rules! runtime {
    ($($fn:ident),* $(,)?) => {
        {
            let mut runtime = crabzilla::Runtime::new();
            $(
                runtime.import($fn);
            )*
            runtime.importing_finished();
            runtime
        }
    }
}

/// Throws an error with a custom message in an imported Rust function.
#[macro_export]
macro_rules! throw {
    ($message:expr) => {
        return Err(crabzilla::custom_error("Error", $message));
    }
}