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
//! Builtins live here, such as Object, String, Math, etc.

pub mod array;
pub mod bigint;
pub mod boolean;
pub mod console;
pub mod error;
pub mod function;
pub mod global_this;
pub mod infinity;
pub mod json;
pub mod math;
pub mod nan;
pub mod number;
pub mod object;
pub mod property;
pub mod regexp;
pub mod string;
pub mod symbol;
pub mod undefined;
pub mod value;

pub(crate) use self::{
    array::Array,
    bigint::BigInt,
    boolean::Boolean,
    error::{Error, RangeError, ReferenceError, TypeError},
    global_this::GlobalThis,
    infinity::Infinity,
    json::Json,
    math::Math,
    nan::NaN,
    number::Number,
    regexp::RegExp,
    string::String,
    symbol::Symbol,
    undefined::Undefined,
    value::{ResultValue, Value},
};

/// Initializes builtin objects and functions
#[inline]
pub fn init(global: &Value) {
    let globals = [
        // The `Function` global must be initialized before other types.
        function::init,
        object::init,
        Array::init,
        BigInt::init,
        Boolean::init,
        Json::init,
        Math::init,
        Number::init,
        RegExp::init,
        String::init,
        Symbol::init,
        console::init,
        // Global error types.
        Error::init,
        RangeError::init,
        ReferenceError::init,
        TypeError::init,
        // Global properties.
        NaN::init,
        Infinity::init,
        GlobalThis::init,
        Undefined::init,
    ];

    match global {
        Value::Object(ref global_object) => {
            for init in &globals {
                let (name, value) = init(global);
                global_object.borrow_mut().insert_field(name, value);
            }
        }
        _ => unreachable!("expect global object"),
    }
}