|
| 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 | + |
| 5 | +#include "dart-pkg/fuchsia/sdk_ext/fuchsia.h" |
| 6 | + |
| 7 | +#include <zircon/syscalls.h> |
| 8 | + |
| 9 | +#include <stdio.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include <memory> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "dart-pkg/zircon/sdk_ext/handle.h" |
| 16 | +#include "dart-pkg/zircon/sdk_ext/natives.h" |
| 17 | +#include "dart-pkg/zircon/sdk_ext/system.h" |
| 18 | +#include "flutter/fml/arraysize.h" |
| 19 | +#include "third_party/dart/runtime/include/dart_api.h" |
| 20 | +#include "third_party/tonic/dart_binding_macros.h" |
| 21 | +#include "third_party/tonic/dart_class_library.h" |
| 22 | +#include "third_party/tonic/dart_class_provider.h" |
| 23 | +#include "third_party/tonic/dart_library_natives.h" |
| 24 | +#include "third_party/tonic/dart_state.h" |
| 25 | +#include "third_party/tonic/logging/dart_invoke.h" |
| 26 | +#include "third_party/tonic/typed_data/uint8_list.h" |
| 27 | + |
| 28 | +using tonic::ToDart; |
| 29 | + |
| 30 | +namespace fuchsia { |
| 31 | +namespace dart { |
| 32 | +namespace { |
| 33 | + |
| 34 | +static tonic::DartLibraryNatives* g_natives; |
| 35 | + |
| 36 | +tonic::DartLibraryNatives* InitNatives() { |
| 37 | + tonic::DartLibraryNatives* natives = new tonic::DartLibraryNatives(); |
| 38 | + |
| 39 | + return natives; |
| 40 | +} |
| 41 | + |
| 42 | +#define REGISTER_FUNCTION(name, count) {"" #name, name, count}, |
| 43 | +#define DECLARE_FUNCTION(name, count) \ |
| 44 | + extern void name(Dart_NativeArguments args); |
| 45 | + |
| 46 | +#define FIDL_NATIVE_LIST(V) V(SetReturnCode, 1) |
| 47 | + |
| 48 | +FIDL_NATIVE_LIST(DECLARE_FUNCTION); |
| 49 | + |
| 50 | +static struct NativeEntries { |
| 51 | + const char* name; |
| 52 | + Dart_NativeFunction function; |
| 53 | + int argument_count; |
| 54 | +} Entries[] = {FIDL_NATIVE_LIST(REGISTER_FUNCTION)}; |
| 55 | + |
| 56 | +Dart_NativeFunction NativeLookup(Dart_Handle name, |
| 57 | + int argument_count, |
| 58 | + bool* auto_setup_scope) { |
| 59 | + const char* function_name = nullptr; |
| 60 | + Dart_Handle result = Dart_StringToCString(name, &function_name); |
| 61 | + if (Dart_IsError(result)) { |
| 62 | + Dart_PropagateError(result); |
| 63 | + } |
| 64 | + FML_DCHECK(function_name != nullptr); |
| 65 | + FML_DCHECK(auto_setup_scope != nullptr); |
| 66 | + *auto_setup_scope = true; |
| 67 | + size_t num_entries = arraysize(Entries); |
| 68 | + for (size_t i = 0; i < num_entries; ++i) { |
| 69 | + const struct NativeEntries& entry = Entries[i]; |
| 70 | + if (!strcmp(function_name, entry.name) && |
| 71 | + (entry.argument_count == argument_count)) { |
| 72 | + return entry.function; |
| 73 | + } |
| 74 | + } |
| 75 | + if (!g_natives) |
| 76 | + g_natives = InitNatives(); |
| 77 | + return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope); |
| 78 | +} |
| 79 | + |
| 80 | +const uint8_t* NativeSymbol(Dart_NativeFunction native_function) { |
| 81 | + size_t num_entries = arraysize(Entries); |
| 82 | + for (size_t i = 0; i < num_entries; ++i) { |
| 83 | + const struct NativeEntries& entry = Entries[i]; |
| 84 | + if (entry.function == native_function) { |
| 85 | + return reinterpret_cast<const uint8_t*>(entry.name); |
| 86 | + } |
| 87 | + } |
| 88 | + if (!g_natives) |
| 89 | + g_natives = InitNatives(); |
| 90 | + return g_natives->GetSymbol(native_function); |
| 91 | +} |
| 92 | + |
| 93 | +void SetReturnCode(Dart_NativeArguments arguments) { |
| 94 | + int64_t return_code; |
| 95 | + Dart_Handle status = |
| 96 | + Dart_GetNativeIntegerArgument(arguments, 0, &return_code); |
| 97 | + if (!tonic::LogIfError(status)) { |
| 98 | + tonic::DartState::Current()->SetReturnCode(return_code); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace |
| 103 | + |
| 104 | +void Initialize(fidl::InterfaceHandle<fuchsia::sys::Environment> environment, |
| 105 | + zx::channel directory_request) { |
| 106 | + zircon::dart::Initialize(); |
| 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 | + |
| 133 | +} // namespace dart |
| 134 | +} // namespace fuchsia |
0 commit comments