mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:07:34 +00:00
LibJS: Add all of the DataView.prototype.get* methods
This commit is contained in:
parent
e4d267d4fb
commit
c54b9a6920
6 changed files with 206 additions and 0 deletions
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Variant.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
namespace JS {
|
||||
|
@ -33,6 +34,13 @@ public:
|
|||
void detach_buffer() { m_buffer = Empty {}; }
|
||||
bool is_detached() const { return m_buffer.has<Empty>(); }
|
||||
|
||||
enum Order {
|
||||
SeqCst,
|
||||
Unordered
|
||||
};
|
||||
template<typename type>
|
||||
Value get_value(size_t byte_index, bool is_typed_array, Order, bool is_little_endian = true);
|
||||
|
||||
private:
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
||||
|
@ -51,4 +59,53 @@ private:
|
|||
Value m_detach_key;
|
||||
};
|
||||
|
||||
// 25.1.2.9 RawBytesToNumeric ( type, rawBytes, isLittleEndian ), https://tc39.es/ecma262/#sec-rawbytestonumeric
|
||||
template<typename T>
|
||||
static Value raw_bytes_to_numeric(GlobalObject& global_object, ByteBuffer raw_value, bool is_little_endian)
|
||||
{
|
||||
if (!is_little_endian) {
|
||||
VERIFY(raw_value.size() % 2 == 0);
|
||||
for (size_t i = 0; i < raw_value.size() / 2; ++i)
|
||||
swap(raw_value[i], raw_value[raw_value.size() - 1 - i]);
|
||||
}
|
||||
if constexpr (IsSame<T, float>) {
|
||||
float value;
|
||||
raw_value.span().copy_to({ &value, sizeof(float) });
|
||||
if (isnan(value))
|
||||
return js_nan();
|
||||
return Value(value);
|
||||
}
|
||||
if constexpr (IsSame<T, double>) {
|
||||
double value;
|
||||
raw_value.span().copy_to({ &value, sizeof(double) });
|
||||
if (isnan(value))
|
||||
return js_nan();
|
||||
return Value(value);
|
||||
}
|
||||
if constexpr (!IsIntegral<T>)
|
||||
VERIFY_NOT_REACHED();
|
||||
T int_value = 0;
|
||||
raw_value.span().copy_to({ &int_value, sizeof(T) });
|
||||
if constexpr (sizeof(T) == 8) {
|
||||
if constexpr (IsSigned<T>)
|
||||
return js_bigint(global_object.heap(), Crypto::SignedBigInteger::create_from(int_value));
|
||||
else
|
||||
return js_bigint(global_object.heap(), Crypto::SignedBigInteger { Crypto::UnsignedBigInteger::create_from(int_value) });
|
||||
} else {
|
||||
return Value(int_value);
|
||||
}
|
||||
}
|
||||
|
||||
// 25.1.2.10 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-getvaluefrombuffer
|
||||
template<typename T>
|
||||
Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
|
||||
{
|
||||
auto element_size = sizeof(T);
|
||||
|
||||
// FIXME: Check for shared buffer
|
||||
|
||||
auto raw_value = buffer_impl().slice(byte_index, element_size);
|
||||
return raw_bytes_to_numeric<T>(global_object(), move(raw_value), is_little_endian);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue