-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
mod.rs
208 lines (196 loc) · 5.39 KB
/
mod.rs
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
//! Builtins live here, such as Object, String, Math, etc.
pub mod array;
pub mod array_buffer;
pub mod bigint;
pub mod boolean;
#[cfg(feature = "console")]
pub mod console;
pub mod dataview;
pub mod date;
pub mod error;
pub mod function;
pub mod generator;
pub mod generator_function;
pub mod global_this;
pub mod infinity;
pub mod intl;
pub mod iterable;
pub mod json;
pub mod map;
pub mod math;
pub mod nan;
pub mod number;
pub mod object;
pub mod proxy;
pub mod reflect;
pub mod regexp;
pub mod set;
pub mod string;
pub mod symbol;
pub mod typed_array;
pub mod undefined;
pub(crate) use self::{
array::{array_iterator::ArrayIterator, Array},
bigint::BigInt,
boolean::Boolean,
dataview::DataView,
date::Date,
error::{
AggregateError, Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError,
UriError,
},
function::BuiltInFunctionObject,
global_this::GlobalThis,
infinity::Infinity,
intl::Intl,
json::Json,
map::map_iterator::MapIterator,
map::Map,
math::Math,
nan::NaN,
number::Number,
object::for_in_iterator::ForInIterator,
object::Object as BuiltInObjectObject,
proxy::Proxy,
reflect::Reflect,
regexp::RegExp,
set::set_iterator::SetIterator,
set::Set,
string::String,
symbol::Symbol,
typed_array::{
BigInt64Array, BigUint64Array, Float32Array, Float64Array, Int16Array, Int32Array,
Int8Array, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray,
},
undefined::Undefined,
};
use crate::{
builtins::{
array_buffer::ArrayBuffer, generator::Generator, generator_function::GeneratorFunction,
typed_array::TypedArray,
},
property::{Attribute, PropertyDescriptor},
Context, JsValue,
};
/// Trait representing a global built-in object such as `Math`, `Object` or
/// `String`.
///
/// This trait must be implemented for any global built-in accessible from
/// Javascript.
pub(crate) trait BuiltIn {
/// Binding name of the built-in inside the global object.
///
/// E.g. If you want access the properties of a `Complex` built-in
/// with the name `Cplx` you must assign `"Cplx"` to this constant,
/// making any property inside it accessible from Javascript as `Cplx.prop`
const NAME: &'static str;
/// Property attribute flags of the built-in.
/// Check [Attribute] for more information.
const ATTRIBUTE: Attribute = Attribute::WRITABLE
.union(Attribute::NON_ENUMERABLE)
.union(Attribute::CONFIGURABLE);
/// Initialization code for the built-in.
/// This is where the methods, properties, static methods and the constructor
/// of a built-in must be initialized to be accessible from Javascript.
///
/// # Note
///
/// A return value of `None` indicates that the value must not be added as
/// a global attribute for the global object.
fn init(context: &mut Context) -> Option<JsValue>;
}
/// Utility function that checks if a type implements `BuiltIn` before
/// initializing it as a global built-in.
#[inline]
fn init_builtin<B: BuiltIn>(context: &mut Context) {
if let Some(value) = B::init(context) {
let property = PropertyDescriptor::builder()
.value(value)
.writable(B::ATTRIBUTE.writable())
.enumerable(B::ATTRIBUTE.enumerable())
.configurable(B::ATTRIBUTE.configurable())
.build();
context
.global_bindings_mut()
.insert(B::NAME.into(), property);
}
}
/// Initializes built-in objects and functions
#[inline]
pub fn init(context: &mut Context) {
macro_rules! globals {
($( $builtin:ty ),*) => {
$(init_builtin::<$builtin>(context)
);*
}
}
globals! {
Undefined,
Infinity,
NaN,
GlobalThis,
BuiltInFunctionObject,
BuiltInObjectObject,
Math,
Intl,
Json,
Array,
Proxy,
ArrayBuffer,
BigInt,
Boolean,
Date,
DataView,
Map,
Number,
Set,
String,
RegExp,
TypedArray,
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
BigInt64Array,
BigUint64Array,
Float32Array,
Float64Array,
Symbol,
Error,
RangeError,
ReferenceError,
TypeError,
SyntaxError,
EvalError,
UriError,
AggregateError,
Reflect,
Generator,
GeneratorFunction
};
#[cfg(feature = "console")]
init_builtin::<console::Console>(context);
}
pub trait JsArgs {
/// Utility function to `get` a parameter from
/// a `[JsValue]` or default to `JsValue::Undefined`
/// if `get` returns `None`.
///
/// Call this if you are thinking of calling something similar to
/// `args.get(n).cloned().unwrap_or_default()` or
/// `args.get(n).unwrap_or(&undefined)`.
///
/// This returns a reference for efficiency, in case
/// you only need to call methods of `JsValue`, so
/// try to minimize calling `clone`.
fn get_or_undefined(&self, index: usize) -> &JsValue;
}
impl JsArgs for [JsValue] {
fn get_or_undefined(&self, index: usize) -> &JsValue {
const UNDEFINED: &JsValue = &JsValue::Undefined;
self.get(index).unwrap_or(UNDEFINED)
}
}