| 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 | |
| 9 | #include "include/bin/dart_io_api.h" |
| 10 | #include "include/dart_api.h" |
| 11 | #include "include/dart_tools_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 | |
| 21 | namespace dart { |
| 22 | namespace 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 | |
| 30 | BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); |
| 31 | |
| 32 | static struct NativeEntries { |
| 33 | const char* name_; |
| 34 | Dart_NativeFunction function_; |
| 35 | int argument_count_; |
| 36 | } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; |
| 37 | |
| 38 | void Builtin_DummyNative(Dart_NativeArguments args) { |
| 39 | UNREACHABLE(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Looks up native functions in both libdart_builtin and libdart_io. |
| 44 | */ |
| 45 | Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, |
| 46 | int argument_count, |
| 47 | bool* auto_setup_scope) { |
| 48 | const char* function_name = NULL; |
| 49 | Dart_Handle err = Dart_StringToCString(name, &function_name); |
| 50 | if (Dart_IsError(err)) { |
| 51 | Dart_PropagateError(err); |
| 52 | } |
| 53 | ASSERT(function_name != NULL); |
| 54 | ASSERT(auto_setup_scope != NULL); |
| 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 | } |
| 64 | Dart_NativeFunction result = |
| 65 | IONativeLookup(name, argument_count, auto_setup_scope); |
| 66 | if (result == NULL) { |
| 67 | result = Builtin_DummyNative; |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | const 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. |
| 85 | void FUNCTION_NAME(Builtin_PrintString)(Dart_NativeArguments args) { |
| 86 | intptr_t length = 0; |
| 87 | uint8_t* chars = NULL; |
| 88 | Dart_Handle str = Dart_GetNativeArgument(args, 0); |
| 89 | Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
| 90 | if (Dart_IsError(result)) { |
| 91 | Dart_PropagateError(result); |
| 92 | } |
| 93 | |
| 94 | // Uses fwrite to support printing NUL bytes. |
| 95 | intptr_t res = fwrite(chars, 1, length, stdout); |
| 96 | ASSERT(res == length); |
| 97 | fputs("\n" , stdout); |
| 98 | fflush(stdout); |
| 99 | if (ShouldCaptureStdout()) { |
| 100 | // For now we report print output on the Stdout stream. |
| 101 | uint8_t newline[] = {'\n'}; |
| 102 | Dart_ServiceSendDataEvent("Stdout" , "WriteEvent" , chars, length); |
| 103 | Dart_ServiceSendDataEvent("Stdout" , "WriteEvent" , newline, sizeof(newline)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } // namespace bin |
| 108 | } // namespace dart |
| 109 | |