| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 "vm/kernel_isolate.h" | 5 #include "vm/kernel_isolate.h" |
| 6 | 6 |
| 7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 241 } |
| 242 | 242 |
| 243 Dart_Port KernelIsolate::WaitForKernelPort() { | 243 Dart_Port KernelIsolate::WaitForKernelPort() { |
| 244 MonitorLocker ml(monitor_); | 244 MonitorLocker ml(monitor_); |
| 245 while (initializing_ && (kernel_port_ == ILLEGAL_PORT)) { | 245 while (initializing_ && (kernel_port_ == ILLEGAL_PORT)) { |
| 246 ml.Wait(); | 246 ml.Wait(); |
| 247 } | 247 } |
| 248 return kernel_port_; | 248 return kernel_port_; |
| 249 } | 249 } |
| 250 | 250 |
| 251 static Dart_CObject BuildFilesPairs(int source_files_count, | |
| 252 Dart_SourceFile source_files[]) { | |
| 253 Dart_CObject files; | |
| 254 files.type = Dart_CObject_kArray; | |
| 255 files.value.as_array.length = source_files_count * 2; | |
| 256 // typedef Dart_CObject* Dart_CObjectPtr; | |
| 257 Dart_CObject** fileNamePairs = new Dart_CObject*[source_files_count * 2]; | |
| 258 for (int i = 0; i < source_files_count; i++) { | |
| 259 Dart_CObject* source_uri = new Dart_CObject(); | |
| 260 source_uri->type = Dart_CObject_kString; | |
| 261 source_uri->value.as_string = const_cast<char*>(source_files[i].uri); | |
| 262 fileNamePairs[i * 2] = source_uri; | |
| 263 | |
| 264 Dart_CObject* source_code = new Dart_CObject(); | |
| 265 source_code->type = Dart_CObject_kTypedData; | |
| 266 source_code->value.as_typed_data.type = Dart_TypedData_kUint8; | |
| 267 source_code->value.as_typed_data.length = strlen(source_files[i].source); | |
| 268 source_code->value.as_typed_data.values = | |
| 269 reinterpret_cast<uint8_t*>(const_cast<char*>(source_files[i].source)); | |
| 270 fileNamePairs[(i * 2) + 1] = source_code; | |
| 271 } | |
| 272 files.value.as_array.values = fileNamePairs; | |
| 273 return files; | |
| 274 } | |
| 275 | |
| 276 class KernelCompilationRequest : public ValueObject { | 251 class KernelCompilationRequest : public ValueObject { |
| 277 public: | 252 public: |
| 278 KernelCompilationRequest() | 253 KernelCompilationRequest() |
| 279 : monitor_(new Monitor()), | 254 : monitor_(new Monitor()), |
| 280 port_(Dart_NewNativePort("kernel-compilation-port", | 255 port_(Dart_NewNativePort("kernel-compilation-port", |
| 281 &HandleResponse, | 256 &HandleResponse, |
| 282 false)), | 257 false)), |
| 283 next_(NULL), | 258 next_(NULL), |
| 284 prev_(NULL) { | 259 prev_(NULL) { |
| 285 ASSERT(port_ != ILLEGAL_PORT); | 260 ASSERT(port_ != ILLEGAL_PORT); |
| 286 RegisterRequest(this); | 261 RegisterRequest(this); |
| 287 result_.status = Dart_KernelCompilationStatus_Unknown; | 262 result_.status = Dart_KernelCompilationStatus_Unknown; |
| 288 result_.error = NULL; | 263 result_.error = NULL; |
| 289 result_.kernel = NULL; | 264 result_.kernel = NULL; |
| 290 result_.kernel_size = 0; | 265 result_.kernel_size = 0; |
| 291 } | 266 } |
| 292 | 267 |
| 293 ~KernelCompilationRequest() { | 268 ~KernelCompilationRequest() { |
| 294 UnregisterRequest(this); | 269 UnregisterRequest(this); |
| 295 Dart_CloseNativePort(port_); | 270 Dart_CloseNativePort(port_); |
| 296 delete monitor_; | 271 delete monitor_; |
| 297 } | 272 } |
| 298 | 273 |
| 299 Dart_KernelCompilationResult SendAndWaitForResponse( | 274 Dart_KernelCompilationResult SendAndWaitForResponse( |
| 300 Dart_Port kernel_port, | 275 Dart_Port kernel_port, |
| 301 const char* script_uri, | 276 const char* script_uri, |
| 302 int source_files_count, | 277 int source_files_count, |
| 303 Dart_SourceFile source_files[], | 278 Dart_SourceFile source_files[]) { |
| 304 bool incremental_compile) { | 279 // Build the [null, send_port, script_uri] message for the Kernel isolate: |
| 305 // Build the [null, send_port, script_uri, incremental_compile, isolate_id, | 280 // null tag tells it that request came from this code, instead of Loader |
| 306 // [files]] message for the Kernel isolate: null tag tells it that request | 281 // so that it can given a more informative response. |
| 307 // came from this code, instead of Loader so that it can given a more | |
| 308 // informative response. | |
| 309 Dart_CObject tag; | 282 Dart_CObject tag; |
| 310 tag.type = Dart_CObject_kNull; | 283 tag.type = Dart_CObject_kNull; |
| 311 | 284 |
| 312 Dart_CObject send_port; | 285 Dart_CObject send_port; |
| 313 send_port.type = Dart_CObject_kSendPort; | 286 send_port.type = Dart_CObject_kSendPort; |
| 314 send_port.value.as_send_port.id = port_; | 287 send_port.value.as_send_port.id = port_; |
| 315 send_port.value.as_send_port.origin_id = ILLEGAL_PORT; | 288 send_port.value.as_send_port.origin_id = ILLEGAL_PORT; |
| 316 | 289 |
| 317 Dart_CObject uri; | 290 Dart_CObject uri; |
| 318 uri.type = Dart_CObject_kString; | 291 uri.type = Dart_CObject_kString; |
| 319 uri.value.as_string = const_cast<char*>(script_uri); | 292 uri.value.as_string = const_cast<char*>(script_uri); |
| 320 | 293 |
| 321 Dart_CObject dart_incremental; | |
| 322 dart_incremental.type = Dart_CObject_kBool; | |
| 323 dart_incremental.value.as_bool = incremental_compile; | |
| 324 | |
| 325 // TODO(aam): Assert that isolate exists once we move CompileAndReadScript | |
| 326 // compilation logic out of CreateIsolateAndSetupHelper and into | |
| 327 // IsolateSetupHelper in main.cc. | |
| 328 Isolate* isolate = | |
| 329 Thread::Current() != NULL ? Thread::Current()->isolate() : NULL; | |
| 330 if (incremental_compile) { | |
| 331 ASSERT(isolate != NULL); | |
| 332 } | |
| 333 Dart_CObject isolate_id; | |
| 334 isolate_id.type = Dart_CObject_kInt64; | |
| 335 isolate_id.value.as_int64 = | |
| 336 isolate != NULL ? static_cast<int64_t>(isolate->main_port()) : 0; | |
| 337 | |
| 338 Dart_CObject message; | 294 Dart_CObject message; |
| 339 message.type = Dart_CObject_kArray; | 295 message.type = Dart_CObject_kArray; |
| 340 | 296 |
| 341 intptr_t message_len = 5; | 297 if (source_files_count == 0) { |
| 342 Dart_CObject files; | 298 static const intptr_t message_len = 3; |
| 343 if (source_files_count != 0) { | 299 Dart_CObject* message_arr[] = {&tag, &send_port, &uri}; |
| 344 files = BuildFilesPairs(source_files_count, source_files); | 300 message.value.as_array.values = message_arr; |
| 345 message_len++; | 301 message.value.as_array.length = message_len; |
| 302 // Send the message. |
| 303 Dart_PostCObject(kernel_port, &message); |
| 304 } else { |
| 305 Dart_CObject files; |
| 306 files.type = Dart_CObject_kArray; |
| 307 files.value.as_array.length = source_files_count * 2; |
| 308 // typedef Dart_CObject* Dart_CObjectPtr; |
| 309 Dart_CObject** fileNamePairs = new Dart_CObject*[source_files_count * 2]; |
| 310 for (int i = 0; i < source_files_count; i++) { |
| 311 Dart_CObject* source_uri = new Dart_CObject(); |
| 312 source_uri->type = Dart_CObject_kString; |
| 313 source_uri->value.as_string = const_cast<char*>(source_files[i].uri); |
| 314 fileNamePairs[i * 2] = source_uri; |
| 315 |
| 316 Dart_CObject* source_code = new Dart_CObject(); |
| 317 source_code->type = Dart_CObject_kTypedData; |
| 318 source_code->value.as_typed_data.type = Dart_TypedData_kUint8; |
| 319 source_code->value.as_typed_data.length = |
| 320 strlen(source_files[i].source); |
| 321 source_code->value.as_typed_data.values = reinterpret_cast<uint8_t*>( |
| 322 const_cast<char*>(source_files[i].source)); |
| 323 fileNamePairs[(i * 2) + 1] = source_code; |
| 324 } |
| 325 files.value.as_array.values = fileNamePairs; |
| 326 static const intptr_t message_len = 4; |
| 327 Dart_CObject* message_arr[] = {&tag, &send_port, &uri, &files}; |
| 328 message.value.as_array.values = message_arr; |
| 329 message.value.as_array.length = message_len; |
| 330 Dart_PostCObject(kernel_port, &message); |
| 346 } | 331 } |
| 347 Dart_CObject* message_arr[] = { | |
| 348 &tag, &send_port, &uri, &dart_incremental, &isolate_id, &files}; | |
| 349 message.value.as_array.values = message_arr; | |
| 350 message.value.as_array.length = message_len; | |
| 351 // Send the message. | |
| 352 Dart_PostCObject(kernel_port, &message); | |
| 353 | 332 |
| 354 // Wait for reply to arrive. | 333 // Wait for reply to arrive. |
| 355 MonitorLocker ml(monitor_); | 334 MonitorLocker ml(monitor_); |
| 356 while (result_.status == Dart_KernelCompilationStatus_Unknown) { | 335 while (result_.status == Dart_KernelCompilationStatus_Unknown) { |
| 357 ml.Wait(); | 336 ml.Wait(); |
| 358 } | 337 } |
| 359 | 338 |
| 360 return result_; | 339 return result_; |
| 361 } | 340 } |
| 362 | 341 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 432 |
| 454 Dart_KernelCompilationResult result_; | 433 Dart_KernelCompilationResult result_; |
| 455 }; | 434 }; |
| 456 | 435 |
| 457 Monitor* KernelCompilationRequest::requests_monitor_ = new Monitor(); | 436 Monitor* KernelCompilationRequest::requests_monitor_ = new Monitor(); |
| 458 KernelCompilationRequest* KernelCompilationRequest::requests_ = NULL; | 437 KernelCompilationRequest* KernelCompilationRequest::requests_ = NULL; |
| 459 | 438 |
| 460 Dart_KernelCompilationResult KernelIsolate::CompileToKernel( | 439 Dart_KernelCompilationResult KernelIsolate::CompileToKernel( |
| 461 const char* script_uri, | 440 const char* script_uri, |
| 462 int source_file_count, | 441 int source_file_count, |
| 463 Dart_SourceFile source_files[], | 442 Dart_SourceFile source_files[]) { |
| 464 bool incremental_compile) { | |
| 465 // This must be the main script to be loaded. Wait for Kernel isolate | 443 // This must be the main script to be loaded. Wait for Kernel isolate |
| 466 // to finish initialization. | 444 // to finish initialization. |
| 467 Dart_Port kernel_port = WaitForKernelPort(); | 445 Dart_Port kernel_port = WaitForKernelPort(); |
| 468 if (kernel_port == ILLEGAL_PORT) { | 446 if (kernel_port == ILLEGAL_PORT) { |
| 469 Dart_KernelCompilationResult result; | 447 Dart_KernelCompilationResult result; |
| 470 result.status = Dart_KernelCompilationStatus_Unknown; | 448 result.status = Dart_KernelCompilationStatus_Unknown; |
| 471 result.error = strdup("Error while initializing Kernel isolate"); | 449 result.error = strdup("Error while initializing Kernel isolate"); |
| 472 return result; | 450 return result; |
| 473 } | 451 } |
| 474 | 452 |
| 475 KernelCompilationRequest request; | 453 KernelCompilationRequest request; |
| 476 return request.SendAndWaitForResponse(kernel_port, script_uri, | 454 return request.SendAndWaitForResponse(kernel_port, script_uri, |
| 477 source_file_count, source_files, | 455 source_file_count, source_files); |
| 478 incremental_compile); | |
| 479 } | 456 } |
| 480 | 457 |
| 481 #endif // DART_PRECOMPILED_RUNTIME | 458 #endif // DART_PRECOMPILED_RUNTIME |
| 482 | 459 |
| 483 } // namespace dart | 460 } // namespace dart |
| OLD | NEW |