summaryrefslogtreecommitdiffstats
path: root/js/rust/src/jsval.rs
blob: f3a388605a034a3a37e95a282d3eddf43c5e9696 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

use jsapi::root::*;
use libc::c_void;
use std::mem;

#[cfg(target_pointer_width = "64")]
const JSVAL_TAG_SHIFT: usize = 47;

#[cfg(target_pointer_width = "64")]
const JSVAL_TAG_MAX_DOUBLE: u32 = 0x1FFF0u32;

#[cfg(target_pointer_width = "32")]
const JSVAL_TAG_CLEAR: u32 = 0xFFFFFF80;

#[cfg(target_pointer_width = "64")]
#[repr(u32)]
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
enum ValueTag {
    INT32 = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_INT32 as u32),
    UNDEFINED = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_UNDEFINED as u32),
    STRING = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_STRING as u32),
    SYMBOL = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_SYMBOL as u32),
    #[cfg(feature = "bigint")]
    BIGINT = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_BIGINT as u32),
    BOOLEAN = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_BOOLEAN as u32),
    MAGIC = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_MAGIC as u32),
    NULL = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_NULL as u32),
    OBJECT = JSVAL_TAG_MAX_DOUBLE | (JSValueType::JSVAL_TYPE_OBJECT as u32),
}

#[cfg(target_pointer_width = "32")]
#[repr(u32)]
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
enum ValueTag {
    PRIVATE = 0,
    INT32 = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_INT32 as u32),
    UNDEFINED = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_UNDEFINED as u32),
    STRING = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_STRING as u32),
    SYMBOL = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_SYMBOL as u32),
    #[cfg(feature = "bigint")]
    BIGINT = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_BIGINT as u32),
    BOOLEAN = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_BOOLEAN as u32),
    MAGIC = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_MAGIC as u32),
    NULL = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_NULL as u32),
    OBJECT = JSVAL_TAG_CLEAR as u32 | (JSValueType::JSVAL_TYPE_OBJECT as u32),
}

#[cfg(target_pointer_width = "64")]
#[repr(u64)]
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
enum ValueShiftedTag {
    MAX_DOUBLE = (((JSVAL_TAG_MAX_DOUBLE as u64) << JSVAL_TAG_SHIFT) | 0xFFFFFFFFu64),
    INT32 = ((ValueTag::INT32 as u64) << JSVAL_TAG_SHIFT),
    UNDEFINED = ((ValueTag::UNDEFINED as u64) << JSVAL_TAG_SHIFT),
    STRING = ((ValueTag::STRING as u64) << JSVAL_TAG_SHIFT),
    SYMBOL = ((ValueTag::SYMBOL as u64) << JSVAL_TAG_SHIFT),
    #[cfg(feature = "bigint")]
    BIGINT = ((ValueTag::BIGINT as u64) << JSVAL_TAG_SHIFT),
    BOOLEAN = ((ValueTag::BOOLEAN as u64) << JSVAL_TAG_SHIFT),
    MAGIC = ((ValueTag::MAGIC as u64) << JSVAL_TAG_SHIFT),
    NULL = ((ValueTag::NULL as u64) << JSVAL_TAG_SHIFT),
    OBJECT = ((ValueTag::OBJECT as u64) << JSVAL_TAG_SHIFT),
}

#[cfg(target_pointer_width = "64")]
const JSVAL_PAYLOAD_MASK: u64 = 0x00007FFFFFFFFFFF;

