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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::utils::bind;
use alloc::string::String;
use emlite::FromVal;
/// JavaScript Symbol type.
///
/// Symbols are a primitive data type in JavaScript that are guaranteed to be unique.
/// They are often used as object keys to avoid naming collisions.
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct Symbol {
inner: emlite::Val,
}
bind!(Symbol);
impl Symbol {
/// Creates a new Symbol with an optional description.
///
/// # Arguments
/// * `description` - Optional description for the symbol
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym1 = Symbol::new(None);
/// let sym2 = Symbol::new(Some("my symbol"));
/// ```
pub fn new(description: Option<String>) -> Self {
let ctor = emlite::Val::global("Symbol");
let val = match description {
Some(desc) => ctor.invoke(&[desc.into()]),
None => ctor.invoke(&[]),
};
Self { inner: val }
}
/// Creates a symbol from the global Symbol registry.
///
/// # Arguments
/// * `key` - The key to lookup/register in the global symbol registry
///
/// # Returns
/// The symbol associated with the key
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym1 = Symbol::for_key("myKey");
/// let sym2 = Symbol::for_key("myKey");
/// // sym1 and sym2 refer to the same symbol
/// ```
pub fn for_key(key: &str) -> Self {
let val = emlite::Val::global("Symbol").call("for", &[key.into()]);
Self { inner: val }
}
/// Gets the key for this symbol from the global symbol registry.
///
/// # Returns
/// The key if this symbol is in the global registry, `None` otherwise
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym = Symbol::for_key("myKey");
/// assert_eq!(sym.key_for(), Some("myKey".to_string()));
///
/// let local_sym = Symbol::new(None);
/// assert_eq!(local_sym.key_for(), None);
/// ```
pub fn key_for(&self) -> Option<String> {
let result = emlite::Val::global("Symbol").call("keyFor", &[self.inner.clone()]);
if result.is_undefined() {
None
} else {
result.as_::<Option<String>>()
}
}
/// Gets the description of this symbol.
///
/// # Returns
/// The description if available
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym = Symbol::new(Some("test symbol"));
/// assert_eq!(sym.description(), Some("test symbol".to_string()));
/// ```
pub fn description(&self) -> Option<String> {
let desc = self.inner.get("description");
if desc.is_undefined() || desc.is_null() {
None
} else {
desc.as_::<Option<String>>()
}
}
/// Returns the string representation of this symbol.
///
/// # Returns
/// String representation like "Symbol(description)"
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym = Symbol::new(Some("test"));
/// let str_repr = sym.to_string_repr();
/// // str_repr will be something like "Symbol(test)"
/// ```
pub fn to_string_repr(&self) -> String {
self.inner
.call("toString", &[])
.as_::<Option<String>>()
.unwrap_or_default()
}
/// Gets primitive symbol value as string.
///
/// # Returns
/// String representation of the symbol
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym = Symbol::new(Some("test"));
/// let value = sym.value_of();
/// ```
pub fn value_of(&self) -> String {
self.inner
.call("valueOf", &[])
.as_::<Option<String>>()
.unwrap_or_default()
}
/// Checks if symbol has no description.
///
/// # Returns
/// `true` if description is undefined
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym1 = Symbol::new(None);
/// assert!(sym1.is_empty());
///
/// let sym2 = Symbol::new(Some("test"));
/// assert!(!sym2.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.description().is_none()
}
/// Gets hash code for symbol.
///
/// # Returns
/// Hash value based on unique handle
///
/// # Examples
/// ```rust
/// use jsbind::prelude::*;
///
/// let sym = Symbol::new(Some("test"));
/// let hash_val = sym.get_hash();
/// ```
pub fn get_hash(&self) -> u64 {
// Simple hash based on the handle value
self.inner.as_handle() as u64
}
// Well-known symbols
/// Symbol.asyncIterator
pub fn async_iterator() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("asyncIterator"),
}
}
/// Symbol.hasInstance
pub fn has_instance() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("hasInstance"),
}
}
/// Symbol.isConcatSpreadable
pub fn is_concat_spreadable() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("isConcatSpreadable"),
}
}
/// Symbol.iterator
pub fn iterator() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("iterator"),
}
}
/// Symbol.match
pub fn match_() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("match"),
}
}
/// Symbol.matchAll
pub fn match_all() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("matchAll"),
}
}
/// Symbol.replace
pub fn replace() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("replace"),
}
}
/// Symbol.search
pub fn search() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("search"),
}
}
/// Symbol.species
pub fn species() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("species"),
}
}
/// Symbol.split
pub fn split() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("split"),
}
}
/// Symbol.toPrimitive
pub fn to_primitive() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("toPrimitive"),
}
}
/// Symbol.toStringTag
pub fn to_string_tag() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("toStringTag"),
}
}
/// Symbol.unscopables
pub fn unscopables() -> Self {
Self {
inner: emlite::Val::global("Symbol").get("unscopables"),
}
}
}
impl PartialEq for Symbol {
fn eq(&self, other: &Self) -> bool {
self.inner.as_handle() == other.inner.as_handle()
}
}
impl Eq for Symbol {}
impl PartialOrd for Symbol {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Symbol {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.inner.as_handle().cmp(&other.inner.as_handle())
}
}
impl core::hash::Hash for Symbol {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.inner.as_handle().hash(state);
}
}
impl crate::prelude::DynCast for Symbol {
fn instanceof(val: &emlite::Val) -> bool {
val.type_of() == "symbol"
}
fn unchecked_from_val(v: emlite::Val) -> Self {
Self { inner: v }
}
fn unchecked_from_val_ref(v: &emlite::Val) -> &Self {
unsafe { &*(v as *const emlite::Val as *const Self) }
}
fn unchecked_from_val_mut(v: &mut emlite::Val) -> &mut Self {
unsafe { &mut *(v as *mut emlite::Val as *mut Self) }
}
}