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
use crate::{qjs, Context, Result, Runtime};
use std::marker::PhantomData;

/// The internal trait to add JS builting
pub trait Intrinsic {
    /// # Safety
    /// Do not need implement it yourself instead you may use predefined intrinsics from [`intrinsic`] module.
    unsafe fn add_intrinsic(ctx: *mut qjs::JSContext);
}

/// Used for building a [`Context`](struct.Context.html) with a specific set of intrinsics
pub struct ContextBuilder<I>(PhantomData<I>);

macro_rules! intrinsic_impls {
    (@builtin: $($(#[$meta:meta])* $name:ident $func:ident $(($($args:expr),*))*,)*) => {
        $(
            $(#[$meta])*
            pub struct $name;

            impl Intrinsic for $name {
                unsafe fn add_intrinsic(ctx: *mut qjs::JSContext) {
                    qjs::$func(ctx $(, $($args),*)*);
                }
            }
        )*
    };

    (@tuple: $($($name:ident)*,)*) => {
        $(
            impl<$($name,)*> Intrinsic for ($($name,)*)
            where
                $($name: Intrinsic,)*
            {
                unsafe fn add_intrinsic(_ctx: *mut qjs::JSContext) {
                    $($name::add_intrinsic(_ctx);)*
                }
            }
        )*
    }
}

/// A marker types for intrinsic
///
/// You can select just you need only. If `lto = true` any unused code will be drop by link-time optimizer.
pub mod intrinsic {
    use super::{qjs, Intrinsic};

    intrinsic_impls! {
        @builtin:
        /// Add base objects support
        BaseObjects JS_AddIntrinsicBaseObjects,
        /// Add Date object support
        Date JS_AddIntrinsicDate,
        /// Add evaluation support
        Eval JS_AddIntrinsicEval,
        /// Add string normalization
        StringNormalize JS_AddIntrinsicStringNormalize,
        /// Add RegExp compiler
        RegExpCompiler JS_AddIntrinsicRegExpCompiler,
        /// Add RegExp object support
        RegExp JS_AddIntrinsicRegExp,
        /// Add JSON parse and stringify
        Json JS_AddIntrinsicJSON,
        /// Add Proxy object support
        Proxy JS_AddIntrinsicProxy,
        /// Add MapSet object support
        MapSet JS_AddIntrinsicMapSet,
        /// Add Typed Arrays support
        TypedArrays JS_AddIntrinsicTypedArrays,
        /// Add Promise object support
        Promise JS_AddIntrinsicPromise,
        /// Add BigInt support
        BigInt JS_AddIntrinsicBigInt,
        /// Add BigFloat support
        BigFloat JS_AddIntrinsicBigFloat,
        /// Add BigDecimal support
        BigDecimal JS_AddIntrinsicBigDecimal,
        /// Add operator overloading support
        Operators JS_AddIntrinsicOperators,
        /// Enable bignum extension
        BignumExt JS_EnableBignumExt (1),
    }

    /// An alias for [`BaseObjects`]
    pub type Base = BaseObjects;

    /// Add none intrinsics
    pub type None = ();

    /// Add all intrinsics
    pub type All = (
        Base,
        Date,
        Eval,
        StringNormalize,
        RegExpCompiler,
        RegExp,
        Json,
        Proxy,
        MapSet,
        TypedArrays,
        Promise,
        BigInt,
        BigFloat,
        BigDecimal,
        Operators,
        BignumExt,
    );
}

intrinsic_impls! {
    @tuple:
    ,
    A,
    A B,
    A B C,
    A B C D,
    A B C D E,
    A B C D E F,
    A B C D E F G,
    A B C D E F G H,
    A B C D E F G H I,
    A B C D E F G H I J,
    A B C D E F G H I J K,
    A B C D E F G H I J K L,
    A B C D E F G H I J K L M,
    A B C D E F G H I J K L M N,
    A B C D E F G H I J K L M N O,
    A B C D E F G H I J K L M N O P,
    A B C D E F G H I J K L M N O P R,
}

impl Default for ContextBuilder<()> {
    fn default() -> Self {
        ContextBuilder(PhantomData)
    }
}

impl<I: Intrinsic> ContextBuilder<I> {
    pub fn with<J: Intrinsic>(self) -> ContextBuilder<(I, J)> {
        ContextBuilder(PhantomData)
    }

    pub fn build(self, runtime: &Runtime) -> Result<Context> {
        Context::custom::<I>(runtime)
    }
}