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

Side by Side Diff: runtime/vm/unit_test.cc

Issue 2993013002: Introduce IKG into kernel-service to support incremental compilation. (Closed)
Patch Set: Use new acceptDelta api. Safe guard against no thread/isolate Created 3 years, 4 months 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
OLDNEW
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
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,
176 Dart_KernelCompilationResult compilation_result = 177 sizeof(sourcefiles) / sizeof(Dart_SourceFile),
177 Dart_CompileSourcesToKernel(url, sourcefiles_count, sourcefiles); 178 sourcefiles, kernel_pgm, incrementally);
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();
187 Dart_KernelCompilationResult compilation_result = Dart_CompileSourcesToKernel(
188 url, sourcefiles_count, sourcefiles, incrementally);
178 189
179 if (compilation_result.status != Dart_KernelCompilationStatus_Ok) { 190 if (compilation_result.status != Dart_KernelCompilationStatus_Ok) {
180 return OS::SCreate(zone, "Compilation failed %s", compilation_result.error); 191 return OS::SCreate(zone, "Compilation failed %s", compilation_result.error);
181 } 192 }
182 const uint8_t* kernel_file = compilation_result.kernel; 193 const uint8_t* kernel_file = compilation_result.kernel;
183 intptr_t kernel_length = compilation_result.kernel_size; 194 intptr_t kernel_length = compilation_result.kernel_size;
184 if (kernel_file == NULL) { 195 if (kernel_file == NULL) {
185 return OS::SCreate(zone, "front end generated a NULL kernel file"); 196 return OS::SCreate(zone, "front end generated a NULL kernel file");
186 } 197 }
187 *kernel_pgm = Dart_ReadKernelBinary(kernel_file, kernel_length); 198 *kernel_pgm = Dart_ReadKernelBinary(kernel_file, kernel_length);
188 if (*kernel_pgm == NULL) { 199 if (*kernel_pgm == NULL) {
189 return OS::SCreate(zone, "Failed to read generated kernel binary"); 200 return OS::SCreate(zone, "Failed to read generated kernel binary");
190 } 201 }
191 return NULL; 202 return NULL;
192 } 203 }
193 204
194 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, 205 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
195 Dart_Handle library, 206 Dart_Handle library,
196 Dart_Handle url) { 207 Dart_Handle url) {
197 if (FLAG_use_dart_frontend) { 208 if (FLAG_use_dart_frontend) {
198 // Reload request. 209 // Reload request.
199 ASSERT(script_reload_key != kUnsetThreadLocalKey); 210
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; 211 const char* urlstr = NULL;
205 Dart_Handle result = Dart_StringToCString(url, &urlstr); 212 Dart_Handle result = Dart_StringToCString(url, &urlstr);
206 if (Dart_IsError(result)) { 213 if (Dart_IsError(result)) {
207 return Dart_NewApiError("accessing url characters failed"); 214 return Dart_NewApiError("accessing url characters failed");
208 } 215 }
216
217 // Updated library either arrives as dart source or as
218 // a precompiled kernel binary.
209 void* kernel_pgm; 219 void* kernel_pgm;
210 char* error = 220 if (script_reload_key != kUnsetThreadLocalKey) {
211 TestCase::CompileTestScriptWithDFE(urlstr, script_source, &kernel_pgm); 221 const char* script_source = reinterpret_cast<const char*>(
212 if (error == NULL) { 222 OSThread::GetThreadLocal(script_reload_key));
213 return Dart_LoadScript(url, Dart_Null(), 223 ASSERT(script_source != NULL);
214 reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0); 224 OSThread::SetThreadLocal(script_reload_key, 0);
225 char* error = TestCase::CompileTestScriptWithDFE(urlstr, script_source,
226 &kernel_pgm);
227 if (error != NULL) {
228 return Dart_NewApiError(error);
229 }
215 } else { 230 } else {
216 return Dart_NewApiError(error); 231 ASSERT(kernel_reload_key != kUnsetThreadLocalKey);
232 kernel_pgm =
233 reinterpret_cast<void*>(OSThread::GetThreadLocal(kernel_reload_key));
234 ASSERT(kernel_pgm != NULL);
235 OSThread::SetThreadLocal(kernel_reload_key, 0);
217 } 236 }
237 return Dart_LoadScript(url, Dart_Null(),
238 reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0);
218 } 239 }
219 if (tag == Dart_kCanonicalizeUrl) { 240 if (tag == Dart_kCanonicalizeUrl) {
220 Dart_Handle library_url = Dart_LibraryUrl(library); 241 Dart_Handle library_url = Dart_LibraryUrl(library);
221 if (Dart_IsError(library_url)) { 242 if (Dart_IsError(library_url)) {
222 return library_url; 243 return library_url;
223 } 244 }
224 return Dart_DefaultCanonicalizeUrl(library_url, url); 245 return Dart_DefaultCanonicalizeUrl(library_url, url);
225 } 246 }
226 if (tag == Dart_kScriptTag) { 247 if (tag == Dart_kScriptTag) {
227 // Reload request. 248 // Reload request.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 DART_CHECK_VALID(lib); 336 DART_CHECK_VALID(lib);
316 result = Dart_SetNativeResolver(lib, resolver, NULL); 337 result = Dart_SetNativeResolver(lib, resolver, NULL);
317 DART_CHECK_VALID(result); 338 DART_CHECK_VALID(result);
318 if (finalize_classes) { 339 if (finalize_classes) {
319 result = Dart_FinalizeLoading(false); 340 result = Dart_FinalizeLoading(false);
320 DART_CHECK_VALID(result); 341 DART_CHECK_VALID(result);
321 } 342 }
322 return lib; 343 return lib;
323 } 344 }
324 345
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, 346 Dart_Handle TestCase::LoadTestScript(const char* script,
352 Dart_NativeEntryResolver resolver, 347 Dart_NativeEntryResolver resolver,
353 const char* lib_url, 348 const char* lib_url,
354 bool finalize_classes) { 349 bool finalize_classes) {
355 if (!FLAG_use_dart_frontend) { 350 return FLAG_use_dart_frontend
356 return LoadTestScriptWithVMParser(script, resolver, lib_url, 351 ? LoadTestScriptWithDFE(
357 finalize_classes); 352 1,
358 } else { 353 (Dart_SourceFile[1]){{OS::SCreate(Thread::Current()->zone(),
359 Zone* zone = Thread::Current()->zone(); 354 "file:///%s", lib_url),
360 char* resolved_lib_url = OS::SCreate(zone, "file:///%s", lib_url); 355 script}},
361 return LoadTestScriptWithDFE(script, resolver, resolved_lib_url, 356 resolver, finalize_classes)
362 finalize_classes); 357 : LoadTestScriptWithVMParser(script, resolver, lib_url,
358 finalize_classes);
359 }
360
361 Dart_Handle TestCase::LoadTestScriptWithDFE(int sourcefiles_count,
362 Dart_SourceFile sourcefiles[],
363 Dart_NativeEntryResolver resolver,
364 bool finalize,
365 bool incrementally) {
366 // First script is the main script.
367 Dart_Handle url = NewString(sourcefiles[0].uri);
368 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler);
369 EXPECT_VALID(result);
370 void* kernel_pgm = NULL;
371 char* error = TestCase::CompileTestScriptWithDFE(
372 sourcefiles[0].uri, sourcefiles_count, sourcefiles, &kernel_pgm,
373 incrementally);
374 if (error != NULL) {
375 return Dart_NewApiError(error);
363 } 376 }
377 Dart_Handle lib = Dart_LoadScript(
378 url, Dart_Null(), reinterpret_cast<Dart_Handle>(kernel_pgm), 0, 0);
379 DART_CHECK_VALID(lib);
380 result = Dart_SetNativeResolver(lib, resolver, NULL);
381 DART_CHECK_VALID(result);
382 if (finalize) {
383 result = Dart_FinalizeLoading(false);
384 DART_CHECK_VALID(result);
385 }
386 return lib;
364 } 387 }
365 388
366 #ifndef PRODUCT 389 #ifndef PRODUCT
367 390
368 void TestCase::SetReloadTestScript(const char* script) { 391 void TestCase::SetReloadTestScript(const char* script) {
369 if (script_reload_key == kUnsetThreadLocalKey) { 392 if (script_reload_key == kUnsetThreadLocalKey) {
370 script_reload_key = OSThread::CreateThreadLocal(); 393 script_reload_key = OSThread::CreateThreadLocal();
371 } 394 }
372 ASSERT(script_reload_key != kUnsetThreadLocalKey); 395 ASSERT(script_reload_key != kUnsetThreadLocalKey);
373 ASSERT(OSThread::GetThreadLocal(script_reload_key) == 0); 396 ASSERT(OSThread::GetThreadLocal(script_reload_key) == 0);
374 // Store the new script in TLS. 397 // Store the new script in TLS.
375 OSThread::SetThreadLocal(script_reload_key, reinterpret_cast<uword>(script)); 398 OSThread::SetThreadLocal(script_reload_key, reinterpret_cast<uword>(script));
376 } 399 }
377 400
401 void TestCase::SetReloadTestKernel(const void* kernel) {
402 if (kernel_reload_key == kUnsetThreadLocalKey) {
403 kernel_reload_key = OSThread::CreateThreadLocal();
404 }
405 ASSERT(kernel_reload_key != kUnsetThreadLocalKey);
406 ASSERT(OSThread::GetThreadLocal(kernel_reload_key) == 0);
407 // Store the new script in TLS.
408 OSThread::SetThreadLocal(kernel_reload_key, reinterpret_cast<uword>(kernel));
409 }
410
378 Dart_Handle TestCase::TriggerReload() { 411 Dart_Handle TestCase::TriggerReload() {
379 Isolate* isolate = Isolate::Current(); 412 Isolate* isolate = Isolate::Current();
380 JSONStream js; 413 JSONStream js;
381 bool success = false; 414 bool success = false;
382 { 415 {
383 TransitionNativeToVM transition(Thread::Current()); 416 TransitionNativeToVM transition(Thread::Current());
384 success = isolate->ReloadSources(&js, 417 success = isolate->ReloadSources(&js,
385 false, // force_reload 418 false, // force_reload
386 NULL, NULL, 419 NULL, NULL,
387 true); // dont_delete_reload_context 420 true); // dont_delete_reload_context
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 TransitionNativeToVM transition(thread); 455 TransitionNativeToVM transition(thread);
423 Isolate* isolate = thread->isolate(); 456 Isolate* isolate = thread->isolate();
424 if (isolate->reload_context() != NULL) { 457 if (isolate->reload_context() != NULL) {
425 isolate->DeleteReloadContext(); 458 isolate->DeleteReloadContext();
426 } 459 }
427 } 460 }
428 461
429 return result; 462 return result;
430 } 463 }
431 464
465 Dart_Handle TestCase::ReloadTestKernel(const void* kernel) {
466 SetReloadTestKernel(kernel);
467
468 Dart_Handle result = TriggerReload();
469 if (Dart_IsError(result)) {
470 return result;
471 }
472
473 result = GetReloadErrorOrRootLibrary();
474
475 {
476 Thread* thread = Thread::Current();
477 TransitionNativeToVM transition(thread);
478 Isolate* isolate = thread->isolate();
479 if (isolate->reload_context() != NULL) {
480 isolate->DeleteReloadContext();
481 }
482 }
483
484 return result;
485 }
486
432 #endif // !PRODUCT 487 #endif // !PRODUCT
433 488
434 Dart_Handle TestCase::LoadCoreTestScript(const char* script, 489 Dart_Handle TestCase::LoadCoreTestScript(const char* script,
435 Dart_NativeEntryResolver resolver) { 490 Dart_NativeEntryResolver resolver) {
436 return LoadTestScript(script, resolver, CORELIB_TEST_URI); 491 return LoadTestScript(script, resolver, CORELIB_TEST_URI);
437 } 492 }
438 493
439 Dart_Handle TestCase::lib() { 494 Dart_Handle TestCase::lib() {
440 Dart_Handle url = NewString(TestCase::url()); 495 Dart_Handle url = NewString(TestCase::url());
441 Dart_Handle lib = Dart_LookupLibrary(url); 496 Dart_Handle lib = Dart_LookupLibrary(url);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 pos = strstr(in, prefix); 615 pos = strstr(in, prefix);
561 } 616 }
562 // Copy the remainder of in to out. 617 // Copy the remainder of in to out.
563 while (*in != '\0') { 618 while (*in != '\0') {
564 *out++ = *in++; 619 *out++ = *in++;
565 } 620 }
566 *out = '\0'; 621 *out = '\0';
567 } 622 }
568 623
569 } // namespace dart 624 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698