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

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

Issue 8380020: Refactor the dart api a bit: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 9 years, 1 month 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 // Handle dart scripts. 5 // Handle dart scripts.
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <string.h> 9 #include <string.h>
10 #include <stdio.h> 10 #include <stdio.h>
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 ASSERT(absolute_filename != NULL); 46 ASSERT(absolute_filename != NULL);
47 47
48 snprintf(absolute_filename, len + 1, "%s%s%s", 48 snprintf(absolute_filename, len + 1, "%s%s%s",
49 path, File::PathSeparator(), filename); 49 path, File::PathSeparator(), filename);
50 50
51 free(path); 51 free(path);
52 return absolute_filename; 52 return absolute_filename;
53 } 53 }
54 54
55 55
56 static Dart_Result ReadStringFromFile(const char* filename) { 56 static Dart_Handle ReadStringFromFile(const char* filename) {
57 File* file = File::OpenFile(filename, false); 57 File* file = File::OpenFile(filename, false);
58 if (file == NULL) { 58 if (file == NULL) {
59 const char* format = "Unable to open file: %s"; 59 const char* format = "Unable to open file: %s";
60 intptr_t len = snprintf(NULL, 0, format, filename); 60 intptr_t len = snprintf(NULL, 0, format, filename);
61 // TODO(iposva): Allocate from the zone instead of leaking error string 61 // TODO(iposva): Allocate from the zone instead of leaking error string
62 // here. On the other hand the binary is about the exit anyway. 62 // here. On the other hand the binary is about the exit anyway.
63 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); 63 char* error_msg = reinterpret_cast<char*>(malloc(len + 1));
64 snprintf(error_msg, len + 1, format, filename); 64 snprintf(error_msg, len + 1, format, filename);
65 return Dart_ErrorResult(error_msg); 65 return Dart_Error(error_msg);
66 } 66 }
67 intptr_t len = file->Length(); 67 intptr_t len = file->Length();
68 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1)); 68 char* text_buffer = reinterpret_cast<char*>(malloc(len + 1));
69 if (text_buffer == NULL) { 69 if (text_buffer == NULL) {
70 delete file; 70 delete file;
71 return Dart_ErrorResult("Unable to allocate buffer"); 71 return Dart_Error("Unable to allocate buffer");
72 } 72 }
73 if (!file->ReadFully(text_buffer, len)) { 73 if (!file->ReadFully(text_buffer, len)) {
74 delete file; 74 delete file;
75 return Dart_ErrorResult("Unable to fully read contents"); 75 return Dart_Error("Unable to fully read contents");
76 } 76 }
77 text_buffer[len] = '\0'; 77 text_buffer[len] = '\0';
78 delete file; 78 delete file;
79 Dart_Handle str = Dart_NewString(text_buffer); 79 Dart_Handle str = Dart_NewString(text_buffer);
80 free(text_buffer); 80 free(text_buffer);
81 return Dart_ResultAsObject(str); 81 return str;
82 } 82 }
83 83
84 84
85 static Dart_Result LibraryTagHandler(Dart_LibraryTag tag, 85 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
86 Dart_Handle library, 86 Dart_Handle library,
87 Dart_Handle url) { 87 Dart_Handle url) {
88 if (!Dart_IsLibrary(library)) { 88 if (!Dart_IsLibrary(library)) {
89 return Dart_ErrorResult("not a library"); 89 return Dart_Error("not a library");
90 } 90 }
91 if (!Dart_IsString8(url)) { 91 if (!Dart_IsString8(url)) {
92 return Dart_ErrorResult("url is not a string"); 92 return Dart_Error("url is not a string");
93 } 93 }
94 Dart_Result result = Dart_StringToCString(url); 94 const char* url_chars = NULL;
95 if (!Dart_IsValidResult(result)) { 95 Dart_Handle result = Dart_StringToCString(url, &url_chars);
96 return Dart_ErrorResult("accessing url characters failed"); 96 if (!Dart_IsValid(result)) {
97 return Dart_Error("accessing url characters failed");
97 } 98 }
98 const char* url_chars = Dart_GetResultAsCString(result);
99 99
100 if (tag == kCanonicalizeUrl) { 100 if (tag == kCanonicalizeUrl) {
101 // Create the full path based on the including library and the current url. 101 // Create the full path based on the including library and the current url.
102 102
103 // Get the url of the calling library. 103 // Get the url of the calling library.
104 result = Dart_LibraryUrl(library); 104 Dart_Handle library_url = Dart_LibraryUrl(library);
105 if (!Dart_IsValidResult(result)) { 105 if (!Dart_IsValid(library_url)) {
106 return Dart_ErrorResult("accessing library url failed"); 106 return Dart_Error("accessing library url failed");
107 } 107 }
108 Dart_Handle library_url = Dart_GetResult(result);
109 if (!Dart_IsString8(library_url)) { 108 if (!Dart_IsString8(library_url)) {
110 return Dart_ErrorResult("library url is not a string"); 109 return Dart_Error("library url is not a string");
111 } 110 }
112 result = Dart_StringToCString(library_url); 111 const char* library_url_chars = NULL;
113 if (!Dart_IsValidResult(result)) { 112 result = Dart_StringToCString(library_url, &library_url_chars);
114 return Dart_ErrorResult("accessing library url characters failed"); 113 if (!Dart_IsValid(result)) {
114 return Dart_Error("accessing library url characters failed");
115 } 115 }
116 const char* library_url_chars = Dart_GetResultAsCString(result);
117 116
118 // Calculate the path. 117 // Calculate the path.
119 const char* canon_url_chars = CanonicalizeUrl(library_url_chars, url_chars); 118 const char* canon_url_chars = CanonicalizeUrl(library_url_chars, url_chars);
120 Dart_Handle canon_url = Dart_NewString(canon_url_chars); 119 Dart_Handle canon_url = Dart_NewString(canon_url_chars);
121 free(const_cast<char*>(canon_url_chars)); 120 free(const_cast<char*>(canon_url_chars));
122 121
123 return Dart_ResultAsObject(canon_url); 122 return canon_url;
124 } 123 }
125 124
126 // The tag is either an import or a source tag. Read the file based on the 125 // The tag is either an import or a source tag. Read the file based on the
127 // url chars. 126 // url chars.
128 result = ReadStringFromFile(url_chars); 127 Dart_Handle source = ReadStringFromFile(url_chars);
129 if (!Dart_IsValidResult(result)) { 128 if (!Dart_IsValid(source)) {
130 return result; 129 return result;
131 } 130 }
132 Dart_Handle source = Dart_GetResult(result);
133 131
134 if (tag == kImportTag) { 132 if (tag == kImportTag) {
135 result = Dart_LoadLibrary(url, source); 133 Dart_Handle new_lib = Dart_LoadLibrary(url, source);
136 if (Dart_IsValidResult(result)) { 134 if (Dart_IsValid(new_lib)) {
137 // TODO(iposva): Should the builtin library be added to all libraries? 135 // TODO(iposva): Should the builtin library be added to all libraries?
138 Dart_Handle new_lib = Dart_GetResult(result);
139 Builtin_ImportLibrary(new_lib); 136 Builtin_ImportLibrary(new_lib);
140 } 137 }
141 return result; 138 return result;
142 } else if (tag == kSourceTag) { 139 } else if (tag == kSourceTag) {
143 return Dart_LoadSource(library, url, source); 140 return Dart_LoadSource(library, url, source);
144 } 141 }
145 return Dart_ErrorResult("wrong tag"); 142 return Dart_Error("wrong tag");
146 } 143 }
147 144
148 145
149 Dart_Result LoadScript(const char* script_name) { 146 Dart_Handle LoadScript(const char* script_name) {
150 Dart_Result result = ReadStringFromFile(script_name); 147 Dart_Handle source = ReadStringFromFile(script_name);
151 if (!Dart_IsValidResult(result)) { 148 if (!Dart_IsValid(source)) {
152 return result; 149 return source;
153 } 150 }
154 Dart_Handle source = Dart_GetResult(result);
155 Dart_Handle url = Dart_NewString(script_name); 151 Dart_Handle url = Dart_NewString(script_name);
156 152
157 return Dart_LoadScript(url, source, LibraryTagHandler); 153 return Dart_LoadScript(url, source, LibraryTagHandler);
158 } 154 }
OLDNEW
« no previous file with comments | « runtime/bin/process_script.h ('k') | runtime/bin/socket.cc » ('j') | runtime/include/dart_api.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698