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