Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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_api.h" | 5 #include "include/dart_api.h" |
| 6 | 6 |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 Isolate::Current()->object_store()->sticky_error()); | 76 Isolate::Current()->object_store()->sticky_error()); |
| 77 const char* errmsg = error.ToCString(); | 77 const char* errmsg = error.ToCString(); |
| 78 intptr_t errlen = strlen(errmsg) + 1; | 78 intptr_t errlen = strlen(errmsg) + 1; |
| 79 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen)); | 79 char* msg = reinterpret_cast<char*>(Api::Allocate(errlen)); |
| 80 OS::SNPrint(msg, errlen, "%s", errmsg); | 80 OS::SNPrint(msg, errlen, "%s", errmsg); |
| 81 result->type_ = kRetError; | 81 result->type_ = kRetError; |
| 82 result->retval_.errmsg = msg; | 82 result->retval_.errmsg = msg; |
| 83 } | 83 } |
| 84 | 84 |
| 85 | 85 |
| 86 DART_EXPORT void Dart_SetPostMessageCallback( | |
| 87 Dart_PostMessageCallback callback) { | |
| 88 Isolate* isolate = Isolate::Current(); | |
| 89 ASSERT(isolate != NULL); | |
| 90 ASSERT(callback != NULL); | |
| 91 isolate->set_post_message_callback(callback); | |
| 92 } | |
| 93 | |
| 94 | |
| 95 DART_EXPORT void Dart_SetClosePortCallback(Dart_ClosePortCallback callback) { | |
| 96 Isolate* isolate = Isolate::Current(); | |
| 97 ASSERT(isolate != NULL); | |
| 98 ASSERT(callback != NULL); | |
| 99 isolate->set_close_port_callback(callback); | |
| 100 } | |
| 101 | |
| 102 | |
| 103 static RawInstance* DeserializeMessage(void* data) { | |
| 104 // Create a snapshot object using the buffer. | |
| 105 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data); | |
| 106 ASSERT(snapshot->IsPartialSnapshot()); | |
| 107 | |
| 108 // Read object back from the snapshot. | |
| 109 Isolate* isolate= Isolate::Current(); | |
| 110 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | |
| 111 Instance& instance = Instance::Handle(); | |
| 112 instance ^= reader.ReadObject(); | |
| 113 return instance.raw(); | |
| 114 } | |
| 115 | |
| 116 | |
| 117 static void ProcessUnhandledException(const UnhandledException& uhe) { | |
| 118 const Instance& exception = Instance::Handle(uhe.exception()); | |
| 119 Instance& strtmp = Instance::Handle(DartLibraryCalls::ToString(exception)); | |
| 120 const char* str = strtmp.ToCString(); | |
| 121 fprintf(stderr, "%s\n", str); | |
| 122 const Instance& stack = Instance::Handle(uhe.stacktrace()); | |
| 123 strtmp = DartLibraryCalls::ToString(stack); | |
| 124 str = strtmp.ToCString(); | |
| 125 fprintf(stderr, "%s\n", str); | |
| 126 exit(255); | |
| 127 } | |
| 128 | |
| 129 | |
| 130 DART_EXPORT void Dart_HandleMessage(Dart_Port dest_port, | |
| 131 Dart_Port reply_port, | |
| 132 Dart_Message dart_message) { | |
| 133 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message)); | |
| 134 const String& class_name = | |
| 135 String::Handle(String::NewSymbol("ReceivePortImpl")); | |
| 136 const String& function_name = | |
| 137 String::Handle(String::NewSymbol("handleMessage_")); | |
| 138 const int kNumArguments = 3; | |
| 139 const Array& kNoArgumentNames = Array::Handle(); | |
| 140 const Function& function = Function::Handle( | |
| 141 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()), | |
| 142 class_name, | |
| 143 function_name, | |
| 144 kNumArguments, | |
| 145 kNoArgumentNames, | |
| 146 Resolver::kIsQualified)); | |
| 147 GrowableArray<const Object*> arguments(kNumArguments); | |
| 148 arguments.Add(&Integer::Handle(Integer::New(dest_port))); | |
| 149 arguments.Add(&Integer::Handle(Integer::New(reply_port))); | |
| 150 arguments.Add(&msg); | |
| 151 const Object& result = Object::Handle( | |
| 152 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); | |
| 153 if (result.IsUnhandledException()) { | |
| 154 UnhandledException& uhe = UnhandledException::Handle(); | |
| 155 uhe ^= result.raw(); | |
| 156 ProcessUnhandledException(uhe); | |
|
siva
2011/10/14 23:50:32
Not for this changelist but in a future changelist
turnidge
2011/10/17 18:38:48
Added TODO
| |
| 157 } | |
| 158 ASSERT(result.IsNull()); | |
| 159 } | |
| 160 | |
| 161 | |
| 86 DART_EXPORT Dart_Result Dart_RunLoop() { | 162 DART_EXPORT Dart_Result Dart_RunLoop() { |
| 87 Isolate* isolate = Isolate::Current(); | 163 Isolate* isolate = Isolate::Current(); |
| 88 LongJump* base = isolate->long_jump_base(); | 164 LongJump* base = isolate->long_jump_base(); |
| 89 LongJump jump; | 165 LongJump jump; |
| 90 Dart_Result result; | 166 Dart_Result result; |
| 91 isolate->set_long_jump_base(&jump); | 167 isolate->set_long_jump_base(&jump); |
| 92 if (setjmp(*jump.Set()) == 0) { | 168 if (setjmp(*jump.Set()) == 0) { |
| 93 // Keep listening until there are no active receive ports. | 169 // Keep listening until there are no active receive ports. |
| 94 while (isolate->active_ports() > 0) { | 170 isolate->StandardRunLoop(); |
| 95 Zone zone; | |
| 96 HandleScope handle_scope; | |
| 97 | 171 |
| 98 PortMessage* message = PortMap::ReceiveMessage(0); | |
| 99 message->Handle(); | |
| 100 delete message; | |
| 101 } | |
| 102 result.type_ = kRetCBool; | 172 result.type_ = kRetCBool; |
| 103 result.retval_.bool_value = true; | 173 result.retval_.bool_value = true; |
| 104 } else { | 174 } else { |
| 105 Zone zone; | 175 Zone zone; |
| 106 HandleScope handle_scope; | 176 HandleScope handle_scope; |
| 107 SetupErrorResult(&result); | 177 SetupErrorResult(&result); |
| 108 } | 178 } |
| 109 isolate->set_long_jump_base(base); | 179 isolate->set_long_jump_base(base); |
| 110 return result; | 180 return result; |
| 111 } | 181 } |
| (...skipping 1371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1483 | 1553 |
| 1484 | 1554 |
| 1485 DART_EXPORT Dart_Result Dart_PostIntArray(Dart_Port port, | 1555 DART_EXPORT Dart_Result Dart_PostIntArray(Dart_Port port, |
| 1486 int field_count, | 1556 int field_count, |
| 1487 intptr_t* data) { | 1557 intptr_t* data) { |
| 1488 uint8_t* buffer = NULL; | 1558 uint8_t* buffer = NULL; |
| 1489 MessageWriter writer(&buffer, &allocator); | 1559 MessageWriter writer(&buffer, &allocator); |
| 1490 | 1560 |
| 1491 writer.WriteMessage(field_count, data); | 1561 writer.WriteMessage(field_count, data); |
| 1492 | 1562 |
| 1493 // Post the message at the given port. The allocated message and | 1563 // Post the message at the given port. |
| 1494 // buffer are deallocated after the message is processed. | 1564 bool result = PortMap::PostMessage(port, 0, buffer); |
|
Anton Muhin
2011/10/17 15:20:29
0 -> kCloseAll?
turnidge
2011/10/17 18:38:48
I have added a kNoReplyPort constant which means t
| |
| 1495 PortMessage* portMessage = new PortMessage(port, 0, buffer); | |
| 1496 bool result = PortMap::PostMessage(portMessage); | |
| 1497 RETURN_CBOOLEAN(result); | 1565 RETURN_CBOOLEAN(result); |
| 1498 } | 1566 } |
| 1499 | 1567 |
| 1500 | 1568 |
| 1501 DART_EXPORT Dart_Result Dart_Post(Dart_Port port, Dart_Handle handle) { | 1569 DART_EXPORT Dart_Result Dart_Post(Dart_Port port, Dart_Handle handle) { |
| 1502 Zone zone; // Setup a VM zone as we are creating some handles. | 1570 Zone zone; // Setup a VM zone as we are creating some handles. |
| 1503 HandleScope scope; // Setup a VM handle scope. | 1571 HandleScope scope; // Setup a VM handle scope. |
| 1504 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); | 1572 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); |
| 1505 uint8_t* data = NULL; | 1573 uint8_t* data = NULL; |
| 1506 SnapshotWriter writer(false, &data, &allocator); | 1574 SnapshotWriter writer(false, &data, &allocator); |
| 1507 writer.WriteObject(object.raw()); | 1575 writer.WriteObject(object.raw()); |
| 1508 writer.FinalizeBuffer(); | 1576 writer.FinalizeBuffer(); |
| 1509 PortMessage* message = new PortMessage(port, 0, data); | 1577 bool result = PortMap::PostMessage(port, 0, data); |
| 1510 bool result = PortMap::PostMessage(message); | |
| 1511 RETURN_CBOOLEAN(result); | 1578 RETURN_CBOOLEAN(result); |
| 1512 } | 1579 } |
| 1513 | 1580 |
| 1514 | 1581 |
| 1515 DART_EXPORT void Dart_InitPprofSupport() { | 1582 DART_EXPORT void Dart_InitPprofSupport() { |
| 1516 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator(); | 1583 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator(); |
| 1517 ASSERT(pprof_symbol_generator != NULL); | 1584 ASSERT(pprof_symbol_generator != NULL); |
| 1518 Dart::set_pprof_symbol_generator(pprof_symbol_generator); | 1585 Dart::set_pprof_symbol_generator(pprof_symbol_generator); |
| 1519 } | 1586 } |
| 1520 | 1587 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1608 ASSERT(isolate != NULL); | 1675 ASSERT(isolate != NULL); |
| 1609 ApiState* state = isolate->api_state(); | 1676 ApiState* state = isolate->api_state(); |
| 1610 ASSERT(state != NULL); | 1677 ASSERT(state != NULL); |
| 1611 ApiLocalScope* scope = state->top_scope(); | 1678 ApiLocalScope* scope = state->top_scope(); |
| 1612 ASSERT(scope != NULL); | 1679 ASSERT(scope != NULL); |
| 1613 return scope->zone().Reallocate(ptr, old_size, new_size); | 1680 return scope->zone().Reallocate(ptr, old_size, new_size); |
| 1614 } | 1681 } |
| 1615 | 1682 |
| 1616 | 1683 |
| 1617 } // namespace dart | 1684 } // namespace dart |
| OLD | NEW |