Flutter Engine
fuchsia.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <zircon/syscalls.h>
8 
9 #include <cstdio>
10 #include <cstring>
11 #include <memory>
12 #include <vector>
13 
17 #include "flutter/fml/size.h"
18 #include "third_party/dart/runtime/include/dart_api.h"
26 
27 using tonic::ToDart;
28 
29 namespace fuchsia {
30 namespace dart {
31 namespace {
32 
33 static tonic::DartLibraryNatives* g_natives;
34 
35 tonic::DartLibraryNatives* InitNatives() {
37 
38  return natives;
39 }
40 
41 #define REGISTER_FUNCTION(name, count) {"" #name, name, count},
42 #define DECLARE_FUNCTION(name, count) \
43  extern void name(Dart_NativeArguments args);
44 
45 #define FIDL_NATIVE_LIST(V) V(SetReturnCode, 1)
46 
48 
49 static struct NativeEntries {
50  const char* name;
51  Dart_NativeFunction function;
53 } Entries[] = {FIDL_NATIVE_LIST(REGISTER_FUNCTION)};
54 
55 Dart_NativeFunction NativeLookup(Dart_Handle name,
56  int argument_count,
57  bool* auto_setup_scope) {
58  const char* function_name = nullptr;
59  Dart_Handle result = Dart_StringToCString(name, &function_name);
60  if (Dart_IsError(result)) {
61  Dart_PropagateError(result);
62  }
63  FML_DCHECK(function_name != nullptr);
64  FML_DCHECK(auto_setup_scope != nullptr);
65  *auto_setup_scope = true;
66  size_t num_entries = fml::size(Entries);
67  for (size_t i = 0; i < num_entries; ++i) {
68  const struct NativeEntries& entry = Entries[i];
69  if (!strcmp(function_name, entry.name) &&
70  (entry.argument_count == argument_count)) {
71  return entry.function;
72  }
73  }
74  if (!g_natives)
75  g_natives = InitNatives();
76  return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope);
77 }
78 
79 const uint8_t* NativeSymbol(Dart_NativeFunction native_function) {
80  size_t num_entries = fml::size(Entries);
81  for (size_t i = 0; i < num_entries; ++i) {
82  const struct NativeEntries& entry = Entries[i];
83  if (entry.function == native_function) {
84  return reinterpret_cast<const uint8_t*>(entry.name);
85  }
86  }
87  if (!g_natives)
88  g_natives = InitNatives();
89  return g_natives->GetSymbol(native_function);
90 }
91 
92 void SetReturnCode(Dart_NativeArguments arguments) {
93  int64_t return_code;
94  Dart_Handle status =
95  Dart_GetNativeIntegerArgument(arguments, 0, &return_code);
96  if (!tonic::LogIfError(status)) {
98  }
99 }
100 
101 } // namespace
102 
103 void Initialize(fidl::InterfaceHandle<fuchsia::sys::Environment> environment,
104  zx::channel directory_request,
105  std::optional<zx::eventpair> view_ref) {
107 
108  Dart_Handle library = Dart_LookupLibrary(ToDart("dart:fuchsia"));
109  FML_CHECK(!tonic::LogIfError(library));
110  Dart_Handle result = Dart_SetNativeResolver(
111  library, fuchsia::dart::NativeLookup, fuchsia::dart::NativeSymbol);
112  FML_CHECK(!tonic::LogIfError(result));
113 
114  auto dart_state = tonic::DartState::Current();
115  std::unique_ptr<tonic::DartClassProvider> fuchsia_class_provider(
116  new tonic::DartClassProvider(dart_state, "dart:fuchsia"));
117  dart_state->class_library().add_provider("fuchsia",
118  std::move(fuchsia_class_provider));
119 
120  result = Dart_SetField(
121  library, ToDart("_environment"),
122  ToDart(zircon::dart::Handle::Create(environment.TakeChannel())));
123  FML_CHECK(!tonic::LogIfError(result));
124 
125  if (directory_request) {
126  result = Dart_SetField(
127  library, ToDart("_outgoingServices"),
128  ToDart(zircon::dart::Handle::Create(std::move(directory_request))));
129  FML_CHECK(!tonic::LogIfError(result));
130  }
131 
132  if (view_ref) {
133  result = Dart_SetField(
134  library, ToDart("_viewRef"),
135  ToDart(zircon::dart::Handle::Create((*view_ref).release())));
136  FML_CHECK(!tonic::LogIfError(result));
137  }
138 }
139 
140 } // namespace dart
141 } // namespace fuchsia
#define FIDL_NATIVE_LIST(V)
Definition: fuchsia.cc:45
#define FML_DCHECK(condition)
Definition: logging.h:86
#define REGISTER_FUNCTION(name, count)
Definition: fuchsia.cc:41
static DartState * Current()
Definition: dart_state.cc:55
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
int argument_count
Definition: fuchsia.cc:52
static fml::RefPtr< Handle > Create(zx_handle_t handle)
Definition: handle.cc:50
Definition: dart_vm.cc:38
void Initialize(fidl::InterfaceHandle< fuchsia::sys::Environment > environment, zx::channel directory_request, std::optional< zx::eventpair > view_ref)
Definition: fuchsia.cc:103
const uint8_t * GetSymbol(Dart_NativeFunction native_function)
void SetReturnCode(uint32_t return_code)
Definition: dart_state.cc:65
#define DECLARE_FUNCTION(name, count)
Definition: fuchsia.cc:42
const char * name
Definition: fuchsia.cc:50
#define FML_CHECK(condition)
Definition: logging.h:68
Dart_NativeFunction GetNativeFunction(Dart_Handle name, int argument_count, bool *auto_setup_scope)
Dart_Handle ToDart(const T &object)
bool LogIfError(Dart_Handle handle)
Definition: dart_error.cc:15
void Initialize()
Definition: natives.cc:66