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