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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 113 |
114 | 114 |
115 DEFINE_NATIVE_ENTRY(ReceivePortImpl_factory, 1) { | 115 DEFINE_NATIVE_ENTRY(ReceivePortImpl_factory, 1) { |
116 ASSERT(AbstractTypeArguments::CheckedHandle(arguments->At(0)).IsNull()); | 116 ASSERT(AbstractTypeArguments::CheckedHandle(arguments->At(0)).IsNull()); |
117 intptr_t port_id = | 117 intptr_t port_id = |
118 PortMap::CreatePort(arguments->isolate()->message_handler()); | 118 PortMap::CreatePort(arguments->isolate()->message_handler()); |
119 const Object& port = Object::Handle(ReceivePortCreate(port_id)); | 119 const Object& port = Object::Handle(ReceivePortCreate(port_id)); |
120 if (port.IsError()) { | 120 if (port.IsError()) { |
121 Exceptions::PropagateError(Error::Cast(port)); | 121 Exceptions::PropagateError(Error::Cast(port)); |
122 } | 122 } |
123 arguments->SetReturn(port); | 123 return port.raw(); |
124 } | 124 } |
125 | 125 |
126 | 126 |
127 DEFINE_NATIVE_ENTRY(ReceivePortImpl_closeInternal, 1) { | 127 DEFINE_NATIVE_ENTRY(ReceivePortImpl_closeInternal, 1) { |
128 GET_NATIVE_ARGUMENT(Smi, id, arguments->At(0)); | 128 GET_NATIVE_ARGUMENT(Smi, id, arguments->At(0)); |
129 PortMap::ClosePort(id.Value()); | 129 PortMap::ClosePort(id.Value()); |
| 130 return Object::null(); |
130 } | 131 } |
131 | 132 |
132 | 133 |
133 DEFINE_NATIVE_ENTRY(SendPortImpl_sendInternal_, 3) { | 134 DEFINE_NATIVE_ENTRY(SendPortImpl_sendInternal_, 3) { |
134 GET_NATIVE_ARGUMENT(Smi, send_id, arguments->At(0)); | 135 GET_NATIVE_ARGUMENT(Smi, send_id, arguments->At(0)); |
135 GET_NATIVE_ARGUMENT(Smi, reply_id, arguments->At(1)); | 136 GET_NATIVE_ARGUMENT(Smi, reply_id, arguments->At(1)); |
136 // TODO(iposva): Allow for arbitrary messages to be sent. | 137 // TODO(iposva): Allow for arbitrary messages to be sent. |
137 GET_NATIVE_ARGUMENT(Instance, obj, arguments->At(2)); | 138 GET_NATIVE_ARGUMENT(Instance, obj, arguments->At(2)); |
138 | 139 |
139 uint8_t* data = NULL; | 140 uint8_t* data = NULL; |
140 MessageWriter writer(&data, &allocator); | 141 MessageWriter writer(&data, &allocator); |
141 writer.WriteMessage(obj); | 142 writer.WriteMessage(obj); |
142 | 143 |
143 // TODO(turnidge): Throw an exception when the return value is false? | 144 // TODO(turnidge): Throw an exception when the return value is false? |
144 PortMap::PostMessage(new Message(send_id.Value(), reply_id.Value(), | 145 PortMap::PostMessage(new Message(send_id.Value(), reply_id.Value(), |
145 data, writer.BytesWritten(), | 146 data, writer.BytesWritten(), |
146 Message::kNormalPriority)); | 147 Message::kNormalPriority)); |
| 148 return Object::null(); |
147 } | 149 } |
148 | 150 |
149 | 151 |
150 static void ThrowIllegalArgException(const String& message) { | 152 static void ThrowIllegalArgException(const String& message) { |
151 GrowableArray<const Object*> args(1); | 153 GrowableArray<const Object*> args(1); |
152 args.Add(&message); | 154 args.Add(&message); |
153 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | 155 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
154 } | 156 } |
155 | 157 |
156 | 158 |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 result = DartEntry::InvokeStatic(func, args, kNoArgNames); | 361 result = DartEntry::InvokeStatic(func, args, kNoArgNames); |
360 if (result.IsError()) { | 362 if (result.IsError()) { |
361 StoreError(isolate, result); | 363 StoreError(isolate, result); |
362 return false; | 364 return false; |
363 } | 365 } |
364 } | 366 } |
365 return true; | 367 return true; |
366 } | 368 } |
367 | 369 |
368 | 370 |
369 static void Spawn(NativeArguments* arguments, SpawnState* state) { | 371 static RawObject* Spawn(NativeArguments* arguments, SpawnState* state) { |
370 // Create a new isolate. | 372 // Create a new isolate. |
371 char* error = NULL; | 373 char* error = NULL; |
372 if (!CreateIsolate(state, &error)) { | 374 if (!CreateIsolate(state, &error)) { |
373 delete state; | 375 delete state; |
374 const String& msg = String::Handle(String::New(error)); | 376 const String& msg = String::Handle(String::New(error)); |
375 free(error); | 377 free(error); |
376 ThrowIsolateSpawnException(msg); | 378 ThrowIsolateSpawnException(msg); |
377 } | 379 } |
378 | 380 |
379 // Try to create a SendPort for the new isolate. | 381 // Try to create a SendPort for the new isolate. |
380 const Object& port = Object::Handle( | 382 const Object& port = Object::Handle( |
381 DartLibraryCalls::NewSendPort(state->isolate()->main_port())); | 383 DartLibraryCalls::NewSendPort(state->isolate()->main_port())); |
382 if (port.IsError()) { | 384 if (port.IsError()) { |
383 state->Cleanup(); | 385 state->Cleanup(); |
384 delete state; | 386 delete state; |
385 Exceptions::PropagateError(Error::Cast(port)); | 387 Exceptions::PropagateError(Error::Cast(port)); |
386 } | 388 } |
387 | 389 |
388 // Start the new isolate. | 390 // Start the new isolate. |
389 state->isolate()->set_spawn_data(reinterpret_cast<uword>(state)); | 391 state->isolate()->set_spawn_data(reinterpret_cast<uword>(state)); |
390 state->isolate()->message_handler()->Run( | 392 state->isolate()->message_handler()->Run( |
391 Dart::thread_pool(), RunIsolate, ShutdownIsolate, | 393 Dart::thread_pool(), RunIsolate, ShutdownIsolate, |
392 reinterpret_cast<uword>(state->isolate())); | 394 reinterpret_cast<uword>(state->isolate())); |
393 | 395 |
394 arguments->SetReturn(port); | 396 return port.raw(); |
395 } | 397 } |
396 | 398 |
397 | 399 |
398 DEFINE_NATIVE_ENTRY(isolate_spawnFunction, 1) { | 400 DEFINE_NATIVE_ENTRY(isolate_spawnFunction, 1) { |
399 GET_NATIVE_ARGUMENT(Closure, closure, arguments->At(0)); | 401 GET_NATIVE_ARGUMENT(Closure, closure, arguments->At(0)); |
400 const Function& func = Function::Handle(closure.function()); | 402 const Function& func = Function::Handle(closure.function()); |
401 const Class& cls = Class::Handle(func.Owner()); | 403 const Class& cls = Class::Handle(func.Owner()); |
402 if (!func.IsClosureFunction() || !func.is_static() || !cls.IsTopLevel()) { | 404 if (!func.IsClosureFunction() || !func.is_static() || !cls.IsTopLevel()) { |
403 const String& msg = String::Handle(String::New( | 405 const String& msg = String::Handle(String::New( |
404 "spawnFunction expects to be passed a closure to a top-level static " | 406 "spawnFunction expects to be passed a closure to a top-level static " |
405 "function")); | 407 "function")); |
406 ThrowIllegalArgException(msg); | 408 ThrowIllegalArgException(msg); |
407 } | 409 } |
408 | 410 |
409 #if defined(DEBUG) | 411 #if defined(DEBUG) |
410 const Context& ctx = Context::Handle(closure.context()); | 412 const Context& ctx = Context::Handle(closure.context()); |
411 ASSERT(ctx.num_variables() == 0); | 413 ASSERT(ctx.num_variables() == 0); |
412 #endif | 414 #endif |
413 | 415 |
414 Spawn(arguments, new SpawnState(func)); | 416 return Spawn(arguments, new SpawnState(func)); |
415 } | 417 } |
416 | 418 |
417 | 419 |
418 DEFINE_NATIVE_ENTRY(isolate_spawnUri, 1) { | 420 DEFINE_NATIVE_ENTRY(isolate_spawnUri, 1) { |
419 GET_NATIVE_ARGUMENT(String, uri, arguments->At(0)); | 421 GET_NATIVE_ARGUMENT(String, uri, arguments->At(0)); |
420 | 422 |
421 // Canonicalize the uri with respect to the current isolate. | 423 // Canonicalize the uri with respect to the current isolate. |
422 char* error = NULL; | 424 char* error = NULL; |
423 char* canonical_uri = NULL; | 425 char* canonical_uri = NULL; |
424 const Library& root_lib = | 426 const Library& root_lib = |
425 Library::Handle(arguments->isolate()->object_store()->root_library()); | 427 Library::Handle(arguments->isolate()->object_store()->root_library()); |
426 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, | 428 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, |
427 &canonical_uri, &error)) { | 429 &canonical_uri, &error)) { |
428 const String& msg = String::Handle(String::New(error)); | 430 const String& msg = String::Handle(String::New(error)); |
429 free(error); | 431 free(error); |
430 ThrowIsolateSpawnException(msg); | 432 ThrowIsolateSpawnException(msg); |
431 } | 433 } |
432 | 434 |
433 Spawn(arguments, new SpawnState(canonical_uri)); | 435 return Spawn(arguments, new SpawnState(canonical_uri)); |
434 } | 436 } |
435 | 437 |
436 | 438 |
437 DEFINE_NATIVE_ENTRY(isolate_getPortInternal, 0) { | 439 DEFINE_NATIVE_ENTRY(isolate_getPortInternal, 0) { |
438 const Object& port = Object::Handle(ReceivePortCreate(isolate->main_port())); | 440 const Object& port = Object::Handle(ReceivePortCreate(isolate->main_port())); |
439 if (port.IsError()) { | 441 if (port.IsError()) { |
440 Exceptions::PropagateError(Error::Cast(port)); | 442 Exceptions::PropagateError(Error::Cast(port)); |
441 } | 443 } |
442 arguments->SetReturn(port); | 444 return port.raw(); |
443 } | 445 } |
444 | 446 |
445 } // namespace dart | 447 } // namespace dart |
OLD | NEW |