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
use crate::runtime::{Variant, Gc};
use crate::runtime::strings::StringSymbol;
use crate::runtime::module::{Namespace, GlobalEnv, Access};
use crate::runtime::function::{NativeFunction, Signature, Parameter};
use crate::runtime::errors::ExecResult;
use std::time::SystemTime;
pub fn create_prelude() -> Gc<GlobalEnv> {
let env = GlobalEnv::new();
let time = native_function!(time, env, _ => {
let time = SystemTime::UNIX_EPOCH
.elapsed()
.unwrap()
.as_secs_f64();
Ok(Variant::from(time))
});
let radians = native_function!(radians, env, this, params(degrees) => {
let result = degrees.as_float()?/180.0 * this.env().borrow().lookup(&StringSymbol::intern("PI"))?.as_float()?;
Ok(Variant::from(result))
});
let add_example = native_function!(add_example, env, _,
params(a),
defaults(b = 1),
variadic(varargs) => {
println!("{:?}", varargs);
a.apply_add(b)
});
namespace!(env.borrow_mut(), {
let PI = core::f64::consts::PI;
fun _ = time;
fun _ = radians;
fun _ = add_example;
});
env
}