OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 #include <stdio.h> |
5 #include <stdlib.h> | 6 #include <stdlib.h> |
6 #include <stdio.h> | |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "include/dart_api.h" | 9 #include "include/dart_api.h" |
10 #include "include/dart_tools_api.h" | 10 #include "include/dart_tools_api.h" |
11 | 11 |
12 #include "platform/assert.h" | 12 #include "platform/assert.h" |
13 | 13 |
14 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
15 #include "bin/dartutils.h" | 15 #include "bin/dartutils.h" |
16 #include "bin/embedded_dart_io.h" | 16 #include "bin/embedded_dart_io.h" |
17 #include "bin/file.h" | 17 #include "bin/file.h" |
18 #include "bin/io_natives.h" | 18 #include "bin/io_natives.h" |
19 #include "bin/platform.h" | 19 #include "bin/platform.h" |
20 | 20 |
21 namespace dart { | 21 namespace dart { |
22 namespace bin { | 22 namespace bin { |
23 | 23 |
24 // Lists the native functions implementing basic functionality in | 24 // Lists the native functions implementing basic functionality in |
25 // standalone dart, such as printing, file I/O, and platform information. | 25 // standalone dart, such as printing, file I/O, and platform information. |
26 // Advanced I/O classes like sockets and process management are implemented | 26 // Advanced I/O classes like sockets and process management are implemented |
27 // using functions listed in io_natives.cc. | 27 // using functions listed in io_natives.cc. |
28 #define BUILTIN_NATIVE_LIST(V) \ | 28 #define BUILTIN_NATIVE_LIST(V) \ |
29 V(Builtin_PrintString, 1) \ | 29 V(Builtin_PrintString, 1) \ |
30 V(Builtin_GetCurrentDirectory, 0) | 30 V(Builtin_GetCurrentDirectory, 0) |
31 | 31 |
32 | |
33 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); | 32 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); |
34 | 33 |
35 static struct NativeEntries { | 34 static struct NativeEntries { |
36 const char* name_; | 35 const char* name_; |
37 Dart_NativeFunction function_; | 36 Dart_NativeFunction function_; |
38 int argument_count_; | 37 int argument_count_; |
39 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; | 38 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; |
40 | 39 |
41 | |
42 void Builtin_DummyNative(Dart_NativeArguments args) { | 40 void Builtin_DummyNative(Dart_NativeArguments args) { |
43 UNREACHABLE(); | 41 UNREACHABLE(); |
44 } | 42 } |
45 | 43 |
46 | |
47 /** | 44 /** |
48 * Looks up native functions in both libdart_builtin and libdart_io. | 45 * Looks up native functions in both libdart_builtin and libdart_io. |
49 */ | 46 */ |
50 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, | 47 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, |
51 int argument_count, | 48 int argument_count, |
52 bool* auto_setup_scope) { | 49 bool* auto_setup_scope) { |
53 const char* function_name = NULL; | 50 const char* function_name = NULL; |
54 Dart_Handle err = Dart_StringToCString(name, &function_name); | 51 Dart_Handle err = Dart_StringToCString(name, &function_name); |
55 DART_CHECK_VALID(err); | 52 DART_CHECK_VALID(err); |
56 ASSERT(function_name != NULL); | 53 ASSERT(function_name != NULL); |
57 ASSERT(auto_setup_scope != NULL); | 54 ASSERT(auto_setup_scope != NULL); |
58 *auto_setup_scope = true; | 55 *auto_setup_scope = true; |
59 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | 56 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
60 for (int i = 0; i < num_entries; i++) { | 57 for (int i = 0; i < num_entries; i++) { |
61 struct NativeEntries* entry = &(BuiltinEntries[i]); | 58 struct NativeEntries* entry = &(BuiltinEntries[i]); |
62 if ((strcmp(function_name, entry->name_) == 0) && | 59 if ((strcmp(function_name, entry->name_) == 0) && |
63 (entry->argument_count_ == argument_count)) { | 60 (entry->argument_count_ == argument_count)) { |
64 return reinterpret_cast<Dart_NativeFunction>(entry->function_); | 61 return reinterpret_cast<Dart_NativeFunction>(entry->function_); |
65 } | 62 } |
66 } | 63 } |
67 Dart_NativeFunction result = | 64 Dart_NativeFunction result = |
68 IONativeLookup(name, argument_count, auto_setup_scope); | 65 IONativeLookup(name, argument_count, auto_setup_scope); |
69 if (result == NULL) { | 66 if (result == NULL) { |
70 result = Builtin_DummyNative; | 67 result = Builtin_DummyNative; |
71 } | 68 } |
72 return result; | 69 return result; |
73 } | 70 } |
74 | 71 |
75 | |
76 const uint8_t* Builtin::NativeSymbol(Dart_NativeFunction nf) { | 72 const uint8_t* Builtin::NativeSymbol(Dart_NativeFunction nf) { |
77 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | 73 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
78 for (int i = 0; i < num_entries; i++) { | 74 for (int i = 0; i < num_entries; i++) { |
79 struct NativeEntries* entry = &(BuiltinEntries[i]); | 75 struct NativeEntries* entry = &(BuiltinEntries[i]); |
80 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) { | 76 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) { |
81 return reinterpret_cast<const uint8_t*>(entry->name_); | 77 return reinterpret_cast<const uint8_t*>(entry->name_); |
82 } | 78 } |
83 } | 79 } |
84 return IONativeSymbol(nf); | 80 return IONativeSymbol(nf); |
85 } | 81 } |
86 | 82 |
87 | |
88 // Implementation of native functions which are used for some | 83 // Implementation of native functions which are used for some |
89 // test/debug functionality in standalone dart mode. | 84 // test/debug functionality in standalone dart mode. |
90 void FUNCTION_NAME(Builtin_PrintString)(Dart_NativeArguments args) { | 85 void FUNCTION_NAME(Builtin_PrintString)(Dart_NativeArguments args) { |
91 intptr_t length = 0; | 86 intptr_t length = 0; |
92 uint8_t* chars = NULL; | 87 uint8_t* chars = NULL; |
93 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 88 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
94 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); | 89 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
95 if (Dart_IsError(result)) { | 90 if (Dart_IsError(result)) { |
96 Dart_PropagateError(result); | 91 Dart_PropagateError(result); |
97 } | 92 } |
98 | 93 |
99 // Uses fwrite to support printing NUL bytes. | 94 // Uses fwrite to support printing NUL bytes. |
100 intptr_t res = fwrite(chars, 1, length, stdout); | 95 intptr_t res = fwrite(chars, 1, length, stdout); |
101 ASSERT(res == length); | 96 ASSERT(res == length); |
102 fputs("\n", stdout); | 97 fputs("\n", stdout); |
103 fflush(stdout); | 98 fflush(stdout); |
104 if (ShouldCaptureStdout()) { | 99 if (ShouldCaptureStdout()) { |
105 // For now we report print output on the Stdout stream. | 100 // For now we report print output on the Stdout stream. |
106 uint8_t newline[] = {'\n'}; | 101 uint8_t newline[] = {'\n'}; |
107 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", chars, length); | 102 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", chars, length); |
108 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", newline, sizeof(newline)); | 103 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", newline, sizeof(newline)); |
109 } | 104 } |
110 } | 105 } |
111 | 106 |
112 } // namespace bin | 107 } // namespace bin |
113 } // namespace dart | 108 } // namespace dart |
OLD | NEW |