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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Boa's implementation of ECMAScript's global `Atomics` object.
//!
//! The `Atomics` object contains synchronization methods to orchestrate multithreading
//! on contexts that live in separate threads.
//!
//! More information:
//! - [ECMAScript reference][spec]
//! - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-atomics-object
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics
mod futex;
use std::sync::atomic::Ordering;
use crate::{
Context, JsArgs, JsNativeError, JsResult, JsString, JsValue,
builtins::{BuiltInObject, OrdinaryObject},
context::intrinsics::Intrinsics,
js_string,
object::{JsObject, JsPromise},
property::Attribute,
realm::Realm,
string::StaticJsStrings,
symbol::JsSymbol,
sys::time::Duration,
value::IntegerOrInfinity,
};
use super::{
BuiltInBuilder, IntrinsicObject,
array_buffer::{BufferObject, BufferRef},
typed_array::{Atomic, ContentType, Element, TypedArray, TypedArrayElement, TypedArrayKind},
};
/// Javascript `Atomics` object.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Atomics;
impl IntrinsicObject for Atomics {
fn init(realm: &Realm) {
let builder = BuiltInBuilder::with_intrinsic::<Self>(realm)
.static_property(
JsSymbol::to_string_tag(),
Self::NAME,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.static_method(Atomics::add, js_string!("add"), 3)
.static_method(Atomics::bit_and, js_string!("and"), 3)
.static_method(Atomics::compare_exchange, js_string!("compareExchange"), 4)
.static_method(Atomics::swap, js_string!("exchange"), 3)
.static_method(Atomics::is_lock_free, js_string!("isLockFree"), 1)
.static_method(Atomics::load, js_string!("load"), 2)
.static_method(Atomics::bit_or, js_string!("or"), 3)
.static_method(Atomics::store, js_string!("store"), 3)
.static_method(Atomics::sub, js_string!("sub"), 3)
.static_method(Atomics::wait::<false>, js_string!("wait"), 4)
.static_method(Atomics::wait::<true>, js_string!("waitAsync"), 4)
.static_method(Atomics::notify, js_string!("notify"), 3)
.static_method(Atomics::bit_xor, js_string!("xor"), 3);
#[cfg(feature = "experimental")]
let builder = builder.static_method(Atomics::pause, js_string!("pause"), 0);
builder.build();
}
fn get(intrinsics: &Intrinsics) -> JsObject {
intrinsics.objects().atomics()
}
}
impl BuiltInObject for Atomics {
const NAME: JsString = StaticJsStrings::ATOMICS;
}
macro_rules! atomic_op {
($(#[$attr:meta])* $name:ident) => {
$(#[$attr])* fn $name(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let array = args.get_or_undefined(0);
let index = args.get_or_undefined(1);
let value = args.get_or_undefined(2);
// AtomicReadModifyWrite ( typedArray, index, value, op )
// <https://tc39.es/ecma262/#sec-atomicreadmodifywrite>
// 1. Let buffer be ? ValidateIntegerTypedArray(typedArray).
let (ta, buf_len) = validate_integer_typed_array(array, false)?;
// 2. Let indexedPosition be ? ValidateAtomicAccess(typedArray, index).
let access = validate_atomic_access(&ta, buf_len, index, context)?;
// 3. If typedArray.[[ContentType]] is BigInt, let v be ? ToBigInt(value).
// 4. Otherwise, let v be 𝔽(? ToIntegerOrInfinity(value)).
// 7. Let elementType be TypedArrayElementType(typedArray).
let value = access.kind.get_element(value, context)?;
// 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
// 6. NOTE: The above check is not redundant with the check in ValidateIntegerTypedArray because the call
// to ToBigInt or ToIntegerOrInfinity on the preceding lines can have arbitrary side effects, which could
// cause the buffer to become detached.
let ta = ta.borrow();
let ta = ta.data();
let mut buffer = ta.viewed_array_buffer().as_buffer_mut();
let Some(mut data) = buffer.bytes_with_len(buf_len) else {
return Err(JsNativeError::typ()
.with_message("cannot execute atomic operation in detached buffer")
.into());
};
let data = data.subslice_mut(access.byte_offset..);
// 8. Return GetModifySetValueInBuffer(buffer, indexedPosition, elementType, v, op).
// SAFETY: The integer indexed object guarantees that the buffer is aligned.
// The call to `validate_atomic_access` guarantees that the index is in-bounds.
let value: TypedArrayElement = unsafe {
match value {
TypedArrayElement::Int8(num) => {
i8::read_mut(data).$name(num, Ordering::SeqCst).into()
}
TypedArrayElement::Uint8(num) => {
u8::read_mut(data).$name(num, Ordering::SeqCst).into()
}
TypedArrayElement::Int16(num) => i16::read_mut(data)
.$name(num, Ordering::SeqCst)
.into(),
TypedArrayElement::Uint16(num) => u16::read_mut(data)
.$name(num, Ordering::SeqCst)
.into(),
TypedArrayElement::Int32(num) => i32::read_mut(data)
.$name(num, Ordering::SeqCst)
.into(),
TypedArrayElement::Uint32(num) => u32::read_mut(data)
.$name(num, Ordering::SeqCst)
.into(),
TypedArrayElement::BigInt64(num) => i64::read_mut(data)
.$name(num, Ordering::SeqCst)
.into(),
TypedArrayElement::BigUint64(num) => u64::read_mut(data)
.$name(num, Ordering::SeqCst)
.into(),
TypedArrayElement::Uint8Clamped(_)
| TypedArrayElement::Float32(_)
| TypedArrayElement::Float64(_) => unreachable!(
"must have been filtered out by the call to `validate_integer_typed_array`"
),
#[cfg(feature = "float16")]
TypedArrayElement::Float16(_) => unreachable!(
"must have been filtered out by the call to `validate_integer_typed_array`"
),
}
};
Ok(value.into())
}
};
}
impl Atomics {
/// [`Atomics.isLockFree ( size )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.islockfree
fn is_lock_free(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let n be ? ToIntegerOrInfinity(size).
let n = args.get_or_undefined(0).to_integer_or_infinity(context)?;
// 2. Let AR be the Agent Record of the surrounding agent.
Ok(match n.as_integer() {
// 3. If n = 1, return AR.[[IsLockFree1]].
Some(1) => <<u8 as Element>::Atomic as Atomic>::is_lock_free(),
// 4. If n = 2, return AR.[[IsLockFree2]].
Some(2) => <<u16 as Element>::Atomic as Atomic>::is_lock_free(),
// 5. If n = 4, return true.
Some(4) => true,
// 6. If n = 8, return AR.[[IsLockFree8]].
Some(8) => <<u64 as Element>::Atomic as Atomic>::is_lock_free(),
// 7. Return false.
_ => false,
}
.into())
}
/// [`Atomics.load ( typedArray, index )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.load
fn load(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let array = args.get_or_undefined(0);
let index = args.get_or_undefined(1);
// 1. Let indexedPosition be ? ValidateAtomicAccessOnIntegerTypedArray(typedArray, index).
let (ta, buf_len) = validate_integer_typed_array(array, false)?;
let access = validate_atomic_access(&ta, buf_len, index, context)?;
// 2. Perform ? RevalidateAtomicAccess(typedArray, indexedPosition).
let ta = ta.borrow();
let ta = ta.data();
let buffer = ta.viewed_array_buffer().as_buffer();
let Some(data) = buffer.bytes_with_len(buf_len) else {
return Err(JsNativeError::typ()
.with_message("cannot execute atomic operation in detached buffer")
.into());
};
let data = data.subslice(access.byte_offset..);
// 3. Let buffer be typedArray.[[ViewedArrayBuffer]].
// 4. Let elementType be TypedArrayElementType(typedArray).
// 5. Return GetValueFromBuffer(buffer, indexedPosition, elementType, true, seq-cst).
// SAFETY: The integer indexed object guarantees that the buffer is aligned.
// The call to `validate_atomic_access` guarantees that the index is in-bounds.
let value = unsafe { data.get_value(access.kind, Ordering::SeqCst) };
Ok(value.into())
}
/// [`Atomics.store ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.store
fn store(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let array = args.get_or_undefined(0);
let index = args.get_or_undefined(1);
let value = args.get_or_undefined(2);
// 1. Let indexedPosition be ? ValidateAtomicAccessOnIntegerTypedArray(typedArray, index).
let (ta, buf_len) = validate_integer_typed_array(array, false)?;
let access = validate_atomic_access(&ta, buf_len, index, context)?;
// bit of a hack to preserve the converted value
// 2. If typedArray.[[ContentType]] is bigint, let v be ? ToBigInt(value).
let converted: JsValue = if access.kind.content_type() == ContentType::BigInt {
value.to_bigint(context)?.into()
} else {
// 3. Otherwise, let v be 𝔽(? ToIntegerOrInfinity(value)).
match value.to_integer_or_infinity(context)? {
IntegerOrInfinity::PositiveInfinity => f64::INFINITY,
IntegerOrInfinity::Integer(i) => i as f64,
IntegerOrInfinity::NegativeInfinity => f64::NEG_INFINITY,
}
.into()
};
let value = access.kind.get_element(&converted, context)?;
// 4. Perform ? RevalidateAtomicAccess(typedArray, indexedPosition).
let ta = ta.borrow();
let ta = ta.data();
let mut buffer = ta.viewed_array_buffer().as_buffer_mut();
let Some(mut buffer) = buffer.bytes_with_len(buf_len) else {
return Err(JsNativeError::typ()
.with_message("cannot execute atomic operation in detached buffer")
.into());
};
let mut data = buffer.subslice_mut(access.byte_offset..);
// 5. Let buffer be typedArray.[[ViewedArrayBuffer]].
// 6. Let elementType be TypedArrayElementType(typedArray).
// 7. Perform SetValueInBuffer(buffer, indexedPosition, elementType, v, true, seq-cst).
// SAFETY: The integer indexed object guarantees that the buffer is aligned.
// The call to `validate_atomic_access` guarantees that the index is in-bounds.
unsafe {
data.set_value(value, Ordering::SeqCst);
}
// 8. Return v.
Ok(converted)
}
/// [`Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.compareexchange
fn compare_exchange(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let array = args.get_or_undefined(0);
let index = args.get_or_undefined(1);
let expected = args.get_or_undefined(2);
let replacement = args.get_or_undefined(3);
// 1. Let indexedPosition be ? ValidateAtomicAccessOnIntegerTypedArray(typedArray, index).
// 2. Let buffer be typedArray.[[ViewedArrayBuffer]].
// 3. Let block be buffer.[[ArrayBufferData]].
let (ta, buf_len) = validate_integer_typed_array(array, false)?;
let access = validate_atomic_access(&ta, buf_len, index, context)?;
// 4. If typedArray.[[ContentType]] is bigint, then
// a. Let expected be ? ToBigInt(expectedValue).
// b. Let replacement be ? ToBigInt(replacementValue).
// 5. Else,
// a. Let expected be 𝔽(? ToIntegerOrInfinity(expectedValue)).
// b. Let replacement be 𝔽(? ToIntegerOrInfinity(replacementValue)).
let exp = access.kind.get_element(expected, context)?.to_bits();
let rep = access.kind.get_element(replacement, context)?.to_bits();
// 6. Perform ? RevalidateAtomicAccess(typedArray, indexedPosition).
let ta = ta.borrow();
let ta = ta.data();
let mut buffer = ta.viewed_array_buffer().as_buffer_mut();
let Some(mut buffer) = buffer.bytes_with_len(buf_len) else {
return Err(JsNativeError::typ()
.with_message("cannot execute atomic operation in detached buffer")
.into());
};
let data = buffer.subslice_mut(access.byte_offset..);
// 7. Let elementType be TypedArrayElementType(typedArray).
// 8. Let elementSize be TypedArrayElementSize(typedArray).
// 9. Let isLittleEndian be the value of the [[LittleEndian]] field of the surrounding agent's Agent Record.
// 10. Let expectedBytes be NumericToRawBytes(elementType, expected, isLittleEndian).
// 11. Let replacementBytes be NumericToRawBytes(elementType, replacement, isLittleEndian).
// 12. If IsSharedArrayBuffer(buffer) is true, then
// a. Let rawBytesRead be AtomicCompareExchangeInSharedBlock(block, indexedPosition, elementSize, expectedBytes, replacementBytes).
// 13. Else,
// a. Let rawBytesRead be a List of length elementSize whose elements are the sequence of elementSize bytes starting with block[indexedPosition].
// b. If ByteListEqual(rawBytesRead, expectedBytes) is true, then
// i. Store the individual bytes of replacementBytes into block, starting at block[indexedPosition].
// 14. Return RawBytesToNumeric(elementType, rawBytesRead, isLittleEndian).
// SAFETY: The integer indexed object guarantees that the buffer is aligned.
// The call to `validate_atomic_access` guarantees that the index is in-bounds.
let value: TypedArrayElement = unsafe {
match access.kind {
TypedArrayKind::Int8 => i8::read_mut(data)
.compare_exchange(exp as i8, rep as i8, Ordering::SeqCst)
.into(),
TypedArrayKind::Uint8 => u8::read_mut(data)
.compare_exchange(exp as u8, rep as u8, Ordering::SeqCst)
.into(),
TypedArrayKind::Int16 => i16::read_mut(data)
.compare_exchange(exp as i16, rep as i16, Ordering::SeqCst)
.into(),
TypedArrayKind::Uint16 => u16::read_mut(data)
.compare_exchange(exp as u16, rep as u16, Ordering::SeqCst)
.into(),
TypedArrayKind::Int32 => i32::read_mut(data)
.compare_exchange(exp as i32, rep as i32, Ordering::SeqCst)
.into(),
TypedArrayKind::Uint32 => u32::read_mut(data)
.compare_exchange(exp as u32, rep as u32, Ordering::SeqCst)
.into(),
TypedArrayKind::BigInt64 => i64::read_mut(data)
.compare_exchange(exp as i64, rep as i64, Ordering::SeqCst)
.into(),
TypedArrayKind::BigUint64 => u64::read_mut(data)
.compare_exchange(exp, rep, Ordering::SeqCst)
.into(),
TypedArrayKind::Uint8Clamped
| TypedArrayKind::Float32
| TypedArrayKind::Float64 => unreachable!(
"must have been filtered out by the call to `validate_integer_typed_array`"
),
#[cfg(feature = "float16")]
TypedArrayKind::Float16 => unreachable!(
"must have been filtered out by the call to `validate_integer_typed_array`"
),
}
};
Ok(value.into())
}
// =========== Atomics.ops start ===========
atomic_op! {
/// [`Atomics.add ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.add
add
}
atomic_op! {
/// [`Atomics.and ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.and
bit_and
}
atomic_op! {
/// [`Atomics.exchange ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.exchange
swap
}
atomic_op! {
/// [`Atomics.or ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.or
bit_or
}
atomic_op! {
/// [`Atomics.sub ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.sub
sub
}
atomic_op! {
/// [`Atomics.xor ( typedArray, index, value )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.xor
bit_xor
}
/// [`Atomics.wait ( typedArray, index, value, timeout )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.wait
fn wait<const ASYNC: bool>(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let array = args.get_or_undefined(0);
let index = args.get_or_undefined(1);
let value = args.get_or_undefined(2);
let timeout = args.get_or_undefined(3);
// 1. Let taRecord be ? ValidateIntegerTypedArray(typedArray, true).
let (ta, buf_len) = validate_integer_typed_array(array, true)?;
// 2. Let buffer be taRecord.[[Object]].[[ViewedArrayBuffer]].
// 3. If IsSharedArrayBuffer(buffer) is false, throw a TypeError exception.
let buffer = match ta.borrow().data().viewed_array_buffer() {
BufferObject::SharedBuffer(buf) => buf.clone(),
BufferObject::Buffer(_) => {
return Err(JsNativeError::typ()
.with_message("cannot use `ArrayBuffer` for an atomic wait")
.into());
}
};
// 4. Let i be ? ValidateAtomicAccess(taRecord, index).
let access = validate_atomic_access(&ta, buf_len, index, context)?;
// 5. Let arrayTypeName be typedArray.[[TypedArrayName]].
let value = if access.kind == TypedArrayKind::BigInt64 {
// 6. If typedArray.[[TypedArrayName]] is "BigInt64Array", let v be ? ToBigInt64(value).
value.to_big_int64(context)?
} else {
// 7. Else, let v be ? ToInt32(value).
i64::from(value.to_i32(context)?)
};
// 8. Let q be ? ToNumber(timeout).
// 9. If q is either NaN or +∞𝔽, let t be +∞; else if q is -∞𝔽, let t be 0; else let t be max(ℝ(q), 0).
let mut timeout = timeout.to_number(context)?;
// convert to nanoseconds to discard any excessively big timeouts.
timeout = timeout.clamp(0.0, f64::INFINITY) * 1000.0 * 1000.0;
let timeout = if timeout.is_nan() || timeout.is_infinite() || timeout > u64::MAX as f64 {
None
} else {
Some(Duration::from_nanos(timeout as u64))
};
// 10. If mode is sync and AgentCanSuspend() is false, throw a TypeError exception.
if !ASYNC && !context.can_block() {
return Err(JsNativeError::typ()
.with_message("agent cannot be suspended")
.into());
}
// SAFETY: the validity of `addr` is verified by our call to `validate_atomic_access`.
if ASYNC {
let (promise, resolvers) = JsPromise::new_pending(context);
let result = unsafe {
if access.kind == TypedArrayKind::BigInt64 {
futex::wait_async(
buffer.borrow().data(),
buf_len,
access.byte_offset,
value,
timeout,
resolvers,
context,
)?
} else {
// value must fit into `i32` since it came from an `i32` above.
futex::wait_async(
buffer.borrow().data(),
buf_len,
access.byte_offset,
value as i32,
timeout,
resolvers,
context,
)?
}
};
let (is_async, value) = match result {
futex::AtomicsWaitResult::NotEqual => (false, js_string!("not-equal").into()),
futex::AtomicsWaitResult::TimedOut => (false, js_string!("timed-out").into()),
futex::AtomicsWaitResult::Ok => (true, promise.into()),
};
Ok(context
.intrinsics()
.templates()
.wait_async()
.create(OrdinaryObject, vec![is_async.into(), value])
.into())
} else {
let result = unsafe {
if access.kind == TypedArrayKind::BigInt64 {
futex::wait(
buffer.borrow().data(),
buf_len,
access.byte_offset,
value,
timeout,
)?
} else {
// value must fit into `i32` since it came from an `i32` above.
futex::wait(
buffer.borrow().data(),
buf_len,
access.byte_offset,
value as i32,
timeout,
)?
}
};
Ok(match result {
futex::AtomicsWaitResult::NotEqual => js_string!("not-equal"),
futex::AtomicsWaitResult::TimedOut => js_string!("timed-out"),
futex::AtomicsWaitResult::Ok => js_string!("ok"),
}
.into())
}
}
/// [`Atomics.notify ( typedArray, index, count )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-atomics.notify
fn notify(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let array = args.get_or_undefined(0);
let index = args.get_or_undefined(1);
let count = args.get_or_undefined(2);
// 1. Let indexedPosition be ? ValidateAtomicAccessOnIntegerTypedArray(typedArray, index, true).
let (ta, buf_len) = validate_integer_typed_array(array, true)?;
let access = validate_atomic_access(&ta, buf_len, index, context)?;
// 2. If count is undefined, then
let count = if count.is_undefined() {
// a. Let c be +∞.
u64::MAX
} else {
// 3. Else,
// a. Let intCount be ? ToIntegerOrInfinity(count).
// b. Let c be max(intCount, 0).
match count.to_integer_or_infinity(context)? {
IntegerOrInfinity::PositiveInfinity => u64::MAX,
IntegerOrInfinity::Integer(i) => i64::max(i, 0) as u64,
IntegerOrInfinity::NegativeInfinity => 0,
}
};
// 4. Let buffer be typedArray.[[ViewedArrayBuffer]].
// 5. Let block be buffer.[[ArrayBufferData]].
// 6. If IsSharedArrayBuffer(buffer) is false, return +0𝔽.
let ta = ta.borrow();
let BufferRef::SharedBuffer(shared) = ta.data().viewed_array_buffer().as_buffer() else {
return Ok(0.into());
};
let count = futex::notify(&shared, access.byte_offset, count)?;
// 12. Let n be the number of elements in S.
// 13. Return 𝔽(n).
Ok(count.into())
}
/// [`Atomics.pause ( [ iterationNumber ] )`][spec]
///
/// [spec]: https://tc39.es/proposal-atomics-microwait/#Atomics.pause
#[cfg(feature = "experimental")]
fn pause(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
use super::Number;
let iteration_number = args.get_or_undefined(0);
// 1. If iterationNumber is not undefined, then
let iterations = if iteration_number.is_undefined() {
1
} else {
// a. If iterationNumber is not an integral Number, throw a TypeError exception.
if !Number::is_integer(iteration_number) {
return Err(JsNativeError::typ()
.with_message("`iterationNumber` must be an integral Number")
.into());
}
// b. If ℝ(iterationNumber) < 0, throw a RangeError exception.
let iteration_number = iteration_number.to_number(context)? as i16;
if iteration_number < 0 {
return Err(JsNativeError::range()
.with_message("`iterationNumber` must be a positive integer")
.into());
}
// Clamp to u16 so that the main thread cannot block using this.
iteration_number as u16
};
// 2. If the execution environment of the ECMAScript implementation supports a signal that the current executing code
// is in a spin-wait loop, send that signal. An ECMAScript implementation may send that signal multiple times,
// determined by iterationNumber when not undefined. The number of times the signal is sent for an integral Number
// N is at most the number of times it is sent for N + 1.
for _ in 0..iterations {
std::hint::spin_loop();
}
// 3. Return undefined.
Ok(JsValue::undefined())
}
}
/// [`ValidateIntegerTypedArray ( typedArray, waitable )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-validateintegertypedarray
fn validate_integer_typed_array(
array: &JsValue,
waitable: bool,
) -> JsResult<(JsObject<TypedArray>, usize)> {
// 1. Let taRecord be ? ValidateTypedArray(typedArray, unordered).
// 2. NOTE: Bounds checking is not a synchronizing operation when typedArray's backing buffer is a growable SharedArrayBuffer.
let ta_record = TypedArray::validate(array, Ordering::Relaxed)?;
{
let array = ta_record.0.borrow();
// 3. If waitable is true, then
if waitable {
// a. If typedArray.[[TypedArrayName]] is neither "Int32Array" nor "BigInt64Array", throw a TypeError exception.
if ![TypedArrayKind::Int32, TypedArrayKind::BigInt64].contains(&array.data().kind()) {
return Err(JsNativeError::typ()
.with_message("can only atomically wait using Int32 or BigInt64 arrays")
.into());
}
} else {
// 4. Else,
// a. Let type be TypedArrayElementType(typedArray).
// b. If IsUnclampedIntegerElementType(type) is false and IsBigIntElementType(type) is false, throw a TypeError exception.
if !array.data().kind().supports_atomic_ops() {
return Err(JsNativeError::typ()
.with_message(
"platform doesn't support atomic operations on the provided `TypedArray`",
)
.into());
}
}
}
// 5. Return taRecord.
Ok(ta_record)
}
#[derive(Debug, Copy, Clone)]
struct AtomicAccess {
byte_offset: usize,
kind: TypedArrayKind,
}
/// [`ValidateAtomicAccess ( taRecord, requestIndex )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-validateatomicaccess
fn validate_atomic_access(
array: &JsObject<TypedArray>,
buf_len: usize,
request_index: &JsValue,
context: &mut Context,
) -> JsResult<AtomicAccess> {
// 5. Let typedArray be taRecord.[[Object]].
let (length, kind, offset) = {
let array = array.borrow();
let array = array.data();
// 1. Let length be typedArray.[[ArrayLength]].
// 6. Let elementSize be TypedArrayElementSize(typedArray).
// 7. Let offset be typedArray.[[ByteOffset]].
(
array.array_length(buf_len),
array.kind(),
array.byte_offset(),
)
};
// 2. Let accessIndex be ? ToIndex(requestIndex).
let access_index = request_index.to_index(context)?;
// 3. Assert: accessIndex ≥ 0.
// ensured by the type.
// 4. If accessIndex ≥ length, throw a RangeError exception.
if access_index >= length {
return Err(JsNativeError::range()
.with_message("index for typed array outside of bounds")
.into());
}
// 8. Return (accessIndex × elementSize) + offset.
let offset = ((access_index * kind.element_size()) + offset) as usize;
Ok(AtomicAccess {
byte_offset: offset,
kind,
})
}
#[cfg(test)]
mod tests;