Flutter Engine
The 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
27using tonic::ToDart;
28
29namespace fuchsia {
30namespace dart {
31namespace {
32
33static tonic::DartLibraryNatives* g_natives;
34
35tonic::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
49static struct NativeEntries {
50 const char* name;
53} Entries[] = {FIDL_NATIVE_LIST(REGISTER_FUNCTION)};
54
57 bool* auto_setup_scope) {
58 const char* function_name = nullptr;
60 if (Dart_IsError(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
79const 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
92void SetReturnCode(Dart_NativeArguments arguments) {
93 int64_t return_code;
94 Dart_Handle status =
95 Dart_GetNativeIntegerArgument(arguments, 0, &return_code);
96 if (!tonic::CheckAndHandleError(status)) {
98 }
99}
100
101} // namespace
102
103void Initialize(zx::channel directory_request,
104 std::optional<zx::eventpair> view_ref) {
106
107 Dart_Handle library = Dart_LookupLibrary(ToDart("dart:fuchsia"));
110 library, fuchsia::dart::NativeLookup, fuchsia::dart::NativeSymbol);
112
113 auto dart_state = tonic::DartState::Current();
114 std::unique_ptr<tonic::DartClassProvider> fuchsia_class_provider(
115 new tonic::DartClassProvider(dart_state, "dart:fuchsia"));
116 dart_state->class_library().add_provider("fuchsia",
117 std::move(fuchsia_class_provider));
118
119 if (directory_request) {
121 library, ToDart("_outgoingServices"),
122 ToDart(zircon::dart::Handle::Create(std::move(directory_request))));
124 }
125
126 if (view_ref) {
128 library, ToDart("_viewRef"),
129 ToDart(zircon::dart::Handle::Create((*view_ref).release())));
131 }
132}
133
134} // namespace dart
135} // namespace fuchsia
const uint8_t * GetSymbol(Dart_NativeFunction native_function)
Dart_NativeFunction GetNativeFunction(Dart_Handle name, int argument_count, bool *auto_setup_scope)
void SetReturnCode(uint32_t return_code)
Definition: dart_state.cc:66
static DartState * Current()
Definition: dart_state.cc:56
static fml::RefPtr< Handle > Create(zx_handle_t handle)
Definition: handle.cc:28
DART_EXPORT Dart_Handle Dart_SetNativeResolver(Dart_Handle library, Dart_NativeEntryResolver resolver, Dart_NativeEntrySymbol symbol)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_SetField(Dart_Handle container, Dart_Handle name, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_GetNativeIntegerArgument(Dart_NativeArguments args, int index, int64_t *value)
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle str, const char **cstr)
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
GAsyncResult * result
#define FML_CHECK(condition)
Definition: logging.h:85
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_NativeFunction function
Definition: fuchsia.cc:51
const char * name
Definition: fuchsia.cc:50
#define FIDL_NATIVE_LIST(V)
Definition: fuchsia.cc:45
#define REGISTER_FUNCTION(name, count)
Definition: fuchsia.cc:41
int argument_count
Definition: fuchsia.cc:52
#define DECLARE_FUNCTION(name, count)
Definition: fuchsia.cc:42
Definition: dart_vm.cc:33
static Dart_NativeFunction NativeLookup(Dart_Handle name, int argc, bool *auto_setup_scope)
const char *const function_name
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
void Initialize(zx::channel directory_request, std::optional< zx::eventpair > view_ref)
Initializes Dart bindings for the Fuchsia application model.
Definition: fuchsia.cc:103
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition: dart_error.cc:33
void Initialize()
Definition: natives.cc:68