Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: runtime/bin/dartutils.cc

Issue 14786012: Second step towards loading the core library scripts directly from the sources (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/dartutils.h ('k') | runtime/bin/gen_snapshot.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/extensions.h" 7 #include "bin/extensions.h"
8 #include "bin/directory.h" 8 #include "bin/directory.h"
9 #include "bin/file.h" 9 #include "bin/file.h"
10 #include "bin/io_buffer.h" 10 #include "bin/io_buffer.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 } 184 }
185 // Calculate the canonical path. 185 // Calculate the canonical path.
186 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); 186 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str);
187 Dart_Handle canon_url = NewString(canon_url_str); 187 Dart_Handle canon_url = NewString(canon_url_str);
188 free(const_cast<char*>(canon_url_str)); 188 free(const_cast<char*>(canon_url_str));
189 189
190 return canon_url; 190 return canon_url;
191 } 191 }
192 192
193 193
194 static const uint8_t* ReadFile(const char* filename, 194 void* DartUtils::OpenFile(const char* name, bool write) {
195 intptr_t* file_len, 195 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead);
196 const char** error_msg) { 196 return reinterpret_cast<void*>(file);
197 File* file = File::Open(filename, File::kRead); 197 }
198 if (file == NULL) { 198
199
200 void DartUtils::ReadFile(const uint8_t** data,
201 intptr_t* file_len,
202 void* stream) {
203 ASSERT(data != NULL);
204 ASSERT(file_len != NULL);
205 ASSERT(stream != NULL);
206 File* file_stream = reinterpret_cast<File*>(stream);
207 *file_len = file_stream->Length();
208 ASSERT(*file_len > 0);
209 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*file_len));
210 ASSERT(text_buffer != NULL);
211 if (!file_stream->ReadFully(text_buffer, *file_len)) {
212 *data = NULL;
213 *file_len = -1; // Indicates read was not successful.
214 return;
215 }
216 *data = text_buffer;
217 }
218
219
220 void DartUtils::WriteFile(const void* buffer,
221 intptr_t num_bytes,
222 void* stream) {
223 ASSERT(stream != NULL);
224 File* file_stream = reinterpret_cast<File*>(stream);
225 bool bytes_written = file_stream->WriteFully(buffer, num_bytes);
226 ASSERT(bytes_written);
227 }
228
229
230 void DartUtils::CloseFile(void* stream) {
231 delete reinterpret_cast<File*>(stream);
232 }
233
234
235 static const uint8_t* ReadFileFully(const char* filename,
236 intptr_t* file_len,
237 const char** error_msg) {
238 void* stream = DartUtils::OpenFile(filename, false);
239 if (stream == NULL) {
199 const char* format = "Unable to open file: %s"; 240 const char* format = "Unable to open file: %s";
200 intptr_t len = snprintf(NULL, 0, format, filename); 241 intptr_t len = snprintf(NULL, 0, format, filename);
201 // TODO(iposva): Allocate from the zone instead of leaking error string 242 // TODO(iposva): Allocate from the zone instead of leaking error string
202 // here. On the other hand the binary is about the exit anyway. 243 // here. On the other hand the binary is about the exit anyway.
203 char* msg = reinterpret_cast<char*>(malloc(len + 1)); 244 char* msg = reinterpret_cast<char*>(malloc(len + 1));
204 snprintf(msg, len + 1, format, filename); 245 snprintf(msg, len + 1, format, filename);
205 *error_msg = msg; 246 *error_msg = msg;
206 return NULL; 247 return NULL;
207 } 248 }
208 *file_len = file->Length(); 249 *file_len = -1;
209 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*file_len)); 250 const uint8_t* text_buffer = NULL;
210 if (text_buffer == NULL) { 251 DartUtils::ReadFile(&text_buffer, file_len, stream);
211 delete file; 252 if (text_buffer == NULL || *file_len == -1) {
212 *error_msg = "Unable to allocate buffer"; 253 *error_msg = "Unable to read file contents";
213 return NULL; 254 text_buffer = NULL;
214 } 255 }
215 if (!file->ReadFully(text_buffer, *file_len)) { 256 DartUtils::CloseFile(stream);
216 delete file;
217 free(text_buffer);
218 *error_msg = "Unable to fully read contents";
219 return NULL;
220 }
221 delete file;
222 return text_buffer; 257 return text_buffer;
223 } 258 }
224 259
225 260
226 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { 261 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) {
227 const char* error_msg = NULL; 262 const char* error_msg = NULL;
228 intptr_t len; 263 intptr_t len;
229 const uint8_t* text_buffer = ReadFile(filename, &len, &error_msg); 264 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg);
230 if (text_buffer == NULL) { 265 if (text_buffer == NULL) {
231 return Dart_Error(error_msg); 266 return Dart_Error(error_msg);
232 } 267 }
233 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); 268 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len);
234 return str; 269 return str;
235 } 270 }
236 271
237 272
238 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, 273 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri,
239 Dart_Handle builtin_lib) { 274 Dart_Handle builtin_lib) {
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 } 412 }
378 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, 413 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri,
379 builtin_lib); 414 builtin_lib);
380 if (Dart_IsError(script_path)) { 415 if (Dart_IsError(script_path)) {
381 return script_path; 416 return script_path;
382 } 417 }
383 const char* script_path_cstr; 418 const char* script_path_cstr;
384 Dart_StringToCString(script_path, &script_path_cstr); 419 Dart_StringToCString(script_path, &script_path_cstr);
385 const char* error_msg = NULL; 420 const char* error_msg = NULL;
386 intptr_t len; 421 intptr_t len;
387 const uint8_t* text_buffer = ReadFile(script_path_cstr, &len, &error_msg); 422 const uint8_t* text_buffer = ReadFileFully(script_path_cstr,
423 &len,
424 &error_msg);
388 if (text_buffer == NULL) { 425 if (text_buffer == NULL) {
389 return Dart_Error(error_msg); 426 return Dart_Error(error_msg);
390 } 427 }
391 bool is_snapshot = false; 428 bool is_snapshot = false;
392 text_buffer = SniffForMagicNumber(text_buffer, &len, &is_snapshot); 429 text_buffer = SniffForMagicNumber(text_buffer, &len, &is_snapshot);
393 if (is_snapshot) { 430 if (is_snapshot) {
394 return Dart_LoadScriptFromSnapshot(text_buffer, len); 431 return Dart_LoadScriptFromSnapshot(text_buffer, len);
395 } else { 432 } else {
396 Dart_Handle source = Dart_NewStringFromUTF8(text_buffer, len); 433 Dart_Handle source = Dart_NewStringFromUTF8(text_buffer, len);
397 if (Dart_IsError(source)) { 434 if (Dart_IsError(source)) {
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 new CObjectString(CObject::NewString(os_error->message())); 789 new CObjectString(CObject::NewString(os_error->message()));
753 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 790 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
754 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); 791 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError)));
755 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); 792 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code())));
756 result->SetAt(2, error_message); 793 result->SetAt(2, error_message);
757 return result; 794 return result;
758 } 795 }
759 796
760 } // namespace bin 797 } // namespace bin
761 } // namespace dart 798 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.h ('k') | runtime/bin/gen_snapshot.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698