Flutter Engine
natives.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 
5 #include "natives.h"
6 
7 #include <zircon/syscalls.h>
8 
9 #include <cstring>
10 #include <memory>
11 #include <vector>
12 
13 #include "handle.h"
14 #include "handle_waiter.h"
15 #include "system.h"
16 #include "third_party/dart/runtime/include/dart_api.h"
24 
25 using tonic::ToDart;
26 
27 namespace zircon {
28 namespace dart {
29 namespace {
30 
31 static tonic::DartLibraryNatives* g_natives;
32 
33 tonic::DartLibraryNatives* InitNatives() {
36  Handle::RegisterNatives(natives);
37  System::RegisterNatives(natives);
38 
39  return natives;
40 }
41 
42 Dart_NativeFunction NativeLookup(Dart_Handle name,
43  int argument_count,
44  bool* auto_setup_scope) {
45  const char* function_name = nullptr;
46  Dart_Handle result = Dart_StringToCString(name, &function_name);
47  if (Dart_IsError(result)) {
48  Dart_PropagateError(result);
49  }
50  FML_DCHECK(function_name != nullptr);
51  FML_DCHECK(auto_setup_scope != nullptr);
52  *auto_setup_scope = true;
53  if (!g_natives)
54  g_natives = InitNatives();
55  return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope);
56 }
57 
58 const uint8_t* NativeSymbol(Dart_NativeFunction native_function) {
59  if (!g_natives)
60  g_natives = InitNatives();
61  return g_natives->GetSymbol(native_function);
62 }
63 
64 } // namespace
65 
66 void Initialize() {
67  Dart_Handle library = Dart_LookupLibrary(ToDart("dart:zircon"));
68  FML_CHECK(!tonic::LogIfError(library));
69  Dart_Handle result = Dart_SetNativeResolver(
70  library, zircon::dart::NativeLookup, zircon::dart::NativeSymbol);
71  FML_CHECK(!tonic::LogIfError(result));
72 
73  auto dart_state = tonic::DartState::Current();
74  std::unique_ptr<tonic::DartClassProvider> zircon_class_provider(
75  new tonic::DartClassProvider(dart_state, "dart:zircon"));
76  dart_state->class_library().add_provider("zircon",
77  std::move(zircon_class_provider));
78 }
79 
80 } // namespace dart
81 } // namespace zircon
Definition: handle.cc:14
#define FML_DCHECK(condition)
Definition: logging.h:86
static DartState * Current()
Definition: dart_state.cc:55
int argument_count
Definition: fuchsia.cc:52
Definition: dart_vm.cc:38
const uint8_t * GetSymbol(Dart_NativeFunction native_function)
const char * name
Definition: fuchsia.cc:50
#define FML_CHECK(condition)
Definition: logging.h:68
static void RegisterNatives(tonic::DartLibraryNatives *natives)
static void RegisterNatives(tonic::DartLibraryNatives *natives)
Definition: handle.cc:169
static void RegisterNatives(tonic::DartLibraryNatives *natives)
Definition: system.cc:497
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