Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
native_entry.h
Go to the documentation of this file.
1// Copyright (c) 2011, 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#ifndef RUNTIME_VM_NATIVE_ENTRY_H_
6#define RUNTIME_VM_NATIVE_ENTRY_H_
7
9
10#include "vm/allocation.h"
11#include "vm/exceptions.h"
12#include "vm/heap/verifier.h"
13#include "vm/log.h"
14#include "vm/native_arguments.h"
15#include "vm/native_function.h"
16#include "vm/runtime_entry.h"
17
18namespace dart {
19
20// Forward declarations.
21class Class;
22class String;
23
24#ifdef DEBUG
25#define TRACE_NATIVE_CALL(format, name) \
26 if (FLAG_trace_natives) { \
27 THR_Print("Calling native: " format "\n", name); \
28 }
29#else
30#define TRACE_NATIVE_CALL(format, name) \
31 do { \
32 } while (0)
33#endif
34
36 Zone* zone,
37 NativeArguments* arguments);
38
39#define DEFINE_NATIVE_ENTRY(name, type_argument_count, argument_count) \
40 static ObjectPtr DN_Helper##name(Isolate* isolate, Thread* thread, \
41 Zone* zone, NativeArguments* arguments); \
42 ObjectPtr BootstrapNatives::DN_##name(Thread* thread, Zone* zone, \
43 NativeArguments* arguments) { \
44 TRACE_NATIVE_CALL("%s", "" #name); \
45 ASSERT(arguments->NativeArgCount() == argument_count); \
46 /* Note: a longer type arguments vector may be passed */ \
47 ASSERT(arguments->NativeTypeArgCount() >= type_argument_count); \
48 return DN_Helper##name(thread->isolate(), thread, zone, arguments); \
49 } \
50 static ObjectPtr DN_Helper##name(Isolate* isolate, Thread* thread, \
51 Zone* zone, NativeArguments* arguments)
52
53#define DEFINE_FFI_NATIVE_ENTRY(name, return_type, argument_types) \
54 return_type BootstrapNatives::FN_##name argument_types
55
56// Helpers that throw an argument exception.
58 int num_type_args_expected);
59void DartNativeThrowArgumentException(const Instance& instance);
60
61// Native should throw an exception if the wrong number of type arguments is
62// passed.
63#define NATIVE_TYPE_ARGUMENT_COUNT(expected) \
64 int __num_type_arguments = arguments->NativeTypeArgCount(); \
65 if (__num_type_arguments != expected) { \
66 DartNativeThrowTypeArgumentCountException(__num_type_arguments, expected); \
67 }
68
69#define GET_NATIVE_TYPE_ARGUMENT(name, value) \
70 AbstractType& name = AbstractType::Handle(value);
71
72// Natives should throw an exception if an illegal argument or null is passed.
73// type name = value.
74#define GET_NON_NULL_NATIVE_ARGUMENT(type, name, value) \
75 const Instance& __##name##_instance__ = \
76 Instance::CheckedHandle(zone, value); \
77 if (!__##name##_instance__.Is##type()) { \
78 DartNativeThrowArgumentException(__##name##_instance__); \
79 } \
80 const type& name = type::Cast(__##name##_instance__);
81
82// Natives should throw an exception if an illegal argument is passed.
83// type name = value.
84#define GET_NATIVE_ARGUMENT(type, name, value) \
85 const Instance& __##name##_instance__ = \
86 Instance::CheckedHandle(zone, value); \
87 type& name = type::Handle(zone); \
88 if (!__##name##_instance__.IsNull()) { \
89 if (!__##name##_instance__.Is##type()) { \
90 DartNativeThrowArgumentException(__##name##_instance__); \
91 } \
92 } \
93 name ^= value;
94
95// Helper class for resolving and handling native functions.
96class NativeEntry : public AllStatic {
97 public:
98 static constexpr intptr_t kNumArguments = 1;
99 static constexpr intptr_t kNumCallWrapperArguments = 2;
100
101 // Resolve specified dart native function to the actual native entrypoint.
102 static NativeFunction ResolveNative(const Library& library,
103 const String& function_name,
104 int number_of_arguments,
105 bool* auto_setup_scope);
106 static const uint8_t* ResolveSymbolInLibrary(const Library& library,
107 uword pc);
108 static const uint8_t* ResolveSymbol(uword pc);
109
113
117
121
122 static uword LinkNativeCallEntry();
124
125 private:
126 static void NoScopeNativeCallWrapperNoStackCheck(Dart_NativeArguments args,
128 static void AutoScopeNativeCallWrapperNoStackCheck(Dart_NativeArguments args,
130
131 static void MaybePropagateError(NativeArguments* arguments);
132};
133
134#if !defined(DART_PRECOMPILED_RUNTIME)
135
137 public:
138 explicit NativeEntryData(const TypedData& data) : data_(data) {}
139
141 void set_kind(MethodRecognizer::Kind value) const;
142 static MethodRecognizer::Kind GetKind(TypedDataPtr data);
143
145 void set_trampoline(NativeFunctionWrapper value) const;
146 static NativeFunctionWrapper GetTrampoline(TypedDataPtr data);
147
149 void set_native_function(NativeFunction value) const;
150 static NativeFunction GetNativeFunction(TypedDataPtr data);
151
152 intptr_t argc_tag() const;
153 void set_argc_tag(intptr_t value) const;
154 static intptr_t GetArgcTag(TypedDataPtr data);
155
156 static TypedDataPtr New(MethodRecognizer::Kind kind,
159 intptr_t argc_tag);
160
161 private:
162 struct Payload {
163 NativeFunctionWrapper trampoline;
164 NativeFunction native_function;
165 intptr_t argc_tag;
167 };
168
169 static Payload* FromTypedArray(TypedDataPtr data);
170
171 const TypedData& data_;
172
175};
176
177#endif // !defined(DART_PRECOMPILED_RUNTIME)
178
179} // namespace dart
180
181#endif // RUNTIME_VM_NATIVE_ENTRY_H_
void set_native_function(NativeFunction value) const
static MethodRecognizer::Kind GetKind(TypedDataPtr data)
NativeEntryData(const TypedData &data)
void set_trampoline(NativeFunctionWrapper value) const
static NativeFunction GetNativeFunction(TypedDataPtr data)
NativeFunctionWrapper trampoline() const
void set_kind(MethodRecognizer::Kind value) const
void set_argc_tag(intptr_t value) const
static intptr_t GetArgcTag(TypedDataPtr data)
intptr_t argc_tag() const
static TypedDataPtr New(MethodRecognizer::Kind kind, NativeFunctionWrapper trampoline, NativeFunction native_function, intptr_t argc_tag)
MethodRecognizer::Kind kind() const
NativeFunction native_function() const
static NativeFunctionWrapper GetTrampoline(TypedDataPtr data)
static void AutoScopeNativeCallWrapper(Dart_NativeArguments args, Dart_NativeFunction func)
static constexpr intptr_t kNumArguments
static void BootstrapNativeCallWrapper(Dart_NativeArguments args, Dart_NativeFunction func)
static const uint8_t * ResolveSymbol(uword pc)
static NativeFunction ResolveNative(const Library &library, const String &function_name, int number_of_arguments, bool *auto_setup_scope)
static void NoScopeNativeCallWrapper(Dart_NativeArguments args, Dart_NativeFunction func)
static uword NoScopeNativeCallWrapperEntry()
static void LinkNativeCall(Dart_NativeArguments args)
static uword AutoScopeNativeCallWrapperEntry()
static uword LinkNativeCallEntry()
static uword BootstrapNativeCallWrapperEntry()
static constexpr intptr_t kNumCallWrapperArguments
static const uint8_t * ResolveSymbolInLibrary(const Library &library, uword pc)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition dart_api.h:3010
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition dart_api.h:3198
VkInstance instance
Definition main.cc:48
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
void(* NativeFunctionWrapper)(Dart_NativeArguments args, Dart_NativeFunction func)
uintptr_t uword
Definition globals.h:501
void DartNativeThrowArgumentException(const Instance &instance)
ObjectPtr(* BootstrapNativeFunction)(Thread *thread, Zone *zone, NativeArguments *arguments)
void DartNativeThrowTypeArgumentCountException(int num_type_args, int num_type_args_expected)
const char *const function_name
static int8_t data[kExtLength]
void(* NativeFunction)(NativeArguments *arguments)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581