Chromium Code Reviews| 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("Isolate spawn is not supported in this Dart embedding.\n"); |
|
Cutch
2014/08/27 18:18:42
What about: "Isolate spawn is not supported by thi
turnidge
2014/08/27 19:28:59
Changed to "by this Dart implemation"
| |
| 158 if (child_isolate == NULL) { | 148 delete state_; |
| 159 Isolate::SetCurrent(parent_isolate); | 149 return; |
| 160 return false; | 150 } |
| 161 } | |
| 162 state->set_isolate(reinterpret_cast<Isolate*>(child_isolate)); | |
| 163 | 151 |
| 164 Isolate::SetCurrent(parent_isolate); | 152 Isolate* isolate = reinterpret_cast<Isolate*>( |
| 165 return true; | 153 (callback)(state_->script_url(), |
| 166 } | 154 state_->function_name(), |
| 155 state_->init_data(), | |
| 156 &error)); | |
| 157 if (isolate == NULL) { | |
| 158 // We were unable to create the child isolate. Report the error | |
| 159 // back to the parent isolate. | |
| 160 ReportError(error); | |
| 161 delete state_; | |
| 162 return; | |
| 163 } | |
| 167 | 164 |
| 168 | 165 MutexLocker ml(isolate->mutex()); |
| 169 static RawObject* Spawn(Isolate* parent_isolate, | 166 state_->set_isolate(reinterpret_cast<Isolate*>(isolate)); |
| 170 IsolateSpawnState* state) { | 167 isolate->set_spawn_state(state_); |
| 171 // Create a new isolate. | 168 if (isolate->is_runnable()) { |
| 172 char* error = NULL; | 169 // Start the new isolate if it has been marked as runnable. |
| 173 if (!CreateIsolate(parent_isolate, state, &error)) { | 170 isolate->Run(); |
| 174 delete state; | 171 } |
| 175 const String& msg = String::Handle(String::New(error)); | |
| 176 free(error); | |
| 177 ThrowIsolateSpawnException(msg); | |
| 178 } | 172 } |
| 179 | 173 |
| 180 // Create a SendPort for the new isolate. | 174 private: |
| 181 Isolate* spawned_isolate = state->isolate(); | 175 void ReportError(const char* error) { |
| 182 const SendPort& port = SendPort::Handle( | 176 Dart_CObject error_cobj; |
| 183 SendPort::New(spawned_isolate->main_port())); | 177 error_cobj.type = Dart_CObject_kString; |
| 184 | 178 error_cobj.value.as_string = const_cast<char*>(error); |
| 185 // Start the new isolate if it is already marked as runnable. | 179 if (!Dart_PostCObject(state_->parent_port(), &error_cobj)) { |
| 186 MutexLocker ml(spawned_isolate->mutex()); | 180 // Perhaps the parent isolate died or closed the port before we |
| 187 spawned_isolate->set_spawn_state(state); | 181 // could report the error. Ignore. |
| 188 if (spawned_isolate->is_runnable()) { | 182 } |
| 189 spawned_isolate->Run(); | |
| 190 } | 183 } |
| 191 | 184 |
| 192 return port.raw(); | 185 IsolateSpawnState* state_; |
| 193 } | 186 |
| 187 DISALLOW_COPY_AND_ASSIGN(SpawnIsolateTask); | |
| 188 }; | |
| 194 | 189 |
| 195 | 190 |
| 196 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 3) { | 191 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 3) { |
| 197 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 192 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
| 198 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(1)); | 193 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(1)); |
| 199 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); | 194 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); |
| 200 if (closure.IsClosure()) { | 195 if (closure.IsClosure()) { |
| 201 Function& func = Function::Handle(); | 196 Function& func = Function::Handle(); |
| 202 func = Closure::function(closure); | 197 func = Closure::function(closure); |
| 203 if (func.IsImplicitClosureFunction() && func.is_static()) { | 198 if (func.IsImplicitClosureFunction() && func.is_static()) { |
| 204 #if defined(DEBUG) | 199 #if defined(DEBUG) |
| 205 Context& ctx = Context::Handle(); | 200 Context& ctx = Context::Handle(); |
| 206 ctx = Closure::context(closure); | 201 ctx = Closure::context(closure); |
| 207 ASSERT(ctx.num_variables() == 0); | 202 ASSERT(ctx.num_variables() == 0); |
| 208 #endif | 203 #endif |
| 209 return Spawn(isolate, new IsolateSpawnState(port.Id(), func, message)); | 204 Dart::thread_pool()->Run(new SpawnIsolateTask( |
| 205 new IsolateSpawnState(port.Id(), | |
| 206 isolate->init_callback_data(), | |
| 207 func, message))); | |
| 208 return Object::null(); | |
| 210 } | 209 } |
| 211 } | 210 } |
| 212 const String& msg = String::Handle(String::New( | 211 const String& msg = String::Handle(String::New( |
| 213 "Isolate.spawn expects to be passed a static or top-level function")); | 212 "Isolate.spawn expects to be passed a static or top-level function")); |
| 214 Exceptions::ThrowArgumentError(msg); | 213 Exceptions::ThrowArgumentError(msg); |
| 215 return Object::null(); | 214 return Object::null(); |
| 216 } | 215 } |
| 217 | 216 |
| 218 | 217 |
| 219 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) { | 218 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) { |
| 220 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 219 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
| 221 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); | 220 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); |
| 222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); | 221 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); |
| 223 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); | 222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); |
| 224 | 223 |
| 225 // Canonicalize the uri with respect to the current isolate. | 224 // Canonicalize the uri with respect to the current isolate. |
| 226 char* error = NULL; | 225 char* error = NULL; |
| 227 char* canonical_uri = NULL; | 226 char* canonical_uri = NULL; |
| 228 const Library& root_lib = | 227 const Library& root_lib = |
| 229 Library::Handle(arguments->isolate()->object_store()->root_library()); | 228 Library::Handle(arguments->isolate()->object_store()->root_library()); |
| 230 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, | 229 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, |
| 231 &canonical_uri, &error)) { | 230 &canonical_uri, &error)) { |
| 232 const String& msg = String::Handle(String::New(error)); | 231 const String& message = String::Handle(String::New(error)); |
| 233 ThrowIsolateSpawnException(msg); | 232 const Array& args = Array::Handle(Array::New(1)); |
| 233 args.SetAt(0, message); | |
| 234 Exceptions::ThrowByType(Exceptions::kIsolateSpawn, args); | |
| 234 } | 235 } |
| 235 | 236 |
| 236 return Spawn(isolate, new IsolateSpawnState(port.Id(), canonical_uri, | 237 Dart::thread_pool()->Run(new SpawnIsolateTask( |
| 237 args, message)); | 238 new IsolateSpawnState(port.Id(), |
| 239 isolate->init_callback_data(), | |
| 240 canonical_uri, | |
| 241 args, message))); | |
| 242 return Object::null(); | |
| 238 } | 243 } |
| 239 | 244 |
| 240 | 245 |
| 241 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { | 246 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { |
| 242 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 247 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
| 243 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); | 248 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); |
| 244 | 249 |
| 245 // Make sure to route this request to the isolate library OOB mesage handler. | 250 // Make sure to route this request to the isolate library OOB mesage handler. |
| 246 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); | 251 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); |
| 247 | 252 |
| 248 uint8_t* data = NULL; | 253 uint8_t* data = NULL; |
| 249 MessageWriter writer(&data, &allocator); | 254 MessageWriter writer(&data, &allocator); |
| 250 writer.WriteMessage(msg); | 255 writer.WriteMessage(msg); |
| 251 | 256 |
| 252 PortMap::PostMessage(new Message(port.Id(), | 257 PortMap::PostMessage(new Message(port.Id(), |
| 253 data, writer.BytesWritten(), | 258 data, writer.BytesWritten(), |
| 254 Message::kOOBPriority)); | 259 Message::kOOBPriority)); |
| 255 return Object::null(); | 260 return Object::null(); |
| 256 } | 261 } |
| 257 | 262 |
| 258 } // namespace dart | 263 } // namespace dart |
| OLD | NEW |