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/dartutils.h" | 5 #include "bin/dartutils.h" |
6 | 6 |
7 #include "bin/crypto.h" | 7 #include "bin/crypto.h" |
8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
9 #include "bin/extensions.h" | 9 #include "bin/extensions.h" |
10 #include "bin/file.h" | 10 #include "bin/file.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 const char* const DartUtils::kBuiltinLibURL = "dart:_builtin"; | 40 const char* const DartUtils::kBuiltinLibURL = "dart:_builtin"; |
41 const char* const DartUtils::kCoreLibURL = "dart:core"; | 41 const char* const DartUtils::kCoreLibURL = "dart:core"; |
42 const char* const DartUtils::kInternalLibURL = "dart:_internal"; | 42 const char* const DartUtils::kInternalLibURL = "dart:_internal"; |
43 const char* const DartUtils::kIsolateLibURL = "dart:isolate"; | 43 const char* const DartUtils::kIsolateLibURL = "dart:isolate"; |
44 const char* const DartUtils::kIOLibURL = "dart:io"; | 44 const char* const DartUtils::kIOLibURL = "dart:io"; |
45 const char* const DartUtils::kIOLibPatchURL = "dart:io-patch"; | 45 const char* const DartUtils::kIOLibPatchURL = "dart:io-patch"; |
46 const char* const DartUtils::kUriLibURL = "dart:uri"; | 46 const char* const DartUtils::kUriLibURL = "dart:uri"; |
47 const char* const DartUtils::kHttpScheme = "http:"; | 47 const char* const DartUtils::kHttpScheme = "http:"; |
48 const char* const DartUtils::kVMServiceLibURL = "dart:vmservice"; | 48 const char* const DartUtils::kVMServiceLibURL = "dart:vmservice"; |
49 | 49 |
50 | |
51 struct MagicNumberData { | 50 struct MagicNumberData { |
52 static const intptr_t kLength = 4; | 51 static const intptr_t kLength = 4; |
53 | 52 |
54 const uint8_t bytes[kLength]; | 53 const uint8_t bytes[kLength]; |
55 bool should_skip; | 54 bool should_skip; |
56 }; | 55 }; |
57 | 56 |
58 | |
59 MagicNumberData snapshot_magic_number = {{0xf5, 0xf5, 0xdc, 0xdc}, true}; | 57 MagicNumberData snapshot_magic_number = {{0xf5, 0xf5, 0xdc, 0xdc}, true}; |
60 MagicNumberData kernel_magic_number = {{0x90, 0xab, 0xcd, 0xef}, false}; | 58 MagicNumberData kernel_magic_number = {{0x90, 0xab, 0xcd, 0xef}, false}; |
61 | 59 |
62 | |
63 static bool IsWindowsHost() { | 60 static bool IsWindowsHost() { |
64 #if defined(HOST_OS_WINDOWS) | 61 #if defined(HOST_OS_WINDOWS) |
65 return true; | 62 return true; |
66 #else // defined(HOST_OS_WINDOWS) | 63 #else // defined(HOST_OS_WINDOWS) |
67 return false; | 64 return false; |
68 #endif // defined(HOST_OS_WINDOWS) | 65 #endif // defined(HOST_OS_WINDOWS) |
69 } | 66 } |
70 | 67 |
71 | |
72 const char* DartUtils::MapLibraryUrl(const char* url_string) { | 68 const char* DartUtils::MapLibraryUrl(const char* url_string) { |
73 ASSERT(url_mapping != NULL); | 69 ASSERT(url_mapping != NULL); |
74 // We need to check if the passed in url is found in the url_mapping array, | 70 // We need to check if the passed in url is found in the url_mapping array, |
75 // in that case use the mapped entry. | 71 // in that case use the mapped entry. |
76 intptr_t len = strlen(url_string); | 72 intptr_t len = strlen(url_string); |
77 for (intptr_t idx = 0; idx < url_mapping->count(); idx++) { | 73 for (intptr_t idx = 0; idx < url_mapping->count(); idx++) { |
78 const char* url_name = url_mapping->GetArgument(idx); | 74 const char* url_name = url_mapping->GetArgument(idx); |
79 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { | 75 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { |
80 const char* url_mapped_name = url_name + len + 1; | 76 const char* url_mapped_name = url_name + len + 1; |
81 if (strlen(url_mapped_name) != 0) { | 77 if (strlen(url_mapped_name) != 0) { |
82 return url_mapped_name; // Found a mapping for this URL. | 78 return url_mapped_name; // Found a mapping for this URL. |
83 } | 79 } |
84 } | 80 } |
85 } | 81 } |
86 return NULL; // Did not find a mapping for this URL. | 82 return NULL; // Did not find a mapping for this URL. |
87 } | 83 } |
88 | 84 |
89 | |
90 int64_t DartUtils::GetIntegerValue(Dart_Handle value_obj) { | 85 int64_t DartUtils::GetIntegerValue(Dart_Handle value_obj) { |
91 int64_t value = 0; | 86 int64_t value = 0; |
92 Dart_Handle result = Dart_IntegerToInt64(value_obj, &value); | 87 Dart_Handle result = Dart_IntegerToInt64(value_obj, &value); |
93 if (Dart_IsError(result)) Dart_PropagateError(result); | 88 if (Dart_IsError(result)) Dart_PropagateError(result); |
94 return value; | 89 return value; |
95 } | 90 } |
96 | 91 |
97 | |
98 int64_t DartUtils::GetInt64ValueCheckRange(Dart_Handle value_obj, | 92 int64_t DartUtils::GetInt64ValueCheckRange(Dart_Handle value_obj, |
99 int64_t lower, | 93 int64_t lower, |
100 int64_t upper) { | 94 int64_t upper) { |
101 int64_t value = DartUtils::GetIntegerValue(value_obj); | 95 int64_t value = DartUtils::GetIntegerValue(value_obj); |
102 if (value < lower || upper < value) { | 96 if (value < lower || upper < value) { |
103 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); | 97 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); |
104 } | 98 } |
105 return value; | 99 return value; |
106 } | 100 } |
107 | 101 |
108 | |
109 intptr_t DartUtils::GetIntptrValue(Dart_Handle value_obj) { | 102 intptr_t DartUtils::GetIntptrValue(Dart_Handle value_obj) { |
110 int64_t value = 0; | 103 int64_t value = 0; |
111 Dart_Handle result = Dart_IntegerToInt64(value_obj, &value); | 104 Dart_Handle result = Dart_IntegerToInt64(value_obj, &value); |
112 if (Dart_IsError(result)) Dart_PropagateError(result); | 105 if (Dart_IsError(result)) Dart_PropagateError(result); |
113 if (value < kIntptrMin || kIntptrMax < value) { | 106 if (value < kIntptrMin || kIntptrMax < value) { |
114 Dart_PropagateError(Dart_NewApiError("Value outside intptr_t range")); | 107 Dart_PropagateError(Dart_NewApiError("Value outside intptr_t range")); |
115 } | 108 } |
116 return static_cast<intptr_t>(value); | 109 return static_cast<intptr_t>(value); |
117 } | 110 } |
118 | 111 |
119 | |
120 bool DartUtils::GetInt64Value(Dart_Handle value_obj, int64_t* value) { | 112 bool DartUtils::GetInt64Value(Dart_Handle value_obj, int64_t* value) { |
121 bool valid = Dart_IsInteger(value_obj); | 113 bool valid = Dart_IsInteger(value_obj); |
122 if (valid) { | 114 if (valid) { |
123 Dart_Handle result = Dart_IntegerFitsIntoInt64(value_obj, &valid); | 115 Dart_Handle result = Dart_IntegerFitsIntoInt64(value_obj, &valid); |
124 if (Dart_IsError(result)) Dart_PropagateError(result); | 116 if (Dart_IsError(result)) Dart_PropagateError(result); |
125 } | 117 } |
126 if (!valid) return false; | 118 if (!valid) return false; |
127 Dart_Handle result = Dart_IntegerToInt64(value_obj, value); | 119 Dart_Handle result = Dart_IntegerToInt64(value_obj, value); |
128 if (Dart_IsError(result)) Dart_PropagateError(result); | 120 if (Dart_IsError(result)) Dart_PropagateError(result); |
129 return true; | 121 return true; |
130 } | 122 } |
131 | 123 |
132 | |
133 const char* DartUtils::GetStringValue(Dart_Handle str_obj) { | 124 const char* DartUtils::GetStringValue(Dart_Handle str_obj) { |
134 const char* cstring = NULL; | 125 const char* cstring = NULL; |
135 Dart_Handle result = Dart_StringToCString(str_obj, &cstring); | 126 Dart_Handle result = Dart_StringToCString(str_obj, &cstring); |
136 if (Dart_IsError(result)) Dart_PropagateError(result); | 127 if (Dart_IsError(result)) Dart_PropagateError(result); |
137 return cstring; | 128 return cstring; |
138 } | 129 } |
139 | 130 |
140 | |
141 bool DartUtils::GetBooleanValue(Dart_Handle bool_obj) { | 131 bool DartUtils::GetBooleanValue(Dart_Handle bool_obj) { |
142 bool value = false; | 132 bool value = false; |
143 Dart_Handle result = Dart_BooleanValue(bool_obj, &value); | 133 Dart_Handle result = Dart_BooleanValue(bool_obj, &value); |
144 if (Dart_IsError(result)) Dart_PropagateError(result); | 134 if (Dart_IsError(result)) Dart_PropagateError(result); |
145 return value; | 135 return value; |
146 } | 136 } |
147 | 137 |
148 | |
149 Dart_Handle DartUtils::SetIntegerField(Dart_Handle handle, | 138 Dart_Handle DartUtils::SetIntegerField(Dart_Handle handle, |
150 const char* name, | 139 const char* name, |
151 int64_t val) { | 140 int64_t val) { |
152 return Dart_SetField(handle, NewString(name), Dart_NewInteger(val)); | 141 return Dart_SetField(handle, NewString(name), Dart_NewInteger(val)); |
153 } | 142 } |
154 | 143 |
155 | |
156 Dart_Handle DartUtils::SetStringField(Dart_Handle handle, | 144 Dart_Handle DartUtils::SetStringField(Dart_Handle handle, |
157 const char* name, | 145 const char* name, |
158 const char* val) { | 146 const char* val) { |
159 return Dart_SetField(handle, NewString(name), NewString(val)); | 147 return Dart_SetField(handle, NewString(name), NewString(val)); |
160 } | 148 } |
161 | 149 |
162 | |
163 bool DartUtils::IsDartSchemeURL(const char* url_name) { | 150 bool DartUtils::IsDartSchemeURL(const char* url_name) { |
164 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 151 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
165 // If the URL starts with "dart:" then it is considered as a special | 152 // If the URL starts with "dart:" then it is considered as a special |
166 // library URL which is handled differently from other URLs. | 153 // library URL which is handled differently from other URLs. |
167 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); | 154 return (strncmp(url_name, kDartScheme, kDartSchemeLen) == 0); |
168 } | 155 } |
169 | 156 |
170 | |
171 bool DartUtils::IsHttpSchemeURL(const char* url_name) { | 157 bool DartUtils::IsHttpSchemeURL(const char* url_name) { |
172 static const intptr_t kHttpSchemeLen = strlen(kHttpScheme); | 158 static const intptr_t kHttpSchemeLen = strlen(kHttpScheme); |
173 return (strncmp(url_name, kHttpScheme, kHttpSchemeLen) == 0); | 159 return (strncmp(url_name, kHttpScheme, kHttpSchemeLen) == 0); |
174 } | 160 } |
175 | 161 |
176 | |
177 bool DartUtils::IsDartExtensionSchemeURL(const char* url_name) { | 162 bool DartUtils::IsDartExtensionSchemeURL(const char* url_name) { |
178 static const intptr_t kDartExtensionSchemeLen = strlen(kDartExtensionScheme); | 163 static const intptr_t kDartExtensionSchemeLen = strlen(kDartExtensionScheme); |
179 // If the URL starts with "dartext:" then it is considered as a special | 164 // If the URL starts with "dartext:" then it is considered as a special |
180 // extension library URL which is handled differently from other URLs. | 165 // extension library URL which is handled differently from other URLs. |
181 return (strncmp(url_name, kDartExtensionScheme, kDartExtensionSchemeLen) == | 166 return (strncmp(url_name, kDartExtensionScheme, kDartExtensionSchemeLen) == |
182 0); | 167 0); |
183 } | 168 } |
184 | 169 |
185 | |
186 bool DartUtils::IsDartIOLibURL(const char* url_name) { | 170 bool DartUtils::IsDartIOLibURL(const char* url_name) { |
187 return (strcmp(url_name, kIOLibURL) == 0); | 171 return (strcmp(url_name, kIOLibURL) == 0); |
188 } | 172 } |
189 | 173 |
190 | |
191 bool DartUtils::IsDartBuiltinLibURL(const char* url_name) { | 174 bool DartUtils::IsDartBuiltinLibURL(const char* url_name) { |
192 return (strcmp(url_name, kBuiltinLibURL) == 0); | 175 return (strcmp(url_name, kBuiltinLibURL) == 0); |
193 } | 176 } |
194 | 177 |
195 | |
196 const char* DartUtils::RemoveScheme(const char* url) { | 178 const char* DartUtils::RemoveScheme(const char* url) { |
197 const char* colon = strchr(url, ':'); | 179 const char* colon = strchr(url, ':'); |
198 if (colon == NULL) { | 180 if (colon == NULL) { |
199 return url; | 181 return url; |
200 } else { | 182 } else { |
201 return colon + 1; | 183 return colon + 1; |
202 } | 184 } |
203 } | 185 } |
204 | 186 |
205 | |
206 char* DartUtils::DirName(const char* url) { | 187 char* DartUtils::DirName(const char* url) { |
207 const char* slash = strrchr(url, File::PathSeparator()[0]); | 188 const char* slash = strrchr(url, File::PathSeparator()[0]); |
208 if (slash == NULL) { | 189 if (slash == NULL) { |
209 return strdup(url); | 190 return strdup(url); |
210 } else { | 191 } else { |
211 return StringUtils::StrNDup(url, slash - url + 1); | 192 return StringUtils::StrNDup(url, slash - url + 1); |
212 } | 193 } |
213 } | 194 } |
214 | 195 |
215 | |
216 void* DartUtils::OpenFile(const char* name, bool write) { | 196 void* DartUtils::OpenFile(const char* name, bool write) { |
217 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); | 197 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); |
218 return reinterpret_cast<void*>(file); | 198 return reinterpret_cast<void*>(file); |
219 } | 199 } |
220 | 200 |
221 | |
222 void DartUtils::ReadFile(const uint8_t** data, intptr_t* len, void* stream) { | 201 void DartUtils::ReadFile(const uint8_t** data, intptr_t* len, void* stream) { |
223 ASSERT(data != NULL); | 202 ASSERT(data != NULL); |
224 ASSERT(len != NULL); | 203 ASSERT(len != NULL); |
225 ASSERT(stream != NULL); | 204 ASSERT(stream != NULL); |
226 File* file_stream = reinterpret_cast<File*>(stream); | 205 File* file_stream = reinterpret_cast<File*>(stream); |
227 int64_t file_len = file_stream->Length(); | 206 int64_t file_len = file_stream->Length(); |
228 if ((file_len < 0) || (file_len > kIntptrMax)) { | 207 if ((file_len < 0) || (file_len > kIntptrMax)) { |
229 *data = NULL; | 208 *data = NULL; |
230 *len = -1; // Indicates read was not successful. | 209 *len = -1; // Indicates read was not successful. |
231 return; | 210 return; |
232 } | 211 } |
233 *len = static_cast<intptr_t>(file_len); | 212 *len = static_cast<intptr_t>(file_len); |
234 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*len)); | 213 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*len)); |
235 ASSERT(text_buffer != NULL); | 214 ASSERT(text_buffer != NULL); |
236 if (!file_stream->ReadFully(text_buffer, *len)) { | 215 if (!file_stream->ReadFully(text_buffer, *len)) { |
237 *data = NULL; | 216 *data = NULL; |
238 *len = -1; // Indicates read was not successful. | 217 *len = -1; // Indicates read was not successful. |
239 return; | 218 return; |
240 } | 219 } |
241 *data = text_buffer; | 220 *data = text_buffer; |
242 } | 221 } |
243 | 222 |
244 | |
245 void DartUtils::WriteFile(const void* buffer, | 223 void DartUtils::WriteFile(const void* buffer, |
246 intptr_t num_bytes, | 224 intptr_t num_bytes, |
247 void* stream) { | 225 void* stream) { |
248 ASSERT(stream != NULL); | 226 ASSERT(stream != NULL); |
249 File* file_stream = reinterpret_cast<File*>(stream); | 227 File* file_stream = reinterpret_cast<File*>(stream); |
250 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); | 228 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); |
251 ASSERT(bytes_written); | 229 ASSERT(bytes_written); |
252 } | 230 } |
253 | 231 |
254 | |
255 void DartUtils::CloseFile(void* stream) { | 232 void DartUtils::CloseFile(void* stream) { |
256 File* file = reinterpret_cast<File*>(stream); | 233 File* file = reinterpret_cast<File*>(stream); |
257 file->Release(); | 234 file->Release(); |
258 } | 235 } |
259 | 236 |
260 | |
261 bool DartUtils::EntropySource(uint8_t* buffer, intptr_t length) { | 237 bool DartUtils::EntropySource(uint8_t* buffer, intptr_t length) { |
262 return Crypto::GetRandomBytes(length, buffer); | 238 return Crypto::GetRandomBytes(length, buffer); |
263 } | 239 } |
264 | 240 |
265 | |
266 static Dart_Handle SingleArgDart_Invoke(Dart_Handle lib, | 241 static Dart_Handle SingleArgDart_Invoke(Dart_Handle lib, |
267 const char* method, | 242 const char* method, |
268 Dart_Handle arg) { | 243 Dart_Handle arg) { |
269 const int kNumArgs = 1; | 244 const int kNumArgs = 1; |
270 Dart_Handle dart_args[kNumArgs]; | 245 Dart_Handle dart_args[kNumArgs]; |
271 dart_args[0] = arg; | 246 dart_args[0] = arg; |
272 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); | 247 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); |
273 } | 248 } |
274 | 249 |
275 | |
276 // TODO(iposva): Allocate from the zone instead of leaking error string | 250 // TODO(iposva): Allocate from the zone instead of leaking error string |
277 // here. On the other hand the binary is about to exit anyway. | 251 // here. On the other hand the binary is about to exit anyway. |
278 #define SET_ERROR_MSG(error_msg, format, ...) \ | 252 #define SET_ERROR_MSG(error_msg, format, ...) \ |
279 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ | 253 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ |
280 char* msg = reinterpret_cast<char*>(malloc(len + 1)); \ | 254 char* msg = reinterpret_cast<char*>(malloc(len + 1)); \ |
281 snprintf(msg, len + 1, format, __VA_ARGS__); \ | 255 snprintf(msg, len + 1, format, __VA_ARGS__); \ |
282 *error_msg = msg | 256 *error_msg = msg |
283 | 257 |
284 | |
285 static const uint8_t* ReadFileFully(const char* filename, | 258 static const uint8_t* ReadFileFully(const char* filename, |
286 intptr_t* file_len, | 259 intptr_t* file_len, |
287 const char** error_msg) { | 260 const char** error_msg) { |
288 *file_len = -1; | 261 *file_len = -1; |
289 void* stream = DartUtils::OpenFile(filename, false); | 262 void* stream = DartUtils::OpenFile(filename, false); |
290 if (stream == NULL) { | 263 if (stream == NULL) { |
291 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); | 264 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); |
292 return NULL; | 265 return NULL; |
293 } | 266 } |
294 const uint8_t* text_buffer = NULL; | 267 const uint8_t* text_buffer = NULL; |
295 DartUtils::ReadFile(&text_buffer, file_len, stream); | 268 DartUtils::ReadFile(&text_buffer, file_len, stream); |
296 if (text_buffer == NULL || *file_len == -1) { | 269 if (text_buffer == NULL || *file_len == -1) { |
297 *error_msg = "Unable to read file contents"; | 270 *error_msg = "Unable to read file contents"; |
298 text_buffer = NULL; | 271 text_buffer = NULL; |
299 } | 272 } |
300 DartUtils::CloseFile(stream); | 273 DartUtils::CloseFile(stream); |
301 return text_buffer; | 274 return text_buffer; |
302 } | 275 } |
303 | 276 |
304 | |
305 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { | 277 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { |
306 const char* error_msg = NULL; | 278 const char* error_msg = NULL; |
307 intptr_t len; | 279 intptr_t len; |
308 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); | 280 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); |
309 if (text_buffer == NULL) { | 281 if (text_buffer == NULL) { |
310 return Dart_NewApiError(error_msg); | 282 return Dart_NewApiError(error_msg); |
311 } | 283 } |
312 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); | 284 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); |
313 free(const_cast<uint8_t*>(text_buffer)); | 285 free(const_cast<uint8_t*>(text_buffer)); |
314 return str; | 286 return str; |
315 } | 287 } |
316 | 288 |
317 | |
318 Dart_Handle DartUtils::MakeUint8Array(const uint8_t* buffer, intptr_t len) { | 289 Dart_Handle DartUtils::MakeUint8Array(const uint8_t* buffer, intptr_t len) { |
319 Dart_Handle array = Dart_NewTypedData(Dart_TypedData_kUint8, len); | 290 Dart_Handle array = Dart_NewTypedData(Dart_TypedData_kUint8, len); |
320 RETURN_IF_ERROR(array); | 291 RETURN_IF_ERROR(array); |
321 { | 292 { |
322 Dart_TypedData_Type td_type; | 293 Dart_TypedData_Type td_type; |
323 void* td_data; | 294 void* td_data; |
324 intptr_t td_len; | 295 intptr_t td_len; |
325 Dart_Handle result = | 296 Dart_Handle result = |
326 Dart_TypedDataAcquireData(array, &td_type, &td_data, &td_len); | 297 Dart_TypedDataAcquireData(array, &td_type, &td_data, &td_len); |
327 RETURN_IF_ERROR(result); | 298 RETURN_IF_ERROR(result); |
328 ASSERT(td_type == Dart_TypedData_kUint8); | 299 ASSERT(td_type == Dart_TypedData_kUint8); |
329 ASSERT(td_len == len); | 300 ASSERT(td_len == len); |
330 ASSERT(td_data != NULL); | 301 ASSERT(td_data != NULL); |
331 memmove(td_data, buffer, td_len); | 302 memmove(td_data, buffer, td_len); |
332 result = Dart_TypedDataReleaseData(array); | 303 result = Dart_TypedDataReleaseData(array); |
333 RETURN_IF_ERROR(result); | 304 RETURN_IF_ERROR(result); |
334 } | 305 } |
335 return array; | 306 return array; |
336 } | 307 } |
337 | 308 |
338 | |
339 Dart_Handle DartUtils::SetWorkingDirectory() { | 309 Dart_Handle DartUtils::SetWorkingDirectory() { |
340 IsolateData* isolate_data = | 310 IsolateData* isolate_data = |
341 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 311 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
342 Dart_Handle builtin_lib = isolate_data->builtin_lib(); | 312 Dart_Handle builtin_lib = isolate_data->builtin_lib(); |
343 Dart_Handle directory = NewString(original_working_directory); | 313 Dart_Handle directory = NewString(original_working_directory); |
344 return SingleArgDart_Invoke(builtin_lib, "_setWorkingDirectory", directory); | 314 return SingleArgDart_Invoke(builtin_lib, "_setWorkingDirectory", directory); |
345 } | 315 } |
346 | 316 |
347 | |
348 Dart_Handle DartUtils::ResolveUriInWorkingDirectory(Dart_Handle script_uri) { | 317 Dart_Handle DartUtils::ResolveUriInWorkingDirectory(Dart_Handle script_uri) { |
349 const int kNumArgs = 1; | 318 const int kNumArgs = 1; |
350 Dart_Handle dart_args[kNumArgs]; | 319 Dart_Handle dart_args[kNumArgs]; |
351 dart_args[0] = script_uri; | 320 dart_args[0] = script_uri; |
352 return Dart_Invoke(DartUtils::BuiltinLib(), | 321 return Dart_Invoke(DartUtils::BuiltinLib(), |
353 NewString("_resolveInWorkingDirectory"), kNumArgs, | 322 NewString("_resolveInWorkingDirectory"), kNumArgs, |
354 dart_args); | 323 dart_args); |
355 } | 324 } |
356 | 325 |
357 | |
358 Dart_Handle DartUtils::LibraryFilePath(Dart_Handle library_uri) { | 326 Dart_Handle DartUtils::LibraryFilePath(Dart_Handle library_uri) { |
359 const int kNumArgs = 1; | 327 const int kNumArgs = 1; |
360 Dart_Handle dart_args[kNumArgs]; | 328 Dart_Handle dart_args[kNumArgs]; |
361 dart_args[0] = library_uri; | 329 dart_args[0] = library_uri; |
362 return Dart_Invoke(DartUtils::BuiltinLib(), NewString("_libraryFilePath"), | 330 return Dart_Invoke(DartUtils::BuiltinLib(), NewString("_libraryFilePath"), |
363 kNumArgs, dart_args); | 331 kNumArgs, dart_args); |
364 } | 332 } |
365 | 333 |
366 | |
367 Dart_Handle DartUtils::ResolveScript(Dart_Handle url) { | 334 Dart_Handle DartUtils::ResolveScript(Dart_Handle url) { |
368 const int kNumArgs = 1; | 335 const int kNumArgs = 1; |
369 Dart_Handle dart_args[kNumArgs]; | 336 Dart_Handle dart_args[kNumArgs]; |
370 dart_args[0] = url; | 337 dart_args[0] = url; |
371 return Dart_Invoke(DartUtils::BuiltinLib(), NewString("_resolveScriptUri"), | 338 return Dart_Invoke(DartUtils::BuiltinLib(), NewString("_resolveScriptUri"), |
372 kNumArgs, dart_args); | 339 kNumArgs, dart_args); |
373 } | 340 } |
374 | 341 |
375 | |
376 static Dart_Handle LoadDataAsync_Invoke(Dart_Handle tag, | 342 static Dart_Handle LoadDataAsync_Invoke(Dart_Handle tag, |
377 Dart_Handle url, | 343 Dart_Handle url, |
378 Dart_Handle library_url) { | 344 Dart_Handle library_url) { |
379 const int kNumArgs = 3; | 345 const int kNumArgs = 3; |
380 Dart_Handle dart_args[kNumArgs]; | 346 Dart_Handle dart_args[kNumArgs]; |
381 dart_args[0] = tag; | 347 dart_args[0] = tag; |
382 dart_args[1] = url; | 348 dart_args[1] = url; |
383 dart_args[2] = library_url; | 349 dart_args[2] = library_url; |
384 return Dart_Invoke(DartUtils::BuiltinLib(), | 350 return Dart_Invoke(DartUtils::BuiltinLib(), |
385 DartUtils::NewString("_loadDataAsync"), kNumArgs, | 351 DartUtils::NewString("_loadDataAsync"), kNumArgs, |
386 dart_args); | 352 dart_args); |
387 } | 353 } |
388 | 354 |
389 | |
390 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag, | 355 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag, |
391 Dart_Handle library, | 356 Dart_Handle library, |
392 Dart_Handle url) { | 357 Dart_Handle url) { |
393 Dart_Handle library_url = Dart_LibraryUrl(library); | 358 Dart_Handle library_url = Dart_LibraryUrl(library); |
394 if (Dart_IsError(library_url)) { | 359 if (Dart_IsError(library_url)) { |
395 return library_url; | 360 return library_url; |
396 } | 361 } |
397 if (tag == Dart_kCanonicalizeUrl) { | 362 if (tag == Dart_kCanonicalizeUrl) { |
398 return Dart_DefaultCanonicalizeUrl(library_url, url); | 363 return Dart_DefaultCanonicalizeUrl(library_url, url); |
399 } | 364 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 extension_path); | 429 extension_path); |
465 } | 430 } |
466 return Extensions::LoadExtension(lib_path_str, extension_path, library); | 431 return Extensions::LoadExtension(lib_path_str, extension_path, library); |
467 } | 432 } |
468 | 433 |
469 // Handle 'import' or 'part' requests for all other URIs. Call dart code to | 434 // Handle 'import' or 'part' requests for all other URIs. Call dart code to |
470 // read the source code asynchronously. | 435 // read the source code asynchronously. |
471 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); | 436 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); |
472 } | 437 } |
473 | 438 |
474 | |
475 static bool CheckMagicNumber(const uint8_t** buf, | 439 static bool CheckMagicNumber(const uint8_t** buf, |
476 intptr_t* len, | 440 intptr_t* len, |
477 const MagicNumberData& magic_number) { | 441 const MagicNumberData& magic_number) { |
478 if ((*len >= MagicNumberData::kLength) && | 442 if ((*len >= MagicNumberData::kLength) && |
479 (memcmp(*buf, magic_number.bytes, MagicNumberData::kLength) == 0)) { | 443 (memcmp(*buf, magic_number.bytes, MagicNumberData::kLength) == 0)) { |
480 if (magic_number.should_skip) { | 444 if (magic_number.should_skip) { |
481 *buf += MagicNumberData::kLength; | 445 *buf += MagicNumberData::kLength; |
482 *len -= MagicNumberData::kLength; | 446 *len -= MagicNumberData::kLength; |
483 } | 447 } |
484 return true; | 448 return true; |
485 } | 449 } |
486 return false; | 450 return false; |
487 } | 451 } |
488 | 452 |
489 | |
490 DartUtils::MagicNumber DartUtils::SniffForMagicNumber(const uint8_t** buf, | 453 DartUtils::MagicNumber DartUtils::SniffForMagicNumber(const uint8_t** buf, |
491 intptr_t* len) { | 454 intptr_t* len) { |
492 if (CheckMagicNumber(buf, len, snapshot_magic_number)) { | 455 if (CheckMagicNumber(buf, len, snapshot_magic_number)) { |
493 return kSnapshotMagicNumber; | 456 return kSnapshotMagicNumber; |
494 } | 457 } |
495 | 458 |
496 if (CheckMagicNumber(buf, len, kernel_magic_number)) { | 459 if (CheckMagicNumber(buf, len, kernel_magic_number)) { |
497 return kKernelMagicNumber; | 460 return kKernelMagicNumber; |
498 } | 461 } |
499 | 462 |
500 return kUnknownMagicNumber; | 463 return kUnknownMagicNumber; |
501 } | 464 } |
502 | 465 |
503 | |
504 void DartUtils::WriteMagicNumber(File* file) { | 466 void DartUtils::WriteMagicNumber(File* file) { |
505 // Write a magic number and version information into the snapshot file. | 467 // Write a magic number and version information into the snapshot file. |
506 bool bytes_written = | 468 bool bytes_written = |
507 file->WriteFully(snapshot_magic_number.bytes, MagicNumberData::kLength); | 469 file->WriteFully(snapshot_magic_number.bytes, MagicNumberData::kLength); |
508 ASSERT(bytes_written); | 470 ASSERT(bytes_written); |
509 } | 471 } |
510 | 472 |
511 | |
512 Dart_Handle DartUtils::LoadScript(const char* script_uri) { | 473 Dart_Handle DartUtils::LoadScript(const char* script_uri) { |
513 Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), | 474 Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), |
514 Dart_GetMainPortId(), Dart_Timeline_Event_Async_Begin, 0, | 475 Dart_GetMainPortId(), Dart_Timeline_Event_Async_Begin, 0, |
515 NULL, NULL); | 476 NULL, NULL); |
516 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | 477 Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
517 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null()); | 478 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null()); |
518 } | 479 } |
519 | 480 |
520 | |
521 void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) { | 481 void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) { |
522 const char* current = Directory::Current(); | 482 const char* current = Directory::Current(); |
523 if (current != NULL) { | 483 if (current != NULL) { |
524 Dart_SetReturnValue(args, DartUtils::NewString(current)); | 484 Dart_SetReturnValue(args, DartUtils::NewString(current)); |
525 } else { | 485 } else { |
526 Dart_Handle err = DartUtils::NewError("Failed to get current directory."); | 486 Dart_Handle err = DartUtils::NewError("Failed to get current directory."); |
527 Dart_PropagateError(err); | 487 Dart_PropagateError(err); |
528 } | 488 } |
529 } | 489 } |
530 | 490 |
531 | |
532 Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib, | 491 Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib, |
533 Dart_Handle internal_lib, | 492 Dart_Handle internal_lib, |
534 bool is_service_isolate, | 493 bool is_service_isolate, |
535 bool trace_loading) { | 494 bool trace_loading) { |
536 // Setup the internal library's 'internalPrint' function. | 495 // Setup the internal library's 'internalPrint' function. |
537 Dart_Handle print = | 496 Dart_Handle print = |
538 Dart_Invoke(builtin_lib, NewString("_getPrintClosure"), 0, NULL); | 497 Dart_Invoke(builtin_lib, NewString("_getPrintClosure"), 0, NULL); |
539 RETURN_IF_ERROR(print); | 498 RETURN_IF_ERROR(print); |
540 Dart_Handle result = | 499 Dart_Handle result = |
541 Dart_SetField(internal_lib, NewString("_printClosure"), print); | 500 Dart_SetField(internal_lib, NewString("_printClosure"), print); |
542 RETURN_IF_ERROR(result); | 501 RETURN_IF_ERROR(result); |
543 | 502 |
544 if (!is_service_isolate) { | 503 if (!is_service_isolate) { |
545 if (IsWindowsHost()) { | 504 if (IsWindowsHost()) { |
546 result = Dart_SetField(builtin_lib, NewString("_isWindows"), Dart_True()); | 505 result = Dart_SetField(builtin_lib, NewString("_isWindows"), Dart_True()); |
547 RETURN_IF_ERROR(result); | 506 RETURN_IF_ERROR(result); |
548 } | 507 } |
549 if (trace_loading) { | 508 if (trace_loading) { |
550 result = | 509 result = |
551 Dart_SetField(builtin_lib, NewString("_traceLoading"), Dart_True()); | 510 Dart_SetField(builtin_lib, NewString("_traceLoading"), Dart_True()); |
552 RETURN_IF_ERROR(result); | 511 RETURN_IF_ERROR(result); |
553 } | 512 } |
554 // Set current working directory. | 513 // Set current working directory. |
555 result = SetWorkingDirectory(); | 514 result = SetWorkingDirectory(); |
556 RETURN_IF_ERROR(result); | 515 RETURN_IF_ERROR(result); |
557 } | 516 } |
558 return Dart_True(); | 517 return Dart_True(); |
559 } | 518 } |
560 | 519 |
561 | |
562 Dart_Handle DartUtils::PrepareCoreLibrary(Dart_Handle core_lib, | 520 Dart_Handle DartUtils::PrepareCoreLibrary(Dart_Handle core_lib, |
563 Dart_Handle builtin_lib, | 521 Dart_Handle builtin_lib, |
564 bool is_service_isolate) { | 522 bool is_service_isolate) { |
565 if (!is_service_isolate) { | 523 if (!is_service_isolate) { |
566 // Setup the 'Uri.base' getter in dart:core. | 524 // Setup the 'Uri.base' getter in dart:core. |
567 Dart_Handle uri_base = | 525 Dart_Handle uri_base = |
568 Dart_Invoke(builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); | 526 Dart_Invoke(builtin_lib, NewString("_getUriBaseClosure"), 0, NULL); |
569 RETURN_IF_ERROR(uri_base); | 527 RETURN_IF_ERROR(uri_base); |
570 Dart_Handle result = | 528 Dart_Handle result = |
571 Dart_SetField(core_lib, NewString("_uriBaseClosure"), uri_base); | 529 Dart_SetField(core_lib, NewString("_uriBaseClosure"), uri_base); |
572 RETURN_IF_ERROR(result); | 530 RETURN_IF_ERROR(result); |
573 } | 531 } |
574 return Dart_True(); | 532 return Dart_True(); |
575 } | 533 } |
576 | 534 |
577 | |
578 Dart_Handle DartUtils::PrepareAsyncLibrary(Dart_Handle async_lib, | 535 Dart_Handle DartUtils::PrepareAsyncLibrary(Dart_Handle async_lib, |
579 Dart_Handle isolate_lib) { | 536 Dart_Handle isolate_lib) { |
580 Dart_Handle schedule_immediate_closure = Dart_Invoke( | 537 Dart_Handle schedule_immediate_closure = Dart_Invoke( |
581 isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), 0, NULL); | 538 isolate_lib, NewString("_getIsolateScheduleImmediateClosure"), 0, NULL); |
582 RETURN_IF_ERROR(schedule_immediate_closure); | 539 RETURN_IF_ERROR(schedule_immediate_closure); |
583 Dart_Handle args[1]; | 540 Dart_Handle args[1]; |
584 args[0] = schedule_immediate_closure; | 541 args[0] = schedule_immediate_closure; |
585 return Dart_Invoke(async_lib, NewString("_setScheduleImmediateClosure"), 1, | 542 return Dart_Invoke(async_lib, NewString("_setScheduleImmediateClosure"), 1, |
586 args); | 543 args); |
587 } | 544 } |
588 | 545 |
589 | |
590 Dart_Handle DartUtils::PrepareIOLibrary(Dart_Handle io_lib) { | 546 Dart_Handle DartUtils::PrepareIOLibrary(Dart_Handle io_lib) { |
591 return Dart_Invoke(io_lib, NewString("_setupHooks"), 0, NULL); | 547 return Dart_Invoke(io_lib, NewString("_setupHooks"), 0, NULL); |
592 } | 548 } |
593 | 549 |
594 | |
595 Dart_Handle DartUtils::PrepareIsolateLibrary(Dart_Handle isolate_lib) { | 550 Dart_Handle DartUtils::PrepareIsolateLibrary(Dart_Handle isolate_lib) { |
596 return Dart_Invoke(isolate_lib, NewString("_setupHooks"), 0, NULL); | 551 return Dart_Invoke(isolate_lib, NewString("_setupHooks"), 0, NULL); |
597 } | 552 } |
598 | 553 |
599 | |
600 Dart_Handle DartUtils::SetupServiceLoadPort() { | 554 Dart_Handle DartUtils::SetupServiceLoadPort() { |
601 // Wait for the service isolate to initialize the load port. | 555 // Wait for the service isolate to initialize the load port. |
602 Dart_Port load_port = Dart_ServiceWaitForLoadPort(); | 556 Dart_Port load_port = Dart_ServiceWaitForLoadPort(); |
603 if (load_port == ILLEGAL_PORT) { | 557 if (load_port == ILLEGAL_PORT) { |
604 return Dart_NewUnhandledExceptionError( | 558 return Dart_NewUnhandledExceptionError( |
605 NewDartUnsupportedError("Service did not return load port.")); | 559 NewDartUnsupportedError("Service did not return load port.")); |
606 } | 560 } |
607 return Builtin::SetLoadPort(load_port); | 561 return Builtin::SetLoadPort(load_port); |
608 } | 562 } |
609 | 563 |
610 | |
611 Dart_Handle DartUtils::SetupPackageRoot(const char* package_root, | 564 Dart_Handle DartUtils::SetupPackageRoot(const char* package_root, |
612 const char* packages_config) { | 565 const char* packages_config) { |
613 // Set up package root if specified. | 566 // Set up package root if specified. |
614 if (package_root != NULL) { | 567 if (package_root != NULL) { |
615 ASSERT(packages_config == NULL); | 568 ASSERT(packages_config == NULL); |
616 Dart_Handle result = NewString(package_root); | 569 Dart_Handle result = NewString(package_root); |
617 RETURN_IF_ERROR(result); | 570 RETURN_IF_ERROR(result); |
618 const int kNumArgs = 1; | 571 const int kNumArgs = 1; |
619 Dart_Handle dart_args[kNumArgs]; | 572 Dart_Handle dart_args[kNumArgs]; |
620 dart_args[0] = result; | 573 dart_args[0] = result; |
621 result = Dart_Invoke(DartUtils::BuiltinLib(), NewString("_setPackageRoot"), | 574 result = Dart_Invoke(DartUtils::BuiltinLib(), NewString("_setPackageRoot"), |
622 kNumArgs, dart_args); | 575 kNumArgs, dart_args); |
623 RETURN_IF_ERROR(result); | 576 RETURN_IF_ERROR(result); |
624 } else if (packages_config != NULL) { | 577 } else if (packages_config != NULL) { |
625 Dart_Handle result = NewString(packages_config); | 578 Dart_Handle result = NewString(packages_config); |
626 RETURN_IF_ERROR(result); | 579 RETURN_IF_ERROR(result); |
627 const int kNumArgs = 1; | 580 const int kNumArgs = 1; |
628 Dart_Handle dart_args[kNumArgs]; | 581 Dart_Handle dart_args[kNumArgs]; |
629 dart_args[0] = result; | 582 dart_args[0] = result; |
630 result = Dart_Invoke(DartUtils::BuiltinLib(), NewString("_setPackagesMap"), | 583 result = Dart_Invoke(DartUtils::BuiltinLib(), NewString("_setPackagesMap"), |
631 kNumArgs, dart_args); | 584 kNumArgs, dart_args); |
632 RETURN_IF_ERROR(result); | 585 RETURN_IF_ERROR(result); |
633 } | 586 } |
634 return Dart_True(); | 587 return Dart_True(); |
635 } | 588 } |
636 | 589 |
637 | |
638 Dart_Handle DartUtils::PrepareForScriptLoading(bool is_service_isolate, | 590 Dart_Handle DartUtils::PrepareForScriptLoading(bool is_service_isolate, |
639 bool trace_loading) { | 591 bool trace_loading) { |
640 // First ensure all required libraries are available. | 592 // First ensure all required libraries are available. |
641 Dart_Handle url = NewString(kCoreLibURL); | 593 Dart_Handle url = NewString(kCoreLibURL); |
642 RETURN_IF_ERROR(url); | 594 RETURN_IF_ERROR(url); |
643 Dart_Handle core_lib = Dart_LookupLibrary(url); | 595 Dart_Handle core_lib = Dart_LookupLibrary(url); |
644 RETURN_IF_ERROR(core_lib); | 596 RETURN_IF_ERROR(core_lib); |
645 url = NewString(kAsyncLibURL); | 597 url = NewString(kAsyncLibURL); |
646 RETURN_IF_ERROR(url); | 598 RETURN_IF_ERROR(url); |
647 Dart_Handle async_lib = Dart_LookupLibrary(url); | 599 Dart_Handle async_lib = Dart_LookupLibrary(url); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 RETURN_IF_ERROR(result); | 631 RETURN_IF_ERROR(result); |
680 | 632 |
681 RETURN_IF_ERROR(PrepareAsyncLibrary(async_lib, isolate_lib)); | 633 RETURN_IF_ERROR(PrepareAsyncLibrary(async_lib, isolate_lib)); |
682 RETURN_IF_ERROR( | 634 RETURN_IF_ERROR( |
683 PrepareCoreLibrary(core_lib, builtin_lib, is_service_isolate)); | 635 PrepareCoreLibrary(core_lib, builtin_lib, is_service_isolate)); |
684 RETURN_IF_ERROR(PrepareIsolateLibrary(isolate_lib)); | 636 RETURN_IF_ERROR(PrepareIsolateLibrary(isolate_lib)); |
685 RETURN_IF_ERROR(PrepareIOLibrary(io_lib)); | 637 RETURN_IF_ERROR(PrepareIOLibrary(io_lib)); |
686 return result; | 638 return result; |
687 } | 639 } |
688 | 640 |
689 | |
690 Dart_Handle DartUtils::SetupIOLibrary(const char* script_uri) { | 641 Dart_Handle DartUtils::SetupIOLibrary(const char* script_uri) { |
691 Dart_Handle io_lib_url = NewString(kIOLibURL); | 642 Dart_Handle io_lib_url = NewString(kIOLibURL); |
692 RETURN_IF_ERROR(io_lib_url); | 643 RETURN_IF_ERROR(io_lib_url); |
693 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); | 644 Dart_Handle io_lib = Dart_LookupLibrary(io_lib_url); |
694 RETURN_IF_ERROR(io_lib); | 645 RETURN_IF_ERROR(io_lib); |
695 Dart_Handle platform_type = GetDartType(DartUtils::kIOLibURL, "_Platform"); | 646 Dart_Handle platform_type = GetDartType(DartUtils::kIOLibURL, "_Platform"); |
696 RETURN_IF_ERROR(platform_type); | 647 RETURN_IF_ERROR(platform_type); |
697 Dart_Handle script_name = NewString("_nativeScript"); | 648 Dart_Handle script_name = NewString("_nativeScript"); |
698 RETURN_IF_ERROR(script_name); | 649 RETURN_IF_ERROR(script_name); |
699 Dart_Handle dart_script = NewString(script_uri); | 650 Dart_Handle dart_script = NewString(script_uri); |
700 RETURN_IF_ERROR(dart_script); | 651 RETURN_IF_ERROR(dart_script); |
701 Dart_Handle set_script_name = | 652 Dart_Handle set_script_name = |
702 Dart_SetField(platform_type, script_name, dart_script); | 653 Dart_SetField(platform_type, script_name, dart_script); |
703 RETURN_IF_ERROR(set_script_name); | 654 RETURN_IF_ERROR(set_script_name); |
704 return Dart_Null(); | 655 return Dart_Null(); |
705 } | 656 } |
706 | 657 |
707 | |
708 bool DartUtils::PostNull(Dart_Port port_id) { | 658 bool DartUtils::PostNull(Dart_Port port_id) { |
709 // Post a message with just the null object. | 659 // Post a message with just the null object. |
710 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 660 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
711 } | 661 } |
712 | 662 |
713 | |
714 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 663 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
715 // Post a message with the integer value. | 664 // Post a message with the integer value. |
716 int32_t min = 0xc0000000; // -1073741824 | 665 int32_t min = 0xc0000000; // -1073741824 |
717 int32_t max = 0x3fffffff; // 1073741823 | 666 int32_t max = 0x3fffffff; // 1073741823 |
718 ASSERT(min <= value && value < max); | 667 ASSERT(min <= value && value < max); |
719 Dart_CObject object; | 668 Dart_CObject object; |
720 object.type = Dart_CObject_kInt32; | 669 object.type = Dart_CObject_kInt32; |
721 object.value.as_int32 = value; | 670 object.value.as_int32 = value; |
722 return Dart_PostCObject(port_id, &object); | 671 return Dart_PostCObject(port_id, &object); |
723 } | 672 } |
724 | 673 |
725 | |
726 bool DartUtils::PostInt64(Dart_Port port_id, int64_t value) { | 674 bool DartUtils::PostInt64(Dart_Port port_id, int64_t value) { |
727 // Post a message with the integer value. | 675 // Post a message with the integer value. |
728 Dart_CObject object; | 676 Dart_CObject object; |
729 object.type = Dart_CObject_kInt64; | 677 object.type = Dart_CObject_kInt64; |
730 object.value.as_int64 = value; | 678 object.value.as_int64 = value; |
731 return Dart_PostCObject(port_id, &object); | 679 return Dart_PostCObject(port_id, &object); |
732 } | 680 } |
733 | 681 |
734 | |
735 Dart_Handle DartUtils::GetDartType(const char* library_url, | 682 Dart_Handle DartUtils::GetDartType(const char* library_url, |
736 const char* class_name) { | 683 const char* class_name) { |
737 return Dart_GetType(Dart_LookupLibrary(NewString(library_url)), | 684 return Dart_GetType(Dart_LookupLibrary(NewString(library_url)), |
738 NewString(class_name), 0, NULL); | 685 NewString(class_name), 0, NULL); |
739 } | 686 } |
740 | 687 |
741 | |
742 Dart_Handle DartUtils::NewDartOSError() { | 688 Dart_Handle DartUtils::NewDartOSError() { |
743 // Extract the current OS error. | 689 // Extract the current OS error. |
744 OSError os_error; | 690 OSError os_error; |
745 return NewDartOSError(&os_error); | 691 return NewDartOSError(&os_error); |
746 } | 692 } |
747 | 693 |
748 | |
749 Dart_Handle DartUtils::NewDartOSError(OSError* os_error) { | 694 Dart_Handle DartUtils::NewDartOSError(OSError* os_error) { |
750 // Create a dart:io OSError object with the information retrieved from the OS. | 695 // Create a dart:io OSError object with the information retrieved from the OS. |
751 Dart_Handle type = GetDartType(kIOLibURL, "OSError"); | 696 Dart_Handle type = GetDartType(kIOLibURL, "OSError"); |
752 ASSERT(!Dart_IsError(type)); | 697 ASSERT(!Dart_IsError(type)); |
753 Dart_Handle args[2]; | 698 Dart_Handle args[2]; |
754 args[0] = NewString(os_error->message()); | 699 args[0] = NewString(os_error->message()); |
755 args[1] = Dart_NewInteger(os_error->code()); | 700 args[1] = Dart_NewInteger(os_error->code()); |
756 return Dart_New(type, Dart_Null(), 2, args); | 701 return Dart_New(type, Dart_Null(), 2, args); |
757 } | 702 } |
758 | 703 |
759 | |
760 Dart_Handle DartUtils::NewDartExceptionWithOSError(const char* library_url, | 704 Dart_Handle DartUtils::NewDartExceptionWithOSError(const char* library_url, |
761 const char* exception_name, | 705 const char* exception_name, |
762 const char* message, | 706 const char* message, |
763 Dart_Handle os_error) { | 707 Dart_Handle os_error) { |
764 // Create a Dart Exception object with a message and an OSError. | 708 // Create a Dart Exception object with a message and an OSError. |
765 Dart_Handle type = GetDartType(library_url, exception_name); | 709 Dart_Handle type = GetDartType(library_url, exception_name); |
766 ASSERT(!Dart_IsError(type)); | 710 ASSERT(!Dart_IsError(type)); |
767 Dart_Handle args[2]; | 711 Dart_Handle args[2]; |
768 args[0] = NewString(message); | 712 args[0] = NewString(message); |
769 args[1] = os_error; | 713 args[1] = os_error; |
770 return Dart_New(type, Dart_Null(), 2, args); | 714 return Dart_New(type, Dart_Null(), 2, args); |
771 } | 715 } |
772 | 716 |
773 | |
774 Dart_Handle DartUtils::NewDartExceptionWithMessage(const char* library_url, | 717 Dart_Handle DartUtils::NewDartExceptionWithMessage(const char* library_url, |
775 const char* exception_name, | 718 const char* exception_name, |
776 const char* message) { | 719 const char* message) { |
777 // Create a Dart Exception object with a message. | 720 // Create a Dart Exception object with a message. |
778 Dart_Handle type = GetDartType(library_url, exception_name); | 721 Dart_Handle type = GetDartType(library_url, exception_name); |
779 ASSERT(!Dart_IsError(type)); | 722 ASSERT(!Dart_IsError(type)); |
780 if (message != NULL) { | 723 if (message != NULL) { |
781 Dart_Handle args[1]; | 724 Dart_Handle args[1]; |
782 args[0] = NewString(message); | 725 args[0] = NewString(message); |
783 return Dart_New(type, Dart_Null(), 1, args); | 726 return Dart_New(type, Dart_Null(), 1, args); |
784 } else { | 727 } else { |
785 return Dart_New(type, Dart_Null(), 0, NULL); | 728 return Dart_New(type, Dart_Null(), 0, NULL); |
786 } | 729 } |
787 } | 730 } |
788 | 731 |
789 | |
790 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { | 732 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { |
791 return NewDartExceptionWithMessage(kCoreLibURL, "ArgumentError", message); | 733 return NewDartExceptionWithMessage(kCoreLibURL, "ArgumentError", message); |
792 } | 734 } |
793 | 735 |
794 | |
795 Dart_Handle DartUtils::NewDartUnsupportedError(const char* message) { | 736 Dart_Handle DartUtils::NewDartUnsupportedError(const char* message) { |
796 return NewDartExceptionWithMessage(kCoreLibURL, "UnsupportedError", message); | 737 return NewDartExceptionWithMessage(kCoreLibURL, "UnsupportedError", message); |
797 } | 738 } |
798 | 739 |
799 | |
800 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, | 740 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, |
801 const char* message, | 741 const char* message, |
802 Dart_Handle os_error) { | 742 Dart_Handle os_error) { |
803 // Create a dart:io exception object of the given type. | 743 // Create a dart:io exception object of the given type. |
804 return NewDartExceptionWithOSError(kIOLibURL, exception_name, message, | 744 return NewDartExceptionWithOSError(kIOLibURL, exception_name, message, |
805 os_error); | 745 os_error); |
806 } | 746 } |
807 | 747 |
808 | |
809 Dart_Handle DartUtils::NewError(const char* format, ...) { | 748 Dart_Handle DartUtils::NewError(const char* format, ...) { |
810 va_list args; | 749 va_list args; |
811 va_start(args, format); | 750 va_start(args, format); |
812 intptr_t len = vsnprintf(NULL, 0, format, args); | 751 intptr_t len = vsnprintf(NULL, 0, format, args); |
813 va_end(args); | 752 va_end(args); |
814 | 753 |
815 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); | 754 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); |
816 MSAN_UNPOISON(buffer, (len + 1)); | 755 MSAN_UNPOISON(buffer, (len + 1)); |
817 va_list args2; | 756 va_list args2; |
818 va_start(args2, format); | 757 va_start(args2, format); |
819 vsnprintf(buffer, (len + 1), format, args2); | 758 vsnprintf(buffer, (len + 1), format, args2); |
820 va_end(args2); | 759 va_end(args2); |
821 | 760 |
822 return Dart_NewApiError(buffer); | 761 return Dart_NewApiError(buffer); |
823 } | 762 } |
824 | 763 |
825 | |
826 Dart_Handle DartUtils::NewInternalError(const char* message) { | 764 Dart_Handle DartUtils::NewInternalError(const char* message) { |
827 return NewDartExceptionWithMessage(kCoreLibURL, "_InternalError", message); | 765 return NewDartExceptionWithMessage(kCoreLibURL, "_InternalError", message); |
828 } | 766 } |
829 | 767 |
830 | |
831 bool DartUtils::SetOriginalWorkingDirectory() { | 768 bool DartUtils::SetOriginalWorkingDirectory() { |
832 original_working_directory = Directory::CurrentNoScope(); | 769 original_working_directory = Directory::CurrentNoScope(); |
833 return original_working_directory != NULL; | 770 return original_working_directory != NULL; |
834 } | 771 } |
835 | 772 |
836 | |
837 Dart_Handle DartUtils::GetCanonicalizableWorkingDirectory() { | 773 Dart_Handle DartUtils::GetCanonicalizableWorkingDirectory() { |
838 const char* str = DartUtils::original_working_directory; | 774 const char* str = DartUtils::original_working_directory; |
839 intptr_t len = strlen(str); | 775 intptr_t len = strlen(str); |
840 if ((str[len] == '/') || (IsWindowsHost() && str[len] == '\\')) { | 776 if ((str[len] == '/') || (IsWindowsHost() && str[len] == '\\')) { |
841 return Dart_NewStringFromCString(str); | 777 return Dart_NewStringFromCString(str); |
842 } | 778 } |
843 char* new_str = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 2)); | 779 char* new_str = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 2)); |
844 snprintf(new_str, (len + 2), "%s%s", str, File::PathSeparator()); | 780 snprintf(new_str, (len + 2), "%s%s", str, File::PathSeparator()); |
845 return Dart_NewStringFromCString(new_str); | 781 return Dart_NewStringFromCString(new_str); |
846 } | 782 } |
847 | 783 |
848 | |
849 // Statically allocated Dart_CObject instances for immutable | 784 // Statically allocated Dart_CObject instances for immutable |
850 // objects. As these will be used by different threads the use of | 785 // objects. As these will be used by different threads the use of |
851 // these depends on the fact that the marking internally in the | 786 // these depends on the fact that the marking internally in the |
852 // Dart_CObject structure is not marking simple value objects. | 787 // Dart_CObject structure is not marking simple value objects. |
853 Dart_CObject CObject::api_null_ = {Dart_CObject_kNull, {0}}; | 788 Dart_CObject CObject::api_null_ = {Dart_CObject_kNull, {0}}; |
854 Dart_CObject CObject::api_true_ = {Dart_CObject_kBool, {true}}; | 789 Dart_CObject CObject::api_true_ = {Dart_CObject_kBool, {true}}; |
855 Dart_CObject CObject::api_false_ = {Dart_CObject_kBool, {false}}; | 790 Dart_CObject CObject::api_false_ = {Dart_CObject_kBool, {false}}; |
856 CObject CObject::null_ = CObject(&api_null_); | 791 CObject CObject::null_ = CObject(&api_null_); |
857 CObject CObject::true_ = CObject(&api_true_); | 792 CObject CObject::true_ = CObject(&api_true_); |
858 CObject CObject::false_ = CObject(&api_false_); | 793 CObject CObject::false_ = CObject(&api_false_); |
859 | 794 |
860 | |
861 CObject* CObject::Null() { | 795 CObject* CObject::Null() { |
862 return &null_; | 796 return &null_; |
863 } | 797 } |
864 | 798 |
865 | |
866 CObject* CObject::True() { | 799 CObject* CObject::True() { |
867 return &true_; | 800 return &true_; |
868 } | 801 } |
869 | 802 |
870 | |
871 CObject* CObject::False() { | 803 CObject* CObject::False() { |
872 return &false_; | 804 return &false_; |
873 } | 805 } |
874 | 806 |
875 | |
876 CObject* CObject::Bool(bool value) { | 807 CObject* CObject::Bool(bool value) { |
877 return value ? &true_ : &false_; | 808 return value ? &true_ : &false_; |
878 } | 809 } |
879 | 810 |
880 | |
881 Dart_CObject* CObject::New(Dart_CObject_Type type, int additional_bytes) { | 811 Dart_CObject* CObject::New(Dart_CObject_Type type, int additional_bytes) { |
882 Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>( | 812 Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>( |
883 Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes)); | 813 Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes)); |
884 cobject->type = type; | 814 cobject->type = type; |
885 return cobject; | 815 return cobject; |
886 } | 816 } |
887 | 817 |
888 | |
889 Dart_CObject* CObject::NewInt32(int32_t value) { | 818 Dart_CObject* CObject::NewInt32(int32_t value) { |
890 Dart_CObject* cobject = New(Dart_CObject_kInt32); | 819 Dart_CObject* cobject = New(Dart_CObject_kInt32); |
891 cobject->value.as_int32 = value; | 820 cobject->value.as_int32 = value; |
892 return cobject; | 821 return cobject; |
893 } | 822 } |
894 | 823 |
895 | |
896 Dart_CObject* CObject::NewInt64(int64_t value) { | 824 Dart_CObject* CObject::NewInt64(int64_t value) { |
897 Dart_CObject* cobject = New(Dart_CObject_kInt64); | 825 Dart_CObject* cobject = New(Dart_CObject_kInt64); |
898 cobject->value.as_int64 = value; | 826 cobject->value.as_int64 = value; |
899 return cobject; | 827 return cobject; |
900 } | 828 } |
901 | 829 |
902 | |
903 Dart_CObject* CObject::NewIntptr(intptr_t value) { | 830 Dart_CObject* CObject::NewIntptr(intptr_t value) { |
904 // Pointer values passed as intptr_t are always send as int64_t. | 831 // Pointer values passed as intptr_t are always send as int64_t. |
905 Dart_CObject* cobject = New(Dart_CObject_kInt64); | 832 Dart_CObject* cobject = New(Dart_CObject_kInt64); |
906 cobject->value.as_int64 = value; | 833 cobject->value.as_int64 = value; |
907 return cobject; | 834 return cobject; |
908 } | 835 } |
909 | 836 |
910 | |
911 static bool IsHexDigit(char c) { | 837 static bool IsHexDigit(char c) { |
912 return (('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) || | 838 return (('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) || |
913 (('a' <= c) && (c <= 'f')); | 839 (('a' <= c) && (c <= 'f')); |
914 } | 840 } |
915 | 841 |
916 | |
917 static int HexDigitToInt(char c) { | 842 static int HexDigitToInt(char c) { |
918 if (('0' <= c) && (c <= '9')) return c - '0'; | 843 if (('0' <= c) && (c <= '9')) return c - '0'; |
919 if (('A' <= c) && (c <= 'F')) return 10 + (c - 'A'); | 844 if (('A' <= c) && (c <= 'F')) return 10 + (c - 'A'); |
920 return 10 + (c - 'a'); | 845 return 10 + (c - 'a'); |
921 } | 846 } |
922 | 847 |
923 | |
924 Dart_CObject* CObject::NewBigint(const char* hex_value) { | 848 Dart_CObject* CObject::NewBigint(const char* hex_value) { |
925 if (hex_value == NULL) { | 849 if (hex_value == NULL) { |
926 return NULL; | 850 return NULL; |
927 } | 851 } |
928 bool neg = false; | 852 bool neg = false; |
929 if (hex_value[0] == '-') { | 853 if (hex_value[0] == '-') { |
930 neg = true; | 854 neg = true; |
931 hex_value++; | 855 hex_value++; |
932 } | 856 } |
933 if ((hex_value[0] != '0') || | 857 if ((hex_value[0] != '0') || |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 used--; | 893 used--; |
970 } | 894 } |
971 cobject->value.as_bigint.used = used; | 895 cobject->value.as_bigint.used = used; |
972 if (used == 0) { | 896 if (used == 0) { |
973 neg = false; | 897 neg = false; |
974 } | 898 } |
975 cobject->value.as_bigint.neg = neg; | 899 cobject->value.as_bigint.neg = neg; |
976 return cobject; | 900 return cobject; |
977 } | 901 } |
978 | 902 |
979 | |
980 static char IntToHexDigit(int i) { | 903 static char IntToHexDigit(int i) { |
981 ASSERT(0 <= i && i < 16); | 904 ASSERT(0 <= i && i < 16); |
982 if (i < 10) return static_cast<char>('0' + i); | 905 if (i < 10) return static_cast<char>('0' + i); |
983 return static_cast<char>('A' + (i - 10)); | 906 return static_cast<char>('A' + (i - 10)); |
984 } | 907 } |
985 | 908 |
986 | |
987 char* CObject::BigintToHexValue(Dart_CObject* bigint) { | 909 char* CObject::BigintToHexValue(Dart_CObject* bigint) { |
988 ASSERT(bigint->type == Dart_CObject_kBigint); | 910 ASSERT(bigint->type == Dart_CObject_kBigint); |
989 const intptr_t used = bigint->value.as_bigint.used; | 911 const intptr_t used = bigint->value.as_bigint.used; |
990 if (used == 0) { | 912 if (used == 0) { |
991 const char* zero = "0x0"; | 913 const char* zero = "0x0"; |
992 const size_t len = strlen(zero) + 1; | 914 const size_t len = strlen(zero) + 1; |
993 char* hex_value = reinterpret_cast<char*>(malloc(len)); | 915 char* hex_value = reinterpret_cast<char*>(malloc(len)); |
994 strncpy(hex_value, zero, len); | 916 strncpy(hex_value, zero, len); |
995 return hex_value; | 917 return hex_value; |
996 } | 918 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1030 } | 952 } |
1031 hex_value[--pos] = 'x'; | 953 hex_value[--pos] = 'x'; |
1032 hex_value[--pos] = '0'; | 954 hex_value[--pos] = '0'; |
1033 if (neg) { | 955 if (neg) { |
1034 hex_value[--pos] = '-'; | 956 hex_value[--pos] = '-'; |
1035 } | 957 } |
1036 ASSERT(pos == 0); | 958 ASSERT(pos == 0); |
1037 return hex_value; | 959 return hex_value; |
1038 } | 960 } |
1039 | 961 |
1040 | |
1041 Dart_CObject* CObject::NewDouble(double value) { | 962 Dart_CObject* CObject::NewDouble(double value) { |
1042 Dart_CObject* cobject = New(Dart_CObject_kDouble); | 963 Dart_CObject* cobject = New(Dart_CObject_kDouble); |
1043 cobject->value.as_double = value; | 964 cobject->value.as_double = value; |
1044 return cobject; | 965 return cobject; |
1045 } | 966 } |
1046 | 967 |
1047 | |
1048 Dart_CObject* CObject::NewString(intptr_t length) { | 968 Dart_CObject* CObject::NewString(intptr_t length) { |
1049 Dart_CObject* cobject = New(Dart_CObject_kString, length + 1); | 969 Dart_CObject* cobject = New(Dart_CObject_kString, length + 1); |
1050 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); | 970 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); |
1051 return cobject; | 971 return cobject; |
1052 } | 972 } |
1053 | 973 |
1054 | |
1055 Dart_CObject* CObject::NewString(const char* str) { | 974 Dart_CObject* CObject::NewString(const char* str) { |
1056 intptr_t length = strlen(str); | 975 intptr_t length = strlen(str); |
1057 Dart_CObject* cobject = NewString(length); | 976 Dart_CObject* cobject = NewString(length); |
1058 memmove(cobject->value.as_string, str, length + 1); | 977 memmove(cobject->value.as_string, str, length + 1); |
1059 return cobject; | 978 return cobject; |
1060 } | 979 } |
1061 | 980 |
1062 | |
1063 Dart_CObject* CObject::NewArray(intptr_t length) { | 981 Dart_CObject* CObject::NewArray(intptr_t length) { |
1064 Dart_CObject* cobject = | 982 Dart_CObject* cobject = |
1065 New(Dart_CObject_kArray, length * sizeof(Dart_CObject*)); // NOLINT | 983 New(Dart_CObject_kArray, length * sizeof(Dart_CObject*)); // NOLINT |
1066 cobject->value.as_array.length = length; | 984 cobject->value.as_array.length = length; |
1067 cobject->value.as_array.values = | 985 cobject->value.as_array.values = |
1068 reinterpret_cast<Dart_CObject**>(cobject + 1); | 986 reinterpret_cast<Dart_CObject**>(cobject + 1); |
1069 return cobject; | 987 return cobject; |
1070 } | 988 } |
1071 | 989 |
1072 | |
1073 Dart_CObject* CObject::NewUint8Array(intptr_t length) { | 990 Dart_CObject* CObject::NewUint8Array(intptr_t length) { |
1074 Dart_CObject* cobject = New(Dart_CObject_kTypedData, length); | 991 Dart_CObject* cobject = New(Dart_CObject_kTypedData, length); |
1075 cobject->value.as_typed_data.type = Dart_TypedData_kUint8; | 992 cobject->value.as_typed_data.type = Dart_TypedData_kUint8; |
1076 cobject->value.as_typed_data.length = length; | 993 cobject->value.as_typed_data.length = length; |
1077 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); | 994 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); |
1078 return cobject; | 995 return cobject; |
1079 } | 996 } |
1080 | 997 |
1081 | |
1082 Dart_CObject* CObject::NewUint32Array(intptr_t length) { | 998 Dart_CObject* CObject::NewUint32Array(intptr_t length) { |
1083 Dart_CObject* cobject = New(Dart_CObject_kTypedData, 4 * length); | 999 Dart_CObject* cobject = New(Dart_CObject_kTypedData, 4 * length); |
1084 cobject->value.as_typed_data.type = Dart_TypedData_kUint32; | 1000 cobject->value.as_typed_data.type = Dart_TypedData_kUint32; |
1085 cobject->value.as_typed_data.length = length; | 1001 cobject->value.as_typed_data.length = length; |
1086 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); | 1002 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); |
1087 return cobject; | 1003 return cobject; |
1088 } | 1004 } |
1089 | 1005 |
1090 | |
1091 Dart_CObject* CObject::NewExternalUint8Array( | 1006 Dart_CObject* CObject::NewExternalUint8Array( |
1092 intptr_t length, | 1007 intptr_t length, |
1093 uint8_t* data, | 1008 uint8_t* data, |
1094 void* peer, | 1009 void* peer, |
1095 Dart_WeakPersistentHandleFinalizer callback) { | 1010 Dart_WeakPersistentHandleFinalizer callback) { |
1096 Dart_CObject* cobject = New(Dart_CObject_kExternalTypedData); | 1011 Dart_CObject* cobject = New(Dart_CObject_kExternalTypedData); |
1097 cobject->value.as_external_typed_data.type = Dart_TypedData_kUint8; | 1012 cobject->value.as_external_typed_data.type = Dart_TypedData_kUint8; |
1098 cobject->value.as_external_typed_data.length = length; | 1013 cobject->value.as_external_typed_data.length = length; |
1099 cobject->value.as_external_typed_data.data = data; | 1014 cobject->value.as_external_typed_data.data = data; |
1100 cobject->value.as_external_typed_data.peer = peer; | 1015 cobject->value.as_external_typed_data.peer = peer; |
1101 cobject->value.as_external_typed_data.callback = callback; | 1016 cobject->value.as_external_typed_data.callback = callback; |
1102 return cobject; | 1017 return cobject; |
1103 } | 1018 } |
1104 | 1019 |
1105 | |
1106 Dart_CObject* CObject::NewIOBuffer(int64_t length) { | 1020 Dart_CObject* CObject::NewIOBuffer(int64_t length) { |
1107 // Make sure that we do not have an integer overflow here. Actual check | 1021 // Make sure that we do not have an integer overflow here. Actual check |
1108 // against max elements will be done at the time of writing, as the constant | 1022 // against max elements will be done at the time of writing, as the constant |
1109 // is not part of the public API. | 1023 // is not part of the public API. |
1110 if ((length < 0) || (length > kIntptrMax)) { | 1024 if ((length < 0) || (length > kIntptrMax)) { |
1111 return NULL; | 1025 return NULL; |
1112 } | 1026 } |
1113 uint8_t* data = IOBuffer::Allocate(static_cast<intptr_t>(length)); | 1027 uint8_t* data = IOBuffer::Allocate(static_cast<intptr_t>(length)); |
1114 ASSERT(data != NULL); | 1028 ASSERT(data != NULL); |
1115 return NewExternalUint8Array(static_cast<intptr_t>(length), data, data, | 1029 return NewExternalUint8Array(static_cast<intptr_t>(length), data, data, |
1116 IOBuffer::Finalizer); | 1030 IOBuffer::Finalizer); |
1117 } | 1031 } |
1118 | 1032 |
1119 | |
1120 void CObject::FreeIOBufferData(Dart_CObject* cobject) { | 1033 void CObject::FreeIOBufferData(Dart_CObject* cobject) { |
1121 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); | 1034 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); |
1122 cobject->value.as_external_typed_data.callback( | 1035 cobject->value.as_external_typed_data.callback( |
1123 NULL, NULL, cobject->value.as_external_typed_data.peer); | 1036 NULL, NULL, cobject->value.as_external_typed_data.peer); |
1124 cobject->value.as_external_typed_data.data = NULL; | 1037 cobject->value.as_external_typed_data.data = NULL; |
1125 } | 1038 } |
1126 | 1039 |
1127 | |
1128 CObject* CObject::IllegalArgumentError() { | 1040 CObject* CObject::IllegalArgumentError() { |
1129 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); | 1041 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); |
1130 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kArgumentError))); | 1042 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kArgumentError))); |
1131 return result; | 1043 return result; |
1132 } | 1044 } |
1133 | 1045 |
1134 | |
1135 CObject* CObject::FileClosedError() { | 1046 CObject* CObject::FileClosedError() { |
1136 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); | 1047 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); |
1137 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kFileClosedError))); | 1048 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kFileClosedError))); |
1138 return result; | 1049 return result; |
1139 } | 1050 } |
1140 | 1051 |
1141 | |
1142 CObject* CObject::NewOSError() { | 1052 CObject* CObject::NewOSError() { |
1143 OSError os_error; | 1053 OSError os_error; |
1144 return NewOSError(&os_error); | 1054 return NewOSError(&os_error); |
1145 } | 1055 } |
1146 | 1056 |
1147 CObject* CObject::NewOSError(OSError* os_error) { | 1057 CObject* CObject::NewOSError(OSError* os_error) { |
1148 CObject* error_message = | 1058 CObject* error_message = |
1149 new CObjectString(CObject::NewString(os_error->message())); | 1059 new CObjectString(CObject::NewString(os_error->message())); |
1150 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1060 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1151 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1061 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1152 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1062 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1153 result->SetAt(2, error_message); | 1063 result->SetAt(2, error_message); |
1154 return result; | 1064 return result; |
1155 } | 1065 } |
1156 | 1066 |
1157 } // namespace bin | 1067 } // namespace bin |
1158 } // namespace dart | 1068 } // namespace dart |
OLD | NEW |