| 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 "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/bootstrap_natives.h" | 6 #include "vm/bootstrap_natives.h" |
| 7 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
| 8 #include "vm/dart.h" | 8 #include "vm/dart.h" |
| 9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
| 10 #include "vm/dart_entry.h" | 10 #include "vm/dart_entry.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); | 31 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); |
| 32 uint64_t id = isolate->random()->NextUInt64(); | 32 uint64_t id = isolate->random()->NextUInt64(); |
| 33 return Capability::New(id); | 33 return Capability::New(id); |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_factory, 1) { | 37 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_factory, 1) { |
| 38 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); | 38 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); |
| 39 Dart_Port port_id = | 39 Dart_Port port_id = |
| 40 PortMap::CreatePort(arguments->isolate()->message_handler()); | 40 PortMap::CreatePort(arguments->isolate()->message_handler()); |
| 41 return ReceivePort::New(port_id); | 41 return ReceivePort::New(port_id, false /* not control port */); |
| 42 } | 42 } |
| 43 | 43 |
| 44 | 44 |
| 45 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_id, 1) { | 45 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_id, 1) { |
| 46 GET_NON_NULL_NATIVE_ARGUMENT(ReceivePort, port, arguments->NativeArgAt(0)); | 46 GET_NON_NULL_NATIVE_ARGUMENT(ReceivePort, port, arguments->NativeArgAt(0)); |
| 47 return Integer::NewFromUint64(port.Id()); | 47 return Integer::NewFromUint64(port.Id()); |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_sendport, 1) { | 51 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_sendport, 1) { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 static RawObject* Spawn(NativeArguments* arguments, IsolateSpawnState* state) { | 169 static RawObject* Spawn(NativeArguments* arguments, IsolateSpawnState* state) { |
| 170 // Create a new isolate. | 170 // Create a new isolate. |
| 171 char* error = NULL; | 171 char* error = NULL; |
| 172 if (!CreateIsolate(state, &error)) { | 172 if (!CreateIsolate(state, &error)) { |
| 173 delete state; | 173 delete state; |
| 174 const String& msg = String::Handle(String::New(error)); | 174 const String& msg = String::Handle(String::New(error)); |
| 175 free(error); | 175 free(error); |
| 176 ThrowIsolateSpawnException(msg); | 176 ThrowIsolateSpawnException(msg); |
| 177 } | 177 } |
| 178 | 178 |
| 179 // The result of spawning an Isolate is an array with 3 elements: | |
| 180 // [main_port, pause_capability, terminate_capability] | |
| 181 const Array& result = Array::Handle(Array::New(3)); | |
| 182 | |
| 183 // Create a SendPort for the new isolate. | 179 // Create a SendPort for the new isolate. |
| 184 Isolate* spawned_isolate = state->isolate(); | 180 Isolate* spawned_isolate = state->isolate(); |
| 185 const SendPort& port = SendPort::Handle( | 181 const SendPort& port = SendPort::Handle( |
| 186 SendPort::New(spawned_isolate->main_port())); | 182 SendPort::New(spawned_isolate->main_port())); |
| 187 result.SetAt(0, port); | |
| 188 Capability& capability = Capability::Handle(); | |
| 189 capability = Capability::New(spawned_isolate->pause_capability()); | |
| 190 result.SetAt(1, capability); // pauseCapability | |
| 191 capability = Capability::New(spawned_isolate->terminate_capability()); | |
| 192 result.SetAt(2, capability); // terminateCapability | |
| 193 | 183 |
| 194 // Start the new isolate if it is already marked as runnable. | 184 // Start the new isolate if it is already marked as runnable. |
| 195 MutexLocker ml(spawned_isolate->mutex()); | 185 MutexLocker ml(spawned_isolate->mutex()); |
| 196 spawned_isolate->set_spawn_state(state); | 186 spawned_isolate->set_spawn_state(state); |
| 197 if (spawned_isolate->is_runnable()) { | 187 if (spawned_isolate->is_runnable()) { |
| 198 spawned_isolate->Run(); | 188 spawned_isolate->Run(); |
| 199 } | 189 } |
| 200 | 190 |
| 201 return result.raw(); | 191 return port.raw(); |
| 202 } | 192 } |
| 203 | 193 |
| 204 | 194 |
| 205 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 1) { | 195 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 3) { |
| 206 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); | 196 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
| 197 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(1)); |
| 198 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2)); |
| 207 if (closure.IsClosure()) { | 199 if (closure.IsClosure()) { |
| 208 Function& func = Function::Handle(); | 200 Function& func = Function::Handle(); |
| 209 func = Closure::function(closure); | 201 func = Closure::function(closure); |
| 210 if (func.IsImplicitClosureFunction() && func.is_static()) { | 202 if (func.IsImplicitClosureFunction() && func.is_static()) { |
| 211 #if defined(DEBUG) | 203 #if defined(DEBUG) |
| 212 Context& ctx = Context::Handle(); | 204 Context& ctx = Context::Handle(); |
| 213 ctx = Closure::context(closure); | 205 ctx = Closure::context(closure); |
| 214 ASSERT(ctx.num_variables() == 0); | 206 ASSERT(ctx.num_variables() == 0); |
| 215 #endif | 207 #endif |
| 216 return Spawn(arguments, new IsolateSpawnState(func)); | 208 return Spawn(arguments, new IsolateSpawnState(port.Id(), func, message)); |
| 217 } | 209 } |
| 218 } | 210 } |
| 219 const String& msg = String::Handle(String::New( | 211 const String& msg = String::Handle(String::New( |
| 220 "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")); |
| 221 Exceptions::ThrowArgumentError(msg); | 213 Exceptions::ThrowArgumentError(msg); |
| 222 return Object::null(); | 214 return Object::null(); |
| 223 } | 215 } |
| 224 | 216 |
| 225 | 217 |
| 226 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 1) { | 218 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) { |
| 227 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(0)); | 219 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
| 220 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1)); |
| 221 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2)); |
| 222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3)); |
| 228 | 223 |
| 229 // Canonicalize the uri with respect to the current isolate. | 224 // Canonicalize the uri with respect to the current isolate. |
| 230 char* error = NULL; | 225 char* error = NULL; |
| 231 char* canonical_uri = NULL; | 226 char* canonical_uri = NULL; |
| 232 const Library& root_lib = | 227 const Library& root_lib = |
| 233 Library::Handle(arguments->isolate()->object_store()->root_library()); | 228 Library::Handle(arguments->isolate()->object_store()->root_library()); |
| 234 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, | 229 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, |
| 235 &canonical_uri, &error)) { | 230 &canonical_uri, &error)) { |
| 236 const String& msg = String::Handle(String::New(error)); | 231 const String& msg = String::Handle(String::New(error)); |
| 237 ThrowIsolateSpawnException(msg); | 232 ThrowIsolateSpawnException(msg); |
| 238 } | 233 } |
| 239 | 234 |
| 240 return Spawn(arguments, new IsolateSpawnState(canonical_uri)); | 235 return Spawn(arguments, new IsolateSpawnState(port.Id(), canonical_uri, |
| 236 args, message)); |
| 241 } | 237 } |
| 242 | 238 |
| 243 | 239 |
| 244 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { | 240 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { |
| 245 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); | 241 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); |
| 246 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); | 242 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); |
| 247 | 243 |
| 248 // Make sure to route this request to the isolate library OOB mesage handler. | 244 // Make sure to route this request to the isolate library OOB mesage handler. |
| 249 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); | 245 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); |
| 250 | 246 |
| 251 uint8_t* data = NULL; | 247 uint8_t* data = NULL; |
| 252 MessageWriter writer(&data, &allocator); | 248 MessageWriter writer(&data, &allocator); |
| 253 writer.WriteMessage(msg); | 249 writer.WriteMessage(msg); |
| 254 | 250 |
| 255 PortMap::PostMessage(new Message(port.Id(), | 251 PortMap::PostMessage(new Message(port.Id(), |
| 256 data, writer.BytesWritten(), | 252 data, writer.BytesWritten(), |
| 257 Message::kOOBPriority)); | 253 Message::kOOBPriority)); |
| 258 return Object::null(); | 254 return Object::null(); |
| 259 } | 255 } |
| 260 | 256 |
| 261 | |
| 262 DEFINE_NATIVE_ENTRY(Isolate_mainPort, 0) { | |
| 263 // The control port is being accessed as a regular port from Dart code. This | |
| 264 // is most likely due to the _startIsolate code in dart:isolate. Account for | |
| 265 // this by increasing the number of open control ports. | |
| 266 isolate->message_handler()->increment_control_ports(); | |
| 267 | |
| 268 return ReceivePort::New(isolate->main_port()); | |
| 269 } | |
| 270 | |
| 271 } // namespace dart | 257 } // namespace dart |
| OLD | NEW |