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 <math.h> | 5 #include <math.h> |
6 #include <stdarg.h> | 6 #include <stdarg.h> |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 error); | 122 error); |
123 } | 123 } |
124 | 124 |
125 const char* VM_FLAGS[] = { | 125 const char* VM_FLAGS[] = { |
126 "--enable_type_checks", // TODO(gram): This should be an option! | 126 "--enable_type_checks", // TODO(gram): This should be an option! |
127 // "--trace_isolates", | 127 // "--trace_isolates", |
128 // "--trace_natives", | 128 // "--trace_natives", |
129 // "--trace_compiler", | 129 // "--trace_compiler", |
130 }; | 130 }; |
131 | 131 |
| 132 static void* openFileCallback(const char* name, bool write) { |
| 133 return fopen(name, write ? "w" : "r"); |
| 134 } |
| 135 |
| 136 static void readFileCallback(const uint8_t** data, intptr_t* fileLength, |
| 137 void* stream) { |
| 138 if (!stream) { |
| 139 *data = 0; |
| 140 *fileLength = 0; |
| 141 } else { |
| 142 FILE* file = reinterpret_cast<FILE*>(stream); |
| 143 |
| 144 // Get the file size. |
| 145 fseek(file, 0, SEEK_END); |
| 146 *fileLength = ftell(file); |
| 147 rewind(file); |
| 148 |
| 149 // Allocate data buffer. |
| 150 *data = new uint8_t[*fileLength]; |
| 151 *fileLength = fread(const_cast<uint8_t*>(*data), 1, *fileLength, file); |
| 152 } |
| 153 } |
| 154 |
| 155 static void writeFileCallback(const void* data, intptr_t length, void* file) { |
| 156 fwrite(data, 1, length, reinterpret_cast<FILE*>(file)); |
| 157 } |
| 158 |
| 159 static void closeFileCallback(void* file) { |
| 160 fclose(reinterpret_cast<FILE*>(file)); |
| 161 } |
| 162 |
132 int VMGlue::InitializeVM() { | 163 int VMGlue::InitializeVM() { |
133 // We need the next call to get Dart_Initialize not to bail early. | 164 // We need the next call to get Dart_Initialize not to bail early. |
134 LOGI("Setting VM Options"); | 165 LOGI("Setting VM Options"); |
135 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); | 166 Dart_SetVMFlags(sizeof(VM_FLAGS) / sizeof(VM_FLAGS[0]), VM_FLAGS); |
136 | 167 |
137 // Initialize the Dart VM, providing the callbacks to use for | 168 // Initialize the Dart VM, providing the callbacks to use for |
138 // creating and shutting down isolates. | 169 // creating and shutting down isolates. |
139 LOGI("Initializing Dart"); | 170 LOGI("Initializing Dart"); |
140 if (!Dart_Initialize(CreateIsolateAndSetup, | 171 if (!Dart_Initialize(CreateIsolateAndSetup, |
141 NULL, | 172 0, |
142 NULL, | 173 0, |
143 NULL, | 174 0, |
144 NULL, | 175 openFileCallback, |
145 NULL, | 176 readFileCallback, |
146 NULL, | 177 writeFileCallback, |
147 NULL)) { | 178 closeFileCallback)) { |
148 LOGE("VM initialization failed\n"); | 179 LOGE("VM initialization failed\n"); |
149 return -1; | 180 return -1; |
150 } | 181 } |
151 initialized_vm_ = true; | 182 initialized_vm_ = true; |
152 | 183 |
153 return 0; | 184 return 0; |
154 } | 185 } |
155 | 186 |
156 Dart_Handle VMGlue::LoadSourceFromFile(const char* url) { | 187 Dart_Handle VMGlue::LoadSourceFromFile(const char* url) { |
157 FILE* file = fopen(url, "r"); | 188 FILE* file = fopen(url, "r"); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 389 |
359 void VMGlue::FinishMainIsolate() { | 390 void VMGlue::FinishMainIsolate() { |
360 LOGI("Finish main isolate"); | 391 LOGI("Finish main isolate"); |
361 Dart_EnterIsolate(isolate_); | 392 Dart_EnterIsolate(isolate_); |
362 // Shutdown the isolate. | 393 // Shutdown the isolate. |
363 Dart_ShutdownIsolate(); | 394 Dart_ShutdownIsolate(); |
364 isolate_ = NULL; | 395 isolate_ = NULL; |
365 initialized_script_ = false; | 396 initialized_script_ = false; |
366 } | 397 } |
367 | 398 |
OLD | NEW |