Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(520)

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 8297004: Allow embedders to provide custom message delivery for an isolate. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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_SetMessageCallbacks(
87 Dart_PostMessageCallback post_message_callback,
88 Dart_ClosePortCallback close_port_callback) {
89 Isolate* isolate = Isolate::Current();
90 ASSERT(isolate != NULL);
91 ASSERT(post_message_callback != NULL);
92 ASSERT(close_port_callback != NULL);
93 isolate->set_post_message_callback(post_message_callback);
94 isolate->set_close_port_callback(close_port_callback);
95 }
96
97
98 static RawInstance* DeserializeMessage(void* data) {
99 // Create a snapshot object using the buffer.
100 const Snapshot* snapshot = Snapshot::SetupFromBuffer(data);
101 ASSERT(snapshot->IsPartialSnapshot());
102
103 // Read object back from the snapshot.
104 Isolate* isolate= Isolate::Current();
Anton Muhin 2011/10/18 17:19:58 nit: space before =
turnidge 2011/10/18 17:33:40 Fixed.
105 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store());
106 Instance& instance = Instance::Handle();
107 instance ^= reader.ReadObject();
108 return instance.raw();
109 }
110
111
112 static void ProcessUnhandledException(const UnhandledException& uhe) {
113 const Instance& exception = Instance::Handle(uhe.exception());
114 Instance& strtmp = Instance::Handle(DartLibraryCalls::ToString(exception));
115 const char* str = strtmp.ToCString();
116 fprintf(stderr, "%s\n", str);
117 const Instance& stack = Instance::Handle(uhe.stacktrace());
118 strtmp = DartLibraryCalls::ToString(stack);
119 str = strtmp.ToCString();
120 fprintf(stderr, "%s\n", str);
121 exit(255);
122 }
123
124
125 DART_EXPORT void Dart_HandleMessage(Dart_Port dest_port,
126 Dart_Port reply_port,
127 Dart_Message dart_message) {
128 const Instance& msg = Instance::Handle(DeserializeMessage(dart_message));
129 const String& class_name =
130 String::Handle(String::NewSymbol("ReceivePortImpl"));
131 const String& function_name =
132 String::Handle(String::NewSymbol("handleMessage_"));
133 const int kNumArguments = 3;
134 const Array& kNoArgumentNames = Array::Handle();
135 const Function& function = Function::Handle(
136 Resolver::ResolveStatic(Library::Handle(Library::CoreLibrary()),
137 class_name,
138 function_name,
139 kNumArguments,
140 kNoArgumentNames,
141 Resolver::kIsQualified));
142 GrowableArray<const Object*> arguments(kNumArguments);
143 arguments.Add(&Integer::Handle(Integer::New(dest_port)));
144 arguments.Add(&Integer::Handle(Integer::New(reply_port)));
145 arguments.Add(&msg);
146 const Object& result = Object::Handle(
147 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames));
148 if (result.IsUnhandledException()) {
149 UnhandledException& uhe = UnhandledException::Handle();
150 uhe ^= result.raw();
151 // TODO(turnidge): Instead of exiting here, just return the
152 // exception so taht eh embedder can choose how to handle this
Anton Muhin 2011/10/18 17:19:58 nit: so [that] [the] embedder can choose
turnidge 2011/10/18 17:33:40 Done.
153 // case.
154 ProcessUnhandledException(uhe);
155 }
156 ASSERT(result.IsNull());
157 }
158
159
86 DART_EXPORT Dart_Result Dart_RunLoop() { 160 DART_EXPORT Dart_Result Dart_RunLoop() {
87 Isolate* isolate = Isolate::Current(); 161 Isolate* isolate = Isolate::Current();
88 LongJump* base = isolate->long_jump_base(); 162 LongJump* base = isolate->long_jump_base();
89 LongJump jump; 163 LongJump jump;
90 Dart_Result result; 164 Dart_Result result;
91 isolate->set_long_jump_base(&jump); 165 isolate->set_long_jump_base(&jump);
92 if (setjmp(*jump.Set()) == 0) { 166 if (setjmp(*jump.Set()) == 0) {
93 // Keep listening until there are no active receive ports. 167 // Keep listening until there are no active receive ports.
Anton Muhin 2011/10/18 17:19:58 nit: ditto for comment.
turnidge 2011/10/18 17:33:40 Done.
94 while (isolate->active_ports() > 0) { 168 isolate->StandardRunLoop();
95 Zone zone;
96 HandleScope handle_scope;
97 169
98 PortMessage* message = PortMap::ReceiveMessage(0);
99 message->Handle();
100 delete message;
101 }
102 result.type_ = kRetCBool; 170 result.type_ = kRetCBool;
103 result.retval_.bool_value = true; 171 result.retval_.bool_value = true;
104 } else { 172 } else {
105 Zone zone; 173 Zone zone;
106 HandleScope handle_scope; 174 HandleScope handle_scope;
107 SetupErrorResult(&result); 175 SetupErrorResult(&result);
108 } 176 }
109 isolate->set_long_jump_base(base); 177 isolate->set_long_jump_base(base);
110 return result; 178 return result;
111 } 179 }
(...skipping 1371 matching lines...) Expand 10 before | Expand all | Expand 10 after
1483 1551
1484 1552
1485 DART_EXPORT Dart_Result Dart_PostIntArray(Dart_Port port, 1553 DART_EXPORT Dart_Result Dart_PostIntArray(Dart_Port port,
1486 int field_count, 1554 int field_count,
1487 intptr_t* data) { 1555 intptr_t* data) {
1488 uint8_t* buffer = NULL; 1556 uint8_t* buffer = NULL;
1489 MessageWriter writer(&buffer, &allocator); 1557 MessageWriter writer(&buffer, &allocator);
1490 1558
1491 writer.WriteMessage(field_count, data); 1559 writer.WriteMessage(field_count, data);
1492 1560
1493 // Post the message at the given port. The allocated message and 1561 // Post the message at the given port.
1494 // buffer are deallocated after the message is processed. 1562 bool result = PortMap::PostMessage(port, kNoReplyPort, buffer);
1495 PortMessage* portMessage = new PortMessage(port, 0, buffer);
1496 bool result = PortMap::PostMessage(portMessage);
1497 RETURN_CBOOLEAN(result); 1563 RETURN_CBOOLEAN(result);
1498 } 1564 }
1499 1565
1500 1566
1501 DART_EXPORT Dart_Result Dart_Post(Dart_Port port, Dart_Handle handle) { 1567 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. 1568 Zone zone; // Setup a VM zone as we are creating some handles.
1503 HandleScope scope; // Setup a VM handle scope. 1569 HandleScope scope; // Setup a VM handle scope.
1504 const Object& object = Object::Handle(Api::UnwrapHandle(handle)); 1570 const Object& object = Object::Handle(Api::UnwrapHandle(handle));
1505 uint8_t* data = NULL; 1571 uint8_t* data = NULL;
1506 SnapshotWriter writer(false, &data, &allocator); 1572 SnapshotWriter writer(false, &data, &allocator);
1507 writer.WriteObject(object.raw()); 1573 writer.WriteObject(object.raw());
1508 writer.FinalizeBuffer(); 1574 writer.FinalizeBuffer();
1509 PortMessage* message = new PortMessage(port, 0, data); 1575 bool result = PortMap::PostMessage(port, kNoReplyPort, data);
1510 bool result = PortMap::PostMessage(message);
1511 RETURN_CBOOLEAN(result); 1576 RETURN_CBOOLEAN(result);
1512 } 1577 }
1513 1578
1514 1579
1515 DART_EXPORT void Dart_InitPprofSupport() { 1580 DART_EXPORT void Dart_InitPprofSupport() {
1516 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator(); 1581 DebugInfo* pprof_symbol_generator = DebugInfo::NewGenerator();
1517 ASSERT(pprof_symbol_generator != NULL); 1582 ASSERT(pprof_symbol_generator != NULL);
1518 Dart::set_pprof_symbol_generator(pprof_symbol_generator); 1583 Dart::set_pprof_symbol_generator(pprof_symbol_generator);
1519 } 1584 }
1520 1585
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 ASSERT(isolate != NULL); 1673 ASSERT(isolate != NULL);
1609 ApiState* state = isolate->api_state(); 1674 ApiState* state = isolate->api_state();
1610 ASSERT(state != NULL); 1675 ASSERT(state != NULL);
1611 ApiLocalScope* scope = state->top_scope(); 1676 ApiLocalScope* scope = state->top_scope();
1612 ASSERT(scope != NULL); 1677 ASSERT(scope != NULL);
1613 return scope->zone().Reallocate(ptr, old_size, new_size); 1678 return scope->zone().Reallocate(ptr, old_size, new_size);
1614 } 1679 }
1615 1680
1616 1681
1617 } // namespace dart 1682 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698