| OLD | NEW | 
|     1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file |     1 // Copyright (c) 2013, 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 "include/dart_api.h" |     5 #include "include/dart_api.h" | 
|     6 #include "include/dart_mirrors_api.h" |     6 #include "include/dart_mirrors_api.h" | 
|     7 #include "include/dart_native_api.h" |     7 #include "include/dart_native_api.h" | 
|     8  |     8  | 
|     9 #include "platform/assert.h" |     9 #include "platform/assert.h" | 
|    10 #include "lib/stacktrace.h" |    10 #include "lib/stacktrace.h" | 
| (...skipping 1222 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1233   chars = reinterpret_cast<char*>(malloc(len)); |  1233   chars = reinterpret_cast<char*>(malloc(len)); | 
|  1234   OS::SNPrint(chars, len, "%s$%s", script_uri, main); |  1234   OS::SNPrint(chars, len, "%s$%s", script_uri, main); | 
|  1235   return chars; |  1235   return chars; | 
|  1236 } |  1236 } | 
|  1237  |  1237  | 
|  1238  |  1238  | 
|  1239 static Dart_Isolate CreateIsolate(const char* script_uri, |  1239 static Dart_Isolate CreateIsolate(const char* script_uri, | 
|  1240                                   const char* main, |  1240                                   const char* main, | 
|  1241                                   const uint8_t* snapshot_buffer, |  1241                                   const uint8_t* snapshot_buffer, | 
|  1242                                   intptr_t snapshot_length, |  1242                                   intptr_t snapshot_length, | 
|  1243                                   bool from_kernel, |  1243                                   kernel::Program* kernel_program, | 
|  1244                                   Dart_IsolateFlags* flags, |  1244                                   Dart_IsolateFlags* flags, | 
|  1245                                   void* callback_data, |  1245                                   void* callback_data, | 
|  1246                                   char** error) { |  1246                                   char** error) { | 
|  1247   ASSERT(!from_kernel || (snapshot_buffer != NULL)); |  | 
|  1248   CHECK_NO_ISOLATE(Isolate::Current()); |  1247   CHECK_NO_ISOLATE(Isolate::Current()); | 
|  1249   char* isolate_name = BuildIsolateName(script_uri, main); |  1248   char* isolate_name = BuildIsolateName(script_uri, main); | 
|  1250  |  1249  | 
|  1251   // Setup default flags in case none were passed. |  1250   // Setup default flags in case none were passed. | 
|  1252   Dart_IsolateFlags api_flags; |  1251   Dart_IsolateFlags api_flags; | 
|  1253   if (flags == NULL) { |  1252   if (flags == NULL) { | 
|  1254     Isolate::FlagsInitialize(&api_flags); |  1253     Isolate::FlagsInitialize(&api_flags); | 
|  1255     flags = &api_flags; |  1254     flags = &api_flags; | 
|  1256   } |  1255   } | 
|  1257   Isolate* I = Dart::CreateIsolate(isolate_name, *flags); |  1256   Isolate* I = Dart::CreateIsolate(isolate_name, *flags); | 
|  1258   free(isolate_name); |  1257   free(isolate_name); | 
|  1259   if (I == NULL) { |  1258   if (I == NULL) { | 
|  1260     *error = strdup("Isolate creation failed"); |  1259     *error = strdup("Isolate creation failed"); | 
|  1261     return reinterpret_cast<Dart_Isolate>(NULL); |  1260     return reinterpret_cast<Dart_Isolate>(NULL); | 
|  1262   } |  1261   } | 
|  1263   { |  1262   { | 
|  1264     Thread* T = Thread::Current(); |  1263     Thread* T = Thread::Current(); | 
|  1265     StackZone zone(T); |  1264     StackZone zone(T); | 
|  1266     HANDLESCOPE(T); |  1265     HANDLESCOPE(T); | 
|  1267     // We enter an API scope here as InitializeIsolate could compile some |  1266     // We enter an API scope here as InitializeIsolate could compile some | 
|  1268     // bootstrap library files which call out to a tag handler that may create |  1267     // bootstrap library files which call out to a tag handler that may create | 
|  1269     // Api Handles when an error is encountered. |  1268     // Api Handles when an error is encountered. | 
|  1270     Dart_EnterScope(); |  1269     Dart_EnterScope(); | 
|  1271     const Error& error_obj = Error::Handle( |  1270     const Error& error_obj = Error::Handle( | 
|  1272         Z, Dart::InitializeIsolate(snapshot_buffer, snapshot_length, |  1271         Z, Dart::InitializeIsolate(snapshot_buffer, snapshot_length, | 
|  1273                                    from_kernel, callback_data)); |  1272                                    kernel_program, callback_data)); | 
|  1274     if (error_obj.IsNull()) { |  1273     if (error_obj.IsNull()) { | 
|  1275 #if defined(DART_NO_SNAPSHOT) && !defined(PRODUCT) |  1274 #if defined(DART_NO_SNAPSHOT) && !defined(PRODUCT) | 
|  1276       if (FLAG_check_function_fingerprints && !from_kernel) { |  1275       if (FLAG_check_function_fingerprints && kernel_program == NULL) { | 
|  1277         Library::CheckFunctionFingerprints(); |  1276         Library::CheckFunctionFingerprints(); | 
|  1278       } |  1277       } | 
|  1279 #endif  // defined(DART_NO_SNAPSHOT) && !defined(PRODUCT). |  1278 #endif  // defined(DART_NO_SNAPSHOT) && !defined(PRODUCT). | 
|  1280       // We exit the API scope entered above. |  1279       // We exit the API scope entered above. | 
|  1281       Dart_ExitScope(); |  1280       Dart_ExitScope(); | 
|  1282       // A Thread structure has been associated to the thread, we do the |  1281       // A Thread structure has been associated to the thread, we do the | 
|  1283       // safepoint transition explicity here instead of using the |  1282       // safepoint transition explicity here instead of using the | 
|  1284       // TransitionXXX scope objects as the reverse transition happens |  1283       // TransitionXXX scope objects as the reverse transition happens | 
|  1285       // outside this scope in Dart_ShutdownIsolate/Dart_ExitIsolate. |  1284       // outside this scope in Dart_ShutdownIsolate/Dart_ExitIsolate. | 
|  1286       T->set_execution_state(Thread::kThreadInNative); |  1285       T->set_execution_state(Thread::kThreadInNative); | 
|  1287       T->EnterSafepoint(); |  1286       T->EnterSafepoint(); | 
|  1288       return Api::CastIsolate(I); |  1287       return Api::CastIsolate(I); | 
|  1289     } |  1288     } | 
|  1290     *error = strdup(error_obj.ToErrorCString()); |  1289     *error = strdup(error_obj.ToErrorCString()); | 
|  1291     // We exit the API scope entered above. |  1290     // We exit the API scope entered above. | 
|  1292     Dart_ExitScope(); |  1291     Dart_ExitScope(); | 
|  1293   } |  1292   } | 
|  1294   Dart::ShutdownIsolate(); |  1293   Dart::ShutdownIsolate(); | 
|  1295   return reinterpret_cast<Dart_Isolate>(NULL); |  1294   return reinterpret_cast<Dart_Isolate>(NULL); | 
|  1296 } |  1295 } | 
|  1297  |  1296  | 
|  1298  |  1297  | 
|  1299 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* script_uri, |  1298 DART_EXPORT Dart_Isolate Dart_CreateIsolate(const char* script_uri, | 
|  1300                                             const char* main, |  1299                                             const char* main, | 
|  1301                                             const uint8_t* snapshot_buffer, |  1300                                             const uint8_t* snapshot_buffer, | 
|  1302                                             Dart_IsolateFlags* flags, |  1301                                             Dart_IsolateFlags* flags, | 
|  1303                                             void* callback_data, |  1302                                             void* callback_data, | 
|  1304                                             char** error) { |  1303                                             char** error) { | 
|  1305   return CreateIsolate(script_uri, main, snapshot_buffer, -1, false, flags, |  1304   return CreateIsolate(script_uri, main, snapshot_buffer, -1, NULL, flags, | 
|  1306                        callback_data, error); |  1305                        callback_data, error); | 
|  1307 } |  1306 } | 
|  1308  |  1307  | 
|  1309  |  1308  | 
|  1310 DART_EXPORT Dart_Isolate |  1309 DART_EXPORT Dart_Isolate Dart_CreateIsolateFromKernel(const char* script_uri, | 
|  1311 Dart_CreateIsolateFromKernel(const char* script_uri, |  1310                                                       const char* main, | 
|  1312                              const char* main, |  1311                                                       void* kernel_program, | 
|  1313                              const uint8_t* kernel_file, |  1312                                                       Dart_IsolateFlags* flags, | 
|  1314                              intptr_t kernel_length, |  1313                                                       void* callback_data, | 
|  1315                              Dart_IsolateFlags* flags, |  1314                                                       char** error) { | 
|  1316                              void* callback_data, |  1315   return CreateIsolate(script_uri, main, NULL, -1, | 
|  1317                              char** error) { |  1316                        reinterpret_cast<kernel::Program*>(kernel_program), | 
|  1318   return CreateIsolate(script_uri, main, kernel_file, kernel_length, true, |  | 
|  1319                        flags, callback_data, error); |  1317                        flags, callback_data, error); | 
|  1320 } |  1318 } | 
|  1321  |  1319  | 
|  1322  |  1320  | 
|  1323 DART_EXPORT void Dart_ShutdownIsolate() { |  1321 DART_EXPORT void Dart_ShutdownIsolate() { | 
|  1324   Thread* T = Thread::Current(); |  1322   Thread* T = Thread::Current(); | 
|  1325   Isolate* I = T->isolate(); |  1323   Isolate* I = T->isolate(); | 
|  1326   CHECK_ISOLATE(I); |  1324   CHECK_ISOLATE(I); | 
|  1327   I->WaitForOutstandingSpawns(); |  1325   I->WaitForOutstandingSpawns(); | 
|  1328   { |  1326   { | 
| (...skipping 4014 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  5343                         I->heap()->UsedInWords(Heap::kOld) * kWordSize); |  5341                         I->heap()->UsedInWords(Heap::kOld) * kWordSize); | 
|  5344   } |  5342   } | 
|  5345 #endif  // !defined(PRODUCT) |  5343 #endif  // !defined(PRODUCT) | 
|  5346   library ^= tmp.raw(); |  5344   library ^= tmp.raw(); | 
|  5347   library.set_debuggable(true); |  5345   library.set_debuggable(true); | 
|  5348   I->object_store()->set_root_library(library); |  5346   I->object_store()->set_root_library(library); | 
|  5349   return Api::NewHandle(T, library.raw()); |  5347   return Api::NewHandle(T, library.raw()); | 
|  5350 } |  5348 } | 
|  5351  |  5349  | 
|  5352  |  5350  | 
|  5353 DART_EXPORT Dart_Handle Dart_LoadKernel(const uint8_t* buffer, |  5351 DART_EXPORT void* Dart_ReadKernelBinary(const uint8_t* buffer, | 
|  5354                                         intptr_t buffer_len) { |  5352                                         intptr_t buffer_len) { | 
|  5355   API_TIMELINE_DURATION; |  5353   API_TIMELINE_DURATION; | 
 |  5354  | 
 |  5355 #if defined(DART_PRECOMPILED_RUNTIME) | 
 |  5356   UNREACHABLE(); | 
 |  5357   return NULL; | 
 |  5358 #else | 
 |  5359   kernel::Program* program = | 
 |  5360       ReadPrecompiledKernelFromBuffer(buffer, buffer_len); | 
 |  5361   return program; | 
 |  5362 #endif | 
 |  5363 } | 
 |  5364  | 
 |  5365  | 
 |  5366 DART_EXPORT Dart_Handle Dart_LoadKernel(void* kernel_program) { | 
 |  5367   API_TIMELINE_DURATION; | 
|  5356   DARTSCOPE(Thread::Current()); |  5368   DARTSCOPE(Thread::Current()); | 
|  5357   StackZone zone(T); |  5369   StackZone zone(T); | 
|  5358  |  5370  | 
|  5359 #if defined(DART_PRECOMPILED_RUNTIME) |  5371 #if defined(DART_PRECOMPILED_RUNTIME) | 
|  5360   return Api::NewError("%s: Can't load Kernel files from precompiled runtime.", |  5372   return Api::NewError("%s: Can't load Kernel files from precompiled runtime.", | 
|  5361                        CURRENT_FUNC); |  5373                        CURRENT_FUNC); | 
|  5362 #else |  5374 #else | 
|  5363   Isolate* I = T->isolate(); |  5375   Isolate* I = T->isolate(); | 
|  5364  |  5376  | 
|  5365   Library& library = Library::Handle(Z, I->object_store()->root_library()); |  5377   Library& library = Library::Handle(Z, I->object_store()->root_library()); | 
|  5366   if (!library.IsNull()) { |  5378   if (!library.IsNull()) { | 
|  5367     const String& library_url = String::Handle(Z, library.url()); |  5379     const String& library_url = String::Handle(Z, library.url()); | 
|  5368     return Api::NewError("%s: A script has already been loaded from '%s'.", |  5380     return Api::NewError("%s: A script has already been loaded from '%s'.", | 
|  5369                          CURRENT_FUNC, library_url.ToCString()); |  5381                          CURRENT_FUNC, library_url.ToCString()); | 
|  5370   } |  5382   } | 
|  5371   CHECK_CALLBACK_STATE(T); |  5383   CHECK_CALLBACK_STATE(T); | 
|  5372   CHECK_COMPILATION_ALLOWED(I); |  5384   CHECK_COMPILATION_ALLOWED(I); | 
|  5373  |  5385  | 
|  5374   // TODO(27588): Memory leak! |  5386   // NOTE: Now the VM owns the [kernel_program] memory!  Currently we do not | 
|  5375   kernel::KernelReader* reader = new kernel::KernelReader(buffer, buffer_len); |  5387   // free it because (similar to the token stream) it will be used to repeatedly | 
|  5376   const Object& tmp = reader->ReadProgram(); |  5388   // run the `kernel::FlowGraphBuilder()`. | 
 |  5389   kernel::KernelReader reader( | 
 |  5390       reinterpret_cast<kernel::Program*>(kernel_program)); | 
 |  5391   const Object& tmp = reader.ReadProgram(); | 
|  5377   if (tmp.IsError()) { |  5392   if (tmp.IsError()) { | 
|  5378     return Api::NewHandle(T, tmp.raw()); |  5393     return Api::NewHandle(T, tmp.raw()); | 
|  5379   } |  5394   } | 
|  5380   library ^= tmp.raw(); |  5395   library ^= tmp.raw(); | 
|  5381   library.set_debuggable(false); |  5396   library.set_debuggable(false); | 
|  5382   I->object_store()->set_root_library(library); |  5397   I->object_store()->set_root_library(library); | 
|  5383   return Api::NewHandle(T, library.raw()); |  5398   return Api::NewHandle(T, library.raw()); | 
|  5384 #endif |  5399 #endif | 
|  5385 } |  5400 } | 
|  5386  |  5401  | 
| (...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  6650  |  6665  | 
|  6651 DART_EXPORT bool Dart_IsPrecompiledRuntime() { |  6666 DART_EXPORT bool Dart_IsPrecompiledRuntime() { | 
|  6652 #if defined(DART_PRECOMPILED_RUNTIME) |  6667 #if defined(DART_PRECOMPILED_RUNTIME) | 
|  6653   return true; |  6668   return true; | 
|  6654 #else |  6669 #else | 
|  6655   return false; |  6670   return false; | 
|  6656 #endif |  6671 #endif | 
|  6657 } |  6672 } | 
|  6658  |  6673  | 
|  6659 }  // namespace dart |  6674 }  // namespace dart | 
| OLD | NEW |