| 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 Dart_Handle dart_args[kNumArgs]; | 163 Dart_Handle dart_args[kNumArgs]; |
| 164 dart_args[0] = DartUtils::NewString(uri_chars); | 164 dart_args[0] = DartUtils::NewString(uri_chars); |
| 165 return Dart_Invoke(DartUtils::BuiltinLib(), | 165 return Dart_Invoke(DartUtils::BuiltinLib(), |
| 166 DartUtils::NewString("_filePathFromUri"), kNumArgs, | 166 DartUtils::NewString("_filePathFromUri"), kNumArgs, |
| 167 dart_args); | 167 dart_args); |
| 168 } | 168 } |
| 169 | 169 |
| 170 | 170 |
| 171 static ThreadLocalKey script_reload_key = kUnsetThreadLocalKey; | 171 static ThreadLocalKey script_reload_key = kUnsetThreadLocalKey; |
| 172 | 172 |
| 173 static char* CompileTestScriptWithDFE(const char* url, |
| 174 const char* source, |
| 175 void** kernel_pgm) { |
| 176 Zone* zone = Thread::Current()->zone(); |
| 177 char* filename = OS::SCreate(zone, "file:///%s", url); |
| 178 // clang-format off |
| 179 Dart_SourceFile sourcefiles[] = { |
| 180 { |
| 181 filename, source, |
| 182 }, |
| 183 { |
| 184 "file:///.packages", "untitled:/" |
| 185 }}; |
| 186 // clang-format on |
| 187 int sourcefiles_count = sizeof(sourcefiles) / sizeof(Dart_SourceFile); |
| 188 Dart_KernelCompilationResult compilation_result = |
| 189 Dart_CompileSourcesToKernel(filename, sourcefiles_count, sourcefiles); |
| 190 |
| 191 if (compilation_result.status != Dart_KernelCompilationStatus_Ok) { |
| 192 return OS::SCreate(zone, "Compilation failed %s", compilation_result.error); |
| 193 } |
| 194 const uint8_t* kernel_file = compilation_result.kernel; |
| 195 intptr_t kernel_length = compilation_result.kernel_size; |
| 196 if (kernel_file == NULL) { |
| 197 return OS::SCreate(zone, "front end generated a NULL kernel file"); |
| 198 } |
| 199 *kernel_pgm = Dart_ReadKernelBinary(kernel_file, kernel_length); |
| 200 if (*kernel_pgm == NULL) { |
| 201 return OS::SCreate(zone, "Failed to read generated kernel binary"); |
| 202 } |
| 203 return NULL; |
| 204 } |
| 205 |
| 173 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, | 206 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 174 Dart_Handle library, | 207 Dart_Handle library, |
| 175 Dart_Handle url) { | 208 Dart_Handle url) { |
| 209 if (FLAG_use_dart_frontend) { |
| 210 // Reload request. |
| 211 ASSERT(script_reload_key != kUnsetThreadLocalKey); |
| 212 const char* script_source = reinterpret_cast<const char*>( |
| 213 OSThread::GetThreadLocal(script_reload_key)); |
| 214 ASSERT(script_source != NULL); |
| 215 OSThread::SetThreadLocal(script_reload_key, 0); |
| 216 const char* urlstr = NULL; |
| 217 Dart_Handle result = Dart_StringToCString(url, &urlstr); |
| 218 if (Dart_IsError(result)) { |
| 219 return Dart_NewApiError("accessing url characters failed"); |
| 220 } |
| 221 void* kernel_pgm; |
| 222 char* error = CompileTestScriptWithDFE(urlstr, script_source, &kernel_pgm); |
| 223 if (error == NULL) { |
| 224 return Dart_LoadKernel(kernel_pgm); |
| 225 } else { |
| 226 return Dart_NewApiError(error); |
| 227 } |
| 228 } |
| 176 if (tag == Dart_kCanonicalizeUrl) { | 229 if (tag == Dart_kCanonicalizeUrl) { |
| 177 Dart_Handle library_url = Dart_LibraryUrl(library); | 230 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 178 if (Dart_IsError(library_url)) { | 231 if (Dart_IsError(library_url)) { |
| 179 return library_url; | 232 return library_url; |
| 180 } | 233 } |
| 181 return Dart_DefaultCanonicalizeUrl(library_url, url); | 234 return Dart_DefaultCanonicalizeUrl(library_url, url); |
| 182 } | 235 } |
| 183 if (tag == Dart_kScriptTag) { | 236 if (tag == Dart_kScriptTag) { |
| 184 // Reload request. | 237 // Reload request. |
| 185 ASSERT(script_reload_key != kUnsetThreadLocalKey); | 238 ASSERT(script_reload_key != kUnsetThreadLocalKey); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 DART_CHECK_VALID(lib); | 325 DART_CHECK_VALID(lib); |
| 273 result = Dart_SetNativeResolver(lib, resolver, NULL); | 326 result = Dart_SetNativeResolver(lib, resolver, NULL); |
| 274 DART_CHECK_VALID(result); | 327 DART_CHECK_VALID(result); |
| 275 if (finalize_classes) { | 328 if (finalize_classes) { |
| 276 result = Dart_FinalizeLoading(false); | 329 result = Dart_FinalizeLoading(false); |
| 277 DART_CHECK_VALID(result); | 330 DART_CHECK_VALID(result); |
| 278 } | 331 } |
| 279 return lib; | 332 return lib; |
| 280 } | 333 } |
| 281 | 334 |
| 335 static Dart_Handle LoadTestScriptWithDFE(const char* script, |
| 336 Dart_NativeEntryResolver resolver, |
| 337 const char* lib_url, |
| 338 bool finalize_classes) { |
| 339 Dart_Handle result = Dart_SetLibraryTagHandler(LibraryTagHandler); |
| 340 EXPECT_VALID(result); |
| 341 void* kernel_pgm = NULL; |
| 342 char* error = CompileTestScriptWithDFE(lib_url, script, &kernel_pgm); |
| 343 if (error == NULL) { |
| 344 Dart_Handle lib = Dart_LoadKernel(kernel_pgm); |
| 345 DART_CHECK_VALID(lib); |
| 346 result = Dart_SetNativeResolver(lib, resolver, NULL); |
| 347 DART_CHECK_VALID(result); |
| 348 if (finalize_classes) { |
| 349 result = Dart_FinalizeLoading(false); |
| 350 DART_CHECK_VALID(result); |
| 351 } |
| 352 return lib; |
| 353 } else { |
| 354 return Dart_NewApiError(error); |
| 355 } |
| 356 } |
| 357 |
| 282 Dart_Handle TestCase::LoadTestScript(const char* script, | 358 Dart_Handle TestCase::LoadTestScript(const char* script, |
| 283 Dart_NativeEntryResolver resolver, | 359 Dart_NativeEntryResolver resolver, |
| 284 const char* lib_url, | 360 const char* lib_url, |
| 285 bool finalize_classes) { | 361 bool finalize_classes) { |
| 286 if (!FLAG_use_dart_frontend) { | 362 if (!FLAG_use_dart_frontend) { |
| 287 return LoadTestScriptWithVMParser(script, resolver, lib_url, | 363 return LoadTestScriptWithVMParser(script, resolver, lib_url, |
| 288 finalize_classes); | 364 finalize_classes); |
| 365 } else { |
| 366 return LoadTestScriptWithDFE(script, resolver, lib_url, finalize_classes); |
| 289 } | 367 } |
| 290 | |
| 291 Zone* zone = Thread::Current()->zone(); | |
| 292 char* filename = OS::SCreate(zone, "file:///%s", lib_url); | |
| 293 // clang-format off | |
| 294 Dart_SourceFile sourcefiles[] = { | |
| 295 { | |
| 296 filename, script, | |
| 297 }, | |
| 298 { | |
| 299 "file:///.packages", "untitled:/" | |
| 300 }}; | |
| 301 // clang-format on | |
| 302 | |
| 303 int sourcefiles_count = sizeof(sourcefiles) / sizeof(Dart_SourceFile); | |
| 304 Dart_KernelCompilationResult compilation_result = | |
| 305 Dart_CompileSourcesToKernel(filename, sourcefiles_count, sourcefiles); | |
| 306 | |
| 307 if (compilation_result.status != Dart_KernelCompilationStatus_Ok) { | |
| 308 return Dart_NewApiError(OS::SCreate(Thread::Current()->zone(), | |
| 309 "Compilation failed %s", | |
| 310 compilation_result.error)); | |
| 311 } | |
| 312 const uint8_t* kernel_file = compilation_result.kernel; | |
| 313 intptr_t kernel_length = compilation_result.kernel_size; | |
| 314 if (kernel_file == NULL) { | |
| 315 return Dart_NewApiError(OS::SCreate( | |
| 316 Thread::Current()->zone(), "front end generated a NULL kernel file")); | |
| 317 } | |
| 318 void* kernel_program = Dart_ReadKernelBinary(kernel_file, kernel_length); | |
| 319 if (kernel_program == NULL) { | |
| 320 return Dart_NewApiError(OS::SCreate( | |
| 321 Thread::Current()->zone(), "Failed to read generated kernel binary")); | |
| 322 } | |
| 323 return Dart_LoadKernel(kernel_program); | |
| 324 } | 368 } |
| 325 | 369 |
| 326 #ifndef PRODUCT | 370 #ifndef PRODUCT |
| 327 | 371 |
| 328 | 372 |
| 329 void TestCase::SetReloadTestScript(const char* script) { | 373 void TestCase::SetReloadTestScript(const char* script) { |
| 330 if (script_reload_key == kUnsetThreadLocalKey) { | 374 if (script_reload_key == kUnsetThreadLocalKey) { |
| 331 script_reload_key = OSThread::CreateThreadLocal(); | 375 script_reload_key = OSThread::CreateThreadLocal(); |
| 332 } | 376 } |
| 333 ASSERT(script_reload_key != kUnsetThreadLocalKey); | 377 ASSERT(script_reload_key != kUnsetThreadLocalKey); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 } | 580 } |
| 537 // Copy the remainder of in to out. | 581 // Copy the remainder of in to out. |
| 538 while (*in != '\0') { | 582 while (*in != '\0') { |
| 539 *out++ = *in++; | 583 *out++ = *in++; |
| 540 } | 584 } |
| 541 *out = '\0'; | 585 *out = '\0'; |
| 542 } | 586 } |
| 543 | 587 |
| 544 | 588 |
| 545 } // namespace dart | 589 } // namespace dart |
| OLD | NEW |