Flutter Engine
The Flutter Engine
builtin_natives.cc
Go to the documentation of this file.
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 <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
10#include "include/dart_api.h"
12
13#include "platform/assert.h"
14
15#include "bin/builtin.h"
16#include "bin/dartutils.h"
17#include "bin/file.h"
18#include "bin/io_natives.h"
19#include "bin/platform.h"
20
21namespace dart {
22namespace bin {
23
24// Lists the native functions implementing basic functionality in
25// standalone dart, such as printing, file I/O, and platform information.
26// Advanced I/O classes like sockets and process management are implemented
27// using functions listed in io_natives.cc.
28#define BUILTIN_NATIVE_LIST(V) V(Builtin_PrintString, 1)
29
31
32static struct NativeEntries {
33 const char* name_;
37
40}
41
42/**
43 * Looks up native functions in both libdart_builtin and libdart_io.
44 */
45Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name,
47 bool* auto_setup_scope) {
48 const char* function_name = nullptr;
50 if (Dart_IsError(err)) {
52 }
53 ASSERT(function_name != nullptr);
54 ASSERT(auto_setup_scope != nullptr);
55 *auto_setup_scope = true;
56 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries);
57 for (int i = 0; i < num_entries; i++) {
58 struct NativeEntries* entry = &(BuiltinEntries[i]);
59 if ((strcmp(function_name, entry->name_) == 0) &&
60 (entry->argument_count_ == argument_count)) {
61 return reinterpret_cast<Dart_NativeFunction>(entry->function_);
62 }
63 }
65 IONativeLookup(name, argument_count, auto_setup_scope);
66 if (result == nullptr) {
68 }
69 return result;
70}
71
72const uint8_t* Builtin::NativeSymbol(Dart_NativeFunction nf) {
73 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries);
74 for (int i = 0; i < num_entries; i++) {
75 struct NativeEntries* entry = &(BuiltinEntries[i]);
76 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) {
77 return reinterpret_cast<const uint8_t*>(entry->name_);
78 }
79 }
80 return IONativeSymbol(nf);
81}
82
83// Implementation of native functions which are used for some
84// test/debug functionality in standalone dart mode.
86 intptr_t length = 0;
89 if (Dart_IsError(result)) {
91 }
92 intptr_t new_length = length + 1;
93 uint8_t* chars = Dart_ScopeAllocate(new_length);
94 ASSERT(chars != nullptr);
96 if (Dart_IsError(result)) {
98 }
99 chars[length] = '\n';
100
101 // Uses fwrite to support printing NUL bytes.
102 intptr_t res = fwrite(chars, 1, new_length, stdout);
103 ASSERT(res == new_length);
104 fflush(stdout);
105 if (ShouldCaptureStdout()) {
106 // For now we report print output on the Stdout stream.
107 const char* res =
108 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", chars, new_length);
109 ASSERT(res == nullptr);
110 }
111}
112
113} // namespace bin
114} // namespace dart
#define UNREACHABLE()
Definition: assert.h:248
#define FUNCTION_NAME(name)
Definition: builtin.h:19
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
#define ASSERT(E)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
#define REGISTER_FUNCTION(name, count)
Definition: fuchsia.cc:41
int argument_count
Definition: fuchsia.cc:52
#define DECLARE_FUNCTION(name, count)
Definition: fuchsia.cc:42
size_t length
bool ShouldCaptureStdout()
Definition: file_support.cc:33
BUILTIN_NATIVE_LIST(DECLARE_FUNCTION)
static struct dart::bin::NativeEntries BuiltinEntries[]
const uint8_t * IONativeSymbol(Dart_NativeFunction nf)
Definition: io_natives.cc:235
Dart_NativeFunction IONativeLookup(Dart_Handle name, int argument_count, bool *auto_setup_scope)
Definition: io_natives.cc:215
void Builtin_DummyNative(Dart_NativeArguments args)
void FUNCTION_NAME() Builtin_PrintString(Dart_NativeArguments args)
Definition: dart_vm.cc:33
const char *const name
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT uint8_t * Dart_ScopeAllocate(intptr_t size)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT char * Dart_ServiceSendDataEvent(const char *stream_id, const char *event_kind, const uint8_t *bytes, intptr_t bytes_length)
DART_EXPORT Dart_Handle Dart_CopyUTF8EncodingOfString(Dart_Handle str, uint8_t *utf8_array, intptr_t length)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
const char *const function_name
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, const char **cstr)
DART_EXPORT Dart_Handle Dart_StringUTF8Length(Dart_Handle str, intptr_t *len)
Dart_NativeFunction function_