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 | 10 |
11 #include "bin/builtin.h" | 11 #include "bin/builtin.h" |
12 #include "bin/io_natives.h" | 12 #include "bin/io_natives.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 namespace bin { | 15 namespace bin { |
16 | 16 |
17 // Lists the native function implementing basic logging facility. | 17 // Lists the native function implementing basic logging facility. |
18 #define BUILTIN_NATIVE_LIST(V) V(Builtin_PrintString, 1) | 18 #define BUILTIN_NATIVE_LIST(V) V(Builtin_PrintString, 1) |
19 | 19 |
20 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); | 20 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); |
21 | 21 |
22 static struct NativeEntries { | 22 static struct NativeEntries { |
23 const char* name_; | 23 const char* name_; |
24 Dart_NativeFunction function_; | 24 Dart_NativeFunction function_; |
25 int argument_count_; | 25 int argument_count_; |
26 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; | 26 } BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)}; |
27 | 27 |
28 | |
29 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, | 28 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, |
30 int argument_count, | 29 int argument_count, |
31 bool* auto_setup_scope) { | 30 bool* auto_setup_scope) { |
32 const char* function_name = NULL; | 31 const char* function_name = NULL; |
33 Dart_Handle result = Dart_StringToCString(name, &function_name); | 32 Dart_Handle result = Dart_StringToCString(name, &function_name); |
34 DART_CHECK_VALID(result); | 33 DART_CHECK_VALID(result); |
35 ASSERT(function_name != NULL); | 34 ASSERT(function_name != NULL); |
36 ASSERT(auto_setup_scope != NULL); | 35 ASSERT(auto_setup_scope != NULL); |
37 *auto_setup_scope = true; | 36 *auto_setup_scope = true; |
38 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | 37 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
39 for (int i = 0; i < num_entries; i++) { | 38 for (int i = 0; i < num_entries; i++) { |
40 struct NativeEntries* entry = &(BuiltinEntries[i]); | 39 struct NativeEntries* entry = &(BuiltinEntries[i]); |
41 if ((strcmp(function_name, entry->name_) == 0) && | 40 if ((strcmp(function_name, entry->name_) == 0) && |
42 (entry->argument_count_ == argument_count)) { | 41 (entry->argument_count_ == argument_count)) { |
43 return reinterpret_cast<Dart_NativeFunction>(entry->function_); | 42 return reinterpret_cast<Dart_NativeFunction>(entry->function_); |
44 } | 43 } |
45 } | 44 } |
46 return IONativeLookup(name, argument_count, auto_setup_scope); | 45 return IONativeLookup(name, argument_count, auto_setup_scope); |
47 } | 46 } |
48 | 47 |
49 | |
50 const uint8_t* Builtin::NativeSymbol(Dart_NativeFunction nf) { | 48 const uint8_t* Builtin::NativeSymbol(Dart_NativeFunction nf) { |
51 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | 49 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
52 for (int i = 0; i < num_entries; i++) { | 50 for (int i = 0; i < num_entries; i++) { |
53 struct NativeEntries* entry = &(BuiltinEntries[i]); | 51 struct NativeEntries* entry = &(BuiltinEntries[i]); |
54 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) { | 52 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) { |
55 return reinterpret_cast<const uint8_t*>(entry->name_); | 53 return reinterpret_cast<const uint8_t*>(entry->name_); |
56 } | 54 } |
57 } | 55 } |
58 return IONativeSymbol(nf); | 56 return IONativeSymbol(nf); |
59 } | 57 } |
60 | 58 |
61 | |
62 // Implementation of native functions which are used for some | 59 // Implementation of native functions which are used for some |
63 // test/debug functionality in standalone dart mode. | 60 // test/debug functionality in standalone dart mode. |
64 void FUNCTION_NAME(Builtin_PrintString)(Dart_NativeArguments args) { | 61 void FUNCTION_NAME(Builtin_PrintString)(Dart_NativeArguments args) { |
65 Dart_EnterScope(); | 62 Dart_EnterScope(); |
66 intptr_t length = 0; | 63 intptr_t length = 0; |
67 uint8_t* chars = NULL; | 64 uint8_t* chars = NULL; |
68 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 65 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
69 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); | 66 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length); |
70 if (Dart_IsError(result)) { | 67 if (Dart_IsError(result)) { |
71 // TODO(turnidge): Consider propagating some errors here. What if | 68 // TODO(turnidge): Consider propagating some errors here. What if |
72 // an isolate gets interrupted by the embedder in the middle of | 69 // an isolate gets interrupted by the embedder in the middle of |
73 // Dart_StringToUTF8? We need to make sure not to swallow the | 70 // Dart_StringToUTF8? We need to make sure not to swallow the |
74 // interrupt. | 71 // interrupt. |
75 fputs(Dart_GetError(result), stdout); | 72 fputs(Dart_GetError(result), stdout); |
76 } else { | 73 } else { |
77 fwrite(chars, sizeof(*chars), length, stdout); | 74 fwrite(chars, sizeof(*chars), length, stdout); |
78 } | 75 } |
79 fputc('\n', stdout); | 76 fputc('\n', stdout); |
80 fflush(stdout); | 77 fflush(stdout); |
81 Dart_ExitScope(); | 78 Dart_ExitScope(); |
82 } | 79 } |
83 | 80 |
84 } // namespace bin | 81 } // namespace bin |
85 } // namespace dart | 82 } // namespace dart |
OLD | NEW |