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 "vm/unit_test.h" | 5 #include "vm/unit_test.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 static Dart_Handle ResolvePackageUri(const char* uri_chars) { | 157 static Dart_Handle ResolvePackageUri(const char* uri_chars) { |
158 const int kNumArgs = 1; | 158 const int kNumArgs = 1; |
159 Dart_Handle dart_args[kNumArgs]; | 159 Dart_Handle dart_args[kNumArgs]; |
160 dart_args[0] = DartUtils::NewString(uri_chars); | 160 dart_args[0] = DartUtils::NewString(uri_chars); |
161 return Dart_Invoke(DartUtils::BuiltinLib(), | 161 return Dart_Invoke(DartUtils::BuiltinLib(), |
162 DartUtils::NewString("_filePathFromUri"), kNumArgs, | 162 DartUtils::NewString("_filePathFromUri"), kNumArgs, |
163 dart_args); | 163 dart_args); |
164 } | 164 } |
165 | 165 |
166 static ThreadLocalKey script_reload_key = kUnsetThreadLocalKey; | 166 static ThreadLocalKey script_reload_key = kUnsetThreadLocalKey; |
| 167 static ThreadLocalKey kernel_reload_key = kUnsetThreadLocalKey; |
167 | 168 |
168 char* TestCase::CompileTestScriptWithDFE(const char* url, | 169 char* TestCase::CompileTestScriptWithDFE(const char* url, |
169 const char* source, | 170 const char* source, |
170 void** kernel_pgm) { | 171 void** kernel_pgm, |
171 Zone* zone = Thread::Current()->zone(); | 172 bool incrementally) { |
172 // clang-format off | 173 // clang-format off |
173 Dart_SourceFile sourcefiles[] = { | 174 Dart_SourceFile sourcefiles[] = { |
174 { | 175 { |
175 url, source, | 176 url, source, |
176 }, | 177 }, |
177 { | 178 { |
178 "file:///.packages", "untitled:/" | 179 "file:///.packages", "untitled:/" |
179 }}; | 180 }}; |
180 // clang-format on | 181 // clang-format on |
181 int sourcefiles_count = sizeof(sourcefiles) / sizeof(Dart_SourceFile); | 182 return CompileTestScriptWithDFE(url, |
182 Dart_KernelCompilationResult compilation_result = | 183 sizeof(sourcefiles) / sizeof(Dart_SourceFile), |
183 Dart_CompileSourcesToKernel(url, sourcefiles_count, sourcefiles); | 184 sourcefiles, kernel_pgm, incrementally); |
| 185 } |
| 186 |
| 187 char* TestCase::CompileTestScriptWithDFE(const char* url, |
| 188 int sourcefiles_count, |
| 189 Dart_SourceFile sourcefiles[], |
| 190 void** kernel_pgm, |
| 191 bool incrementally) { |
| 192 Zone* zone = Thread::Current()->zone(); |
| 193 Dart_KernelCompilationResult compilation_result = Dart_CompileSourcesToKernel( |
| 194 url, sourcefiles_count, sourcefiles, incrementally); |
184 | 195 |
185 if (compilation_result.status != Dart_KernelCompilationStatus_Ok) { | 196 if (compilation_result.status != Dart_KernelCompilationStatus_Ok) { |
186 return OS::SCreate(zone, "Compilation failed %s", compilation_result.error); | 197 return OS::SCreate(zone, "Compilation failed %s", compilation_result.error); |
187 } | 198 } |
188 const uint8_t* kernel_file = compilation_result.kernel; | 199 const uint8_t* kernel_file = compilation_result.kernel; |
189 intptr_t kernel_length = compilation_result.kernel_size; | 200 intptr_t kernel_length = compilation_result.kernel_size; |
190 if (kernel_file == NULL) { | 201 if (kernel_file == NULL) { |
191 return OS::SCreate(zone, "front end generated a NULL kernel file"); | 202 return OS::SCreate(zone, "front end generated a NULL kernel file"); |
192 } | 203 } |
193 *kernel_pgm = Dart_ReadKernelBinary(kernel_file, kernel_length); | 204 *kernel_pgm = Dart_ReadKernelBinary(kernel_file, kernel_length); |
194 if (*kernel_pgm == NULL) { | 205 if (*kernel_pgm == NULL) { |
195 return OS::SCreate(zone, "Failed to read generated kernel binary"); | 206 return OS::SCreate(zone, "Failed to read generated kernel binary"); |
196 } | 207 } |
197 return NULL; | 208 return NULL; |
198 } | 209 } |
199 | 210 |
200 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | 211 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
201 Dart_Handle library, | 212 Dart_Handle library, |
202 Dart_Handle url) { | 213 Dart_Handle url) { |
203 if (FLAG_use_dart_frontend) { | 214 if (FLAG_use_dart_frontend) { |
204 // Reload request. | 215 // Reload request. |
205 ASSERT(script_reload_key != kUnsetThreadLocalKey); | 216 |
206 const char* script_source = reinterpret_cast<const char*>( | |
207 OSThread::GetThreadLocal(script_reload_key)); | |
208 ASSERT(script_source != NULL); | |
209 OSThread::SetThreadLocal(script_reload_key, 0); | |
210 const char* urlstr = NULL; | 217 const char* urlstr = NULL; |
211 Dart_Handle result = Dart_StringToCString(url, &urlstr); | 218 Dart_Handle result = Dart_StringToCString(url, &urlstr); |
212 if (Dart_IsError(result)) { | 219 if (Dart_IsError(result)) { |
213 return Dart_NewApiError("accessing url characters failed"); | 220 return Dart_NewApiError("accessing url characters failed"); |
214 } | 221 } |
| 222 |
| 223 // Updated library either arrives as dart source or as |
| 224 // a precompiled kernel binary. |
215 void* kernel_pgm; | 225 void* kernel_pgm; |
216 char* error = | 226 if (script_reload_key != kUnsetThreadLocalKey) { |
217 TestCase::CompileTestScriptWithDFE(urlstr, script_source, &kernel_pgm); | 227 const char* script_source = reinterpret_cast<const char*>( |
218 if (error == NULL) { | 228 OSThread::GetThreadLocal(script_reload_key)); |
219 return Dart_LoadScript(url, Dart_Null(), | 229 ASSERT(script_source != NULL); |
220 reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0); | 230 OSThread::SetThreadLocal(script_reload_key, 0); |
| 231 char* error = TestCase::CompileTestScriptWithDFE(urlstr, script_source, |
| 232 &kernel_pgm); |
| 233 if (error != NULL) { |
| 234 return Dart_NewApiError(error); |
| 235 } |
221 } else { | 236 } else { |
222 return Dart_NewApiError(error); | 237 ASSERT(kernel_reload_key != kUnsetThreadLocalKey); |
| 238 kernel_pgm = |
| 239 reinterpret_cast<void*>(OSThread::GetThreadLocal(kernel_reload_key)); |
| 240 ASSERT(kernel_pgm != NULL); |
| 241 OSThread::SetThreadLocal(kernel_reload_key, 0); |
223 } | 242 } |
| 243 return Dart_LoadScript(url, Dart_Null(), |
| 244 reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0); |
224 } | 245 } |
225 if (tag == Dart_kCanonicalizeUrl) { | 246 if (tag == Dart_kCanonicalizeUrl) { |
226 Dart_Handle library_url = Dart_LibraryUrl(library); | 247 Dart_Handle library_url = Dart_LibraryUrl(library); |
227 if (Dart_IsError(library_url)) { | 248 if (Dart_IsError(library_url)) { |
228 return library_url; | 249 return library_url; |
229 } | 250 } |
230 return Dart_DefaultCanonicalizeUrl(library_url, url); | 251 return Dart_DefaultCanonicalizeUrl(library_url, url); |
231 } | 252 } |
232 if (tag == Dart_kScriptTag) { | 253 if (tag == Dart_kScriptTag) { |
233 // Reload request. | 254 // Reload request. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 DART_CHECK_VALID(lib); | 342 DART_CHECK_VALID(lib); |
322 result = Dart_SetNativeResolver(lib, resolver, NULL); | 343 result = Dart_SetNativeResolver(lib, resolver, NULL); |
323 DART_CHECK_VALID(result); | 344 DART_CHECK_VALID(result); |
324 if (finalize_classes) { | 345 if (finalize_classes) { |
325 result = Dart_FinalizeLoading(false); | 346 result = Dart_FinalizeLoading(false); |
326 DART_CHECK_VALID(result); | 347 DART_CHECK_VALID(result); |
327 } | 348 } |
328 return lib; | 349 return lib; |
329 } | 350 } |
330 | 351 |
331 static Dart_Handle LoadTestScriptWithDFE(const char* script, | |
332 Dart_NativeEntryResolver resolver, | |
333 const char* lib_url, | |
334 bool finalize_classes) { | |
335 Dart_Handle url = NewString(lib_url); | |
336 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); | |
337 EXPECT_VALID(result); | |
338 void* kernel_pgm = NULL; | |
339 char* error = | |
340 TestCase::CompileTestScriptWithDFE(lib_url, script, &kernel_pgm); | |
341 if (error == NULL) { | |
342 Dart_Handle lib = Dart_LoadScript( | |
343 url, Dart_Null(), reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0); | |
344 DART_CHECK_VALID(lib); | |
345 result = Dart_SetNativeResolver(lib, resolver, NULL); | |
346 DART_CHECK_VALID(result); | |
347 if (finalize_classes) { | |
348 result = Dart_FinalizeLoading(false); | |
349 DART_CHECK_VALID(result); | |
350 } | |
351 return lib; | |
352 } else { | |
353 return Dart_NewApiError(error); | |
354 } | |
355 } | |
356 | |
357 Dart_Handle TestCase::LoadTestScript(const char* script, | 352 Dart_Handle TestCase::LoadTestScript(const char* script, |
358 Dart_NativeEntryResolver resolver, | 353 Dart_NativeEntryResolver resolver, |
359 const char* lib_url, | 354 const char* lib_url, |
360 bool finalize_classes) { | 355 bool finalize_classes) { |
361 if (!FLAG_use_dart_frontend) { | 356 return FLAG_use_dart_frontend |
362 return LoadTestScriptWithVMParser(script, resolver, lib_url, | 357 ? LoadTestScriptWithDFE( |
363 finalize_classes); | 358 1, |
364 } else { | 359 (Dart_SourceFile[1]){{OS::SCreate(Thread::Current()->zone(), |
365 Zone* zone = Thread::Current()->zone(); | 360 "file:///%s", lib_url), |
366 char* resolved_lib_url = OS::SCreate(zone, "file:///%s", lib_url); | 361 script}}, |
367 return LoadTestScriptWithDFE(script, resolver, resolved_lib_url, | 362 resolver, finalize_classes) |
368 finalize_classes); | 363 : LoadTestScriptWithVMParser(script, resolver, lib_url, |
| 364 finalize_classes); |
| 365 } |
| 366 |
| 367 Dart_Handle TestCase::LoadTestScriptWithDFE(int sourcefiles_count, |
| 368 Dart_SourceFile sourcefiles[], |
| 369 Dart_NativeEntryResolver resolver, |
| 370 bool finalize, |
| 371 bool incrementally) { |
| 372 // First script is the main script. |
| 373 Dart_Handle url = NewString(sourcefiles[0].uri); |
| 374 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); |
| 375 EXPECT_VALID(result); |
| 376 void* kernel_pgm = NULL; |
| 377 char* error = TestCase::CompileTestScriptWithDFE( |
| 378 sourcefiles[0].uri, sourcefiles_count, sourcefiles, &kernel_pgm, |
| 379 incrementally); |
| 380 if (error != NULL) { |
| 381 return Dart_NewApiError(error); |
369 } | 382 } |
| 383 Dart_Handle lib = Dart_LoadScript( |
| 384 url, Dart_Null(), reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0); |
| 385 DART_CHECK_VALID(lib); |
| 386 result = Dart_SetNativeResolver(lib, resolver, NULL); |
| 387 DART_CHECK_VALID(result); |
| 388 if (finalize) { |
| 389 result = Dart_FinalizeLoading(false); |
| 390 DART_CHECK_VALID(result); |
| 391 } |
| 392 return lib; |
370 } | 393 } |
371 | 394 |
372 #ifndef PRODUCT | 395 #ifndef PRODUCT |
373 | 396 |
374 void TestCase::SetReloadTestScript(const char* script) { | 397 void TestCase::SetReloadTestScript(const char* script) { |
375 if (script_reload_key == kUnsetThreadLocalKey) { | 398 if (script_reload_key == kUnsetThreadLocalKey) { |
376 script_reload_key = OSThread::CreateThreadLocal(); | 399 script_reload_key = OSThread::CreateThreadLocal(); |
377 } | 400 } |
378 ASSERT(script_reload_key != kUnsetThreadLocalKey); | 401 ASSERT(script_reload_key != kUnsetThreadLocalKey); |
379 ASSERT(OSThread::GetThreadLocal(script_reload_key) == 0); | 402 ASSERT(OSThread::GetThreadLocal(script_reload_key) == 0); |
380 // Store the new script in TLS. | 403 // Store the new script in TLS. |
381 OSThread::SetThreadLocal(script_reload_key, reinterpret_cast<uword>(script)); | 404 OSThread::SetThreadLocal(script_reload_key, reinterpret_cast<uword>(script)); |
382 } | 405 } |
383 | 406 |
| 407 void TestCase::SetReloadTestKernel(const void* kernel) { |
| 408 if (kernel_reload_key == kUnsetThreadLocalKey) { |
| 409 kernel_reload_key = OSThread::CreateThreadLocal(); |
| 410 } |
| 411 ASSERT(kernel_reload_key != kUnsetThreadLocalKey); |
| 412 ASSERT(OSThread::GetThreadLocal(kernel_reload_key) == 0); |
| 413 // Store the new script in TLS. |
| 414 OSThread::SetThreadLocal(kernel_reload_key, reinterpret_cast<uword>(kernel)); |
| 415 } |
| 416 |
384 Dart_Handle TestCase::TriggerReload() { | 417 Dart_Handle TestCase::TriggerReload() { |
385 Isolate* isolate = Isolate::Current(); | 418 Isolate* isolate = Isolate::Current(); |
386 JSONStream js; | 419 JSONStream js; |
387 bool success = false; | 420 bool success = false; |
388 { | 421 { |
389 TransitionNativeToVM transition(Thread::Current()); | 422 TransitionNativeToVM transition(Thread::Current()); |
390 success = isolate->ReloadSources(&js, | 423 success = isolate->ReloadSources(&js, |
391 false, // force_reload | 424 false, // force_reload |
392 NULL, NULL, | 425 NULL, NULL, |
393 true); // dont_delete_reload_context | 426 true); // dont_delete_reload_context |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 TransitionNativeToVM transition(thread); | 461 TransitionNativeToVM transition(thread); |
429 Isolate* isolate = thread->isolate(); | 462 Isolate* isolate = thread->isolate(); |
430 if (isolate->reload_context() != NULL) { | 463 if (isolate->reload_context() != NULL) { |
431 isolate->DeleteReloadContext(); | 464 isolate->DeleteReloadContext(); |
432 } | 465 } |
433 } | 466 } |
434 | 467 |
435 return result; | 468 return result; |
436 } | 469 } |
437 | 470 |
| 471 Dart_Handle TestCase::ReloadTestKernel(const void* kernel) { |
| 472 SetReloadTestKernel(kernel); |
| 473 |
| 474 Dart_Handle result = TriggerReload(); |
| 475 if (Dart_IsError(result)) { |
| 476 return result; |
| 477 } |
| 478 |
| 479 result = GetReloadErrorOrRootLibrary(); |
| 480 |
| 481 { |
| 482 Thread* thread = Thread::Current(); |
| 483 TransitionNativeToVM transition(thread); |
| 484 Isolate* isolate = thread->isolate(); |
| 485 if (isolate->reload_context() != NULL) { |
| 486 isolate->DeleteReloadContext(); |
| 487 } |
| 488 } |
| 489 |
| 490 return result; |
| 491 } |
| 492 |
438 #endif // !PRODUCT | 493 #endif // !PRODUCT |
439 | 494 |
440 Dart_Handle TestCase::LoadCoreTestScript(const char* script, | 495 Dart_Handle TestCase::LoadCoreTestScript(const char* script, |
441 Dart_NativeEntryResolver resolver) { | 496 Dart_NativeEntryResolver resolver) { |
442 return LoadTestScript(script, resolver, CORELIB_TEST_URI); | 497 return LoadTestScript(script, resolver, CORELIB_TEST_URI); |
443 } | 498 } |
444 | 499 |
445 Dart_Handle TestCase::lib() { | 500 Dart_Handle TestCase::lib() { |
446 Dart_Handle url = NewString(TestCase::url()); | 501 Dart_Handle url = NewString(TestCase::url()); |
447 Dart_Handle lib = Dart_LookupLibrary(url); | 502 Dart_Handle lib = Dart_LookupLibrary(url); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 pos = strstr(in, prefix); | 621 pos = strstr(in, prefix); |
567 } | 622 } |
568 // Copy the remainder of in to out. | 623 // Copy the remainder of in to out. |
569 while (*in != '\0') { | 624 while (*in != '\0') { |
570 *out++ = *in++; | 625 *out++ = *in++; |
571 } | 626 } |
572 *out = '\0'; | 627 *out = '\0'; |
573 } | 628 } |
574 | 629 |
575 } // namespace dart | 630 } // namespace dart |
OLD | NEW |