AtomicsObject.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <AK/ByteBuffer.h>
  8. #include <LibJS/Runtime/AtomicsObject.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/TypedArray.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS {
  13. // 25.4.2.1 ValidateIntegerTypedArray ( typedArray [ , waitable ] ), https://tc39.es/ecma262/#sec-validateintegertypedarray
  14. static void validate_integer_typed_array(GlobalObject& global_object, TypedArrayBase& typed_array, bool waitable = false)
  15. {
  16. auto& vm = global_object.vm();
  17. validate_typed_array(global_object, typed_array);
  18. if (vm.exception())
  19. return;
  20. auto type_name = typed_array.element_name();
  21. if (waitable) {
  22. if ((type_name != "Int32Array"sv) && (type_name != "BigInt64Array"sv))
  23. vm.throw_exception<TypeError>(global_object, ErrorType::TypedArrayTypeIsNot, type_name, "Int32 or BigInt64"sv);
  24. } else {
  25. if (!typed_array.is_unclamped_integer_element_type() && !typed_array.is_bigint_element_type())
  26. vm.throw_exception<TypeError>(global_object, ErrorType::TypedArrayTypeIsNot, type_name, "an unclamped integer or BigInt"sv);
  27. }
  28. }
  29. // 25.4.2.2 ValidateAtomicAccess ( typedArray, requestIndex ), https://tc39.es/ecma262/#sec-validateatomicaccess
  30. static Optional<size_t> validate_atomic_access(GlobalObject& global_object, TypedArrayBase& typed_array, Value request_index)
  31. {
  32. auto& vm = global_object.vm();
  33. auto access_index = request_index.to_index(global_object);
  34. if (vm.exception())
  35. return {};
  36. if (access_index >= typed_array.array_length()) {
  37. vm.throw_exception<RangeError>(global_object, ErrorType::IndexOutOfRange, access_index, typed_array.array_length());
  38. return {};
  39. }
  40. return access_index * typed_array.element_size() + typed_array.byte_offset();
  41. }
  42. // 25.4.2.11 AtomicReadModifyWrite ( typedArray, index, value, op ), https://tc39.es/ecma262/#sec-atomicreadmodifywrite
  43. static Value atomic_read_modify_write(GlobalObject& global_object, TypedArrayBase& typed_array, Value index, Value value, ReadWriteModifyFunction operation)
  44. {
  45. auto& vm = global_object.vm();
  46. validate_integer_typed_array(global_object, typed_array);
  47. if (vm.exception())
  48. return {};
  49. auto byte_index = validate_atomic_access(global_object, typed_array, index);
  50. if (!byte_index.has_value())
  51. return {};
  52. Value value_to_set;
  53. if (typed_array.content_type() == TypedArrayBase::ContentType::BigInt) {
  54. value_to_set = value.to_bigint(global_object);
  55. if (vm.exception())
  56. return {};
  57. } else {
  58. value_to_set = Value(value.to_integer_or_infinity(global_object));
  59. if (vm.exception())
  60. return {};
  61. }
  62. if (typed_array.viewed_array_buffer()->is_detached()) {
  63. vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  64. return {};
  65. }
  66. return typed_array.get_modify_set_value_in_buffer(*byte_index, value_to_set, move(operation));
  67. }
  68. template<typename T, typename AtomicFunction>
  69. static Value perform_atomic_operation(GlobalObject& global_object, TypedArrayBase& typed_array, AtomicFunction&& operation)
  70. {
  71. auto& vm = global_object.vm();
  72. auto index = vm.argument(1);
  73. auto value = vm.argument(2);
  74. auto operation_wrapper = [&, operation = forward<AtomicFunction>(operation)](ByteBuffer x_bytes, ByteBuffer y_bytes) -> ByteBuffer {
  75. if constexpr (IsFloatingPoint<T>) {
  76. VERIFY_NOT_REACHED();
  77. } else {
  78. using U = Conditional<IsSame<ClampedU8, T>, u8, T>;
  79. auto* x = reinterpret_cast<U*>(x_bytes.data());
  80. auto* y = reinterpret_cast<U*>(y_bytes.data());
  81. operation(x, *y);
  82. return x_bytes;
  83. }
  84. };
  85. return atomic_read_modify_write(global_object, typed_array, index, value, move(operation_wrapper));
  86. }
  87. AtomicsObject::AtomicsObject(GlobalObject& global_object)
  88. : Object(*global_object.object_prototype())
  89. {
  90. }
  91. void AtomicsObject::initialize(GlobalObject& global_object)
  92. {
  93. Object::initialize(global_object);
  94. auto& vm = this->vm();
  95. u8 attr = Attribute::Writable | Attribute::Configurable;
  96. define_native_function(vm.names.add, add, 3, attr);
  97. define_native_function(vm.names.load, load, 2, attr);
  98. // 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag
  99. define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Atomics"), Attribute::Configurable);
  100. }
  101. // 25.4.3 Atomics.add ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.add
  102. JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::add)
  103. {
  104. auto* typed_array = typed_array_from(global_object, vm.argument(0));
  105. if (!typed_array)
  106. return {};
  107. auto atomic_add = [](auto* storage, auto value) { return AK::atomic_fetch_add(storage, value); };
  108. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  109. if (is<ClassName>(typed_array)) \
  110. return perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_add));
  111. JS_ENUMERATE_TYPED_ARRAYS
  112. #undef __JS_ENUMERATE
  113. VERIFY_NOT_REACHED();
  114. }
  115. // 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
  116. JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
  117. {
  118. auto* typed_array = typed_array_from(global_object, vm.argument(0));
  119. if (!typed_array)
  120. return {};
  121. validate_integer_typed_array(global_object, *typed_array);
  122. if (vm.exception())
  123. return {};
  124. auto indexed_position = validate_atomic_access(global_object, *typed_array, vm.argument(1));
  125. if (!indexed_position.has_value())
  126. return {};
  127. if (typed_array->viewed_array_buffer()->is_detached()) {
  128. vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  129. return {};
  130. }
  131. return typed_array->get_value_from_buffer(*indexed_position, ArrayBuffer::Order::SeqCst, true);
  132. }
  133. }