Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
JSSpan< T > Class Template Reference

#include <WasmCommon.h>

Public Member Functions

 JSSpan (JSArray src)
 
 JSSpan (WASMPointer ptr, size_t len, bool takeOwnership)
 
 ~JSSpan ()
 
const Tdata () const
 
size_t size () const
 

Detailed Description

template<typename T>
class JSSpan< T >

Gives read access to a JSArray

We explicitly use malloc/free (not new/delete) so this can be used with allocations from the JS side (ala CanvasKit.Malloc).

Definition at line 89 of file WasmCommon.h.

Constructor & Destructor Documentation

◆ JSSpan() [1/2]

template<typename T >
JSSpan< T >::JSSpan ( JSArray  src)
inline

Definition at line 93 of file WasmCommon.h.

93 {
94 const size_t len = src["length"].as<size_t>();
95 T* data;
96
97 // If the buffer was allocated via CanvasKit' Malloc, we can peek directly at it!
98 if (src["_ck"].isTrue()) {
99 fOwned = false;
100 data = reinterpret_cast<T*>(src["byteOffset"].as<size_t>());
101 } else {
102 fOwned = true;
103 data = static_cast<T*>(sk_malloc_throw(len, sizeof(T)));
104
105 // now actually copy into 'data'
106 if (src.instanceof(emscripten::val::global(JSArrayType<T>::gName))) {
107 auto dst_view = emscripten::val(typed_memory_view(len, data));
108 dst_view.call<void>("set", src);
109 } else {
110 for (size_t i = 0; i < len; ++i) {
111 data[i] = src[i].as<T>();
112 }
113 }
114 }
115 fSpan = SkSpan(data, len);
116 }
static void * sk_malloc_throw(size_t size)
Definition SkMalloc.h:67
const T * data() const
Definition WasmCommon.h:128
#define T

◆ JSSpan() [2/2]

template<typename T >
JSSpan< T >::JSSpan ( WASMPointer  ptr,
size_t  len,
bool  takeOwnership 
)
inline

Definition at line 118 of file WasmCommon.h.

118 : fOwned(takeOwnership) {
119 fSpan = SkSpan(reinterpret_cast<T*>(ptr), len);
120 }

◆ ~JSSpan()

template<typename T >
JSSpan< T >::~JSSpan ( )
inline

Definition at line 122 of file WasmCommon.h.

122 {
123 if (fOwned) {
124 sk_free(fSpan.data());
125 }
126 }
SK_API void sk_free(void *)

Member Function Documentation

◆ data()

template<typename T >
const T * JSSpan< T >::data ( ) const
inline

Definition at line 128 of file WasmCommon.h.

128{ return fSpan.data(); }

◆ size()

template<typename T >
size_t JSSpan< T >::size ( ) const
inline

Definition at line 129 of file WasmCommon.h.

129{ return fSpan.size(); }

The documentation for this class was generated from the following file: