Flutter Engine
tonic::DartConverter< DartConverterMapping > Struct Template Reference

#include <dart_converter.h>

Static Public Member Functions

static Dart_Handle ToDart (const DartConverterMapping &val)
 
static void SetReturnValue (Dart_NativeArguments args, const DartConverterMapping &val)
 
static DartConverterMapping FromDart (Dart_Handle dart_list)
 

Detailed Description

template<>
struct tonic::DartConverter< DartConverterMapping >

Definition at line 20 of file dart_converter.h.

Member Function Documentation

◆ FromDart()

static DartConverterMapping tonic::DartConverter< DartConverterMapping >::FromDart ( Dart_Handle  dart_list)
inlinestatic

Definition at line 63 of file dart_converter.h.

References FML_LOG, length, and fml::size().

63  {
64  if (Dart_IsNull(dart_list)) {
65  return nullptr;
66  }
67 
68  if (Dart_IsError(dart_list)) {
69  FML_LOG(ERROR) << "Cannot convert an error handle to a list: "
70  << Dart_GetError(dart_list);
71  return nullptr;
72  }
73 
74  if (!Dart_IsList(dart_list)) {
75  FML_LOG(ERROR) << "Dart handle was not a list.";
76  return nullptr;
77  }
78 
79  intptr_t length = 0;
80  auto handle = Dart_ListLength(dart_list, &length);
81 
82  if (Dart_IsError(handle)) {
83  FML_LOG(ERROR) << "Could not get the length of the Dart list: "
84  << Dart_GetError(handle);
85  return nullptr;
86  }
87 
88  if (length == 0) {
89  // Return a valid zero sized mapping.
90  return std::make_unique<fml::NonOwnedMapping>(nullptr, 0);
91  }
92 
93  auto mapping_buffer = ::malloc(length);
94 
95  if (!mapping_buffer) {
96  FML_LOG(ERROR)
97  << "Out of memory while attempting to allocate a mapping of size: "
98  << length;
99  return nullptr;
100  }
101 
102  auto mapping = std::make_unique<fml::NonOwnedMapping>(
103  static_cast<const uint8_t*>(mapping_buffer), length,
104  [](const uint8_t* data, size_t size) {
105  ::free(const_cast<uint8_t*>(data));
106  });
107 
108  handle = Dart_ListGetAsBytes(
109  dart_list, // list
110  0, // offset
111  static_cast<uint8_t*>(mapping_buffer), // native array
112  length // length
113  );
114 
115  if (Dart_IsError(handle)) {
116  FML_LOG(ERROR) << "Could not copy Dart list to native buffer: "
117  << Dart_GetError(handle);
118  return nullptr;
119  }
120 
121  return mapping;
122  }
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define FML_LOG(severity)
Definition: logging.h:65
size_t length

◆ SetReturnValue()

static void tonic::DartConverter< DartConverterMapping >::SetReturnValue ( Dart_NativeArguments  args,
const DartConverterMapping val 
)
inlinestatic

Definition at line 58 of file dart_converter.h.

References tonic::ToDart().

59  {
60  Dart_SetReturnValue(args, ToDart(val));
61  }
G_BEGIN_DECLS FlValue * args
static Dart_Handle ToDart(const DartConverterMapping &val)

◆ ToDart()

static Dart_Handle tonic::DartConverter< DartConverterMapping >::ToDart ( const DartConverterMapping val)
inlinestatic

Definition at line 21 of file dart_converter.h.

References FML_LOG.

21  {
22  if (!val) {
23  return Dart_Null();
24  }
25 
26  auto dart_list_handle = Dart_NewListOfTypeFilled(
27  ToDartTypeHandle<size_t>(), // type
28  CreateZeroInitializedDartObject<size_t>(), // sentinel
29  val->GetSize() // size
30  );
31 
32  if (Dart_IsError(dart_list_handle)) {
33  FML_LOG(ERROR) << "Error while attempting to allocate a list: "
34  << Dart_GetError(dart_list_handle);
35  return dart_list_handle;
36  }
37 
38  if (val->GetSize() == 0) {
39  // Nothing to copy. Just return the zero sized list.
40  return dart_list_handle;
41  }
42 
43  auto result = Dart_ListSetAsBytes(dart_list_handle, // list
44  0, // offset
45  val->GetMapping(), // native array,
46  val->GetSize() // length
47  );
48 
49  if (Dart_IsError(result)) {
50  FML_LOG(ERROR) << "Error while attempting to create a Dart list: "
51  << Dart_GetError(result);
52  return result;
53  }
54 
55  return dart_list_handle;
56  }
#define FML_LOG(severity)
Definition: logging.h:65

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