7#include <Ultralight/CAPI/CAPI_JSBuffer.h>
11#include <Ultralight/js/detail/BufferBridge.h>
102 :
Value(value.
raw() && ulJSValueIsArrayBuffer(value.
raw()) ? value :
Value()) {}
125 return detail::AdoptULBuffer(ulJSArrayBufferGetBuffer(
raw()));
148 [[nodiscard]]
bool Detach()
const {
return ulJSArrayBufferDetach(
raw()); }
204 static_assert(detail::kTypedArrayTypeOf<T> != kULJSTypedArrayType_None,
205 "TypedArray<T>: T must be one of the fixed-width typed-array element types "
206 "(intN_t / uintN_t, float, double)");
222 && detail::ElementAccepts(ulJSValueGetTypedArrayType(value.
raw()),
223 detail::kTypedArrayTypeOf<T>)
248 ctx, detail::kTypedArrayTypeOf<T>,
buffer.
raw(), byte_offset, length)));
249 ulDestroyJSContext(ctx);
265 ULJSTypedArrayInfo info;
266 if (!ulJSTypedArrayGetInfo(
raw(), &info) || !info.data
267 || !detail::ElementAccepts(info.type, detail::kTypedArrayTypeOf<T>))
269 return std::span<T>(
static_cast<T*
>(info.data), info.length);
287 if (!detail::ElementAccepts(ulJSValueGetTypedArrayType(
raw()), detail::kTypedArrayTypeOf<T>))
289 return ulJSTypedArrayGetLength(
raw());
320 if (ulJSValueGetType(value) == kULJSType_Null) {
324 ULBuffer handle = ulJSArrayBufferGetBuffer(value);
327 *out = detail::AdoptULBuffer(handle);
328 return *out !=
nullptr;
332 return detail::WrapBufferThrough(ctx, value, [&](ULBuffer handle) {
333 return ulCreateJSArrayBufferFromBuffer(ctx, handle);
337 static constexpr const char*
SchemaType() {
return "ArrayBuffer"; }
363 std::enable_if_t<detail::kTypedArrayTypeOf<std::remove_const_t<T>>
364 != kULJSTypedArrayType_None>> {
366 using Element = std::remove_const_t<T>;
369 static bool FromJS(
ULJSContext, ULJSValue value, std::span<T>* out, ULJSValue*) {
373 ULJSTypedArrayType have = ulJSValueGetTypedArrayType(value);
374 if (have != kULJSTypedArrayType_None) {
375 if (!detail::ElementAccepts(have, detail::kTypedArrayTypeOf<Element>))
377 ULJSTypedArrayInfo info;
378 if (!ulJSTypedArrayGetInfo(value, &info))
381 *out = info.data ? std::span<T>(
static_cast<T*
>(info.data), info.length)
385 if constexpr (std::is_same_v<Element, uint8_t>) {
386 if (ulJSValueIsArrayBuffer(value)) {
388 void* bytes = ulJSArrayBufferGetBytes(value, &length);
390 *out = bytes ? std::span<T>(
static_cast<T*
>(bytes), length) :
std::span<T>();
397 static constexpr const char* SchemaType() {
return detail::TypedArrayNameOf<Element>(); }
401 static void WriteSchemaExpr(std::string& out) {
402 out +=
"{\"typedarray\":\"";
403 out += detail::TypedArrayElementToken<Element>();
struct C_JSContext * ULJSContext
Opaque handle to a JavaScript execution context (one page's script world in one View).
Definition View.h:25
A fixed-size container for raw byte data.
Definition Buffer.h:29
A nullable smart pointer.
Definition RefPtr.h:126
Handle to a JavaScript ArrayBuffer for sharing raw binary memory with the page.
Definition Buffer.h:88
ArrayBuffer(const Value &value)
Wrap a value if it's an ArrayBuffer (detached ones included), otherwise create an empty wrapper.
Definition Buffer.h:101
bool Detach() const
Detach this ArrayBuffer so JavaScript can no longer reach its bytes.
Definition Buffer.h:148
ArrayBuffer()=default
Create an empty ArrayBuffer wrapper.
RefPtr< ultralight::Buffer > ShareBytes() const
Share this ArrayBuffer's bytes with native code without copying.
Definition Buffer.h:124
size_t byte_length() const
Get the length in bytes.
Definition Buffer.h:134
TypedArray(const Value &value)
Wrap a value if it's a typed array with elements of type T, otherwise create an empty wrapper.
Definition Buffer.h:220
size_t size() const
Get the number of elements.
Definition Buffer.h:285
T * data() const
Get a pointer to the first element.
Definition Buffer.h:278
std::span< T > view() const
Get a span over the elements without copying.
Definition Buffer.h:264
static TypedArray Create(const ArrayBuffer &buffer, size_t byte_offset, size_t length)
Create a typed array over part of an existing ArrayBuffer without copying.
Definition Buffer.h:243
ArrayBuffer buffer() const
Get the ArrayBuffer this typed array views.
Definition Buffer.h:297
TypedArray()=default
Create an empty TypedArray wrapper.
static Value Adopt(ULJSValue handle)
Take ownership of a handle from the C API without adding a reference.
Definition Value.h:799
Value()
Create an empty Value.
Definition Value.h:215
ULJSValue raw() const
Get the C API handle without transferring ownership.
Definition Value.h:820
Definition StringSTL.h:166
Type-checked JavaScript bridge between C++ and web pages.
Definition JSInterop.h:141
Root namespace for every public Ultralight type, function, and enumeration.
static void WriteSchemaExpr(std::string &out)
Definition Buffer.h:340
static ULJSValue ToJS(ULJSContext ctx, const RefPtr< ultralight::Buffer > &value)
Definition Buffer.h:331
static bool FromJS(ULJSContext, ULJSValue value, RefPtr< ultralight::Buffer > *out, ULJSValue *)
Definition Buffer.h:318
static constexpr const char * SchemaType()
Definition Buffer.h:337
Type conversions between C++ and JavaScript across the bridge.
Definition TypeTraits.h:240