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 "bin/builtin.h" | 5 #include "bin/builtin.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 Dart_NativeFunction function_; | 77 Dart_NativeFunction function_; |
78 int argument_count_; | 78 int argument_count_; |
79 } BuiltinEntries[] = { | 79 } BuiltinEntries[] = { |
80 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) | 80 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) |
81 }; | 81 }; |
82 | 82 |
83 | 83 |
84 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, | 84 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, |
85 int argument_count) { | 85 int argument_count) { |
86 const char* function_name = NULL; | 86 const char* function_name = NULL; |
87 Dart_Handle result = Dart_StringToCString(name, &function_name); | 87 Dart_Handle result = Dart_StringAsCString(name, &function_name); |
88 DART_CHECK_VALID(result); | 88 DART_CHECK_VALID(result); |
89 ASSERT(function_name != NULL); | 89 ASSERT(function_name != NULL); |
90 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | 90 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
91 for (int i = 0; i < num_entries; i++) { | 91 for (int i = 0; i < num_entries; i++) { |
92 struct NativeEntries* entry = &(BuiltinEntries[i]); | 92 struct NativeEntries* entry = &(BuiltinEntries[i]); |
93 if (!strcmp(function_name, entry->name_) && | 93 if (!strcmp(function_name, entry->name_) && |
94 (entry->argument_count_ == argument_count)) { | 94 (entry->argument_count_ == argument_count)) { |
95 return reinterpret_cast<Dart_NativeFunction>(entry->function_); | 95 return reinterpret_cast<Dart_NativeFunction>(entry->function_); |
96 } | 96 } |
97 } | 97 } |
98 return NULL; | 98 return NULL; |
99 } | 99 } |
100 | 100 |
101 | 101 |
102 // Implementation of native functions which are used for some | 102 // Implementation of native functions which are used for some |
103 // test/debug functionality in standalone dart mode. | 103 // test/debug functionality in standalone dart mode. |
104 | 104 |
105 void Builtin::PrintString(FILE* out, Dart_Handle str) { | 105 void Builtin::PrintString(FILE* out, Dart_Handle str) { |
106 const uint8_t* characters = NULL; | 106 intptr_t length = 0; |
107 intptr_t length; | 107 Dart_Handle result = Dart_StringLength(str, &length); |
108 Dart_Handle result = Dart_StringToBytes(str, &characters, &length); | 108 DART_CHECK_VALID(result); |
| 109 uint8_t* chars = reinterpret_cast<uint8_t*>(malloc(length * sizeof(uint8_t))); |
| 110 result = Dart_StringAsUTF8(str, chars, &length); |
109 if (Dart_IsError(result)) { | 111 if (Dart_IsError(result)) { |
110 // TODO(turnidge): Consider propagating some errors here. What if | 112 // TODO(turnidge): Consider propagating some errors here. What if |
111 // an isolate gets interrupted by the embedder in the middle of | 113 // an isolate gets interrupted by the embedder in the middle of |
112 // Dart_StringToBytes? We need to make sure not to swallow the | 114 // Dart_StringToBytes? We need to make sure not to swallow the |
113 // interrupt. | 115 // interrupt. |
114 fputs(Dart_GetError(result), out); | 116 fputs(Dart_GetError(result), out); |
115 } else { | 117 } else { |
116 fwrite(characters, sizeof(*characters), length, out); | 118 fwrite(chars, sizeof(*chars), length, out); |
117 } | 119 } |
118 fputc('\n', out); | 120 fputc('\n', out); |
119 fflush(out); | 121 fflush(out); |
| 122 free(chars); |
120 } | 123 } |
121 | 124 |
122 | 125 |
123 void FUNCTION_NAME(Logger_PrintString)(Dart_NativeArguments args) { | 126 void FUNCTION_NAME(Logger_PrintString)(Dart_NativeArguments args) { |
124 Dart_EnterScope(); | 127 Dart_EnterScope(); |
125 Builtin::PrintString(stdout, Dart_GetNativeArgument(args, 0)); | 128 Builtin::PrintString(stdout, Dart_GetNativeArgument(args, 0)); |
126 Dart_ExitScope(); | 129 Dart_ExitScope(); |
127 } | 130 } |
128 | 131 |
129 | 132 |
130 void FUNCTION_NAME(Exit)(Dart_NativeArguments args) { | 133 void FUNCTION_NAME(Exit)(Dart_NativeArguments args) { |
131 Dart_EnterScope(); | 134 Dart_EnterScope(); |
132 int64_t status = 0; | 135 int64_t status = 0; |
133 // Ignore result if passing invalid argument and just exit 0. | 136 // Ignore result if passing invalid argument and just exit 0. |
134 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &status); | 137 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &status); |
135 Dart_ExitScope(); | 138 Dart_ExitScope(); |
136 exit(status); | 139 exit(status); |
137 } | 140 } |
OLD | NEW |