Flutter Engine
The Flutter Engine
native_functions.c
Go to the documentation of this file.
1// Copyright (c) 2021, 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 <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9// TODO(dartbug.com/40579): This requires static linking to either link
10// dart.exe or dart_precompiled_runtime.exe on Windows.
11// The sample currently fails on Windows in AOT mode.
12#include "include/dart_api.h"
13
14#define ENSURE(X) \
15 if (!(X)) { \
16 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, "Check failed: " #X); \
17 exit(1); \
18 }
19
20#define ENSURE_VALID(X) ENSURE(!Dart_IsError(X))
21
22//
23// Functions under test.
24//
25
27 int64_t arg = 0;
28 Dart_GetNativeIntegerArgument(args, /*index=*/0, &arg);
30}
31
33 int64_t arg = 0;
34 int64_t result = 0;
35 for (int i = 0; i < 20; i++) {
36 Dart_GetNativeIntegerArgument(args, /*index=*/i, &arg);
37 result += arg;
38 }
40}
41
43 double arg = 0.0;
44 Dart_GetNativeDoubleArgument(args, /*index=*/0, &arg);
46}
47
49 double arg = 0;
50 double result = 0;
51 for (int i = 0; i < 20; i++) {
52 Dart_GetNativeDoubleArgument(args, /*index=*/i, &arg);
53 result += arg;
54 }
56}
57
59 Dart_Handle arg = Dart_GetNativeArgument(args, /*index=*/0);
61}
62
64 Dart_Handle arg = Dart_GetNativeArgument(args, /*index=*/0);
66}
67
68//
69// Test helpers.
70//
71
73 Dart_Handle root_lib = Dart_RootLibrary();
74 Dart_Handle lib_url = Dart_LibraryUrl(root_lib);
75 ENSURE_VALID(lib_url);
76 return lib_url;
77}
78
80 int num_of_arguments,
81 bool* auto_setup_scope) {
83
84 ENSURE(auto_setup_scope != NULL);
85 *auto_setup_scope = true;
86
87 const char* name_str = NULL;
89
90 if (strcmp(name_str, "Function1Uint8") == 0 && num_of_arguments == 1) {
91 return &Function1Uint8;
92 } else if (strcmp(name_str, "Function20Int64") == 0 &&
93 num_of_arguments == 20) {
94 return &Function20Int64;
95 } else if (strcmp(name_str, "Function1Double") == 0 &&
96 num_of_arguments == 1) {
97 return &Function1Double;
98 } else if (strcmp(name_str, "Function20Double") == 0 &&
99 num_of_arguments == 20) {
100 return &Function20Double;
101 } else if (strcmp(name_str, "Function1Handle") == 0 &&
102 num_of_arguments == 1) {
103 return &Function1Handle;
104 } else if (strcmp(name_str, "Function20Handle") == 0 &&
105 num_of_arguments == 20) {
106 return &Function20Handle;
107 }
108
109 // Unreachable in benchmark.
110 ENSURE(false);
111}
112
114 Dart_Handle library = Dart_LookupLibrary(url);
115 ENSURE_VALID(library);
119}
void * Function1Handle(void *a)
uint8_t Function1Uint8(uint8_t x)
void * Function20Handle(void *a, void *b, void *c, void *d, void *e, void *f, void *g, void *h, void *i, void *j, void *k, void *l, void *m, void *n, void *o, void *p, void *q, void *r, void *s, void *t)
int64_t Function20Int64(int64_t a, int64_t b, int64_t c, int64_t d, int64_t e, int64_t f, int64_t g, int64_t h, int64_t i, int64_t j, int64_t k, int64_t l, int64_t m, int64_t n, int64_t o, int64_t p, int64_t q, int64_t r, int64_t s, int64_t t)
double Function1Double(double x)
double Function20Double(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j, double k, double l, double m, double n, double o, double p, double q, double r, double s, double t)
DART_EXPORT Dart_Handle GetRootLibraryUrl()
DART_EXPORT void SetNativeResolverForTest(Dart_Handle url)
#define ENSURE_VALID(X)
Dart_NativeFunction NativeEntryResolver(Dart_Handle name, int num_of_arguments, bool *auto_setup_scope)
#define ENSURE(X)
DART_EXPORT void Dart_SetIntegerReturnValue(Dart_NativeArguments args, int64_t retval)
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_SetDoubleReturnValue(Dart_NativeArguments args, double retval)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
DART_EXPORT Dart_Handle Dart_GetNativeIntegerArgument(Dart_NativeArguments args, int index, int64_t *value)
DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, Dart_Handle retval)
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 Dart_Handle Dart_LibraryUrl(Dart_Handle library)
DART_EXPORT bool Dart_IsString(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_RootLibrary(void)
DART_EXPORT Dart_Handle Dart_GetNativeDoubleArgument(Dart_NativeArguments args, int index, double *value)
#define DART_EXPORT
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32