1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/bootstrap.h"
6
7#include "include/dart_api.h"
8
9#include "vm/bootstrap_natives.h"
10#include "vm/dart_api_impl.h"
11#include "vm/object.h"
12#include "vm/object_store.h"
13#include "vm/service_isolate.h"
14
15namespace dart {
16
17// Helper macros for declaring and defining native entries.
18#define REGISTER_NATIVE_ENTRY(name, count) \
19 {"" #name, BootstrapNatives::DN_##name, count},
20
21// List all native functions implemented in the vm or core bootstrap dart
22// libraries so that we can resolve the native function to it's entry
23// point.
24static struct NativeEntries {
25 const char* name_;
26 BootstrapNativeFunction function_;
27 int argument_count_;
28} BootStrapEntries[] = {BOOTSTRAP_NATIVE_LIST(REGISTER_NATIVE_ENTRY)
29#if !defined(DART_PRECOMPILED_RUNTIME)
30 MIRRORS_BOOTSTRAP_NATIVE_LIST(REGISTER_NATIVE_ENTRY)
31#endif // !DART_PRECOMPILED_RUNTIME
32};
33
34Dart_NativeFunction BootstrapNatives::Lookup(Dart_Handle name,
35 int argument_count,
36 bool* auto_setup_scope) {
37 Thread* thread = Thread::Current();
38 TransitionNativeToVM transition(thread);
39 const Object& obj = Object::Handle(thread->zone(), Api::UnwrapHandle(name));
40 if (!obj.IsString()) {
41 return NULL;
42 }
43 ASSERT(auto_setup_scope);
44 *auto_setup_scope = false;
45 const char* function_name = obj.ToCString();
46 ASSERT(function_name != NULL);
47 int num_entries = sizeof(BootStrapEntries) / sizeof(struct NativeEntries);
48 for (int i = 0; i < num_entries; i++) {
49 struct NativeEntries* entry = &(BootStrapEntries[i]);
50 if ((strcmp(function_name, entry->name_) == 0) &&
51 (entry->argument_count_ == argument_count)) {
52 return reinterpret_cast<Dart_NativeFunction>(entry->function_);
53 }
54 }
55 return NULL;
56}
57
58const uint8_t* BootstrapNatives::Symbol(Dart_NativeFunction nf) {
59 int num_entries = sizeof(BootStrapEntries) / sizeof(struct NativeEntries);
60 for (int i = 0; i < num_entries; i++) {
61 struct NativeEntries* entry = &(BootStrapEntries[i]);
62 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) {
63 return reinterpret_cast<const uint8_t*>(entry->name_);
64 }
65 }
66 return NULL;
67}
68
69void Bootstrap::SetupNativeResolver() {
70 Library& library = Library::Handle();
71
72 Dart_NativeEntryResolver resolver = BootstrapNatives::Lookup;
73
74 Dart_NativeEntrySymbol symbol_resolver = BootstrapNatives::Symbol;
75
76 library = Library::AsyncLibrary();
77 ASSERT(!library.IsNull());
78 library.set_native_entry_resolver(resolver);
79 library.set_native_entry_symbol_resolver(symbol_resolver);
80
81 library = Library::CollectionLibrary();
82 ASSERT(!library.IsNull());
83 library.set_native_entry_resolver(resolver);
84 library.set_native_entry_symbol_resolver(symbol_resolver);
85
86 library = Library::ConvertLibrary();
87 ASSERT(!library.IsNull());
88 library.set_native_entry_resolver(resolver);
89 library.set_native_entry_symbol_resolver(symbol_resolver);
90
91 library = Library::CoreLibrary();
92 ASSERT(!library.IsNull());
93 library.set_native_entry_resolver(resolver);
94 library.set_native_entry_symbol_resolver(symbol_resolver);
95
96 library = Library::DeveloperLibrary();
97 ASSERT(!library.IsNull());
98 library.set_native_entry_resolver(resolver);
99 library.set_native_entry_symbol_resolver(symbol_resolver);
100
101 library = Library::FfiLibrary();
102 ASSERT(!library.IsNull());
103 library.set_native_entry_resolver(resolver);
104 library.set_native_entry_symbol_resolver(symbol_resolver);
105
106 library = Library::InternalLibrary();
107 ASSERT(!library.IsNull());
108 library.set_native_entry_resolver(resolver);
109 library.set_native_entry_symbol_resolver(symbol_resolver);
110
111 library = Library::IsolateLibrary();
112 ASSERT(!library.IsNull());
113 library.set_native_entry_resolver(resolver);
114 library.set_native_entry_symbol_resolver(symbol_resolver);
115
116 library = Library::MathLibrary();
117 ASSERT(!library.IsNull());
118 library.set_native_entry_resolver(resolver);
119 library.set_native_entry_symbol_resolver(symbol_resolver);
120
121#if !defined(DART_PRECOMPILED_RUNTIME)
122 library = Library::MirrorsLibrary();
123 ASSERT(!library.IsNull());
124 library.set_native_entry_resolver(resolver);
125 library.set_native_entry_symbol_resolver(symbol_resolver);
126#endif
127
128 library = Library::TypedDataLibrary();
129 ASSERT(!library.IsNull());
130 library.set_native_entry_resolver(resolver);
131 library.set_native_entry_symbol_resolver(symbol_resolver);
132
133 library = Library::VMServiceLibrary();
134 ASSERT(!library.IsNull());
135 library.set_native_entry_resolver(resolver);
136 library.set_native_entry_symbol_resolver(symbol_resolver);
137
138 library = Library::WasmLibrary();
139 ASSERT(!library.IsNull());
140 library.set_native_entry_resolver(resolver);
141 library.set_native_entry_symbol_resolver(symbol_resolver);
142}
143
144bool Bootstrap::IsBootstrapResolver(Dart_NativeEntryResolver resolver) {
145 return resolver == BootstrapNatives::Lookup;
146}
147
148} // namespace dart
149