OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #ifndef BIN_LOADER_H_ |
| 6 #define BIN_LOADER_H_ |
| 7 |
| 8 #include "bin/isolate_data.h" |
| 9 #include "include/dart_api.h" |
| 10 #include "include/dart_native_api.h" |
| 11 #include "platform/assert.h" |
| 12 #include "platform/globals.h" |
| 13 #include "bin/thread.h" |
| 14 |
| 15 namespace dart { |
| 16 namespace bin { |
| 17 |
| 18 class Loader { |
| 19 public: |
| 20 explicit Loader(IsolateData* isolate_data); |
| 21 ~Loader(); |
| 22 |
| 23 // A static tag handler that hides all usage of a loader for an isolate. |
| 24 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| 25 Dart_Handle library, |
| 26 Dart_Handle url); |
| 27 |
| 28 Dart_Handle error() const { |
| 29 return error_; |
| 30 } |
| 31 |
| 32 private: |
| 33 // The port assigned to our native message handler. |
| 34 Dart_Port port_; |
| 35 // Each Loader is associated with an Isolate via its IsolateData. |
| 36 IsolateData* isolate_data_; |
| 37 // Remember the first error that occurs during loading. |
| 38 Dart_Handle error_; |
| 39 |
| 40 // This monitor is used to protect the pending operations count and the |
| 41 // I/O result queue. |
| 42 Monitor* monitor_; |
| 43 |
| 44 // The number of operations dispatched to the service isolate for loading. |
| 45 // Must be accessed with monitor_ held. |
| 46 intptr_t pending_operations_; |
| 47 |
| 48 // The result of an I/O request to the service isolate. Payload is either |
| 49 // a UInt8Array or a C string containing an error message. |
| 50 struct IOResult { |
| 51 uint8_t* payload; |
| 52 intptr_t payload_length; |
| 53 char* library_uri; |
| 54 char* uri; |
| 55 int8_t tag; |
| 56 |
| 57 void Setup(Dart_CObject* message); |
| 58 void Cleanup(); |
| 59 }; |
| 60 // An array of I/O results queued from the service isolate. |
| 61 IOResult* results_; |
| 62 intptr_t results_length_; |
| 63 intptr_t results_capacity_; |
| 64 |
| 65 intptr_t results_length() { |
| 66 return *static_cast<volatile intptr_t*>(&results_length_); |
| 67 } |
| 68 |
| 69 // Send the loader init request to the service isolate. |
| 70 void Init(const char* package_root, |
| 71 const char* packages_file, |
| 72 const char* working_directory, |
| 73 const char* root_script_uri); |
| 74 |
| 75 // Send a request from the tag handler to the service isolate. |
| 76 void SendRequest(Dart_LibraryTag tag, |
| 77 Dart_Handle url, |
| 78 Dart_Handle library_url); |
| 79 |
| 80 /// Queue |message| and notify the loader that a message is available. |
| 81 void QueueMessage(Dart_CObject* message); |
| 82 |
| 83 /// Blocks the caller until the loader is finished. |
| 84 void BlockUntilComplete(); |
| 85 |
| 86 /// Returns false if |result| is an error and the loader should quit. |
| 87 bool ProcessResultLocked(IOResult* result); |
| 88 |
| 89 /// Returns false if an error occurred and the loader should quit. |
| 90 bool ProcessQueueLocked(); |
| 91 |
| 92 // Special inner tag handler for dart: uris. |
| 93 static Dart_Handle DartColonLibraryTagHandler(Dart_LibraryTag tag, |
| 94 Dart_Handle library, |
| 95 Dart_Handle url, |
| 96 const char* library_url_string, |
| 97 const char* url_string); |
| 98 |
| 99 // We use one native message handler callback for N loaders. The native |
| 100 // message handler callback provides us with the Dart_Port which we use as a |
| 101 // key into our map of active loaders from |port| to |isolate_data|. |
| 102 |
| 103 // Static information to map Dart_Port back to the isolate in question. |
| 104 struct LoaderInfo { |
| 105 Dart_Port port; |
| 106 IsolateData* isolate_data; |
| 107 }; |
| 108 |
| 109 // The map of active loaders. |
| 110 static Mutex loader_infos_lock_; |
| 111 static LoaderInfo* loader_infos_; |
| 112 static intptr_t loader_infos_length_; |
| 113 static intptr_t loader_infos_capacity_; |
| 114 |
| 115 static void AddLoader(Dart_Port port, IsolateData* data); |
| 116 static void RemoveLoader(Dart_Port port); |
| 117 static intptr_t LoaderIndexFor(Dart_Port port); |
| 118 static Loader* LoaderFor(Dart_Port port); |
| 119 |
| 120 // This is the global callback for the native message handlers. |
| 121 static void NativeMessageHandler(Dart_Port dest_port_id, |
| 122 Dart_CObject* message); |
| 123 }; |
| 124 |
| 125 } // namespace bin |
| 126 } // namespace dart |
| 127 |
| 128 #endif // BIN_LOADER_H_ |
OLD | NEW |