#[inline(always)]
fn AsJSVal(val: u64) -> JS::Value {
    JS::Value { asBits_: val }
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
fn BuildJSVal(tag: ValueTag, payload: u64) -> JS::Value {
    AsJSVal(((tag as u32 as u64) << JSVAL_TAG_SHIFT) | payload)
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
fn BuildJSVal(tag: ValueTag, payload: u64) -> JS::Value {
    AsJSVal(((tag as u32 as u64) << 32) | payload)
}

#[inline(always)]
pub fn NullValue() -> JS::Value {
    BuildJSVal(ValueTag::NULL, 0)
}

#[inline(always)]
pub fn UndefinedValue() -> JS::Value {
    BuildJSVal(ValueTag::UNDEFINED, 0)
}

#[inline(always)]
pub fn Int32Value(i: i32) -> JS::Value {
    BuildJSVal(ValueTag::INT32, i as u32 as u64)
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
pub fn DoubleValue(f: f64) -> JS::Value {
    let bits: u64 = unsafe { mem::transmute(f) };
    assert!(bits <= ValueShiftedTag::MAX_DOUBLE as u64);
    AsJSVal(bits)
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
pub fn DoubleValue(f: f64) -> JS::Value {
    let bits: u64 = unsafe { mem::transmute(f) };
    let val = AsJSVal(bits);
    assert!(val.is_double());
    val
}

#[inline(always)]
pub fn UInt32Value(ui: u32) -> JS::Value {
    if ui > 0x7fffffff {
        DoubleValue(ui as f64)
    } else {
        Int32Value(ui as i32)
    }
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
pub fn StringValue(s: &JSString) -> JS::Value {
    let bits = s as *const JSString as usize as u64;
    assert!((bits >> JSVAL_TAG_SHIFT) == 0);
    BuildJSVal(ValueTag::STRING, bits)
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
pub fn StringValue(s: &JSString) -> JS::Value {
    let bits = s as *const JSString as usize as u64;
    BuildJSVal(ValueTag::STRING, bits)
}

#[inline(always)]
pub fn BooleanValue(b: bool) -> JS::Value {
    BuildJSVal(ValueTag::BOOLEAN, b as u64)
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
pub fn ObjectValue(o: *mut JSObject) -> JS::Value {
    let bits = o as usize as u64;
    assert!((bits >> JSVAL_TAG_SHIFT) == 0);
    BuildJSVal(ValueTag::OBJECT, bits)
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
pub fn ObjectValue(o: *mut JSObject) -> JS::Value {
    let bits = o as usize as u64;
    BuildJSVal(ValueTag::OBJECT, bits)
}

#[inline(always)]
pub fn ObjectOrNullValue(o: *mut JSObject) -> JS::Value {
    if o.is_null() {
        NullValue()
    } else {
        ObjectValue(o)
    }
}

#[cfg(target_pointer_width = "64")]
#[inline(always)]
pub fn PrivateValue(o: *const c_void) -> JS::Value {
    let ptrBits = o as usize as u64;
    assert!((ptrBits & 1) == 0);
    AsJSVal(ptrBits >> 1)
}

#[cfg(target_pointer_width = "32")]
#[inline(always)]
pub fn PrivateValue(o: *const c_void) -> JS::Value {
    let ptrBits = o as usize as u64;
    assert!((ptrBits & 1) == 0);
    BuildJSVal(ValueTag::PRIVATE, ptrBits)
}

#[inline(always)]
#[cfg(feature = "bigint")]
#[cfg(target_pointer_width = "64")]
pub fn BigIntValue(b: &JS::BigInt) -> JS::Value {
    let bits = b as *const JS::BigInt as usize as u64;
    assert!((bits >> JSVAL_TAG_SHIFT) == 0);
    BuildJSVal(ValueTag::BIGINT, bits)
}

#[inline(always)]
#[cfg(target_pointer_width = "32")]
#[inline(always)]
pub fn BigIntValue(s: &JS::BigInt) -> JS::Value {
    let bits = s as *const JS::BigInt as usize as u64;
    BuildJSVal(ValueTag::BIGINT, bits)
}

impl JS::Value {
    #[inline(always)]
    unsafe fn asBits(&self) -> u64 {
        self.asBits_
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_undefined(&self) -> bool {
        unsafe { self.asBits() == ValueShiftedTag::UNDEFINED as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_undefined(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::UNDEFINED as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_null(&self) -> bool {
        unsafe { self.asBits() == ValueShiftedTag::NULL as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_null(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::NULL as u64 }
    }

    #[inline(always)]
    pub fn is_null_or_undefined(&self) -> bool {
        self.is_null() || self.is_undefined()
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_boolean(&self) -> bool {
        unsafe { (self.asBits() >> JSVAL_TAG_SHIFT) == ValueTag::BOOLEAN as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_boolean(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::BOOLEAN as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_int32(&self) -> bool {
        unsafe { (self.asBits() >> JSVAL_TAG_SHIFT) == ValueTag::INT32 as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_int32(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::INT32 as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_double(&self) -> bool {
        unsafe { self.asBits() <= ValueShiftedTag::MAX_DOUBLE as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_double(&self) -> bool {
        unsafe { (self.asBits() >> 32) <= JSVAL_TAG_CLEAR as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_number(&self) -> bool {
        const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_NUMBER_SET: u64 = ValueShiftedTag::UNDEFINED as u64;
        unsafe { self.asBits() < JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_NUMBER_SET }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_number(&self) -> bool {
        const JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET: u64 = ValueTag::INT32 as u64;
        unsafe { (self.asBits() >> 32) <= JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_primitive(&self) -> bool {
        const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_PRIMITIVE_SET: u64 = ValueShiftedTag::OBJECT as u64;
        unsafe { self.asBits() < JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_PRIMITIVE_SET }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_primitive(&self) -> bool {
        const JSVAL_UPPER_EXCL_TAG_OF_PRIMITIVE_SET: u64 = ValueTag::OBJECT as u64;
        unsafe { (self.asBits() >> 32) < JSVAL_UPPER_EXCL_TAG_OF_PRIMITIVE_SET }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_string(&self) -> bool {
        unsafe { (self.asBits() >> JSVAL_TAG_SHIFT) == ValueTag::STRING as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_string(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::STRING as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_object(&self) -> bool {
        unsafe {
            assert!((self.asBits() >> JSVAL_TAG_SHIFT) <= ValueTag::OBJECT as u64);
            self.asBits() >= ValueShiftedTag::OBJECT as u64
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_object(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::OBJECT as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_symbol(&self) -> bool {
        unsafe { (self.asBits() >> JSVAL_TAG_SHIFT) == ValueTag::SYMBOL as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_symbol(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::SYMBOL as u64 }
    }

    #[inline(always)]
    #[cfg(feature = "bigint")]
    #[cfg(target_pointer_width = "64")]
    pub fn is_bigint(&self) -> bool {
        unsafe { (self.asBits() >> JSVAL_TAG_SHIFT) == ValueTag::BIGINT as u64 }
    }

    #[inline(always)]
    #[cfg(feature = "bigint")]
    #[cfg(target_pointer_width = "32")]
    pub fn is_bigint(&self) -> bool {
        unsafe { (self.asBits() >> 32) == ValueTag::BIGINT as u64 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn to_boolean(&self) -> bool {
        assert!(self.is_boolean());
        unsafe { (self.asBits() & JSVAL_PAYLOAD_MASK) != 0 }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn to_boolean(&self) -> bool {
        assert!(self.is_boolean());
        unsafe { (self.asBits() & 0x00000000FFFFFFFF) != 0 }
    }

    #[inline(always)]
    pub fn to_int32(&self) -> i32 {
        assert!(self.is_int32());
        unsafe { (self.asBits() & 0x00000000FFFFFFFF) as i32 }
    }

    #[inline(always)]
    pub fn to_double(&self) -> f64 {
        assert!(self.is_double());
        unsafe { mem::transmute(self.asBits()) }
    }

    #[inline(always)]
    pub fn to_number(&self) -> f64 {
        assert!(self.is_number());
        if self.is_double() {
            self.to_double()
        } else {
            self.to_int32() as f64
        }
    }

    #[inline(always)]
    pub fn to_object(&self) -> *mut JSObject {
        assert!(self.is_object());
        self.to_object_or_null()
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn to_string(&self) -> *mut JSString {
        assert!(self.is_string());
        unsafe {
            let ptrBits = self.asBits() & JSVAL_PAYLOAD_MASK;
            ptrBits as usize as *mut JSString
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn to_string(&self) -> *mut JSString {
        assert!(self.is_string());
        unsafe {
            let ptrBits: u32 = (self.asBits() & 0x00000000FFFFFFFF) as u32;
            ptrBits as *mut JSString
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_object_or_null(&self) -> bool {
        const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_OBJ_OR_NULL_SET: u64 = ValueShiftedTag::NULL as u64;
        unsafe {
            assert!((self.asBits() >> JSVAL_TAG_SHIFT) <= ValueTag::OBJECT as u64);
            self.asBits() >= JSVAL_LOWER_INCL_SHIFTED_TAG_OF_OBJ_OR_NULL_SET
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_object_or_null(&self) -> bool {
        const JSVAL_LOWER_INCL_TAG_OF_OBJ_OR_NULL_SET: u64 = ValueTag::NULL as u64;
        unsafe {
            assert!((self.asBits() >> 32) <= ValueTag::OBJECT as u64);
            (self.asBits() >> 32) >= JSVAL_LOWER_INCL_TAG_OF_OBJ_OR_NULL_SET
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn to_object_or_null(&self) -> *mut JSObject {
        assert!(self.is_object_or_null());
        unsafe {
            let ptrBits = self.asBits() & JSVAL_PAYLOAD_MASK;
            assert!((ptrBits & 0x7) == 0);
            ptrBits as usize as *mut JSObject
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn to_object_or_null(&self) -> *mut JSObject {
        assert!(self.is_object_or_null());
        unsafe {
            let ptrBits: u32 = (self.asBits() & 0x00000000FFFFFFFF) as u32;
            ptrBits as *mut JSObject
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn to_private(&self) -> *const c_void {
        assert!(self.is_double());
        unsafe {
            assert!((self.asBits() & 0x8000000000000000u64) == 0);
            (self.asBits() << 1) as usize as *const c_void
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn to_private(&self) -> *const c_void {
        unsafe {
            let ptrBits: u32 = (self.asBits() & 0x00000000FFFFFFFF) as u32;
            ptrBits as *const c_void
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn is_gcthing(&self) -> bool {
        const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_GCTHING_SET: u64 = ValueShiftedTag::STRING as u64;
        unsafe { self.asBits() >= JSVAL_LOWER_INCL_SHIFTED_TAG_OF_GCTHING_SET }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn is_gcthing(&self) -> bool {
        const JSVAL_LOWER_INCL_TAG_OF_GCTHING_SET: u64 = ValueTag::STRING as u64;
        unsafe { (self.asBits() >> 32) >= JSVAL_LOWER_INCL_TAG_OF_GCTHING_SET }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "64")]
    pub fn to_gcthing(&self) -> *mut c_void {
        assert!(self.is_gcthing());
        unsafe {
            let ptrBits = self.asBits() & JSVAL_PAYLOAD_MASK;
            assert!((ptrBits & 0x7) == 0);
            ptrBits as *mut c_void
        }
    }

    #[inline(always)]
    #[cfg(target_pointer_width = "32")]
    pub fn to_gcthing(&self) -> *mut c_void {
        assert!(self.is_gcthing());
        unsafe {
            let ptrBits: u32 = (self.asBits() & 0x00000000FFFFFFFF) as u32;
            ptrBits as *mut c_void
        }
    }

    #[inline(always)]
    pub fn is_markable(&self) -> bool {
        self.is_gcthing() && !self.is_null()
    }

    #[inline(always)]
    pub fn trace_kind(&self) -> JS::TraceKind {
        assert!(self.is_markable());
        if self.is_object() {
            JS::TraceKind::Object
        } else {
            JS::TraceKind::String
        }
    }
}