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 "include/dart_native_api.h" |
5 #include "platform/assert.h" | 6 #include "platform/assert.h" |
6 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
7 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
8 #include "vm/dart.h" | 9 #include "vm/dart.h" |
9 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
10 #include "vm/dart_entry.h" | 11 #include "vm/dart_entry.h" |
11 #include "vm/exceptions.h" | 12 #include "vm/exceptions.h" |
12 #include "vm/lockers.h" | 13 #include "vm/lockers.h" |
13 #include "vm/longjump.h" | 14 #include "vm/longjump.h" |
14 #include "vm/message_handler.h" | 15 #include "vm/message_handler.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 writer.WriteMessage(obj); | 89 writer.WriteMessage(obj); |
89 | 90 |
90 // TODO(turnidge): Throw an exception when the return value is false? | 91 // TODO(turnidge): Throw an exception when the return value is false? |
91 PortMap::PostMessage(new Message(port.Id(), | 92 PortMap::PostMessage(new Message(port.Id(), |
92 data, writer.BytesWritten(), | 93 data, writer.BytesWritten(), |
93 Message::kNormalPriority)); | 94 Message::kNormalPriority)); |
94 return Object::null(); | 95 return Object::null(); |
95 } | 96 } |
96 | 97 |
97 | 98 |
98 static void ThrowIsolateSpawnException(const String& message) { | |
99 const Array& args = Array::Handle(Array::New(1)); | |
100 args.SetAt(0, message); | |
101 Exceptions::ThrowByType(Exceptions::kIsolateSpawn, args); | |
102 } | |
103 | |
104 | |
105 static bool CanonicalizeUri(Isolate* isolate, | 99 static bool CanonicalizeUri(Isolate* isolate, |
106 const Library& library, | 100 const Library& library, |
107 const String& uri, | 101 const String& uri, |
108 char** canonical_uri, | 102 char** canonical_uri, |
109 char** error) { | 103 char** error) { |
110 Zone* zone = isolate->current_zone(); | 104 Zone* zone = isolate->current_zone(); |
111 bool retval = false; | 105 bool retval = false; |
112 Dart_LibraryTagHandler handler = isolate->library_tag_handler(); | 106 Dart_LibraryTagHandler handler = isolate->library_tag_handler(); |
113 if (handler != NULL) { | 107 if (handler != NULL) { |
114 Dart_EnterScope(); | 108 Dart_EnterScope(); |
(...skipping 17 matching lines...) Expand all Loading... |
132 Dart_ExitScope(); | 126 Dart_ExitScope(); |
133 } else { | 127 } else { |
134 *error = zone->PrintToString( | 128 *error = zone->PrintToString( |
135 "Unable to canonicalize uri '%s': no library tag handler found.", | 129 "Unable to canonicalize uri '%s': no library tag handler found.", |
136 uri.ToCString()); | 130 uri.ToCString()); |
137 } | 131 } |
138 return retval; | 132 return retval; |
139 } | 133 } |
140 | 134 |
141 | 135 |
142 static bool CreateIsolate(Isolate* parent_isolate, | 136 class SpawnIsolateTask : public ThreadPool::Task { |
143 IsolateSpawnState* state, | 137 public: |
144 char** error) { | 138 explicit SpawnIsolateTask(IsolateSpawnState* state) |
145 Dart_IsolateCreateCallback callback = Isolate::CreateCallback(); | 139 : state_(state) { |
146 if (callback == NULL) { | |
147 *error = strdup("Null callback specified for isolate creation\n"); | |
148 Isolate::SetCurrent(parent_isolate); | |
149 return false; | |
150 } | 140 } |
151 | 141 |
152 void* init_data = parent_isolate->init_callback_data(); | 142 virtual void Run() { |
153 Isolate* child_isolate = reinterpret_cast<Isolate*>( | 143 // Create a new isolate. |
154 (callback)(state->script_url(), | 144 char* error = NULL; |
155 state->function_name(), | 145 Dart_IsolateCreateCallback callback = Isolate::CreateCallback(); |
156 init_data, | 146 if (callback == NULL) { |
157 error)); | 147 ReportError( |
158 if (child_isolate == NULL) { | 148 "Isolate spawn is not supported by this Dart implementation.\n"); |
159 Isolate::SetCurrent(parent_isolate); | 149 delete state_; |
160 return false; | 150 return; |
161 } | 151 } |
162 state->set_isolate(reinterpret_cast<Isolate*>(child_isolate)); | |
163 | 152 |
164 Isolate::SetCurrent(parent_isolate); | 153 Isolate* isolate = reinterpret_cast<Isolate*>( |
165 return true; | 154 (callback)(state_->script_url(), |
166 } | 155 state_->function_name(), |
| 156 state_->init_data(), |
| 157 &error)); |
| 158 if (isolate == NULL) { |
| 159 // We were unable to create the child isolate. Report the error |
| 160 // back to the parent isolate. |
| 161 ReportError(error); |
| 162 delete state_; |
| 163 return; |
| 164 } |
167 | 165 |
168 | 166 MutexLocker ml(isolate->mutex()); |
169 static RawObject* Spawn(Isolate* parent_isolate, | 167 state_->set_isolate(reinterpret_cast<Isolate*>(isolate)); |
170 IsolateSpawnState* state) { | 168 isolate->set_spawn_state(state_); |
171 // Create a new isolate. | 169 if (isolate->is_runnable()) { |
172 char* error = NULL; | 170 // Start the new isolate if it has been marked as runnable. |
173 if (!CreateIsolate(parent_isolate, state, &error)) { | 171 isolate->Run(); |
174 delete state; | 172 } |
175 const String& msg = String::Handle(String::New(error)); | |
176 free(error); | |
177 ThrowIsolateSpawnException(msg); | |
178 } | 173 } |
179 | 174 |
180 // Create a SendPort for the new isolate. | 175 private: |
181 Isolate* spawned_isolate = state->isolate(); | 176 void ReportError(const char* error) { |
182 const SendPort& port = SendPort::Handle( | 177 Dart_CObject error_cobj; |
183 SendPort::New(spawned_isolate->main_port())); | 178 error_cobj.type = Dart_CObject_kString; |
184 | 179 error_cobj.value.as_string = const_cast<char*>(error); |
185 // Start the new isolate if it is already marked as runnable. | 180 if (!Dart_PostCObject(state_->parent_port(), &error_cobj)) { |
186 MutexLocker ml(spawned_isolate->mutex()); | 181 // Perhaps the parent isolate died or closed the port before we |
187 spawned_isolate->set_spawn_state(state); | 182 // could report the error. Ignore. |
188 if (spawned_isolate->is_runnable()) { | 183 } |
189 spawned_isolate->Run(); | |
190 } | 184 } |
191 | 185 |
192 return port.raw(); | 186 IsolateSpawnState* state_; |
193 } | 187 |
| 188 DISALLOW_COPY_AND_ASSIGN(SpawnIsolateTask); |
| 189 }; |
194 | 190 |
195 | 191 |
196 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 3) { | 192 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 3) { |
197 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 193 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
198 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(1)); | 194 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(1)); |
199 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); | 195 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); |
200 if (closure.IsClosure()) { | 196 if (closure.IsClosure()) { |
201 Function& func = Function::Handle(); | 197 Function& func = Function::Handle(); |
202 func = Closure::function(closure); | 198 func = Closure::function(closure); |
203 if (func.IsImplicitClosureFunction() && func.is_static()) { | 199 if (func.IsImplicitClosureFunction() && func.is_static()) { |
204 #if defined(DEBUG) | 200 #if defined(DEBUG) |
205 Context& ctx = Context::Handle(); | 201 Context& ctx = Context::Handle(); |
206 ctx = Closure::context(closure); | 202 ctx = Closure::context(closure); |
207 ASSERT(ctx.num_variables() == 0); | 203 ASSERT(ctx.num_variables() == 0); |
208 #endif | 204 #endif |
209 return Spawn(isolate, new IsolateSpawnState(port.Id(), func, message)); | 205 Dart::thread_pool()->Run(new SpawnIsolateTask( |
| 206 new IsolateSpawnState(port.Id(), |
| 207 isolate->init_callback_data(), |
| 208 func, message))); |
| 209 return Object::null(); |
210 } | 210 } |
211 } | 211 } |
212 const String& msg = String::Handle(String::New( | 212 const String& msg = String::Handle(String::New( |
213 "Isolate.spawn expects to be passed a static or top-level function")); | 213 "Isolate.spawn expects to be passed a static or top-level function")); |
214 Exceptions::ThrowArgumentError(msg); | 214 Exceptions::ThrowArgumentError(msg); |
215 return Object::null(); | 215 return Object::null(); |
216 } | 216 } |
217 | 217 |
218 | 218 |
219 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) { | 219 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) { |
220 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 220 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
221 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); | 221 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); |
222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); | 222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); |
223 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); | 223 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); |
224 | 224 |
225 // Canonicalize the uri with respect to the current isolate. | 225 // Canonicalize the uri with respect to the current isolate. |
226 char* error = NULL; | 226 char* error = NULL; |
227 char* canonical_uri = NULL; | 227 char* canonical_uri = NULL; |
228 const Library& root_lib = | 228 const Library& root_lib = |
229 Library::Handle(arguments->isolate()->object_store()->root_library()); | 229 Library::Handle(arguments->isolate()->object_store()->root_library()); |
230 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, | 230 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, |
231 &canonical_uri, &error)) { | 231 &canonical_uri, &error)) { |
232 const String& msg = String::Handle(String::New(error)); | 232 const String& message = String::Handle(String::New(error)); |
233 ThrowIsolateSpawnException(msg); | 233 const Array& args = Array::Handle(Array::New(1)); |
| 234 args.SetAt(0, message); |
| 235 Exceptions::ThrowByType(Exceptions::kIsolateSpawn, args); |
234 } | 236 } |
235 | 237 |
236 return Spawn(isolate, new IsolateSpawnState(port.Id(), canonical_uri, | 238 Dart::thread_pool()->Run(new SpawnIsolateTask( |
237 args, message)); | 239 new IsolateSpawnState(port.Id(), |
| 240 isolate->init_callback_data(), |
| 241 canonical_uri, |
| 242 args, message))); |
| 243 return Object::null(); |
238 } | 244 } |
239 | 245 |
240 | 246 |
241 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { | 247 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { |
242 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 248 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
243 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); | 249 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); |
244 | 250 |
245 // Make sure to route this request to the isolate library OOB mesage handler. | 251 // Make sure to route this request to the isolate library OOB mesage handler. |
246 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); | 252 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); |
247 | 253 |
248 uint8_t* data = NULL; | 254 uint8_t* data = NULL; |
249 MessageWriter writer(&data, &allocator); | 255 MessageWriter writer(&data, &allocator); |
250 writer.WriteMessage(msg); | 256 writer.WriteMessage(msg); |
251 | 257 |
252 PortMap::PostMessage(new Message(port.Id(), | 258 PortMap::PostMessage(new Message(port.Id(), |
253 data, writer.BytesWritten(), | 259 data, writer.BytesWritten(), |
254 Message::kOOBPriority)); | 260 Message::kOOBPriority)); |
255 return Object::null(); | 261 return Object::null(); |
256 } | 262 } |
257 | 263 |
258 } // namespace dart | 264 } // namespace dart |
OLD | NEW |